Mercurial > games > semicongine
annotate 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 |
rev | line source |
---|---|
1247
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
1 import std/os |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
2 import std/sequtils |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
3 import std/monotimes |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
4 import std/times |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
5 import std/options |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
6 import std/random |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
7 |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
8 import ../semiconginev2 |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
9 |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
10 proc test_gltf(time: float32) = |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
11 var renderdata = InitRenderData() |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
12 |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
13 type |
1253 | 14 ObjectData = object |
15 transform: Mat4 | |
16 Camera = object | |
17 view: Mat4 | |
18 perspective: Mat4 | |
1247
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
19 Material = object |
1248 | 20 color: Vec4f = NewVec4f(1, 1, 1, 1) |
1247
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
21 colorTexture: int32 = -1 |
1248 | 22 metallic: float32 = 0 |
23 roughness: float32 = 0 | |
1247
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
24 metallicRoughnessTexture: int32 = -1 |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
25 |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
26 normalTexture: int32 = -1 |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
27 occlusionTexture: int32 = -1 |
1248 | 28 emissive: Vec4f = NewVec4f(0, 0, 0, 0) |
1247
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
29 emissiveTexture: int32 = -1 |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
30 MainDescriptors = object |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
31 material: GPUValue[Material, UniformBuffer] |
1253 | 32 camera: GPUValue[Camera, UniformBufferMapped] |
1247
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
33 Shader = object |
1253 | 34 objectData {.PushConstantAttribute.}: ObjectData |
1247
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
35 position {.VertexAttribute.}: Vec3f |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
36 uv {.VertexAttribute.}: Vec2f |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
37 fragmentColor {.Pass.}: Vec4f |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
38 fragmentUv {.Pass.}: Vec2f |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
39 outColor {.ShaderOutput.}: Vec4f |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
40 descriptors {.DescriptorSets.}: (MainDescriptors, ) |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
41 # code |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
42 vertexCode: string = """ |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
43 void main() { |
1249
d83726af7abb
did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents:
1248
diff
changeset
|
44 fragmentColor = vec4(1, 1, 1, 1); |
1247
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
45 fragmentUv = uv; |
1253 | 46 gl_Position = vec4(position, 1) * camera.perspective * camera.view; |
1247
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
47 }""" |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
48 fragmentCode: string = """void main() { outColor = fragmentColor;}""" |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
49 Mesh = object |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
50 position: GPUArray[Vec3f, VertexBuffer] |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
51 uv: GPUArray[Vec2f, VertexBuffer] |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
52 |
1251 | 53 var gltfData = LoadMeshes[Mesh, Material]( |
1247
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
54 "town.glb", |
1248 | 55 MeshAttributeNames( |
56 POSITION: "position", | |
1249
d83726af7abb
did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents:
1248
diff
changeset
|
57 TEXCOORD: @["uv"], |
d83726af7abb
did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents:
1248
diff
changeset
|
58 ), |
1248 | 59 MaterialAttributeNames( |
60 baseColorFactor: "color", | |
61 baseColorTexture: "colorTexture", | |
62 metallicFactor: "metallic", | |
63 roughnessFactor: "roughness", | |
64 metallicRoughnessTexture: "metallicRoughnessTexture", | |
65 normalTexture: "normalTexture", | |
66 occlusionTexture: "occlusionTexture", | |
67 emissiveTexture: "emissiveTexture", | |
68 emissiveFactor: "emissive", | |
69 ) | |
1247
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
70 ) |
1253 | 71 var descriptors = asDescriptorSet( |
72 MainDescriptors( | |
73 camera: asGPUValue(Camera( | |
74 view: Unit4, | |
75 perspective: Unit4, | |
76 ), UniformBufferMapped) | |
77 ) | |
78 ) | |
1251 | 79 for mesh in mitems(gltfData.meshes): |
1249
d83726af7abb
did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents:
1248
diff
changeset
|
80 for primitive in mitems(mesh): |
d83726af7abb
did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents:
1248
diff
changeset
|
81 renderdata.AssignBuffers(primitive[0]) |
1253 | 82 renderdata.AssignBuffers(descriptors) |
1247
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
83 |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
84 var pipeline = CreatePipeline[Shader](renderPass = vulkan.swapchain.renderPass) |
1253 | 85 InitDescriptorSet(renderdata, pipeline.descriptorSetLayouts[0], descriptors) |
86 renderdata.FlushAllMemory() | |
1247
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
87 |
1251 | 88 proc drawNode(commandbuffer: VkCommandBuffer, pipeline: Pipeline, nodeId: int, transform: Mat4 = Unit4) = |
89 let nodeTransform = gltfData.nodes[nodeId].transform * transform | |
90 if gltfData.nodes[nodeId].mesh >= 0: | |
91 for primitive in gltfData.meshes[gltfData.nodes[nodeId].mesh]: | |
1253 | 92 RenderWithPushConstant(commandbuffer = commandbuffer, pipeline = pipeline, mesh = primitive[0], pushConstant = ObjectData(transform: nodeTransform)) |
1251 | 93 for childNode in gltfData.nodes[nodeId].children: |
94 drawNode(commandbuffer = commandbuffer, pipeline = pipeline, nodeId = childNode, transform = nodeTransform) | |
95 | |
96 | |
1247
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
97 var start = getMonoTime() |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
98 while ((getMonoTime() - start).inMilliseconds().int / 1000) < time: |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
99 |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
100 WithNextFrame(framebuffer, commandbuffer): |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
101 |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
102 WithRenderPass(vulkan.swapchain.renderPass, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, NewVec4f(0, 0, 0, 0)): |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
103 |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
104 WithPipeline(commandbuffer, pipeline): |
1253 | 105 WithBind(commandbuffer, (descriptors, ), pipeline): |
106 for nodeId in gltfData.scenes[0]: | |
107 drawNode(commandbuffer = commandbuffer, pipeline = pipeline, nodeId = nodeId) | |
1247
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
108 |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
109 # cleanup |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
110 checkVkResult vkDeviceWaitIdle(vulkan.device) |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
111 DestroyPipeline(pipeline) |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
112 DestroyRenderData(renderdata) |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
113 when isMainModule: |
1249
d83726af7abb
did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents:
1248
diff
changeset
|
114 var time = 5'f32 |
1247
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
115 InitVulkan() |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
116 |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
117 var renderpass = CreateDirectPresentationRenderPass(depthBuffer = true, samples = VK_SAMPLE_COUNT_4_BIT) |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
118 SetupSwapchain(renderpass = renderpass) |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
119 |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
120 # tests a simple triangle with minimalistic shader and vertex format |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
121 test_gltf(time) |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
122 |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
123 checkVkResult vkDeviceWaitIdle(vulkan.device) |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
124 vkDestroyRenderPass(vulkan.device, renderpass.vk, nil) |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
125 ClearSwapchain() |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
126 |
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
127 DestroyVulkan() |