dymokomi/luce-gpu
Source of release 0.5.6
0.5.6 / src / gpu / vulkan / shader.lucb
#==============================================================================================
#
# shader - Client shader modules and their per-format pipelines
#
# DESCRIPTION:
# A client fragment shader is one VkShaderModule. A client pipeline remembers its blend
# mode and target and builds one VkPipeline per color format it is actually drawn into,
# since a presentation surface's format is known only once its swapchain exists.
#
#==============================================================================================
struct VulkanShader:
var module: u64
struct VulkanPipelineEntry:
var format: u32
# The vertex stage (client_variant): 0 ordinary, 1 instanced, 2 quads.
var variant: usize
var pipeline: u64
struct VulkanPipeline:
var shader: VulkanShader*
var blend: Blend
var format: Format?
# Depth tested without writing depth (Pipeline.create's depth_test).
var depth_test: bool
var entries: VulkanPipelineEntry[18]
var count: usize
func vulkan_shader_create(device: VulkanDevice*, spirv: const u32[]) -> VulkanShader*!:
try vulkan_drawing(device)
let state = try new VulkanShader() in memory.heap
errdefer free(state) in memory.heap
state.module = try vulkan_shader(device, spirv)
return state
func vulkan_shader_destroy(device: VulkanDevice*, state: VulkanShader*):
_ = vkDeviceWaitIdle(device.device)
if state.module != 0: vkDestroyShaderModule(device.device, state.module, none)
free(state) in memory.heap
func vulkan_pipeline_create(device: VulkanDevice*, shader: VulkanShader*, blend: Blend, pixel_format: Format?, depth_test: bool) -> VulkanPipeline*!:
let state = try new VulkanPipeline(shader = shader, blend = blend, format = pixel_format, depth_test = depth_test) in memory.heap
errdefer vulkan_pipeline_destroy(device, state)
# A texture target's format is known now; build eagerly so failures surface at creation.
if let target = pixel_format:
_ = try vulkan_pipeline_for(device, state, vulkan_native_format(target))
return state
func vulkan_pipeline_destroy(device: VulkanDevice*, state: VulkanPipeline*):
_ = vkDeviceWaitIdle(device.device)
for entry in state.entries[..<state.count]:
if entry.pipeline != 0: vkDestroyPipeline(device.device, entry.pipeline, none)
free(state) in memory.heap
## The client pipeline compiled for color attachments of `format`, for
## for the vertex stage `variant` (client_variant).
func vulkan_pipeline_for(device: VulkanDevice*, state: VulkanPipeline*, pixel_format: u32, variant: usize = 0) -> u64!:
for entry in state.entries[..<state.count]:
if entry.format == pixel_format and entry.variant == variant:
return entry.pipeline
if state.count == state.entries.length:
error(failed, "a Vulkan pipeline was drawn into too many color formats")
let formats = try vulkan_format_state(device, pixel_format)
let pipeline = try vulkan_build_pipeline(device, formats.pass_texture, state.shader.module, state.blend, false, instanced = variant == 1,
depth_read = state.depth_test, quads = variant == 2)
state.entries[state.count] = VulkanPipelineEntry(format = pixel_format, variant = variant, pipeline = pipeline)
state.count += 1
return pipeline