Mercurial > games > semicongine
annotate src/semicongine/renderer.nim @ 341:6cc16b62e497
did: small font improvments
author | Sam <sam@basx.dev> |
---|---|
date | Fri, 08 Sep 2023 00:34:24 +0700 |
parents | 01ccd1393e7f |
children | 8c67f67b62fb |
rev | line source |
---|---|
127 | 1 import std/options |
128
9901ec3831d1
did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents:
127
diff
changeset
|
2 import std/tables |
9901ec3831d1
did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents:
127
diff
changeset
|
3 import std/strformat |
326
4ec852355750
fix: many issues, better mesh-handling, still need to cope with different binding numbers when using different pipelines...
Sam <sam@basx.dev>
parents:
323
diff
changeset
|
4 import std/sequtils |
128
9901ec3831d1
did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents:
127
diff
changeset
|
5 import std/logging |
127 | 6 |
206
7f921d7d0a2b
did: small refactoring of module structure
Sam <sam@basx.dev>
parents:
205
diff
changeset
|
7 import ./core |
128
9901ec3831d1
did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents:
127
diff
changeset
|
8 import ./vulkan/buffer |
127 | 9 import ./vulkan/device |
128
9901ec3831d1
did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents:
127
diff
changeset
|
10 import ./vulkan/drawable |
322
6dab370d1758
add: first, incomplete version of material use
Sam <sam@basx.dev>
parents:
321
diff
changeset
|
11 import ./vulkan/physicaldevice |
128
9901ec3831d1
did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents:
127
diff
changeset
|
12 import ./vulkan/pipeline |
127 | 13 import ./vulkan/renderpass |
14 import ./vulkan/swapchain | |
322
6dab370d1758
add: first, incomplete version of material use
Sam <sam@basx.dev>
parents:
321
diff
changeset
|
15 import ./vulkan/shader |
189
df92519d4d68
add: initial code for texture support, not finished, had to completely refactor how to handle material-data (ie scene-wide data, sorry if you ever read this
Sam <sam@basx.dev>
parents:
164
diff
changeset
|
16 import ./vulkan/descriptor |
df92519d4d68
add: initial code for texture support, not finished, had to completely refactor how to handle material-data (ie scene-wide data, sorry if you ever read this
Sam <sam@basx.dev>
parents:
164
diff
changeset
|
17 import ./vulkan/image |
127 | 18 |
247 | 19 import ./scene |
128
9901ec3831d1
did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents:
127
diff
changeset
|
20 import ./mesh |
9901ec3831d1
did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents:
127
diff
changeset
|
21 |
326
4ec852355750
fix: many issues, better mesh-handling, still need to cope with different binding numbers when using different pipelines...
Sam <sam@basx.dev>
parents:
323
diff
changeset
|
22 const TRANSFORMATTRIBUTE = "transform" |
330
04531bec3583
did: remove some stuff from the heap, maybe nicer?
Sam <sam@basx.dev>
parents:
329
diff
changeset
|
23 const VERTEX_ATTRIB_ALIGNMENT = 4 # used for buffer alignment |
326
4ec852355750
fix: many issues, better mesh-handling, still need to cope with different binding numbers when using different pipelines...
Sam <sam@basx.dev>
parents:
323
diff
changeset
|
24 |
127 | 25 type |
339
55c9183f7ece
add: nice mesh API, fix: copying of whole scenedata all the time
Sam <sam@basx.dev>
parents:
334
diff
changeset
|
26 SceneData = ref object |
331
05fb85ba97dd
did: undid using meshes as values, ref is much better, fix a few things, fix a few huge performance issues
Sam <sam@basx.dev>
parents:
330
diff
changeset
|
27 drawables*: seq[tuple[drawable: Drawable, mesh: Mesh]] |
156 | 28 vertexBuffers*: Table[MemoryPerformanceHint, Buffer] |
128
9901ec3831d1
did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents:
127
diff
changeset
|
29 indexBuffer*: Buffer |
266
fd1a95f433d1
add: better material loading system, still far from great
Sam <sam@basx.dev>
parents:
263
diff
changeset
|
30 uniformBuffers*: Table[VkPipeline, seq[Buffer]] # one per frame-in-flight |
243
3b388986c7fd
did: refactor texture data structures, add more complete (untested) material import
Sam <sam@basx.dev>
parents:
242
diff
changeset
|
31 textures*: Table[string, seq[VulkanTexture]] # per frame-in-flight |
156 | 32 attributeLocation*: Table[string, MemoryPerformanceHint] |
331
05fb85ba97dd
did: undid using meshes as values, ref is much better, fix a few things, fix a few huge performance issues
Sam <sam@basx.dev>
parents:
330
diff
changeset
|
33 vertexBufferOffsets*: Table[(Mesh, string), int] |
323
9defff46da48
add: first complete working version of multiple materials and shaders per scene, yipie :)
Sam <sam@basx.dev>
parents:
322
diff
changeset
|
34 descriptorPools*: Table[VkPipeline, DescriptorPool] |
241 | 35 descriptorSets*: Table[VkPipeline, seq[DescriptorSet]] |
326
4ec852355750
fix: many issues, better mesh-handling, still need to cope with different binding numbers when using different pipelines...
Sam <sam@basx.dev>
parents:
323
diff
changeset
|
36 materials: seq[Material] |
128
9901ec3831d1
did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents:
127
diff
changeset
|
37 Renderer* = object |
9901ec3831d1
did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents:
127
diff
changeset
|
38 device: Device |
127 | 39 surfaceFormat: VkSurfaceFormatKHR |
131
11666d28e04d
add: recreation of swapchain (at least on linux, windows will likely fail, needs testing
Sam <sam@basx.dev>
parents:
129
diff
changeset
|
40 renderPass: RenderPass |
127 | 41 swapchain: Swapchain |
189
df92519d4d68
add: initial code for texture support, not finished, had to completely refactor how to handle material-data (ie scene-wide data, sorry if you ever read this
Sam <sam@basx.dev>
parents:
164
diff
changeset
|
42 scenedata: Table[Scene, SceneData] |
323
9defff46da48
add: first complete working version of multiple materials and shaders per scene, yipie :)
Sam <sam@basx.dev>
parents:
322
diff
changeset
|
43 emptyTexture: VulkanTexture |
127 | 44 |
330
04531bec3583
did: remove some stuff from the heap, maybe nicer?
Sam <sam@basx.dev>
parents:
329
diff
changeset
|
45 func usesMaterial(scene: Scene, materialName: string): bool = |
04531bec3583
did: remove some stuff from the heap, maybe nicer?
Sam <sam@basx.dev>
parents:
329
diff
changeset
|
46 return scene.meshes.anyIt(it.material.name == materialName) |
329
69e18f69713b
add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents:
328
diff
changeset
|
47 |
322
6dab370d1758
add: first, incomplete version of material use
Sam <sam@basx.dev>
parents:
321
diff
changeset
|
48 proc initRenderer*(device: Device, shaders: Table[string, ShaderConfiguration], clearColor=Vec4f([0.8'f32, 0.8'f32, 0.8'f32, 1'f32])): Renderer = |
127 | 49 assert device.vk.valid |
322
6dab370d1758
add: first, incomplete version of material use
Sam <sam@basx.dev>
parents:
321
diff
changeset
|
50 |
128
9901ec3831d1
did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents:
127
diff
changeset
|
51 result.device = device |
322
6dab370d1758
add: first, incomplete version of material use
Sam <sam@basx.dev>
parents:
321
diff
changeset
|
52 result.renderPass = device.simpleForwardRenderPass(shaders, clearColor=clearColor) |
127 | 53 result.surfaceFormat = device.physicalDevice.getSurfaceFormats().filterSurfaceFormat() |
131
11666d28e04d
add: recreation of swapchain (at least on linux, windows will likely fail, needs testing
Sam <sam@basx.dev>
parents:
129
diff
changeset
|
54 # use last renderpass as output for swapchain |
11666d28e04d
add: recreation of swapchain (at least on linux, windows will likely fail, needs testing
Sam <sam@basx.dev>
parents:
129
diff
changeset
|
55 let swapchain = device.createSwapchain(result.renderPass.vk, result.surfaceFormat, device.firstGraphicsQueue().get().family) |
11666d28e04d
add: recreation of swapchain (at least on linux, windows will likely fail, needs testing
Sam <sam@basx.dev>
parents:
129
diff
changeset
|
56 if not swapchain.isSome: |
127 | 57 raise newException(Exception, "Unable to create swapchain") |
322
6dab370d1758
add: first, incomplete version of material use
Sam <sam@basx.dev>
parents:
321
diff
changeset
|
58 |
131
11666d28e04d
add: recreation of swapchain (at least on linux, windows will likely fail, needs testing
Sam <sam@basx.dev>
parents:
129
diff
changeset
|
59 result.swapchain = swapchain.get() |
323
9defff46da48
add: first complete working version of multiple materials and shaders per scene, yipie :)
Sam <sam@basx.dev>
parents:
322
diff
changeset
|
60 result.emptyTexture = device.uploadTexture(EMPTYTEXTURE) |
128
9901ec3831d1
did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents:
127
diff
changeset
|
61 |
326
4ec852355750
fix: many issues, better mesh-handling, still need to cope with different binding numbers when using different pipelines...
Sam <sam@basx.dev>
parents:
323
diff
changeset
|
62 func inputs(renderer: Renderer, scene: Scene): seq[ShaderAttribute] = |
4ec852355750
fix: many issues, better mesh-handling, still need to cope with different binding numbers when using different pipelines...
Sam <sam@basx.dev>
parents:
323
diff
changeset
|
63 var found: Table[string, ShaderAttribute] |
322
6dab370d1758
add: first, incomplete version of material use
Sam <sam@basx.dev>
parents:
321
diff
changeset
|
64 for i in 0 ..< renderer.renderPass.subpasses.len: |
330
04531bec3583
did: remove some stuff from the heap, maybe nicer?
Sam <sam@basx.dev>
parents:
329
diff
changeset
|
65 for materialName, pipeline in renderer.renderPass.subpasses[i].pipelines.pairs: |
04531bec3583
did: remove some stuff from the heap, maybe nicer?
Sam <sam@basx.dev>
parents:
329
diff
changeset
|
66 if scene.usesMaterial(materialName): |
326
4ec852355750
fix: many issues, better mesh-handling, still need to cope with different binding numbers when using different pipelines...
Sam <sam@basx.dev>
parents:
323
diff
changeset
|
67 for input in pipeline.inputs: |
4ec852355750
fix: many issues, better mesh-handling, still need to cope with different binding numbers when using different pipelines...
Sam <sam@basx.dev>
parents:
323
diff
changeset
|
68 if found.contains(input.name): |
341 | 69 assert input.name == found[input.name].name, &"{input.name} != {found[input.name].name}" |
70 assert input.theType == found[input.name].theType, &"{input.theType} != {found[input.name].theType}" | |
71 assert input.arrayCount == found[input.name].arrayCount, &"{input.arrayCount} != {found[input.name].arrayCount}" | |
326
4ec852355750
fix: many issues, better mesh-handling, still need to cope with different binding numbers when using different pipelines...
Sam <sam@basx.dev>
parents:
323
diff
changeset
|
72 else: |
4ec852355750
fix: many issues, better mesh-handling, still need to cope with different binding numbers when using different pipelines...
Sam <sam@basx.dev>
parents:
323
diff
changeset
|
73 result.add input |
4ec852355750
fix: many issues, better mesh-handling, still need to cope with different binding numbers when using different pipelines...
Sam <sam@basx.dev>
parents:
323
diff
changeset
|
74 found[input.name] = input |
322
6dab370d1758
add: first, incomplete version of material use
Sam <sam@basx.dev>
parents:
321
diff
changeset
|
75 |
326
4ec852355750
fix: many issues, better mesh-handling, still need to cope with different binding numbers when using different pipelines...
Sam <sam@basx.dev>
parents:
323
diff
changeset
|
76 func samplers(renderer: Renderer, scene: Scene): seq[ShaderAttribute] = |
322
6dab370d1758
add: first, incomplete version of material use
Sam <sam@basx.dev>
parents:
321
diff
changeset
|
77 for i in 0 ..< renderer.renderPass.subpasses.len: |
330
04531bec3583
did: remove some stuff from the heap, maybe nicer?
Sam <sam@basx.dev>
parents:
329
diff
changeset
|
78 for materialName, pipeline in renderer.renderPass.subpasses[i].pipelines.pairs: |
04531bec3583
did: remove some stuff from the heap, maybe nicer?
Sam <sam@basx.dev>
parents:
329
diff
changeset
|
79 if scene.usesMaterial(materialName): |
326
4ec852355750
fix: many issues, better mesh-handling, still need to cope with different binding numbers when using different pipelines...
Sam <sam@basx.dev>
parents:
323
diff
changeset
|
80 result.add pipeline.samplers |
322
6dab370d1758
add: first, incomplete version of material use
Sam <sam@basx.dev>
parents:
321
diff
changeset
|
81 |
329
69e18f69713b
add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents:
328
diff
changeset
|
82 func materialCompatibleWithPipeline(scene: Scene, material: Material, pipeline: Pipeline): (bool, string) = |
69e18f69713b
add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents:
328
diff
changeset
|
83 for uniform in pipeline.uniforms: |
69e18f69713b
add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents:
328
diff
changeset
|
84 if scene.shaderGlobals.contains(uniform.name): |
69e18f69713b
add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents:
328
diff
changeset
|
85 if scene.shaderGlobals[uniform.name].theType != uniform.theType: |
330
04531bec3583
did: remove some stuff from the heap, maybe nicer?
Sam <sam@basx.dev>
parents:
329
diff
changeset
|
86 return (true, &"shader uniform needs type {uniform.theType} but scene global is of type {scene.shaderGlobals[uniform.name].theType}") |
329
69e18f69713b
add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents:
328
diff
changeset
|
87 else: |
330
04531bec3583
did: remove some stuff from the heap, maybe nicer?
Sam <sam@basx.dev>
parents:
329
diff
changeset
|
88 var foundMatch = true |
329
69e18f69713b
add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents:
328
diff
changeset
|
89 for name, constant in material.constants.pairs: |
69e18f69713b
add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents:
328
diff
changeset
|
90 if name == uniform.name and constant.theType == uniform.theType: |
69e18f69713b
add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents:
328
diff
changeset
|
91 foundMatch = true |
69e18f69713b
add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents:
328
diff
changeset
|
92 break |
69e18f69713b
add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents:
328
diff
changeset
|
93 if not foundMatch: |
330
04531bec3583
did: remove some stuff from the heap, maybe nicer?
Sam <sam@basx.dev>
parents:
329
diff
changeset
|
94 return (true, &"shader uniform '{uniform.name}' was not found in scene globals or scene materials") |
329
69e18f69713b
add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents:
328
diff
changeset
|
95 for sampler in pipeline.samplers: |
333
27aaf43e18b4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
331
diff
changeset
|
96 if scene.shaderGlobals.contains(sampler.name): |
27aaf43e18b4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
331
diff
changeset
|
97 if scene.shaderGlobals[sampler.name].theType != sampler.theType: |
27aaf43e18b4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
331
diff
changeset
|
98 return (true, &"shader sampler '{sampler.name}' needs type {sampler.theType} but scene global is of type {scene.shaderGlobals[sampler.name].theType}") |
27aaf43e18b4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
331
diff
changeset
|
99 else: |
27aaf43e18b4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
331
diff
changeset
|
100 var foundMatch = true |
27aaf43e18b4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
331
diff
changeset
|
101 for name, value in material.textures: |
27aaf43e18b4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
331
diff
changeset
|
102 if name == sampler.name: |
27aaf43e18b4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
331
diff
changeset
|
103 foundMatch = true |
27aaf43e18b4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
331
diff
changeset
|
104 break |
27aaf43e18b4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
331
diff
changeset
|
105 if not foundMatch: |
27aaf43e18b4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
331
diff
changeset
|
106 return (true, &"Required texture for shader sampler '{sampler.name}' was not found in scene materials") |
329
69e18f69713b
add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents:
328
diff
changeset
|
107 |
330
04531bec3583
did: remove some stuff from the heap, maybe nicer?
Sam <sam@basx.dev>
parents:
329
diff
changeset
|
108 return (false, "") |
329
69e18f69713b
add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents:
328
diff
changeset
|
109 |
69e18f69713b
add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents:
328
diff
changeset
|
110 func meshCompatibleWithPipeline(scene: Scene, mesh: Mesh, pipeline: Pipeline): (bool, string) = |
69e18f69713b
add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents:
328
diff
changeset
|
111 for input in pipeline.inputs: |
330
04531bec3583
did: remove some stuff from the heap, maybe nicer?
Sam <sam@basx.dev>
parents:
329
diff
changeset
|
112 if input.name == TRANSFORMATTRIBUTE: # will be populated automatically |
04531bec3583
did: remove some stuff from the heap, maybe nicer?
Sam <sam@basx.dev>
parents:
329
diff
changeset
|
113 continue |
331
05fb85ba97dd
did: undid using meshes as values, ref is much better, fix a few things, fix a few huge performance issues
Sam <sam@basx.dev>
parents:
330
diff
changeset
|
114 if not (input.name in mesh[].attributes): |
330
04531bec3583
did: remove some stuff from the heap, maybe nicer?
Sam <sam@basx.dev>
parents:
329
diff
changeset
|
115 return (true, &"Shader input '{input.name}' is not available for mesh '{mesh}'") |
331
05fb85ba97dd
did: undid using meshes as values, ref is much better, fix a few things, fix a few huge performance issues
Sam <sam@basx.dev>
parents:
330
diff
changeset
|
116 if input.theType != mesh[].attributeType(input.name): |
05fb85ba97dd
did: undid using meshes as values, ref is much better, fix a few things, fix a few huge performance issues
Sam <sam@basx.dev>
parents:
330
diff
changeset
|
117 return (true, &"Shader input '{input.name}' expects type {input.theType}, but mesh '{mesh}' has {mesh[].attributeType(input.name)}") |
333
27aaf43e18b4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
331
diff
changeset
|
118 if not input.perInstance and not mesh[].vertexAttributes.contains(input.name): |
27aaf43e18b4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
331
diff
changeset
|
119 return (true, &"Shader input '{input.name}' expected to be vertex attribute, but mesh has no such vertex attribute (available are: {mesh[].vertexAttributes})") |
27aaf43e18b4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
331
diff
changeset
|
120 if input.perInstance and not mesh[].instanceAttributes.contains(input.name): |
27aaf43e18b4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
331
diff
changeset
|
121 return (true, &"Shader input '{input.name}' expected to be per instance attribute, but mesh has no such instance attribute (available are: {mesh[].instanceAttributes})") |
329
69e18f69713b
add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents:
328
diff
changeset
|
122 |
69e18f69713b
add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents:
328
diff
changeset
|
123 return materialCompatibleWithPipeline(scene, mesh.material, pipeline) |
69e18f69713b
add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents:
328
diff
changeset
|
124 |
69e18f69713b
add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents:
328
diff
changeset
|
125 func checkSceneIntegrity(renderer: Renderer, scene: Scene) = |
330
04531bec3583
did: remove some stuff from the heap, maybe nicer?
Sam <sam@basx.dev>
parents:
329
diff
changeset
|
126 if scene.meshes.len == 0: |
04531bec3583
did: remove some stuff from the heap, maybe nicer?
Sam <sam@basx.dev>
parents:
329
diff
changeset
|
127 return |
04531bec3583
did: remove some stuff from the heap, maybe nicer?
Sam <sam@basx.dev>
parents:
329
diff
changeset
|
128 |
329
69e18f69713b
add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents:
328
diff
changeset
|
129 var foundRenderableObject = false |
69e18f69713b
add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents:
328
diff
changeset
|
130 var shaderTypes: seq[string] |
69e18f69713b
add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents:
328
diff
changeset
|
131 for i in 0 ..< renderer.renderPass.subpasses.len: |
330
04531bec3583
did: remove some stuff from the heap, maybe nicer?
Sam <sam@basx.dev>
parents:
329
diff
changeset
|
132 for materialName, pipeline in renderer.renderPass.subpasses[i].pipelines.pairs: |
04531bec3583
did: remove some stuff from the heap, maybe nicer?
Sam <sam@basx.dev>
parents:
329
diff
changeset
|
133 shaderTypes.add materialName |
329
69e18f69713b
add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents:
328
diff
changeset
|
134 for mesh in scene.meshes: |
330
04531bec3583
did: remove some stuff from the heap, maybe nicer?
Sam <sam@basx.dev>
parents:
329
diff
changeset
|
135 if mesh.material.name == materialName: |
329
69e18f69713b
add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents:
328
diff
changeset
|
136 foundRenderableObject = true |
69e18f69713b
add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents:
328
diff
changeset
|
137 let (error, message) = scene.meshCompatibleWithPipeline(mesh, pipeline) |
69e18f69713b
add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents:
328
diff
changeset
|
138 if error: |
330
04531bec3583
did: remove some stuff from the heap, maybe nicer?
Sam <sam@basx.dev>
parents:
329
diff
changeset
|
139 raise newException(Exception, &"Mesh '{mesh}' not compatible with assigned pipeline ({materialName}) because: {message}") |
329
69e18f69713b
add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents:
328
diff
changeset
|
140 |
69e18f69713b
add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents:
328
diff
changeset
|
141 if not foundRenderableObject: |
69e18f69713b
add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents:
328
diff
changeset
|
142 var materialTypes: seq[string] |
69e18f69713b
add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents:
328
diff
changeset
|
143 for mesh in scene.meshes: |
330
04531bec3583
did: remove some stuff from the heap, maybe nicer?
Sam <sam@basx.dev>
parents:
329
diff
changeset
|
144 if not materialTypes.contains(mesh.material.name): |
04531bec3583
did: remove some stuff from the heap, maybe nicer?
Sam <sam@basx.dev>
parents:
329
diff
changeset
|
145 materialTypes.add mesh.material.name |
04531bec3583
did: remove some stuff from the heap, maybe nicer?
Sam <sam@basx.dev>
parents:
329
diff
changeset
|
146 raise newException(Exception, &"Scene '{scene.name}' has been added but materials are not compatible with any registered shader: Materials in scene: {materialTypes}, registered shader-materialtypes: {shaderTypes}") |
329
69e18f69713b
add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents:
328
diff
changeset
|
147 |
326
4ec852355750
fix: many issues, better mesh-handling, still need to cope with different binding numbers when using different pipelines...
Sam <sam@basx.dev>
parents:
323
diff
changeset
|
148 proc setupDrawableBuffers*(renderer: var Renderer, scene: var Scene) = |
142
9c0d7839dc91
add: correct mesh buffer data updates to GPU
Sam <sam@basx.dev>
parents:
140
diff
changeset
|
149 assert not (scene in renderer.scenedata) |
329
69e18f69713b
add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents:
328
diff
changeset
|
150 renderer.checkSceneIntegrity(scene) |
69e18f69713b
add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents:
328
diff
changeset
|
151 |
323
9defff46da48
add: first complete working version of multiple materials and shaders per scene, yipie :)
Sam <sam@basx.dev>
parents:
322
diff
changeset
|
152 let |
326
4ec852355750
fix: many issues, better mesh-handling, still need to cope with different binding numbers when using different pipelines...
Sam <sam@basx.dev>
parents:
323
diff
changeset
|
153 inputs = renderer.inputs(scene) |
4ec852355750
fix: many issues, better mesh-handling, still need to cope with different binding numbers when using different pipelines...
Sam <sam@basx.dev>
parents:
323
diff
changeset
|
154 samplers = renderer.samplers(scene) |
315
4921ec86dcb4
did: preparations to refactor material system, still tons to do
Sam <sam@basx.dev>
parents:
308
diff
changeset
|
155 var scenedata = SceneData() |
128
9901ec3831d1
did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents:
127
diff
changeset
|
156 |
326
4ec852355750
fix: many issues, better mesh-handling, still need to cope with different binding numbers when using different pipelines...
Sam <sam@basx.dev>
parents:
323
diff
changeset
|
157 for mesh in scene.meshes: |
329
69e18f69713b
add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents:
328
diff
changeset
|
158 if not scenedata.materials.contains(mesh.material): |
322
6dab370d1758
add: first, incomplete version of material use
Sam <sam@basx.dev>
parents:
321
diff
changeset
|
159 scenedata.materials.add mesh.material |
323
9defff46da48
add: first complete working version of multiple materials and shaders per scene, yipie :)
Sam <sam@basx.dev>
parents:
322
diff
changeset
|
160 for textureName, texture in mesh.material.textures.pairs: |
333
27aaf43e18b4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
331
diff
changeset
|
161 if scene.shaderGlobals.contains(textureName) and scene.shaderGlobals[textureName].theType == Sampler2D: |
27aaf43e18b4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
331
diff
changeset
|
162 warn &"Ignoring material texture '{textureName}' as scene-global textures with the same name have been defined" |
27aaf43e18b4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
331
diff
changeset
|
163 else: |
27aaf43e18b4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
331
diff
changeset
|
164 if not scenedata.textures.hasKey(textureName): |
27aaf43e18b4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
331
diff
changeset
|
165 scenedata.textures[textureName] = @[] |
27aaf43e18b4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
331
diff
changeset
|
166 scenedata.textures[textureName].add renderer.device.uploadTexture(texture) |
27aaf43e18b4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
331
diff
changeset
|
167 |
27aaf43e18b4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
331
diff
changeset
|
168 for name, value in scene.shaderGlobals.pairs: |
27aaf43e18b4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
331
diff
changeset
|
169 if value.theType == Sampler2D: |
27aaf43e18b4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
331
diff
changeset
|
170 assert not scenedata.textures.contains(name) # should be handled by the above code |
27aaf43e18b4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
331
diff
changeset
|
171 scenedata.textures[name] = @[] |
340 | 172 for texture in getValues[Texture](value)[]: |
333
27aaf43e18b4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
331
diff
changeset
|
173 scenedata.textures[name].add renderer.device.uploadTexture(texture) |
27aaf43e18b4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
331
diff
changeset
|
174 |
322
6dab370d1758
add: first, incomplete version of material use
Sam <sam@basx.dev>
parents:
321
diff
changeset
|
175 |
189
df92519d4d68
add: initial code for texture support, not finished, had to completely refactor how to handle material-data (ie scene-wide data, sorry if you ever read this
Sam <sam@basx.dev>
parents:
164
diff
changeset
|
176 # find all meshes, populate missing attribute values for shader |
326
4ec852355750
fix: many issues, better mesh-handling, still need to cope with different binding numbers when using different pipelines...
Sam <sam@basx.dev>
parents:
323
diff
changeset
|
177 for mesh in scene.meshes.mitems: |
128
9901ec3831d1
did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents:
127
diff
changeset
|
178 for inputAttr in inputs: |
326
4ec852355750
fix: many issues, better mesh-handling, still need to cope with different binding numbers when using different pipelines...
Sam <sam@basx.dev>
parents:
323
diff
changeset
|
179 if inputAttr.name == TRANSFORMATTRIBUTE: |
331
05fb85ba97dd
did: undid using meshes as values, ref is much better, fix a few things, fix a few huge performance issues
Sam <sam@basx.dev>
parents:
330
diff
changeset
|
180 mesh[].initInstanceAttribute(inputAttr.name, inputAttr.thetype) |
05fb85ba97dd
did: undid using meshes as values, ref is much better, fix a few things, fix a few huge performance issues
Sam <sam@basx.dev>
parents:
330
diff
changeset
|
181 elif not mesh[].attributes.contains(inputAttr.name): |
323
9defff46da48
add: first complete working version of multiple materials and shaders per scene, yipie :)
Sam <sam@basx.dev>
parents:
322
diff
changeset
|
182 warn(&"Mesh is missing data for shader attribute {inputAttr.name}, auto-filling with empty values") |
326
4ec852355750
fix: many issues, better mesh-handling, still need to cope with different binding numbers when using different pipelines...
Sam <sam@basx.dev>
parents:
323
diff
changeset
|
183 if inputAttr.perInstance: |
331
05fb85ba97dd
did: undid using meshes as values, ref is much better, fix a few things, fix a few huge performance issues
Sam <sam@basx.dev>
parents:
330
diff
changeset
|
184 mesh[].initInstanceAttribute(inputAttr.name, inputAttr.thetype) |
326
4ec852355750
fix: many issues, better mesh-handling, still need to cope with different binding numbers when using different pipelines...
Sam <sam@basx.dev>
parents:
323
diff
changeset
|
185 else: |
331
05fb85ba97dd
did: undid using meshes as values, ref is much better, fix a few things, fix a few huge performance issues
Sam <sam@basx.dev>
parents:
330
diff
changeset
|
186 mesh[].initVertexAttribute(inputAttr.name, inputAttr.thetype) |
05fb85ba97dd
did: undid using meshes as values, ref is much better, fix a few things, fix a few huge performance issues
Sam <sam@basx.dev>
parents:
330
diff
changeset
|
187 assert mesh[].attributeType(inputAttr.name) == inputAttr.thetype, &"mesh attribute {inputAttr.name} has type {mesh[].attributeType(inputAttr.name)} but shader expects {inputAttr.thetype}" |
128
9901ec3831d1
did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents:
127
diff
changeset
|
188 |
189
df92519d4d68
add: initial code for texture support, not finished, had to completely refactor how to handle material-data (ie scene-wide data, sorry if you ever read this
Sam <sam@basx.dev>
parents:
164
diff
changeset
|
189 # create index buffer if necessary |
326
4ec852355750
fix: many issues, better mesh-handling, still need to cope with different binding numbers when using different pipelines...
Sam <sam@basx.dev>
parents:
323
diff
changeset
|
190 var indicesBufferSize = 0 |
4ec852355750
fix: many issues, better mesh-handling, still need to cope with different binding numbers when using different pipelines...
Sam <sam@basx.dev>
parents:
323
diff
changeset
|
191 for mesh in scene.meshes: |
331
05fb85ba97dd
did: undid using meshes as values, ref is much better, fix a few things, fix a few huge performance issues
Sam <sam@basx.dev>
parents:
330
diff
changeset
|
192 if mesh[].indexType != MeshIndexType.None: |
05fb85ba97dd
did: undid using meshes as values, ref is much better, fix a few things, fix a few huge performance issues
Sam <sam@basx.dev>
parents:
330
diff
changeset
|
193 let indexAlignment = case mesh[].indexType |
326
4ec852355750
fix: many issues, better mesh-handling, still need to cope with different binding numbers when using different pipelines...
Sam <sam@basx.dev>
parents:
323
diff
changeset
|
194 of MeshIndexType.None: 0 |
4ec852355750
fix: many issues, better mesh-handling, still need to cope with different binding numbers when using different pipelines...
Sam <sam@basx.dev>
parents:
323
diff
changeset
|
195 of Tiny: 1 |
4ec852355750
fix: many issues, better mesh-handling, still need to cope with different binding numbers when using different pipelines...
Sam <sam@basx.dev>
parents:
323
diff
changeset
|
196 of Small: 2 |
4ec852355750
fix: many issues, better mesh-handling, still need to cope with different binding numbers when using different pipelines...
Sam <sam@basx.dev>
parents:
323
diff
changeset
|
197 of Big: 4 |
128
9901ec3831d1
did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents:
127
diff
changeset
|
198 # index value alignment required by Vulkan |
9901ec3831d1
did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents:
127
diff
changeset
|
199 if indicesBufferSize mod indexAlignment != 0: |
9901ec3831d1
did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents:
127
diff
changeset
|
200 indicesBufferSize += indexAlignment - (indicesBufferSize mod indexAlignment) |
331
05fb85ba97dd
did: undid using meshes as values, ref is much better, fix a few things, fix a few huge performance issues
Sam <sam@basx.dev>
parents:
330
diff
changeset
|
201 indicesBufferSize += mesh[].indexSize |
128
9901ec3831d1
did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents:
127
diff
changeset
|
202 if indicesBufferSize > 0: |
315
4921ec86dcb4
did: preparations to refactor material system, still tons to do
Sam <sam@basx.dev>
parents:
308
diff
changeset
|
203 scenedata.indexBuffer = renderer.device.createBuffer( |
128
9901ec3831d1
did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents:
127
diff
changeset
|
204 size=indicesBufferSize, |
9901ec3831d1
did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents:
127
diff
changeset
|
205 usage=[VK_BUFFER_USAGE_INDEX_BUFFER_BIT], |
156 | 206 requireMappable=false, |
151 | 207 preferVRAM=true, |
128
9901ec3831d1
did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents:
127
diff
changeset
|
208 ) |
9901ec3831d1
did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents:
127
diff
changeset
|
209 |
327
a63bd8f29252
add: make same attribute for different shaders work correctly, yipie!
Sam <sam@basx.dev>
parents:
326
diff
changeset
|
210 # calculcate offsets for attributes in vertex buffers |
189
df92519d4d68
add: initial code for texture support, not finished, had to completely refactor how to handle material-data (ie scene-wide data, sorry if you ever read this
Sam <sam@basx.dev>
parents:
164
diff
changeset
|
211 # trying to use one buffer per memory type |
327
a63bd8f29252
add: make same attribute for different shaders work correctly, yipie!
Sam <sam@basx.dev>
parents:
326
diff
changeset
|
212 var perLocationSizes: Table[MemoryPerformanceHint, int] |
222
ddfc54036e00
add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
211
diff
changeset
|
213 for hint in MemoryPerformanceHint: |
ddfc54036e00
add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
211
diff
changeset
|
214 perLocationSizes[hint] = 0 |
138
62bc83b8a8c7
fix: mixing memory location types is not working
Sam <sam@basx.dev>
parents:
137
diff
changeset
|
215 for attribute in inputs: |
315
4921ec86dcb4
did: preparations to refactor material system, still tons to do
Sam <sam@basx.dev>
parents:
308
diff
changeset
|
216 scenedata.attributeLocation[attribute.name] = attribute.memoryPerformanceHint |
128
9901ec3831d1
did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents:
127
diff
changeset
|
217 # setup one buffer per attribute-location-type |
326
4ec852355750
fix: many issues, better mesh-handling, still need to cope with different binding numbers when using different pipelines...
Sam <sam@basx.dev>
parents:
323
diff
changeset
|
218 for mesh in scene.meshes: |
316
b145a05c2459
add: changing rendering system, not finished yet, also upgrading to Nim 2
Sam <sam@basx.dev>
parents:
315
diff
changeset
|
219 # align size to VERTEX_ATTRIB_ALIGNMENT bytes (the important thing is the correct alignment of the offsets, but |
237 | 220 # we need to expand the buffer size as well, therefore considering alignment already here as well |
221 if perLocationSizes[attribute.memoryPerformanceHint] mod VERTEX_ATTRIB_ALIGNMENT != 0: | |
222 perLocationSizes[attribute.memoryPerformanceHint] += VERTEX_ATTRIB_ALIGNMENT - (perLocationSizes[attribute.memoryPerformanceHint] mod VERTEX_ATTRIB_ALIGNMENT) | |
331
05fb85ba97dd
did: undid using meshes as values, ref is much better, fix a few things, fix a few huge performance issues
Sam <sam@basx.dev>
parents:
330
diff
changeset
|
223 perLocationSizes[attribute.memoryPerformanceHint] += mesh[].attributeSize(attribute.name) |
327
a63bd8f29252
add: make same attribute for different shaders work correctly, yipie!
Sam <sam@basx.dev>
parents:
326
diff
changeset
|
224 |
a63bd8f29252
add: make same attribute for different shaders work correctly, yipie!
Sam <sam@basx.dev>
parents:
326
diff
changeset
|
225 # create vertex buffers |
156 | 226 for memoryPerformanceHint, bufferSize in perLocationSizes.pairs: |
128
9901ec3831d1
did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents:
127
diff
changeset
|
227 if bufferSize > 0: |
315
4921ec86dcb4
did: preparations to refactor material system, still tons to do
Sam <sam@basx.dev>
parents:
308
diff
changeset
|
228 scenedata.vertexBuffers[memoryPerformanceHint] = renderer.device.createBuffer( |
128
9901ec3831d1
did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents:
127
diff
changeset
|
229 size=bufferSize, |
9901ec3831d1
did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents:
127
diff
changeset
|
230 usage=[VK_BUFFER_USAGE_VERTEX_BUFFER_BIT], |
156 | 231 requireMappable=memoryPerformanceHint==PreferFastWrite, |
232 preferVRAM=true, | |
128
9901ec3831d1
did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents:
127
diff
changeset
|
233 ) |
9901ec3831d1
did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents:
127
diff
changeset
|
234 |
330
04531bec3583
did: remove some stuff from the heap, maybe nicer?
Sam <sam@basx.dev>
parents:
329
diff
changeset
|
235 # calculate offset of each attribute for all meshes |
327
a63bd8f29252
add: make same attribute for different shaders work correctly, yipie!
Sam <sam@basx.dev>
parents:
326
diff
changeset
|
236 var perLocationOffsets: Table[MemoryPerformanceHint, int] |
326
4ec852355750
fix: many issues, better mesh-handling, still need to cope with different binding numbers when using different pipelines...
Sam <sam@basx.dev>
parents:
323
diff
changeset
|
237 var indexBufferOffset = 0 |
327
a63bd8f29252
add: make same attribute for different shaders work correctly, yipie!
Sam <sam@basx.dev>
parents:
326
diff
changeset
|
238 for hint in MemoryPerformanceHint: |
a63bd8f29252
add: make same attribute for different shaders work correctly, yipie!
Sam <sam@basx.dev>
parents:
326
diff
changeset
|
239 perLocationOffsets[hint] = 0 |
328 | 240 |
331
05fb85ba97dd
did: undid using meshes as values, ref is much better, fix a few things, fix a few huge performance issues
Sam <sam@basx.dev>
parents:
330
diff
changeset
|
241 for mesh in scene.meshes: |
138
62bc83b8a8c7
fix: mixing memory location types is not working
Sam <sam@basx.dev>
parents:
137
diff
changeset
|
242 for attribute in inputs: |
331
05fb85ba97dd
did: undid using meshes as values, ref is much better, fix a few things, fix a few huge performance issues
Sam <sam@basx.dev>
parents:
330
diff
changeset
|
243 scenedata.vertexBufferOffsets[(mesh, attribute.name)] = perLocationOffsets[attribute.memoryPerformanceHint] |
05fb85ba97dd
did: undid using meshes as values, ref is much better, fix a few things, fix a few huge performance issues
Sam <sam@basx.dev>
parents:
330
diff
changeset
|
244 let size = mesh[].getRawData(attribute.name)[1] |
327
a63bd8f29252
add: make same attribute for different shaders work correctly, yipie!
Sam <sam@basx.dev>
parents:
326
diff
changeset
|
245 perLocationOffsets[attribute.memoryPerformanceHint] += size |
a63bd8f29252
add: make same attribute for different shaders work correctly, yipie!
Sam <sam@basx.dev>
parents:
326
diff
changeset
|
246 if perLocationOffsets[attribute.memoryPerformanceHint] mod VERTEX_ATTRIB_ALIGNMENT != 0: |
a63bd8f29252
add: make same attribute for different shaders work correctly, yipie!
Sam <sam@basx.dev>
parents:
326
diff
changeset
|
247 perLocationOffsets[attribute.memoryPerformanceHint] += VERTEX_ATTRIB_ALIGNMENT - (perLocationOffsets[attribute.memoryPerformanceHint] mod VERTEX_ATTRIB_ALIGNMENT) |
a63bd8f29252
add: make same attribute for different shaders work correctly, yipie!
Sam <sam@basx.dev>
parents:
326
diff
changeset
|
248 |
328 | 249 # fill offsets per pipeline (as sequence corresponds to shader input binding) |
327
a63bd8f29252
add: make same attribute for different shaders work correctly, yipie!
Sam <sam@basx.dev>
parents:
326
diff
changeset
|
250 var offsets: Table[VkPipeline, seq[(string, MemoryPerformanceHint, int)]] |
a63bd8f29252
add: make same attribute for different shaders work correctly, yipie!
Sam <sam@basx.dev>
parents:
326
diff
changeset
|
251 for subpass_i in 0 ..< renderer.renderPass.subpasses.len: |
330
04531bec3583
did: remove some stuff from the heap, maybe nicer?
Sam <sam@basx.dev>
parents:
329
diff
changeset
|
252 for materialName, pipeline in renderer.renderPass.subpasses[subpass_i].pipelines.pairs: |
04531bec3583
did: remove some stuff from the heap, maybe nicer?
Sam <sam@basx.dev>
parents:
329
diff
changeset
|
253 if scene.usesMaterial(materialName): |
329
69e18f69713b
add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents:
328
diff
changeset
|
254 offsets[pipeline.vk] = newSeq[(string, MemoryPerformanceHint, int)]() |
69e18f69713b
add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents:
328
diff
changeset
|
255 for attribute in pipeline.inputs: |
331
05fb85ba97dd
did: undid using meshes as values, ref is much better, fix a few things, fix a few huge performance issues
Sam <sam@basx.dev>
parents:
330
diff
changeset
|
256 offsets[pipeline.vk].add (attribute.name, attribute.memoryPerformanceHint, scenedata.vertexBufferOffsets[(mesh, attribute.name)]) |
128
9901ec3831d1
did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents:
127
diff
changeset
|
257 |
328 | 258 # create drawables |
288
5af702c95b16
add: what seems like a working animation system, atm integrated with entities, will add more for meshes
Sam <sam@basx.dev>
parents:
270
diff
changeset
|
259 let indexed = mesh.indexType != MeshIndexType.None |
128
9901ec3831d1
did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents:
127
diff
changeset
|
260 var drawable = Drawable( |
331
05fb85ba97dd
did: undid using meshes as values, ref is much better, fix a few things, fix a few huge performance issues
Sam <sam@basx.dev>
parents:
330
diff
changeset
|
261 elementCount: if indexed: mesh[].indicesCount else: mesh[].vertexCount, |
128
9901ec3831d1
did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents:
127
diff
changeset
|
262 bufferOffsets: offsets, |
331
05fb85ba97dd
did: undid using meshes as values, ref is much better, fix a few things, fix a few huge performance issues
Sam <sam@basx.dev>
parents:
330
diff
changeset
|
263 instanceCount: mesh[].instanceCount, |
128
9901ec3831d1
did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents:
127
diff
changeset
|
264 indexed: indexed, |
9901ec3831d1
did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents:
127
diff
changeset
|
265 ) |
9901ec3831d1
did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents:
127
diff
changeset
|
266 if indexed: |
9901ec3831d1
did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents:
127
diff
changeset
|
267 let indexAlignment = case mesh.indexType |
326
4ec852355750
fix: many issues, better mesh-handling, still need to cope with different binding numbers when using different pipelines...
Sam <sam@basx.dev>
parents:
323
diff
changeset
|
268 of MeshIndexType.None: 0 |
4ec852355750
fix: many issues, better mesh-handling, still need to cope with different binding numbers when using different pipelines...
Sam <sam@basx.dev>
parents:
323
diff
changeset
|
269 of Tiny: 1 |
4ec852355750
fix: many issues, better mesh-handling, still need to cope with different binding numbers when using different pipelines...
Sam <sam@basx.dev>
parents:
323
diff
changeset
|
270 of Small: 2 |
4ec852355750
fix: many issues, better mesh-handling, still need to cope with different binding numbers when using different pipelines...
Sam <sam@basx.dev>
parents:
323
diff
changeset
|
271 of Big: 4 |
128
9901ec3831d1
did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents:
127
diff
changeset
|
272 # index value alignment required by Vulkan |
9901ec3831d1
did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents:
127
diff
changeset
|
273 if indexBufferOffset mod indexAlignment != 0: |
9901ec3831d1
did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents:
127
diff
changeset
|
274 indexBufferOffset += indexAlignment - (indexBufferOffset mod indexAlignment) |
9901ec3831d1
did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents:
127
diff
changeset
|
275 drawable.indexBufferOffset = indexBufferOffset |
9901ec3831d1
did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents:
127
diff
changeset
|
276 drawable.indexType = mesh.indexType |
331
05fb85ba97dd
did: undid using meshes as values, ref is much better, fix a few things, fix a few huge performance issues
Sam <sam@basx.dev>
parents:
330
diff
changeset
|
277 var (pdata, size) = mesh[].getRawIndexData() |
315
4921ec86dcb4
did: preparations to refactor material system, still tons to do
Sam <sam@basx.dev>
parents:
308
diff
changeset
|
278 scenedata.indexBuffer.setData(pdata, size, indexBufferOffset) |
128
9901ec3831d1
did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents:
127
diff
changeset
|
279 indexBufferOffset += size |
331
05fb85ba97dd
did: undid using meshes as values, ref is much better, fix a few things, fix a few huge performance issues
Sam <sam@basx.dev>
parents:
330
diff
changeset
|
280 scenedata.drawables.add (drawable, mesh) |
142
9c0d7839dc91
add: correct mesh buffer data updates to GPU
Sam <sam@basx.dev>
parents:
140
diff
changeset
|
281 |
263
d1ee2a815fa1
add: some api improvments, preparing for font-loading
Sam <sam@basx.dev>
parents:
251
diff
changeset
|
282 # setup uniforms and samplers |
192
659992f14dd6
add: textures now support in shader via scene data, also: improved config handling a bit, more to come
Sam <sam@basx.dev>
parents:
191
diff
changeset
|
283 for subpass_i in 0 ..< renderer.renderPass.subpasses.len: |
330
04531bec3583
did: remove some stuff from the heap, maybe nicer?
Sam <sam@basx.dev>
parents:
329
diff
changeset
|
284 for materialName, pipeline in renderer.renderPass.subpasses[subpass_i].pipelines.pairs: |
04531bec3583
did: remove some stuff from the heap, maybe nicer?
Sam <sam@basx.dev>
parents:
329
diff
changeset
|
285 if scene.usesMaterial(materialName): |
326
4ec852355750
fix: many issues, better mesh-handling, still need to cope with different binding numbers when using different pipelines...
Sam <sam@basx.dev>
parents:
323
diff
changeset
|
286 var uniformBufferSize = 0 |
4ec852355750
fix: many issues, better mesh-handling, still need to cope with different binding numbers when using different pipelines...
Sam <sam@basx.dev>
parents:
323
diff
changeset
|
287 for uniform in pipeline.uniforms: |
4ec852355750
fix: many issues, better mesh-handling, still need to cope with different binding numbers when using different pipelines...
Sam <sam@basx.dev>
parents:
323
diff
changeset
|
288 uniformBufferSize += uniform.size |
4ec852355750
fix: many issues, better mesh-handling, still need to cope with different binding numbers when using different pipelines...
Sam <sam@basx.dev>
parents:
323
diff
changeset
|
289 if uniformBufferSize > 0: |
4ec852355750
fix: many issues, better mesh-handling, still need to cope with different binding numbers when using different pipelines...
Sam <sam@basx.dev>
parents:
323
diff
changeset
|
290 scenedata.uniformBuffers[pipeline.vk] = newSeq[Buffer]() |
4ec852355750
fix: many issues, better mesh-handling, still need to cope with different binding numbers when using different pipelines...
Sam <sam@basx.dev>
parents:
323
diff
changeset
|
291 for frame_i in 0 ..< renderer.swapchain.inFlightFrames: |
4ec852355750
fix: many issues, better mesh-handling, still need to cope with different binding numbers when using different pipelines...
Sam <sam@basx.dev>
parents:
323
diff
changeset
|
292 scenedata.uniformBuffers[pipeline.vk].add renderer.device.createBuffer( |
4ec852355750
fix: many issues, better mesh-handling, still need to cope with different binding numbers when using different pipelines...
Sam <sam@basx.dev>
parents:
323
diff
changeset
|
293 size=uniformBufferSize, |
4ec852355750
fix: many issues, better mesh-handling, still need to cope with different binding numbers when using different pipelines...
Sam <sam@basx.dev>
parents:
323
diff
changeset
|
294 usage=[VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT], |
4ec852355750
fix: many issues, better mesh-handling, still need to cope with different binding numbers when using different pipelines...
Sam <sam@basx.dev>
parents:
323
diff
changeset
|
295 requireMappable=true, |
4ec852355750
fix: many issues, better mesh-handling, still need to cope with different binding numbers when using different pipelines...
Sam <sam@basx.dev>
parents:
323
diff
changeset
|
296 preferVRAM=true, |
4ec852355750
fix: many issues, better mesh-handling, still need to cope with different binding numbers when using different pipelines...
Sam <sam@basx.dev>
parents:
323
diff
changeset
|
297 ) |
4ec852355750
fix: many issues, better mesh-handling, still need to cope with different binding numbers when using different pipelines...
Sam <sam@basx.dev>
parents:
323
diff
changeset
|
298 |
4ec852355750
fix: many issues, better mesh-handling, still need to cope with different binding numbers when using different pipelines...
Sam <sam@basx.dev>
parents:
323
diff
changeset
|
299 var poolsizes = @[(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, renderer.swapchain.inFlightFrames)] |
4ec852355750
fix: many issues, better mesh-handling, still need to cope with different binding numbers when using different pipelines...
Sam <sam@basx.dev>
parents:
323
diff
changeset
|
300 if samplers.len > 0: |
4ec852355750
fix: many issues, better mesh-handling, still need to cope with different binding numbers when using different pipelines...
Sam <sam@basx.dev>
parents:
323
diff
changeset
|
301 var samplercount = 0 |
4ec852355750
fix: many issues, better mesh-handling, still need to cope with different binding numbers when using different pipelines...
Sam <sam@basx.dev>
parents:
323
diff
changeset
|
302 for sampler in samplers: |
4ec852355750
fix: many issues, better mesh-handling, still need to cope with different binding numbers when using different pipelines...
Sam <sam@basx.dev>
parents:
323
diff
changeset
|
303 samplercount += (if sampler.arrayCount == 0: 1 else: sampler.arrayCount) |
4ec852355750
fix: many issues, better mesh-handling, still need to cope with different binding numbers when using different pipelines...
Sam <sam@basx.dev>
parents:
323
diff
changeset
|
304 poolsizes.add (VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, renderer.swapchain.inFlightFrames * samplercount * 2) |
4ec852355750
fix: many issues, better mesh-handling, still need to cope with different binding numbers when using different pipelines...
Sam <sam@basx.dev>
parents:
323
diff
changeset
|
305 |
4ec852355750
fix: many issues, better mesh-handling, still need to cope with different binding numbers when using different pipelines...
Sam <sam@basx.dev>
parents:
323
diff
changeset
|
306 scenedata.descriptorPools[pipeline.vk] = renderer.device.createDescriptorSetPool(poolsizes) |
4ec852355750
fix: many issues, better mesh-handling, still need to cope with different binding numbers when using different pipelines...
Sam <sam@basx.dev>
parents:
323
diff
changeset
|
307 |
4ec852355750
fix: many issues, better mesh-handling, still need to cope with different binding numbers when using different pipelines...
Sam <sam@basx.dev>
parents:
323
diff
changeset
|
308 scenedata.descriptorSets[pipeline.vk] = pipeline.setupDescriptors( |
4ec852355750
fix: many issues, better mesh-handling, still need to cope with different binding numbers when using different pipelines...
Sam <sam@basx.dev>
parents:
323
diff
changeset
|
309 scenedata.descriptorPools[pipeline.vk], |
4ec852355750
fix: many issues, better mesh-handling, still need to cope with different binding numbers when using different pipelines...
Sam <sam@basx.dev>
parents:
323
diff
changeset
|
310 scenedata.uniformBuffers.getOrDefault(pipeline.vk, @[]), |
4ec852355750
fix: many issues, better mesh-handling, still need to cope with different binding numbers when using different pipelines...
Sam <sam@basx.dev>
parents:
323
diff
changeset
|
311 scenedata.textures, |
4ec852355750
fix: many issues, better mesh-handling, still need to cope with different binding numbers when using different pipelines...
Sam <sam@basx.dev>
parents:
323
diff
changeset
|
312 inFlightFrames=renderer.swapchain.inFlightFrames, |
4ec852355750
fix: many issues, better mesh-handling, still need to cope with different binding numbers when using different pipelines...
Sam <sam@basx.dev>
parents:
323
diff
changeset
|
313 emptyTexture=renderer.emptyTexture, |
4ec852355750
fix: many issues, better mesh-handling, still need to cope with different binding numbers when using different pipelines...
Sam <sam@basx.dev>
parents:
323
diff
changeset
|
314 ) |
192
659992f14dd6
add: textures now support in shader via scene data, also: improved config handling a bit, more to come
Sam <sam@basx.dev>
parents:
191
diff
changeset
|
315 for frame_i in 0 ..< renderer.swapchain.inFlightFrames: |
326
4ec852355750
fix: many issues, better mesh-handling, still need to cope with different binding numbers when using different pipelines...
Sam <sam@basx.dev>
parents:
323
diff
changeset
|
316 scenedata.descriptorSets[pipeline.vk][frame_i].writeDescriptorSet() |
189
df92519d4d68
add: initial code for texture support, not finished, had to completely refactor how to handle material-data (ie scene-wide data, sorry if you ever read this
Sam <sam@basx.dev>
parents:
164
diff
changeset
|
317 |
315
4921ec86dcb4
did: preparations to refactor material system, still tons to do
Sam <sam@basx.dev>
parents:
308
diff
changeset
|
318 renderer.scenedata[scene] = scenedata |
142
9c0d7839dc91
add: correct mesh buffer data updates to GPU
Sam <sam@basx.dev>
parents:
140
diff
changeset
|
319 |
331
05fb85ba97dd
did: undid using meshes as values, ref is much better, fix a few things, fix a few huge performance issues
Sam <sam@basx.dev>
parents:
330
diff
changeset
|
320 proc refreshMeshAttributeData(renderer: Renderer, scene: var Scene, drawable: Drawable, mesh: Mesh, attribute: string) = |
05fb85ba97dd
did: undid using meshes as values, ref is much better, fix a few things, fix a few huge performance issues
Sam <sam@basx.dev>
parents:
330
diff
changeset
|
321 debug &"Refreshing data on mesh mesh for {attribute}" |
222
ddfc54036e00
add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
211
diff
changeset
|
322 # ignore attributes that are not used in this shader |
326
4ec852355750
fix: many issues, better mesh-handling, still need to cope with different binding numbers when using different pipelines...
Sam <sam@basx.dev>
parents:
323
diff
changeset
|
323 if not (attribute in renderer.scenedata[scene].attributeLocation): |
222
ddfc54036e00
add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
211
diff
changeset
|
324 return |
340 | 325 |
331
05fb85ba97dd
did: undid using meshes as values, ref is much better, fix a few things, fix a few huge performance issues
Sam <sam@basx.dev>
parents:
330
diff
changeset
|
326 let (pdata, size) = mesh[].getRawData(attribute) |
326
4ec852355750
fix: many issues, better mesh-handling, still need to cope with different binding numbers when using different pipelines...
Sam <sam@basx.dev>
parents:
323
diff
changeset
|
327 let memoryPerformanceHint = renderer.scenedata[scene].attributeLocation[attribute] |
331
05fb85ba97dd
did: undid using meshes as values, ref is much better, fix a few things, fix a few huge performance issues
Sam <sam@basx.dev>
parents:
330
diff
changeset
|
328 renderer.scenedata[scene].vertexBuffers[memoryPerformanceHint].setData(pdata, size, renderer.scenedata[scene].vertexBufferOffsets[(mesh, attribute)]) |
142
9c0d7839dc91
add: correct mesh buffer data updates to GPU
Sam <sam@basx.dev>
parents:
140
diff
changeset
|
329 |
327
a63bd8f29252
add: make same attribute for different shaders work correctly, yipie!
Sam <sam@basx.dev>
parents:
326
diff
changeset
|
330 proc updateMeshData*(renderer: var Renderer, scene: var Scene, forceAll=false) = |
142
9c0d7839dc91
add: correct mesh buffer data updates to GPU
Sam <sam@basx.dev>
parents:
140
diff
changeset
|
331 assert scene in renderer.scenedata |
128
9901ec3831d1
did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents:
127
diff
changeset
|
332 |
331
05fb85ba97dd
did: undid using meshes as values, ref is much better, fix a few things, fix a few huge performance issues
Sam <sam@basx.dev>
parents:
330
diff
changeset
|
333 for (drawable, mesh) in renderer.scenedata[scene].drawables.mitems: |
05fb85ba97dd
did: undid using meshes as values, ref is much better, fix a few things, fix a few huge performance issues
Sam <sam@basx.dev>
parents:
330
diff
changeset
|
334 if mesh[].attributes.contains(TRANSFORMATTRIBUTE): |
05fb85ba97dd
did: undid using meshes as values, ref is much better, fix a few things, fix a few huge performance issues
Sam <sam@basx.dev>
parents:
330
diff
changeset
|
335 mesh[].updateInstanceTransforms(TRANSFORMATTRIBUTE) |
05fb85ba97dd
did: undid using meshes as values, ref is much better, fix a few things, fix a few huge performance issues
Sam <sam@basx.dev>
parents:
330
diff
changeset
|
336 let attrs = (if forceAll: mesh[].attributes else: mesh[].dirtyAttributes) |
327
a63bd8f29252
add: make same attribute for different shaders work correctly, yipie!
Sam <sam@basx.dev>
parents:
326
diff
changeset
|
337 for attribute in attrs: |
331
05fb85ba97dd
did: undid using meshes as values, ref is much better, fix a few things, fix a few huge performance issues
Sam <sam@basx.dev>
parents:
330
diff
changeset
|
338 renderer.refreshMeshAttributeData(scene, drawable, mesh, attribute) |
326
4ec852355750
fix: many issues, better mesh-handling, still need to cope with different binding numbers when using different pipelines...
Sam <sam@basx.dev>
parents:
323
diff
changeset
|
339 debug &"Update mesh attribute {attribute}" |
331
05fb85ba97dd
did: undid using meshes as values, ref is much better, fix a few things, fix a few huge performance issues
Sam <sam@basx.dev>
parents:
330
diff
changeset
|
340 mesh[].clearDirtyAttributes() |
288
5af702c95b16
add: what seems like a working animation system, atm integrated with entities, will add more for meshes
Sam <sam@basx.dev>
parents:
270
diff
changeset
|
341 |
331
05fb85ba97dd
did: undid using meshes as values, ref is much better, fix a few things, fix a few huge performance issues
Sam <sam@basx.dev>
parents:
330
diff
changeset
|
342 proc updateUniformData*(renderer: var Renderer, scene: var Scene, forceAll=false) = |
189
df92519d4d68
add: initial code for texture support, not finished, had to completely refactor how to handle material-data (ie scene-wide data, sorry if you ever read this
Sam <sam@basx.dev>
parents:
164
diff
changeset
|
343 assert scene in renderer.scenedata |
331
05fb85ba97dd
did: undid using meshes as values, ref is much better, fix a few things, fix a few huge performance issues
Sam <sam@basx.dev>
parents:
330
diff
changeset
|
344 # TODO: maybe check for dirty materials too, but atm we copy materials into the |
05fb85ba97dd
did: undid using meshes as values, ref is much better, fix a few things, fix a few huge performance issues
Sam <sam@basx.dev>
parents:
330
diff
changeset
|
345 # renderers scenedata, so they will are immutable after initialization, would |
05fb85ba97dd
did: undid using meshes as values, ref is much better, fix a few things, fix a few huge performance issues
Sam <sam@basx.dev>
parents:
330
diff
changeset
|
346 # need to allow updates of materials too in order to make sense |
05fb85ba97dd
did: undid using meshes as values, ref is much better, fix a few things, fix a few huge performance issues
Sam <sam@basx.dev>
parents:
330
diff
changeset
|
347 |
05fb85ba97dd
did: undid using meshes as values, ref is much better, fix a few things, fix a few huge performance issues
Sam <sam@basx.dev>
parents:
330
diff
changeset
|
348 let dirty = scene.dirtyShaderGlobals |
05fb85ba97dd
did: undid using meshes as values, ref is much better, fix a few things, fix a few huge performance issues
Sam <sam@basx.dev>
parents:
330
diff
changeset
|
349 if not forceAll and dirty.len == 0: |
05fb85ba97dd
did: undid using meshes as values, ref is much better, fix a few things, fix a few huge performance issues
Sam <sam@basx.dev>
parents:
330
diff
changeset
|
350 return |
05fb85ba97dd
did: undid using meshes as values, ref is much better, fix a few things, fix a few huge performance issues
Sam <sam@basx.dev>
parents:
330
diff
changeset
|
351 |
05fb85ba97dd
did: undid using meshes as values, ref is much better, fix a few things, fix a few huge performance issues
Sam <sam@basx.dev>
parents:
330
diff
changeset
|
352 if forceAll: |
05fb85ba97dd
did: undid using meshes as values, ref is much better, fix a few things, fix a few huge performance issues
Sam <sam@basx.dev>
parents:
330
diff
changeset
|
353 debug "Update uniforms because 'forceAll' was given" |
05fb85ba97dd
did: undid using meshes as values, ref is much better, fix a few things, fix a few huge performance issues
Sam <sam@basx.dev>
parents:
330
diff
changeset
|
354 else: |
05fb85ba97dd
did: undid using meshes as values, ref is much better, fix a few things, fix a few huge performance issues
Sam <sam@basx.dev>
parents:
330
diff
changeset
|
355 debug &"Update uniforms because of dirty scene globals: {dirty}" |
228 | 356 |
330
04531bec3583
did: remove some stuff from the heap, maybe nicer?
Sam <sam@basx.dev>
parents:
329
diff
changeset
|
357 # loop over all used shaders/pipelines |
189
df92519d4d68
add: initial code for texture support, not finished, had to completely refactor how to handle material-data (ie scene-wide data, sorry if you ever read this
Sam <sam@basx.dev>
parents:
164
diff
changeset
|
358 for i in 0 ..< renderer.renderPass.subpasses.len: |
330
04531bec3583
did: remove some stuff from the heap, maybe nicer?
Sam <sam@basx.dev>
parents:
329
diff
changeset
|
359 for materialName, pipeline in renderer.renderPass.subpasses[i].pipelines.pairs: |
331
05fb85ba97dd
did: undid using meshes as values, ref is much better, fix a few things, fix a few huge performance issues
Sam <sam@basx.dev>
parents:
330
diff
changeset
|
360 if ( |
05fb85ba97dd
did: undid using meshes as values, ref is much better, fix a few things, fix a few huge performance issues
Sam <sam@basx.dev>
parents:
330
diff
changeset
|
361 scene.usesMaterial(materialName) and |
05fb85ba97dd
did: undid using meshes as values, ref is much better, fix a few things, fix a few huge performance issues
Sam <sam@basx.dev>
parents:
330
diff
changeset
|
362 renderer.scenedata[scene].uniformBuffers.hasKey(pipeline.vk) and |
05fb85ba97dd
did: undid using meshes as values, ref is much better, fix a few things, fix a few huge performance issues
Sam <sam@basx.dev>
parents:
330
diff
changeset
|
363 renderer.scenedata[scene].uniformBuffers[pipeline.vk].len != 0 |
05fb85ba97dd
did: undid using meshes as values, ref is much better, fix a few things, fix a few huge performance issues
Sam <sam@basx.dev>
parents:
330
diff
changeset
|
364 ): |
266
fd1a95f433d1
add: better material loading system, still far from great
Sam <sam@basx.dev>
parents:
263
diff
changeset
|
365 assert renderer.scenedata[scene].uniformBuffers[pipeline.vk][renderer.swapchain.currentInFlight].vk.valid |
331
05fb85ba97dd
did: undid using meshes as values, ref is much better, fix a few things, fix a few huge performance issues
Sam <sam@basx.dev>
parents:
330
diff
changeset
|
366 if forceAll: |
05fb85ba97dd
did: undid using meshes as values, ref is much better, fix a few things, fix a few huge performance issues
Sam <sam@basx.dev>
parents:
330
diff
changeset
|
367 for buffer in renderer.scenedata[scene].uniformBuffers[pipeline.vk]: |
05fb85ba97dd
did: undid using meshes as values, ref is much better, fix a few things, fix a few huge performance issues
Sam <sam@basx.dev>
parents:
330
diff
changeset
|
368 assert buffer.vk.valid |
05fb85ba97dd
did: undid using meshes as values, ref is much better, fix a few things, fix a few huge performance issues
Sam <sam@basx.dev>
parents:
330
diff
changeset
|
369 |
326
4ec852355750
fix: many issues, better mesh-handling, still need to cope with different binding numbers when using different pipelines...
Sam <sam@basx.dev>
parents:
323
diff
changeset
|
370 var offset = 0 |
331
05fb85ba97dd
did: undid using meshes as values, ref is much better, fix a few things, fix a few huge performance issues
Sam <sam@basx.dev>
parents:
330
diff
changeset
|
371 # loop over all uniforms of the shader-pipeline |
266
fd1a95f433d1
add: better material loading system, still far from great
Sam <sam@basx.dev>
parents:
263
diff
changeset
|
372 for uniform in pipeline.uniforms: |
330
04531bec3583
did: remove some stuff from the heap, maybe nicer?
Sam <sam@basx.dev>
parents:
329
diff
changeset
|
373 var foundValue = false |
04531bec3583
did: remove some stuff from the heap, maybe nicer?
Sam <sam@basx.dev>
parents:
329
diff
changeset
|
374 var value: DataList |
329
69e18f69713b
add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents:
328
diff
changeset
|
375 if scene.shaderGlobals.hasKey(uniform.name): |
330
04531bec3583
did: remove some stuff from the heap, maybe nicer?
Sam <sam@basx.dev>
parents:
329
diff
changeset
|
376 assert scene.shaderGlobals[uniform.name].thetype == uniform.thetype |
329
69e18f69713b
add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents:
328
diff
changeset
|
377 value = scene.shaderGlobals[uniform.name] |
330
04531bec3583
did: remove some stuff from the heap, maybe nicer?
Sam <sam@basx.dev>
parents:
329
diff
changeset
|
378 foundValue = true |
329
69e18f69713b
add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents:
328
diff
changeset
|
379 else: |
69e18f69713b
add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents:
328
diff
changeset
|
380 for mat in renderer.scenedata[scene].materials: |
330
04531bec3583
did: remove some stuff from the heap, maybe nicer?
Sam <sam@basx.dev>
parents:
329
diff
changeset
|
381 for name, materialConstant in mat.constants.pairs: |
04531bec3583
did: remove some stuff from the heap, maybe nicer?
Sam <sam@basx.dev>
parents:
329
diff
changeset
|
382 if uniform.name == name: |
04531bec3583
did: remove some stuff from the heap, maybe nicer?
Sam <sam@basx.dev>
parents:
329
diff
changeset
|
383 value = materialConstant |
04531bec3583
did: remove some stuff from the heap, maybe nicer?
Sam <sam@basx.dev>
parents:
329
diff
changeset
|
384 foundValue = true |
04531bec3583
did: remove some stuff from the heap, maybe nicer?
Sam <sam@basx.dev>
parents:
329
diff
changeset
|
385 break |
04531bec3583
did: remove some stuff from the heap, maybe nicer?
Sam <sam@basx.dev>
parents:
329
diff
changeset
|
386 if foundValue: break |
04531bec3583
did: remove some stuff from the heap, maybe nicer?
Sam <sam@basx.dev>
parents:
329
diff
changeset
|
387 if not foundValue: |
329
69e18f69713b
add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents:
328
diff
changeset
|
388 raise newException(Exception, &"Uniform '{uniform.name}' not found in scene shaderGlobals or materials") |
331
05fb85ba97dd
did: undid using meshes as values, ref is much better, fix a few things, fix a few huge performance issues
Sam <sam@basx.dev>
parents:
330
diff
changeset
|
389 debug &" update uniform {uniform.name} with value: {value}" |
329
69e18f69713b
add: scene/shader compatability check, fix collision code to work with new APIs
Sam <sam@basx.dev>
parents:
328
diff
changeset
|
390 let (pdata, size) = value.getRawData() |
331
05fb85ba97dd
did: undid using meshes as values, ref is much better, fix a few things, fix a few huge performance issues
Sam <sam@basx.dev>
parents:
330
diff
changeset
|
391 if dirty.contains(uniform.name) or forceAll: # only update if necessary |
334
2533f524bdb6
fix: remaining tests and an issue with updating uniforms
Sam <sam@basx.dev>
parents:
333
diff
changeset
|
392 # TODO: technically we would only need to update the uniform buffer of the current |
2533f524bdb6
fix: remaining tests and an issue with updating uniforms
Sam <sam@basx.dev>
parents:
333
diff
changeset
|
393 # frameInFlight, but we don't track for which frame the shaderglobals are no longer dirty |
2533f524bdb6
fix: remaining tests and an issue with updating uniforms
Sam <sam@basx.dev>
parents:
333
diff
changeset
|
394 # therefore we have to update the uniform values in all buffers, of all inFlightframes (usually 2) |
2533f524bdb6
fix: remaining tests and an issue with updating uniforms
Sam <sam@basx.dev>
parents:
333
diff
changeset
|
395 for buffer in renderer.scenedata[scene].uniformBuffers[pipeline.vk]: |
2533f524bdb6
fix: remaining tests and an issue with updating uniforms
Sam <sam@basx.dev>
parents:
333
diff
changeset
|
396 buffer.setData(pdata, size, offset) |
266
fd1a95f433d1
add: better material loading system, still far from great
Sam <sam@basx.dev>
parents:
263
diff
changeset
|
397 offset += size |
331
05fb85ba97dd
did: undid using meshes as values, ref is much better, fix a few things, fix a few huge performance issues
Sam <sam@basx.dev>
parents:
330
diff
changeset
|
398 scene.clearDirtyShaderGlobals() |
189
df92519d4d68
add: initial code for texture support, not finished, had to completely refactor how to handle material-data (ie scene-wide data, sorry if you ever read this
Sam <sam@basx.dev>
parents:
164
diff
changeset
|
399 |
326
4ec852355750
fix: many issues, better mesh-handling, still need to cope with different binding numbers when using different pipelines...
Sam <sam@basx.dev>
parents:
323
diff
changeset
|
400 proc render*(renderer: var Renderer, scene: Scene) = |
142
9c0d7839dc91
add: correct mesh buffer data updates to GPU
Sam <sam@basx.dev>
parents:
140
diff
changeset
|
401 assert scene in renderer.scenedata |
9c0d7839dc91
add: correct mesh buffer data updates to GPU
Sam <sam@basx.dev>
parents:
140
diff
changeset
|
402 |
131
11666d28e04d
add: recreation of swapchain (at least on linux, windows will likely fail, needs testing
Sam <sam@basx.dev>
parents:
129
diff
changeset
|
403 var |
11666d28e04d
add: recreation of swapchain (at least on linux, windows will likely fail, needs testing
Sam <sam@basx.dev>
parents:
129
diff
changeset
|
404 commandBufferResult = renderer.swapchain.nextFrame() |
11666d28e04d
add: recreation of swapchain (at least on linux, windows will likely fail, needs testing
Sam <sam@basx.dev>
parents:
129
diff
changeset
|
405 commandBuffer: VkCommandBuffer |
128
9901ec3831d1
did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents:
127
diff
changeset
|
406 |
131
11666d28e04d
add: recreation of swapchain (at least on linux, windows will likely fail, needs testing
Sam <sam@basx.dev>
parents:
129
diff
changeset
|
407 if not commandBufferResult.isSome: |
11666d28e04d
add: recreation of swapchain (at least on linux, windows will likely fail, needs testing
Sam <sam@basx.dev>
parents:
129
diff
changeset
|
408 let res = renderer.swapchain.recreate() |
11666d28e04d
add: recreation of swapchain (at least on linux, windows will likely fail, needs testing
Sam <sam@basx.dev>
parents:
129
diff
changeset
|
409 if res.isSome: |
205
24d18cd8be8a
did: try to simplfy swapchain recreation, not sure if it is a good idea
Sam <sam@basx.dev>
parents:
201
diff
changeset
|
410 var oldSwapchain = renderer.swapchain |
131
11666d28e04d
add: recreation of swapchain (at least on linux, windows will likely fail, needs testing
Sam <sam@basx.dev>
parents:
129
diff
changeset
|
411 renderer.swapchain = res.get() |
205
24d18cd8be8a
did: try to simplfy swapchain recreation, not sure if it is a good idea
Sam <sam@basx.dev>
parents:
201
diff
changeset
|
412 checkVkResult renderer.device.vk.vkDeviceWaitIdle() |
24d18cd8be8a
did: try to simplfy swapchain recreation, not sure if it is a good idea
Sam <sam@basx.dev>
parents:
201
diff
changeset
|
413 oldSwapchain.destroy() |
24d18cd8be8a
did: try to simplfy swapchain recreation, not sure if it is a good idea
Sam <sam@basx.dev>
parents:
201
diff
changeset
|
414 return |
228 | 415 |
131
11666d28e04d
add: recreation of swapchain (at least on linux, windows will likely fail, needs testing
Sam <sam@basx.dev>
parents:
129
diff
changeset
|
416 commandBuffer = commandBufferResult.get() |
11666d28e04d
add: recreation of swapchain (at least on linux, windows will likely fail, needs testing
Sam <sam@basx.dev>
parents:
129
diff
changeset
|
417 commandBuffer.beginRenderCommands(renderer.renderPass, renderer.swapchain.currentFramebuffer()) |
11666d28e04d
add: recreation of swapchain (at least on linux, windows will likely fail, needs testing
Sam <sam@basx.dev>
parents:
129
diff
changeset
|
418 |
322
6dab370d1758
add: first, incomplete version of material use
Sam <sam@basx.dev>
parents:
321
diff
changeset
|
419 debug "Scene buffers:" |
6dab370d1758
add: first, incomplete version of material use
Sam <sam@basx.dev>
parents:
321
diff
changeset
|
420 for (location, buffer) in renderer.scenedata[scene].vertexBuffers.pairs: |
6dab370d1758
add: first, incomplete version of material use
Sam <sam@basx.dev>
parents:
321
diff
changeset
|
421 debug " ", location, ": ", buffer |
6dab370d1758
add: first, incomplete version of material use
Sam <sam@basx.dev>
parents:
321
diff
changeset
|
422 debug " Index buffer: ", renderer.scenedata[scene].indexBuffer |
6dab370d1758
add: first, incomplete version of material use
Sam <sam@basx.dev>
parents:
321
diff
changeset
|
423 |
131
11666d28e04d
add: recreation of swapchain (at least on linux, windows will likely fail, needs testing
Sam <sam@basx.dev>
parents:
129
diff
changeset
|
424 for i in 0 ..< renderer.renderPass.subpasses.len: |
330
04531bec3583
did: remove some stuff from the heap, maybe nicer?
Sam <sam@basx.dev>
parents:
329
diff
changeset
|
425 for materialName, pipeline in renderer.renderPass.subpasses[i].pipelines.pairs: |
04531bec3583
did: remove some stuff from the heap, maybe nicer?
Sam <sam@basx.dev>
parents:
329
diff
changeset
|
426 if scene.usesMaterial(materialName): |
04531bec3583
did: remove some stuff from the heap, maybe nicer?
Sam <sam@basx.dev>
parents:
329
diff
changeset
|
427 debug &"Start pipeline for {materialName}" |
323
9defff46da48
add: first complete working version of multiple materials and shaders per scene, yipie :)
Sam <sam@basx.dev>
parents:
322
diff
changeset
|
428 commandBuffer.vkCmdBindPipeline(renderer.renderPass.subpasses[i].pipelineBindPoint, pipeline.vk) |
9defff46da48
add: first complete working version of multiple materials and shaders per scene, yipie :)
Sam <sam@basx.dev>
parents:
322
diff
changeset
|
429 commandBuffer.vkCmdBindDescriptorSets(renderer.renderPass.subpasses[i].pipelineBindPoint, pipeline.layout, 0, 1, addr(renderer.scenedata[scene].descriptorSets[pipeline.vk][renderer.swapchain.currentInFlight].vk), 0, nil) |
128
9901ec3831d1
did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents:
127
diff
changeset
|
430 |
331
05fb85ba97dd
did: undid using meshes as values, ref is much better, fix a few things, fix a few huge performance issues
Sam <sam@basx.dev>
parents:
330
diff
changeset
|
431 for (drawable, mesh) in renderer.scenedata[scene].drawables: |
05fb85ba97dd
did: undid using meshes as values, ref is much better, fix a few things, fix a few huge performance issues
Sam <sam@basx.dev>
parents:
330
diff
changeset
|
432 if mesh.material.name == materialName: |
327
a63bd8f29252
add: make same attribute for different shaders work correctly, yipie!
Sam <sam@basx.dev>
parents:
326
diff
changeset
|
433 drawable.draw(commandBuffer, vertexBuffers=renderer.scenedata[scene].vertexBuffers, indexBuffer=renderer.scenedata[scene].indexBuffer, pipeline.vk) |
128
9901ec3831d1
did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents:
127
diff
changeset
|
434 |
131
11666d28e04d
add: recreation of swapchain (at least on linux, windows will likely fail, needs testing
Sam <sam@basx.dev>
parents:
129
diff
changeset
|
435 if i < renderer.renderPass.subpasses.len - 1: |
128
9901ec3831d1
did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents:
127
diff
changeset
|
436 commandBuffer.vkCmdNextSubpass(VK_SUBPASS_CONTENTS_INLINE) |
9901ec3831d1
did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents:
127
diff
changeset
|
437 |
9901ec3831d1
did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents:
127
diff
changeset
|
438 commandBuffer.endRenderCommands() |
9901ec3831d1
did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents:
127
diff
changeset
|
439 |
131
11666d28e04d
add: recreation of swapchain (at least on linux, windows will likely fail, needs testing
Sam <sam@basx.dev>
parents:
129
diff
changeset
|
440 if not renderer.swapchain.swap(): |
11666d28e04d
add: recreation of swapchain (at least on linux, windows will likely fail, needs testing
Sam <sam@basx.dev>
parents:
129
diff
changeset
|
441 let res = renderer.swapchain.recreate() |
11666d28e04d
add: recreation of swapchain (at least on linux, windows will likely fail, needs testing
Sam <sam@basx.dev>
parents:
129
diff
changeset
|
442 if res.isSome: |
205
24d18cd8be8a
did: try to simplfy swapchain recreation, not sure if it is a good idea
Sam <sam@basx.dev>
parents:
201
diff
changeset
|
443 var oldSwapchain = renderer.swapchain |
131
11666d28e04d
add: recreation of swapchain (at least on linux, windows will likely fail, needs testing
Sam <sam@basx.dev>
parents:
129
diff
changeset
|
444 renderer.swapchain = res.get() |
205
24d18cd8be8a
did: try to simplfy swapchain recreation, not sure if it is a good idea
Sam <sam@basx.dev>
parents:
201
diff
changeset
|
445 checkVkResult renderer.device.vk.vkDeviceWaitIdle() |
24d18cd8be8a
did: try to simplfy swapchain recreation, not sure if it is a good idea
Sam <sam@basx.dev>
parents:
201
diff
changeset
|
446 oldSwapchain.destroy() |
132
25fe4972ef9c
add: support for smooth swapchain-recreation
Sam <sam@basx.dev>
parents:
131
diff
changeset
|
447 |
128
9901ec3831d1
did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents:
127
diff
changeset
|
448 func framesRendered*(renderer: Renderer): uint64 = |
9901ec3831d1
did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents:
127
diff
changeset
|
449 renderer.swapchain.framesRendered |
9901ec3831d1
did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents:
127
diff
changeset
|
450 |
140 | 451 func valid*(renderer: Renderer): bool = |
452 renderer.device.vk.valid | |
453 | |
128
9901ec3831d1
did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents:
127
diff
changeset
|
454 proc destroy*(renderer: var Renderer) = |
315
4921ec86dcb4
did: preparations to refactor material system, still tons to do
Sam <sam@basx.dev>
parents:
308
diff
changeset
|
455 for scenedata in renderer.scenedata.mvalues: |
4921ec86dcb4
did: preparations to refactor material system, still tons to do
Sam <sam@basx.dev>
parents:
308
diff
changeset
|
456 for buffer in scenedata.vertexBuffers.mvalues: |
189
df92519d4d68
add: initial code for texture support, not finished, had to completely refactor how to handle material-data (ie scene-wide data, sorry if you ever read this
Sam <sam@basx.dev>
parents:
164
diff
changeset
|
457 assert buffer.vk.valid |
128
9901ec3831d1
did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents:
127
diff
changeset
|
458 buffer.destroy() |
315
4921ec86dcb4
did: preparations to refactor material system, still tons to do
Sam <sam@basx.dev>
parents:
308
diff
changeset
|
459 if scenedata.indexBuffer.vk.valid: |
4921ec86dcb4
did: preparations to refactor material system, still tons to do
Sam <sam@basx.dev>
parents:
308
diff
changeset
|
460 assert scenedata.indexBuffer.vk.valid |
4921ec86dcb4
did: preparations to refactor material system, still tons to do
Sam <sam@basx.dev>
parents:
308
diff
changeset
|
461 scenedata.indexBuffer.destroy() |
4921ec86dcb4
did: preparations to refactor material system, still tons to do
Sam <sam@basx.dev>
parents:
308
diff
changeset
|
462 for pipelineUniforms in scenedata.uniformBuffers.mvalues: |
266
fd1a95f433d1
add: better material loading system, still far from great
Sam <sam@basx.dev>
parents:
263
diff
changeset
|
463 for buffer in pipelineUniforms.mitems: |
fd1a95f433d1
add: better material loading system, still far from great
Sam <sam@basx.dev>
parents:
263
diff
changeset
|
464 assert buffer.vk.valid |
fd1a95f433d1
add: better material loading system, still far from great
Sam <sam@basx.dev>
parents:
263
diff
changeset
|
465 buffer.destroy() |
315
4921ec86dcb4
did: preparations to refactor material system, still tons to do
Sam <sam@basx.dev>
parents:
308
diff
changeset
|
466 for textures in scenedata.textures.mvalues: |
201 | 467 for texture in textures.mitems: |
468 texture.destroy() | |
323
9defff46da48
add: first complete working version of multiple materials and shaders per scene, yipie :)
Sam <sam@basx.dev>
parents:
322
diff
changeset
|
469 for descriptorPool in scenedata.descriptorPools.mvalues: |
9defff46da48
add: first complete working version of multiple materials and shaders per scene, yipie :)
Sam <sam@basx.dev>
parents:
322
diff
changeset
|
470 descriptorPool.destroy() |
9defff46da48
add: first complete working version of multiple materials and shaders per scene, yipie :)
Sam <sam@basx.dev>
parents:
322
diff
changeset
|
471 renderer.emptyTexture.destroy() |
131
11666d28e04d
add: recreation of swapchain (at least on linux, windows will likely fail, needs testing
Sam <sam@basx.dev>
parents:
129
diff
changeset
|
472 renderer.renderPass.destroy() |
128
9901ec3831d1
did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents:
127
diff
changeset
|
473 renderer.swapchain.destroy() |