Mercurial > games > semicongine
annotate semiconginev2/rendering/shaders.nim @ 1265:51348a4abefc
Added tag hg2 for changeset cb4d626ca671
| author | sam <sam@basx.dev> |
|---|---|
| date | Sun, 28 Jul 2024 21:59:22 +0700 |
| parents | e9b8d87b9883 |
| children |
| rev | line source |
|---|---|
| 1228 | 1 func GlslType[T: SupportedGPUType|Image](value: T): string = |
| 1192 | 2 when T is float32: "float" |
| 3 elif T is float64: "double" | |
| 4 elif T is int8 or T is int16 or T is int32 or T is int64: "int" | |
| 5 elif T is uint8 or T is uint16 or T is uint32 or T is uint64: "uint" | |
| 6 elif T is TVec2[int32]: "ivec2" | |
| 7 elif T is TVec2[int64]: "ivec2" | |
| 8 elif T is TVec3[int32]: "ivec3" | |
| 9 elif T is TVec3[int64]: "ivec3" | |
| 10 elif T is TVec4[int32]: "ivec4" | |
| 11 elif T is TVec4[int64]: "ivec4" | |
| 12 elif T is TVec2[uint32]: "uvec2" | |
| 13 elif T is TVec2[uint64]: "uvec2" | |
| 14 elif T is TVec3[uint32]: "uvec3" | |
| 15 elif T is TVec3[uint64]: "uvec3" | |
| 16 elif T is TVec4[uint32]: "uvec4" | |
| 17 elif T is TVec4[uint64]: "uvec4" | |
| 18 elif T is TVec2[float32]: "vec2" | |
| 19 elif T is TVec2[float64]: "dvec2" | |
| 20 elif T is TVec3[float32]: "vec3" | |
| 21 elif T is TVec3[float64]: "dvec3" | |
| 22 elif T is TVec4[float32]: "vec4" | |
| 23 elif T is TVec4[float64]: "dvec4" | |
| 24 elif T is TMat2[float32]: "mat2" | |
| 25 elif T is TMat2[float64]: "dmat2" | |
| 26 elif T is TMat23[float32]: "mat23" | |
| 27 elif T is TMat23[float64]: "dmat23" | |
| 28 elif T is TMat32[float32]: "mat32" | |
| 29 elif T is TMat32[float64]: "dmat32" | |
| 30 elif T is TMat3[float32]: "mat3" | |
| 31 elif T is TMat3[float64]: "dmat3" | |
| 32 elif T is TMat34[float32]: "mat34" | |
| 33 elif T is TMat34[float64]: "dmat34" | |
| 34 elif T is TMat43[float32]: "mat43" | |
| 35 elif T is TMat43[float64]: "dmat43" | |
| 36 elif T is TMat4[float32]: "mat4" | |
| 37 elif T is TMat4[float64]: "dmat4" | |
| 1228 | 38 elif T is Image: "sampler2D" |
|
1247
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1240
diff
changeset
|
39 else: |
|
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1240
diff
changeset
|
40 const n = typetraits.name(T) |
|
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
1240
diff
changeset
|
41 {.error: "Unsupported data type on GPU: " & n.} |
| 1192 | 42 |
| 43 func VkType[T: SupportedGPUType](value: T): VkFormat = | |
| 44 when T is float32: VK_FORMAT_R32_SFLOAT | |
| 45 elif T is float64: VK_FORMAT_R64_SFLOAT | |
| 46 elif T is int8: VK_FORMAT_R8_SINT | |
| 47 elif T is int16: VK_FORMAT_R16_SINT | |
| 48 elif T is int32: VK_FORMAT_R32_SINT | |
| 49 elif T is int64: VK_FORMAT_R64_SINT | |
| 50 elif T is uint8: VK_FORMAT_R8_UINT | |
| 51 elif T is uint16: VK_FORMAT_R16_UINT | |
| 52 elif T is uint32: VK_FORMAT_R32_UINT | |
| 53 elif T is uint64: VK_FORMAT_R64_UINT | |
| 54 elif T is TVec2[int32]: VK_FORMAT_R32G32_SINT | |
| 55 elif T is TVec2[int64]: VK_FORMAT_R64G64_SINT | |
| 56 elif T is TVec3[int32]: VK_FORMAT_R32G32B32_SINT | |
| 57 elif T is TVec3[int64]: VK_FORMAT_R64G64B64_SINT | |
| 58 elif T is TVec4[int32]: VK_FORMAT_R32G32B32A32_SINT | |
| 59 elif T is TVec4[int64]: VK_FORMAT_R64G64B64A64_SINT | |
| 60 elif T is TVec2[uint32]: VK_FORMAT_R32G32_UINT | |
| 61 elif T is TVec2[uint64]: VK_FORMAT_R64G64_UINT | |
| 62 elif T is TVec3[uint32]: VK_FORMAT_R32G32B32_UINT | |
| 63 elif T is TVec3[uint64]: VK_FORMAT_R64G64B64_UINT | |
| 64 elif T is TVec4[uint32]: VK_FORMAT_R32G32B32A32_UINT | |
| 65 elif T is TVec4[uint64]: VK_FORMAT_R64G64B64A64_UINT | |
| 66 elif T is TVec2[float32]: VK_FORMAT_R32G32_SFLOAT | |
| 67 elif T is TVec2[float64]: VK_FORMAT_R64G64_SFLOAT | |
| 68 elif T is TVec3[float32]: VK_FORMAT_R32G32B32_SFLOAT | |
| 69 elif T is TVec3[float64]: VK_FORMAT_R64G64B64_SFLOAT | |
| 70 elif T is TVec4[float32]: VK_FORMAT_R32G32B32A32_SFLOAT | |
| 71 elif T is TVec4[float64]: VK_FORMAT_R64G64B64A64_SFLOAT | |
| 72 elif T is TMat2[float32]: VK_FORMAT_R32G32_SFLOAT | |
| 73 elif T is TMat2[float64]: VK_FORMAT_R64G64_SFLOAT | |
| 74 elif T is TMat23[float32]: VK_FORMAT_R32G32B32_SFLOAT | |
| 75 elif T is TMat23[float64]: VK_FORMAT_R64G64B64_SFLOAT | |
| 76 elif T is TMat32[float32]: VK_FORMAT_R32G32_SFLOAT | |
| 77 elif T is TMat32[float64]: VK_FORMAT_R64G64_SFLOAT | |
| 78 elif T is TMat3[float32]: VK_FORMAT_R32G32B32_SFLOAT | |
| 79 elif T is TMat3[float64]: VK_FORMAT_R64G64B64_SFLOAT | |
| 80 elif T is TMat34[float32]: VK_FORMAT_R32G32B32A32_SFLOAT | |
| 81 elif T is TMat34[float64]: VK_FORMAT_R64G64B64A64_SFLOAT | |
| 82 elif T is TMat43[float32]: VK_FORMAT_R32G32B32_SFLOAT | |
| 83 elif T is TMat43[float64]: VK_FORMAT_R64G64B64_SFLOAT | |
| 84 elif T is TMat4[float32]: VK_FORMAT_R32G32B32A32_SFLOAT | |
| 85 elif T is TMat4[float64]: VK_FORMAT_R64G64B64A64_SFLOAT | |
| 86 else: {.error: "Unsupported data type on GPU".} | |
| 87 | |
| 88 | |
| 1228 | 89 func NumberOfVertexInputAttributeDescriptors[T: SupportedGPUType|Image](value: T): uint32 = |
| 1192 | 90 when T is TMat2[float32] or T is TMat2[float64] or T is TMat23[float32] or T is TMat23[float64]: |
| 91 2 | |
| 92 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]: | |
| 93 3 | |
| 94 elif T is TMat43[float32] or T is TMat43[float64] or T is TMat4[float32] or T is TMat4[float64]: | |
| 95 4 | |
| 96 else: | |
| 97 1 | |
| 98 | |
| 1228 | 99 func NLocationSlots[T: SupportedGPUType|Image](value: T): uint32 = |
| 1192 | 100 #[ |
| 101 single location: | |
| 102 - any scalar | |
| 103 - any 16-bit vector | |
| 104 - any 32-bit vector | |
| 105 - any 64-bit vector that has max. 2 components | |
| 106 16-bit scalar and vector types, and | |
| 107 32-bit scalar and vector types, and | |
| 108 64-bit scalar and 2-component vector types. | |
| 109 two locations | |
| 110 64-bit three- and four-component vectors | |
| 111 ]# | |
| 112 when T is TVec3[int64] or | |
| 113 T is TVec4[int64] or | |
| 114 T is TVec3[uint64] or | |
| 115 T is TVec4[uint64] or | |
| 116 T is TVec3[float64] or | |
| 117 T is TVec4[float64] or | |
| 118 T is TMat23[float64] or | |
| 119 T is TMat3[float64] or | |
| 120 T is TMat34[float64] or | |
| 121 T is TMat43[float64] or | |
| 122 T is TMat4[float64]: | |
| 123 return 2 | |
| 124 else: | |
| 125 return 1 | |
|
1190
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
126 |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
127 proc generateShaderSource[TShader](shader: TShader): (string, string) {.compileTime.} = |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
128 const GLSL_VERSION = "450" |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
129 var vsInput: seq[string] |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
130 var vsOutput: seq[string] |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
131 var fsInput: seq[string] |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
132 var fsOutput: seq[string] |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
133 var uniforms: seq[string] |
| 1252 | 134 var pushConstants: seq[string] |
|
1190
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
135 var samplers: seq[string] |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
136 var vsInputLocation = 0'u32 |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
137 var passLocation = 0 |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
138 var fsOutputLocation = 0 |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
139 |
|
1205
f7530247a21f
did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
140 var sawDescriptorSets = false |
|
1190
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
141 for fieldname, value in fieldPairs(shader): |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
142 # vertex shader inputs |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
143 when hasCustomPragma(value, VertexAttribute) or hasCustomPragma(value, InstanceAttribute): |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
144 assert typeof(value) is SupportedGPUType |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
145 vsInput.add "layout(location = " & $vsInputLocation & ") in " & GlslType(value) & " " & fieldname & ";" |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
146 for j in 0 ..< NumberOfVertexInputAttributeDescriptors(value): |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
147 vsInputLocation += NLocationSlots(value) |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
148 |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
149 # intermediate values, passed between shaders |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
150 elif hasCustomPragma(value, Pass) or hasCustomPragma(value, PassFlat): |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
151 let flat = if hasCustomPragma(value, PassFlat): "flat " else: "" |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
152 vsOutput.add "layout(location = " & $passLocation & ") " & flat & "out " & GlslType(value) & " " & fieldname & ";" |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
153 fsInput.add "layout(location = " & $passLocation & ") " & flat & "in " & GlslType(value) & " " & fieldname & ";" |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
154 passLocation.inc |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
155 |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
156 # fragment shader output |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
157 elif hasCustomPragma(value, ShaderOutput): |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
158 fsOutput.add &"layout(location = " & $fsOutputLocation & ") out " & GlslType(value) & " " & fieldname & ";" |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
159 fsOutputLocation.inc |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
160 |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
161 # descriptor sets |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
162 # need to consider 4 cases: uniform block, texture, uniform block array, texture array |
|
1205
f7530247a21f
did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
163 elif hasCustomPragma(value, DescriptorSets): |
|
f7530247a21f
did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
164 assert not sawDescriptorSets, "Only one field with pragma DescriptorSets allowed per shader" |
|
f7530247a21f
did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
165 assert typeof(value) is tuple, "Descriptor field '" & fieldname & "' must be of type tuple" |
|
f7530247a21f
did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
166 assert tupleLen(typeof(value)) <= MAX_DESCRIPTORSETS, typetraits.name(TShader) & ": maximum " & $MAX_DESCRIPTORSETS & " allowed" |
|
f7530247a21f
did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
167 sawDescriptorSets = true |
|
f7530247a21f
did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
168 var descriptorSetIndex = 0 |
|
f7530247a21f
did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
169 for descriptor in value.fields: |
|
1190
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
170 |
|
1205
f7530247a21f
did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
171 var descriptorBinding = 0 |
| 1210 | 172 |
|
1205
f7530247a21f
did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
173 for descriptorName, descriptorValue in fieldPairs(descriptor): |
|
1190
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
174 |
| 1228 | 175 when typeof(descriptorValue) is Image: |
|
1205
f7530247a21f
did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
176 samplers.add "layout(set=" & $descriptorSetIndex & ", binding = " & $descriptorBinding & ") uniform " & GlslType(descriptorValue) & " " & descriptorName & ";" |
|
f7530247a21f
did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
177 descriptorBinding.inc |
|
1190
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
178 |
|
1205
f7530247a21f
did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
179 elif typeof(descriptorValue) is GPUValue: |
| 1210 | 180 |
|
1205
f7530247a21f
did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
181 uniforms.add "layout(set=" & $descriptorSetIndex & ", binding = " & $descriptorBinding & ") uniform T" & descriptorName & " {" |
|
f7530247a21f
did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
182 when typeof(descriptorValue.data) is object: |
| 1210 | 183 |
|
1205
f7530247a21f
did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
184 for blockFieldName, blockFieldValue in descriptorValue.data.fieldPairs(): |
|
f7530247a21f
did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
185 assert typeof(blockFieldValue) is SupportedGPUType, "uniform block field '" & blockFieldName & "' is not a SupportedGPUType" |
|
f7530247a21f
did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
186 uniforms.add " " & GlslType(blockFieldValue) & " " & blockFieldName & ";" |
|
f7530247a21f
did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
187 uniforms.add "} " & descriptorName & ";" |
| 1210 | 188 |
| 189 else: | |
| 190 {.error: "Unsupported shader descriptor field " & descriptorName & " (must be object)".} | |
|
1190
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
191 descriptorBinding.inc |
| 1210 | 192 |
|
1205
f7530247a21f
did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
193 elif typeof(descriptorValue) is array: |
| 1210 | 194 |
| 1228 | 195 when elementType(descriptorValue) is Image: |
| 1210 | 196 |
|
1205
f7530247a21f
did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
197 let arrayDecl = "[" & $typeof(descriptorValue).len & "]" |
|
f7530247a21f
did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
198 samplers.add "layout(set=" & $descriptorSetIndex & ", binding = " & $descriptorBinding & ") uniform " & GlslType(default(elementType(descriptorValue))) & " " & descriptorName & "" & arrayDecl & ";" |
|
f7530247a21f
did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
199 descriptorBinding.inc |
| 1210 | 200 |
| 201 elif elementType(descriptorValue) is GPUValue: | |
| 202 | |
| 203 uniforms.add "layout(set=" & $descriptorSetIndex & ", binding = " & $descriptorBinding & ") uniform T" & descriptorName & " {" | |
| 204 | |
| 205 for blockFieldName, blockFieldValue in default(elementType(descriptorValue)).data.fieldPairs(): | |
| 206 assert typeof(blockFieldValue) is SupportedGPUType, "uniform block field '" & blockFieldName & "' is not a SupportedGPUType" | |
| 207 uniforms.add " " & GlslType(blockFieldValue) & " " & blockFieldName & ";" | |
| 208 uniforms.add "} " & descriptorName & "[" & $descriptorValue.len & "];" | |
| 209 descriptorBinding.inc | |
| 210 | |
|
1205
f7530247a21f
did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
211 else: |
|
f7530247a21f
did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
212 {.error: "Unsupported shader descriptor field " & descriptorName.} |
| 1210 | 213 |
|
1205
f7530247a21f
did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
214 descriptorSetIndex.inc |
|
1190
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
215 elif fieldname in ["vertexCode", "fragmentCode"]: |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
216 discard |
| 1252 | 217 elif hasCustomPragma(value, PushConstantAttribute): |
| 218 assert pushConstants.len == 0, "Only one push constant value allowed" | |
| 219 assert value is object, "push constants need to be objects" | |
| 220 pushConstants.add "layout( push_constant ) uniform constants" | |
| 221 pushConstants.add "{" | |
| 222 for constFieldName, constFieldValue in value.fieldPairs(): | |
| 223 assert typeof(constFieldValue) is SupportedGPUType, "push constant field '" & constFieldName & "' is not a SupportedGPUType" | |
| 224 pushConstants.add " " & GlslType(constFieldValue) & " " & constFieldName & ";" | |
| 225 pushConstants.add "} " & fieldname & ";" | |
|
1190
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
226 else: |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
227 {.error: "Unsupported shader field '" & typetraits.name(TShader) & "." & fieldname & "' of type " & typetraits.name(typeof(value)).} |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
228 |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
229 result[0] = (@[&"#version {GLSL_VERSION}", "#extension GL_EXT_scalar_block_layout : require", ""] & |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
230 vsInput & |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
231 uniforms & |
| 1252 | 232 pushConstants & |
|
1190
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
233 samplers & |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
234 vsOutput & |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
235 @[shader.vertexCode]).join("\n") |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
236 |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
237 result[1] = (@[&"#version {GLSL_VERSION}", "#extension GL_EXT_scalar_block_layout : require", ""] & |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
238 fsInput & |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
239 uniforms & |
| 1252 | 240 pushConstants & |
|
1190
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
241 samplers & |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
242 fsOutput & |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
243 @[shader.fragmentCode]).join("\n") |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
244 |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
245 proc compileGlslToSPIRV(stage: VkShaderStageFlagBits, shaderSource: string): seq[uint32] {.compileTime.} = |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
246 func stage2string(stage: VkShaderStageFlagBits): string {.compileTime.} = |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
247 case stage |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
248 of VK_SHADER_STAGE_VERTEX_BIT: "vert" |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
249 of VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT: "tesc" |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
250 of VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT: "tese" |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
251 of VK_SHADER_STAGE_GEOMETRY_BIT: "geom" |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
252 of VK_SHADER_STAGE_FRAGMENT_BIT: "frag" |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
253 of VK_SHADER_STAGE_COMPUTE_BIT: "comp" |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
254 else: "" |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
255 |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
256 when defined(nimcheck): # will not run if nimcheck is running |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
257 return result |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
258 |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
259 let |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
260 stagename = stage2string(stage) |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
261 shaderHash = hash(shaderSource) |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
262 shaderfile = getTempDir() / &"shader_{shaderHash}.{stagename}" |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
263 |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
264 if not shaderfile.fileExists: |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
265 echo "shader of type ", stage |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
266 for i, line in enumerate(shaderSource.splitlines()): |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
267 echo " ", i + 1, " ", line |
| 1192 | 268 var glslExe = currentSourcePath.parentDir.parentDir.parentDir / "tools" / "glslangValidator" |
|
1190
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
269 when defined(windows): |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
270 glslExe = glslExe & "." & ExeExt |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
271 let command = &"{glslExe} --entry-point main -V --stdin -S {stagename} -o {shaderfile}" |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
272 echo "run: ", command |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
273 discard StaticExecChecked( |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
274 command = command, |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
275 input = shaderSource |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
276 ) |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
277 else: |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
278 echo &"shaderfile {shaderfile} is up-to-date" |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
279 |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
280 when defined(mingw) and defined(linux): # required for crosscompilation, path separators get messed up |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
281 let shaderbinary = staticRead shaderfile.replace("\\", "/") |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
282 else: |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
283 let shaderbinary = staticRead shaderfile |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
284 |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
285 var i = 0 |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
286 while i < shaderbinary.len: |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
287 result.add( |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
288 (uint32(shaderbinary[i + 0]) shl 0) or |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
289 (uint32(shaderbinary[i + 1]) shl 8) or |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
290 (uint32(shaderbinary[i + 2]) shl 16) or |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
291 (uint32(shaderbinary[i + 3]) shl 24) |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
292 ) |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
293 i += 4 |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
294 |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
295 |
|
1200
5c6491f28dcd
did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents:
1192
diff
changeset
|
296 proc CompileShader[TShader](shader: static TShader): (VkShaderModule, VkShaderModule) = |
|
1190
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
297 const (vertexShaderSource, fragmentShaderSource) = generateShaderSource(shader) |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
298 |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
299 let vertexBinary = compileGlslToSPIRV(VK_SHADER_STAGE_VERTEX_BIT, vertexShaderSource) |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
300 let fragmentBinary = compileGlslToSPIRV(VK_SHADER_STAGE_FRAGMENT_BIT, fragmentShaderSource) |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
301 |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
302 var createInfoVertex = VkShaderModuleCreateInfo( |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
303 sType: VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO, |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
304 codeSize: csize_t(vertexBinary.len * sizeof(uint32)), |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
305 pCode: vertexBinary.ToCPointer, |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
306 ) |
|
1200
5c6491f28dcd
did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents:
1192
diff
changeset
|
307 checkVkResult vulkan.device.vkCreateShaderModule(addr(createInfoVertex), nil, addr(result[0])) |
|
1190
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
308 var createInfoFragment = VkShaderModuleCreateInfo( |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
309 sType: VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO, |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
310 codeSize: csize_t(fragmentBinary.len * sizeof(uint32)), |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
311 pCode: fragmentBinary.ToCPointer, |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
312 ) |
|
1200
5c6491f28dcd
did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents:
1192
diff
changeset
|
313 checkVkResult vulkan.device.vkCreateShaderModule(addr(createInfoFragment), nil, addr(result[1])) |
|
1190
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
314 |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
315 template ForVertexDataFields(shader: typed, fieldname, valuename, isinstancename, body: untyped): untyped = |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
316 for theFieldname, value in fieldPairs(shader): |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
317 when hasCustomPragma(value, VertexAttribute) or hasCustomPragma(value, InstanceAttribute): |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
318 when not typeof(value) is seq: |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
319 {.error: "field '" & theFieldname & "' needs to be a seq".} |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
320 when not typeof(value) is SupportedGPUType: |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
321 {.error: "field '" & theFieldname & "' is not a supported GPU type".} |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
322 block: |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
323 const `fieldname` {.inject.} = theFieldname |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
324 let `valuename` {.inject.} = value |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
325 const `isinstancename` {.inject.} = hasCustomPragma(value, InstanceAttribute) |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
326 body |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
327 |
|
1205
f7530247a21f
did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
328 proc GetDescriptorSetCount[TShader](): uint32 = |
|
f7530247a21f
did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
329 for _, value in fieldPairs(default(TShader)): |
|
f7530247a21f
did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
330 when hasCustomPragma(value, DescriptorSets): |
|
f7530247a21f
did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
331 return tupleLen(typeof(value)).uint32 |
|
f7530247a21f
did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
332 |
|
f7530247a21f
did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
333 proc CreateDescriptorSetLayouts[TShader](): array[MAX_DESCRIPTORSETS, VkDescriptorSetLayout] = |
|
f7530247a21f
did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
334 var setNumber: int |
|
f7530247a21f
did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
335 for _, value in fieldPairs(default(TShader)): |
|
f7530247a21f
did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
336 when hasCustomPragma(value, DescriptorSets): |
| 1210 | 337 for descriptorSet in value.fields: |
|
1205
f7530247a21f
did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
338 var layoutbindings: seq[VkDescriptorSetLayoutBinding] |
| 1210 | 339 ForDescriptorFields(descriptorSet, fieldName, fieldValue, descriptorType, descriptorCount, descriptorBindingNumber): |
|
1205
f7530247a21f
did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
340 layoutbindings.add VkDescriptorSetLayoutBinding( |
|
f7530247a21f
did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
341 binding: descriptorBindingNumber, |
|
f7530247a21f
did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
342 descriptorType: descriptorType, |
|
f7530247a21f
did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
343 descriptorCount: descriptorCount, |
|
f7530247a21f
did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
344 stageFlags: VkShaderStageFlags(VK_SHADER_STAGE_ALL_GRAPHICS), |
|
f7530247a21f
did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
345 pImmutableSamplers: nil, |
|
f7530247a21f
did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
346 ) |
|
f7530247a21f
did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
347 var layoutCreateInfo = VkDescriptorSetLayoutCreateInfo( |
|
f7530247a21f
did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
348 sType: VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO, |
|
f7530247a21f
did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
349 bindingCount: layoutbindings.len.uint32, |
|
f7530247a21f
did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
350 pBindings: layoutbindings.ToCPointer |
|
f7530247a21f
did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
351 ) |
|
f7530247a21f
did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
352 checkVkResult vkCreateDescriptorSetLayout( |
|
f7530247a21f
did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
353 vulkan.device, |
|
f7530247a21f
did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
354 addr(layoutCreateInfo), |
|
f7530247a21f
did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
355 nil, |
|
f7530247a21f
did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
356 addr(result[setNumber]) |
|
f7530247a21f
did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
357 ) |
|
f7530247a21f
did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
358 inc setNumber |
|
f7530247a21f
did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
359 |
| 1192 | 360 proc CreatePipeline*[TShader]( |
|
1229
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
361 renderPass: RenderPass, |
|
1190
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
362 topology: VkPrimitiveTopology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST, |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
363 polygonMode: VkPolygonMode = VK_POLYGON_MODE_FILL, |
| 1231 | 364 cullMode: openArray[VkCullModeFlagBits] = [VK_CULL_MODE_BACK_BIT], |
|
1190
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
365 frontFace: VkFrontFace = VK_FRONT_FACE_CLOCKWISE, |
|
1240
42eeb59f3a43
add: more tests, line and point rendering
sam <sam@basx.dev>
parents:
1231
diff
changeset
|
366 lineWidth = 1'f32, |
|
1257
e9b8d87b9883
fix: super stupid bug in perspective matrix X(
sam <sam@basx.dev>
parents:
1253
diff
changeset
|
367 depthClampEnable = false, |
|
1190
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
368 ): Pipeline[TShader] = |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
369 # create pipeline |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
370 |
| 1192 | 371 const shader = default(TShader) |
|
1200
5c6491f28dcd
did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents:
1192
diff
changeset
|
372 (result.vertexShaderModule, result.fragmentShaderModule) = CompileShader(shader) |
| 1192 | 373 |
|
1205
f7530247a21f
did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
374 var nSets = GetDescriptorSetCount[TShader]() |
|
f7530247a21f
did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
375 result.descriptorSetLayouts = CreateDescriptorSetLayouts[TShader]() |
| 1252 | 376 |
| 377 let pushConstant = VkPushConstantRange( | |
| 378 stageFlags: VkShaderStageFlags(VK_SHADER_STAGE_ALL_GRAPHICS), | |
| 379 offset: 0, | |
| 1253 | 380 size: PUSH_CONSTANT_SIZE, # currently supported everywhere, places for two mat4 |
| 1252 | 381 ) |
| 382 | |
|
1190
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
383 let pipelineLayoutInfo = VkPipelineLayoutCreateInfo( |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
384 sType: VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO, |
|
1205
f7530247a21f
did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
385 setLayoutCount: nSets, |
|
f7530247a21f
did: improve descriptor-set handling, add simple descriptor set test
sam <sam@basx.dev>
parents:
1204
diff
changeset
|
386 pSetLayouts: if nSets == 0: nil else: result.descriptorSetLayouts.ToCPointer, |
| 1252 | 387 pushConstantRangeCount: 1, |
| 388 pPushConstantRanges: addr(pushConstant), | |
|
1190
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
389 ) |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
390 checkVkResult vkCreatePipelineLayout(vulkan.device, addr(pipelineLayoutInfo), nil, addr(result.layout)) |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
391 |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
392 let stages = [ |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
393 VkPipelineShaderStageCreateInfo( |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
394 sType: VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
395 stage: VK_SHADER_STAGE_VERTEX_BIT, |
|
1200
5c6491f28dcd
did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents:
1192
diff
changeset
|
396 module: result.vertexShaderModule, |
|
1190
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
397 pName: "main", |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
398 ), |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
399 VkPipelineShaderStageCreateInfo( |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
400 sType: VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
401 stage: VK_SHADER_STAGE_FRAGMENT_BIT, |
|
1200
5c6491f28dcd
did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents:
1192
diff
changeset
|
402 module: result.fragmentShaderModule, |
|
1190
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
403 pName: "main", |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
404 ), |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
405 ] |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
406 var |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
407 bindings: seq[VkVertexInputBindingDescription] |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
408 attributes: seq[VkVertexInputAttributeDescription] |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
409 var inputBindingNumber = 0'u32 |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
410 var location = 0'u32 |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
411 ForVertexDataFields(default(TShader), fieldname, value, isInstanceAttr): |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
412 bindings.add VkVertexInputBindingDescription( |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
413 binding: inputBindingNumber, |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
414 stride: sizeof(value).uint32, |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
415 inputRate: if isInstanceAttr: VK_VERTEX_INPUT_RATE_INSTANCE else: VK_VERTEX_INPUT_RATE_VERTEX, |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
416 ) |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
417 # allows to submit larger data structures like Mat44, for most other types will be 1 |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
418 let perDescriptorSize = sizeof(value).uint32 div NumberOfVertexInputAttributeDescriptors(value) |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
419 for i in 0'u32 ..< NumberOfVertexInputAttributeDescriptors(value): |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
420 attributes.add VkVertexInputAttributeDescription( |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
421 binding: inputBindingNumber, |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
422 location: location, |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
423 format: VkType(value), |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
424 offset: i * perDescriptorSize, |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
425 ) |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
426 location += NLocationSlots(value) |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
427 inc inputBindingNumber |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
428 |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
429 let |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
430 vertexInputInfo = VkPipelineVertexInputStateCreateInfo( |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
431 sType: VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO, |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
432 vertexBindingDescriptionCount: uint32(bindings.len), |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
433 pVertexBindingDescriptions: bindings.ToCPointer, |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
434 vertexAttributeDescriptionCount: uint32(attributes.len), |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
435 pVertexAttributeDescriptions: attributes.ToCPointer, |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
436 ) |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
437 inputAssembly = VkPipelineInputAssemblyStateCreateInfo( |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
438 sType: VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO, |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
439 topology: topology, |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
440 primitiveRestartEnable: false, |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
441 ) |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
442 viewportState = VkPipelineViewportStateCreateInfo( |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
443 sType: VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO, |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
444 viewportCount: 1, |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
445 scissorCount: 1, |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
446 ) |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
447 rasterizer = VkPipelineRasterizationStateCreateInfo( |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
448 sType: VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO, |
|
1257
e9b8d87b9883
fix: super stupid bug in perspective matrix X(
sam <sam@basx.dev>
parents:
1253
diff
changeset
|
449 depthClampEnable: depthClampEnable, |
|
1190
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
450 rasterizerDiscardEnable: VK_FALSE, |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
451 polygonMode: polygonMode, |
|
1240
42eeb59f3a43
add: more tests, line and point rendering
sam <sam@basx.dev>
parents:
1231
diff
changeset
|
452 lineWidth: lineWidth, |
| 1231 | 453 cullMode: toBits cullMode, |
|
1190
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
454 frontFace: frontFace, |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
455 depthBiasEnable: VK_FALSE, |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
456 depthBiasConstantFactor: 0.0, |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
457 depthBiasClamp: 0.0, |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
458 depthBiasSlopeFactor: 0.0, |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
459 ) |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
460 multisampling = VkPipelineMultisampleStateCreateInfo( |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
461 sType: VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO, |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
462 sampleShadingEnable: VK_FALSE, |
|
1229
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
463 rasterizationSamples: renderPass.samples, |
|
1190
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
464 minSampleShading: 1.0, |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
465 pSampleMask: nil, |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
466 alphaToCoverageEnable: VK_FALSE, |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
467 alphaToOneEnable: VK_FALSE, |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
468 ) |
|
1229
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
469 # will only be enabled it the renderpass actually uses a depth buffer |
|
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
470 depthStencil = VkPipelineDepthStencilStateCreateInfo( |
|
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
471 sType: VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO, |
|
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
472 depthTestEnable: true, |
|
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
473 depthWriteEnable: true, |
|
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
474 depthCompareOp: VK_COMPARE_OP_LESS, |
|
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
475 depthBoundsTestEnable: false, |
|
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
476 stencilTestEnable: false, |
|
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
477 minDepthBounds: 0'f32, |
|
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
478 maxDepthBounds: 0'f32, |
|
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
479 ) |
|
1190
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
480 colorBlendAttachment = VkPipelineColorBlendAttachmentState( |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
481 colorWriteMask: toBits [VK_COLOR_COMPONENT_R_BIT, VK_COLOR_COMPONENT_G_BIT, VK_COLOR_COMPONENT_B_BIT, VK_COLOR_COMPONENT_A_BIT], |
|
1240
42eeb59f3a43
add: more tests, line and point rendering
sam <sam@basx.dev>
parents:
1231
diff
changeset
|
482 blendEnable: true, |
|
1190
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
483 srcColorBlendFactor: VK_BLEND_FACTOR_SRC_ALPHA, |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
484 dstColorBlendFactor: VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA, |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
485 colorBlendOp: VK_BLEND_OP_ADD, |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
486 srcAlphaBlendFactor: VK_BLEND_FACTOR_ONE, |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
487 dstAlphaBlendFactor: VK_BLEND_FACTOR_ZERO, |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
488 alphaBlendOp: VK_BLEND_OP_ADD, |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
489 ) |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
490 colorBlending = VkPipelineColorBlendStateCreateInfo( |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
491 sType: VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO, |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
492 logicOpEnable: false, |
|
1240
42eeb59f3a43
add: more tests, line and point rendering
sam <sam@basx.dev>
parents:
1231
diff
changeset
|
493 logicOp: VK_LOGIC_OP_COPY, |
|
1190
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
494 attachmentCount: 1, |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
495 pAttachments: addr(colorBlendAttachment), |
|
1240
42eeb59f3a43
add: more tests, line and point rendering
sam <sam@basx.dev>
parents:
1231
diff
changeset
|
496 blendConstants: [0'f32, 0'f32, 0'f32, 0'f32] |
|
1190
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
497 ) |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
498 dynamicStates = [VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR] |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
499 dynamicState = VkPipelineDynamicStateCreateInfo( |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
500 sType: VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO, |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
501 dynamicStateCount: dynamicStates.len.uint32, |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
502 pDynamicStates: dynamicStates.ToCPointer, |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
503 ) |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
504 let createInfo = VkGraphicsPipelineCreateInfo( |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
505 sType: VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO, |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
506 stageCount: 2, |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
507 pStages: stages.ToCPointer, |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
508 pVertexInputState: addr(vertexInputInfo), |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
509 pInputAssemblyState: addr(inputAssembly), |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
510 pViewportState: addr(viewportState), |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
511 pRasterizationState: addr(rasterizer), |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
512 pMultisampleState: addr(multisampling), |
|
1229
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
513 pDepthStencilState: if renderPass.depthBuffer: addr(depthStencil) else: nil, |
|
1190
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
514 pColorBlendState: addr(colorBlending), |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
515 pDynamicState: addr(dynamicState), |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
516 layout: result.layout, |
|
1229
5dcb503ef0c0
did: refactor renderpass a bit, enable depth buffering and msaa on offscreen-rendering
sam <sam@basx.dev>
parents:
1228
diff
changeset
|
517 renderPass: renderPass.vk, |
|
1190
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
518 subpass: 0, |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
519 basePipelineHandle: VkPipeline(0), |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
520 basePipelineIndex: -1, |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
521 ) |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
522 checkVkResult vkCreateGraphicsPipelines( |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
523 vulkan.device, |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
524 VkPipelineCache(0), |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
525 1, |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
526 addr(createInfo), |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
527 nil, |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
528 addr(result.vk) |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
529 ) |
|
a3eb305bcac2
start of complete and total refactoring: the ULTIMATE
sam <sam@basx.dev>
parents:
diff
changeset
|
530 |
| 1202 | 531 template WithPipeline*(commandbuffer: VkCommandBuffer, pipeline: Pipeline, body: untyped): untyped = |
| 532 block: | |
| 533 vkCmdBindPipeline(commandbuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline.vk) | |
| 534 body | |
| 535 | |
|
1200
5c6491f28dcd
did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents:
1192
diff
changeset
|
536 proc DestroyPipeline*(pipeline: Pipeline) = |
| 1201 | 537 |
| 538 for descriptorSetLayout in pipeline.descriptorSetLayouts: | |
| 539 vkDestroyDescriptorSetLayout(vulkan.device, descriptorSetLayout, nil) | |
| 540 | |
|
1200
5c6491f28dcd
did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents:
1192
diff
changeset
|
541 vkDestroyShaderModule(vulkan.device, pipeline.vertexShaderModule, nil) |
|
5c6491f28dcd
did: simplify some swapchain stuff, add many destructor calls
sam <sam@basx.dev>
parents:
1192
diff
changeset
|
542 vkDestroyShaderModule(vulkan.device, pipeline.fragmentShaderModule, nil) |
| 1201 | 543 vkDestroyPipelineLayout(vulkan.device, pipeline.layout, nil) |
| 544 vkDestroyPipeline(vulkan.device, pipeline.vk, nil) |
