summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorskal <pascal.massimino@gmail.com>2026-02-14 14:03:58 +0100
committerskal <pascal.massimino@gmail.com>2026-02-14 14:03:58 +0100
commit61ced8aa1946cc32de4328cc75b5faf6b77723be (patch)
tree142bad05c24362d7051e80acabaddd6bac2bb753
parent197a03c24baba3acc35327e0e126ec49754f9945 (diff)
Refactor: add gpu_create_texture_view_2d helper
Reduces WGPUTextureViewDescriptor boilerplate from 5-7 lines to 1-2. Helper supports optional mip_levels parameter (defaults to 1). Updated 17 call sites across gpu/, tests/, and tools/. Net: -82 lines. All tests passing (34/34). Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
-rw-r--r--src/gpu/effect.cc30
-rw-r--r--src/gpu/gpu.cc14
-rw-r--r--src/gpu/gpu.h3
-rw-r--r--src/gpu/texture_manager.cc60
-rw-r--r--src/tests/3d/test_3d_physics.cc9
-rw-r--r--src/tests/3d/test_3d_render.cc9
-rw-r--r--src/tests/common/offscreen_render_target.cc11
-rw-r--r--src/tests/gpu/test_post_process_helper.cc10
-rw-r--r--tools/cnn_test.cc36
9 files changed, 50 insertions, 132 deletions
diff --git a/src/gpu/effect.cc b/src/gpu/effect.cc
index 3ee2acd..52128c4 100644
--- a/src/gpu/effect.cc
+++ b/src/gpu/effect.cc
@@ -127,14 +127,10 @@ void MainSequence::create_framebuffers(int width, int height) {
framebuffer_a_ = wgpuDeviceCreateTexture(gpu_ctx.device, &desc);
framebuffer_b_ = wgpuDeviceCreateTexture(gpu_ctx.device, &desc);
- WGPUTextureViewDescriptor view_desc = {};
- view_desc.dimension = WGPUTextureViewDimension_2D;
- view_desc.format = gpu_ctx.format;
- view_desc.mipLevelCount = 1;
- view_desc.arrayLayerCount = 1;
-
- framebuffer_view_a_ = wgpuTextureCreateView(framebuffer_a_, &view_desc);
- framebuffer_view_b_ = wgpuTextureCreateView(framebuffer_b_, &view_desc);
+ framebuffer_view_a_ =
+ gpu_create_texture_view_2d(framebuffer_a_, gpu_ctx.format);
+ framebuffer_view_b_ =
+ gpu_create_texture_view_2d(framebuffer_b_, gpu_ctx.format);
// Depth Buffer
WGPUTextureDescriptor depth_desc = {};
@@ -478,14 +474,7 @@ void MainSequence::register_auxiliary_texture(const char* name, int width,
FATAL_CHECK(!texture, "Failed to create auxiliary texture: %s\n", name);
// Create view
- const WGPUTextureViewDescriptor view_desc = {
- .format = gpu_ctx.format,
- .dimension = WGPUTextureViewDimension_2D,
- .mipLevelCount = 1,
- .arrayLayerCount = 1,
- };
-
- WGPUTextureView view = wgpuTextureCreateView(texture, &view_desc);
+ WGPUTextureView view = gpu_create_texture_view_2d(texture, gpu_ctx.format);
FATAL_CHECK(!view, "Failed to create auxiliary texture view: %s\n", name);
// Store in registry
@@ -535,14 +524,7 @@ void MainSequence::resize_auxiliary_texture(const char* name, int width,
FATAL_CHECK(!texture, "Failed to resize auxiliary texture: %s\n", name);
// Create view
- const WGPUTextureViewDescriptor view_desc = {
- .format = gpu_ctx.format,
- .dimension = WGPUTextureViewDimension_2D,
- .mipLevelCount = 1,
- .arrayLayerCount = 1,
- };
-
- WGPUTextureView view = wgpuTextureCreateView(texture, &view_desc);
+ WGPUTextureView view = gpu_create_texture_view_2d(texture, gpu_ctx.format);
FATAL_CHECK(!view, "Failed to create resized auxiliary texture view: %s\n",
name);
diff --git a/src/gpu/gpu.cc b/src/gpu/gpu.cc
index cf5d85d..535de67 100644
--- a/src/gpu/gpu.cc
+++ b/src/gpu/gpu.cc
@@ -98,6 +98,20 @@ WGPUTextureView gpu_create_mip_view(WGPUTexture texture,
return wgpuTextureCreateView(texture, &view_desc);
}
+WGPUTextureView gpu_create_texture_view_2d(WGPUTexture texture,
+ WGPUTextureFormat format,
+ uint32_t mip_levels) {
+ const WGPUTextureViewDescriptor view_desc = {
+ .format = format,
+ .dimension = WGPUTextureViewDimension_2D,
+ .baseMipLevel = 0,
+ .mipLevelCount = mip_levels,
+ .baseArrayLayer = 0,
+ .arrayLayerCount = 1,
+ };
+ return wgpuTextureCreateView(texture, &view_desc);
+}
+
RenderPass gpu_create_render_pass(WGPUDevice device, WGPUTextureFormat format,
const char* shader_code,
ResourceBinding* bindings, int num_bindings) {
diff --git a/src/gpu/gpu.h b/src/gpu/gpu.h
index b52d6ab..74e0eb7 100644
--- a/src/gpu/gpu.h
+++ b/src/gpu/gpu.h
@@ -99,6 +99,9 @@ TextureWithView gpu_create_storage_texture_2d(WGPUDevice device, uint32_t width,
WGPUTextureFormat format);
WGPUTextureView gpu_create_mip_view(WGPUTexture texture,
WGPUTextureFormat format, uint32_t mip_level);
+WGPUTextureView gpu_create_texture_view_2d(WGPUTexture texture,
+ WGPUTextureFormat format,
+ uint32_t mip_levels = 1);
ComputePass gpu_create_compute_pass(WGPUDevice device, const char* shader_code,
ResourceBinding* bindings,
int num_bindings);
diff --git a/src/gpu/texture_manager.cc b/src/gpu/texture_manager.cc
index dfa6315..d5821ee 100644
--- a/src/gpu/texture_manager.cc
+++ b/src/gpu/texture_manager.cc
@@ -103,16 +103,8 @@ void TextureManager::create_texture(const std::string& name, int width,
&source_layout, &tex_size);
// 4. Create View
- WGPUTextureViewDescriptor view_desc = {};
- view_desc.format = WGPUTextureFormat_RGBA8Unorm;
- view_desc.dimension = WGPUTextureViewDimension_2D;
- view_desc.baseMipLevel = 0;
- view_desc.mipLevelCount = 1;
- view_desc.baseArrayLayer = 0;
- view_desc.arrayLayerCount = 1;
- view_desc.aspect = WGPUTextureAspect_All;
-
- WGPUTextureView view = wgpuTextureCreateView(texture, &view_desc);
+ WGPUTextureView view =
+ gpu_create_texture_view_2d(texture, WGPUTextureFormat_RGBA8Unorm);
// 5. Store
GpuTexture gpu_tex;
@@ -242,12 +234,8 @@ void TextureManager::dispatch_compute(const std::string& func_name,
wgpuBufferUnmap(uniform_buf);
// Create storage texture view
- WGPUTextureViewDescriptor view_desc = {};
- view_desc.format = WGPUTextureFormat_RGBA8Unorm;
- view_desc.dimension = WGPUTextureViewDimension_2D;
- view_desc.mipLevelCount = 1;
- view_desc.arrayLayerCount = 1;
- WGPUTextureView target_view = wgpuTextureCreateView(target, &view_desc);
+ WGPUTextureView target_view =
+ gpu_create_texture_view_2d(target, WGPUTextureFormat_RGBA8Unorm);
// Create bind group layout entries (must match pipeline)
WGPUBindGroupLayoutEntry bgl_entries[2] = {};
@@ -332,12 +320,8 @@ void TextureManager::create_gpu_noise_texture(
params.params[0], params.params[1]};
dispatch_compute("gen_noise", texture, params, &uniforms, sizeof(NoiseParams));
- WGPUTextureViewDescriptor view_desc = {};
- view_desc.format = WGPUTextureFormat_RGBA8Unorm;
- view_desc.dimension = WGPUTextureViewDimension_2D;
- view_desc.mipLevelCount = 1;
- view_desc.arrayLayerCount = 1;
- WGPUTextureView view = wgpuTextureCreateView(texture, &view_desc);
+ WGPUTextureView view =
+ gpu_create_texture_view_2d(texture, WGPUTextureFormat_RGBA8Unorm);
GpuTexture gpu_tex;
gpu_tex.texture = texture;
@@ -389,12 +373,8 @@ void TextureManager::create_gpu_perlin_texture(
dispatch_compute("gen_perlin", texture, params, &uniforms,
sizeof(PerlinParams));
- WGPUTextureViewDescriptor view_desc = {};
- view_desc.format = WGPUTextureFormat_RGBA8Unorm;
- view_desc.dimension = WGPUTextureViewDimension_2D;
- view_desc.mipLevelCount = 1;
- view_desc.arrayLayerCount = 1;
- WGPUTextureView view = wgpuTextureCreateView(texture, &view_desc);
+ WGPUTextureView view =
+ gpu_create_texture_view_2d(texture, WGPUTextureFormat_RGBA8Unorm);
GpuTexture gpu_tex;
gpu_tex.texture = texture;
@@ -436,12 +416,8 @@ void TextureManager::create_gpu_grid_texture(
params.num_params > 1 ? (uint32_t)params.params[1] : 2u};
dispatch_compute("gen_grid", texture, params, &uniforms, sizeof(GridParams));
- WGPUTextureViewDescriptor view_desc = {};
- view_desc.format = WGPUTextureFormat_RGBA8Unorm;
- view_desc.dimension = WGPUTextureViewDimension_2D;
- view_desc.mipLevelCount = 1;
- view_desc.arrayLayerCount = 1;
- WGPUTextureView view = wgpuTextureCreateView(texture, &view_desc);
+ WGPUTextureView view =
+ gpu_create_texture_view_2d(texture, WGPUTextureFormat_RGBA8Unorm);
GpuTexture gpu_tex;
gpu_tex.texture = texture;
@@ -528,12 +504,8 @@ void TextureManager::dispatch_composite(
wgpuBufferUnmap(uniform_buf);
// Create storage texture view
- WGPUTextureViewDescriptor view_desc = {};
- view_desc.format = WGPUTextureFormat_RGBA8Unorm;
- view_desc.dimension = WGPUTextureViewDimension_2D;
- view_desc.mipLevelCount = 1;
- view_desc.arrayLayerCount = 1;
- WGPUTextureView target_view = wgpuTextureCreateView(target, &view_desc);
+ WGPUTextureView target_view =
+ gpu_create_texture_view_2d(target, WGPUTextureFormat_RGBA8Unorm);
// Dynamic bind group
const int max_entries = 2 + num_inputs + (num_inputs > 0 ? 1 : 0);
@@ -665,12 +637,8 @@ void TextureManager::create_gpu_composite_texture(
input_views, sampler);
// Create view
- WGPUTextureViewDescriptor view_desc = {};
- view_desc.format = WGPUTextureFormat_RGBA8Unorm;
- view_desc.dimension = WGPUTextureViewDimension_2D;
- view_desc.mipLevelCount = 1;
- view_desc.arrayLayerCount = 1;
- WGPUTextureView view = wgpuTextureCreateView(texture, &view_desc);
+ WGPUTextureView view =
+ gpu_create_texture_view_2d(texture, WGPUTextureFormat_RGBA8Unorm);
// Store
GpuTexture gpu_tex;
diff --git a/src/tests/3d/test_3d_physics.cc b/src/tests/3d/test_3d_physics.cc
index 2ee5a4a..736cfc0 100644
--- a/src/tests/3d/test_3d_physics.cc
+++ b/src/tests/3d/test_3d_physics.cc
@@ -185,15 +185,8 @@ int main(int argc, char** argv) {
wgpuSurfaceGetCurrentTexture(g_surface, &surface_tex);
if (surface_tex.status ==
WGPUSurfaceGetCurrentTextureStatus_SuccessOptimal) {
- const WGPUTextureViewDescriptor view_desc = {
- .format = g_format,
- .dimension = WGPUTextureViewDimension_2D,
- .mipLevelCount = 1,
- .arrayLayerCount = 1,
- };
-
const WGPUTextureView view =
- wgpuTextureCreateView(surface_tex.texture, &view_desc);
+ gpu_create_texture_view_2d(surface_tex.texture, g_format);
g_renderer.render(g_scene, g_camera, time, view);
wgpuTextureViewRelease(view);
wgpuSurfacePresent(g_surface);
diff --git a/src/tests/3d/test_3d_render.cc b/src/tests/3d/test_3d_render.cc
index 49a265f..06e95c4 100644
--- a/src/tests/3d/test_3d_render.cc
+++ b/src/tests/3d/test_3d_render.cc
@@ -215,15 +215,8 @@ int main(int argc, char** argv) {
wgpuSurfaceGetCurrentTexture(g_surface, &surface_tex);
if (surface_tex.status ==
WGPUSurfaceGetCurrentTextureStatus_SuccessOptimal) {
- const WGPUTextureViewDescriptor view_desc = {
- .format = g_format,
- .dimension = WGPUTextureViewDimension_2D,
- .mipLevelCount = 1,
- .arrayLayerCount = 1,
- };
-
const WGPUTextureView view =
- wgpuTextureCreateView(surface_tex.texture, &view_desc);
+ gpu_create_texture_view_2d(surface_tex.texture, g_format);
g_renderer.render(g_scene, g_camera, time, view);
wgpuTextureViewRelease(view);
wgpuSurfacePresent(g_surface);
diff --git a/src/tests/common/offscreen_render_target.cc b/src/tests/common/offscreen_render_target.cc
index 10775a1..d322a7c 100644
--- a/src/tests/common/offscreen_render_target.cc
+++ b/src/tests/common/offscreen_render_target.cc
@@ -3,6 +3,7 @@
// Provides pixel readback for validation.
#include "offscreen_render_target.h"
+#include "gpu/gpu.h"
#include "gpu/texture_readback.h"
#include <cassert>
#include <cstdio>
@@ -27,15 +28,7 @@ OffscreenRenderTarget::OffscreenRenderTarget(WGPUInstance instance,
assert(texture_ && "Failed to create offscreen texture");
// Create texture view
- const WGPUTextureViewDescriptor view_desc = {
- .format = format,
- .dimension = WGPUTextureViewDimension_2D,
- .baseMipLevel = 0,
- .mipLevelCount = 1,
- .baseArrayLayer = 0,
- .arrayLayerCount = 1,
- };
- view_ = wgpuTextureCreateView(texture_, &view_desc);
+ view_ = gpu_create_texture_view_2d(texture_, format);
assert(view_ && "Failed to create offscreen texture view");
}
diff --git a/src/tests/gpu/test_post_process_helper.cc b/src/tests/gpu/test_post_process_helper.cc
index 13fd856..08b9e49 100644
--- a/src/tests/gpu/test_post_process_helper.cc
+++ b/src/tests/gpu/test_post_process_helper.cc
@@ -38,15 +38,7 @@ static WGPUTexture create_post_process_texture(WGPUDevice device, int width,
// Helper: Create texture view
static WGPUTextureView create_texture_view(WGPUTexture texture,
WGPUTextureFormat format) {
- const WGPUTextureViewDescriptor view_desc = {
- .format = format,
- .dimension = WGPUTextureViewDimension_2D,
- .baseMipLevel = 0,
- .mipLevelCount = 1,
- .baseArrayLayer = 0,
- .arrayLayerCount = 1,
- };
- return wgpuTextureCreateView(texture, &view_desc);
+ return gpu_create_texture_view_2d(texture, format);
}
// Minimal valid post-process shader for testing
diff --git a/tools/cnn_test.cc b/tools/cnn_test.cc
index b4a4bdc..1d736d9 100644
--- a/tools/cnn_test.cc
+++ b/tools/cnn_test.cc
@@ -700,15 +700,8 @@ static bool process_cnn_v2(WGPUDevice device, WGPUQueue queue,
wgpuQueueWriteBuffer(queue, weights_buffer, 0, weights_data + weights_offset, weights_only_size);
// Create input view
- const WGPUTextureViewDescriptor view_desc = {
- .format = WGPUTextureFormat_BGRA8Unorm,
- .dimension = WGPUTextureViewDimension_2D,
- .baseMipLevel = 0,
- .mipLevelCount = 1,
- .baseArrayLayer = 0,
- .arrayLayerCount = 1,
- };
- WGPUTextureView input_view = wgpuTextureCreateView(input_texture, &view_desc);
+ WGPUTextureView input_view =
+ gpu_create_texture_view_2d(input_texture, WGPUTextureFormat_BGRA8Unorm);
// Create static features texture (RGBA32Uint)
const WGPUTextureDescriptor static_desc = {
@@ -1269,15 +1262,8 @@ int main(int argc, char** argv) {
printf("Using CNN v1 (render pipeline architecture)\n");
// Create input texture view
- const WGPUTextureViewDescriptor view_desc = {
- .format = WGPUTextureFormat_BGRA8Unorm,
- .dimension = WGPUTextureViewDimension_2D,
- .baseMipLevel = 0,
- .mipLevelCount = 1,
- .baseArrayLayer = 0,
- .arrayLayerCount = 1,
- };
- WGPUTextureView input_view = wgpuTextureCreateView(input_texture, &view_desc);
+ WGPUTextureView input_view =
+ gpu_create_texture_view_2d(input_texture, WGPUTextureFormat_BGRA8Unorm);
WGPUTextureView original_view = input_view; // Keep reference to original
// Create CNN pipelines (different formats for intermediate vs final)
@@ -1333,17 +1319,11 @@ int main(int argc, char** argv) {
};
// Create views for intermediate textures (RGBA16Float)
- const WGPUTextureViewDescriptor intermediate_view_desc = {
- .format = WGPUTextureFormat_RGBA16Float,
- .dimension = WGPUTextureViewDimension_2D,
- .baseMipLevel = 0,
- .mipLevelCount = 1,
- .baseArrayLayer = 0,
- .arrayLayerCount = 1,
- };
WGPUTextureView intermediate_views[2] = {
- wgpuTextureCreateView(intermediate_textures[0], &intermediate_view_desc),
- wgpuTextureCreateView(intermediate_textures[1], &intermediate_view_desc),
+ gpu_create_texture_view_2d(intermediate_textures[0],
+ WGPUTextureFormat_RGBA16Float),
+ gpu_create_texture_view_2d(intermediate_textures[1],
+ WGPUTextureFormat_RGBA16Float),
};
// Get sampler