Mercurial > games > semicongine
annotate semicongine/renderer.nim @ 959:c104dbf5bbb8
did: adjust integer sizes to match vulkan API (more) directly
author | sam <sam@basx.dev> |
---|---|
date | Tue, 02 Apr 2024 16:09:38 +0700 |
parents | cca6f1a675db |
children | 616f97e92270 |
rev | line source |
---|---|
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
1 import std/options |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
2 import std/tables |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
3 import std/strformat |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
4 import std/sequtils |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
5 import std/strutils |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
6 import std/logging |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
7 |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
8 import ./core |
943
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
9 import ./vulkan/commandbuffer |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
10 import ./vulkan/buffer |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
11 import ./vulkan/device |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
12 import ./vulkan/drawable |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
13 import ./vulkan/physicaldevice |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
14 import ./vulkan/pipeline |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
15 import ./vulkan/renderpass |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
16 import ./vulkan/swapchain |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
17 import ./vulkan/shader |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
18 import ./vulkan/descriptor |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
19 import ./vulkan/image |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
20 |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
21 import ./scene |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
22 import ./mesh |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
23 import ./material |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
24 |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
25 const VERTEX_ATTRIB_ALIGNMENT = 4 # used for buffer alignment |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
26 |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
27 type |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
28 SceneData = ref object |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
29 drawables*: seq[tuple[drawable: Drawable, mesh: Mesh]] |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
30 vertexBuffers*: Table[MemoryPerformanceHint, Buffer] |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
31 indexBuffer*: Buffer |
882
5392cbd9db41
fix: material handlinge, did: formatting, add: support for multi-material texts
Sam <sam@basx.dev>
parents:
876
diff
changeset
|
32 uniformBuffers*: Table[VkPipeline, seq[Buffer]] # one per frame-in-flight |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
33 textures*: Table[VkPipeline, Table[string, seq[VulkanTexture]]] # per frame-in-flight |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
34 attributeLocation*: Table[string, MemoryPerformanceHint] |
959
c104dbf5bbb8
did: adjust integer sizes to match vulkan API (more) directly
sam <sam@basx.dev>
parents:
956
diff
changeset
|
35 vertexBufferOffsets*: Table[(Mesh, string), uint64] |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
36 descriptorPools*: Table[VkPipeline, DescriptorPool] |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
37 descriptorSets*: Table[VkPipeline, seq[DescriptorSet]] |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
38 materials: Table[MaterialType, seq[MaterialData]] |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
39 Renderer* = object |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
40 device: Device |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
41 renderPass: RenderPass |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
42 swapchain: Swapchain |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
43 scenedata: Table[Scene, SceneData] |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
44 emptyTexture: VulkanTexture |
943
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
45 queue: Queue |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
46 commandBufferPool: CommandBufferPool |
951
c6213a794ab0
fix: bad syncing mechanism, much better now (I think)
sam <sam@basx.dev>
parents:
950
diff
changeset
|
47 nextFrameReady: bool = false |
943
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
48 |
948 | 49 proc currentFrameCommandBuffer(renderer: Renderer): VkCommandBuffer = |
943
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
50 renderer.commandBufferPool.buffers[renderer.swapchain.currentInFlight] |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
51 |
949
0cf68e7336e8
add: a few more exposed parameters to the render API
sam <sam@basx.dev>
parents:
948
diff
changeset
|
52 proc initRenderer*( |
0cf68e7336e8
add: a few more exposed parameters to the render API
sam <sam@basx.dev>
parents:
948
diff
changeset
|
53 device: Device, |
0cf68e7336e8
add: a few more exposed parameters to the render API
sam <sam@basx.dev>
parents:
948
diff
changeset
|
54 shaders: openArray[(MaterialType, ShaderConfiguration)], |
0cf68e7336e8
add: a few more exposed parameters to the render API
sam <sam@basx.dev>
parents:
948
diff
changeset
|
55 clearColor = newVec4f(0, 0, 0, 0), |
0cf68e7336e8
add: a few more exposed parameters to the render API
sam <sam@basx.dev>
parents:
948
diff
changeset
|
56 backFaceCulling = true, |
0cf68e7336e8
add: a few more exposed parameters to the render API
sam <sam@basx.dev>
parents:
948
diff
changeset
|
57 vSync = false, |
0cf68e7336e8
add: a few more exposed parameters to the render API
sam <sam@basx.dev>
parents:
948
diff
changeset
|
58 inFlightFrames = 2, |
0cf68e7336e8
add: a few more exposed parameters to the render API
sam <sam@basx.dev>
parents:
948
diff
changeset
|
59 ): Renderer = |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
60 assert device.vk.valid |
882
5392cbd9db41
fix: material handlinge, did: formatting, add: support for multi-material texts
Sam <sam@basx.dev>
parents:
876
diff
changeset
|
61 |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
62 result.device = device |
943
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
63 result.renderPass = device.createRenderPass(shaders, clearColor = clearColor, backFaceCulling = backFaceCulling) |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
64 let swapchain = device.createSwapchain( |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
65 result.renderPass.vk, |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
66 device.physicalDevice.getSurfaceFormats().filterSurfaceFormat(), |
949
0cf68e7336e8
add: a few more exposed parameters to the render API
sam <sam@basx.dev>
parents:
948
diff
changeset
|
67 vSync = vSync, |
0cf68e7336e8
add: a few more exposed parameters to the render API
sam <sam@basx.dev>
parents:
948
diff
changeset
|
68 inFlightFrames = inFlightFrames, |
943
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
69 ) |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
70 if not swapchain.isSome: |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
71 raise newException(Exception, "Unable to create swapchain") |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
72 |
943
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
73 result.queue = device.firstGraphicsQueue().get() |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
74 result.commandBufferPool = device.createCommandBufferPool(result.queue.family, swapchain.get().inFlightFrames) |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
75 result.swapchain = swapchain.get() |
943
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
76 result.emptyTexture = device.uploadTexture(result.queue, EMPTYTEXTURE) |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
77 |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
78 func inputs(renderer: Renderer, scene: Scene): seq[ShaderAttribute] = |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
79 var found: Table[string, ShaderAttribute] |
943
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
80 for (materialType, shaderPipeline) in renderer.renderPass.shaderPipelines: |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
81 if scene.usesMaterial(materialType): |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
82 for input in shaderPipeline.inputs: |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
83 if found.contains(input.name): |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
84 assert input.name == found[input.name].name, &"{input.name}: {input.name} != {found[input.name].name}" |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
85 assert input.theType == found[input.name].theType, &"{input.name}: {input.theType} != {found[input.name].theType}" |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
86 assert input.arrayCount == found[input.name].arrayCount, &"{input.name}: {input.arrayCount} != {found[input.name].arrayCount}" |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
87 assert input.memoryPerformanceHint == found[input.name].memoryPerformanceHint, &"{input.name}: {input.memoryPerformanceHint} != {found[input.name].memoryPerformanceHint}" |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
88 else: |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
89 result.add input |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
90 found[input.name] = input |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
91 |
904
a253a691e0d6
fix: font-api, allow consistent use of mesh-transform
Sam <sam@basx.dev>
parents:
899
diff
changeset
|
92 proc materialCompatibleWithPipeline(scene: Scene, materialType: MaterialType, shaderPipeline: ShaderPipeline): (bool, string) = |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
93 for uniform in shaderPipeline.uniforms: |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
94 if scene.shaderGlobals.contains(uniform.name): |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
95 if scene.shaderGlobals[uniform.name].theType != uniform.theType: |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
96 return (true, &"shader uniform needs type {uniform.theType} but scene global is of type {scene.shaderGlobals[uniform.name].theType}") |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
97 else: |
867 | 98 if not materialType.hasMatchingAttribute(uniform): |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
99 return (true, &"shader uniform '{uniform.name}' was not found in scene globals or scene materials") |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
100 for texture in shaderPipeline.samplers: |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
101 if scene.shaderGlobals.contains(texture.name): |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
102 if scene.shaderGlobals[texture.name].theType != texture.theType: |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
103 return (true, &"shader texture '{texture.name}' needs type {texture.theType} but scene global is of type {scene.shaderGlobals[texture.name].theType}") |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
104 else: |
867 | 105 if not materialType.hasMatchingAttribute(texture): |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
106 return (true, &"Required texture for shader texture '{texture.name}' was not found in scene materials") |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
107 |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
108 return (false, "") |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
109 |
904
a253a691e0d6
fix: font-api, allow consistent use of mesh-transform
Sam <sam@basx.dev>
parents:
899
diff
changeset
|
110 proc meshCompatibleWithPipeline(scene: Scene, mesh: Mesh, shaderPipeline: ShaderPipeline): (bool, string) = |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
111 for input in shaderPipeline.inputs: |
876
164b41a2d5a6
add: font/text improvments, support for newline rendering
Sam <sam@basx.dev>
parents:
870
diff
changeset
|
112 if input.name in [TRANSFORM_ATTRIB, MATERIALINDEX_ATTRIBUTE]: # will be populated automatically |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
113 assert input.perInstance == true, &"Currently the {input.name} attribute must be a per instance attribute" |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
114 continue |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
115 if not (input.name in mesh[].attributes): |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
116 return (true, &"Shader input '{input.name}' is not available for mesh") |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
117 if input.theType != mesh[].attributeType(input.name): |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
118 return (true, &"Shader input '{input.name}' expects type {input.theType}, but mesh has {mesh[].attributeType(input.name)}") |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
119 if not input.perInstance and not mesh[].vertexAttributes.contains(input.name): |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
120 return (true, &"Shader input '{input.name}' expected to be vertex attribute, but mesh has no such vertex attribute (available are: {mesh[].vertexAttributes})") |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
121 if input.perInstance and not mesh[].instanceAttributes.contains(input.name): |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
122 return (true, &"Shader input '{input.name}' expected to be per instance attribute, but mesh has no such instance attribute (available are: {mesh[].instanceAttributes})") |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
123 |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
124 let pipelineCompatability = scene.materialCompatibleWithPipeline(mesh.material.theType, shaderPipeline) |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
125 if pipelineCompatability[0]: |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
126 return (true, pipelineCompatability[1]) |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
127 return (false, "") |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
128 |
904
a253a691e0d6
fix: font-api, allow consistent use of mesh-transform
Sam <sam@basx.dev>
parents:
899
diff
changeset
|
129 proc checkSceneIntegrity(renderer: Renderer, scene: Scene) = |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
130 # TODO: this and the sub-functions can likely be simplified a ton |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
131 if scene.meshes.len == 0: |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
132 return |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
133 |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
134 var foundRenderableObject = false |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
135 var materialTypes: seq[MaterialType] |
943
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
136 for (materialType, shaderPipeline) in renderer.renderPass.shaderPipelines: |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
137 materialTypes.add materialType |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
138 for mesh in scene.meshes: |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
139 if mesh.material.theType == materialType: |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
140 foundRenderableObject = true |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
141 let (error, message) = scene.meshCompatibleWithPipeline(mesh, shaderPipeline) |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
142 assert not error, &"Mesh '{mesh}' not compatible with assigned shaderPipeline ({materialType}) because: {message}" |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
143 |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
144 if not foundRenderableObject: |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
145 var matTypes: Table[string, MaterialType] |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
146 for mesh in scene.meshes: |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
147 if not matTypes.contains(mesh.material.name): |
882
5392cbd9db41
fix: material handlinge, did: formatting, add: support for multi-material texts
Sam <sam@basx.dev>
parents:
876
diff
changeset
|
148 matTypes[mesh.material.name] = mesh.material.theType |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
149 assert false, &"Scene '{scene.name}' has been added but materials are not compatible with any registered shader: Materials in scene: {matTypes}, registered shader-materialtypes: {materialTypes}" |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
150 |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
151 proc setupDrawableBuffers*(renderer: var Renderer, scene: var Scene) = |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
152 assert not (scene in renderer.scenedata) |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
153 |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
154 var scenedata = SceneData() |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
155 |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
156 # find all material data and group it by material type |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
157 for mesh in scene.meshes: |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
158 if not scenedata.materials.contains(mesh.material.theType): |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
159 scenedata.materials[mesh.material.theType] = @[] |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
160 if not scenedata.materials[mesh.material.theType].contains(mesh.material): |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
161 scenedata.materials[mesh.material.theType].add mesh.material |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
162 |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
163 # automatically populate material and tranform attributes |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
164 for mesh in scene.meshes: |
876
164b41a2d5a6
add: font/text improvments, support for newline rendering
Sam <sam@basx.dev>
parents:
870
diff
changeset
|
165 if not (TRANSFORM_ATTRIB in mesh[].attributes): |
164b41a2d5a6
add: font/text improvments, support for newline rendering
Sam <sam@basx.dev>
parents:
870
diff
changeset
|
166 mesh[].initInstanceAttribute(TRANSFORM_ATTRIB, Unit4) |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
167 if not (MATERIALINDEX_ATTRIBUTE in mesh[].attributes): |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
168 mesh[].initInstanceAttribute(MATERIALINDEX_ATTRIBUTE, uint16(scenedata.materials[mesh.material.theType].find(mesh.material))) |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
169 |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
170 renderer.checkSceneIntegrity(scene) |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
171 |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
172 # create index buffer if necessary |
959
c104dbf5bbb8
did: adjust integer sizes to match vulkan API (more) directly
sam <sam@basx.dev>
parents:
956
diff
changeset
|
173 var indicesBufferSize = 0'u64 |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
174 for mesh in scene.meshes: |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
175 if mesh[].indexType != MeshIndexType.None: |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
176 let indexAlignment = case mesh[].indexType |
959
c104dbf5bbb8
did: adjust integer sizes to match vulkan API (more) directly
sam <sam@basx.dev>
parents:
956
diff
changeset
|
177 of MeshIndexType.None: 0'u64 |
c104dbf5bbb8
did: adjust integer sizes to match vulkan API (more) directly
sam <sam@basx.dev>
parents:
956
diff
changeset
|
178 of Tiny: 1'u64 |
c104dbf5bbb8
did: adjust integer sizes to match vulkan API (more) directly
sam <sam@basx.dev>
parents:
956
diff
changeset
|
179 of Small: 2'u64 |
c104dbf5bbb8
did: adjust integer sizes to match vulkan API (more) directly
sam <sam@basx.dev>
parents:
956
diff
changeset
|
180 of Big: 4'u64 |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
181 # index value alignment required by Vulkan |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
182 if indicesBufferSize mod indexAlignment != 0: |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
183 indicesBufferSize += indexAlignment - (indicesBufferSize mod indexAlignment) |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
184 indicesBufferSize += mesh[].indexSize |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
185 if indicesBufferSize > 0: |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
186 scenedata.indexBuffer = renderer.device.createBuffer( |
882
5392cbd9db41
fix: material handlinge, did: formatting, add: support for multi-material texts
Sam <sam@basx.dev>
parents:
876
diff
changeset
|
187 size = indicesBufferSize, |
5392cbd9db41
fix: material handlinge, did: formatting, add: support for multi-material texts
Sam <sam@basx.dev>
parents:
876
diff
changeset
|
188 usage = [VK_BUFFER_USAGE_INDEX_BUFFER_BIT], |
5392cbd9db41
fix: material handlinge, did: formatting, add: support for multi-material texts
Sam <sam@basx.dev>
parents:
876
diff
changeset
|
189 requireMappable = false, |
5392cbd9db41
fix: material handlinge, did: formatting, add: support for multi-material texts
Sam <sam@basx.dev>
parents:
876
diff
changeset
|
190 preferVRAM = true, |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
191 ) |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
192 |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
193 # calculcate offsets for attributes in vertex buffers |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
194 # trying to use one buffer per memory type |
959
c104dbf5bbb8
did: adjust integer sizes to match vulkan API (more) directly
sam <sam@basx.dev>
parents:
956
diff
changeset
|
195 var perLocationSizes: Table[MemoryPerformanceHint, uint64] |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
196 for hint in MemoryPerformanceHint: |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
197 perLocationSizes[hint] = 0 |
899 | 198 |
199 let inputs = renderer.inputs(scene) | |
200 | |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
201 for attribute in inputs: |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
202 scenedata.attributeLocation[attribute.name] = attribute.memoryPerformanceHint |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
203 # setup one buffer per attribute-location-type |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
204 for mesh in scene.meshes: |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
205 # align size to VERTEX_ATTRIB_ALIGNMENT bytes (the important thing is the correct alignment of the offsets, but |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
206 # we need to expand the buffer size as well, therefore considering alignment already here as well |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
207 if perLocationSizes[attribute.memoryPerformanceHint] mod VERTEX_ATTRIB_ALIGNMENT != 0: |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
208 perLocationSizes[attribute.memoryPerformanceHint] += VERTEX_ATTRIB_ALIGNMENT - (perLocationSizes[attribute.memoryPerformanceHint] mod VERTEX_ATTRIB_ALIGNMENT) |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
209 perLocationSizes[attribute.memoryPerformanceHint] += mesh[].attributeSize(attribute.name) |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
210 |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
211 # create vertex buffers |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
212 for memoryPerformanceHint, bufferSize in perLocationSizes.pairs: |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
213 if bufferSize > 0: |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
214 scenedata.vertexBuffers[memoryPerformanceHint] = renderer.device.createBuffer( |
882
5392cbd9db41
fix: material handlinge, did: formatting, add: support for multi-material texts
Sam <sam@basx.dev>
parents:
876
diff
changeset
|
215 size = bufferSize, |
5392cbd9db41
fix: material handlinge, did: formatting, add: support for multi-material texts
Sam <sam@basx.dev>
parents:
876
diff
changeset
|
216 usage = [VK_BUFFER_USAGE_VERTEX_BUFFER_BIT], |
5392cbd9db41
fix: material handlinge, did: formatting, add: support for multi-material texts
Sam <sam@basx.dev>
parents:
876
diff
changeset
|
217 requireMappable = memoryPerformanceHint == PreferFastWrite, |
5392cbd9db41
fix: material handlinge, did: formatting, add: support for multi-material texts
Sam <sam@basx.dev>
parents:
876
diff
changeset
|
218 preferVRAM = true, |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
219 ) |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
220 |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
221 # calculate offset of each attribute for all meshes |
959
c104dbf5bbb8
did: adjust integer sizes to match vulkan API (more) directly
sam <sam@basx.dev>
parents:
956
diff
changeset
|
222 var perLocationOffsets: Table[MemoryPerformanceHint, uint64] |
c104dbf5bbb8
did: adjust integer sizes to match vulkan API (more) directly
sam <sam@basx.dev>
parents:
956
diff
changeset
|
223 var indexBufferOffset = 0'u64 |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
224 for hint in MemoryPerformanceHint: |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
225 perLocationOffsets[hint] = 0 |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
226 |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
227 for mesh in scene.meshes: |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
228 for attribute in inputs: |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
229 scenedata.vertexBufferOffsets[(mesh, attribute.name)] = perLocationOffsets[attribute.memoryPerformanceHint] |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
230 if mesh[].attributes.contains(attribute.name): |
870
b975eab2b694
did: improve dynamic array, mesh and material APIs a ton, changes in material attributes are now detected and will trigger uniform-updates
Sam <sam@basx.dev>
parents:
869
diff
changeset
|
231 perLocationOffsets[attribute.memoryPerformanceHint] += mesh[].attributeSize(attribute.name) |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
232 if perLocationOffsets[attribute.memoryPerformanceHint] mod VERTEX_ATTRIB_ALIGNMENT != 0: |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
233 perLocationOffsets[attribute.memoryPerformanceHint] += VERTEX_ATTRIB_ALIGNMENT - (perLocationOffsets[attribute.memoryPerformanceHint] mod VERTEX_ATTRIB_ALIGNMENT) |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
234 |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
235 # fill offsets per shaderPipeline (as sequence corresponds to shader input binding) |
959
c104dbf5bbb8
did: adjust integer sizes to match vulkan API (more) directly
sam <sam@basx.dev>
parents:
956
diff
changeset
|
236 var offsets: Table[VkPipeline, seq[(string, MemoryPerformanceHint, uint64)]] |
943
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
237 for (materialType, shaderPipeline) in renderer.renderPass.shaderPipelines: |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
238 if scene.usesMaterial(materialType): |
959
c104dbf5bbb8
did: adjust integer sizes to match vulkan API (more) directly
sam <sam@basx.dev>
parents:
956
diff
changeset
|
239 offsets[shaderPipeline.vk] = newSeq[(string, MemoryPerformanceHint, uint64)]() |
943
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
240 for attribute in shaderPipeline.inputs: |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
241 offsets[shaderPipeline.vk].add (attribute.name, attribute.memoryPerformanceHint, scenedata.vertexBufferOffsets[(mesh, attribute.name)]) |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
242 |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
243 # create drawables |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
244 let indexed = mesh.indexType != MeshIndexType.None |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
245 var drawable = Drawable( |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
246 name: mesh.name, |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
247 elementCount: if indexed: mesh[].indicesCount else: mesh[].vertexCount, |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
248 bufferOffsets: offsets, |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
249 instanceCount: mesh[].instanceCount, |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
250 indexed: indexed, |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
251 ) |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
252 if indexed: |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
253 let indexAlignment = case mesh.indexType |
959
c104dbf5bbb8
did: adjust integer sizes to match vulkan API (more) directly
sam <sam@basx.dev>
parents:
956
diff
changeset
|
254 of MeshIndexType.None: 0'u64 |
c104dbf5bbb8
did: adjust integer sizes to match vulkan API (more) directly
sam <sam@basx.dev>
parents:
956
diff
changeset
|
255 of Tiny: 1'u64 |
c104dbf5bbb8
did: adjust integer sizes to match vulkan API (more) directly
sam <sam@basx.dev>
parents:
956
diff
changeset
|
256 of Small: 2'u64 |
c104dbf5bbb8
did: adjust integer sizes to match vulkan API (more) directly
sam <sam@basx.dev>
parents:
956
diff
changeset
|
257 of Big: 4'u64 |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
258 # index value alignment required by Vulkan |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
259 if indexBufferOffset mod indexAlignment != 0: |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
260 indexBufferOffset += indexAlignment - (indexBufferOffset mod indexAlignment) |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
261 drawable.indexBufferOffset = indexBufferOffset |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
262 drawable.indexType = mesh.indexType |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
263 var (pdata, size) = mesh[].getRawIndexData() |
943
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
264 scenedata.indexBuffer.setData(renderer.queue, pdata, size, indexBufferOffset) |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
265 indexBufferOffset += size |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
266 scenedata.drawables.add (drawable, mesh) |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
267 |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
268 # setup uniforms and textures (anything descriptor) |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
269 var uploadedTextures: Table[Texture, VulkanTexture] |
943
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
270 for (materialType, shaderPipeline) in renderer.renderPass.shaderPipelines: |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
271 if scene.usesMaterial(materialType): |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
272 # gather textures |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
273 scenedata.textures[shaderPipeline.vk] = initTable[string, seq[VulkanTexture]]() |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
274 for texture in shaderPipeline.samplers: |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
275 scenedata.textures[shaderPipeline.vk][texture.name] = newSeq[VulkanTexture]() |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
276 if scene.shaderGlobals.contains(texture.name): |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
277 for textureValue in scene.shaderGlobals[texture.name][Texture][]: |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
278 if not uploadedTextures.contains(textureValue): |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
279 uploadedTextures[textureValue] = renderer.device.uploadTexture(renderer.queue, textureValue) |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
280 scenedata.textures[shaderPipeline.vk][texture.name].add uploadedTextures[textureValue] |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
281 else: |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
282 var foundTexture = false |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
283 for material in scene.getMaterials(materialType): |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
284 if material.hasMatchingAttribute(texture): |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
285 foundTexture = true |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
286 let value = material[texture.name, Texture][] |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
287 assert value.len == 1, &"Mesh material attribute '{texture.name}' has texture-array, but only single textures are allowed" |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
288 if not uploadedTextures.contains(value[0]): |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
289 uploadedTextures[value[0]] = renderer.device.uploadTexture(renderer.queue, value[0]) |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
290 scenedata.textures[shaderPipeline.vk][texture.name].add uploadedTextures[value[0]] |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
291 assert foundTexture, &"No texture found in shaderGlobals or materials for '{texture.name}'" |
959
c104dbf5bbb8
did: adjust integer sizes to match vulkan API (more) directly
sam <sam@basx.dev>
parents:
956
diff
changeset
|
292 let nTextures = scenedata.textures[shaderPipeline.vk][texture.name].len.uint32 |
943
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
293 assert (texture.arrayCount == 0 and nTextures == 1) or texture.arrayCount >= nTextures, &"Shader assigned to render '{materialType}' expected {texture.arrayCount} textures for '{texture.name}' but got {nTextures}" |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
294 if texture.arrayCount < nTextures: |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
295 warn &"Shader assigned to render '{materialType}' expected {texture.arrayCount} textures for '{texture.name}' but got {nTextures}" |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
296 |
943
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
297 # gather uniform sizes |
959
c104dbf5bbb8
did: adjust integer sizes to match vulkan API (more) directly
sam <sam@basx.dev>
parents:
956
diff
changeset
|
298 var uniformBufferSize = 0'u64 |
943
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
299 for uniform in shaderPipeline.uniforms: |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
300 uniformBufferSize += uniform.size |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
301 if uniformBufferSize > 0: |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
302 scenedata.uniformBuffers[shaderPipeline.vk] = newSeq[Buffer]() |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
303 for frame_i in 0 ..< renderer.swapchain.inFlightFrames: |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
304 scenedata.uniformBuffers[shaderPipeline.vk].add renderer.device.createBuffer( |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
305 size = uniformBufferSize, |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
306 usage = [VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT], |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
307 requireMappable = true, |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
308 preferVRAM = true, |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
309 ) |
882
5392cbd9db41
fix: material handlinge, did: formatting, add: support for multi-material texts
Sam <sam@basx.dev>
parents:
876
diff
changeset
|
310 |
943
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
311 # TODO: rework the whole descriptor/pool/layout stuff, a bit unclear |
959
c104dbf5bbb8
did: adjust integer sizes to match vulkan API (more) directly
sam <sam@basx.dev>
parents:
956
diff
changeset
|
312 var poolsizes = @[(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, renderer.swapchain.inFlightFrames.uint32)] |
c104dbf5bbb8
did: adjust integer sizes to match vulkan API (more) directly
sam <sam@basx.dev>
parents:
956
diff
changeset
|
313 var nTextures = 0'u32 |
943
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
314 for descriptor in shaderPipeline.descriptorSetLayout.descriptors: |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
315 if descriptor.thetype == ImageSampler: |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
316 nTextures += descriptor.count |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
317 if nTextures > 0: |
959
c104dbf5bbb8
did: adjust integer sizes to match vulkan API (more) directly
sam <sam@basx.dev>
parents:
956
diff
changeset
|
318 poolsizes.add (VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, nTextures * renderer.swapchain.inFlightFrames.uint32) |
943
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
319 scenedata.descriptorPools[shaderPipeline.vk] = renderer.device.createDescriptorSetPool(poolsizes) |
882
5392cbd9db41
fix: material handlinge, did: formatting, add: support for multi-material texts
Sam <sam@basx.dev>
parents:
876
diff
changeset
|
320 |
943
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
321 scenedata.descriptorSets[shaderPipeline.vk] = shaderPipeline.setupDescriptors( |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
322 scenedata.descriptorPools[shaderPipeline.vk], |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
323 scenedata.uniformBuffers.getOrDefault(shaderPipeline.vk, @[]), |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
324 scenedata.textures[shaderPipeline.vk], |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
325 inFlightFrames = renderer.swapchain.inFlightFrames, |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
326 emptyTexture = renderer.emptyTexture, |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
327 ) |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
328 for frame_i in 0 ..< renderer.swapchain.inFlightFrames: |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
329 scenedata.descriptorSets[shaderPipeline.vk][frame_i].writeDescriptorSet() |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
330 |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
331 renderer.scenedata[scene] = scenedata |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
332 |
882
5392cbd9db41
fix: material handlinge, did: formatting, add: support for multi-material texts
Sam <sam@basx.dev>
parents:
876
diff
changeset
|
333 proc updateMeshData*(renderer: var Renderer, scene: var Scene, forceAll = false) = |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
334 assert scene in renderer.scenedata |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
335 |
956
cca6f1a675db
fix: again, I think, I have now a correct syncing of vertex buffers updates and drawing of multiple frames in flight XD
sam <sam@basx.dev>
parents:
953
diff
changeset
|
336 var addedBarrier = false; |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
337 for (drawable, mesh) in renderer.scenedata[scene].drawables.mitems: |
876
164b41a2d5a6
add: font/text improvments, support for newline rendering
Sam <sam@basx.dev>
parents:
870
diff
changeset
|
338 if mesh[].attributes.contains(TRANSFORM_ATTRIB): |
164b41a2d5a6
add: font/text improvments, support for newline rendering
Sam <sam@basx.dev>
parents:
870
diff
changeset
|
339 mesh[].updateInstanceTransforms(TRANSFORM_ATTRIB) |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
340 let attrs = (if forceAll: mesh[].attributes else: mesh[].dirtyAttributes) |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
341 for attribute in attrs: |
956
cca6f1a675db
fix: again, I think, I have now a correct syncing of vertex buffers updates and drawing of multiple frames in flight XD
sam <sam@basx.dev>
parents:
953
diff
changeset
|
342 # ignore attributes that are not used in this scene |
cca6f1a675db
fix: again, I think, I have now a correct syncing of vertex buffers updates and drawing of multiple frames in flight XD
sam <sam@basx.dev>
parents:
953
diff
changeset
|
343 if attribute in renderer.scenedata[scene].attributeLocation: |
cca6f1a675db
fix: again, I think, I have now a correct syncing of vertex buffers updates and drawing of multiple frames in flight XD
sam <sam@basx.dev>
parents:
953
diff
changeset
|
344 debug &"Update mesh attribute {attribute}" |
cca6f1a675db
fix: again, I think, I have now a correct syncing of vertex buffers updates and drawing of multiple frames in flight XD
sam <sam@basx.dev>
parents:
953
diff
changeset
|
345 let memoryPerformanceHint = renderer.scenedata[scene].attributeLocation[attribute] |
cca6f1a675db
fix: again, I think, I have now a correct syncing of vertex buffers updates and drawing of multiple frames in flight XD
sam <sam@basx.dev>
parents:
953
diff
changeset
|
346 # if we have to do a vkCmdCopyBuffer (not buffer.canMap), then we want to added a barrier to |
cca6f1a675db
fix: again, I think, I have now a correct syncing of vertex buffers updates and drawing of multiple frames in flight XD
sam <sam@basx.dev>
parents:
953
diff
changeset
|
347 # not infer with the current frame that is being renderer (relevant when we have multiple frames in flight) |
cca6f1a675db
fix: again, I think, I have now a correct syncing of vertex buffers updates and drawing of multiple frames in flight XD
sam <sam@basx.dev>
parents:
953
diff
changeset
|
348 # (remark: ...I think..., I am pretty new to this sync stuff) |
cca6f1a675db
fix: again, I think, I have now a correct syncing of vertex buffers updates and drawing of multiple frames in flight XD
sam <sam@basx.dev>
parents:
953
diff
changeset
|
349 if not renderer.scenedata[scene].vertexBuffers[memoryPerformanceHint].canMap and not addedBarrier: |
cca6f1a675db
fix: again, I think, I have now a correct syncing of vertex buffers updates and drawing of multiple frames in flight XD
sam <sam@basx.dev>
parents:
953
diff
changeset
|
350 withSingleUseCommandBuffer(renderer.device, renderer.queue, commandBuffer): |
cca6f1a675db
fix: again, I think, I have now a correct syncing of vertex buffers updates and drawing of multiple frames in flight XD
sam <sam@basx.dev>
parents:
953
diff
changeset
|
351 let barrier = VkMemoryBarrier( |
cca6f1a675db
fix: again, I think, I have now a correct syncing of vertex buffers updates and drawing of multiple frames in flight XD
sam <sam@basx.dev>
parents:
953
diff
changeset
|
352 sType: VK_STRUCTURE_TYPE_MEMORY_BARRIER, |
cca6f1a675db
fix: again, I think, I have now a correct syncing of vertex buffers updates and drawing of multiple frames in flight XD
sam <sam@basx.dev>
parents:
953
diff
changeset
|
353 srcAccessMask: [VK_ACCESS_MEMORY_READ_BIT].toBits, |
cca6f1a675db
fix: again, I think, I have now a correct syncing of vertex buffers updates and drawing of multiple frames in flight XD
sam <sam@basx.dev>
parents:
953
diff
changeset
|
354 dstAccessMask: [VK_ACCESS_MEMORY_WRITE_BIT].toBits, |
cca6f1a675db
fix: again, I think, I have now a correct syncing of vertex buffers updates and drawing of multiple frames in flight XD
sam <sam@basx.dev>
parents:
953
diff
changeset
|
355 ) |
cca6f1a675db
fix: again, I think, I have now a correct syncing of vertex buffers updates and drawing of multiple frames in flight XD
sam <sam@basx.dev>
parents:
953
diff
changeset
|
356 commandBuffer.pipelineBarrier( |
cca6f1a675db
fix: again, I think, I have now a correct syncing of vertex buffers updates and drawing of multiple frames in flight XD
sam <sam@basx.dev>
parents:
953
diff
changeset
|
357 srcStages = [VK_PIPELINE_STAGE_VERTEX_INPUT_BIT], |
cca6f1a675db
fix: again, I think, I have now a correct syncing of vertex buffers updates and drawing of multiple frames in flight XD
sam <sam@basx.dev>
parents:
953
diff
changeset
|
358 dstStages = [VK_PIPELINE_STAGE_TRANSFER_BIT], |
cca6f1a675db
fix: again, I think, I have now a correct syncing of vertex buffers updates and drawing of multiple frames in flight XD
sam <sam@basx.dev>
parents:
953
diff
changeset
|
359 memoryBarriers = [barrier] |
cca6f1a675db
fix: again, I think, I have now a correct syncing of vertex buffers updates and drawing of multiple frames in flight XD
sam <sam@basx.dev>
parents:
953
diff
changeset
|
360 ) |
cca6f1a675db
fix: again, I think, I have now a correct syncing of vertex buffers updates and drawing of multiple frames in flight XD
sam <sam@basx.dev>
parents:
953
diff
changeset
|
361 addedBarrier = true |
cca6f1a675db
fix: again, I think, I have now a correct syncing of vertex buffers updates and drawing of multiple frames in flight XD
sam <sam@basx.dev>
parents:
953
diff
changeset
|
362 renderer.scenedata[scene].vertexBuffers[memoryPerformanceHint].setData( |
cca6f1a675db
fix: again, I think, I have now a correct syncing of vertex buffers updates and drawing of multiple frames in flight XD
sam <sam@basx.dev>
parents:
953
diff
changeset
|
363 renderer.queue, |
cca6f1a675db
fix: again, I think, I have now a correct syncing of vertex buffers updates and drawing of multiple frames in flight XD
sam <sam@basx.dev>
parents:
953
diff
changeset
|
364 mesh[].getPointer(attribute), |
cca6f1a675db
fix: again, I think, I have now a correct syncing of vertex buffers updates and drawing of multiple frames in flight XD
sam <sam@basx.dev>
parents:
953
diff
changeset
|
365 mesh[].attributeSize(attribute), |
cca6f1a675db
fix: again, I think, I have now a correct syncing of vertex buffers updates and drawing of multiple frames in flight XD
sam <sam@basx.dev>
parents:
953
diff
changeset
|
366 renderer.scenedata[scene].vertexBufferOffsets[(mesh, attribute)] |
cca6f1a675db
fix: again, I think, I have now a correct syncing of vertex buffers updates and drawing of multiple frames in flight XD
sam <sam@basx.dev>
parents:
953
diff
changeset
|
367 ) |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
368 mesh[].clearDirtyAttributes() |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
369 |
882
5392cbd9db41
fix: material handlinge, did: formatting, add: support for multi-material texts
Sam <sam@basx.dev>
parents:
876
diff
changeset
|
370 proc updateUniformData*(renderer: var Renderer, scene: var Scene, forceAll = false) = |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
371 assert scene in renderer.scenedata |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
372 |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
373 let dirty = scene.dirtyShaderGlobals |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
374 |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
375 if forceAll: |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
376 debug "Update uniforms because 'forceAll' was given" |
950
fe48b091e83f
did: tons of small improvments, on the way to make GPU sync (more) correct I guess
sam <sam@basx.dev>
parents:
949
diff
changeset
|
377 elif dirty.len > 0: |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
378 debug &"Update uniforms because of dirty scene globals: {dirty}" |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
379 |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
380 # loop over all used shaders/pipelines |
943
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
381 for (materialType, shaderPipeline) in renderer.renderPass.shaderPipelines: |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
382 if ( |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
383 scene.usesMaterial(materialType) and |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
384 renderer.scenedata[scene].uniformBuffers.hasKey(shaderPipeline.vk) and |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
385 renderer.scenedata[scene].uniformBuffers[shaderPipeline.vk].len != 0 |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
386 ): |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
387 var dirtyMaterialAttribs: seq[string] |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
388 for material in renderer.scenedata[scene].materials[materialType].mitems: |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
389 dirtyMaterialAttribs.add material.dirtyAttributes |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
390 material.clearDirtyAttributes() |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
391 assert renderer.scenedata[scene].uniformBuffers[shaderPipeline.vk][renderer.swapchain.currentInFlight].vk.valid |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
392 if forceAll: |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
393 for buffer in renderer.scenedata[scene].uniformBuffers[shaderPipeline.vk]: |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
394 assert buffer.vk.valid |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
395 |
959
c104dbf5bbb8
did: adjust integer sizes to match vulkan API (more) directly
sam <sam@basx.dev>
parents:
956
diff
changeset
|
396 var offset = 0'u64 |
943
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
397 # loop over all uniforms of the shader-shaderPipeline |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
398 for uniform in shaderPipeline.uniforms: |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
399 if dirty.contains(uniform.name) or dirtyMaterialAttribs.contains(uniform.name) or forceAll: # only update uniforms if necessary |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
400 var value = initDataList(uniform.theType) |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
401 if scene.shaderGlobals.hasKey(uniform.name): |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
402 assert scene.shaderGlobals[uniform.name].thetype == uniform.thetype |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
403 value = scene.shaderGlobals[uniform.name] |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
404 else: |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
405 var foundValue = false |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
406 for material in renderer.scenedata[scene].materials[materialType]: |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
407 if material.hasMatchingAttribute(uniform): |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
408 value.appendValues(material[uniform.name]) |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
409 foundValue = true |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
410 assert foundValue, &"Uniform '{uniform.name}' not found in scene shaderGlobals or materials" |
959
c104dbf5bbb8
did: adjust integer sizes to match vulkan API (more) directly
sam <sam@basx.dev>
parents:
956
diff
changeset
|
411 assert (uniform.arrayCount == 0 and value.len == 1) or value.len.uint <= uniform.arrayCount, &"Uniform '{uniform.name}' found has wrong length (shader declares {uniform.arrayCount} but shaderGlobals and materials provide {value.len})" |
c104dbf5bbb8
did: adjust integer sizes to match vulkan API (more) directly
sam <sam@basx.dev>
parents:
956
diff
changeset
|
412 if value.len.uint <= uniform.arrayCount: |
943
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
413 debug &"Uniform '{uniform.name}' found has short length (shader declares {uniform.arrayCount} but shaderGlobals and materials provide {value.len})" |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
414 assert value.size <= uniform.size, &"During uniform update: gathered value has size {value.size} but uniform expects size {uniform.size}" |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
415 if value.size < uniform.size: |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
416 debug &"During uniform update: gathered value has size {value.size} but uniform expects size {uniform.size}" |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
417 debug &" update uniform '{uniform.name}' with value: {value}" |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
418 # TODO: technically we would only need to update the uniform buffer of the current |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
419 # frameInFlight (I think), but we don't track for which frame the shaderglobals are no longer dirty |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
420 # therefore we have to update the uniform values in all buffers, of all inFlightframes (usually 2) |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
421 for buffer in renderer.scenedata[scene].uniformBuffers[shaderPipeline.vk]: |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
422 buffer.setData(renderer.queue, value.getPointer(), value.size, offset) |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
423 offset += uniform.size |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
424 scene.clearDirtyShaderGlobals() |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
425 |
951
c6213a794ab0
fix: bad syncing mechanism, much better now (I think)
sam <sam@basx.dev>
parents:
950
diff
changeset
|
426 proc startNewFrame*(renderer: var Renderer) = |
956
cca6f1a675db
fix: again, I think, I have now a correct syncing of vertex buffers updates and drawing of multiple frames in flight XD
sam <sam@basx.dev>
parents:
953
diff
changeset
|
427 # TODO: chance for an infinity-loop? |
953 | 428 while not renderer.swapchain.acquireNextFrame(): |
429 checkVkResult renderer.device.vk.vkDeviceWaitIdle() | |
950
fe48b091e83f
did: tons of small improvments, on the way to make GPU sync (more) correct I guess
sam <sam@basx.dev>
parents:
949
diff
changeset
|
430 let res = renderer.swapchain.recreate() |
fe48b091e83f
did: tons of small improvments, on the way to make GPU sync (more) correct I guess
sam <sam@basx.dev>
parents:
949
diff
changeset
|
431 if not res.isSome: |
fe48b091e83f
did: tons of small improvments, on the way to make GPU sync (more) correct I guess
sam <sam@basx.dev>
parents:
949
diff
changeset
|
432 raise newException(Exception, "Unable to recreate swapchain") |
fe48b091e83f
did: tons of small improvments, on the way to make GPU sync (more) correct I guess
sam <sam@basx.dev>
parents:
949
diff
changeset
|
433 var oldSwapchain = renderer.swapchain |
fe48b091e83f
did: tons of small improvments, on the way to make GPU sync (more) correct I guess
sam <sam@basx.dev>
parents:
949
diff
changeset
|
434 renderer.swapchain = res.get() |
fe48b091e83f
did: tons of small improvments, on the way to make GPU sync (more) correct I guess
sam <sam@basx.dev>
parents:
949
diff
changeset
|
435 checkVkResult renderer.device.vk.vkDeviceWaitIdle() |
fe48b091e83f
did: tons of small improvments, on the way to make GPU sync (more) correct I guess
sam <sam@basx.dev>
parents:
949
diff
changeset
|
436 oldSwapchain.destroy() |
951
c6213a794ab0
fix: bad syncing mechanism, much better now (I think)
sam <sam@basx.dev>
parents:
950
diff
changeset
|
437 renderer.nextFrameReady = true |
950
fe48b091e83f
did: tons of small improvments, on the way to make GPU sync (more) correct I guess
sam <sam@basx.dev>
parents:
949
diff
changeset
|
438 |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
439 proc render*(renderer: var Renderer, scene: Scene) = |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
440 assert scene in renderer.scenedata |
951
c6213a794ab0
fix: bad syncing mechanism, much better now (I think)
sam <sam@basx.dev>
parents:
950
diff
changeset
|
441 assert renderer.nextFrameReady, "startNewFrame() must be called before calling render()" |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
442 |
950
fe48b091e83f
did: tons of small improvments, on the way to make GPU sync (more) correct I guess
sam <sam@basx.dev>
parents:
949
diff
changeset
|
443 # preparation |
948 | 444 renderer.currentFrameCommandBuffer.beginRenderCommands(renderer.renderPass, renderer.swapchain.currentFramebuffer(), oneTimeSubmit = true) |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
445 |
950
fe48b091e83f
did: tons of small improvments, on the way to make GPU sync (more) correct I guess
sam <sam@basx.dev>
parents:
949
diff
changeset
|
446 # debug output |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
447 debug "Scene buffers:" |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
448 for (location, buffer) in renderer.scenedata[scene].vertexBuffers.pairs: |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
449 debug " ", location, ": ", buffer |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
450 debug " Index buffer: ", renderer.scenedata[scene].indexBuffer |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
451 |
950
fe48b091e83f
did: tons of small improvments, on the way to make GPU sync (more) correct I guess
sam <sam@basx.dev>
parents:
949
diff
changeset
|
452 # draw all meshes |
943
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
453 for (materialType, shaderPipeline) in renderer.renderPass.shaderPipelines: |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
454 if scene.usesMaterial(materialType): |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
455 debug &"Start shaderPipeline for '{materialType}'" |
948 | 456 renderer.currentFrameCommandBuffer.vkCmdBindPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS, shaderPipeline.vk) |
457 renderer.currentFrameCommandBuffer.vkCmdBindDescriptorSets(VK_PIPELINE_BIND_POINT_GRAPHICS, shaderPipeline.layout, 0, 1, addr(renderer.scenedata[scene].descriptorSets[shaderPipeline.vk][renderer.swapchain.currentInFlight].vk), 0, nil) | |
943
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
458 for (drawable, mesh) in renderer.scenedata[scene].drawables.filterIt(it[1].visible and it[1].material.theType == materialType): |
948 | 459 drawable.draw(renderer.currentFrameCommandBuffer, vertexBuffers = renderer.scenedata[scene].vertexBuffers, indexBuffer = renderer.scenedata[scene].indexBuffer, shaderPipeline.vk) |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
460 |
950
fe48b091e83f
did: tons of small improvments, on the way to make GPU sync (more) correct I guess
sam <sam@basx.dev>
parents:
949
diff
changeset
|
461 # done rendering |
948 | 462 renderer.currentFrameCommandBuffer.endRenderCommands() |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
463 |
950
fe48b091e83f
did: tons of small improvments, on the way to make GPU sync (more) correct I guess
sam <sam@basx.dev>
parents:
949
diff
changeset
|
464 # swap framebuffer |
948 | 465 if not renderer.swapchain.swap(renderer.queue, renderer.currentFrameCommandBuffer): |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
466 let res = renderer.swapchain.recreate() |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
467 if res.isSome: |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
468 var oldSwapchain = renderer.swapchain |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
469 renderer.swapchain = res.get() |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
470 checkVkResult renderer.device.vk.vkDeviceWaitIdle() |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
471 oldSwapchain.destroy() |
956
cca6f1a675db
fix: again, I think, I have now a correct syncing of vertex buffers updates and drawing of multiple frames in flight XD
sam <sam@basx.dev>
parents:
953
diff
changeset
|
472 renderer.swapchain.currentInFlight = (renderer.swapchain.currentInFlight + 1) mod renderer.swapchain.inFlightFrames |
951
c6213a794ab0
fix: bad syncing mechanism, much better now (I think)
sam <sam@basx.dev>
parents:
950
diff
changeset
|
473 renderer.nextFrameReady = false |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
474 |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
475 func valid*(renderer: Renderer): bool = |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
476 renderer.device.vk.valid |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
477 |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
478 proc destroy*(renderer: var Renderer, scene: Scene) = |
910
c62f7947d371
fix: error in descriptors, wait for gpu before cleaning up
Sam <sam@basx.dev>
parents:
904
diff
changeset
|
479 checkVkResult renderer.device.vk.vkDeviceWaitIdle() |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
480 var scenedata = renderer.scenedata[scene] |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
481 for buffer in scenedata.vertexBuffers.mvalues: |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
482 assert buffer.vk.valid |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
483 buffer.destroy() |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
484 if scenedata.indexBuffer.vk.valid: |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
485 assert scenedata.indexBuffer.vk.valid |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
486 scenedata.indexBuffer.destroy() |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
487 for pipelineUniforms in scenedata.uniformBuffers.mvalues: |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
488 for buffer in pipelineUniforms.mitems: |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
489 assert buffer.vk.valid |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
490 buffer.destroy() |
845
19b5051d6eb7
fix: cleanup when multiple textures use same vulkan image
Sam <sam@basx.dev>
parents:
840
diff
changeset
|
491 var destroyedTextures: seq[VkImage] |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
492 for pipelineTextures in scenedata.textures.mvalues: |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
493 for textures in pipelineTextures.mvalues: |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
494 for texture in textures.mitems: |
845
19b5051d6eb7
fix: cleanup when multiple textures use same vulkan image
Sam <sam@basx.dev>
parents:
840
diff
changeset
|
495 if not destroyedTextures.contains(texture.image.vk): |
19b5051d6eb7
fix: cleanup when multiple textures use same vulkan image
Sam <sam@basx.dev>
parents:
840
diff
changeset
|
496 destroyedTextures.add texture.image.vk |
19b5051d6eb7
fix: cleanup when multiple textures use same vulkan image
Sam <sam@basx.dev>
parents:
840
diff
changeset
|
497 texture.destroy() |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
498 for descriptorPool in scenedata.descriptorPools.mvalues: |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
499 descriptorPool.destroy() |
845
19b5051d6eb7
fix: cleanup when multiple textures use same vulkan image
Sam <sam@basx.dev>
parents:
840
diff
changeset
|
500 renderer.scenedata.del(scene) |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
501 |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
502 proc destroy*(renderer: var Renderer) = |
910
c62f7947d371
fix: error in descriptors, wait for gpu before cleaning up
Sam <sam@basx.dev>
parents:
904
diff
changeset
|
503 checkVkResult renderer.device.vk.vkDeviceWaitIdle() |
943
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
504 |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
505 for scenedata in renderer.scenedata.mvalues: |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
506 for buffer in scenedata.vertexBuffers.mvalues: |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
507 assert buffer.vk.valid |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
508 buffer.destroy() |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
509 if scenedata.indexBuffer.vk.valid: |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
510 assert scenedata.indexBuffer.vk.valid |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
511 scenedata.indexBuffer.destroy() |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
512 for pipelineUniforms in scenedata.uniformBuffers.mvalues: |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
513 for buffer in pipelineUniforms.mitems: |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
514 assert buffer.vk.valid |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
515 buffer.destroy() |
845
19b5051d6eb7
fix: cleanup when multiple textures use same vulkan image
Sam <sam@basx.dev>
parents:
840
diff
changeset
|
516 var destroyedTextures: seq[VkImage] |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
517 for pipelineTextures in scenedata.textures.mvalues: |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
518 for textures in pipelineTextures.mvalues: |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
519 for texture in textures.mitems: |
845
19b5051d6eb7
fix: cleanup when multiple textures use same vulkan image
Sam <sam@basx.dev>
parents:
840
diff
changeset
|
520 if not destroyedTextures.contains(texture.image.vk): |
19b5051d6eb7
fix: cleanup when multiple textures use same vulkan image
Sam <sam@basx.dev>
parents:
840
diff
changeset
|
521 destroyedTextures.add texture.image.vk |
19b5051d6eb7
fix: cleanup when multiple textures use same vulkan image
Sam <sam@basx.dev>
parents:
840
diff
changeset
|
522 texture.destroy() |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
523 for descriptorPool in scenedata.descriptorPools.mvalues: |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
524 descriptorPool.destroy() |
845
19b5051d6eb7
fix: cleanup when multiple textures use same vulkan image
Sam <sam@basx.dev>
parents:
840
diff
changeset
|
525 for scene in renderer.scenedata.keys.toSeq: |
19b5051d6eb7
fix: cleanup when multiple textures use same vulkan image
Sam <sam@basx.dev>
parents:
840
diff
changeset
|
526 renderer.scenedata.del(scene) |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
527 renderer.emptyTexture.destroy() |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
528 renderer.renderPass.destroy() |
943
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
529 renderer.commandBufferPool.destroy() |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
530 renderer.swapchain.destroy() |