Mercurial > games > semicongine
annotate semiconginev2/old/renderer.nim @ 1218:56781cc0fc7c compiletime-tests
did: renamge main package
author | sam <sam@basx.dev> |
---|---|
date | Wed, 17 Jul 2024 21:01:37 +0700 |
parents | semicongine/old/renderer.nim@a3eb305bcac2 |
children |
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 |
1024 | 28 ShaderData = ref object |
29 descriptorPool: DescriptorPool | |
30 descriptorSets: seq[DescriptorSet] # len = n swapchain images | |
31 uniformBuffers: seq[Buffer] | |
32 textures: Table[string, seq[VulkanTexture]] | |
33 | |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
34 SceneData = ref object |
1024 | 35 drawables: seq[tuple[drawable: Drawable, mesh: Mesh]] |
36 vertexBuffers: Table[MemoryPerformanceHint, Buffer] | |
37 indexBuffer: Buffer | |
38 attributeLocation: Table[string, MemoryPerformanceHint] | |
39 vertexBufferOffsets: Table[(Mesh, string), uint64] | |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
40 materials: Table[MaterialType, seq[MaterialData]] |
1024 | 41 shaderData: Table[VkPipeline, ShaderData] |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
42 Renderer* = object |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
43 device: Device |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
44 renderPass: RenderPass |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
45 swapchain: Swapchain |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
46 scenedata: Table[Scene, SceneData] |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
47 emptyTexture: VulkanTexture |
943
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
48 queue: Queue |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
49 commandBufferPool: CommandBufferPool |
951
c6213a794ab0
fix: bad syncing mechanism, much better now (I think)
sam <sam@basx.dev>
parents:
950
diff
changeset
|
50 nextFrameReady: bool = false |
943
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
51 |
948 | 52 proc currentFrameCommandBuffer(renderer: Renderer): VkCommandBuffer = |
943
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
53 renderer.commandBufferPool.buffers[renderer.swapchain.currentInFlight] |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
54 |
1138 | 55 proc HasScene*(renderer: Renderer, scene: Scene): bool = |
1009 | 56 scene in renderer.scenedata |
57 | |
1138 | 58 proc InitRenderer*( |
949
0cf68e7336e8
add: a few more exposed parameters to the render API
sam <sam@basx.dev>
parents:
948
diff
changeset
|
59 device: Device, |
0cf68e7336e8
add: a few more exposed parameters to the render API
sam <sam@basx.dev>
parents:
948
diff
changeset
|
60 shaders: openArray[(MaterialType, ShaderConfiguration)], |
1136 | 61 clearColor = NewVec4f(0, 0, 0, 0), |
949
0cf68e7336e8
add: a few more exposed parameters to the render API
sam <sam@basx.dev>
parents:
948
diff
changeset
|
62 backFaceCulling = true, |
0cf68e7336e8
add: a few more exposed parameters to the render API
sam <sam@basx.dev>
parents:
948
diff
changeset
|
63 vSync = false, |
0cf68e7336e8
add: a few more exposed parameters to the render API
sam <sam@basx.dev>
parents:
948
diff
changeset
|
64 inFlightFrames = 2, |
1167 | 65 samples = VK_SAMPLE_COUNT_1_BIT, |
949
0cf68e7336e8
add: a few more exposed parameters to the render API
sam <sam@basx.dev>
parents:
948
diff
changeset
|
66 ): Renderer = |
1139
114f395b9144
did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents:
1138
diff
changeset
|
67 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
|
68 |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
69 result.device = device |
1167 | 70 result.renderPass = device.CreateRenderPass( |
71 shaders, | |
72 clearColor = clearColor, | |
73 backFaceCulling = backFaceCulling, | |
74 samples = samples | |
75 ) | |
1138 | 76 let swapchain = device.CreateSwapchain( |
943
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
77 result.renderPass.vk, |
1138 | 78 device.physicalDevice.GetSurfaceFormats().FilterSurfaceFormat(), |
949
0cf68e7336e8
add: a few more exposed parameters to the render API
sam <sam@basx.dev>
parents:
948
diff
changeset
|
79 vSync = vSync, |
0cf68e7336e8
add: a few more exposed parameters to the render API
sam <sam@basx.dev>
parents:
948
diff
changeset
|
80 inFlightFrames = inFlightFrames, |
1167 | 81 samples = samples, |
943
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
82 ) |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
83 if not swapchain.isSome: |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
84 raise newException(Exception, "Unable to create swapchain") |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
85 |
1138 | 86 result.queue = device.FirstGraphicsQueue().get() |
87 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
|
88 result.swapchain = swapchain.get() |
1138 | 89 result.emptyTexture = device.UploadTexture(result.queue, EMPTY_TEXTURE) |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
90 |
1024 | 91 func shadersForScene(renderer: Renderer, scene: Scene): seq[(MaterialType, ShaderPipeline)] = |
943
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
92 for (materialType, shaderPipeline) in renderer.renderPass.shaderPipelines: |
1138 | 93 if scene.UsesMaterial(materialType): |
1024 | 94 result.add (materialType, shaderPipeline) |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
95 |
1024 | 96 func vertexInputsForScene(renderer: Renderer, scene: Scene): seq[ShaderAttribute] = |
97 var found: Table[string, ShaderAttribute] | |
98 for (materialType, shaderPipeline) in renderer.shadersForScene(scene): | |
1138 | 99 for input in shaderPipeline.Inputs: |
1024 | 100 if found.contains(input.name): |
101 assert input.name == found[input.name].name, &"{input.name}: {input.name} != {found[input.name].name}" | |
102 assert input.theType == found[input.name].theType, &"{input.name}: {input.theType} != {found[input.name].theType}" | |
103 assert input.arrayCount == found[input.name].arrayCount, &"{input.name}: {input.arrayCount} != {found[input.name].arrayCount}" | |
104 assert input.memoryPerformanceHint == found[input.name].memoryPerformanceHint, &"{input.name}: {input.memoryPerformanceHint} != {found[input.name].memoryPerformanceHint}" | |
105 else: | |
106 result.add input | |
107 found[input.name] = input | |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
108 |
1138 | 109 proc SetupDrawableBuffers*(renderer: var Renderer, scene: var Scene) = |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
110 assert not (scene in renderer.scenedata) |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
111 |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
112 var scenedata = SceneData() |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
113 |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
114 # 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
|
115 for mesh in scene.meshes: |
1008
443e8090614c
fix: some mesh attributes will be handled automatically, ignore them when doing mesh-material-shader compatability-check
sam <sam@basx.dev>
parents:
961
diff
changeset
|
116 assert mesh.material != nil, "Mesh {mesh} has no material assigned" |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
117 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
|
118 scenedata.materials[mesh.material.theType] = @[] |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
119 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
|
120 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
|
121 |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
122 # automatically populate material and tranform attributes |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
123 for mesh in scene.meshes: |
1138 | 124 if not (TRANSFORM_ATTRIB in mesh[].Attributes): |
125 mesh[].InitInstanceAttribute(TRANSFORM_ATTRIB, Unit4) | |
126 if not (MATERIALINDEX_ATTRIBUTE in mesh[].Attributes): | |
127 mesh[].InitInstanceAttribute(MATERIALINDEX_ATTRIBUTE, uint16(scenedata.materials[mesh.material.theType].find(mesh.material))) | |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
128 |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
129 # 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
|
130 var indicesBufferSize = 0'u64 |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
131 for mesh in scene.meshes: |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
132 if mesh[].indexType != MeshIndexType.None: |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
133 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
|
134 of MeshIndexType.None: 0'u64 |
1172 | 135 of Tiny: 1'u64 |
959
c104dbf5bbb8
did: adjust integer sizes to match vulkan API (more) directly
sam <sam@basx.dev>
parents:
956
diff
changeset
|
136 of Small: 2'u64 |
c104dbf5bbb8
did: adjust integer sizes to match vulkan API (more) directly
sam <sam@basx.dev>
parents:
956
diff
changeset
|
137 of Big: 4'u64 |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
138 # index value alignment required by Vulkan |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
139 if indicesBufferSize mod indexAlignment != 0: |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
140 indicesBufferSize += indexAlignment - (indicesBufferSize mod indexAlignment) |
1138 | 141 indicesBufferSize += mesh[].IndexSize |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
142 if indicesBufferSize > 0: |
1138 | 143 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
|
144 size = indicesBufferSize, |
5392cbd9db41
fix: material handlinge, did: formatting, add: support for multi-material texts
Sam <sam@basx.dev>
parents:
876
diff
changeset
|
145 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
|
146 requireMappable = false, |
5392cbd9db41
fix: material handlinge, did: formatting, add: support for multi-material texts
Sam <sam@basx.dev>
parents:
876
diff
changeset
|
147 preferVRAM = true, |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
148 ) |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
149 |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
150 # calculcate offsets for attributes in vertex buffers |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
151 # 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
|
152 var perLocationSizes: Table[MemoryPerformanceHint, uint64] |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
153 for hint in MemoryPerformanceHint: |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
154 perLocationSizes[hint] = 0 |
899 | 155 |
1024 | 156 let sceneVertexInputs = renderer.vertexInputsForScene(scene) |
157 let sceneShaders = renderer.shadersForScene(scene) | |
899 | 158 |
1024 | 159 for (materialType, shaderPipeline) in sceneShaders: |
160 scenedata.shaderData[shaderPipeline.vk] = ShaderData() | |
161 | |
162 for vertexAttribute in sceneVertexInputs: | |
163 scenedata.attributeLocation[vertexAttribute.name] = vertexAttribute.memoryPerformanceHint | |
164 # setup one buffer per vertexAttribute-location-type | |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
165 for mesh in scene.meshes: |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
166 # 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
|
167 # we need to expand the buffer size as well, therefore considering alignment already here as well |
1024 | 168 if perLocationSizes[vertexAttribute.memoryPerformanceHint] mod VERTEX_ATTRIB_ALIGNMENT != 0: |
169 perLocationSizes[vertexAttribute.memoryPerformanceHint] += VERTEX_ATTRIB_ALIGNMENT - (perLocationSizes[vertexAttribute.memoryPerformanceHint] mod VERTEX_ATTRIB_ALIGNMENT) | |
1138 | 170 perLocationSizes[vertexAttribute.memoryPerformanceHint] += mesh[].AttributeSize(vertexAttribute.name) |
840
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 vertex buffers |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
173 for memoryPerformanceHint, bufferSize in perLocationSizes.pairs: |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
174 if bufferSize > 0: |
1138 | 175 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
|
176 size = bufferSize, |
5392cbd9db41
fix: material handlinge, did: formatting, add: support for multi-material texts
Sam <sam@basx.dev>
parents:
876
diff
changeset
|
177 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
|
178 requireMappable = memoryPerformanceHint == PreferFastWrite, |
5392cbd9db41
fix: material handlinge, did: formatting, add: support for multi-material texts
Sam <sam@basx.dev>
parents:
876
diff
changeset
|
179 preferVRAM = true, |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
180 ) |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
181 |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
182 # 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
|
183 var perLocationOffsets: Table[MemoryPerformanceHint, uint64] |
c104dbf5bbb8
did: adjust integer sizes to match vulkan API (more) directly
sam <sam@basx.dev>
parents:
956
diff
changeset
|
184 var indexBufferOffset = 0'u64 |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
185 for hint in MemoryPerformanceHint: |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
186 perLocationOffsets[hint] = 0 |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
187 |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
188 for mesh in scene.meshes: |
1024 | 189 for attribute in sceneVertexInputs: |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
190 scenedata.vertexBufferOffsets[(mesh, attribute.name)] = perLocationOffsets[attribute.memoryPerformanceHint] |
1138 | 191 if mesh[].Attributes.contains(attribute.name): |
192 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
|
193 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
|
194 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
|
195 |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
196 # 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
|
197 var offsets: Table[VkPipeline, seq[(string, MemoryPerformanceHint, uint64)]] |
1024 | 198 for (materialType, shaderPipeline) in sceneShaders: |
199 offsets[shaderPipeline.vk] = newSeq[(string, MemoryPerformanceHint, uint64)]() | |
1138 | 200 for attribute in shaderPipeline.Inputs: |
1024 | 201 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
|
202 |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
203 # create drawables |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
204 let indexed = mesh.indexType != MeshIndexType.None |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
205 var drawable = Drawable( |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
206 name: mesh.name, |
1138 | 207 elementCount: if indexed: mesh[].IndicesCount else: mesh[].vertexCount, |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
208 bufferOffsets: offsets, |
1138 | 209 instanceCount: mesh[].InstanceCount, |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
210 indexed: indexed, |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
211 ) |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
212 if indexed: |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
213 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
|
214 of MeshIndexType.None: 0'u64 |
1172 | 215 of Tiny: 1'u64 |
959
c104dbf5bbb8
did: adjust integer sizes to match vulkan API (more) directly
sam <sam@basx.dev>
parents:
956
diff
changeset
|
216 of Small: 2'u64 |
c104dbf5bbb8
did: adjust integer sizes to match vulkan API (more) directly
sam <sam@basx.dev>
parents:
956
diff
changeset
|
217 of Big: 4'u64 |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
218 # index value alignment required by Vulkan |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
219 if indexBufferOffset mod indexAlignment != 0: |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
220 indexBufferOffset += indexAlignment - (indexBufferOffset mod indexAlignment) |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
221 drawable.indexBufferOffset = indexBufferOffset |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
222 drawable.indexType = mesh.indexType |
1138 | 223 var (pdata, size) = mesh[].GetRawIndexData() |
224 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
|
225 indexBufferOffset += size |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
226 scenedata.drawables.add (drawable, mesh) |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
227 |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
228 # setup uniforms and textures (anything descriptor) |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
229 var uploadedTextures: Table[Texture, VulkanTexture] |
1024 | 230 for (materialType, shaderPipeline) in sceneShaders: |
231 # gather textures | |
1138 | 232 for textureAttribute in shaderPipeline.Samplers: |
1026 | 233 scenedata.shaderData[shaderPipeline.vk].textures[textureAttribute.name] = newSeq[VulkanTexture]() |
234 if scene.shaderGlobals.contains(textureAttribute.name): | |
235 for textureValue in scene.shaderGlobals[textureAttribute.name][Texture][]: | |
1024 | 236 if not uploadedTextures.contains(textureValue): |
1138 | 237 uploadedTextures[textureValue] = renderer.device.UploadTexture(renderer.queue, textureValue) |
1026 | 238 scenedata.shaderData[shaderPipeline.vk].textures[textureAttribute.name].add uploadedTextures[textureValue] |
1024 | 239 else: |
240 var foundTexture = false | |
1138 | 241 for material in scene.GetMaterials(materialType): |
242 if material.HasMatchingAttribute(textureAttribute): | |
1024 | 243 foundTexture = true |
1026 | 244 let value = material[textureAttribute.name, Texture][] |
245 assert value.len == 1, &"Mesh material attribute '{textureAttribute.name}' has texture-array, but only single textures are allowed" | |
1024 | 246 if not uploadedTextures.contains(value[0]): |
1138 | 247 uploadedTextures[value[0]] = renderer.device.UploadTexture(renderer.queue, value[0]) |
1026 | 248 scenedata.shaderData[shaderPipeline.vk].textures[textureAttribute.name].add uploadedTextures[value[0]] |
249 assert foundTexture, &"No texture found in shaderGlobals or materials for '{textureAttribute.name}'" | |
250 let nTextures = scenedata.shaderData[shaderPipeline.vk].textures[textureAttribute.name].len.uint32 | |
251 assert (textureAttribute.arrayCount == 0 and nTextures == 1) or textureAttribute.arrayCount >= nTextures, &"Shader assigned to render '{materialType}' expected {textureAttribute.arrayCount} textures for '{textureAttribute.name}' but got {nTextures}" | |
252 if textureAttribute.arrayCount < nTextures: | |
253 warn &"Shader assigned to render '{materialType}' expected {textureAttribute.arrayCount} textures for '{textureAttribute.name}' but got {nTextures}" | |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
254 |
1024 | 255 # gather uniform sizes |
256 var uniformBufferSize = 0'u64 | |
1138 | 257 for uniform in shaderPipeline.Uniforms: |
1137 | 258 uniformBufferSize += uniform.Size |
1024 | 259 if uniformBufferSize > 0: |
260 for frame_i in 0 ..< renderer.swapchain.inFlightFrames: | |
1138 | 261 scenedata.shaderData[shaderPipeline.vk].uniformBuffers.add renderer.device.CreateBuffer( |
1024 | 262 size = uniformBufferSize, |
263 usage = [VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT], | |
264 requireMappable = true, | |
265 preferVRAM = true, | |
266 ) | |
882
5392cbd9db41
fix: material handlinge, did: formatting, add: support for multi-material texts
Sam <sam@basx.dev>
parents:
876
diff
changeset
|
267 |
1024 | 268 # TODO: rework the whole descriptor/pool/layout stuff, a bit unclear |
269 var poolsizes = @[(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, renderer.swapchain.inFlightFrames.uint32)] | |
270 var nTextures = 0'u32 | |
271 for descriptor in shaderPipeline.descriptorSetLayout.descriptors: | |
272 if descriptor.thetype == ImageSampler: | |
273 nTextures += descriptor.count | |
274 if nTextures > 0: | |
275 poolsizes.add (VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, nTextures * renderer.swapchain.inFlightFrames.uint32) | |
1138 | 276 scenedata.shaderData[shaderPipeline.vk].descriptorPool = 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
|
277 |
1138 | 278 scenedata.shaderData[shaderPipeline.vk].descriptorSets = shaderPipeline.SetupDescriptors( |
1024 | 279 scenedata.shaderData[shaderPipeline.vk].descriptorPool, |
280 scenedata.shaderData[shaderPipeline.vk].uniformBuffers, | |
281 scenedata.shaderData[shaderPipeline.vk].textures, | |
282 inFlightFrames = renderer.swapchain.inFlightFrames, | |
283 emptyTexture = renderer.emptyTexture, | |
284 ) | |
285 for frame_i in 0 ..< renderer.swapchain.inFlightFrames: | |
1138 | 286 scenedata.shaderData[shaderPipeline.vk].descriptorSets[frame_i].WriteDescriptorSet() |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
287 |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
288 renderer.scenedata[scene] = scenedata |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
289 |
1138 | 290 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
|
291 assert scene in renderer.scenedata |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
292 |
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
|
293 var addedBarrier = false; |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
294 for (drawable, mesh) in renderer.scenedata[scene].drawables.mitems: |
1138 | 295 if mesh[].Attributes.contains(TRANSFORM_ATTRIB): |
296 mesh[].UpdateInstanceTransforms(TRANSFORM_ATTRIB) | |
297 let attrs = (if forceAll: mesh[].Attributes else: mesh[].DirtyAttributes) | |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
298 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
|
299 # 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
|
300 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
|
301 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
|
302 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
|
303 # 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
|
304 # 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
|
305 # (remark: ...I think..., I am pretty new to this sync stuff) |
1138 | 306 if not renderer.scenedata[scene].vertexBuffers[memoryPerformanceHint].CanMap and not addedBarrier: |
307 WithSingleUseCommandBuffer(renderer.device, renderer.queue, commandBuffer): | |
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
|
308 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
|
309 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
|
310 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
|
311 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
|
312 ) |
1138 | 313 commandBuffer.PipelineBarrier( |
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
|
314 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
|
315 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
|
316 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
|
317 ) |
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
|
318 addedBarrier = true |
1138 | 319 renderer.scenedata[scene].vertexBuffers[memoryPerformanceHint].SetData( |
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
|
320 renderer.queue, |
1138 | 321 mesh[].GetPointer(attribute), |
322 mesh[].AttributeSize(attribute), | |
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
|
323 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
|
324 ) |
1138 | 325 mesh[].ClearDirtyAttributes() |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
326 |
1138 | 327 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
|
328 assert scene in renderer.scenedata |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
329 |
1138 | 330 let dirty = scene.DirtyShaderGlobals |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
331 |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
332 if forceAll: |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
333 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
|
334 elif dirty.len > 0: |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
335 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
|
336 |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
337 # loop over all used shaders/pipelines |
1024 | 338 for (materialType, shaderPipeline) in renderer.shadersForScene(scene): |
339 if renderer.scenedata[scene].shaderData[shaderPipeline.vk].uniformBuffers.len > 0: | |
943
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
340 var dirtyMaterialAttribs: seq[string] |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
341 for material in renderer.scenedata[scene].materials[materialType].mitems: |
1138 | 342 dirtyMaterialAttribs.add material.DirtyAttributes |
343 material.ClearDirtyAttributes() | |
1139
114f395b9144
did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents:
1138
diff
changeset
|
344 assert renderer.scenedata[scene].shaderData[shaderPipeline.vk].uniformBuffers[renderer.swapchain.currentInFlight].vk.Valid |
943
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
345 if forceAll: |
1024 | 346 for buffer in renderer.scenedata[scene].shaderData[shaderPipeline.vk].uniformBuffers: |
1139
114f395b9144
did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents:
1138
diff
changeset
|
347 assert buffer.vk.Valid |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
348 |
959
c104dbf5bbb8
did: adjust integer sizes to match vulkan API (more) directly
sam <sam@basx.dev>
parents:
956
diff
changeset
|
349 var offset = 0'u64 |
943
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
350 # loop over all uniforms of the shader-shaderPipeline |
1138 | 351 for uniform in shaderPipeline.Uniforms: |
943
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
352 if dirty.contains(uniform.name) or dirtyMaterialAttribs.contains(uniform.name) or forceAll: # only update uniforms if necessary |
1137 | 353 var value = InitDataList(uniform.theType) |
943
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
354 if scene.shaderGlobals.hasKey(uniform.name): |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
355 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
|
356 value = scene.shaderGlobals[uniform.name] |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
357 else: |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
358 var foundValue = false |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
359 for material in renderer.scenedata[scene].materials[materialType]: |
1138 | 360 if material.HasMatchingAttribute(uniform): |
1137 | 361 value.AppendValues(material[uniform.name]) |
943
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
362 foundValue = true |
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
363 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
|
364 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
|
365 if value.len.uint <= uniform.arrayCount: |
943
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
366 debug &"Uniform '{uniform.name}' found has short length (shader declares {uniform.arrayCount} but shaderGlobals and materials provide {value.len})" |
1137 | 367 assert value.Size <= uniform.Size, &"During uniform update: gathered value has size {value.Size} but uniform expects size {uniform.Size}" |
368 if value.Size < uniform.Size: | |
369 debug &"During uniform update: gathered value has size {value.Size} but uniform expects size {uniform.Size}" | |
943
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
370 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
|
371 # 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
|
372 # 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
|
373 # therefore we have to update the uniform values in all buffers, of all inFlightframes (usually 2) |
1024 | 374 for buffer in renderer.scenedata[scene].shaderData[shaderPipeline.vk].uniformBuffers: |
1138 | 375 buffer.SetData(renderer.queue, value.GetPointer(), value.Size, offset) |
1137 | 376 offset += uniform.Size |
1138 | 377 scene.ClearDirtyShaderGlobals() |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
378 |
1157
dd757eb5ca86
fix: minimizing window on windows breaks everything, also: do not try to acquire next frame for inifinity
sam@sambook-windows.localdomain
parents:
1139
diff
changeset
|
379 proc StartNewFrame*(renderer: var Renderer): bool = |
1158
f32359ffd882
fix: now something wrong with swapchain on linux XD
sam <sam@basx.dev>
parents:
1157
diff
changeset
|
380 # first, we need to await the next free frame from the swapchain |
1157
dd757eb5ca86
fix: minimizing window on windows breaks everything, also: do not try to acquire next frame for inifinity
sam@sambook-windows.localdomain
parents:
1139
diff
changeset
|
381 if not renderer.swapchain.AcquireNextFrame(): |
1158
f32359ffd882
fix: now something wrong with swapchain on linux XD
sam <sam@basx.dev>
parents:
1157
diff
changeset
|
382 # so, there was a problem while acquiring the frame |
f32359ffd882
fix: now something wrong with swapchain on linux XD
sam <sam@basx.dev>
parents:
1157
diff
changeset
|
383 # lets first take a break (not sure if this helps anything) |
953 | 384 checkVkResult renderer.device.vk.vkDeviceWaitIdle() |
1158
f32359ffd882
fix: now something wrong with swapchain on linux XD
sam <sam@basx.dev>
parents:
1157
diff
changeset
|
385 # now, first thing is, we recreate the swapchain, because a invalid swapchain |
f32359ffd882
fix: now something wrong with swapchain on linux XD
sam <sam@basx.dev>
parents:
1157
diff
changeset
|
386 # is a common reason for the inability to acquire the next frame |
1138 | 387 let res = renderer.swapchain.Recreate() |
1139
114f395b9144
did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents:
1138
diff
changeset
|
388 if res.isSome: |
1158
f32359ffd882
fix: now something wrong with swapchain on linux XD
sam <sam@basx.dev>
parents:
1157
diff
changeset
|
389 # okay, swapchain recreation worked |
f32359ffd882
fix: now something wrong with swapchain on linux XD
sam <sam@basx.dev>
parents:
1157
diff
changeset
|
390 # Now we can swap old and new swapchain |
f32359ffd882
fix: now something wrong with swapchain on linux XD
sam <sam@basx.dev>
parents:
1157
diff
changeset
|
391 # the vkDeviceWaitIdle makes the resizing of windows not super smooth, |
f32359ffd882
fix: now something wrong with swapchain on linux XD
sam <sam@basx.dev>
parents:
1157
diff
changeset
|
392 # but things seem to be more stable this way |
1139
114f395b9144
did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents:
1138
diff
changeset
|
393 var oldSwapchain = renderer.swapchain |
114f395b9144
did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents:
1138
diff
changeset
|
394 renderer.swapchain = res.get() |
114f395b9144
did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents:
1138
diff
changeset
|
395 checkVkResult renderer.device.vk.vkDeviceWaitIdle() |
114f395b9144
did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents:
1138
diff
changeset
|
396 oldSwapchain.Destroy() |
1158
f32359ffd882
fix: now something wrong with swapchain on linux XD
sam <sam@basx.dev>
parents:
1157
diff
changeset
|
397 # NOW, we still have to acquire that next frame with the NEW swapchain |
f32359ffd882
fix: now something wrong with swapchain on linux XD
sam <sam@basx.dev>
parents:
1157
diff
changeset
|
398 # if that fails, I don't know what to smart to do... |
f32359ffd882
fix: now something wrong with swapchain on linux XD
sam <sam@basx.dev>
parents:
1157
diff
changeset
|
399 if not renderer.swapchain.AcquireNextFrame(): |
f32359ffd882
fix: now something wrong with swapchain on linux XD
sam <sam@basx.dev>
parents:
1157
diff
changeset
|
400 return false |
1157
dd757eb5ca86
fix: minimizing window on windows breaks everything, also: do not try to acquire next frame for inifinity
sam@sambook-windows.localdomain
parents:
1139
diff
changeset
|
401 else: |
1158
f32359ffd882
fix: now something wrong with swapchain on linux XD
sam <sam@basx.dev>
parents:
1157
diff
changeset
|
402 # dang, swapchain could not be recreated. Some bigger issues is at hand... |
1157
dd757eb5ca86
fix: minimizing window on windows breaks everything, also: do not try to acquire next frame for inifinity
sam@sambook-windows.localdomain
parents:
1139
diff
changeset
|
403 return false |
951
c6213a794ab0
fix: bad syncing mechanism, much better now (I think)
sam <sam@basx.dev>
parents:
950
diff
changeset
|
404 renderer.nextFrameReady = true |
1157
dd757eb5ca86
fix: minimizing window on windows breaks everything, also: do not try to acquire next frame for inifinity
sam@sambook-windows.localdomain
parents:
1139
diff
changeset
|
405 return 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
|
406 |
1138 | 407 proc Render*(renderer: var Renderer, scene: Scene) = |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
408 assert scene in renderer.scenedata |
951
c6213a794ab0
fix: bad syncing mechanism, much better now (I think)
sam <sam@basx.dev>
parents:
950
diff
changeset
|
409 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
|
410 |
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
|
411 # preparation |
1138 | 412 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
|
413 |
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
|
414 # debug output |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
415 debug "Scene buffers:" |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
416 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
|
417 debug " ", location, ": ", buffer |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
418 debug " Index buffer: ", renderer.scenedata[scene].indexBuffer |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
419 |
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
|
420 # draw all meshes |
943
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
421 for (materialType, shaderPipeline) in renderer.renderPass.shaderPipelines: |
1138 | 422 if scene.UsesMaterial(materialType): |
943
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
423 debug &"Start shaderPipeline for '{materialType}'" |
948 | 424 renderer.currentFrameCommandBuffer.vkCmdBindPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS, shaderPipeline.vk) |
1024 | 425 renderer.currentFrameCommandBuffer.vkCmdBindDescriptorSets( |
426 VK_PIPELINE_BIND_POINT_GRAPHICS, | |
427 shaderPipeline.layout, | |
428 0, | |
429 1, | |
430 addr(renderer.scenedata[scene].shaderData[shaderPipeline.vk].descriptorSets[renderer.swapchain.currentInFlight].vk), | |
431 0, | |
432 nil | |
433 ) | |
943
925197a1ec95
add: some sync (not finished), simplify renderpass af
sam <sam@basx.dev>
parents:
937
diff
changeset
|
434 for (drawable, mesh) in renderer.scenedata[scene].drawables.filterIt(it[1].visible and it[1].material.theType == materialType): |
1138 | 435 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
|
436 |
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
|
437 # done rendering |
1138 | 438 renderer.currentFrameCommandBuffer.EndRenderCommands() |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
439 |
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
|
440 # swap framebuffer |
1138 | 441 if not renderer.swapchain.Swap(renderer.queue, renderer.currentFrameCommandBuffer): |
442 let res = renderer.swapchain.Recreate() | |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
443 if res.isSome: |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
444 var oldSwapchain = renderer.swapchain |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
445 renderer.swapchain = res.get() |
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
446 checkVkResult renderer.device.vk.vkDeviceWaitIdle() |
1138 | 447 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
|
448 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
|
449 renderer.nextFrameReady = false |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
450 |
1138 | 451 func Valid*(renderer: Renderer): bool = |
1139
114f395b9144
did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents:
1138
diff
changeset
|
452 renderer.device.vk.Valid |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
453 |
1138 | 454 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
|
455 checkVkResult renderer.device.vk.vkDeviceWaitIdle() |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
456 var scenedata = renderer.scenedata[scene] |
1024 | 457 |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
458 for buffer in scenedata.vertexBuffers.mvalues: |
1139
114f395b9144
did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents:
1138
diff
changeset
|
459 assert buffer.vk.Valid |
1138 | 460 buffer.Destroy() |
1024 | 461 |
1139
114f395b9144
did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents:
1138
diff
changeset
|
462 if scenedata.indexBuffer.vk.Valid: |
114f395b9144
did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents:
1138
diff
changeset
|
463 assert scenedata.indexBuffer.vk.Valid |
1138 | 464 scenedata.indexBuffer.Destroy() |
1024 | 465 |
466 var destroyedTextures: seq[VkImage] | |
467 | |
468 for (vkPipeline, shaderData) in scenedata.shaderData.mpairs: | |
469 | |
470 for buffer in shaderData.uniformBuffers.mitems: | |
1139
114f395b9144
did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents:
1138
diff
changeset
|
471 assert buffer.vk.Valid |
1138 | 472 buffer.Destroy() |
1024 | 473 |
474 for textures in shaderData.textures.mvalues: | |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
475 for texture in textures.mitems: |
845
19b5051d6eb7
fix: cleanup when multiple textures use same vulkan image
Sam <sam@basx.dev>
parents:
840
diff
changeset
|
476 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
|
477 destroyedTextures.add texture.image.vk |
1138 | 478 texture.Destroy() |
1024 | 479 |
1138 | 480 shaderData.descriptorPool.Destroy() |
1024 | 481 |
845
19b5051d6eb7
fix: cleanup when multiple textures use same vulkan image
Sam <sam@basx.dev>
parents:
840
diff
changeset
|
482 renderer.scenedata.del(scene) |
840
44ec744fbedc
did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff
changeset
|
483 |
1138 | 484 proc Destroy*(renderer: var Renderer) = |
845
19b5051d6eb7
fix: cleanup when multiple textures use same vulkan image
Sam <sam@basx.dev>
parents:
840
diff
changeset
|
485 for scene in renderer.scenedata.keys.toSeq: |
1138 | 486 renderer.Destroy(scene) |
1024 | 487 assert renderer.scenedata.len == 0 |
1138 | 488 renderer.emptyTexture.Destroy() |
489 renderer.renderPass.Destroy() | |
490 renderer.commandBufferPool.Destroy() | |
491 renderer.swapchain.Destroy() |