summaryrefslogtreecommitdiff
path: root/src/gpu/texture_manager.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/gpu/texture_manager.cc')
-rw-r--r--src/gpu/texture_manager.cc154
1 files changed, 44 insertions, 110 deletions
diff --git a/src/gpu/texture_manager.cc b/src/gpu/texture_manager.cc
index bdeb508..20e215d 100644
--- a/src/gpu/texture_manager.cc
+++ b/src/gpu/texture_manager.cc
@@ -285,142 +285,76 @@ void TextureManager::dispatch_compute(const std::string& func_name,
wgpuTextureViewRelease(target_view);
}
-void TextureManager::create_gpu_noise_texture(
- const std::string& name, const GpuProceduralParams& params) {
- extern const char* gen_noise_compute_wgsl;
- get_or_create_compute_pipeline("gen_noise", gen_noise_compute_wgsl, 16);
-
- WGPUTextureDescriptor tex_desc = {};
- tex_desc.usage =
- WGPUTextureUsage_StorageBinding | WGPUTextureUsage_TextureBinding;
- tex_desc.dimension = WGPUTextureDimension_2D;
- tex_desc.size = {(uint32_t)params.width, (uint32_t)params.height, 1};
- tex_desc.format = WGPUTextureFormat_RGBA8Unorm;
- tex_desc.mipLevelCount = 1;
- tex_desc.sampleCount = 1;
- WGPUTexture texture = wgpuDeviceCreateTexture(device_, &tex_desc);
+void TextureManager::create_gpu_procedural(
+ const std::string& name, const std::string& func_name,
+ const char* shader_code, const GpuProceduralParams& params,
+ const void* uniform_data, size_t uniform_size) {
+ get_or_create_compute_pipeline(func_name, shader_code, uniform_size);
- struct NoiseParams {
- uint32_t width;
- uint32_t height;
- float seed;
- float frequency;
- };
- NoiseParams uniforms = {(uint32_t)params.width, (uint32_t)params.height,
- params.params[0], params.params[1]};
- dispatch_compute("gen_noise", texture, params, &uniforms,
- sizeof(NoiseParams));
+ TextureWithView tv = gpu_create_storage_texture_2d(
+ device_, (uint32_t)params.width, (uint32_t)params.height,
+ WGPUTextureFormat_RGBA8Unorm);
- WGPUTextureView view =
- gpu_create_texture_view_2d(texture, WGPUTextureFormat_RGBA8Unorm);
+ dispatch_compute(func_name, tv.texture, params, uniform_data, uniform_size);
GpuTexture gpu_tex;
- gpu_tex.texture = texture;
- gpu_tex.view = view;
+ gpu_tex.texture = tv.texture;
+ gpu_tex.view = tv.view;
gpu_tex.width = params.width;
gpu_tex.height = params.height;
textures_[name] = gpu_tex;
#if !defined(STRIP_ALL)
- printf("Generated GPU noise texture: %s (%dx%d)\n", name.c_str(),
- params.width, params.height);
+ printf("Generated GPU %s texture: %s (%dx%d)\n", func_name.c_str(),
+ name.c_str(), params.width, params.height);
#endif
}
+void TextureManager::create_gpu_noise_texture(
+ const std::string& name, const GpuProceduralParams& params) {
+ extern const char* gen_noise_compute_wgsl;
+ struct NoiseParams {
+ uint32_t width, height;
+ float seed, frequency;
+ };
+ NoiseParams u = {(uint32_t)params.width, (uint32_t)params.height,
+ params.params[0], params.params[1]};
+ create_gpu_procedural(name, "gen_noise", gen_noise_compute_wgsl, params, &u,
+ sizeof(u));
+}
+
void TextureManager::create_gpu_perlin_texture(
const std::string& name, const GpuProceduralParams& params) {
extern const char* gen_perlin_compute_wgsl;
- get_or_create_compute_pipeline("gen_perlin", gen_perlin_compute_wgsl, 32);
-
- WGPUTextureDescriptor tex_desc = {};
- tex_desc.usage =
- WGPUTextureUsage_StorageBinding | WGPUTextureUsage_TextureBinding;
- tex_desc.dimension = WGPUTextureDimension_2D;
- tex_desc.size = {(uint32_t)params.width, (uint32_t)params.height, 1};
- tex_desc.format = WGPUTextureFormat_RGBA8Unorm;
- tex_desc.mipLevelCount = 1;
- tex_desc.sampleCount = 1;
- WGPUTexture texture = wgpuDeviceCreateTexture(device_, &tex_desc);
-
struct PerlinParams {
- uint32_t width;
- uint32_t height;
- float seed;
- float frequency;
- float amplitude;
- float amplitude_decay;
+ uint32_t width, height;
+ float seed, frequency, amplitude, amplitude_decay;
uint32_t octaves;
float _pad0;
};
- PerlinParams uniforms = {(uint32_t)params.width,
- (uint32_t)params.height,
- params.params[0],
- params.params[1],
- params.num_params > 2 ? params.params[2] : 1.0f,
- params.num_params > 3 ? params.params[3] : 0.5f,
- params.num_params > 4 ? (uint32_t)params.params[4]
- : 4u,
- 0.0f};
- dispatch_compute("gen_perlin", texture, params, &uniforms,
- sizeof(PerlinParams));
-
- WGPUTextureView view =
- gpu_create_texture_view_2d(texture, WGPUTextureFormat_RGBA8Unorm);
-
- GpuTexture gpu_tex;
- gpu_tex.texture = texture;
- gpu_tex.view = view;
- gpu_tex.width = params.width;
- gpu_tex.height = params.height;
- textures_[name] = gpu_tex;
-
-#if !defined(STRIP_ALL)
- printf("Generated GPU perlin texture: %s (%dx%d)\n", name.c_str(),
- params.width, params.height);
-#endif
+ PerlinParams u = {(uint32_t)params.width,
+ (uint32_t)params.height,
+ params.params[0],
+ params.params[1],
+ params.num_params > 2 ? params.params[2] : 1.0f,
+ params.num_params > 3 ? params.params[3] : 0.5f,
+ params.num_params > 4 ? (uint32_t)params.params[4] : 4u,
+ 0.0f};
+ create_gpu_procedural(name, "gen_perlin", gen_perlin_compute_wgsl, params, &u,
+ sizeof(u));
}
void TextureManager::create_gpu_grid_texture(
const std::string& name, const GpuProceduralParams& params) {
extern const char* gen_grid_compute_wgsl;
- get_or_create_compute_pipeline("gen_grid", gen_grid_compute_wgsl, 16);
-
- WGPUTextureDescriptor tex_desc = {};
- tex_desc.usage =
- WGPUTextureUsage_StorageBinding | WGPUTextureUsage_TextureBinding;
- tex_desc.dimension = WGPUTextureDimension_2D;
- tex_desc.size = {(uint32_t)params.width, (uint32_t)params.height, 1};
- tex_desc.format = WGPUTextureFormat_RGBA8Unorm;
- tex_desc.mipLevelCount = 1;
- tex_desc.sampleCount = 1;
- WGPUTexture texture = wgpuDeviceCreateTexture(device_, &tex_desc);
-
struct GridParams {
- uint32_t width;
- uint32_t height;
- uint32_t grid_size;
- uint32_t thickness;
+ uint32_t width, height, grid_size, thickness;
};
- GridParams uniforms = {
- (uint32_t)params.width, (uint32_t)params.height,
- params.num_params > 0 ? (uint32_t)params.params[0] : 32u,
- params.num_params > 1 ? (uint32_t)params.params[1] : 2u};
- dispatch_compute("gen_grid", texture, params, &uniforms, sizeof(GridParams));
-
- WGPUTextureView view =
- gpu_create_texture_view_2d(texture, WGPUTextureFormat_RGBA8Unorm);
-
- GpuTexture gpu_tex;
- gpu_tex.texture = texture;
- gpu_tex.view = view;
- gpu_tex.width = params.width;
- gpu_tex.height = params.height;
- textures_[name] = gpu_tex;
-
-#if !defined(STRIP_ALL)
- printf("Generated GPU grid texture: %s (%dx%d)\n", name.c_str(), params.width,
- params.height);
-#endif
+ GridParams u = {(uint32_t)params.width, (uint32_t)params.height,
+ params.num_params > 0 ? (uint32_t)params.params[0] : 32u,
+ params.num_params > 1 ? (uint32_t)params.params[1] : 2u};
+ create_gpu_procedural(name, "gen_grid", gen_grid_compute_wgsl, params, &u,
+ sizeof(u));
}
#if !defined(STRIP_GPU_COMPOSITE)