Mercurial > games > semicongine
annotate static_utils.nim @ 1164:7b4d4d85d9f5 compiletime-tests
add: descriptor sets allocation
| author | sam <sam@basx.dev> |
|---|---|
| date | Sat, 22 Jun 2024 02:26:16 +0700 |
| parents | 438d32d8b14f |
| children | 58694b30b9cb |
| rev | line source |
|---|---|
| 1162 | 1 import std/os |
|
1163
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
2 import std/enumerate |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
3 import std/hashes |
| 1159 | 4 import std/macros |
| 1161 | 5 import std/strformat |
|
1163
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
6 import std/strutils |
| 1164 | 7 import std/sequtils |
| 1162 | 8 import std/typetraits as tt |
| 1159 | 9 |
| 1161 | 10 import semicongine/core/utils |
| 11 import semicongine/core/imagetypes | |
| 1159 | 12 import semicongine/core/vector |
| 13 import semicongine/core/matrix | |
| 14 import semicongine/core/vulkanapi | |
| 1161 | 15 import semicongine/vulkan/buffer |
| 1159 | 16 |
| 17 template VertexAttribute* {.pragma.} | |
| 18 template InstanceAttribute* {.pragma.} | |
| 1162 | 19 template Pass* {.pragma.} |
| 20 template PassFlat* {.pragma.} | |
| 21 template ShaderOutput* {.pragma.} | |
| 1159 | 22 |
|
1163
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
23 const INFLIGHTFRAMES = 2 |
| 1159 | 24 type |
| 25 SupportedGPUType* = float32 | float64 | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | TVec2[int32] | TVec2[int64] | TVec3[int32] | TVec3[int64] | TVec4[int32] | TVec4[int64] | TVec2[uint32] | TVec2[uint64] | TVec3[uint32] | TVec3[uint64] | TVec4[uint32] | TVec4[uint64] | TVec2[float32] | TVec2[float64] | TVec3[float32] | TVec3[float64] | TVec4[float32] | TVec4[float64] | TMat2[float32] | TMat2[float64] | TMat23[float32] | TMat23[float64] | TMat32[float32] | TMat32[float64] | TMat3[float32] | TMat3[float64] | TMat34[float32] | TMat34[float64] | TMat43[float32] | TMat43[float64] | TMat4[float32] | TMat4[float64] | |
|
1163
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
26 ShaderObject*[TShader] = object |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
27 vertexShader: VkShaderModule |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
28 fragmentShader: VkShaderModule |
| 1159 | 29 |
| 30 func VkType[T: SupportedGPUType](value: T): VkFormat = | |
| 31 when T is float32: VK_FORMAT_R32_SFLOAT | |
| 32 elif T is float64: VK_FORMAT_R64_SFLOAT | |
| 33 elif T is int8: VK_FORMAT_R8_SINT | |
| 34 elif T is int16: VK_FORMAT_R16_SINT | |
| 35 elif T is int32: VK_FORMAT_R32_SINT | |
| 36 elif T is int64: VK_FORMAT_R64_SINT | |
| 37 elif T is uint8: VK_FORMAT_R8_UINT | |
| 38 elif T is uint16: VK_FORMAT_R16_UINT | |
| 39 elif T is uint32: VK_FORMAT_R32_UINT | |
| 40 elif T is uint64: VK_FORMAT_R64_UINT | |
| 41 elif T is TVec2[int32]: VK_FORMAT_R32G32_SINT | |
| 42 elif T is TVec2[int64]: VK_FORMAT_R64G64_SINT | |
| 43 elif T is TVec3[int32]: VK_FORMAT_R32G32B32_SINT | |
| 44 elif T is TVec3[int64]: VK_FORMAT_R64G64B64_SINT | |
| 45 elif T is TVec4[int32]: VK_FORMAT_R32G32B32A32_SINT | |
| 46 elif T is TVec4[int64]: VK_FORMAT_R64G64B64A64_SINT | |
| 47 elif T is TVec2[uint32]: VK_FORMAT_R32G32_UINT | |
| 48 elif T is TVec2[uint64]: VK_FORMAT_R64G64_UINT | |
| 49 elif T is TVec3[uint32]: VK_FORMAT_R32G32B32_UINT | |
| 50 elif T is TVec3[uint64]: VK_FORMAT_R64G64B64_UINT | |
| 51 elif T is TVec4[uint32]: VK_FORMAT_R32G32B32A32_UINT | |
| 52 elif T is TVec4[uint64]: VK_FORMAT_R64G64B64A64_UINT | |
| 53 elif T is TVec2[float32]: VK_FORMAT_R32G32_SFLOAT | |
| 54 elif T is TVec2[float64]: VK_FORMAT_R64G64_SFLOAT | |
| 55 elif T is TVec3[float32]: VK_FORMAT_R32G32B32_SFLOAT | |
| 56 elif T is TVec3[float64]: VK_FORMAT_R64G64B64_SFLOAT | |
| 57 elif T is TVec4[float32]: VK_FORMAT_R32G32B32A32_SFLOAT | |
| 58 elif T is TVec4[float64]: VK_FORMAT_R64G64B64A64_SFLOAT | |
| 1162 | 59 elif T is TMat2[float32]: VK_FORMAT_R32G32_SFLOAT |
| 60 elif T is TMat2[float64]: VK_FORMAT_R64G64_SFLOAT | |
| 61 elif T is TMat23[float32]: VK_FORMAT_R32G32B32_SFLOAT | |
| 62 elif T is TMat23[float64]: VK_FORMAT_R64G64B64_SFLOAT | |
| 63 elif T is TMat32[float32]: VK_FORMAT_R32G32_SFLOAT | |
| 64 elif T is TMat32[float64]: VK_FORMAT_R64G64_SFLOAT | |
| 65 elif T is TMat3[float32]: VK_FORMAT_R32G32B32_SFLOAT | |
| 66 elif T is TMat3[float64]: VK_FORMAT_R64G64B64_SFLOAT | |
| 67 elif T is TMat34[float32]: VK_FORMAT_R32G32B32A32_SFLOAT | |
| 68 elif T is TMat34[float64]: VK_FORMAT_R64G64B64A64_SFLOAT | |
| 69 elif T is TMat43[float32]: VK_FORMAT_R32G32B32_SFLOAT | |
| 70 elif T is TMat43[float64]: VK_FORMAT_R64G64B64_SFLOAT | |
| 71 elif T is TMat4[float32]: VK_FORMAT_R32G32B32A32_SFLOAT | |
| 72 elif T is TMat4[float64]: VK_FORMAT_R64G64B64A64_SFLOAT | |
| 73 else: {.error: "Unsupported data type on GPU".} | |
| 74 | |
|
1163
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
75 func GlslType[T: SupportedGPUType|Texture](value: T): string = |
| 1162 | 76 when T is float32: "float" |
| 77 elif T is float64: "double" | |
|
1163
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
78 elif T is int8 or T is int16 or T is int32 or T is int64: "int" |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
79 elif T is uint8 or T is uint16 or T is uint32 or T is uint64: "uint" |
| 1162 | 80 elif T is TVec2[int32]: "ivec2" |
| 81 elif T is TVec2[int64]: "ivec2" | |
| 82 elif T is TVec3[int32]: "ivec3" | |
| 83 elif T is TVec3[int64]: "ivec3" | |
| 84 elif T is TVec4[int32]: "ivec4" | |
| 85 elif T is TVec4[int64]: "ivec4" | |
| 86 elif T is TVec2[uint32]: "uvec2" | |
| 87 elif T is TVec2[uint64]: "uvec2" | |
| 88 elif T is TVec3[uint32]: "uvec3" | |
| 89 elif T is TVec3[uint64]: "uvec3" | |
| 90 elif T is TVec4[uint32]: "uvec4" | |
| 91 elif T is TVec4[uint64]: "uvec4" | |
| 92 elif T is TVec2[float32]: "vec2" | |
| 93 elif T is TVec2[float64]: "dvec2" | |
| 94 elif T is TVec3[float32]: "vec3" | |
| 95 elif T is TVec3[float64]: "dvec3" | |
| 96 elif T is TVec4[float32]: "vec4" | |
| 97 elif T is TVec4[float64]: "dvec4" | |
| 98 elif T is TMat2[float32]: "mat2" | |
| 99 elif T is TMat2[float64]: "dmat2" | |
|
1163
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
100 elif T is TMat23[float32]: "mat23" |
| 1162 | 101 elif T is TMat23[float64]: "dmat23" |
| 102 elif T is TMat32[float32]: "mat32" | |
| 103 elif T is TMat32[float64]: "dmat32" | |
| 104 elif T is TMat3[float32]: "mat3" | |
| 105 elif T is TMat3[float64]: "dmat3" | |
| 106 elif T is TMat34[float32]: "mat34" | |
| 107 elif T is TMat34[float64]: "dmat34" | |
| 108 elif T is TMat43[float32]: "mat43" | |
| 109 elif T is TMat43[float64]: "dmat43" | |
| 110 elif T is TMat4[float32]: "mat4" | |
| 111 elif T is TMat4[float64]: "dmat4" | |
| 112 elif T is Texture: "sampler2D" | |
| 1159 | 113 else: {.error: "Unsupported data type on GPU".} |
| 114 | |
| 1161 | 115 template ForVertexDataFields*(inputData: typed, fieldname, valuename, isinstancename, body: untyped): untyped = |
| 1159 | 116 for theFieldname, value in fieldPairs(inputData): |
| 1161 | 117 when hasCustomPragma(value, VertexAttribute) or hasCustomPragma(value, InstanceAttribute): |
| 1159 | 118 when not typeof(value) is seq: |
| 119 {.error: "field '" & theFieldname & "' needs to be a seq".} | |
| 120 when not typeof(value) is SupportedGPUType: | |
| 121 {.error: "field '" & theFieldname & "' is not a supported GPU type".} | |
| 122 block: | |
| 123 let `fieldname` {.inject.} = theFieldname | |
| 1162 | 124 let `valuename` {.inject.} = value |
| 125 let `isinstancename` {.inject.} = hasCustomPragma(value, InstanceAttribute) | |
| 1159 | 126 body |
| 127 | |
|
1163
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
128 template ForDescriptorFields*(inputData: typed, typename, countname, body: untyped): untyped = |
| 1161 | 129 for theFieldname, value in fieldPairs(inputData): |
|
1163
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
130 when typeof(value) is Texture: |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
131 block: |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
132 let `typename` {.inject.} = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
133 let `countname` {.inject.} = 1'u32 |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
134 body |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
135 elif typeof(value) is object: |
| 1161 | 136 block: |
|
1163
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
137 let `typename` {.inject.} = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
138 let `countname` {.inject.} = 1'u32 |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
139 body |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
140 elif typeof(value) is array: |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
141 when elementType(value) is Texture: |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
142 block: |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
143 let `typename` {.inject.} = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
144 let `countname` {.inject.} = uint32(typeof(value).len) |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
145 body |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
146 elif elementType(value) is object: |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
147 block: |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
148 let `typename` {.inject.} = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
149 let `countname` {.inject.} = uint32(typeof(value).len) |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
150 body |
| 1161 | 151 |
|
1163
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
152 func NumberOfVertexInputAttributeDescriptors[T: SupportedGPUType|Texture](value: T): uint32 = |
| 1159 | 153 when T is TMat2[float32] or T is TMat2[float64] or T is TMat23[float32] or T is TMat23[float64]: |
| 154 2 | |
| 155 elif T is TMat32[float32] or T is TMat32[float64] or T is TMat3[float32] or T is TMat3[float64] or T is TMat34[float32] or T is TMat34[float64]: | |
| 156 3 | |
| 157 elif T is TMat43[float32] or T is TMat43[float64] or T is TMat4[float32] or T is TMat4[float64]: | |
| 158 4 | |
| 159 else: | |
| 160 1 | |
| 161 | |
| 1162 | 162 func NLocationSlots[T: SupportedGPUType|Texture](value: T): uint32 = |
| 1159 | 163 #[ |
| 164 single location: | |
| 1162 | 165 - any scalar |
| 166 - any 16-bit vector | |
| 167 - any 32-bit vector | |
| 168 - any 64-bit vector that has max. 2 components | |
| 1159 | 169 16-bit scalar and vector types, and |
| 170 32-bit scalar and vector types, and | |
| 171 64-bit scalar and 2-component vector types. | |
| 172 two locations | |
| 173 64-bit three- and four-component vectors | |
| 174 ]# | |
| 1162 | 175 when T is TVec3[int64] or |
| 176 T is TVec4[int64] or | |
| 177 T is TVec3[uint64] or | |
| 178 T is TVec4[uint64] or | |
| 179 T is TVec3[float64] or | |
| 180 T is TVec4[float64] or | |
| 181 T is TMat23[float64] or | |
| 182 T is TMat3[float64] or | |
| 183 T is TMat34[float64] or | |
| 184 T is TMat43[float64] or | |
| 185 T is TMat4[float64]: | |
| 1159 | 186 return 2 |
| 187 else: | |
| 188 return 1 | |
| 189 | |
| 190 type | |
| 1161 | 191 IndexType = enum |
| 192 None, UInt8, UInt16, UInt32 | |
| 193 RenderBuffers = object | |
| 194 deviceBuffers: seq[Buffer] # for fast reads | |
| 195 hostVisibleBuffers: seq[Buffer] # for fast writes | |
| 196 Renderable[TMesh, TInstance] = object | |
| 197 vertexBuffers: seq[VkBuffer] | |
| 198 bufferOffsets: seq[VkDeviceSize] | |
| 1159 | 199 instanceCount: uint32 |
| 1161 | 200 case indexType: IndexType |
| 201 of None: | |
| 1160 | 202 vertexCount: uint32 |
| 1161 | 203 else: |
| 1160 | 204 indexBuffer: VkBuffer |
| 205 indexCount: uint32 | |
| 206 indexBufferOffset: VkDeviceSize | |
| 1162 | 207 Pipeline[TShader] = object |
| 1159 | 208 pipeline: VkPipeline |
| 209 layout: VkPipelineLayout | |
| 1164 | 210 descriptorSets: array[INFLIGHTFRAMES, VkDescriptorSet] |
| 1162 | 211 |
| 1161 | 212 converter toVkIndexType(indexType: IndexType): VkIndexType = |
| 213 case indexType: | |
| 214 of None: VK_INDEX_TYPE_NONE_KHR | |
| 215 of UInt8: VK_INDEX_TYPE_UINT8_EXT | |
| 216 of UInt16: VK_INDEX_TYPE_UINT16 | |
| 217 of UInt32: VK_INDEX_TYPE_UINT32 | |
| 1159 | 218 |
| 1162 | 219 proc compileGlslToSPIRV(stage: VkShaderStageFlagBits, shaderSource: string): seq[uint32] {.compileTime.} = |
| 220 func stage2string(stage: VkShaderStageFlagBits): string {.compileTime.} = | |
| 221 case stage | |
| 222 of VK_SHADER_STAGE_VERTEX_BIT: "vert" | |
| 223 of VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT: "tesc" | |
| 224 of VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT: "tese" | |
| 225 of VK_SHADER_STAGE_GEOMETRY_BIT: "geom" | |
| 226 of VK_SHADER_STAGE_FRAGMENT_BIT: "frag" | |
| 227 of VK_SHADER_STAGE_COMPUTE_BIT: "comp" | |
| 228 else: "" | |
| 1161 | 229 |
| 1162 | 230 when defined(nimcheck): # will not run if nimcheck is running |
| 231 return result | |
| 232 | |
| 233 let | |
| 234 stagename = stage2string(stage) | |
| 235 shaderHash = hash(shaderSource) | |
| 236 shaderfile = getTempDir() / &"shader_{shaderHash}.{stagename}" | |
| 237 | |
| 238 if not shaderfile.fileExists: | |
|
1163
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
239 echo "shader of type ", stage |
| 1162 | 240 for i, line in enumerate(shaderSource.splitlines()): |
| 241 echo " ", i + 1, " ", line | |
|
1163
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
242 # var glslExe = currentSourcePath.parentDir.parentDir.parentDir / "tools" / "glslangValidator" |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
243 var glslExe = currentSourcePath.parentDir / "tools" / "glslangValidator" |
| 1162 | 244 when defined(windows): |
| 245 glslExe = glslExe & "." & ExeExt | |
|
1163
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
246 let command = &"{glslExe} --entry-point main -V --stdin -S {stagename} -o {shaderfile}" |
| 1162 | 247 echo "run: ", command |
| 248 discard StaticExecChecked( | |
| 249 command = command, | |
| 250 input = shaderSource | |
| 251 ) | |
| 252 else: | |
| 253 echo &"shaderfile {shaderfile} is up-to-date" | |
| 254 | |
| 255 when defined(mingw) and defined(linux): # required for crosscompilation, path separators get messed up | |
| 256 let shaderbinary = staticRead shaderfile.replace("\\", "/") | |
| 257 else: | |
| 258 let shaderbinary = staticRead shaderfile | |
| 259 | |
| 260 var i = 0 | |
| 261 while i < shaderbinary.len: | |
| 262 result.add( | |
| 263 (uint32(shaderbinary[i + 0]) shl 0) or | |
| 264 (uint32(shaderbinary[i + 1]) shl 8) or | |
| 265 (uint32(shaderbinary[i + 2]) shl 16) or | |
| 266 (uint32(shaderbinary[i + 3]) shl 24) | |
| 267 ) | |
| 268 i += 4 | |
| 269 | |
| 270 proc generateShaderSource[TShader](shader: TShader): (string, string) {.compileTime.} = | |
| 271 const GLSL_VERSION = "450" | |
| 272 var vsInput: seq[string] | |
| 273 var vsOutput: seq[string] | |
| 274 var fsInput: seq[string] | |
| 275 var fsOutput: seq[string] | |
| 276 var uniforms: seq[string] | |
| 277 var samplers: seq[string] | |
|
1163
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
278 var vsInputLocation = 0'u32 |
| 1162 | 279 var passLocation = 0 |
| 280 var fsOutputLocation = 0 | |
|
1163
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
281 var descriptorBinding = 0 |
| 1162 | 282 |
| 283 for fieldname, value in fieldPairs(shader): | |
| 284 # vertex shader inputs | |
|
1163
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
285 when hasCustomPragma(value, VertexAttribute) or hasCustomPragma(value, InstanceAttribute): |
| 1162 | 286 assert typeof(value) is SupportedGPUType |
|
1163
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
287 vsInput.add "layout(location = " & $vsInputLocation & ") in " & GlslType(value) & " " & fieldname & ";" |
| 1162 | 288 for j in 0 ..< NumberOfVertexInputAttributeDescriptors(value): |
| 289 vsInputLocation += NLocationSlots(value) | |
| 290 # intermediate values, passed between shaders | |
|
1163
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
291 elif hasCustomPragma(value, Pass) or hasCustomPragma(value, PassFlat): |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
292 let flat = if hasCustomPragma(value, PassFlat): "flat " else: "" |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
293 vsOutput.add "layout(location = " & $passLocation & ") " & flat & "out " & GlslType(value) & " " & fieldname & ";" |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
294 fsInput.add "layout(location = " & $passLocation & ") " & flat & "in " & GlslType(value) & " " & fieldname & ";" |
| 1162 | 295 passLocation.inc |
|
1163
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
296 elif hasCustomPragma(value, ShaderOutput): |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
297 fsOutput.add &"layout(location = " & $fsOutputLocation & ") out " & GlslType(value) & " " & fieldname & ";" |
| 1162 | 298 fsOutputLocation.inc |
|
1163
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
299 elif typeof(value) is Texture: |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
300 samplers.add "layout(binding = " & $descriptorBinding & ") uniform " & GlslType(value) & " " & fieldname & ";" |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
301 descriptorBinding.inc |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
302 elif typeof(value) is object: |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
303 # TODO |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
304 uniforms.add "" |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
305 descriptorBinding.inc |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
306 elif typeof(value) is array: |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
307 when elementType(value) is Texture: |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
308 let arrayDecl = "[" & $typeof(value).len & "]" |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
309 samplers.add "layout(binding = " & $descriptorBinding & ") uniform " & GlslType(default(elementType(value))) & " " & fieldname & "" & arrayDecl & ";" |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
310 descriptorBinding.inc |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
311 elif elementType(value) is object: |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
312 # TODO |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
313 let arrayDecl = "[" & $typeof(value).len & "]" |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
314 # uniforms.add "layout(binding = " & $descriptorBinding & ") uniform " & GlslType(elementType(value)) & " " & fieldname & "" & arrayDecl & ";" |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
315 descriptorBinding.inc |
| 1162 | 316 else: |
|
1163
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
317 {.error: "Unsupported shader field " & fieldname.} |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
318 elif fieldname in ["vertexCode", "fragmentCode"]: |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
319 discard |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
320 else: |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
321 {.error: "Unsupported shader field '" & tt.name(TShader) & "." & fieldname & "' of type " & tt.name(typeof(value)).} |
| 1162 | 322 |
| 323 result[0] = (@[&"#version {GLSL_VERSION}", "#extension GL_EXT_scalar_block_layout : require", ""] & | |
| 324 vsInput & | |
| 325 uniforms & | |
| 326 samplers & | |
| 327 vsOutput & | |
| 328 @[shader.vertexCode]).join("\n") | |
| 329 | |
| 330 result[1] = (@[&"#version {GLSL_VERSION}", "#extension GL_EXT_scalar_block_layout : require", ""] & | |
| 331 fsInput & | |
| 332 uniforms & | |
| 333 samplers & | |
| 334 fsOutput & | |
| 335 @[shader.fragmentCode]).join("\n") | |
| 336 | |
|
1163
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
337 # proc CompileShader[TShader](shader: static TShader): (seq[uint32], seq[uint32]) {.compileTime.}= |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
338 proc CompileShader[TShader](device: VkDevice, shader: static TShader): ShaderObject[TShader] = |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
339 const (vertexShaderSource, fragmentShaderSource) = generateShaderSource(shader) |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
340 |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
341 let vertexBinary = compileGlslToSPIRV(VK_SHADER_STAGE_VERTEX_BIT, vertexShaderSource) |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
342 let fragmentBinary = compileGlslToSPIRV(VK_SHADER_STAGE_FRAGMENT_BIT, fragmentShaderSource) |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
343 |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
344 var createInfoVertex = VkShaderModuleCreateInfo( |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
345 sType: VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO, |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
346 codeSize: csize_t(vertexBinary.len * sizeof(uint32)), |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
347 pCode: vertexBinary.ToCPointer, |
| 1162 | 348 ) |
|
1163
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
349 checkVkResult device.vkCreateShaderModule(addr(createInfoVertex), nil, addr(result.vertexShader)) |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
350 var createInfoFragment = VkShaderModuleCreateInfo( |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
351 sType: VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO, |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
352 codeSize: csize_t(fragmentBinary.len * sizeof(uint32)), |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
353 pCode: fragmentBinary.ToCPointer, |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
354 ) |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
355 checkVkResult device.vkCreateShaderModule(addr(createInfoFragment), nil, addr(result.fragmentShader)) |
| 1162 | 356 |
| 357 | |
| 358 proc CreatePipeline*[TShader]( | |
| 1159 | 359 device: VkDevice, |
| 360 renderPass: VkRenderPass, | |
|
1163
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
361 shader: ShaderObject[TShader], |
| 1159 | 362 topology: VkPrimitiveTopology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST, |
| 363 polygonMode: VkPolygonMode = VK_POLYGON_MODE_FILL, | |
| 364 cullMode: VkCullModeFlagBits = VK_CULL_MODE_BACK_BIT, | |
| 365 frontFace: VkFrontFace = VK_FRONT_FACE_CLOCKWISE, | |
| 1162 | 366 ): Pipeline[TShader] = |
| 1159 | 367 # assumptions/limitations: |
| 368 # - we are only using vertex and fragment shaders (2 stages) | |
| 369 # - we only support one subpass | |
| 1162 | 370 # = we only support one Uniform-Block |
| 1161 | 371 |
| 1164 | 372 # create pipeline |
| 1161 | 373 var layoutbindings: seq[VkDescriptorSetLayoutBinding] |
| 374 var descriptorBindingNumber = 0'u32 | |
|
1163
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
375 ForDescriptorFields(default(TShader), descriptorType, descriptorCount): |
| 1161 | 376 layoutbindings.add VkDescriptorSetLayoutBinding( |
| 377 binding: descriptorBindingNumber, | |
| 378 descriptorType: descriptorType, | |
| 379 descriptorCount: descriptorCount, | |
| 1162 | 380 stageFlags: VkShaderStageFlags(VK_SHADER_STAGE_ALL_GRAPHICS), |
| 1161 | 381 pImmutableSamplers: nil, |
| 382 ) | |
| 383 inc descriptorBindingNumber | |
| 384 var layoutCreateInfo = VkDescriptorSetLayoutCreateInfo( | |
| 385 sType: VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO, | |
| 386 bindingCount: uint32(layoutbindings.len), | |
| 387 pBindings: layoutbindings.ToCPointer | |
| 388 ) | |
| 389 var descriptorSetLayout: VkDescriptorSetLayout | |
| 1162 | 390 checkVkResult vkCreateDescriptorSetLayout(device, addr(layoutCreateInfo), nil, addr(descriptorSetLayout)) |
| 1161 | 391 let pipelineLayoutInfo = VkPipelineLayoutCreateInfo( |
| 392 sType: VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO, | |
| 393 setLayoutCount: 1, | |
| 394 pSetLayouts: addr(descriptorSetLayout), | |
| 395 # pushConstantRangeCount: uint32(pushConstants.len), | |
| 396 # pPushConstantRanges: pushConstants.ToCPointer, | |
| 397 ) | |
| 398 checkVkResult vkCreatePipelineLayout(device, addr(pipelineLayoutInfo), nil, addr(result.layout)) | |
| 1159 | 399 |
| 400 let stages = [ | |
| 401 VkPipelineShaderStageCreateInfo( | |
| 402 sType: VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, | |
| 403 stage: VK_SHADER_STAGE_VERTEX_BIT, | |
|
1163
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
404 module: shader.vertexShader, |
| 1159 | 405 pName: "main", |
| 406 ), | |
| 407 VkPipelineShaderStageCreateInfo( | |
| 408 sType: VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, | |
| 409 stage: VK_SHADER_STAGE_FRAGMENT_BIT, | |
|
1163
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
410 module: shader.fragmentShader, |
| 1159 | 411 pName: "main", |
| 412 ), | |
| 413 ] | |
| 1162 | 414 var |
| 415 bindings: seq[VkVertexInputBindingDescription] | |
| 416 attributes: seq[VkVertexInputAttributeDescription] | |
| 1159 | 417 var inputBindingNumber = 0'u32 |
| 1162 | 418 var location = 0'u32 |
| 419 ForVertexDataFields(default(TShader), fieldname, value, isInstanceAttr): | |
| 1159 | 420 bindings.add VkVertexInputBindingDescription( |
| 421 binding: inputBindingNumber, | |
| 422 stride: sizeof(value).uint32, | |
| 423 inputRate: if isInstanceAttr: VK_VERTEX_INPUT_RATE_INSTANCE else: VK_VERTEX_INPUT_RATE_VERTEX, | |
| 424 ) | |
| 425 # allows to submit larger data structures like Mat44, for most other types will be 1 | |
| 426 let perDescriptorSize = sizeof(value).uint32 div NumberOfVertexInputAttributeDescriptors(value) | |
| 427 for i in 0'u32 ..< NumberOfVertexInputAttributeDescriptors(value): | |
| 428 attributes.add VkVertexInputAttributeDescription( | |
| 429 binding: inputBindingNumber, | |
| 1162 | 430 location: location, |
| 1159 | 431 format: VkType(value), |
| 432 offset: i * perDescriptorSize, | |
| 433 ) | |
| 1162 | 434 location += NLocationSlots(value) |
| 1159 | 435 inc inputBindingNumber |
| 436 | |
| 437 let | |
| 438 vertexInputInfo = VkPipelineVertexInputStateCreateInfo( | |
| 439 sType: VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO, | |
| 440 vertexBindingDescriptionCount: uint32(bindings.len), | |
| 441 pVertexBindingDescriptions: bindings.ToCPointer, | |
| 442 vertexAttributeDescriptionCount: uint32(attributes.len), | |
| 443 pVertexAttributeDescriptions: attributes.ToCPointer, | |
| 444 ) | |
| 445 inputAssembly = VkPipelineInputAssemblyStateCreateInfo( | |
| 446 sType: VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO, | |
| 447 topology: topology, | |
| 448 primitiveRestartEnable: false, | |
| 449 ) | |
| 450 viewportState = VkPipelineViewportStateCreateInfo( | |
| 451 sType: VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO, | |
| 452 viewportCount: 1, | |
| 453 scissorCount: 1, | |
| 454 ) | |
| 455 rasterizer = VkPipelineRasterizationStateCreateInfo( | |
| 456 sType: VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO, | |
| 457 depthClampEnable: VK_FALSE, | |
| 458 rasterizerDiscardEnable: VK_FALSE, | |
| 459 polygonMode: polygonMode, | |
| 460 lineWidth: 1.0, | |
| 461 cullMode: toBits [cullMode], | |
| 462 frontFace: frontFace, | |
| 463 depthBiasEnable: VK_FALSE, | |
| 464 depthBiasConstantFactor: 0.0, | |
| 465 depthBiasClamp: 0.0, | |
| 466 depthBiasSlopeFactor: 0.0, | |
| 467 ) | |
| 468 multisampling = VkPipelineMultisampleStateCreateInfo( | |
| 469 sType: VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO, | |
| 470 sampleShadingEnable: VK_FALSE, | |
| 471 rasterizationSamples: VK_SAMPLE_COUNT_1_BIT, | |
| 472 minSampleShading: 1.0, | |
| 473 pSampleMask: nil, | |
| 474 alphaToCoverageEnable: VK_FALSE, | |
| 475 alphaToOneEnable: VK_FALSE, | |
| 476 ) | |
| 477 colorBlendAttachment = VkPipelineColorBlendAttachmentState( | |
| 478 colorWriteMask: toBits [VK_COLOR_COMPONENT_R_BIT, VK_COLOR_COMPONENT_G_BIT, VK_COLOR_COMPONENT_B_BIT, VK_COLOR_COMPONENT_A_BIT], | |
| 479 blendEnable: VK_TRUE, | |
| 480 srcColorBlendFactor: VK_BLEND_FACTOR_SRC_ALPHA, | |
| 481 dstColorBlendFactor: VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA, | |
| 482 colorBlendOp: VK_BLEND_OP_ADD, | |
| 483 srcAlphaBlendFactor: VK_BLEND_FACTOR_ONE, | |
| 484 dstAlphaBlendFactor: VK_BLEND_FACTOR_ZERO, | |
| 485 alphaBlendOp: VK_BLEND_OP_ADD, | |
| 486 ) | |
| 487 colorBlending = VkPipelineColorBlendStateCreateInfo( | |
| 488 sType: VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO, | |
| 489 logicOpEnable: false, | |
| 490 attachmentCount: 1, | |
| 491 pAttachments: addr(colorBlendAttachment), | |
| 492 ) | |
| 493 dynamicStates = [VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR] | |
| 494 dynamicState = VkPipelineDynamicStateCreateInfo( | |
| 495 sType: VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO, | |
| 496 dynamicStateCount: dynamicStates.len.uint32, | |
| 497 pDynamicStates: dynamicStates.ToCPointer, | |
| 498 ) | |
| 499 let createInfo = VkGraphicsPipelineCreateInfo( | |
| 500 sType: VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO, | |
| 501 stageCount: 2, | |
| 1162 | 502 pStages: stages.ToCPointer, |
| 1159 | 503 pVertexInputState: addr(vertexInputInfo), |
| 504 pInputAssemblyState: addr(inputAssembly), | |
| 505 pViewportState: addr(viewportState), | |
| 506 pRasterizationState: addr(rasterizer), | |
| 507 pMultisampleState: addr(multisampling), | |
| 508 pDepthStencilState: nil, | |
| 509 pColorBlendState: addr(colorBlending), | |
| 510 pDynamicState: addr(dynamicState), | |
| 511 layout: result.layout, | |
| 512 renderPass: renderPass, | |
| 513 subpass: 0, | |
| 514 basePipelineHandle: VkPipeline(0), | |
| 515 basePipelineIndex: -1, | |
| 516 ) | |
| 517 checkVkResult vkCreateGraphicsPipelines( | |
| 518 device, | |
| 519 VkPipelineCache(0), | |
| 520 1, | |
| 521 addr(createInfo), | |
| 522 nil, | |
| 523 addr(result.pipeline) | |
| 524 ) | |
| 525 | |
| 1164 | 526 # create descriptors, one per frame-in-flight |
| 527 let nSamplers = 0'u32 | |
| 528 let nUniformBuffers = 0'u32 | |
| 529 | |
| 530 if nSamplers + nUniformBuffers > 0: | |
| 531 var poolSizes: seq[VkDescriptorPoolSize] | |
| 532 if nUniformBuffers > 0: | |
| 533 poolSizes.add VkDescriptorPoolSize(thetype: VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, descriptorCount: nSamplers * INFLIGHTFRAMES.uint32) | |
| 534 if nSamplers > 0: | |
| 535 poolSizes.add VkDescriptorPoolSize(thetype: VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, descriptorCount: nUniformBuffers * INFLIGHTFRAMES.uint32) | |
| 536 var poolInfo = VkDescriptorPoolCreateInfo( | |
| 537 sType: VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO, | |
| 538 poolSizeCount: uint32(poolSizes.len), | |
| 539 pPoolSizes: poolSizes.ToCPointer, | |
| 540 maxSets: (nUniformBuffers + nSamplers) * INFLIGHTFRAMES.uint32 * 2, # good formula? no idea... | |
| 541 ) | |
| 542 var pool: VkDescriptorPool | |
| 543 checkVkResult vkCreateDescriptorPool(device, addr(poolInfo), nil, addr(pool)) | |
| 544 | |
| 545 var layouts = newSeqWith(result.descriptorSets.len, descriptorSetLayout) | |
| 546 var allocInfo = VkDescriptorSetAllocateInfo( | |
| 547 sType: VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO, | |
| 548 descriptorPool: pool, | |
| 549 descriptorSetCount: uint32(layouts.len), | |
| 550 pSetLayouts: layouts.ToCPointer, | |
| 551 ) | |
| 552 checkVkResult vkAllocateDescriptorSets(device, addr(allocInfo), result.descriptorSets.ToCPointer) | |
| 553 | |
| 1161 | 554 proc CreateRenderable[TMesh, TInstance]( |
| 555 mesh: TMesh, | |
| 556 instance: TInstance, | |
| 557 buffers: RenderBuffers, | |
| 558 ): Renderable[TMesh, TInstance] = | |
| 559 result.indexType = None | |
| 1159 | 560 |
| 1161 | 561 proc Bind(pipeline: Pipeline, commandBuffer: VkCommandBuffer, currentFrameInFlight: int) = |
| 1159 | 562 commandBuffer.vkCmdBindPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline.pipeline) |
| 1164 | 563 if pipeline.descriptorSets[currentFrameInFlight] != VkDescriptorSet(0): |
| 564 commandBuffer.vkCmdBindDescriptorSets( | |
| 565 VK_PIPELINE_BIND_POINT_GRAPHICS, | |
| 566 pipeline.layout, | |
| 567 0, | |
| 568 1, | |
| 569 addr pipeline.descriptorSets[currentFrameInFlight], | |
| 570 0, | |
| 571 nil, | |
| 572 ) | |
| 1161 | 573 |
| 1162 | 574 proc AssertCompatible(TShader, TMesh, TInstance, TGlobals: typedesc) = |
| 575 # assert seq-fields of TMesh|TInstance == seq-fields of TShader | |
| 1161 | 576 # assert normal fields of TMesh|Globals == normal fields of TShaderDescriptors |
| 1162 | 577 for inputName, inputValue in default(TShader).fieldPairs: |
| 1161 | 578 var foundField = false |
| 579 when hasCustomPragma(inputValue, VertexAttribute): | |
|
1163
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
580 assert typeof(inputValue) is SupportedGPUType |
| 1161 | 581 for meshName, meshValue in default(TMesh).fieldPairs: |
| 582 when meshName == inputName: | |
| 1162 | 583 assert foundField == false, "Shader input '" & tt.name(TShader) & "." & inputName & "' has been found more than once" |
|
1163
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
584 assert elementType(meshValue) is typeof(inputValue), "Shader input " & tt.name(TShader) & "." & inputName & " is of type '" & tt.name(typeof(inputValue)) & "' but mesh attribute is of type '" & tt.name(elementType(meshValue)) & "'" |
| 1161 | 585 foundField = true |
| 1162 | 586 assert foundField, "Shader input '" & tt.name(TShader) & "." & inputName & ": " & tt.name(typeof(inputValue)) & "' not found in '" & tt.name(TMesh) & "'" |
| 1161 | 587 elif hasCustomPragma(inputValue, InstanceAttribute): |
|
1163
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
588 assert typeof(inputValue) is SupportedGPUType |
| 1161 | 589 for instanceName, instanceValue in default(TInstance).fieldPairs: |
| 590 when instanceName == inputName: | |
| 1162 | 591 assert foundField == false, "Shader input '" & tt.name(TShader) & "." & inputName & "' has been found more than once" |
|
1163
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
592 assert elementType(instanceValue) is typeof(inputValue), "Shader input " & tt.name(TShader) & "." & inputName & " is of type '" & tt.name(typeof(inputValue)) & "' but instance attribute is of type '" & tt.name(elementType(instanceValue)) & "'" |
| 1161 | 593 foundField = true |
| 1162 | 594 assert foundField, "Shader input '" & tt.name(TShader) & "." & inputName & ": " & tt.name(typeof(inputValue)) & "' not found in '" & tt.name(TInstance) & "'" |
|
1163
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
595 elif typeof(inputValue) is Texture or typeof(inputValue) is object: |
| 1161 | 596 for meshName, meshValue in default(TMesh).fieldPairs: |
| 597 when meshName == inputName: | |
| 1162 | 598 assert foundField == false, "Shader input '" & tt.name(TShader) & "." & inputName & "' has been found more than once" |
|
1163
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
599 assert typeof(meshValue) is typeof(inputValue), "Shader input " & tt.name(TShader) & "." & inputName & " is of type '" & tt.name(typeof(inputValue)) & "' but mesh attribute is of type '" & tt.name(elementType(meshValue)) & "'" |
| 1161 | 600 foundField = true |
| 601 for globalName, globalValue in default(TGlobals).fieldPairs: | |
| 602 when globalName == inputName: | |
| 1162 | 603 assert foundField == false, "Shader input '" & tt.name(TShader) & "." & inputName & "' has been found more than once" |
| 604 assert typeof(globalValue) is typeof(inputValue), "Shader input " & tt.name(TShader) & "." & inputName & " is of type '" & tt.name(typeof(inputValue)) & "' but global attribute is of type '" & tt.name(typeof(globalValue)) & "'" | |
| 1161 | 605 foundField = true |
| 1162 | 606 assert foundField, "Shader input '" & tt.name(TShader) & "." & inputName & ": " & tt.name(typeof(inputValue)) & "' not found in '" & tt.name(TMesh) & "|" & tt.name(TGlobals) & "'" |
|
1163
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
607 elif typeof(inputValue) is array: |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
608 when (elementType(inputValue) is Texture or elementType(inputValue) is object): |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
609 for meshName, meshValue in default(TMesh).fieldPairs: |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
610 when meshName == inputName: |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
611 assert foundField == false, "Shader input '" & tt.name(TShader) & "." & inputName & "' has been found more than once" |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
612 assert typeof(meshValue) is typeof(inputValue), "Shader input " & tt.name(TShader) & "." & inputName & " is of type '" & tt.name(typeof(inputValue)) & "' but mesh attribute is of type '" & tt.name(elementType(meshValue)) & "'" |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
613 foundField = true |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
614 for globalName, globalValue in default(TGlobals).fieldPairs: |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
615 when globalName == inputName: |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
616 assert foundField == false, "Shader input '" & tt.name(TShader) & "." & inputName & "' has been found more than once" |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
617 assert typeof(globalValue) is typeof(inputValue), "Shader input " & tt.name(TShader) & "." & inputName & " is of type '" & tt.name(typeof(inputValue)) & "' but global attribute is of type '" & tt.name(typeof(globalValue)) & "'" |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
618 foundField = true |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
619 assert foundField, "Shader input '" & tt.name(TShader) & "." & inputName & ": " & tt.name(typeof(inputValue)) & "' not found in '" & tt.name(TMesh) & "|" & tt.name(TGlobals) & "'" |
| 1161 | 620 |
| 621 | |
| 1162 | 622 proc Render[TShader, TMesh, TInstance, TGlobals]( |
| 623 pipeline: Pipeline[TShader], | |
| 1161 | 624 renderable: Renderable[TMesh, TInstance], |
| 625 globals: TGlobals, | |
| 626 commandBuffer: VkCommandBuffer, | |
| 627 ) = | |
| 1164 | 628 static: AssertCompatible(TShader, TMesh, TInstance, TGlobals) |
| 629 if renderable.vertexBuffers.len > 0: | |
| 630 commandBuffer.vkCmdBindVertexBuffers( | |
| 631 firstBinding = 0'u32, | |
| 632 bindingCount = uint32(renderable.vertexBuffers.len), | |
| 633 pBuffers = renderable.vertexBuffers.ToCPointer(), | |
| 634 pOffsets = renderable.bufferOffsets.ToCPointer() | |
| 635 ) | |
| 1161 | 636 if renderable.indexType != None: |
| 1159 | 637 commandBuffer.vkCmdBindIndexBuffer( |
| 638 renderable.indexBuffer, | |
| 639 renderable.indexBufferOffset, | |
| 1161 | 640 renderable.indexType, |
| 1159 | 641 ) |
| 642 commandBuffer.vkCmdDrawIndexed( | |
| 1161 | 643 indexCount = renderable.indexCount, |
| 644 instanceCount = renderable.instanceCount, | |
| 1159 | 645 firstIndex = 0, |
| 646 vertexOffset = 0, | |
| 647 firstInstance = 0 | |
| 648 ) | |
| 649 else: | |
| 650 commandBuffer.vkCmdDraw( | |
| 1161 | 651 vertexCount = renderable.vertexCount, |
| 652 instanceCount = renderable.instanceCount, | |
| 1159 | 653 firstVertex = 0, |
| 654 firstInstance = 0 | |
| 655 ) | |
| 1161 | 656 |
| 657 when isMainModule: | |
| 1162 | 658 import semicongine/platform/window |
| 659 import semicongine/vulkan/instance | |
| 660 import semicongine/vulkan/device | |
| 661 import semicongine/vulkan/physicaldevice | |
| 662 import semicongine/vulkan/renderpass | |
|
1163
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
663 import semicongine/vulkan/commandbuffer |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
664 import std/options |
| 1162 | 665 |
| 1161 | 666 type |
|
1163
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
667 MaterialA = object |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
668 reflection: float32 |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
669 baseColor: Vec3f |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
670 ShaderSettings = object |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
671 brightness: float32 |
| 1161 | 672 MeshA = object |
| 673 position: seq[Vec3f] | |
| 674 transparency: float | |
|
1163
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
675 material: array[3, MaterialA] |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
676 materialTextures: array[3, Texture] |
| 1161 | 677 InstanceA = object |
| 678 transform: seq[Mat4] | |
| 679 position: seq[Vec3f] | |
| 680 Globals = object | |
| 1162 | 681 fontAtlas: Texture |
|
1163
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
682 settings: ShaderSettings |
| 1161 | 683 |
| 1162 | 684 ShaderA = object |
|
1163
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
685 # vertex input |
| 1161 | 686 position {.VertexAttribute.}: Vec3f |
| 687 transform {.InstanceAttribute.}: Mat4 | |
|
1163
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
688 # intermediate |
| 1162 | 689 test {.Pass.}: float32 |
| 690 test1 {.PassFlat.}: Vec3f | |
|
1163
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
691 # output |
| 1162 | 692 color {.ShaderOutput.}: Vec4f |
|
1163
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
693 # uniforms |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
694 material: array[3, MaterialA] |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
695 settings: ShaderSettings |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
696 # textures |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
697 fontAtlas: Texture |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
698 materialTextures: array[3, Texture] |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
699 # code |
| 1162 | 700 vertexCode: string = "void main() {}" |
| 701 fragmentCode: string = "void main() {}" | |
| 1161 | 702 |
| 1162 | 703 let w = CreateWindow("test2") |
| 704 putEnv("VK_LAYER_ENABLES", "VALIDATION_CHECK_ENABLE_VENDOR_SPECIFIC_AMD,VALIDATION_CHECK_ENABLE_VENDOR_SPECIFIC_NVIDIA,VK_VALIDATION_FEATURE_ENABLE_SYNCHRONIZATION_VALIDATION_EXTVK_VALIDATION_FEATURE_ENABLE_GPU_ASSISTED_EXT,VK_VALIDATION_FEATURE_ENABLE_SYNCHRONIZATION_VALIDATION_EXT") | |
| 705 let i = w.CreateInstance( | |
| 706 vulkanVersion = VK_MAKE_API_VERSION(0, 1, 3, 0), | |
| 707 instanceExtensions = @[], | |
| 708 layers = @["VK_LAYER_KHRONOS_validation"], | |
| 709 ) | |
| 710 | |
| 711 | |
| 712 let selectedPhysicalDevice = i.GetPhysicalDevices().FilterBestGraphics() | |
| 713 let d = i.CreateDevice( | |
| 714 selectedPhysicalDevice, | |
| 715 enabledExtensions = @[], | |
| 716 selectedPhysicalDevice.FilterForGraphicsPresentationQueues() | |
| 717 ) | |
| 718 | |
| 1161 | 719 var r: Renderable[MeshA, InstanceA] |
| 720 var g: Globals | |
| 721 | |
|
1163
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
722 const shader = ShaderA() |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
723 let shaderObject = d.vk.CompileShader(shader) |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
724 let rp = d.vk.CreateRenderPass(d.physicalDevice.GetSurfaceFormats().FilterSurfaceFormat().format) |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
725 var p = CreatePipeline(d.vk, renderPass = rp, shaderObject) |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
726 |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
727 let commandBufferPool = d.CreateCommandBufferPool(d.FirstGraphicsQueue().get().family, INFLIGHTFRAMES) |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
728 let cmd = commandBufferPool.buffers[0] |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
729 |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
730 checkVkResult cmd.vkResetCommandBuffer(VkCommandBufferResetFlags(0)) |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
731 let beginInfo = VkCommandBufferBeginInfo( |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
732 sType: VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO, |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
733 flags: VkCommandBufferUsageFlags(VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT), |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
734 ) |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
735 checkVkResult cmd.vkBeginCommandBuffer(addr(beginInfo)) |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
736 p.Bind(cmd, currentFrameInFlight = 0) |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
737 p.Render(r, g, cmd) |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
738 |
|
438d32d8b14f
add: more static compilation stuff, code is getting a bit crazy, but also super nice API
sam <sam@basx.dev>
parents:
1162
diff
changeset
|
739 checkVkResult cmd.vkEndCommandBuffer() |
