annotate src/semicongine/renderer.nim @ 238:ef8cea2545f8

did: make material working halfway, add some more mouse functionality
author Sam <sam@basx.dev>
date Sat, 20 May 2023 23:16:48 +0700
parents 2fca78b0c0d6
children 671b9267a533
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
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
4 import std/strutils
128
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
5 import std/logging
127
5871acc2977e did: big refactoring
Sam <sam@basx.dev>
parents:
diff changeset
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
5871acc2977e did: big refactoring
Sam <sam@basx.dev>
parents:
diff changeset
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
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
11 import ./vulkan/pipeline
127
5871acc2977e did: big refactoring
Sam <sam@basx.dev>
parents:
diff changeset
12 import ./vulkan/physicaldevice
5871acc2977e did: big refactoring
Sam <sam@basx.dev>
parents:
diff changeset
13 import ./vulkan/renderpass
5871acc2977e did: big refactoring
Sam <sam@basx.dev>
parents:
diff changeset
14 import ./vulkan/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
15 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
16 import ./vulkan/image
127
5871acc2977e did: big refactoring
Sam <sam@basx.dev>
parents:
diff changeset
17
128
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
18 import ./entity
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
19 import ./mesh
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
20
127
5871acc2977e did: big refactoring
Sam <sam@basx.dev>
parents:
diff changeset
21 type
128
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
22 SceneData = object
142
9c0d7839dc91 add: correct mesh buffer data updates to GPU
Sam <sam@basx.dev>
parents: 140
diff changeset
23 drawables*: OrderedTable[Mesh, Drawable]
156
134647ed5b60 did: refactor memory selection
Sam <sam@basx.dev>
parents: 151
diff changeset
24 vertexBuffers*: Table[MemoryPerformanceHint, Buffer]
128
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
25 indexBuffer*: Buffer
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
26 uniformBuffers*: seq[Buffer] # one per frame-in-flight
201
ab626e67a1ee add: support for arrays of samplers
Sam <sam@basx.dev>
parents: 200
diff changeset
27 textures*: Table[string, seq[Texture]] # per frame-in-flight
156
134647ed5b60 did: refactor memory selection
Sam <sam@basx.dev>
parents: 151
diff changeset
28 attributeLocation*: Table[string, MemoryPerformanceHint]
142
9c0d7839dc91 add: correct mesh buffer data updates to GPU
Sam <sam@basx.dev>
parents: 140
diff changeset
29 attributeBindingNumber*: Table[string, int]
9c0d7839dc91 add: correct mesh buffer data updates to GPU
Sam <sam@basx.dev>
parents: 140
diff changeset
30 transformAttribute: string # name of attribute that is used for per-instance mesh transformation
9c0d7839dc91 add: correct mesh buffer data updates to GPU
Sam <sam@basx.dev>
parents: 140
diff changeset
31 entityTransformationCache: Table[Mesh, Mat4] # remembers last transformation, avoid to send GPU-updates if no changes
128
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
32 Renderer* = object
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
33 device: Device
127
5871acc2977e did: big refactoring
Sam <sam@basx.dev>
parents:
diff changeset
34 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
35 renderPass: RenderPass
127
5871acc2977e did: big refactoring
Sam <sam@basx.dev>
parents:
diff changeset
36 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
37 scenedata: Table[Scene, SceneData]
127
5871acc2977e did: big refactoring
Sam <sam@basx.dev>
parents:
diff changeset
38
5871acc2977e did: big refactoring
Sam <sam@basx.dev>
parents:
diff changeset
39
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 proc initRenderer*(device: Device, renderPass: RenderPass): Renderer =
127
5871acc2977e did: big refactoring
Sam <sam@basx.dev>
parents:
diff changeset
41 assert device.vk.valid
131
11666d28e04d add: recreation of swapchain (at least on linux, windows will likely fail, needs testing
Sam <sam@basx.dev>
parents: 129
diff changeset
42 assert renderPass.vk.valid
127
5871acc2977e did: big refactoring
Sam <sam@basx.dev>
parents:
diff changeset
43
128
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
44 result.device = device
131
11666d28e04d add: recreation of swapchain (at least on linux, windows will likely fail, needs testing
Sam <sam@basx.dev>
parents: 129
diff changeset
45 result.renderPass = renderPass
127
5871acc2977e did: big refactoring
Sam <sam@basx.dev>
parents:
diff changeset
46 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
47 # 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
48 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
49 if not swapchain.isSome:
127
5871acc2977e did: big refactoring
Sam <sam@basx.dev>
parents:
diff changeset
50 raise newException(Exception, "Unable to create 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
51 result.swapchain = swapchain.get()
128
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
52
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
53 proc setupDrawableBuffers*(renderer: var Renderer, scene: Scene, inputs: seq[ShaderAttribute], transformAttribute="") =
142
9c0d7839dc91 add: correct mesh buffer data updates to GPU
Sam <sam@basx.dev>
parents: 140
diff changeset
54 assert not (scene in renderer.scenedata)
237
2fca78b0c0d6 fix: incorrect vertex data alignment
Sam <sam@basx.dev>
parents: 228
diff changeset
55 const VERTEX_ATTRIB_ALIGNMENT = 4 # used for buffer alignment
128
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
56 var data = SceneData()
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
57
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
58 # when mesh transformation are handled through the scenegraph-transformation, set it up here
142
9c0d7839dc91 add: correct mesh buffer data updates to GPU
Sam <sam@basx.dev>
parents: 140
diff changeset
59 if transformattribute != "":
9c0d7839dc91 add: correct mesh buffer data updates to GPU
Sam <sam@basx.dev>
parents: 140
diff changeset
60 var hasTransformAttribute = false
9c0d7839dc91 add: correct mesh buffer data updates to GPU
Sam <sam@basx.dev>
parents: 140
diff changeset
61 for input in inputs:
9c0d7839dc91 add: correct mesh buffer data updates to GPU
Sam <sam@basx.dev>
parents: 140
diff changeset
62 if input.name == transformattribute:
9c0d7839dc91 add: correct mesh buffer data updates to GPU
Sam <sam@basx.dev>
parents: 140
diff changeset
63 assert input.perInstance == true, $input
9c0d7839dc91 add: correct mesh buffer data updates to GPU
Sam <sam@basx.dev>
parents: 140
diff changeset
64 assert getDataType[Mat4]() == input.thetype
9c0d7839dc91 add: correct mesh buffer data updates to GPU
Sam <sam@basx.dev>
parents: 140
diff changeset
65 hasTransformAttribute = true
9c0d7839dc91 add: correct mesh buffer data updates to GPU
Sam <sam@basx.dev>
parents: 140
diff changeset
66 assert hasTransformAttribute
9c0d7839dc91 add: correct mesh buffer data updates to GPU
Sam <sam@basx.dev>
parents: 140
diff changeset
67 data.transformAttribute = transformAttribute
9c0d7839dc91 add: correct mesh buffer data updates to GPU
Sam <sam@basx.dev>
parents: 140
diff changeset
68
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
69 # find all meshes, populate missing attribute values for shader
128
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
70 var allMeshes: seq[Mesh]
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
71 for mesh in allComponentsOfType[Mesh](scene.root):
128
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
72 allMeshes.add mesh
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
73 for inputAttr in inputs:
146
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 142
diff changeset
74 if not mesh.hasDataFor(inputAttr.name):
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 142
diff changeset
75 mesh.initData(inputAttr)
210
f3912838cd69 fix: always use rgba
Sam <sam@basx.dev>
parents: 206
diff changeset
76 assert mesh.dataType(inputAttr.name) == inputAttr.thetype, &"mesh attribute {inputAttr.name} has type {mesh.dataType(inputAttr.name)} but shader expects {inputAttr.thetype}"
128
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
77
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
78 # create index buffer if necessary
128
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
79 var indicesBufferSize = 0'u64
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
80 for mesh in allMeshes:
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
81 if mesh.indexType != None:
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
82 let indexAlignment = case mesh.indexType
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
83 of None: 0'u64
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
84 of Tiny: 1'u64
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
85 of Small: 2'u64
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
86 of Big: 4'u64
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
87 # index value alignment required by Vulkan
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
88 if indicesBufferSize mod indexAlignment != 0:
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
89 indicesBufferSize += indexAlignment - (indicesBufferSize mod indexAlignment)
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
90 indicesBufferSize += mesh.indexDataSize
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
91 if indicesBufferSize > 0:
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
92 data.indexBuffer = renderer.device.createBuffer(
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
93 size=indicesBufferSize,
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
94 usage=[VK_BUFFER_USAGE_INDEX_BUFFER_BIT],
156
134647ed5b60 did: refactor memory selection
Sam <sam@basx.dev>
parents: 151
diff changeset
95 requireMappable=false,
151
a46923cb0790 did: better memory selection
Sam <sam@basx.dev>
parents: 146
diff changeset
96 preferVRAM=true,
128
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
97 )
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
98
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
99 # 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
100 # 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
101 var
156
134647ed5b60 did: refactor memory selection
Sam <sam@basx.dev>
parents: 151
diff changeset
102 perLocationOffsets: Table[MemoryPerformanceHint, uint64]
134647ed5b60 did: refactor memory selection
Sam <sam@basx.dev>
parents: 151
diff changeset
103 perLocationSizes: Table[MemoryPerformanceHint, uint64]
142
9c0d7839dc91 add: correct mesh buffer data updates to GPU
Sam <sam@basx.dev>
parents: 140
diff changeset
104 bindingNumber = 0
222
ddfc54036e00 add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents: 211
diff changeset
105 for hint in MemoryPerformanceHint:
ddfc54036e00 add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents: 211
diff changeset
106 perLocationOffsets[hint] = 0
ddfc54036e00 add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents: 211
diff changeset
107 perLocationSizes[hint] = 0
138
62bc83b8a8c7 fix: mixing memory location types is not working
Sam <sam@basx.dev>
parents: 137
diff changeset
108 for attribute in inputs:
156
134647ed5b60 did: refactor memory selection
Sam <sam@basx.dev>
parents: 151
diff changeset
109 data.attributeLocation[attribute.name] = attribute.memoryPerformanceHint
142
9c0d7839dc91 add: correct mesh buffer data updates to GPU
Sam <sam@basx.dev>
parents: 140
diff changeset
110 data.attributeBindingNumber[attribute.name] = bindingNumber
9c0d7839dc91 add: correct mesh buffer data updates to GPU
Sam <sam@basx.dev>
parents: 140
diff changeset
111 inc bindingNumber
128
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
112 # setup one buffer per attribute-location-type
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
113 for mesh in allMeshes:
237
2fca78b0c0d6 fix: incorrect vertex data alignment
Sam <sam@basx.dev>
parents: 228
diff changeset
114 # align size to VERTEX_ATTRIB_ALIGNMENT bytes (the important thing is the correct alignment of the offsets, bu
2fca78b0c0d6 fix: incorrect vertex data alignment
Sam <sam@basx.dev>
parents: 228
diff changeset
115 # 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
116 if perLocationSizes[attribute.memoryPerformanceHint] mod VERTEX_ATTRIB_ALIGNMENT != 0:
2fca78b0c0d6 fix: incorrect vertex data alignment
Sam <sam@basx.dev>
parents: 228
diff changeset
117 perLocationSizes[attribute.memoryPerformanceHint] += VERTEX_ATTRIB_ALIGNMENT - (perLocationSizes[attribute.memoryPerformanceHint] mod VERTEX_ATTRIB_ALIGNMENT)
156
134647ed5b60 did: refactor memory selection
Sam <sam@basx.dev>
parents: 151
diff changeset
118 perLocationSizes[attribute.memoryPerformanceHint] += mesh.dataSize(attribute.name)
134647ed5b60 did: refactor memory selection
Sam <sam@basx.dev>
parents: 151
diff changeset
119 for memoryPerformanceHint, bufferSize in perLocationSizes.pairs:
128
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
120 if bufferSize > 0:
156
134647ed5b60 did: refactor memory selection
Sam <sam@basx.dev>
parents: 151
diff changeset
121 data.vertexBuffers[memoryPerformanceHint] = renderer.device.createBuffer(
128
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
122 size=bufferSize,
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
123 usage=[VK_BUFFER_USAGE_VERTEX_BUFFER_BIT],
156
134647ed5b60 did: refactor memory selection
Sam <sam@basx.dev>
parents: 151
diff changeset
124 requireMappable=memoryPerformanceHint==PreferFastWrite,
134647ed5b60 did: refactor memory selection
Sam <sam@basx.dev>
parents: 151
diff changeset
125 preferVRAM=true,
128
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
126 )
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
127
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
128 # fill vertex buffers
128
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
129 var indexBufferOffset = 0'u64
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
130 for mesh in allMeshes:
164
7a0ca5c01095 fix: buffer update with staging buffer not correctly working
Sam <sam@basx.dev>
parents: 157
diff changeset
131 var offsets: seq[(string, MemoryPerformanceHint, uint64)]
138
62bc83b8a8c7 fix: mixing memory location types is not working
Sam <sam@basx.dev>
parents: 137
diff changeset
132 for attribute in inputs:
164
7a0ca5c01095 fix: buffer update with staging buffer not correctly working
Sam <sam@basx.dev>
parents: 157
diff changeset
133 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
134 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
135 if pdata != nil: # no data
ddfc54036e00 add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents: 211
diff changeset
136 data.vertexBuffers[attribute.memoryPerformanceHint].setData(pdata, size, perLocationOffsets[attribute.memoryPerformanceHint])
ddfc54036e00 add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents: 211
diff changeset
137 perLocationOffsets[attribute.memoryPerformanceHint] += size
237
2fca78b0c0d6 fix: incorrect vertex data alignment
Sam <sam@basx.dev>
parents: 228
diff changeset
138 if perLocationOffsets[attribute.memoryPerformanceHint] mod VERTEX_ATTRIB_ALIGNMENT != 0:
2fca78b0c0d6 fix: incorrect vertex data alignment
Sam <sam@basx.dev>
parents: 228
diff changeset
139 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
140
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
141 let indexed = mesh.indexType != None
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
142 var drawable = Drawable(
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
143 elementCount: if indexed: mesh.indicesCount else: mesh.vertexCount,
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
144 bufferOffsets: offsets,
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
145 instanceCount: mesh.instanceCount,
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
146 indexed: indexed,
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
147 )
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
148 if indexed:
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
149 let indexAlignment = case mesh.indexType
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
150 of None: 0'u64
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
151 of Tiny: 1'u64
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
152 of Small: 2'u64
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
153 of Big: 4'u64
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
154 # index value alignment required by Vulkan
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
155 if indexBufferOffset mod indexAlignment != 0:
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
156 indexBufferOffset += indexAlignment - (indexBufferOffset mod indexAlignment)
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
157 drawable.indexBufferOffset = indexBufferOffset
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
158 drawable.indexType = mesh.indexType
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
159 var (pdata, size) = mesh.getRawIndexData()
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
160 data.indexBuffer.setData(pdata, size, indexBufferOffset)
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
161 indexBufferOffset += size
142
9c0d7839dc91 add: correct mesh buffer data updates to GPU
Sam <sam@basx.dev>
parents: 140
diff changeset
162 data.drawables[mesh] = drawable
9c0d7839dc91 add: correct mesh buffer data updates to GPU
Sam <sam@basx.dev>
parents: 140
diff changeset
163
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
164 # setup uniforms and textures
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
165 for subpass_i in 0 ..< renderer.renderPass.subpasses.len:
228
6b02e108ba54 add: small refactoring
Sam <sam@basx.dev>
parents: 222
diff changeset
166 for pipeline in renderer.renderPass.subpasses[subpass_i].pipelines.mitems:
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 var uniformBufferSize = 0'u64
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
168 for uniform in pipeline.uniforms:
228
6b02e108ba54 add: small refactoring
Sam <sam@basx.dev>
parents: 222
diff changeset
169 uniformBufferSize += uniform.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
170 if uniformBufferSize > 0:
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
171 for frame_i in 0 ..< renderer.swapchain.inFlightFrames:
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
172 data.uniformBuffers.add renderer.device.createBuffer(
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
173 size=uniformBufferSize,
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
174 usage=[VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT],
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
175 requireMappable=true,
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 preferVRAM=true,
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
177 )
201
ab626e67a1ee add: support for arrays of samplers
Sam <sam@basx.dev>
parents: 200
diff changeset
178 for name, images in scene.textures.pairs:
ab626e67a1ee add: support for arrays of samplers
Sam <sam@basx.dev>
parents: 200
diff changeset
179 data.textures[name] = @[]
211
744285b47a4d did: refactor image handling
Sam <sam@basx.dev>
parents: 210
diff changeset
180 let interpolation = images[1]
744285b47a4d did: refactor image handling
Sam <sam@basx.dev>
parents: 210
diff changeset
181 for image in images[0]:
744285b47a4d did: refactor image handling
Sam <sam@basx.dev>
parents: 210
diff changeset
182 data.textures[name].add renderer.device.createTexture(image.width, image.height, 4, addr image.imagedata[0][0], interpolation)
238
ef8cea2545f8 did: make material working halfway, add some more mouse functionality
Sam <sam@basx.dev>
parents: 237
diff changeset
183 # TODO: this only bind shit to the last scene, find per-scene solution
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
184 pipeline.setupDescriptors(data.uniformBuffers, data.textures, inFlightFrames=renderer.swapchain.inFlightFrames)
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
185 for frame_i in 0 ..< renderer.swapchain.inFlightFrames:
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
186 pipeline.descriptorSets[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
187
142
9c0d7839dc91 add: correct mesh buffer data updates to GPU
Sam <sam@basx.dev>
parents: 140
diff changeset
188 renderer.scenedata[scene] = data
9c0d7839dc91 add: correct mesh buffer data updates to GPU
Sam <sam@basx.dev>
parents: 140
diff changeset
189
9c0d7839dc91 add: correct mesh buffer data updates to GPU
Sam <sam@basx.dev>
parents: 140
diff changeset
190 proc refreshMeshAttributeData(sceneData: var SceneData, mesh: Mesh, attribute: string) =
9c0d7839dc91 add: correct mesh buffer data updates to GPU
Sam <sam@basx.dev>
parents: 140
diff changeset
191 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
192 # ignore attributes that are not used in this shader
ddfc54036e00 add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents: 211
diff changeset
193 if not (attribute in sceneData.attributeLocation):
ddfc54036e00 add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents: 211
diff changeset
194 return
142
9c0d7839dc91 add: correct mesh buffer data updates to GPU
Sam <sam@basx.dev>
parents: 140
diff changeset
195 var (pdata, size) = mesh.getRawData(attribute)
156
134647ed5b60 did: refactor memory selection
Sam <sam@basx.dev>
parents: 151
diff changeset
196 let memoryPerformanceHint = sceneData.attributeLocation[attribute]
142
9c0d7839dc91 add: correct mesh buffer data updates to GPU
Sam <sam@basx.dev>
parents: 140
diff changeset
197 let bindingNumber = sceneData.attributeBindingNumber[attribute]
164
7a0ca5c01095 fix: buffer update with staging buffer not correctly working
Sam <sam@basx.dev>
parents: 157
diff changeset
198 sceneData.vertexBuffers[memoryPerformanceHint].setData(pdata, size, sceneData.drawables[mesh].bufferOffsets[bindingNumber][2])
142
9c0d7839dc91 add: correct mesh buffer data updates to GPU
Sam <sam@basx.dev>
parents: 140
diff changeset
199
228
6b02e108ba54 add: small refactoring
Sam <sam@basx.dev>
parents: 222
diff changeset
200 proc updateMeshData*(renderer: var Renderer, scene: Scene) =
142
9c0d7839dc91 add: correct mesh buffer data updates to GPU
Sam <sam@basx.dev>
parents: 140
diff changeset
201 assert scene in renderer.scenedata
128
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
202
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
203 for mesh in allComponentsOfType[Mesh](scene.root):
142
9c0d7839dc91 add: correct mesh buffer data updates to GPU
Sam <sam@basx.dev>
parents: 140
diff changeset
204 # if mesh transformation attribute is enabled, update the model matrix
9c0d7839dc91 add: correct mesh buffer data updates to GPU
Sam <sam@basx.dev>
parents: 140
diff changeset
205 if renderer.scenedata[scene].transformAttribute != "":
9c0d7839dc91 add: correct mesh buffer data updates to GPU
Sam <sam@basx.dev>
parents: 140
diff changeset
206 let transform = mesh.entity.getModelTransform()
9c0d7839dc91 add: correct mesh buffer data updates to GPU
Sam <sam@basx.dev>
parents: 140
diff changeset
207 if not (mesh in renderer.scenedata[scene].entityTransformationCache) or renderer.scenedata[scene].entityTransformationCache[mesh] != transform:
9c0d7839dc91 add: correct mesh buffer data updates to GPU
Sam <sam@basx.dev>
parents: 140
diff changeset
208 mesh.updateInstanceData(renderer.scenedata[scene].transformAttribute, @[transform])
9c0d7839dc91 add: correct mesh buffer data updates to GPU
Sam <sam@basx.dev>
parents: 140
diff changeset
209 renderer.scenedata[scene].entityTransformationCache[mesh] = transform
128
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
210
142
9c0d7839dc91 add: correct mesh buffer data updates to GPU
Sam <sam@basx.dev>
parents: 140
diff changeset
211 # update any changed mesh attributes
9c0d7839dc91 add: correct mesh buffer data updates to GPU
Sam <sam@basx.dev>
parents: 140
diff changeset
212 for attribute in mesh.availableAttributes():
9c0d7839dc91 add: correct mesh buffer data updates to GPU
Sam <sam@basx.dev>
parents: 140
diff changeset
213 if mesh.hasDataChanged(attribute):
9c0d7839dc91 add: correct mesh buffer data updates to GPU
Sam <sam@basx.dev>
parents: 140
diff changeset
214 renderer.scenedata[scene].refreshMeshAttributeData(mesh, attribute)
9c0d7839dc91 add: correct mesh buffer data updates to GPU
Sam <sam@basx.dev>
parents: 140
diff changeset
215 var m = mesh
9c0d7839dc91 add: correct mesh buffer data updates to GPU
Sam <sam@basx.dev>
parents: 140
diff changeset
216 m.clearDataChanged()
9c0d7839dc91 add: correct mesh buffer data updates to GPU
Sam <sam@basx.dev>
parents: 140
diff changeset
217
228
6b02e108ba54 add: small refactoring
Sam <sam@basx.dev>
parents: 222
diff changeset
218 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
219 assert scene in renderer.scenedata
228
6b02e108ba54 add: small refactoring
Sam <sam@basx.dev>
parents: 222
diff changeset
220
238
ef8cea2545f8 did: make material working halfway, add some more mouse functionality
Sam <sam@basx.dev>
parents: 237
diff changeset
221 if renderer.scenedata[scene].uniformBuffers.len == 0:
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
222 return
238
ef8cea2545f8 did: make material working halfway, add some more mouse functionality
Sam <sam@basx.dev>
parents: 237
diff changeset
223 assert renderer.scenedata[scene].uniformBuffers[renderer.swapchain.currentInFlight].vk.valid
142
9c0d7839dc91 add: correct mesh buffer data updates to GPU
Sam <sam@basx.dev>
parents: 140
diff changeset
224
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
225 for i in 0 ..< renderer.renderPass.subpasses.len:
228
6b02e108ba54 add: small refactoring
Sam <sam@basx.dev>
parents: 222
diff changeset
226 for pipeline in renderer.renderPass.subpasses[i].pipelines.mitems:
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
227 var offset = 0'u64
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
228 for uniform in pipeline.uniforms:
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
229 assert uniform.thetype == scene.shaderGlobals[uniform.name].thetype
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
230 let (pdata, size) = scene.shaderGlobals[uniform.name].getRawData()
238
ef8cea2545f8 did: make material working halfway, add some more mouse functionality
Sam <sam@basx.dev>
parents: 237
diff changeset
231 renderer.scenedata[scene].uniformBuffers[renderer.swapchain.currentInFlight].setData(pdata, size, offset)
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
232 offset += size
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
233
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
234 proc render*(renderer: var Renderer, scene: var Scene) =
142
9c0d7839dc91 add: correct mesh buffer data updates to GPU
Sam <sam@basx.dev>
parents: 140
diff changeset
235 assert scene in renderer.scenedata
9c0d7839dc91 add: correct mesh buffer data updates to GPU
Sam <sam@basx.dev>
parents: 140
diff changeset
236
131
11666d28e04d add: recreation of swapchain (at least on linux, windows will likely fail, needs testing
Sam <sam@basx.dev>
parents: 129
diff changeset
237 var
11666d28e04d add: recreation of swapchain (at least on linux, windows will likely fail, needs testing
Sam <sam@basx.dev>
parents: 129
diff changeset
238 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
239 commandBuffer: VkCommandBuffer
128
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
240
131
11666d28e04d add: recreation of swapchain (at least on linux, windows will likely fail, needs testing
Sam <sam@basx.dev>
parents: 129
diff changeset
241 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
242 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
243 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
244 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
245 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
246 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
247 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
248 return
228
6b02e108ba54 add: small refactoring
Sam <sam@basx.dev>
parents: 222
diff changeset
249
131
11666d28e04d add: recreation of swapchain (at least on linux, windows will likely fail, needs testing
Sam <sam@basx.dev>
parents: 129
diff changeset
250 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
251 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
252
11666d28e04d add: recreation of swapchain (at least on linux, windows will likely fail, needs testing
Sam <sam@basx.dev>
parents: 129
diff changeset
253 for i in 0 ..< renderer.renderPass.subpasses.len:
228
6b02e108ba54 add: small refactoring
Sam <sam@basx.dev>
parents: 222
diff changeset
254 var subpass = renderer.renderPass.subpasses[i]
6b02e108ba54 add: small refactoring
Sam <sam@basx.dev>
parents: 222
diff changeset
255 for pipeline in subpass.pipelines.mitems:
6b02e108ba54 add: small refactoring
Sam <sam@basx.dev>
parents: 222
diff changeset
256 commandBuffer.vkCmdBindPipeline(subpass.pipelineBindPoint, pipeline.vk)
6b02e108ba54 add: small refactoring
Sam <sam@basx.dev>
parents: 222
diff changeset
257 commandBuffer.vkCmdBindDescriptorSets(subpass.pipelineBindPoint, pipeline.layout, 0, 1, addr(pipeline.descriptorSets[renderer.swapchain.currentInFlight].vk), 0, nil)
128
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
258
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
259 debug "Scene buffers:"
142
9c0d7839dc91 add: correct mesh buffer data updates to GPU
Sam <sam@basx.dev>
parents: 140
diff changeset
260 for (location, buffer) in renderer.scenedata[scene].vertexBuffers.pairs:
131
11666d28e04d add: recreation of swapchain (at least on linux, windows will likely fail, needs testing
Sam <sam@basx.dev>
parents: 129
diff changeset
261 debug " ", location, ": ", buffer
142
9c0d7839dc91 add: correct mesh buffer data updates to GPU
Sam <sam@basx.dev>
parents: 140
diff changeset
262 debug " Index buffer: ", renderer.scenedata[scene].indexBuffer
128
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
263
142
9c0d7839dc91 add: correct mesh buffer data updates to GPU
Sam <sam@basx.dev>
parents: 140
diff changeset
264 for drawable in renderer.scenedata[scene].drawables.values:
9c0d7839dc91 add: correct mesh buffer data updates to GPU
Sam <sam@basx.dev>
parents: 140
diff changeset
265 commandBuffer.draw(drawable, 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
266
131
11666d28e04d add: recreation of swapchain (at least on linux, windows will likely fail, needs testing
Sam <sam@basx.dev>
parents: 129
diff changeset
267 if i < renderer.renderPass.subpasses.len - 1:
128
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
268 commandBuffer.vkCmdNextSubpass(VK_SUBPASS_CONTENTS_INLINE)
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
269
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
270 commandBuffer.endRenderCommands()
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
271
131
11666d28e04d add: recreation of swapchain (at least on linux, windows will likely fail, needs testing
Sam <sam@basx.dev>
parents: 129
diff changeset
272 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
273 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
274 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
275 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
276 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
277 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
278 oldSwapchain.destroy()
132
25fe4972ef9c add: support for smooth swapchain-recreation
Sam <sam@basx.dev>
parents: 131
diff changeset
279
128
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
280 func framesRendered*(renderer: Renderer): uint64 =
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
281 renderer.swapchain.framesRendered
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
282
140
29f0109c5222 add: some helper functions
Sam <sam@basx.dev>
parents: 138
diff changeset
283 func valid*(renderer: Renderer): bool =
29f0109c5222 add: some helper functions
Sam <sam@basx.dev>
parents: 138
diff changeset
284 renderer.device.vk.valid
29f0109c5222 add: some helper functions
Sam <sam@basx.dev>
parents: 138
diff changeset
285
128
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
286 proc destroy*(renderer: var Renderer) =
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
287 for data in renderer.scenedata.mvalues:
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
288 for buffer in data.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
289 assert buffer.vk.valid
128
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
290 buffer.destroy()
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
291 if data.indexBuffer.vk.valid:
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
292 assert data.indexBuffer.vk.valid
128
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
293 data.indexBuffer.destroy()
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
294 for buffer in data.uniformBuffers.mitems:
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
295 assert buffer.vk.valid
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
296 buffer.destroy()
201
ab626e67a1ee add: support for arrays of samplers
Sam <sam@basx.dev>
parents: 200
diff changeset
297 for textures in data.textures.mvalues:
ab626e67a1ee add: support for arrays of samplers
Sam <sam@basx.dev>
parents: 200
diff changeset
298 for texture in textures.mitems:
ab626e67a1ee add: support for arrays of samplers
Sam <sam@basx.dev>
parents: 200
diff changeset
299 texture.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
300 renderer.renderPass.destroy()
128
9901ec3831d1 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 127
diff changeset
301 renderer.swapchain.destroy()