annotate src/semicongine/vulkan/pipeline.nim @ 653:ac7dfbd56cc6

add: textures now support in shader via scene data, also: improved config handling a bit, more to come
author Sam <sam@basx.dev>
date Sat, 06 May 2023 23:06:33 +0700
parents 43240986f76d
children 1f8612f74fac
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
583
90537a8887ae did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents: 575
diff changeset
1 import std/tables
90537a8887ae did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents: 575
diff changeset
2 import std/sequtils
90537a8887ae did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents: 575
diff changeset
3
560
8de8a2102071 add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents:
diff changeset
4 import ./api
8de8a2102071 add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents:
diff changeset
5 import ./device
566
cc7ba46fe3c4 add: descriptors, better swapchain implementation
Sam <sam@basx.dev>
parents: 565
diff changeset
6 import ./descriptor
573
f31a9821ae1c did: real implementation of buffer and memory, getting closer to collect shit for drawing per pipeline
Sam <sam@basx.dev>
parents: 572
diff changeset
7 import ./shader
588
008592db0442 did: big refactoring
Sam <sam@basx.dev>
parents: 583
diff changeset
8 import ./buffer
575
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
9 import ./utils
650
be6e0f89645a add: initial code for texture support, not finished, had to completely refactor how to handle material-data (ie scene-wide data, sorry if you ever read this
Sam <sam@basx.dev>
parents: 644
diff changeset
10 import ./image
573
f31a9821ae1c did: real implementation of buffer and memory, getting closer to collect shit for drawing per pipeline
Sam <sam@basx.dev>
parents: 572
diff changeset
11
f31a9821ae1c did: real implementation of buffer and memory, getting closer to collect shit for drawing per pipeline
Sam <sam@basx.dev>
parents: 572
diff changeset
12 import ../gpu_data
560
8de8a2102071 add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents:
diff changeset
13
8de8a2102071 add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents:
diff changeset
14 type
566
cc7ba46fe3c4 add: descriptors, better swapchain implementation
Sam <sam@basx.dev>
parents: 565
diff changeset
15 Pipeline* = object
569
f4d8f0018670 did: reorganize code
Sam <sam@basx.dev>
parents: 568
diff changeset
16 device*: Device
560
8de8a2102071 add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents:
diff changeset
17 vk*: VkPipeline
569
f4d8f0018670 did: reorganize code
Sam <sam@basx.dev>
parents: 568
diff changeset
18 layout*: VkPipelineLayout
573
f31a9821ae1c did: real implementation of buffer and memory, getting closer to collect shit for drawing per pipeline
Sam <sam@basx.dev>
parents: 572
diff changeset
19 shaders*: seq[Shader]
566
cc7ba46fe3c4 add: descriptors, better swapchain implementation
Sam <sam@basx.dev>
parents: 565
diff changeset
20 descriptorSetLayout*: DescriptorSetLayout
569
f4d8f0018670 did: reorganize code
Sam <sam@basx.dev>
parents: 568
diff changeset
21 descriptorPool*: DescriptorPool
f4d8f0018670 did: reorganize code
Sam <sam@basx.dev>
parents: 568
diff changeset
22 descriptorSets*: seq[DescriptorSet]
560
8de8a2102071 add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents:
diff changeset
23
583
90537a8887ae did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents: 575
diff changeset
24 func inputs*(pipeline: Pipeline): seq[ShaderAttribute] =
573
f31a9821ae1c did: real implementation of buffer and memory, getting closer to collect shit for drawing per pipeline
Sam <sam@basx.dev>
parents: 572
diff changeset
25 for shader in pipeline.shaders:
f31a9821ae1c did: real implementation of buffer and memory, getting closer to collect shit for drawing per pipeline
Sam <sam@basx.dev>
parents: 572
diff changeset
26 if shader.stage == VK_SHADER_STAGE_VERTEX_BIT:
f31a9821ae1c did: real implementation of buffer and memory, getting closer to collect shit for drawing per pipeline
Sam <sam@basx.dev>
parents: 572
diff changeset
27 return shader.inputs
560
8de8a2102071 add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents:
diff changeset
28
583
90537a8887ae did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents: 575
diff changeset
29 func uniforms*(pipeline: Pipeline): seq[ShaderAttribute] =
90537a8887ae did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents: 575
diff changeset
30 var uniformList: Table[string, ShaderAttribute]
90537a8887ae did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents: 575
diff changeset
31 for shader in pipeline.shaders:
90537a8887ae did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents: 575
diff changeset
32 for attribute in shader.uniforms:
90537a8887ae did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents: 575
diff changeset
33 if attribute.name in uniformList:
90537a8887ae did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents: 575
diff changeset
34 assert uniformList[attribute.name] == attribute
90537a8887ae did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents: 575
diff changeset
35 else:
90537a8887ae did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents: 575
diff changeset
36 uniformList[attribute.name] = attribute
90537a8887ae did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents: 575
diff changeset
37 result = uniformList.values.toSeq
90537a8887ae did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents: 575
diff changeset
38
653
ac7dfbd56cc6 add: textures now support in shader via scene data, also: improved config handling a bit, more to come
Sam <sam@basx.dev>
parents: 652
diff changeset
39 proc setupDescriptors*(pipeline: var Pipeline, buffers: seq[Buffer], textures: Table[string, Texture], inFlightFrames: int) =
588
008592db0442 did: big refactoring
Sam <sam@basx.dev>
parents: 583
diff changeset
40 assert pipeline.vk.valid
650
be6e0f89645a add: initial code for texture support, not finished, had to completely refactor how to handle material-data (ie scene-wide data, sorry if you ever read this
Sam <sam@basx.dev>
parents: 644
diff changeset
41 assert buffers.len == 0 or buffers.len == inFlightFrames
652
43240986f76d add: texture testing, not working yet, something update un-updated descritors (even though we write the descriptors man!)
Sam <sam@basx.dev>
parents: 651
diff changeset
42 assert pipeline.descriptorSets.len > 0
588
008592db0442 did: big refactoring
Sam <sam@basx.dev>
parents: 583
diff changeset
43
008592db0442 did: big refactoring
Sam <sam@basx.dev>
parents: 583
diff changeset
44 for i in 0 ..< inFlightFrames:
650
be6e0f89645a add: initial code for texture support, not finished, had to completely refactor how to handle material-data (ie scene-wide data, sorry if you ever read this
Sam <sam@basx.dev>
parents: 644
diff changeset
45 var offset = 0'u64
652
43240986f76d add: texture testing, not working yet, something update un-updated descritors (even though we write the descriptors man!)
Sam <sam@basx.dev>
parents: 651
diff changeset
46 # first descriptor is always uniform for globals, match should be better somehow
43240986f76d add: texture testing, not working yet, something update un-updated descritors (even though we write the descriptors man!)
Sam <sam@basx.dev>
parents: 651
diff changeset
47 assert pipeline.descriptorSets[i].layout.descriptors[0].thetype == Uniform
650
be6e0f89645a add: initial code for texture support, not finished, had to completely refactor how to handle material-data (ie scene-wide data, sorry if you ever read this
Sam <sam@basx.dev>
parents: 644
diff changeset
48 for descriptor in pipeline.descriptorSets[i].layout.descriptors.mitems:
be6e0f89645a add: initial code for texture support, not finished, had to completely refactor how to handle material-data (ie scene-wide data, sorry if you ever read this
Sam <sam@basx.dev>
parents: 644
diff changeset
49 if descriptor.thetype == Uniform:
be6e0f89645a add: initial code for texture support, not finished, had to completely refactor how to handle material-data (ie scene-wide data, sorry if you ever read this
Sam <sam@basx.dev>
parents: 644
diff changeset
50 let size = VkDeviceSize(descriptor.itemsize * descriptor.count)
be6e0f89645a add: initial code for texture support, not finished, had to completely refactor how to handle material-data (ie scene-wide data, sorry if you ever read this
Sam <sam@basx.dev>
parents: 644
diff changeset
51 descriptor.buffer = buffers[i]
be6e0f89645a add: initial code for texture support, not finished, had to completely refactor how to handle material-data (ie scene-wide data, sorry if you ever read this
Sam <sam@basx.dev>
parents: 644
diff changeset
52 descriptor.offset = offset
be6e0f89645a add: initial code for texture support, not finished, had to completely refactor how to handle material-data (ie scene-wide data, sorry if you ever read this
Sam <sam@basx.dev>
parents: 644
diff changeset
53 descriptor.size = size
be6e0f89645a add: initial code for texture support, not finished, had to completely refactor how to handle material-data (ie scene-wide data, sorry if you ever read this
Sam <sam@basx.dev>
parents: 644
diff changeset
54 offset += size
651
164bf40c664f add: uncomment some of the prepared texture code, nice interface for scene-global shader values (aka uniforms
Sam <sam@basx.dev>
parents: 650
diff changeset
55 elif descriptor.thetype == ImageSampler:
653
ac7dfbd56cc6 add: textures now support in shader via scene data, also: improved config handling a bit, more to come
Sam <sam@basx.dev>
parents: 652
diff changeset
56 if not (descriptor.name in textures):
652
43240986f76d add: texture testing, not working yet, something update un-updated descritors (even though we write the descriptors man!)
Sam <sam@basx.dev>
parents: 651
diff changeset
57 raise newException(Exception, "Missing shader texture in scene: " & descriptor.name)
653
ac7dfbd56cc6 add: textures now support in shader via scene data, also: improved config handling a bit, more to come
Sam <sam@basx.dev>
parents: 652
diff changeset
58 descriptor.imageview = textures[descriptor.name].imageView
ac7dfbd56cc6 add: textures now support in shader via scene data, also: improved config handling a bit, more to come
Sam <sam@basx.dev>
parents: 652
diff changeset
59 descriptor.sampler = textures[descriptor.name].sampler
588
008592db0442 did: big refactoring
Sam <sam@basx.dev>
parents: 583
diff changeset
60
589
b434feaf2b67 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 588
diff changeset
61 proc createPipeline*(device: Device, renderPass: VkRenderPass, vertexCode: ShaderCode, fragmentCode: ShaderCode, inFlightFrames: int, subpass = 0'u32): Pipeline =
575
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
62 assert renderPass.valid
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
63 assert device.vk.valid
589
b434feaf2b67 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 588
diff changeset
64
b434feaf2b67 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 588
diff changeset
65 var
b434feaf2b67 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 588
diff changeset
66 vertexShader = device.createShaderModule(vertexCode)
b434feaf2b67 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 588
diff changeset
67 fragmentShader = device.createShaderModule(fragmentCode)
575
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
68 assert vertexShader.stage == VK_SHADER_STAGE_VERTEX_BIT
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
69 assert fragmentShader.stage == VK_SHADER_STAGE_FRAGMENT_BIT
583
90537a8887ae did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents: 575
diff changeset
70 assert vertexShader.outputs == fragmentShader.inputs
90537a8887ae did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents: 575
diff changeset
71 assert vertexShader.uniforms == fragmentShader.uniforms
650
be6e0f89645a add: initial code for texture support, not finished, had to completely refactor how to handle material-data (ie scene-wide data, sorry if you ever read this
Sam <sam@basx.dev>
parents: 644
diff changeset
72 assert vertexShader.samplers == fragmentShader.samplers
575
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
73
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
74 result.device = device
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
75 result.shaders = @[vertexShader, fragmentShader]
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
76
583
90537a8887ae did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents: 575
diff changeset
77 var descriptors = @[
90537a8887ae did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents: 575
diff changeset
78 Descriptor(
650
be6e0f89645a add: initial code for texture support, not finished, had to completely refactor how to handle material-data (ie scene-wide data, sorry if you ever read this
Sam <sam@basx.dev>
parents: 644
diff changeset
79 name: "Uniforms",
be6e0f89645a add: initial code for texture support, not finished, had to completely refactor how to handle material-data (ie scene-wide data, sorry if you ever read this
Sam <sam@basx.dev>
parents: 644
diff changeset
80 thetype: Uniform,
575
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
81 count: 1,
583
90537a8887ae did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents: 575
diff changeset
82 stages: @[VK_SHADER_STAGE_VERTEX_BIT, VK_SHADER_STAGE_FRAGMENT_BIT],
90537a8887ae did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents: 575
diff changeset
83 itemsize: vertexShader.uniforms.size(),
90537a8887ae did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents: 575
diff changeset
84 ),
90537a8887ae did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents: 575
diff changeset
85 ]
650
be6e0f89645a add: initial code for texture support, not finished, had to completely refactor how to handle material-data (ie scene-wide data, sorry if you ever read this
Sam <sam@basx.dev>
parents: 644
diff changeset
86 for sampler in vertexShader.samplers:
be6e0f89645a add: initial code for texture support, not finished, had to completely refactor how to handle material-data (ie scene-wide data, sorry if you ever read this
Sam <sam@basx.dev>
parents: 644
diff changeset
87 descriptors.add Descriptor(
be6e0f89645a add: initial code for texture support, not finished, had to completely refactor how to handle material-data (ie scene-wide data, sorry if you ever read this
Sam <sam@basx.dev>
parents: 644
diff changeset
88 name: sampler.name,
be6e0f89645a add: initial code for texture support, not finished, had to completely refactor how to handle material-data (ie scene-wide data, sorry if you ever read this
Sam <sam@basx.dev>
parents: 644
diff changeset
89 thetype: ImageSampler,
be6e0f89645a add: initial code for texture support, not finished, had to completely refactor how to handle material-data (ie scene-wide data, sorry if you ever read this
Sam <sam@basx.dev>
parents: 644
diff changeset
90 count: 1,
be6e0f89645a add: initial code for texture support, not finished, had to completely refactor how to handle material-data (ie scene-wide data, sorry if you ever read this
Sam <sam@basx.dev>
parents: 644
diff changeset
91 stages: @[VK_SHADER_STAGE_VERTEX_BIT, VK_SHADER_STAGE_FRAGMENT_BIT],
be6e0f89645a add: initial code for texture support, not finished, had to completely refactor how to handle material-data (ie scene-wide data, sorry if you ever read this
Sam <sam@basx.dev>
parents: 644
diff changeset
92 itemsize: 0,
be6e0f89645a add: initial code for texture support, not finished, had to completely refactor how to handle material-data (ie scene-wide data, sorry if you ever read this
Sam <sam@basx.dev>
parents: 644
diff changeset
93 )
575
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
94 result.descriptorSetLayout = device.createDescriptorSetLayout(descriptors)
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
95
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
96 # TODO: Push constants
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
97 # var pushConstant = VkPushConstantRange(
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
98 # stageFlags: toBits shaderStage,
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
99 # offset: 0,
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
100 # size: 0,
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
101 # )
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
102 var descriptorSetLayouts: seq[VkDescriptorSetLayout] = @[result.descriptorSetLayout.vk]
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
103 # var pushConstants: seq[VkPushConstantRange] = @[pushConstant]
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
104 var pipelineLayoutInfo = VkPipelineLayoutCreateInfo(
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
105 sType: VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
106 setLayoutCount: uint32(descriptorSetLayouts.len),
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
107 pSetLayouts: descriptorSetLayouts.toCPointer,
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
108 # pushConstantRangeCount: uint32(pushConstants.len),
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
109 # pPushConstantRanges: pushConstants.toCPointer,
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
110 )
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
111 checkVkResult vkCreatePipelineLayout(device.vk, addr(pipelineLayoutInfo), nil, addr(result.layout))
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
112
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
113 var
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
114 bindings: seq[VkVertexInputBindingDescription]
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
115 attributes: seq[VkVertexInputAttributeDescription]
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
116 vertexInputInfo = vertexShader.getVertexInputInfo(bindings, attributes)
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
117 inputAssembly = VkPipelineInputAssemblyStateCreateInfo(
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
118 sType: VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO,
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
119 topology: VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST,
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
120 primitiveRestartEnable: VK_FALSE,
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
121 )
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
122 viewportState = VkPipelineViewportStateCreateInfo(
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
123 sType: VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO,
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
124 viewportCount: 1,
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
125 scissorCount: 1,
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
126 )
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
127 rasterizer = VkPipelineRasterizationStateCreateInfo(
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
128 sType: VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO,
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
129 depthClampEnable: VK_FALSE,
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
130 rasterizerDiscardEnable: VK_FALSE,
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
131 polygonMode: VK_POLYGON_MODE_FILL,
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
132 lineWidth: 1.0,
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
133 cullMode: toBits [VK_CULL_MODE_BACK_BIT],
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
134 frontFace: VK_FRONT_FACE_CLOCKWISE,
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
135 depthBiasEnable: VK_FALSE,
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
136 depthBiasConstantFactor: 0.0,
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
137 depthBiasClamp: 0.0,
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
138 depthBiasSlopeFactor: 0.0,
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
139 )
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
140 multisampling = VkPipelineMultisampleStateCreateInfo(
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
141 sType: VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO,
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
142 sampleShadingEnable: VK_FALSE,
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
143 rasterizationSamples: VK_SAMPLE_COUNT_1_BIT,
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
144 minSampleShading: 1.0,
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
145 pSampleMask: nil,
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
146 alphaToCoverageEnable: VK_FALSE,
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
147 alphaToOneEnable: VK_FALSE,
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
148 )
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
149 colorBlendAttachment = VkPipelineColorBlendAttachmentState(
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
150 colorWriteMask: toBits [VK_COLOR_COMPONENT_R_BIT, VK_COLOR_COMPONENT_G_BIT, VK_COLOR_COMPONENT_B_BIT, VK_COLOR_COMPONENT_A_BIT],
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
151 blendEnable: VK_TRUE,
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
152 srcColorBlendFactor: VK_BLEND_FACTOR_SRC_ALPHA,
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
153 dstColorBlendFactor: VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA,
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
154 colorBlendOp: VK_BLEND_OP_ADD,
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
155 srcAlphaBlendFactor: VK_BLEND_FACTOR_ONE,
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
156 dstAlphaBlendFactor: VK_BLEND_FACTOR_ZERO,
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
157 alphaBlendOp: VK_BLEND_OP_ADD,
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
158 )
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
159 colorBlending = VkPipelineColorBlendStateCreateInfo(
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
160 sType: VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO,
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
161 logicOpEnable: false,
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
162 attachmentCount: 1,
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
163 pAttachments: addr(colorBlendAttachment),
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
164 )
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
165 dynamicStates = @[VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR]
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
166 dynamicState = VkPipelineDynamicStateCreateInfo(
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
167 sType: VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO,
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
168 dynamicStateCount: uint32(dynamicStates.len),
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
169 pDynamicStates: dynamicStates.toCPointer,
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
170 )
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
171 stages = @[vertexShader.getPipelineInfo(), fragmentShader.getPipelineInfo()]
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
172 createInfo = VkGraphicsPipelineCreateInfo(
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
173 sType: VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO,
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
174 stageCount: uint32(stages.len),
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
175 pStages: stages.toCPointer,
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
176 pVertexInputState: addr(vertexInputInfo),
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
177 pInputAssemblyState: addr(inputAssembly),
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
178 pViewportState: addr(viewportState),
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
179 pRasterizationState: addr(rasterizer),
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
180 pMultisampleState: addr(multisampling),
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
181 pDepthStencilState: nil,
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
182 pColorBlendState: addr(colorBlending),
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
183 pDynamicState: addr(dynamicState),
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
184 layout: result.layout,
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
185 renderPass: renderPass,
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
186 subpass: subpass,
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
187 basePipelineHandle: VkPipeline(0),
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
188 basePipelineIndex: -1,
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
189 )
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
190 checkVkResult vkCreateGraphicsPipelines(
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
191 device.vk,
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
192 VkPipelineCache(0),
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
193 1,
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
194 addr(createInfo),
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
195 nil,
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
196 addr(result.vk)
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
197 )
650
be6e0f89645a add: initial code for texture support, not finished, had to completely refactor how to handle material-data (ie scene-wide data, sorry if you ever read this
Sam <sam@basx.dev>
parents: 644
diff changeset
198 var poolsizes = @[(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, uint32(inFlightFrames))]
be6e0f89645a add: initial code for texture support, not finished, had to completely refactor how to handle material-data (ie scene-wide data, sorry if you ever read this
Sam <sam@basx.dev>
parents: 644
diff changeset
199 if vertexShader.samplers.len > 0:
be6e0f89645a add: initial code for texture support, not finished, had to completely refactor how to handle material-data (ie scene-wide data, sorry if you ever read this
Sam <sam@basx.dev>
parents: 644
diff changeset
200 poolsizes.add (VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, uint32(inFlightFrames * vertexShader.samplers.len))
be6e0f89645a add: initial code for texture support, not finished, had to completely refactor how to handle material-data (ie scene-wide data, sorry if you ever read this
Sam <sam@basx.dev>
parents: 644
diff changeset
201
be6e0f89645a add: initial code for texture support, not finished, had to completely refactor how to handle material-data (ie scene-wide data, sorry if you ever read this
Sam <sam@basx.dev>
parents: 644
diff changeset
202 result.descriptorPool = result.device.createDescriptorSetPool(poolsizes)
575
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
203 result.descriptorSets = result.descriptorPool.allocateDescriptorSet(result.descriptorSetLayout, inFlightFrames)
583
90537a8887ae did: implement uniforms, some refactoring
Sam <sam@basx.dev>
parents: 575
diff changeset
204 discard result.uniforms # just for assertion
588
008592db0442 did: big refactoring
Sam <sam@basx.dev>
parents: 583
diff changeset
205
575
eaedc0369c38 yay: first triangle rendering with new engine implmentation
Sam <sam@basx.dev>
parents: 573
diff changeset
206
560
8de8a2102071 add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents:
diff changeset
207 proc destroy*(pipeline: var Pipeline) =
8de8a2102071 add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents:
diff changeset
208 assert pipeline.device.vk.valid
8de8a2102071 add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents:
diff changeset
209 assert pipeline.vk.valid
561
4a9fc283c3af add: initial version of better shader-shit
Sam <sam@basx.dev>
parents: 560
diff changeset
210 assert pipeline.layout.valid
566
cc7ba46fe3c4 add: descriptors, better swapchain implementation
Sam <sam@basx.dev>
parents: 565
diff changeset
211 assert pipeline.descriptorSetLayout.vk.valid
588
008592db0442 did: big refactoring
Sam <sam@basx.dev>
parents: 583
diff changeset
212
569
f4d8f0018670 did: reorganize code
Sam <sam@basx.dev>
parents: 568
diff changeset
213 if pipeline.descriptorPool.vk.valid:
f4d8f0018670 did: reorganize code
Sam <sam@basx.dev>
parents: 568
diff changeset
214 pipeline.descriptorPool.destroy()
589
b434feaf2b67 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 588
diff changeset
215
b434feaf2b67 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 588
diff changeset
216 for shader in pipeline.shaders.mitems:
b434feaf2b67 did: finish refactoring of render pipeline, yipi! :)
Sam <sam@basx.dev>
parents: 588
diff changeset
217 shader.destroy()
566
cc7ba46fe3c4 add: descriptors, better swapchain implementation
Sam <sam@basx.dev>
parents: 565
diff changeset
218 pipeline.descriptorSetLayout.destroy()
561
4a9fc283c3af add: initial version of better shader-shit
Sam <sam@basx.dev>
parents: 560
diff changeset
219 pipeline.device.vk.vkDestroyPipelineLayout(pipeline.layout, nil)
560
8de8a2102071 add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents:
diff changeset
220 pipeline.device.vk.vkDestroyPipeline(pipeline.vk, nil)
566
cc7ba46fe3c4 add: descriptors, better swapchain implementation
Sam <sam@basx.dev>
parents: 565
diff changeset
221 pipeline.descriptorSetLayout.reset()
561
4a9fc283c3af add: initial version of better shader-shit
Sam <sam@basx.dev>
parents: 560
diff changeset
222 pipeline.layout.reset()
560
8de8a2102071 add: vertex and (initial) shader types and methods
Sam <sam@basx.dev>
parents:
diff changeset
223 pipeline.vk.reset()