comparison tests/test_vulkan_wrapper.nim @ 672:d7c61e6580ea

did: refactor image handling
author Sam <sam@basx.dev>
date Tue, 09 May 2023 19:52:33 +0700
parents ac7dfbd56cc6
children 3cbbf50e9e4c
comparison
equal deleted inserted replaced
671:d84b2e88776a 672:d7c61e6580ea
35 35
36 proc scene_different_mesh_types(): Entity = 36 proc scene_different_mesh_types(): Entity =
37 result = newEntity("root", 37 result = newEntity("root",
38 newEntity("triangle1", newMesh( 38 newEntity("triangle1", newMesh(
39 positions=[newVec3f(0.0, -0.5), newVec3f(0.5, 0.5), newVec3f(-0.5, 0.5)], 39 positions=[newVec3f(0.0, -0.5), newVec3f(0.5, 0.5), newVec3f(-0.5, 0.5)],
40 colors=[newVec3f(1.0, 0.0, 0.0), newVec3f(0.0, 1.0, 0.0), newVec3f(0.0, 0.0, 1.0)], 40 colors=[newVec4f(1.0, 0.0, 0.0, 1), newVec4f(0.0, 1.0, 0.0, 1), newVec4f(0.0, 0.0, 1.0, 1)],
41 )), 41 )),
42 newEntity("triangle1b", newMesh( 42 newEntity("triangle1b", newMesh(
43 positions=[newVec3f(0.0, -0.4), newVec3f(0.4, 0.4), newVec3f(-0.4, 0.5)], 43 positions=[newVec3f(0.0, -0.4), newVec3f(0.4, 0.4), newVec3f(-0.4, 0.5)],
44 colors=[newVec3f(1.0, 0.0, 0.0), newVec3f(0.0, 1.0, 0.0), newVec3f(0.0, 0.0, 1.0)], 44 colors=[newVec4f(1.0, 0.0, 0.0, 1), newVec4f(0.0, 1.0, 0.0, 1), newVec4f(0.0, 0.0, 1.0, 1)],
45 )), 45 )),
46 newEntity("triangle2a", newMesh( 46 newEntity("triangle2a", newMesh(
47 positions=[newVec3f(0.0, 0.5), newVec3f(0.5, -0.5), newVec3f(-0.5, -0.5)], 47 positions=[newVec3f(0.0, 0.5), newVec3f(0.5, -0.5), newVec3f(-0.5, -0.5)],
48 colors=[newVec3f(1.0, 0.0, 0.0), newVec3f(0.0, 1.0, 0.0), newVec3f(0.0, 0.0, 1.0)], 48 colors=[newVec4f(1.0, 0.0, 0.0, 1), newVec4f(0.0, 1.0, 0.0, 1), newVec4f(0.0, 0.0, 1.0, 1)],
49 indices=[[0'u16, 2'u16, 1'u16]] 49 indices=[[0'u16, 2'u16, 1'u16]]
50 )), 50 )),
51 newEntity("triangle2b", newMesh( 51 newEntity("triangle2b", newMesh(
52 positions=[newVec3f(0.0, 0.4), newVec3f(0.4, -0.4), newVec3f(-0.4, -0.4)], 52 positions=[newVec3f(0.0, 0.4), newVec3f(0.4, -0.4), newVec3f(-0.4, -0.4)],
53 colors=[newVec3f(1.0, 0.0, 0.0), newVec3f(0.0, 1.0, 0.0), newVec3f(0.0, 0.0, 1.0)], 53 colors=[newVec4f(1.0, 0.0, 0.0, 1), newVec4f(0.0, 1.0, 0.0, 1), newVec4f(0.0, 0.0, 1.0, 1)],
54 indices=[[0'u16, 2'u16, 1'u16]] 54 indices=[[0'u16, 2'u16, 1'u16]]
55 )), 55 )),
56 newEntity("triangle3a", newMesh( 56 newEntity("triangle3a", newMesh(
57 positions=[newVec3f(0.4, 0.5), newVec3f(0.9, -0.3), newVec3f(0.0, -0.3)], 57 positions=[newVec3f(0.4, 0.5), newVec3f(0.9, -0.3), newVec3f(0.0, -0.3)],
58 colors=[newVec3f(1.0, 1.0, 0.0), newVec3f(1.0, 1.0, 0.0), newVec3f(1.0, 1.0, 0.0)], 58 colors=[newVec4f(1.0, 1.0, 0.0, 1), newVec4f(1.0, 1.0, 0.0, 1), newVec4f(1.0, 1.0, 0.0, 1)],
59 indices=[[0'u32, 2'u32, 1'u32]], 59 indices=[[0'u32, 2'u32, 1'u32]],
60 autoResize=false 60 autoResize=false
61 )), 61 )),
62 newEntity("triangle3b", newMesh( 62 newEntity("triangle3b", newMesh(
63 positions=[newVec3f(0.4, 0.5), newVec3f(0.9, -0.3), newVec3f(0.0, -0.3)], 63 positions=[newVec3f(0.4, 0.5), newVec3f(0.9, -0.3), newVec3f(0.0, -0.3)],
64 colors=[newVec3f(1.0, 1.0, 0.0), newVec3f(1.0, 1.0, 0.0), newVec3f(1.0, 1.0, 0.0)], 64 colors=[newVec4f(1.0, 1.0, 0.0, 1), newVec4f(1.0, 1.0, 0.0, 1), newVec4f(1.0, 1.0, 0.0, 1)],
65 indices=[[0'u32, 2'u32, 1'u32]], 65 indices=[[0'u32, 2'u32, 1'u32]],
66 autoResize=false 66 autoResize=false
67 )), 67 )),
68 ) 68 )
69 for mesh in allComponentsOfType[Mesh](result): 69 for mesh in allComponentsOfType[Mesh](result):
70 mesh.setInstanceData("translate", @[newVec3f()]) 70 mesh.setInstanceData("translate", @[newVec3f()])
71 71
72 proc scene_simple(): Entity = 72 proc scene_simple(): Entity =
73 var mymesh1 = newMesh( 73 var mymesh1 = newMesh(
74 positions=[newVec3f(0.0, -0.3), newVec3f(0.3, 0.3), newVec3f(-0.3, 0.3)], 74 positions=[newVec3f(0.0, -0.3), newVec3f(0.3, 0.3), newVec3f(-0.3, 0.3)],
75 colors=[newVec3f(1.0, 0.0, 0.0), newVec3f(0.0, 1.0, 0.0), newVec3f(0.0, 0.0, 1.0)], 75 colors=[newVec4f(1.0, 0.0, 0.0, 1), newVec4f(0.0, 1.0, 0.0, 1), newVec4f(0.0, 0.0, 1.0, 1)],
76 ) 76 )
77 var mymesh2 = newMesh( 77 var mymesh2 = newMesh(
78 positions=[newVec3f(0.0, -0.5), newVec3f(0.5, 0.5), newVec3f(-0.5, 0.5)], 78 positions=[newVec3f(0.0, -0.5), newVec3f(0.5, 0.5), newVec3f(-0.5, 0.5)],
79 colors=[newVec3f(1.0, 0.0, 0.0), newVec3f(0.0, 1.0, 0.0), newVec3f(0.0, 0.0, 1.0)], 79 colors=[newVec4f(1.0, 0.0, 0.0, 1), newVec4f(0.0, 1.0, 0.0, 1), newVec4f(0.0, 0.0, 1.0, 1)],
80 ) 80 )
81 var mymesh3 = newMesh( 81 var mymesh3 = newMesh(
82 positions=[newVec3f(0.0, -0.6), newVec3f(0.6, 0.6), newVec3f(-0.6, 0.6)], 82 positions=[newVec3f(0.0, -0.6), newVec3f(0.6, 0.6), newVec3f(-0.6, 0.6)],
83 colors=[newVec3f(1.0, 1.0, 0.0), newVec3f(1.0, 1.0, 0.0), newVec3f(1.0, 1.0, 0.0)], 83 colors=[newVec4f(1.0, 1.0, 0.0, 1), newVec4f(1.0, 1.0, 0.0, 1), newVec4f(1.0, 1.0, 0.0, 1)],
84 indices=[[0'u32, 1'u32, 2'u32]], 84 indices=[[0'u32, 1'u32, 2'u32]],
85 autoResize=false 85 autoResize=false
86 ) 86 )
87 var mymesh4 = newMesh( 87 var mymesh4 = newMesh(
88 positions=[newVec3f(0.0, -0.8), newVec3f(0.8, 0.8), newVec3f(-0.8, 0.8)], 88 positions=[newVec3f(0.0, -0.8), newVec3f(0.8, 0.8), newVec3f(-0.8, 0.8)],
89 colors=[newVec3f(0.0, 0.0, 1.0), newVec3f(0.0, 0.0, 1.0), newVec3f(0.0, 0.0, 1.0)], 89 colors=[newVec4f(0.0, 0.0, 1.0, 1), newVec4f(0.0, 0.0, 1.0, 1), newVec4f(0.0, 0.0, 1.0, 1)],
90 indices=[[0'u16, 1'u16, 2'u16]], 90 indices=[[0'u16, 1'u16, 2'u16]],
91 instanceCount=2 91 instanceCount=2
92 ) 92 )
93 mymesh1.setInstanceData("translate", @[newVec3f(0.3, 0.0)]) 93 mymesh1.setInstanceData("translate", @[newVec3f(0.3, 0.0)])
94 mymesh2.setInstanceData("translate", @[newVec3f(0.0, 0.3)]) 94 mymesh2.setInstanceData("translate", @[newVec3f(0.0, 0.3)])
105 c.setInstanceData("translate", @[newVec3f(-0.3, 0.1)]) 105 c.setInstanceData("translate", @[newVec3f(-0.3, 0.1)])
106 result = newEntity("root", t, r, c) 106 result = newEntity("root", t, r, c)
107 107
108 proc scene_flag(): Entity = 108 proc scene_flag(): Entity =
109 var r = rect(color="ff0000") 109 var r = rect(color="ff0000")
110 r.updateMeshData("color", @[newVec3f(0, 0), newVec3f(1, 0), newVec3f(1, 1), newVec3f(0, 1)]) 110 r.updateMeshData("color", @[newVec4f(0, 0), newVec4f(1, 0), newVec4f(1, 1), newVec4f(0, 1)])
111 result = newEntity("root", r) 111 result = newEntity("root", r)
112 112
113 proc main() = 113 proc main() =
114 var engine = initEngine("Test") 114 var engine = initEngine("Test")
115 115
116 # INIT RENDERER: 116 # INIT RENDERER:
117 const 117 const
118 vertexInput = @[ 118 vertexInput = @[
119 attr[Vec3f]("position", memoryPerformanceHint=PreferFastRead), 119 attr[Vec3f]("position", memoryPerformanceHint=PreferFastRead),
120 attr[Vec3f]("color", memoryPerformanceHint=PreferFastWrite), 120 attr[Vec4f]("color", memoryPerformanceHint=PreferFastWrite),
121 attr[Vec3f]("translate", perInstance=true) 121 attr[Vec3f]("translate", perInstance=true)
122 ] 122 ]
123 vertexOutput = @[attr[Vec3f]("outcolor")] 123 vertexOutput = @[attr[Vec4f]("outcolor")]
124 uniforms = @[attr[float32]("time")] 124 uniforms = @[attr[float32]("time")]
125 samplers = @[attr[Sampler2DType]("my_little_texture")] 125 samplers = @[attr[Sampler2DType]("my_little_texture")]
126 fragOutput = @[attr[Vec4f]("color")] 126 fragOutput = @[attr[Vec4f]("color")]
127 vertexCode = compileGlslShader( 127 vertexCode = compileGlslShader(
128 stage=VK_SHADER_STAGE_VERTEX_BIT, 128 stage=VK_SHADER_STAGE_VERTEX_BIT,
136 stage=VK_SHADER_STAGE_FRAGMENT_BIT, 136 stage=VK_SHADER_STAGE_FRAGMENT_BIT,
137 inputs=vertexOutput, 137 inputs=vertexOutput,
138 uniforms=uniforms, 138 uniforms=uniforms,
139 samplers=samplers, 139 samplers=samplers,
140 outputs=fragOutput, 140 outputs=fragOutput,
141 main="color = texture(my_little_texture, outcolor.xy) * 0.5 + vec4(outcolor, 1) * 0.5;" 141 main="color = texture(my_little_texture, outcolor.xy) * 0.5 + outcolor * 0.5;"
142 ) 142 )
143 var renderPass = engine.gpuDevice.simpleForwardRenderPass(vertexCode, fragmentCode) 143 var renderPass = engine.gpuDevice.simpleForwardRenderPass(vertexCode, fragmentCode)
144 engine.setRenderer(renderPass) 144 engine.setRenderer(renderPass)
145 145
146 # INIT SCENES 146 # INIT SCENES
151 newScene("flag", scene_flag()), 151 newScene("flag", scene_flag()),
152 ] 152 ]
153 for scene in scenes.mitems: 153 for scene in scenes.mitems:
154 scene.addShaderGlobal("time", 0.0'f32) 154 scene.addShaderGlobal("time", 0.0'f32)
155 let (R, W) = ([255'u8, 0'u8, 0'u8, 255'u8], [255'u8, 255'u8, 255'u8, 255'u8]) 155 let (R, W) = ([255'u8, 0'u8, 0'u8, 255'u8], [255'u8, 255'u8, 255'u8, 255'u8])
156 scene.addTexture("my_little_texture", TextureImage(width: 5, height: 5, imagedata: @[ 156 scene.addTexture("my_little_texture", Image(width: 5, height: 5, imagedata: @[
157 R, R, R, R, R, 157 R, R, R, R, R,
158 R, R, W, R, R, 158 R, R, W, R, R,
159 R, W, W, W, R, 159 R, W, W, W, R,
160 R, R, W, R, R, 160 R, R, W, R, R,
161 R, R, R, R, R, 161 R, R, R, R, R,
162 ])) 162 ]), VK_FILTER_NEAREST)
163 engine.addScene(scene, vertexInput) 163 engine.addScene(scene, vertexInput)
164 164
165 # MAINLOOP 165 # MAINLOOP
166 echo "Setup successfull, start rendering" 166 echo "Setup successfull, start rendering"
167 for i in 0 ..< 3: 167 for i in 0 ..< 3: