Advertisement
niedzwiedzw

Untitled

Dec 11th, 2022
1,372
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 2.78 KB | None | 0 0
  1.  
  2. struct DataConfiguration {
  3.     destination_resolution: vec2<f32>,
  4.     source_resolution: vec2<f32>,
  5. };
  6.  
  7. @group(0) @binding(0) var<uniform> data_configuration: DataConfiguration;
  8. @group(0) @binding(1) var source_texture: texture_2d<f32>;
  9. @group(0) @binding(2) var sampler_nearest: sampler;
  10.  
  11. // VERTEX SHADER.
  12.  
  13. struct DataVertexInput {
  14.     @location(0) position: vec2<f32>,
  15.     @location(1) uv: vec2<f32>,
  16.     @location(2) color: vec4<f32>,
  17.     @location(3) clip: vec4<f32>,
  18. };
  19.  
  20. struct DataVertexOutput {
  21.     @builtin(position) position: vec4<f32>,
  22.     @location(0) uv: vec2<f32>,
  23.     @location(1) color: vec4<f32>,
  24.     @location(2) clip: vec4<f32>,
  25. };
  26.  
  27. @vertex
  28. fn vs_main(
  29.     data_vertex_input: DataVertexInput,
  30. ) -> DataVertexOutput
  31. {
  32.     let destination_resolution = vec2<f32>(
  33.         data_configuration.destination_resolution.x,
  34.         data_configuration.destination_resolution.y
  35.     );
  36.     let destination_resolution_half = vec2<f32>(
  37.         data_configuration.destination_resolution.x / 2.0,
  38.         data_configuration.destination_resolution.y / 2.0
  39.     );
  40.     // Generate result for output position and fragment data.
  41.     // Vertex position is configured to start at the top-left and to end at the bottom-right.
  42.     var data_vertex_output: DataVertexOutput;
  43.     data_vertex_output.position = vec4<f32>(
  44.         (data_vertex_input.position.x - destination_resolution_half.x) / destination_resolution_half.x,
  45.         (-1.0 * data_vertex_input.position.y + destination_resolution_half.y) / destination_resolution_half.y,
  46.         0.0,
  47.         1.0
  48.     );
  49.     //data_vertex_output.position = vec4<f32>(
  50.     //    data_vertex_input.position.x / destination_resolution_half.x,
  51.     //    (-1.0 * data_vertex_input.position.y) / destination_resolution_half.y,
  52.     //    0.0,
  53.     //    1.0
  54.     //);
  55.     data_vertex_output.uv = data_vertex_input.uv;
  56.     data_vertex_output.color = data_vertex_input.color;
  57.     data_vertex_output.clip = data_vertex_input.clip;
  58.     return data_vertex_output;
  59. }
  60.  
  61. // FRAGMENT SHADER.
  62.  
  63. struct DataFragmentInput {
  64.     @location(0) uv: vec2<f32>,
  65.     @location(1) color: vec4<f32>,
  66.     @location(2) clip: vec4<f32>,
  67. };
  68.  
  69. struct DataFragmentOutput {
  70.     @location(0) output_color: vec4<f32>,
  71. };
  72.  
  73. @fragment
  74. fn fs_main(data_fragment: DataFragmentInput) -> DataFragmentOutput
  75. {
  76.     let uv_normalized = vec2<f32>(
  77.         data_fragment.uv.x / data_configuration.source_resolution.x,
  78.         data_fragment.uv.y / data_configuration.source_resolution.y
  79.     );
  80.     let texture_color = textureSample(source_texture, sampler_nearest, uv_normalized);
  81.     // Generate result for output color.
  82.     var data_vertex_output: DataFragmentOutput;
  83.     data_vertex_output.output_color = texture_color * data_fragment.color;
  84.     return data_vertex_output;
  85. }
  86.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement