class CrShader::ComputeHelper

Overview

============================================================================= ComputeHelper - High-Level Godot 4 RenderingDevice Compute Orchestration

Eliminates low-level RenderingDevice boilerplate for compiling GLSL 450 compute kernels to SPIR-V, managing pipeline states, sampler states, multi-set uniform descriptors, push constants, and workgroup dispatches.

Defined in:

crshader/compute/compute_helper.cr

Constant Summary

RD_BRIDGE_GDSCRIPT = "extends RefCounted\nvar _uniforms: Array[RDUniform] = []\n\nfunc clear_uniforms() -> void:\n\t_uniforms.clear()\n\nfunc push_uniform(u: RDUniform) -> void:\n\t_uniforms.push_back(u)\n\nfunc create_uniform_set(rd: RenderingDevice, shader_id: int, set_idx: int) -> int:\n\tvar rid := rd.uniform_set_create(_uniforms, rid_from_int64(shader_id), set_idx)\n\t_uniforms.clear()\n\treturn rid.get_id()\n\nfunc create_ubo(rd: RenderingDevice, size_bytes: int, data: PackedByteArray) -> int:\n\treturn rd.uniform_buffer_create(size_bytes, data).get_id()\n\nfunc update_buf(rd: RenderingDevice, buf_id: int, offset: int, size_bytes: int, data: PackedByteArray) -> int:\n\treturn rd.buffer_update(rid_from_int64(buf_id), offset, size_bytes, data)\n\nfunc create_ssbo_data(rd: RenderingDevice, size_bytes: int, data: PackedByteArray) -> int:\n\treturn rd.storage_buffer_create(size_bytes, data).get_id()\n\nfunc create_ssbo_empty(rd: RenderingDevice, size_bytes: int) -> int:\n\treturn rd.storage_buffer_create(size_bytes).get_id()\n\nfunc set_push_const(rd: RenderingDevice, compute_list: int, data: PackedByteArray, size_bytes: int) -> void:\n\trd.compute_list_set_push_constant(compute_list, data, size_bytes)"
SHADER_STAGE_COMPUTE = 4_i64
UNIFORM_TYPE_IMAGE = 3
UNIFORM_TYPE_SAMPLER = 0

Exact Godot 4.x RenderingDevice::UniformType values

UNIFORM_TYPE_SAMPLER_WITH_TEXTURE = 1
UNIFORM_TYPE_STORAGE_BUFFER = 8
UNIFORM_TYPE_TEXTURE = 2
UNIFORM_TYPE_UNIFORM_BUFFER = 7

Constructors

Instance Method Summary

Constructor Detail

def self.create(shader_rid : Int64, rd : Godot::RenderingDevice | Nil = nil) : ComputeHelper #

Factory method creating helper from a compiled shader RID


def self.new(rd : Godot::RenderingDevice | Nil = nil) #

Instance Method Detail

def bind_image(binding : Int32, texture_rid : Int64, set_index : Int32 = 0) : Void #

Binds an image2D storage image (UniformTypeImage = 3)


def bind_sampler_with_texture(binding : Int32, texture_rid : Int64, linear : Bool = false, set_index : Int32 = 0) : Void #

Binds a combined sampler2D (UniformTypeSamplerWithTexture = 1), required for depth & normal_roughness buffers


def bind_storage_buffer(binding : Int32, buffer_rid : Int64, set_index : Int32 = 0) : Void #

Binds a storage buffer (UniformTypeStorageBuffer = 8)


def bind_texture(binding : Int32, texture_rid : Int64, set_index : Int32 = 0) : Void #

Binds a raw texture without sampler (UniformTypeTexture = 2)


def bind_uniform_buffer(binding : Int32, ubo_rid : Int64, set_index : Int32 = 0) : Void #

Binds a Uniform Buffer Object (UniformTypeUniformBuffer = 7)


def bindings : Hash(Int32, BoundUniform) #

Backwards-compatible accessor for set 0 bindings


def cleanup_all : Void #

def cleanup_pipeline(free_shader : Bool = false) : Void #

Cleans up GPU pipeline, uniform sets, samplers, and owned shader/buffer RIDs


def compile_glsl(glsl_source : String, name : String = "CrShaderCompute") : Int64 #

Compiles GLSL 450 compute source directly to SPIR-V on RenderingDevice and creates pipeline


def create_storage_buffer(byte_size : Int32, data : Bytes | Nil = nil, binding : Int32 | Nil = nil, set_index : Int32 = 0) : Int64 #

Allocates a GPU storage buffer of byte_size bytes on the RenderingDevice


def dirty_sets : Set(Int32) #

def dirty_sets=(dirty_sets : Set(Int32)) #

def dispatch(width : Int32, height : Int32, depth : Int32 = 1, local_x : Int32 = 8, local_y : Int32 = 8, local_z : Int32 = 1) : Bool #

Dispatches the compute shader over screen / volume dimensions


def dispatch_1d(element_count : Int32, local_x : Int32 = 64) : Bool #

Dispatches a 1D compute kernel over element_count items


def ensure_linear_sampler : Int64 #

def ensure_nearest_sampler : Int64 #

def ensure_uniform_buffer(existing_rid : Int64, bytes : Bytes, binding : Int32, set_index : Int32 = 0) : Int64 #

Allocates or updates a GPU Uniform Buffer Object (UBO)


def finalize #

def last_compile_error : String #

def pipeline_rid : Int64 #

def pipeline_rid=(pipeline_rid : Int64) #

def push_constant_bytes : Bytes | Nil #

Cached push constant payload bytes for the next dispatch


def push_constant_bytes=(push_constant_bytes : Bytes | Nil) #

Cached push constant payload bytes for the next dispatch


def rd : Godot::RenderingDevice | Nil #

def rd=(rd : Godot::RenderingDevice | Nil) #

def set_bindings : Hash(Int32, Hash(Int32, BoundUniform)) #

def set_bindings=(set_bindings : Hash(Int32, Hash(Int32, BoundUniform))) #

def set_push_constant(bytes : Bytes) : Void #

Sets push constant bytes (aligned to 16 bytes as required by Vulkan / RenderingDevice)


def set_rids : Hash(Int32, Int64) #

def set_rids=(set_rids : Hash(Int32, Int64)) #

def set_shader(shader_rid : Int64, owned : Bool = false) : Void #

def shader_rid : Int64 #

def shader_rid=(shader_rid : Int64) #

def sync : Void #

Syncs execution on local RenderingDevices


def uniform_set_dirty : Bool #

def uniform_set_dirty=(uniform_set_dirty : Bool) #

def uniform_set_rid : Int64 #

def uniform_set_rid=(uniform_set_rid : Int64) #

def update_uniform_set(set_index : Int32 = 0) : Bool #

Rebuilds the RDUniform set for set_index if bindings changed