comparison src/semicongine/vulkan/renderpass.nim @ 783:893ec0fbfd44

add: first, incomplete version of material use
author Sam <sam@basx.dev>
date Sat, 19 Aug 2023 01:10:42 +0700
parents b6950ea89b37
children 451b7ccfe722
comparison
equal deleted inserted replaced
782:b6950ea89b37 783:893ec0fbfd44
1 import std/options 1 import std/options
2 import std/tables
2 3
3 import ../core 4 import ../core
4 import ./device 5 import ./device
5 import ./physicaldevice 6 import ./physicaldevice
6 import ./pipeline 7 import ./pipeline
10 type 11 type
11 Subpass* = object 12 Subpass* = object
12 clearColor*: Vec4f 13 clearColor*: Vec4f
13 pipelineBindPoint*: VkPipelineBindPoint 14 pipelineBindPoint*: VkPipelineBindPoint
14 flags: VkSubpassDescriptionFlags 15 flags: VkSubpassDescriptionFlags
15 inputs: seq[VkAttachmentReference]
16 outputs: seq[VkAttachmentReference] 16 outputs: seq[VkAttachmentReference]
17 resolvers: seq[VkAttachmentReference]
18 depthStencil: Option[VkAttachmentReference] 17 depthStencil: Option[VkAttachmentReference]
19 preserves: seq[uint32] 18 pipelines*: Table[string, Pipeline]
20 pipelines*: seq[Pipeline]
21 RenderPass* = object 19 RenderPass* = object
22 vk*: VkRenderPass 20 vk*: VkRenderPass
23 device*: Device 21 device*: Device
24 subpasses*: seq[Subpass] 22 subpasses*: seq[Subpass]
25 23
37 var subpassesList: seq[VkSubpassDescription] 35 var subpassesList: seq[VkSubpassDescription]
38 for subpass in pSubpasses.mitems: 36 for subpass in pSubpasses.mitems:
39 subpassesList.add VkSubpassDescription( 37 subpassesList.add VkSubpassDescription(
40 flags: subpass.flags, 38 flags: subpass.flags,
41 pipelineBindPoint: subpass.pipelineBindPoint, 39 pipelineBindPoint: subpass.pipelineBindPoint,
42 inputAttachmentCount: uint32(subpass.inputs.len), 40 inputAttachmentCount: 0,
43 pInputAttachments: subpass.inputs.toCPointer, 41 pInputAttachments: nil,
44 colorAttachmentCount: uint32(subpass.outputs.len), 42 colorAttachmentCount: uint32(subpass.outputs.len),
45 pColorAttachments: subpass.outputs.toCPointer, 43 pColorAttachments: subpass.outputs.toCPointer,
46 pResolveAttachments: subpass.resolvers.toCPointer, 44 pResolveAttachments: nil,
47 pDepthStencilAttachment: if subpass.depthStencil.isSome: addr(subpass.depthStencil.get) else: nil, 45 pDepthStencilAttachment: if subpass.depthStencil.isSome: addr(subpass.depthStencil.get) else: nil,
48 preserveAttachmentCount: uint32(subpass.preserves.len), 46 preserveAttachmentCount: 0,
49 pPreserveAttachments: subpass.preserves.toCPointer, 47 pPreserveAttachments: nil,
50 ) 48 )
51 49
52 var createInfo = VkRenderPassCreateInfo( 50 var createInfo = VkRenderPassCreateInfo(
53 sType: VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO, 51 sType: VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
54 attachmentCount: uint32(pAttachments.len), 52 attachmentCount: uint32(pAttachments.len),
62 result.subpasses = pSubpasses 60 result.subpasses = pSubpasses
63 checkVkResult device.vk.vkCreateRenderPass(addr(createInfo), nil, addr(result.vk)) 61 checkVkResult device.vk.vkCreateRenderPass(addr(createInfo), nil, addr(result.vk))
64 62
65 proc simpleForwardRenderPass*( 63 proc simpleForwardRenderPass*(
66 device: Device, 64 device: Device,
67 shaderConfiguration: ShaderConfiguration, 65 shaders: Table[string, ShaderConfiguration],
68 inFlightFrames=2, 66 inFlightFrames=2,
69 format=VK_FORMAT_UNDEFINED ,
70 clearColor=Vec4f([0.8'f32, 0.8'f32, 0.8'f32, 1'f32]) 67 clearColor=Vec4f([0.8'f32, 0.8'f32, 0.8'f32, 1'f32])
71 ): RenderPass = 68 ): RenderPass =
69 # TODO: check wether materials are compatible with the assigned shaders
70 {.warning: "Need to implement material -> shader compatability" .}
71
72 assert device.vk.valid 72 assert device.vk.valid
73 assert shaderConfiguration.outputs.len == 1 73 for shaderconfig in shaders.values:
74 var theformat = format 74 assert shaderconfig.outputs.len == 1
75 if theformat == VK_FORMAT_UNDEFINED:
76 theformat = device.physicalDevice.getSurfaceFormats().filterSurfaceFormat().format
77 var 75 var
78 attachments = @[VkAttachmentDescription( 76 attachments = @[VkAttachmentDescription(
79 format: theformat, 77 format: device.physicalDevice.getSurfaceFormats().filterSurfaceFormat().format,
80 samples: VK_SAMPLE_COUNT_1_BIT, 78 samples: VK_SAMPLE_COUNT_1_BIT,
81 loadOp: VK_ATTACHMENT_LOAD_OP_CLEAR, 79 loadOp: VK_ATTACHMENT_LOAD_OP_CLEAR,
82 storeOp: VK_ATTACHMENT_STORE_OP_STORE, 80 storeOp: VK_ATTACHMENT_STORE_OP_STORE,
83 stencilLoadOp: VK_ATTACHMENT_LOAD_OP_DONT_CARE, 81 stencilLoadOp: VK_ATTACHMENT_LOAD_OP_DONT_CARE,
84 stencilStoreOp: VK_ATTACHMENT_STORE_OP_DONT_CARE, 82 stencilStoreOp: VK_ATTACHMENT_STORE_OP_DONT_CARE,
100 srcAccessMask: VkAccessFlags(0), 98 srcAccessMask: VkAccessFlags(0),
101 dstStageMask: toBits [VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT], 99 dstStageMask: toBits [VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT],
102 dstAccessMask: toBits [VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT], 100 dstAccessMask: toBits [VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT],
103 )] 101 )]
104 result = device.createRenderPass(attachments=attachments, subpasses=subpasses, dependencies=dependencies) 102 result = device.createRenderPass(attachments=attachments, subpasses=subpasses, dependencies=dependencies)
105 result.subpasses[0].pipelines.add device.createPipeline(result.vk, shaderConfiguration, inFlightFrames, 0) 103 for material, shaderconfig in shaders.pairs:
104 result.subpasses[0].pipelines[material] = device.createPipeline(result.vk, shaderconfig, inFlightFrames, 0)
105
106 106
107 proc beginRenderCommands*(commandBuffer: VkCommandBuffer, renderpass: RenderPass, framebuffer: Framebuffer) = 107 proc beginRenderCommands*(commandBuffer: VkCommandBuffer, renderpass: RenderPass, framebuffer: Framebuffer) =
108 assert commandBuffer.valid 108 assert commandBuffer.valid
109 assert renderpass.vk.valid 109 assert renderpass.vk.valid
110 assert framebuffer.vk.valid 110 assert framebuffer.vk.valid
157 proc destroy*(renderPass: var RenderPass) = 157 proc destroy*(renderPass: var RenderPass) =
158 assert renderPass.device.vk.valid 158 assert renderPass.device.vk.valid
159 assert renderPass.vk.valid 159 assert renderPass.vk.valid
160 renderPass.device.vk.vkDestroyRenderPass(renderPass.vk, nil) 160 renderPass.device.vk.vkDestroyRenderPass(renderPass.vk, nil)
161 renderPass.vk.reset 161 renderPass.vk.reset
162 for subpass in renderPass.subpasses.mitems: 162 for i in 0 ..< renderPass.subpasses.len:
163 for pipeline in subpass.pipelines.mitems: 163 for pipeline in renderPass.subpasses[i].pipelines.mvalues:
164 pipeline.destroy() 164 pipeline.destroy()
165 renderPass.subpasses = @[] 165 renderPass.subpasses = @[]