Mercurial > games > semicongine
annotate src/zamikongine/descriptor.nim @ 34:a3d46f434616
fix: newline
author | Sam <sam@basx.dev> |
---|---|
date | Mon, 16 Jan 2023 00:03:20 +0700 |
parents | 94c38e4b5782 |
children |
rev | line source |
---|---|
32 | 1 import std/strutils |
2 import std/unicode | |
3 import std/strformat | |
4 import std/typetraits | |
5 | |
30 | 6 import ./vulkan |
7 import ./vulkan_helpers | |
8 import ./math/vector | |
9 import ./math/matrix | |
10 import ./buffer | |
32 | 11 import ./glsl_helpers |
30 | 12 |
32 | 13 # TODO: check for alignment in uniform blocks |
14 # | |
30 | 15 type |
16 DescriptorType = SomeNumber|Vec|Mat | |
17 Descriptor*[T:DescriptorType] = object | |
18 value*: T | |
19 | |
20 proc createUniformDescriptorLayout*(device: VkDevice, shaderStage: VkShaderStageFlags, binding: uint32): VkDescriptorSetLayout = | |
21 var | |
22 layoutbinding = VkDescriptorSetLayoutBinding( | |
23 binding: binding, | |
24 descriptorType: VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, | |
25 descriptorCount: 1, | |
26 stageFlags: shaderStage, | |
27 pImmutableSamplers: nil, | |
28 ) | |
29 layoutInfo = VkDescriptorSetLayoutCreateInfo( | |
30 sType: VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO, | |
31 bindingCount: 1, | |
32 pBindings: addr(layoutbinding) | |
33 ) | |
34 checkVkResult device.vkCreateDescriptorSetLayout(addr(layoutInfo), nil, addr(result)) | |
35 | |
33
94c38e4b5782
did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
32
diff
changeset
|
36 proc createUniformBuffers*[nBuffers: static int, Uniforms](device: VkDevice, physicalDevice: VkPhysicalDevice): array[nBuffers, Buffer] = |
94c38e4b5782
did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
32
diff
changeset
|
37 let size = sizeof(Uniforms) |
30 | 38 for i in 0 ..< nBuffers: |
39 var buffer = InitBuffer( | |
40 device, | |
41 physicalDevice, | |
42 uint64(size), | |
43 {UniformBuffer}, | |
44 {VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, VK_MEMORY_PROPERTY_HOST_COHERENT_BIT}, | |
45 persistentMapping=true, | |
46 ) | |
47 result[i] = buffer | |
32 | 48 |
49 template getDescriptorType*(v: Descriptor): auto = get(genericParams(typeof(v)), 0) | |
50 | |
51 func generateGLSLUniformDeclarations*[Uniforms](binding: int = 0): string {.compileTime.} = | |
52 var stmtList: seq[string] | |
53 | |
33
94c38e4b5782
did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
32
diff
changeset
|
54 when not (Uniforms is void): |
94c38e4b5782
did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
32
diff
changeset
|
55 let uniformTypeName = name(Uniforms).toUpper() |
94c38e4b5782
did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
32
diff
changeset
|
56 let uniformInstanceName = name(Uniforms).toLower() |
94c38e4b5782
did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
32
diff
changeset
|
57 stmtList.add(&"layout(binding = {binding}) uniform {uniformTypeName} {{") |
94c38e4b5782
did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
32
diff
changeset
|
58 for fieldname, value in Uniforms().fieldPairs: |
94c38e4b5782
did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
32
diff
changeset
|
59 when typeof(value) is Descriptor: |
94c38e4b5782
did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
32
diff
changeset
|
60 let glsltype = getGLSLType[getDescriptorType(value)]() |
94c38e4b5782
did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
32
diff
changeset
|
61 let n = fieldname |
94c38e4b5782
did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
32
diff
changeset
|
62 stmtList.add(&" {glsltype} {n};") |
94c38e4b5782
did: refactoring, move more from make to nimscript
Sam <sam@basx.dev>
parents:
32
diff
changeset
|
63 stmtList.add(&"}} {uniformInstanceName};") |
32 | 64 |
65 return stmtList.join("\n") |