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