summaryrefslogtreecommitdiff
path: root/src/gpu
diff options
context:
space:
mode:
authorskal <pascal.massimino@gmail.com>2026-02-09 14:31:43 +0100
committerskal <pascal.massimino@gmail.com>2026-02-09 14:31:43 +0100
commitbd2ca805eeddd2688a8f1b3ed5ec8ed1677748fc (patch)
treee2b5311631354e9328ed5ce91a4261cbce7c0b93 /src/gpu
parent9bb5fd64776ac8a7e4b012ac2de340ddfa09a2c9 (diff)
refactor: Generic sampler system for composites
Replace hardcoded linear_sampler_ with configurable sampler map. - SamplerType enum (LinearClamp, LinearRepeat, NearestClamp, NearestRepeat) - get_or_create_sampler() for lazy sampler creation - Default to LinearClamp for backward compatibility Eliminates hardcoded assumptions, more flexible for future use cases.
Diffstat (limited to 'src/gpu')
-rw-r--r--src/gpu/texture_manager.cc79
-rw-r--r--src/gpu/texture_manager.h16
2 files changed, 71 insertions, 24 deletions
diff --git a/src/gpu/texture_manager.cc b/src/gpu/texture_manager.cc
index 7aeb67a..dfa6315 100644
--- a/src/gpu/texture_manager.cc
+++ b/src/gpu/texture_manager.cc
@@ -21,20 +21,6 @@
void TextureManager::init(WGPUDevice device, WGPUQueue queue) {
device_ = device;
queue_ = queue;
-
-#if !defined(STRIP_GPU_COMPOSITE)
- // Create linear sampler for composite shaders
- WGPUSamplerDescriptor sampler_desc = {};
- sampler_desc.addressModeU = WGPUAddressMode_ClampToEdge;
- sampler_desc.addressModeV = WGPUAddressMode_ClampToEdge;
- sampler_desc.magFilter = WGPUFilterMode_Linear;
- sampler_desc.minFilter = WGPUFilterMode_Linear;
- sampler_desc.mipmapFilter = WGPUMipmapFilterMode_Linear;
- sampler_desc.lodMinClamp = 0.0f;
- sampler_desc.lodMaxClamp = 1.0f;
- sampler_desc.maxAnisotropy = 1;
- linear_sampler_ = wgpuDeviceCreateSampler(device_, &sampler_desc);
-#endif
}
void TextureManager::shutdown() {
@@ -52,10 +38,12 @@ void TextureManager::shutdown() {
compute_pipelines_.clear();
#if !defined(STRIP_GPU_COMPOSITE)
- if (linear_sampler_) {
- wgpuSamplerRelease(linear_sampler_);
- linear_sampler_ = nullptr;
+ for (auto& pair : samplers_) {
+ if (pair.second) {
+ wgpuSamplerRelease(pair.second);
+ }
}
+ samplers_.clear();
#endif
}
@@ -469,10 +457,58 @@ void TextureManager::create_gpu_grid_texture(
}
#if !defined(STRIP_GPU_COMPOSITE)
+WGPUSampler TextureManager::get_or_create_sampler(SamplerType type) {
+ auto it = samplers_.find(type);
+ if (it != samplers_.end()) {
+ return it->second;
+ }
+
+ WGPUSamplerDescriptor desc = {};
+ desc.lodMinClamp = 0.0f;
+ desc.lodMaxClamp = 1.0f;
+ desc.maxAnisotropy = 1;
+
+ switch (type) {
+ case SamplerType::LinearClamp:
+ desc.addressModeU = WGPUAddressMode_ClampToEdge;
+ desc.addressModeV = WGPUAddressMode_ClampToEdge;
+ desc.magFilter = WGPUFilterMode_Linear;
+ desc.minFilter = WGPUFilterMode_Linear;
+ desc.mipmapFilter = WGPUMipmapFilterMode_Linear;
+ break;
+ case SamplerType::LinearRepeat:
+ desc.addressModeU = WGPUAddressMode_Repeat;
+ desc.addressModeV = WGPUAddressMode_Repeat;
+ desc.magFilter = WGPUFilterMode_Linear;
+ desc.minFilter = WGPUFilterMode_Linear;
+ desc.mipmapFilter = WGPUMipmapFilterMode_Linear;
+ break;
+ case SamplerType::NearestClamp:
+ desc.addressModeU = WGPUAddressMode_ClampToEdge;
+ desc.addressModeV = WGPUAddressMode_ClampToEdge;
+ desc.magFilter = WGPUFilterMode_Nearest;
+ desc.minFilter = WGPUFilterMode_Nearest;
+ desc.mipmapFilter = WGPUMipmapFilterMode_Nearest;
+ break;
+ case SamplerType::NearestRepeat:
+ desc.addressModeU = WGPUAddressMode_Repeat;
+ desc.addressModeV = WGPUAddressMode_Repeat;
+ desc.magFilter = WGPUFilterMode_Nearest;
+ desc.minFilter = WGPUFilterMode_Nearest;
+ desc.mipmapFilter = WGPUMipmapFilterMode_Nearest;
+ break;
+ }
+
+ WGPUSampler sampler = wgpuDeviceCreateSampler(device_, &desc);
+ samplers_[type] = sampler;
+ return sampler;
+}
+
void TextureManager::dispatch_composite(
const std::string& func_name, WGPUTexture target,
const GpuProceduralParams& params, const void* uniform_data,
- size_t uniform_size, const std::vector<WGPUTextureView>& input_views) {
+ size_t uniform_size, const std::vector<WGPUTextureView>& input_views,
+ SamplerType sampler_type) {
auto it = compute_pipelines_.find(func_name);
if (it == compute_pipelines_.end()) {
return; // Pipeline not created yet
@@ -521,7 +557,7 @@ void TextureManager::dispatch_composite(
// Binding N: Sampler
if (num_inputs > 0) {
bg_entries[2 + num_inputs].binding = 2 + num_inputs;
- bg_entries[2 + num_inputs].sampler = linear_sampler_;
+ bg_entries[2 + num_inputs].sampler = get_or_create_sampler(sampler_type);
}
// Create bind group layout (must match pipeline)
@@ -593,7 +629,8 @@ void TextureManager::dispatch_composite(
void TextureManager::create_gpu_composite_texture(
const std::string& name, const std::string& shader_func,
const char* shader_code, const void* uniform_data, size_t uniform_size,
- int width, int height, const std::vector<std::string>& input_names) {
+ int width, int height, const std::vector<std::string>& input_names,
+ SamplerType sampler) {
// Create pipeline if needed
get_or_create_compute_pipeline(shader_func, shader_code, uniform_size,
(int)input_names.size());
@@ -625,7 +662,7 @@ void TextureManager::create_gpu_composite_texture(
// Dispatch composite shader
GpuProceduralParams params = {width, height, nullptr, 0};
dispatch_composite(shader_func, texture, params, uniform_data, uniform_size,
- input_views);
+ input_views, sampler);
// Create view
WGPUTextureViewDescriptor view_desc = {};
diff --git a/src/gpu/texture_manager.h b/src/gpu/texture_manager.h
index 86d1f63..5a2b9f8 100644
--- a/src/gpu/texture_manager.h
+++ b/src/gpu/texture_manager.h
@@ -52,6 +52,13 @@ class TextureManager {
const GpuProceduralParams& params);
#if !defined(STRIP_GPU_COMPOSITE)
+ enum class SamplerType {
+ LinearClamp,
+ LinearRepeat,
+ NearestClamp,
+ NearestRepeat
+ };
+
// GPU composite generation (multi-input textures)
void create_gpu_composite_texture(const std::string& name,
const std::string& shader_func,
@@ -59,7 +66,8 @@ class TextureManager {
const void* uniform_data,
size_t uniform_size,
int width, int height,
- const std::vector<std::string>& input_names);
+ const std::vector<std::string>& input_names,
+ SamplerType sampler = SamplerType::LinearClamp);
#endif
#if !defined(STRIP_ALL)
@@ -91,7 +99,8 @@ class TextureManager {
void dispatch_composite(const std::string& func_name, WGPUTexture target,
const GpuProceduralParams& params,
const void* uniform_data, size_t uniform_size,
- const std::vector<WGPUTextureView>& input_views);
+ const std::vector<WGPUTextureView>& input_views,
+ SamplerType sampler_type);
#endif
WGPUDevice device_;
@@ -99,6 +108,7 @@ class TextureManager {
std::map<std::string, GpuTexture> textures_;
std::map<std::string, ComputePipelineInfo> compute_pipelines_;
#if !defined(STRIP_GPU_COMPOSITE)
- WGPUSampler linear_sampler_;
+ WGPUSampler get_or_create_sampler(SamplerType type);
+ std::map<SamplerType, WGPUSampler> samplers_;
#endif
};