annotate tests/test_gltf.nim @ 1247:c15770761865

add: gltf loading test, gltf loading for materials
author sam <sam@basx.dev>
date Wed, 24 Jul 2024 23:26:34 +0700
parents
children 317bb5a73606
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
14 Material = object
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
15 color: Vec4f
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
16 colorTexture: int32 = -1
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
17 metallic: float32 = -1
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
18 roughness: float32 = -1
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
19 metallicRoughnessTexture: int32 = -1
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
20
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
21 normalTexture: int32 = -1
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
22 occlusionTexture: int32 = -1
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
23 emissive: Vec4f = NewVec4f(-1, -1, -1, -1)
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
24 emissiveTexture: int32 = -1
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
25 MainDescriptors = object
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
26 material: GPUValue[Material, UniformBuffer]
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
27 Shader = object
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
28 position {.VertexAttribute.}: Vec3f
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
29 color {.VertexAttribute.}: Vec4f
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
30 uv {.VertexAttribute.}: Vec2f
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
31 fragmentColor {.Pass.}: Vec4f
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
32 fragmentUv {.Pass.}: Vec2f
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
33 outColor {.ShaderOutput.}: Vec4f
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
34 descriptors {.DescriptorSets.}: (MainDescriptors, )
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
35 # code
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
36 vertexCode: string = """
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
37 void main() {
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
38 fragmentColor = color;
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
39 fragmentUv = uv;
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
40 gl_Position = vec4(position, 1);
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
41 }"""
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
42 fragmentCode: string = """void main() { outColor = fragmentColor;}"""
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
43 Mesh = object
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
44 position: GPUArray[Vec3f, VertexBuffer]
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
45 color: GPUArray[Vec4f, VertexBuffer]
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
46 uv: GPUArray[Vec2f, VertexBuffer]
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 let gltfMesh = LoadMeshes[Mesh, Material](
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
49 "town.glb",
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
50 baseColorFactor = "color",
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
51 baseColorTexture = "colorTexture",
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
52 metallicFactor = "metallic",
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
53 roughnessFactor = "roughness",
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
54 metallicRoughnessTexture = "metallicRoughnessTexture",
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
55 normalTexture = "normalTexture",
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
56 occlusionTexture = "occlusionTexture",
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
57 emissiveTexture = "emissiveTexture",
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
58 emissiveFactor = "emissive",
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
59 )
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
60 var mesh = gltfMesh.meshes[0]
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
61 renderdata.AssignBuffers(mesh)
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
62 renderdata.FlushAllMemory()
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
63
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
64 var pipeline = CreatePipeline[Shader](renderPass = vulkan.swapchain.renderPass)
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
65
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
66 var start = getMonoTime()
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
67 while ((getMonoTime() - start).inMilliseconds().int / 1000) < time:
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
68
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
69 WithNextFrame(framebuffer, commandbuffer):
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
70
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
71 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
72
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
73 WithPipeline(commandbuffer, pipeline):
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
74
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
75 Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = mesh)
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
76
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
77 # cleanup
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
78 checkVkResult vkDeviceWaitIdle(vulkan.device)
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
79 DestroyPipeline(pipeline)
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
80 DestroyRenderData(renderdata)
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
81 when isMainModule:
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
82 var time = 1'f32
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
83 InitVulkan()
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
84
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
85 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
86 SetupSwapchain(renderpass = renderpass)
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
87
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
88 # 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
89 test_gltf(time)
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
90
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
91 checkVkResult vkDeviceWaitIdle(vulkan.device)
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
92 vkDestroyRenderPass(vulkan.device, renderpass.vk, nil)
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
93 ClearSwapchain()
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
94
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
95 DestroyVulkan()