comparison tests/test_mesh.nim @ 730:88ab3e6dcaa0

add: simpler shader construction
author Sam <sam@basx.dev>
date Sun, 28 May 2023 18:36:11 +0700
parents 7b72da29ad8d
children da0bd61abe91
comparison
equal deleted inserted replaced
729:7b72da29ad8d 730:88ab3e6dcaa0
22 attr[Vec3f]("position", memoryPerformanceHint=PreferFastRead), 22 attr[Vec3f]("position", memoryPerformanceHint=PreferFastRead),
23 attr[uint8]("material", memoryPerformanceHint=PreferFastRead), 23 attr[uint8]("material", memoryPerformanceHint=PreferFastRead),
24 attr[Vec2f]("texcoord_0", memoryPerformanceHint=PreferFastRead), 24 attr[Vec2f]("texcoord_0", memoryPerformanceHint=PreferFastRead),
25 attr[Mat4]("transform", memoryPerformanceHint=PreferFastWrite, perInstance=true), 25 attr[Mat4]("transform", memoryPerformanceHint=PreferFastWrite, perInstance=true),
26 ] 26 ]
27 vertexOutput = @[ 27 intermediate = @[
28 attr[Vec4f]("vertexColor"), 28 attr[Vec4f]("vertexColor"),
29 attr[Vec2f]("colorTexCoord"), 29 attr[Vec2f]("colorTexCoord"),
30 attr[uint8]("materialId", noInterpolation=true) 30 attr[uint8]("materialId", noInterpolation=true)
31 ] 31 ]
32 fragOutput = @[attr[Vec4f]("color")] 32 fragOutput = @[attr[Vec4f]("color")]
33 uniforms = @[ 33 uniforms = @[
34 attr[Mat4]("projection"), 34 attr[Mat4]("projection"),
35 attr[Mat4]("view"), 35 attr[Mat4]("view"),
36 attr[Vec4f]("baseColorFactor", arrayCount=4), 36 attr[Vec4f]("baseColorFactor", arrayCount=4),
37 ] 37 ]
38 samplers = @[ 38 samplers = @[attr[Sampler2DType]("baseColorTexture", arrayCount=4)]
39 attr[Sampler2DType]("baseColorTexture", arrayCount=4), 39 (vertexCode, fragmentCode) = compileVertexFragmentShaderSet(
40 ]
41 vertexCode = compileGlslShader(
42 stage=VK_SHADER_STAGE_VERTEX_BIT,
43 inputs=vertexInput, 40 inputs=vertexInput,
44 outputs=vertexOutput, 41 intermediate=intermediate,
42 outputs=fragOutput,
45 uniforms=uniforms, 43 uniforms=uniforms,
46 samplers=samplers, 44 samplers=samplers,
47 main=""" 45 vertexCode="""
48 gl_Position = vec4(position, 1.0) * (transform * Uniforms.view * Uniforms.projection); 46 gl_Position = vec4(position, 1.0) * (transform * Uniforms.view * Uniforms.projection);
49 vertexColor = Uniforms.baseColorFactor[material]; 47 vertexColor = Uniforms.baseColorFactor[material];
50 colorTexCoord = texcoord_0; 48 colorTexCoord = texcoord_0;
51 materialId = material; 49 materialId = material;
52 """ 50 """,
53 ) 51 fragmentCode="""
54 fragmentCode = compileGlslShader(
55 stage=VK_SHADER_STAGE_FRAGMENT_BIT,
56 inputs=vertexOutput,
57 outputs=fragOutput,
58 uniforms=uniforms,
59 samplers=samplers,
60 main="""
61 vec4 col[4] = vec4[4](vec4(1, 0, 0, 1), vec4(0, 1, 0, 1), vec4(0, 0, 1, 1), vec4(1, 1, 1, 1)); 52 vec4 col[4] = vec4[4](vec4(1, 0, 0, 1), vec4(0, 1, 0, 1), vec4(0, 0, 1, 1), vec4(1, 1, 1, 1));
62 color = texture(baseColorTexture[materialId], colorTexCoord) * vertexColor; 53 color = texture(baseColorTexture[materialId], colorTexCoord) * vertexColor;
63 """ 54 """
64 ) 55 )
65 engine.setRenderer(engine.gpuDevice.simpleForwardRenderPass(vertexCode, fragmentCode, clearColor=newVec4f(0, 0, 0, 1))) 56 engine.setRenderer(engine.gpuDevice.simpleForwardRenderPass(vertexCode, fragmentCode, clearColor=newVec4f(0, 0, 0, 1)))