annotate semicongine/renderer.nim @ 949:0cf68e7336e8

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