annotate src/semicongine/renderer.nim @ 326:4ec852355750

fix: many issues, better mesh-handling, still need to cope with different binding numbers when using different pipelines...
author Sam <sam@basx.dev>
date Fri, 25 Aug 2023 00:29:51 +0700
parents 9defff46da48
children a63bd8f29252
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
127
5871acc2977e did: big refactoring
Sam <sam@basx.dev>
parents:
diff changeset
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
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
6 import std/enumerate
127
5871acc2977e did: big refactoring
Sam <sam@basx.dev>
parents:
diff changeset
7
206
7f921d7d0a2b did: small refactoring of module structure
Sam <sam@basx.dev>
parents: 205
diff changeset
8 import ./core
128
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
9 import ./vulkan/buffer
127
5871acc2977e did: big refactoring
Sam <sam@basx.dev>
parents:
diff changeset
10 import ./vulkan/device
128
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
11 import ./vulkan/drawable
322
6dab370d1758 add: first, incomplete version of material use
Sam <sam@basx.dev>
parents: 321
diff changeset
12 import ./vulkan/physicaldevice
128
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
13 import ./vulkan/pipeline
127
5871acc2977e did: big refactoring
Sam <sam@basx.dev>
parents:
diff changeset
14 import ./vulkan/renderpass
5871acc2977e did: big refactoring
Sam <sam@basx.dev>
parents:
diff changeset
15 import ./vulkan/swapchain
322
6dab370d1758 add: first, incomplete version of material use
Sam <sam@basx.dev>
parents: 321
diff changeset
16 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
17 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
18 import ./vulkan/image
127
5871acc2977e did: big refactoring
Sam <sam@basx.dev>
parents:
diff changeset
19
247
beb41c93aa3f fix: gltf loading
Sam <sam@basx.dev>
parents: 243
diff changeset
20 import ./scene
128
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
21 import ./mesh
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
22
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
23 const MATERIALINDEXATTRIBUTE = "materialIndex"
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 const TRANSFORMATTRIBUTE = "transform"
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
25
127
5871acc2977e did: big refactoring
Sam <sam@basx.dev>
parents:
diff changeset
26 type
128
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
27 SceneData = object
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
28 drawables*: seq[tuple[drawable: Drawable, meshIndex: int]]
156
134647ed5b60 did: refactor memory selection
Sam <sam@basx.dev>
parents: 151
diff changeset
29 vertexBuffers*: Table[MemoryPerformanceHint, Buffer]
128
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
30 indexBuffer*: Buffer
266
fd1a95f433d1 add: better material loading system, still far from great
Sam <sam@basx.dev>
parents: 263
diff changeset
31 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
32 textures*: Table[string, seq[VulkanTexture]] # per frame-in-flight
156
134647ed5b60 did: refactor memory selection
Sam <sam@basx.dev>
parents: 151
diff changeset
33 attributeLocation*: Table[string, MemoryPerformanceHint]
142
9c0d7839dc91 add: correct mesh buffer data updates to GPU
Sam <sam@basx.dev>
parents: 140
diff changeset
34 attributeBindingNumber*: Table[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
35 descriptorPools*: Table[VkPipeline, DescriptorPool]
241
671b9267a533 fix: separate descriptors per scene
Sam <sam@basx.dev>
parents: 238
diff changeset
36 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
37 materials: seq[Material]
128
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
38 Renderer* = object
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
39 device: Device
127
5871acc2977e did: big refactoring
Sam <sam@basx.dev>
parents:
diff changeset
40 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
41 renderPass: RenderPass
127
5871acc2977e did: big refactoring
Sam <sam@basx.dev>
parents:
diff changeset
42 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
43 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
44 emptyTexture: VulkanTexture
127
5871acc2977e did: big refactoring
Sam <sam@basx.dev>
parents:
diff changeset
45
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
46 func usesMaterialType(scene: Scene, materialType: string): bool =
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
47 return scene.meshes.anyIt(it.material.materialType == materialType)
127
5871acc2977e did: big refactoring
Sam <sam@basx.dev>
parents:
diff changeset
48
322
6dab370d1758 add: first, incomplete version of material use
Sam <sam@basx.dev>
parents: 321
diff changeset
49 proc initRenderer*(device: Device, shaders: Table[string, ShaderConfiguration], clearColor=Vec4f([0.8'f32, 0.8'f32, 0.8'f32, 1'f32])): Renderer =
127
5871acc2977e did: big refactoring
Sam <sam@basx.dev>
parents:
diff changeset
50 assert device.vk.valid
322
6dab370d1758 add: first, incomplete version of material use
Sam <sam@basx.dev>
parents: 321
diff changeset
51
128
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
52 result.device = device
322
6dab370d1758 add: first, incomplete version of material use
Sam <sam@basx.dev>
parents: 321
diff changeset
53 result.renderPass = device.simpleForwardRenderPass(shaders, clearColor=clearColor)
127
5871acc2977e did: big refactoring
Sam <sam@basx.dev>
parents:
diff changeset
54 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
55 # 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
56 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
57 if not swapchain.isSome:
127
5871acc2977e did: big refactoring
Sam <sam@basx.dev>
parents:
diff changeset
58 raise newException(Exception, "Unable to create swapchain")
322
6dab370d1758 add: first, incomplete version of material use
Sam <sam@basx.dev>
parents: 321
diff changeset
59
131
11666d28e04d add: recreation of swapchain (at least on linux, windows will likely fail, needs testing
Sam <sam@basx.dev>
parents: 129
diff changeset
60 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
61 result.emptyTexture = device.uploadTexture(EMPTYTEXTURE)
128
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
62
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
63 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
64 var found: Table[string, ShaderAttribute]
322
6dab370d1758 add: first, incomplete version of material use
Sam <sam@basx.dev>
parents: 321
diff changeset
65 for i in 0 ..< renderer.renderPass.subpasses.len:
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
66 for materialType, pipeline in renderer.renderPass.subpasses[i].pipelines.pairs:
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 if scene.usesMaterialType(materialType):
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 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
69 if found.contains(input.name):
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
70 assert input == found[input.name]
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
71 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
72 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
73 found[input.name] = input
322
6dab370d1758 add: first, incomplete version of material use
Sam <sam@basx.dev>
parents: 321
diff changeset
74
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
75 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
76 for i in 0 ..< renderer.renderPass.subpasses.len:
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
77 for materialType, pipeline in renderer.renderPass.subpasses[i].pipelines.pairs:
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
78 if scene.usesMaterialType(materialType):
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
79 result.add pipeline.samplers
322
6dab370d1758 add: first, incomplete version of material use
Sam <sam@basx.dev>
parents: 321
diff changeset
80
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
81 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
82 assert not (scene in renderer.scenedata)
237
2fca78b0c0d6 fix: incorrect vertex data alignment
Sam <sam@basx.dev>
parents: 228
diff changeset
83 const VERTEX_ATTRIB_ALIGNMENT = 4 # used for buffer alignment
322
6dab370d1758 add: first, incomplete version of material use
Sam <sam@basx.dev>
parents: 321
diff changeset
84
323
9defff46da48 add: first complete working version of multiple materials and shaders per scene, yipie :)
Sam <sam@basx.dev>
parents: 322
diff changeset
85 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
86 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
87 samplers = renderer.samplers(scene)
315
4921ec86dcb4 did: preparations to refactor material system, still tons to do
Sam <sam@basx.dev>
parents: 308
diff changeset
88 var scenedata = SceneData()
128
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
89
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
90 for mesh in scene.meshes:
322
6dab370d1758 add: first, incomplete version of material use
Sam <sam@basx.dev>
parents: 321
diff changeset
91 if mesh.material != nil and not scenedata.materials.contains(mesh.material):
6dab370d1758 add: first, incomplete version of material use
Sam <sam@basx.dev>
parents: 321
diff changeset
92 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
93 for textureName, texture in mesh.material.textures.pairs:
9defff46da48 add: first complete working version of multiple materials and shaders per scene, yipie :)
Sam <sam@basx.dev>
parents: 322
diff changeset
94 if not scenedata.textures.hasKey(textureName):
9defff46da48 add: first complete working version of multiple materials and shaders per scene, yipie :)
Sam <sam@basx.dev>
parents: 322
diff changeset
95 scenedata.textures[textureName] = @[]
9defff46da48 add: first complete working version of multiple materials and shaders per scene, yipie :)
Sam <sam@basx.dev>
parents: 322
diff changeset
96 scenedata.textures[textureName].add renderer.device.uploadTexture(texture)
322
6dab370d1758 add: first, incomplete version of material use
Sam <sam@basx.dev>
parents: 321
diff changeset
97
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
98 # 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
99 for mesh in scene.meshes.mitems:
128
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
100 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
101 if inputAttr.name == TRANSFORMATTRIBUTE:
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
102 mesh.initInstanceAttribute(inputAttr.name, inputAttr.thetype)
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
103 elif inputAttr.name == MATERIALINDEXATTRIBUTE:
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
104 assert mesh.material != nil, "Missing material specification for mesh. Set material attribute on mesh"
322
6dab370d1758 add: first, incomplete version of material use
Sam <sam@basx.dev>
parents: 321
diff changeset
105 let matIndex = scenedata.materials.find(mesh.material)
6dab370d1758 add: first, incomplete version of material use
Sam <sam@basx.dev>
parents: 321
diff changeset
106 if matIndex < 0:
6dab370d1758 add: first, incomplete version of material use
Sam <sam@basx.dev>
parents: 321
diff changeset
107 raise newException(Exception, &"Required material '{mesh.material}' not available in scene (available are: {scenedata.materials})")
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
108 mesh.initInstanceAttribute(inputAttr.name, uint16(matIndex))
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
109 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
110 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
111 if inputAttr.perInstance:
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
112 mesh.initInstanceAttribute(inputAttr.name, inputAttr.thetype)
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
113 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
114 mesh.initVertexAttribute(inputAttr.name, inputAttr.thetype)
323
9defff46da48 add: first complete working version of multiple materials and shaders per scene, yipie :)
Sam <sam@basx.dev>
parents: 322
diff changeset
115 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
116
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
117 # 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
118 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
119 for mesh in scene.meshes:
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
120 if mesh.indexType != MeshIndexType.None:
128
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
121 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
122 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
123 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
124 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
125 of Big: 4
128
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
126 # index value alignment required by Vulkan
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
127 if indicesBufferSize mod indexAlignment != 0:
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
128 indicesBufferSize += indexAlignment - (indicesBufferSize mod indexAlignment)
323
9defff46da48 add: first complete working version of multiple materials and shaders per scene, yipie :)
Sam <sam@basx.dev>
parents: 322
diff changeset
129 indicesBufferSize += mesh.indexSize
128
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
130 if indicesBufferSize > 0:
315
4921ec86dcb4 did: preparations to refactor material system, still tons to do
Sam <sam@basx.dev>
parents: 308
diff changeset
131 scenedata.indexBuffer = renderer.device.createBuffer(
128
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
132 size=indicesBufferSize,
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
133 usage=[VK_BUFFER_USAGE_INDEX_BUFFER_BIT],
156
134647ed5b60 did: refactor memory selection
Sam <sam@basx.dev>
parents: 151
diff changeset
134 requireMappable=false,
151
a46923cb0790 did: better memory selection
Sam <sam@basx.dev>
parents: 146
diff changeset
135 preferVRAM=true,
128
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
136 )
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
137
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
138 # create vertex buffers and calculcate offsets
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
139 # trying to use one buffer per memory type
142
9c0d7839dc91 add: correct mesh buffer data updates to GPU
Sam <sam@basx.dev>
parents: 140
diff changeset
140 var
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
141 perLocationOffsets: Table[MemoryPerformanceHint, int]
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
142 perLocationSizes: Table[MemoryPerformanceHint, int]
142
9c0d7839dc91 add: correct mesh buffer data updates to GPU
Sam <sam@basx.dev>
parents: 140
diff changeset
143 bindingNumber = 0
222
ddfc54036e00 add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents: 211
diff changeset
144 for hint in MemoryPerformanceHint:
ddfc54036e00 add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents: 211
diff changeset
145 perLocationOffsets[hint] = 0
ddfc54036e00 add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents: 211
diff changeset
146 perLocationSizes[hint] = 0
138
62bc83b8a8c7 fix: mixing memory location types is not working
Sam <sam@basx.dev>
parents: 137
diff changeset
147 for attribute in inputs:
315
4921ec86dcb4 did: preparations to refactor material system, still tons to do
Sam <sam@basx.dev>
parents: 308
diff changeset
148 scenedata.attributeLocation[attribute.name] = attribute.memoryPerformanceHint
4921ec86dcb4 did: preparations to refactor material system, still tons to do
Sam <sam@basx.dev>
parents: 308
diff changeset
149 scenedata.attributeBindingNumber[attribute.name] = bindingNumber
142
9c0d7839dc91 add: correct mesh buffer data updates to GPU
Sam <sam@basx.dev>
parents: 140
diff changeset
150 inc bindingNumber
128
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
151 # 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
152 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
153 # align size to VERTEX_ATTRIB_ALIGNMENT bytes (the important thing is the correct alignment of the offsets, but
237
2fca78b0c0d6 fix: incorrect vertex data alignment
Sam <sam@basx.dev>
parents: 228
diff changeset
154 # we need to expand the buffer size as well, therefore considering alignment already here as well
2fca78b0c0d6 fix: incorrect vertex data alignment
Sam <sam@basx.dev>
parents: 228
diff changeset
155 if perLocationSizes[attribute.memoryPerformanceHint] mod VERTEX_ATTRIB_ALIGNMENT != 0:
2fca78b0c0d6 fix: incorrect vertex data alignment
Sam <sam@basx.dev>
parents: 228
diff changeset
156 perLocationSizes[attribute.memoryPerformanceHint] += VERTEX_ATTRIB_ALIGNMENT - (perLocationSizes[attribute.memoryPerformanceHint] mod VERTEX_ATTRIB_ALIGNMENT)
323
9defff46da48 add: first complete working version of multiple materials and shaders per scene, yipie :)
Sam <sam@basx.dev>
parents: 322
diff changeset
157 perLocationSizes[attribute.memoryPerformanceHint] += mesh.attributeSize(attribute.name)
156
134647ed5b60 did: refactor memory selection
Sam <sam@basx.dev>
parents: 151
diff changeset
158 for memoryPerformanceHint, bufferSize in perLocationSizes.pairs:
128
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
159 if bufferSize > 0:
315
4921ec86dcb4 did: preparations to refactor material system, still tons to do
Sam <sam@basx.dev>
parents: 308
diff changeset
160 scenedata.vertexBuffers[memoryPerformanceHint] = renderer.device.createBuffer(
128
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
161 size=bufferSize,
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
162 usage=[VK_BUFFER_USAGE_VERTEX_BUFFER_BIT],
156
134647ed5b60 did: refactor memory selection
Sam <sam@basx.dev>
parents: 151
diff changeset
163 requireMappable=memoryPerformanceHint==PreferFastWrite,
134647ed5b60 did: refactor memory selection
Sam <sam@basx.dev>
parents: 151
diff changeset
164 preferVRAM=true,
128
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
165 )
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
166
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
167 # fill vertex buffers
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
168 var indexBufferOffset = 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
169 for (meshIndex, mesh) in enumerate(scene.meshes):
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
170 var offsets: seq[(string, MemoryPerformanceHint, int)]
138
62bc83b8a8c7 fix: mixing memory location types is not working
Sam <sam@basx.dev>
parents: 137
diff changeset
171 for attribute in inputs:
164
7a0ca5c01095 fix: buffer update with staging buffer not correctly working
Sam <sam@basx.dev>
parents: 157
diff changeset
172 offsets.add (attribute.name, attribute.memoryPerformanceHint, perLocationOffsets[attribute.memoryPerformanceHint])
138
62bc83b8a8c7 fix: mixing memory location types is not working
Sam <sam@basx.dev>
parents: 137
diff changeset
173 var (pdata, size) = mesh.getRawData(attribute.name)
222
ddfc54036e00 add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents: 211
diff changeset
174 if pdata != nil: # no data
315
4921ec86dcb4 did: preparations to refactor material system, still tons to do
Sam <sam@basx.dev>
parents: 308
diff changeset
175 scenedata.vertexBuffers[attribute.memoryPerformanceHint].setData(pdata, size, perLocationOffsets[attribute.memoryPerformanceHint])
222
ddfc54036e00 add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents: 211
diff changeset
176 perLocationOffsets[attribute.memoryPerformanceHint] += size
237
2fca78b0c0d6 fix: incorrect vertex data alignment
Sam <sam@basx.dev>
parents: 228
diff changeset
177 if perLocationOffsets[attribute.memoryPerformanceHint] mod VERTEX_ATTRIB_ALIGNMENT != 0:
2fca78b0c0d6 fix: incorrect vertex data alignment
Sam <sam@basx.dev>
parents: 228
diff changeset
178 perLocationOffsets[attribute.memoryPerformanceHint] += VERTEX_ATTRIB_ALIGNMENT - (perLocationOffsets[attribute.memoryPerformanceHint] mod VERTEX_ATTRIB_ALIGNMENT)
128
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
179
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
180 let indexed = mesh.indexType != MeshIndexType.None
128
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
181 var drawable = Drawable(
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
182 elementCount: if indexed: mesh.indicesCount else: mesh.vertexCount,
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
183 bufferOffsets: offsets,
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
184 instanceCount: mesh.instanceCount,
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
185 indexed: indexed,
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
186 )
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
187 if indexed:
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
188 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
189 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
190 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
191 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
192 of Big: 4
128
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
193 # index value alignment required by Vulkan
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
194 if indexBufferOffset mod indexAlignment != 0:
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
195 indexBufferOffset += indexAlignment - (indexBufferOffset mod indexAlignment)
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
196 drawable.indexBufferOffset = indexBufferOffset
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
197 drawable.indexType = mesh.indexType
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
198 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
199 scenedata.indexBuffer.setData(pdata, size, indexBufferOffset)
128
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
200 indexBufferOffset += size
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
201 scenedata.drawables.add (drawable, meshIndex)
142
9c0d7839dc91 add: correct mesh buffer data updates to GPU
Sam <sam@basx.dev>
parents: 140
diff changeset
202
263
d1ee2a815fa1 add: some api improvments, preparing for font-loading
Sam <sam@basx.dev>
parents: 251
diff changeset
203 # 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
204 for subpass_i in 0 ..< renderer.renderPass.subpasses.len:
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
205 for materialType, pipeline in renderer.renderPass.subpasses[subpass_i].pipelines.pairs:
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
206 if scene.usesMaterialType(materialType):
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
207 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
208 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
209 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
210 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
211 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
212 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
213 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
214 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
215 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
216 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
217 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
218 )
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
219
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
220 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
221 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
222 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
223 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
224 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
225 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
226
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
227 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
228
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
229 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
230 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
231 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
232 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
233 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
234 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
235 )
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
236 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
237 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
238
315
4921ec86dcb4 did: preparations to refactor material system, still tons to do
Sam <sam@basx.dev>
parents: 308
diff changeset
239 renderer.scenedata[scene] = scenedata
142
9c0d7839dc91 add: correct mesh buffer data updates to GPU
Sam <sam@basx.dev>
parents: 140
diff changeset
240
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
241 proc refreshMeshAttributeData(renderer: Renderer, scene: Scene, drawable: Drawable, meshIndex: int, attribute: string) =
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
242 debug &"Refreshing data on mesh {scene.meshes[meshIndex]} for {attribute}"
222
ddfc54036e00 add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents: 211
diff changeset
243 # 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
244 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
245 return
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
246 var (pdata, size) = scene.meshes[meshIndex].getRawData(attribute)
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
247 let memoryPerformanceHint = renderer.scenedata[scene].attributeLocation[attribute]
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
248 let bindingNumber = renderer.scenedata[scene].attributeBindingNumber[attribute]
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
249 renderer.scenedata[scene].vertexBuffers[memoryPerformanceHint].setData(pdata, size, drawable.bufferOffsets[bindingNumber][2])
142
9c0d7839dc91 add: correct mesh buffer data updates to GPU
Sam <sam@basx.dev>
parents: 140
diff changeset
250
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
251 proc updateMeshData*(renderer: var Renderer, scene: var Scene) =
142
9c0d7839dc91 add: correct mesh buffer data updates to GPU
Sam <sam@basx.dev>
parents: 140
diff changeset
252 assert scene in renderer.scenedata
128
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
253
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
254 for (drawable, meshIndex) in renderer.scenedata[scene].drawables.mitems:
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
255 if scene.meshes[meshIndex].attributes.contains(TRANSFORMATTRIBUTE):
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
256 scene.meshes[meshIndex].updateInstanceTransforms(TRANSFORMATTRIBUTE)
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
257 for attribute in scene.meshes[meshIndex].dirtyAttributes:
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
258 renderer.refreshMeshAttributeData(scene, drawable, meshIndex, attribute)
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
259 debug &"Update mesh attribute {attribute}"
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
260 scene.meshes[meshIndex].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
261
228
6b02e108ba54 add: small refactoring
Sam <sam@basx.dev>
parents: 222
diff changeset
262 proc updateUniformData*(renderer: var Renderer, scene: var Scene) =
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
263 assert scene in renderer.scenedata
228
6b02e108ba54 add: small refactoring
Sam <sam@basx.dev>
parents: 222
diff changeset
264
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
265 for i in 0 ..< renderer.renderPass.subpasses.len:
323
9defff46da48 add: first complete working version of multiple materials and shaders per scene, yipie :)
Sam <sam@basx.dev>
parents: 322
diff changeset
266 for materialType, pipeline in renderer.renderPass.subpasses[i].pipelines.pairs:
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
267 if scene.usesMaterialType(materialType) and renderer.scenedata[scene].uniformBuffers.hasKey(pipeline.vk) and renderer.scenedata[scene].uniformBuffers[pipeline.vk].len != 0:
266
fd1a95f433d1 add: better material loading system, still far from great
Sam <sam@basx.dev>
parents: 263
diff changeset
268 assert renderer.scenedata[scene].uniformBuffers[pipeline.vk][renderer.swapchain.currentInFlight].vk.valid
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
269 var offset = 0
266
fd1a95f433d1 add: better material loading system, still far from great
Sam <sam@basx.dev>
parents: 263
diff changeset
270 for uniform in pipeline.uniforms:
fd1a95f433d1 add: better material loading system, still far from great
Sam <sam@basx.dev>
parents: 263
diff changeset
271 if not scene.shaderGlobals.hasKey(uniform.name):
fd1a95f433d1 add: better material loading system, still far from great
Sam <sam@basx.dev>
parents: 263
diff changeset
272 raise newException(Exception, &"Uniform '{uniform.name}' not found in scene shaderGlobals")
fd1a95f433d1 add: better material loading system, still far from great
Sam <sam@basx.dev>
parents: 263
diff changeset
273 if uniform.thetype != scene.shaderGlobals[uniform.name].thetype:
fd1a95f433d1 add: better material loading system, still far from great
Sam <sam@basx.dev>
parents: 263
diff changeset
274 raise newException(Exception, &"Uniform '{uniform.name}' has wrong type {uniform.thetype}, required is {scene.shaderGlobals[uniform.name].thetype}")
323
9defff46da48 add: first complete working version of multiple materials and shaders per scene, yipie :)
Sam <sam@basx.dev>
parents: 322
diff changeset
275 debug &"Update uniforms {uniform.name}"
266
fd1a95f433d1 add: better material loading system, still far from great
Sam <sam@basx.dev>
parents: 263
diff changeset
276 let (pdata, size) = scene.shaderGlobals[uniform.name].getRawData()
fd1a95f433d1 add: better material loading system, still far from great
Sam <sam@basx.dev>
parents: 263
diff changeset
277 renderer.scenedata[scene].uniformBuffers[pipeline.vk][renderer.swapchain.currentInFlight].setData(pdata, size, offset)
fd1a95f433d1 add: better material loading system, still far from great
Sam <sam@basx.dev>
parents: 263
diff changeset
278 offset += size
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
279
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
280 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
281 assert scene in renderer.scenedata
9c0d7839dc91 add: correct mesh buffer data updates to GPU
Sam <sam@basx.dev>
parents: 140
diff changeset
282
131
11666d28e04d add: recreation of swapchain (at least on linux, windows will likely fail, needs testing
Sam <sam@basx.dev>
parents: 129
diff changeset
283 var
11666d28e04d add: recreation of swapchain (at least on linux, windows will likely fail, needs testing
Sam <sam@basx.dev>
parents: 129
diff changeset
284 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
285 commandBuffer: VkCommandBuffer
128
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
286
131
11666d28e04d add: recreation of swapchain (at least on linux, windows will likely fail, needs testing
Sam <sam@basx.dev>
parents: 129
diff changeset
287 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
288 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
289 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
290 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
291 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
292 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
293 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
294 return
228
6b02e108ba54 add: small refactoring
Sam <sam@basx.dev>
parents: 222
diff changeset
295
131
11666d28e04d add: recreation of swapchain (at least on linux, windows will likely fail, needs testing
Sam <sam@basx.dev>
parents: 129
diff changeset
296 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
297 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
298
322
6dab370d1758 add: first, incomplete version of material use
Sam <sam@basx.dev>
parents: 321
diff changeset
299 debug "Scene buffers:"
6dab370d1758 add: first, incomplete version of material use
Sam <sam@basx.dev>
parents: 321
diff changeset
300 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
301 debug " ", location, ": ", buffer
6dab370d1758 add: first, incomplete version of material use
Sam <sam@basx.dev>
parents: 321
diff changeset
302 debug " Index buffer: ", renderer.scenedata[scene].indexBuffer
6dab370d1758 add: first, incomplete version of material use
Sam <sam@basx.dev>
parents: 321
diff changeset
303
131
11666d28e04d add: recreation of swapchain (at least on linux, windows will likely fail, needs testing
Sam <sam@basx.dev>
parents: 129
diff changeset
304 for i in 0 ..< renderer.renderPass.subpasses.len:
322
6dab370d1758 add: first, incomplete version of material use
Sam <sam@basx.dev>
parents: 321
diff changeset
305 for materialType, pipeline in renderer.renderPass.subpasses[i].pipelines.pairs:
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
306 if scene.usesMaterialType(materialType):
323
9defff46da48 add: first complete working version of multiple materials and shaders per scene, yipie :)
Sam <sam@basx.dev>
parents: 322
diff changeset
307 debug &"Start pipeline for {materialType}"
9defff46da48 add: first complete working version of multiple materials and shaders per scene, yipie :)
Sam <sam@basx.dev>
parents: 322
diff changeset
308 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
309 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
310
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
311 for (drawable, meshIndex) in renderer.scenedata[scene].drawables:
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 if scene.meshes[meshIndex].material != nil and scene.meshes[meshIndex].material.materialType == materialType:
323
9defff46da48 add: first complete working version of multiple materials and shaders per scene, yipie :)
Sam <sam@basx.dev>
parents: 322
diff changeset
313 drawable.draw(commandBuffer, vertexBuffers=renderer.scenedata[scene].vertexBuffers, indexBuffer=renderer.scenedata[scene].indexBuffer)
128
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
314
131
11666d28e04d add: recreation of swapchain (at least on linux, windows will likely fail, needs testing
Sam <sam@basx.dev>
parents: 129
diff changeset
315 if i < renderer.renderPass.subpasses.len - 1:
128
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
316 commandBuffer.vkCmdNextSubpass(VK_SUBPASS_CONTENTS_INLINE)
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
317
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
318 commandBuffer.endRenderCommands()
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
319
131
11666d28e04d add: recreation of swapchain (at least on linux, windows will likely fail, needs testing
Sam <sam@basx.dev>
parents: 129
diff changeset
320 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
321 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
322 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
323 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
324 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
325 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
326 oldSwapchain.destroy()
132
25fe4972ef9c add: support for smooth swapchain-recreation
Sam <sam@basx.dev>
parents: 131
diff changeset
327
128
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
328 func framesRendered*(renderer: Renderer): uint64 =
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
329 renderer.swapchain.framesRendered
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
330
140
29f0109c5222 add: some helper functions
Sam <sam@basx.dev>
parents: 138
diff changeset
331 func valid*(renderer: Renderer): bool =
29f0109c5222 add: some helper functions
Sam <sam@basx.dev>
parents: 138
diff changeset
332 renderer.device.vk.valid
29f0109c5222 add: some helper functions
Sam <sam@basx.dev>
parents: 138
diff changeset
333
128
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
334 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
335 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
336 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
337 assert buffer.vk.valid
128
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
338 buffer.destroy()
315
4921ec86dcb4 did: preparations to refactor material system, still tons to do
Sam <sam@basx.dev>
parents: 308
diff changeset
339 if scenedata.indexBuffer.vk.valid:
4921ec86dcb4 did: preparations to refactor material system, still tons to do
Sam <sam@basx.dev>
parents: 308
diff changeset
340 assert scenedata.indexBuffer.vk.valid
4921ec86dcb4 did: preparations to refactor material system, still tons to do
Sam <sam@basx.dev>
parents: 308
diff changeset
341 scenedata.indexBuffer.destroy()
4921ec86dcb4 did: preparations to refactor material system, still tons to do
Sam <sam@basx.dev>
parents: 308
diff changeset
342 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
343 for buffer in pipelineUniforms.mitems:
fd1a95f433d1 add: better material loading system, still far from great
Sam <sam@basx.dev>
parents: 263
diff changeset
344 assert buffer.vk.valid
fd1a95f433d1 add: better material loading system, still far from great
Sam <sam@basx.dev>
parents: 263
diff changeset
345 buffer.destroy()
315
4921ec86dcb4 did: preparations to refactor material system, still tons to do
Sam <sam@basx.dev>
parents: 308
diff changeset
346 for textures in scenedata.textures.mvalues:
201
ab626e67a1ee add: support for arrays of samplers
Sam <sam@basx.dev>
parents: 200
diff changeset
347 for texture in textures.mitems:
ab626e67a1ee add: support for arrays of samplers
Sam <sam@basx.dev>
parents: 200
diff changeset
348 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
349 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
350 descriptorPool.destroy()
9defff46da48 add: first complete working version of multiple materials and shaders per scene, yipie :)
Sam <sam@basx.dev>
parents: 322
diff changeset
351 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
352 renderer.renderPass.destroy()
128
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
353 renderer.swapchain.destroy()