Mercurial > games > semicongine
comparison tests/test_gltf.nim @ 1253:c4f98eb4bb05
fix: a few things
author | sam <sam@basx.dev> |
---|---|
date | Fri, 26 Jul 2024 23:39:24 +0700 |
parents | 3f98ad20a9d3 |
children | b0f4c8ccd49a |
comparison
equal
deleted
inserted
replaced
1252:01e9f41d35b1 | 1253:c4f98eb4bb05 |
---|---|
9 | 9 |
10 proc test_gltf(time: float32) = | 10 proc test_gltf(time: float32) = |
11 var renderdata = InitRenderData() | 11 var renderdata = InitRenderData() |
12 | 12 |
13 type | 13 type |
14 ObjectData = object | |
15 transform: Mat4 | |
16 Camera = object | |
17 view: Mat4 | |
18 perspective: Mat4 | |
14 Material = object | 19 Material = object |
15 color: Vec4f = NewVec4f(1, 1, 1, 1) | 20 color: Vec4f = NewVec4f(1, 1, 1, 1) |
16 colorTexture: int32 = -1 | 21 colorTexture: int32 = -1 |
17 metallic: float32 = 0 | 22 metallic: float32 = 0 |
18 roughness: float32 = 0 | 23 roughness: float32 = 0 |
22 occlusionTexture: int32 = -1 | 27 occlusionTexture: int32 = -1 |
23 emissive: Vec4f = NewVec4f(0, 0, 0, 0) | 28 emissive: Vec4f = NewVec4f(0, 0, 0, 0) |
24 emissiveTexture: int32 = -1 | 29 emissiveTexture: int32 = -1 |
25 MainDescriptors = object | 30 MainDescriptors = object |
26 material: GPUValue[Material, UniformBuffer] | 31 material: GPUValue[Material, UniformBuffer] |
32 camera: GPUValue[Camera, UniformBufferMapped] | |
27 Shader = object | 33 Shader = object |
34 objectData {.PushConstantAttribute.}: ObjectData | |
28 position {.VertexAttribute.}: Vec3f | 35 position {.VertexAttribute.}: Vec3f |
29 uv {.VertexAttribute.}: Vec2f | 36 uv {.VertexAttribute.}: Vec2f |
30 fragmentColor {.Pass.}: Vec4f | 37 fragmentColor {.Pass.}: Vec4f |
31 fragmentUv {.Pass.}: Vec2f | 38 fragmentUv {.Pass.}: Vec2f |
32 outColor {.ShaderOutput.}: Vec4f | 39 outColor {.ShaderOutput.}: Vec4f |
34 # code | 41 # code |
35 vertexCode: string = """ | 42 vertexCode: string = """ |
36 void main() { | 43 void main() { |
37 fragmentColor = vec4(1, 1, 1, 1); | 44 fragmentColor = vec4(1, 1, 1, 1); |
38 fragmentUv = uv; | 45 fragmentUv = uv; |
39 gl_Position = vec4(position, 1); | 46 gl_Position = vec4(position, 1) * camera.perspective * camera.view; |
40 }""" | 47 }""" |
41 fragmentCode: string = """void main() { outColor = fragmentColor;}""" | 48 fragmentCode: string = """void main() { outColor = fragmentColor;}""" |
42 Mesh = object | 49 Mesh = object |
43 position: GPUArray[Vec3f, VertexBuffer] | 50 position: GPUArray[Vec3f, VertexBuffer] |
44 uv: GPUArray[Vec2f, VertexBuffer] | 51 uv: GPUArray[Vec2f, VertexBuffer] |
59 occlusionTexture: "occlusionTexture", | 66 occlusionTexture: "occlusionTexture", |
60 emissiveTexture: "emissiveTexture", | 67 emissiveTexture: "emissiveTexture", |
61 emissiveFactor: "emissive", | 68 emissiveFactor: "emissive", |
62 ) | 69 ) |
63 ) | 70 ) |
71 var descriptors = asDescriptorSet( | |
72 MainDescriptors( | |
73 camera: asGPUValue(Camera( | |
74 view: Unit4, | |
75 perspective: Unit4, | |
76 ), UniformBufferMapped) | |
77 ) | |
78 ) | |
64 for mesh in mitems(gltfData.meshes): | 79 for mesh in mitems(gltfData.meshes): |
65 for primitive in mitems(mesh): | 80 for primitive in mitems(mesh): |
66 renderdata.AssignBuffers(primitive[0]) | 81 renderdata.AssignBuffers(primitive[0]) |
67 renderdata.FlushAllMemory() | 82 renderdata.AssignBuffers(descriptors) |
68 | 83 |
69 var pipeline = CreatePipeline[Shader](renderPass = vulkan.swapchain.renderPass) | 84 var pipeline = CreatePipeline[Shader](renderPass = vulkan.swapchain.renderPass) |
85 InitDescriptorSet(renderdata, pipeline.descriptorSetLayouts[0], descriptors) | |
86 renderdata.FlushAllMemory() | |
70 | 87 |
71 proc drawNode(commandbuffer: VkCommandBuffer, pipeline: Pipeline, nodeId: int, transform: Mat4 = Unit4) = | 88 proc drawNode(commandbuffer: VkCommandBuffer, pipeline: Pipeline, nodeId: int, transform: Mat4 = Unit4) = |
72 let nodeTransform = gltfData.nodes[nodeId].transform * transform | 89 let nodeTransform = gltfData.nodes[nodeId].transform * transform |
73 if gltfData.nodes[nodeId].mesh >= 0: | 90 if gltfData.nodes[nodeId].mesh >= 0: |
74 for primitive in gltfData.meshes[gltfData.nodes[nodeId].mesh]: | 91 for primitive in gltfData.meshes[gltfData.nodes[nodeId].mesh]: |
75 Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = primitive[0]) | 92 RenderWithPushConstant(commandbuffer = commandbuffer, pipeline = pipeline, mesh = primitive[0], pushConstant = ObjectData(transform: nodeTransform)) |
76 for childNode in gltfData.nodes[nodeId].children: | 93 for childNode in gltfData.nodes[nodeId].children: |
77 drawNode(commandbuffer = commandbuffer, pipeline = pipeline, nodeId = childNode, transform = nodeTransform) | 94 drawNode(commandbuffer = commandbuffer, pipeline = pipeline, nodeId = childNode, transform = nodeTransform) |
78 | 95 |
79 | 96 |
80 var start = getMonoTime() | 97 var start = getMonoTime() |
83 WithNextFrame(framebuffer, commandbuffer): | 100 WithNextFrame(framebuffer, commandbuffer): |
84 | 101 |
85 WithRenderPass(vulkan.swapchain.renderPass, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, NewVec4f(0, 0, 0, 0)): | 102 WithRenderPass(vulkan.swapchain.renderPass, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, NewVec4f(0, 0, 0, 0)): |
86 | 103 |
87 WithPipeline(commandbuffer, pipeline): | 104 WithPipeline(commandbuffer, pipeline): |
88 for nodeId in gltfData.scenes[0]: | 105 WithBind(commandbuffer, (descriptors, ), pipeline): |
89 drawNode(commandbuffer = commandbuffer, pipeline = pipeline, nodeId = nodeId) | 106 for nodeId in gltfData.scenes[0]: |
107 drawNode(commandbuffer = commandbuffer, pipeline = pipeline, nodeId = nodeId) | |
90 | 108 |
91 # cleanup | 109 # cleanup |
92 checkVkResult vkDeviceWaitIdle(vulkan.device) | 110 checkVkResult vkDeviceWaitIdle(vulkan.device) |
93 DestroyPipeline(pipeline) | 111 DestroyPipeline(pipeline) |
94 DestroyRenderData(renderdata) | 112 DestroyRenderData(renderdata) |