WebGPUTextureUtils constructor

WebGPUTextureUtils(
  1. dynamic device
)

Implementation

WebGPUTextureUtils(device) {
  this.device = device;

  var mipmapVertexSource = """
struct VarysStruct {

	[[ builtin( position ) ]] Position: vec4<f32>,
	[[ location( 0 ) ]] vTex : vec2<f32>

};

[[ stage( vertex ) ]]
fn main( [[ builtin( vertex_index ) ]] vertexIndex : u32 ) -> VarysStruct {

	var Varys: VarysStruct;

	var pos = array< vec2<f32>, 4 >(
		vec2<f32>( -1.0,  1.0 ),
		vec2<f32>(  1.0,  1.0 ),
		vec2<f32>( -1.0, -1.0 ),
		vec2<f32>(  1.0, -1.0 )
	);

	var tex = array< vec2<f32>, 4 >(
		vec2<f32>( 0.0, 0.0 ),
		vec2<f32>( 1.0, 0.0 ),
		vec2<f32>( 0.0, 1.0 ),
		vec2<f32>( 1.0, 1.0 )
	);

	Varys.vTex = tex[ vertexIndex ];
	Varys.Position = vec4<f32>( pos[ vertexIndex ], 0.0, 1.0 );

	return Varys;

}
""";

  var mipmapFragmentSource = """
[[ group( 0 ), binding( 0 ) ]]
var imgSampler : sampler;

[[ group( 0 ), binding( 1 ) ]]
var img : texture_2d<f32>;

[[ stage( fragment ) ]]
fn main( [[ location( 0 ) ]] vTex : vec2<f32> ) -> [[ location( 0 ) ]] vec4<f32> {

	return textureSample( img, imgSampler, vTex );

}
""";

  this.sampler = device.createSampler({"minFilter": GPUFilterMode.Linear});

  // We'll need a new pipeline for every texture format used.
  this.pipelines = {};

  this.mipmapVertexShaderModule = device.createShaderModule(
      GPUShaderModuleDescriptor(code: mipmapVertexSource));

  this.mipmapFragmentShaderModule = device.createShaderModule(
      GPUShaderModuleDescriptor(code: mipmapFragmentSource));
}