annotate static_utils.nim @ 1161:dbca0528c714 compiletime-tests

add: more stuff
author sam <sam@basx.dev>
date Wed, 19 Jun 2024 13:50:18 +0700
parents 836dc1eda5e3
children 46fae89cffb0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
1 import std/macros
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
2 import std/strformat
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
3 import std/typetraits
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
4
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
5 import semicongine/core/utils
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
6 import semicongine/core/imagetypes
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
7 import semicongine/core/vector
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
8 import semicongine/core/matrix
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
9 import semicongine/core/vulkanapi
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
10 import semicongine/vulkan/buffer
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
11
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
12 template VertexAttribute* {.pragma.}
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
13 template InstanceAttribute* {.pragma.}
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
14 template DescriptorAttribute* {.pragma.}
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
15
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
16
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
17 type
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
18 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]
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
19
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
20 func VkType[T: SupportedGPUType](value: T): VkFormat =
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
21 when T is float32: VK_FORMAT_R32_SFLOAT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
22 elif T is float64: VK_FORMAT_R64_SFLOAT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
23 elif T is int8: VK_FORMAT_R8_SINT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
24 elif T is int16: VK_FORMAT_R16_SINT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
25 elif T is int32: VK_FORMAT_R32_SINT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
26 elif T is int64: VK_FORMAT_R64_SINT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
27 elif T is uint8: VK_FORMAT_R8_UINT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
28 elif T is uint16: VK_FORMAT_R16_UINT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
29 elif T is uint32: VK_FORMAT_R32_UINT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
30 elif T is uint64: VK_FORMAT_R64_UINT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
31 elif T is TVec2[int32]: VK_FORMAT_R32G32_SINT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
32 elif T is TVec2[int64]: VK_FORMAT_R64G64_SINT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
33 elif T is TVec3[int32]: VK_FORMAT_R32G32B32_SINT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
34 elif T is TVec3[int64]: VK_FORMAT_R64G64B64_SINT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
35 elif T is TVec4[int32]: VK_FORMAT_R32G32B32A32_SINT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
36 elif T is TVec4[int64]: VK_FORMAT_R64G64B64A64_SINT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
37 elif T is TVec2[uint32]: VK_FORMAT_R32G32_UINT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
38 elif T is TVec2[uint64]: VK_FORMAT_R64G64_UINT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
39 elif T is TVec3[uint32]: VK_FORMAT_R32G32B32_UINT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
40 elif T is TVec3[uint64]: VK_FORMAT_R64G64B64_UINT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
41 elif T is TVec4[uint32]: VK_FORMAT_R32G32B32A32_UINT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
42 elif T is TVec4[uint64]: VK_FORMAT_R64G64B64A64_UINT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
43 elif T is TVec2[float32]: VK_FORMAT_R32G32_SFLOAT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
44 elif T is TVec2[float64]: VK_FORMAT_R64G64_SFLOAT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
45 elif T is TVec3[float32]: VK_FORMAT_R32G32B32_SFLOAT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
46 elif T is TVec3[float64]: VK_FORMAT_R64G64B64_SFLOAT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
47 elif T is TVec4[float32]: VK_FORMAT_R32G32B32A32_SFLOAT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
48 elif T is TVec4[float64]: VK_FORMAT_R64G64B64A64_SFLOAT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
49 elif T is Mat2[float32]: VK_FORMAT_R32G32_SFLOAT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
50 elif T is Mat2[float64]: VK_FORMAT_R64G64_SFLOAT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
51 elif T is Mat23[float32]: VK_FORMAT_R32G32B32_SFLOAT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
52 elif T is Mat23[float64]: VK_FORMAT_R64G64B64_SFLOAT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
53 elif T is Mat32[float32]: VK_FORMAT_R32G32_SFLOAT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
54 elif T is Mat32[float64]: VK_FORMAT_R64G64_SFLOAT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
55 elif T is Mat3[float32]: VK_FORMAT_R32G32B32_SFLOAT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
56 elif T is Mat3[float64]: VK_FORMAT_R64G64B64_SFLOAT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
57 elif T is Mat34[float32]: VK_FORMAT_R32G32B32A32_SFLOAT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
58 elif T is Mat34[float64]: VK_FORMAT_R64G64B64A64_SFLOAT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
59 elif T is Mat43[float32]: VK_FORMAT_R32G32B32_SFLOAT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
60 elif T is Mat43[float64]: VK_FORMAT_R64G64B64_SFLOAT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
61 elif T is Mat4[float32]: VK_FORMAT_R32G32B32A32_SFLOAT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
62 elif T is Mat4[float64]: VK_FORMAT_R64G64B64A64_SFLOAT
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
63 else: {.error: "Unsupported data type on GPU".}
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
64
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
65 template getElementType(field: typed): untyped =
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
66 when not (typeof(field) is seq or typeof(field) is array):
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
67 {.error: "getElementType can only be used with seq or array".}
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
68 genericParams(typeof(field)).get(0)
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
69
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
70 template ForVertexDataFields*(inputData: typed, fieldname, valuename, isinstancename, body: untyped): untyped =
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
71 for theFieldname, value in fieldPairs(inputData):
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
72 when hasCustomPragma(value, VertexAttribute) or hasCustomPragma(value, InstanceAttribute):
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
73 when not typeof(value) is seq:
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
74 {.error: "field '" & theFieldname & "' needs to be a seq".}
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
75 when not typeof(value) is SupportedGPUType:
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
76 {.error: "field '" & theFieldname & "' is not a supported GPU type".}
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
77 block:
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
78 let `fieldname` {.inject.} = theFieldname
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
79 let `valuename` {.inject.} = default(getElementType(value))
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
80 let `isinstancename` {.inject.} = value.isInstanceAttribute()
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
81 body
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
82
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
83 template ForDescriptorFields*(inputData: typed, fieldname, valuename, typename, countname, body: untyped): untyped =
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
84 for theFieldname, value in fieldPairs(inputData):
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
85 when hasCustomPragma(value, DescriptorAttribute):
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
86 when not (
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
87 typeof(value) is SupportedGPUType
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
88 or (typeof(value) is array and elementType(value) is SupportedGPUType)
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
89 or typeof(value) is Texture
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
90 ):
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
91 {.error: "field '" & theFieldname & "' needs to be a SupportedGPUType or an array of SupportedGPUType".}
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
92 block:
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
93 let `fieldname` {.inject.} = theFieldname
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
94 let `valuename` {.inject.} = default(getElementType(value))
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
95
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
96 # TODO
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
97 let `typename` {.inject.} = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
98 let `typename` {.inject.} = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
99
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
100 when typeof(value) is array:
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
101 let `countname` {.inject.} = genericParams(typeof(value)).get(0)
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
102 else:
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
103 let `countname` {.inject.} = 1
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
104 body
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
105
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
106 func NumberOfVertexInputAttributeDescriptors[T: SupportedGPUType](value: T): uint32 =
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
107 when T is TMat2[float32] or T is TMat2[float64] or T is TMat23[float32] or T is TMat23[float64]:
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
108 2
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
109 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]:
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
110 3
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
111 elif T is TMat43[float32] or T is TMat43[float64] or T is TMat4[float32] or T is TMat4[float64]:
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
112 4
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
113 else:
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
114 1
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
115
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
116 func NLocationSlots[T: SupportedGPUType](value: T): uint32 =
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
117 #[
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
118 single location:
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
119 16-bit scalar and vector types, and
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
120 32-bit scalar and vector types, and
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
121 64-bit scalar and 2-component vector types.
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
122 two locations
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
123 64-bit three- and four-component vectors
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
124 ]#
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
125 when typeof(value) is TVec3 and sizeof(getElementType(value)) == 8:
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
126 return 2
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
127 elif typeof(value) is TVec4 and sizeof(getElementType(value)) == 8:
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
128 return 2
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
129 else:
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
130 return 1
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
131
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
132 type
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
133 IndexType = enum
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
134 None, UInt8, UInt16, UInt32
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
135 RenderBuffers = object
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
136 deviceBuffers: seq[Buffer] # for fast reads
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
137 hostVisibleBuffers: seq[Buffer] # for fast writes
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
138 Renderable[TMesh, TInstance] = object
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
139 vertexBuffers: seq[VkBuffer]
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
140 bufferOffsets: seq[VkDeviceSize]
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
141 instanceCount: uint32
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
142 case indexType: IndexType
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
143 of None:
1160
836dc1eda5e3 did: some stuff
sam <sam@basx.dev>
parents: 1159
diff changeset
144 vertexCount: uint32
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
145 else:
1160
836dc1eda5e3 did: some stuff
sam <sam@basx.dev>
parents: 1159
diff changeset
146 indexBuffer: VkBuffer
836dc1eda5e3 did: some stuff
sam <sam@basx.dev>
parents: 1159
diff changeset
147 indexCount: uint32
836dc1eda5e3 did: some stuff
sam <sam@basx.dev>
parents: 1159
diff changeset
148 indexBufferOffset: VkDeviceSize
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
149 Pipeline[TShaderInputs] = object
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
150 pipeline: VkPipeline
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
151 layout: VkPipelineLayout
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
152 descriptorSets: array[2, seq[VkDescriptorSet]]
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
153 ShaderSet[TShaderInputs] = object
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
154 vertexShader: VkShaderModule
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
155 fragmentShader: VkShaderModule
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
156 converter toVkIndexType(indexType: IndexType): VkIndexType =
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
157 case indexType:
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
158 of None: VK_INDEX_TYPE_NONE_KHR
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
159 of UInt8: VK_INDEX_TYPE_UINT8_EXT
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
160 of UInt16: VK_INDEX_TYPE_UINT16
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
161 of UInt32: VK_INDEX_TYPE_UINT32
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
162
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
163
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
164 proc CreatePipeline*[TShaderInputs](
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
165 device: VkDevice,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
166 renderPass: VkRenderPass,
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
167 shaderSet: ShaderSet[TShaderInputs],
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
168 topology: VkPrimitiveTopology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
169 polygonMode: VkPolygonMode = VK_POLYGON_MODE_FILL,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
170 cullMode: VkCullModeFlagBits = VK_CULL_MODE_BACK_BIT,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
171 frontFace: VkFrontFace = VK_FRONT_FACE_CLOCKWISE,
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
172 ): Pipeline[TShaderInputs] =
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
173 # assumptions/limitations:
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
174 # - we are only using vertex and fragment shaders (2 stages)
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
175 # - we only support one subpass
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
176
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
177 # CONTINUE HERE, WITH PIPELINE LAYOUT!!!!
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
178 # Rely on TShaderInputs
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
179
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
180 var layoutbindings: seq[VkDescriptorSetLayoutBinding]
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
181 let descriptors = [
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
182 (VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1), # more than 1 for arrays
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
183 (VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1),
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
184 ]
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
185 var descriptorBindingNumber = 0'u32
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
186 ForDescriptorFields(default(TShaderInputs), fieldname, value, descriptorCount):
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
187 layoutbindings.add VkDescriptorSetLayoutBinding(
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
188 binding: descriptorBindingNumber,
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
189 descriptorType: descriptorType,
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
190 descriptorCount: descriptorCount,
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
191 stageFlags: VK_SHADER_STAGE_ALL_GRAPHICS,
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
192 pImmutableSamplers: nil,
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
193 )
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
194 inc descriptorBindingNumber
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
195 var layoutCreateInfo = VkDescriptorSetLayoutCreateInfo(
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
196 sType: VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
197 bindingCount: uint32(layoutbindings.len),
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
198 pBindings: layoutbindings.ToCPointer
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
199 )
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
200 var descriptorSetLayout: VkDescriptorSetLayout
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
201 checkVkResult vkCreateDescriptorSetLayout(device.vk, addr(layoutCreateInfo), nil, addr(descriptorSetLayout))
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
202 let pipelineLayoutInfo = VkPipelineLayoutCreateInfo(
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
203 sType: VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
204 setLayoutCount: 1,
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
205 pSetLayouts: addr(descriptorSetLayout),
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
206 # pushConstantRangeCount: uint32(pushConstants.len),
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
207 # pPushConstantRanges: pushConstants.ToCPointer,
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
208 )
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
209 checkVkResult vkCreatePipelineLayout(device, addr(pipelineLayoutInfo), nil, addr(result.layout))
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
210
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
211 let stages = [
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
212 VkPipelineShaderStageCreateInfo(
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
213 sType: VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
214 stage: VK_SHADER_STAGE_VERTEX_BIT,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
215 module: shaderSet.vertexShader,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
216 pName: "main",
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
217 ),
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
218 VkPipelineShaderStageCreateInfo(
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
219 sType: VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
220 stage: VK_SHADER_STAGE_FRAGMENT_BIT,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
221 module: shaderSet.fragmentShader,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
222 pName: "main",
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
223 ),
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
224 ]
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
225 let
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
226 bindings: var seq[VkVertexInputBindingDescription]
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
227 attributes: var seq[VkVertexInputAttributeDescription]
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
228 var inputBindingNumber = 0'u32
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
229 var inputLocationNumber = 0'u32
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
230 ForVertexDataFields(default(TShaderInputs), fieldname, value, isInstanceAttr):
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
231 bindings.add VkVertexInputBindingDescription(
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
232 binding: inputBindingNumber,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
233 stride: sizeof(value).uint32,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
234 inputRate: if isInstanceAttr: VK_VERTEX_INPUT_RATE_INSTANCE else: VK_VERTEX_INPUT_RATE_VERTEX,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
235 )
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
236 # allows to submit larger data structures like Mat44, for most other types will be 1
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
237 let perDescriptorSize = sizeof(value).uint32 div NumberOfVertexInputAttributeDescriptors(value)
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
238 for i in 0'u32 ..< NumberOfVertexInputAttributeDescriptors(value):
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
239 attributes.add VkVertexInputAttributeDescription(
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
240 binding: inputBindingNumber,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
241 inputLocationNumber: inputLocationNumber,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
242 format: VkType(value),
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
243 offset: i * perDescriptorSize,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
244 )
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
245 inputLocationNumber += NLocationSlots(value)
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
246 inc inputBindingNumber
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
247
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
248 let
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
249 vertexInputInfo = VkPipelineVertexInputStateCreateInfo(
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
250 sType: VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
251 vertexBindingDescriptionCount: uint32(bindings.len),
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
252 pVertexBindingDescriptions: bindings.ToCPointer,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
253 vertexAttributeDescriptionCount: uint32(attributes.len),
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
254 pVertexAttributeDescriptions: attributes.ToCPointer,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
255 )
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
256 inputAssembly = VkPipelineInputAssemblyStateCreateInfo(
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
257 sType: VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
258 topology: topology,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
259 primitiveRestartEnable: false,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
260 )
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
261 viewportState = VkPipelineViewportStateCreateInfo(
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
262 sType: VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
263 viewportCount: 1,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
264 scissorCount: 1,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
265 )
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
266 rasterizer = VkPipelineRasterizationStateCreateInfo(
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
267 sType: VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
268 depthClampEnable: VK_FALSE,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
269 rasterizerDiscardEnable: VK_FALSE,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
270 polygonMode: polygonMode,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
271 lineWidth: 1.0,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
272 cullMode: toBits [cullMode],
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
273 frontFace: frontFace,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
274 depthBiasEnable: VK_FALSE,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
275 depthBiasConstantFactor: 0.0,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
276 depthBiasClamp: 0.0,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
277 depthBiasSlopeFactor: 0.0,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
278 )
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
279 multisampling = VkPipelineMultisampleStateCreateInfo(
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
280 sType: VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
281 sampleShadingEnable: VK_FALSE,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
282 rasterizationSamples: VK_SAMPLE_COUNT_1_BIT,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
283 minSampleShading: 1.0,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
284 pSampleMask: nil,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
285 alphaToCoverageEnable: VK_FALSE,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
286 alphaToOneEnable: VK_FALSE,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
287 )
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
288 colorBlendAttachment = VkPipelineColorBlendAttachmentState(
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
289 colorWriteMask: toBits [VK_COLOR_COMPONENT_R_BIT, VK_COLOR_COMPONENT_G_BIT, VK_COLOR_COMPONENT_B_BIT, VK_COLOR_COMPONENT_A_BIT],
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
290 blendEnable: VK_TRUE,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
291 srcColorBlendFactor: VK_BLEND_FACTOR_SRC_ALPHA,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
292 dstColorBlendFactor: VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
293 colorBlendOp: VK_BLEND_OP_ADD,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
294 srcAlphaBlendFactor: VK_BLEND_FACTOR_ONE,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
295 dstAlphaBlendFactor: VK_BLEND_FACTOR_ZERO,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
296 alphaBlendOp: VK_BLEND_OP_ADD,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
297 )
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
298 colorBlending = VkPipelineColorBlendStateCreateInfo(
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
299 sType: VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
300 logicOpEnable: false,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
301 attachmentCount: 1,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
302 pAttachments: addr(colorBlendAttachment),
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
303 )
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
304 dynamicStates = [VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR]
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
305 dynamicState = VkPipelineDynamicStateCreateInfo(
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
306 sType: VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
307 dynamicStateCount: dynamicStates.len.uint32,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
308 pDynamicStates: dynamicStates.ToCPointer,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
309 )
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
310 let createInfo = VkGraphicsPipelineCreateInfo(
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
311 sType: VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
312 stageCount: 2,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
313 pStages: addr(stages),
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
314 pVertexInputState: addr(vertexInputInfo),
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
315 pInputAssemblyState: addr(inputAssembly),
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
316 pViewportState: addr(viewportState),
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
317 pRasterizationState: addr(rasterizer),
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
318 pMultisampleState: addr(multisampling),
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
319 pDepthStencilState: nil,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
320 pColorBlendState: addr(colorBlending),
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
321 pDynamicState: addr(dynamicState),
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
322 layout: result.layout,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
323 renderPass: renderPass,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
324 subpass: 0,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
325 basePipelineHandle: VkPipeline(0),
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
326 basePipelineIndex: -1,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
327 )
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
328 checkVkResult vkCreateGraphicsPipelines(
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
329 device,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
330 VkPipelineCache(0),
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
331 1,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
332 addr(createInfo),
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
333 nil,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
334 addr(result.pipeline)
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
335 )
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
336
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
337 proc CreateRenderable[TMesh, TInstance](
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
338 mesh: TMesh,
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
339 instance: TInstance,
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
340 buffers: RenderBuffers,
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
341 ): Renderable[TMesh, TInstance] =
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
342 result.indexType = None
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
343
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
344 proc Bind(pipeline: Pipeline, commandBuffer: VkCommandBuffer, currentFrameInFlight: int) =
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
345 commandBuffer.vkCmdBindPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline.pipeline)
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
346 commandBuffer.vkCmdBindDescriptorSets(
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
347 VK_PIPELINE_BIND_POINT_GRAPHICS,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
348 pipeline.layout,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
349 0,
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
350 pipeline.descriptorSets[currentFrameInFlight].len,
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
351 pipeline.descriptorSets[currentFrameInFlight],
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
352 0,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
353 nil,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
354 )
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
355
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
356 proc AssertCompatible(TShaderInputs, TMesh, TInstance, TGlobals: typedesc) =
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
357 # assert seq-fields of TMesh|TInstance == seq-fields of TShaderInputs
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
358 # assert normal fields of TMesh|Globals == normal fields of TShaderDescriptors
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
359 for inputName, inputValue in default(TShaderInputs).fieldPairs:
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
360 echo "checking shader input '" & inputName & "'"
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
361 var foundField = false
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
362 when hasCustomPragma(inputValue, VertexAttribute):
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
363 echo " is vertex attribute"
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
364 for meshName, meshValue in default(TMesh).fieldPairs:
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
365 when meshName == inputName:
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
366 assert foundField == false, "Shader input '" & TShaderInputs.name & "." & inputName & "' has been found more than once"
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
367 assert getElementType(meshValue) is typeof(inputValue), "Shader input " & TShaderInputs.name & "." & inputName & " is of type '" & typeof(inputValue).name & "' but mesh attribute is of type '" & getElementType(meshValue).name & "'"
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
368 foundField = true
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
369 assert foundField, "Shader input '" & TShaderInputs.name & "." & inputName & ": " & typeof(inputValue).name & "' not found in '" & TMesh.name & "'"
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
370 elif hasCustomPragma(inputValue, InstanceAttribute):
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
371 echo " is instance attribute"
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
372 for instanceName, instanceValue in default(TInstance).fieldPairs:
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
373 when instanceName == inputName:
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
374 assert foundField == false, "Shader input '" & TShaderInputs.name & "." & inputName & "' has been found more than once"
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
375 assert getElementType(instanceValue) is typeof(inputValue), "Shader input " & TShaderInputs.name & "." & inputName & " is of type '" & typeof(inputValue).name & "' but instance attribute is of type '" & getElementType(instanceValue).name & "'"
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
376 foundField = true
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
377 assert foundField, "Shader input '" & TShaderInputs.name & "." & inputName & ": " & typeof(inputValue).name & "' not found in '" & TInstance.name & "'"
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
378 elif hasCustomPragma(inputValue, DescriptorAttribute):
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
379 echo " is descriptor attribute"
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
380 for meshName, meshValue in default(TMesh).fieldPairs:
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
381 when meshName == inputName:
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
382 assert foundField == false, "Shader input '" & TShaderInputs.name & "." & inputName & "' has been found more than once"
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
383 assert typeof(meshValue) is typeof(inputValue), "Shader input " & TShaderInputs.name & "." & inputName & " is of type '" & typeof(inputValue).name & "' but mesh attribute is of type '" & getElementType(meshValue).name & "'"
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
384 foundField = true
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
385 for globalName, globalValue in default(TGlobals).fieldPairs:
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
386 when globalName == inputName:
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
387 assert foundField == false, "Shader input '" & TShaderInputs.name & "." & inputName & "' has been found more than once"
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
388 assert typeof(globalValue) is typeof(inputValue), "Shader input " & TShaderInputs.name & "." & inputName & " is of type '" & typeof(inputValue).name & "' but global attribute is of type '" & typeof(globalValue).name & "'"
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
389 foundField = true
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
390 assert foundField, "Shader input '" & TShaderInputs.name & "." & inputName & ": " & typeof(inputValue).name & "' not found in '" & TMesh.name & "|" & TGlobals.name & "'"
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
391 echo " found"
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
392
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
393
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
394 proc Render[TShaderInputs, TMesh, TInstance, TGlobals](
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
395 pipeline: Pipeline[TShaderInputs],
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
396 renderable: Renderable[TMesh, TInstance],
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
397 globals: TGlobals,
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
398 commandBuffer: VkCommandBuffer,
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
399 ) =
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
400 static:
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
401 AssertCompatible(TShaderInputs, TMesh, TInstance, TGlobals)
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
402 commandBuffer.vkCmdBindVertexBuffers(
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
403 firstBinding = 0'u32,
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
404 bindingCount = uint32(renderable.vertexBuffers.len),
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
405 pBuffers = renderable.vertexBuffers.ToCPointer(),
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
406 pOffsets = renderable.bufferOffsets.ToCPointer()
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
407 )
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
408 if renderable.indexType != None:
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
409 commandBuffer.vkCmdBindIndexBuffer(
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
410 renderable.indexBuffer,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
411 renderable.indexBufferOffset,
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
412 renderable.indexType,
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
413 )
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
414 commandBuffer.vkCmdDrawIndexed(
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
415 indexCount = renderable.indexCount,
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
416 instanceCount = renderable.instanceCount,
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
417 firstIndex = 0,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
418 vertexOffset = 0,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
419 firstInstance = 0
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
420 )
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
421 else:
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
422 commandBuffer.vkCmdDraw(
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
423 vertexCount = renderable.vertexCount,
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
424 instanceCount = renderable.instanceCount,
1159
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
425 firstVertex = 0,
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
426 firstInstance = 0
e7cbb13999e4 add: changes and static tools
sam <sam@basx.dev>
parents:
diff changeset
427 )
1161
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
428
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
429 when isMainModule:
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
430 type
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
431 MeshA = object
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
432 position: seq[Vec3f]
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
433 transparency: float
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
434 InstanceA = object
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
435 transform: seq[Mat4]
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
436 position: seq[Vec3f]
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
437 Globals = object
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
438 color: Vec4f
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
439
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
440 ShaderInputsA = object
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
441 position {.VertexAttribute.}: Vec3f
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
442 transform {.InstanceAttribute.}: Mat4
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
443 color {.DescriptorAttribute.}: Vec4f
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
444
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
445 var p: Pipeline[ShaderInputsA]
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
446 var r: Renderable[MeshA, InstanceA]
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
447 var g: Globals
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
448 var s: ShaderSet[ShaderInputsA]
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
449
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
450 var p1 = CreatePipeline(device = VkDevice(0), renderPass = VkRenderPass(0), shaderSet = s)
dbca0528c714 add: more stuff
sam <sam@basx.dev>
parents: 1160
diff changeset
451 Render(p, r, g, VkCommandBuffer(0))