diff options
| author | skal <pascal.massimino@gmail.com> | 2026-02-17 08:50:24 +0100 |
|---|---|---|
| committer | skal <pascal.massimino@gmail.com> | 2026-02-17 08:50:24 +0100 |
| commit | 59b7ca9adff07f8d457fba53ba4d67c293229b68 (patch) | |
| tree | f84cd7fcb42cffa15fad7bc339abfb2b8e13cb3c | |
| parent | 7041babc9e5333d01191f3eb80fd711bd26cd4f7 (diff) | |
refactor: centralize platform-specific code in gpu.h
Move platform-specific type definitions to gpu.h and establish coding rule
that platform ifdefs must be confined to gpu/platform layers.
- gpu.h: add GpuTextureCopyInfo, GpuTextureDataLayout type aliases
- effect.cc: use GpuTextureCopyInfo instead of platform ifdefs
- texture_manager.cc: use type aliases and label_view() helper
- CODING_STYLE.md: add platform-specific code section with rule
Tests: 34/34 passing
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
| -rw-r--r-- | doc/CODING_STYLE.md | 35 | ||||
| -rw-r--r-- | src/gpu/effect.cc | 11 | ||||
| -rw-r--r-- | src/gpu/gpu.h | 9 | ||||
| -rw-r--r-- | src/gpu/texture_manager.cc | 21 |
4 files changed, 50 insertions, 26 deletions
diff --git a/doc/CODING_STYLE.md b/doc/CODING_STYLE.md index 5c86409..254a1c3 100644 --- a/doc/CODING_STYLE.md +++ b/doc/CODING_STYLE.md @@ -52,6 +52,41 @@ Always use `defined()` and closing comment. --- +## Platform-Specific Code + +**Rule:** Platform-specific compilation (`#ifdef DEMO_CROSS_COMPILE_WIN32`, etc.) must be confined to `src/gpu/gpu.{h,cc}` and `src/platform/platform.{h,cc}`. + +### Correct +```cpp +// In gpu.h - abstract platform differences +#if defined(DEMO_CROSS_COMPILE_WIN32) +using GpuTextureCopyInfo = WGPUImageCopyTexture; +#else +using GpuTextureCopyInfo = WGPUTexelCopyTextureInfo; +#endif + +// In effect.cc - use abstraction +GpuTextureCopyInfo src_copy = { + .texture = src, .mipLevel = 0, .origin = {0, 0, 0}}; +``` + +### Wrong +```cpp +// In effect.cc - direct platform check (FORBIDDEN) +#if defined(DEMO_CROSS_COMPILE_WIN32) + WGPUImageCopyTexture src_copy = {...}; +#else + WGPUTexelCopyTextureInfo src_copy = {...}; +#endif +``` + +**Rationale:** +- Centralizes platform handling in gpu/platform layers +- Prevents #ifdef proliferation across codebase +- Makes cross-platform testing easier + +--- + ## Struct Initialization ### Good diff --git a/src/gpu/effect.cc b/src/gpu/effect.cc index 117ede2..0e53862 100644 --- a/src/gpu/effect.cc +++ b/src/gpu/effect.cc @@ -44,17 +44,10 @@ void Effect::blit_input_to_output(WGPUCommandEncoder encoder, return; } -#if defined(DEMO_CROSS_COMPILE_WIN32) - WGPUImageCopyTexture src_copy = { + GpuTextureCopyInfo src_copy = { .texture = src, .mipLevel = 0, .origin = {0, 0, 0}}; - WGPUImageCopyTexture dst_copy = { + GpuTextureCopyInfo dst_copy = { .texture = dst, .mipLevel = 0, .origin = {0, 0, 0}}; -#else - WGPUTexelCopyTextureInfo src_copy = { - .texture = src, .mipLevel = 0, .origin = {0, 0, 0}}; - WGPUTexelCopyTextureInfo dst_copy = { - .texture = dst, .mipLevel = 0, .origin = {0, 0, 0}}; -#endif WGPUExtent3D extent = {static_cast<unsigned int>(width_), static_cast<unsigned int>(height_), 1}; diff --git a/src/gpu/gpu.h b/src/gpu/gpu.h index 41eeb29..5e928b2 100644 --- a/src/gpu/gpu.h +++ b/src/gpu/gpu.h @@ -75,6 +75,15 @@ struct TextureWithView { WGPUTextureView view; }; +// Platform-abstracted texture copy types +#if defined(DEMO_CROSS_COMPILE_WIN32) +using GpuTextureCopyInfo = WGPUImageCopyTexture; +using GpuTextureDataLayout = WGPUTextureDataLayout; +#else +using GpuTextureCopyInfo = WGPUTexelCopyTextureInfo; +using GpuTextureDataLayout = WGPUTexelCopyBufferLayout; +#endif + GpuBuffer gpu_create_buffer(WGPUDevice device, size_t size, uint32_t usage, const void* data = nullptr); TextureWithView gpu_create_texture_2d(WGPUDevice device, uint32_t width, diff --git a/src/gpu/texture_manager.cc b/src/gpu/texture_manager.cc index 5bca95b..bdeb508 100644 --- a/src/gpu/texture_manager.cc +++ b/src/gpu/texture_manager.cc @@ -2,22 +2,13 @@ // It implements the TextureManager. #include "gpu/texture_manager.h" +#include "gpu/gpu.h" #include "gpu/shader_composer.h" #include "platform/platform.h" #include <cstdio> #include <cstring> #include <vector> -#if defined(DEMO_CROSS_COMPILE_WIN32) -// Old API -#define WGPU_TEX_COPY_INFO WGPUImageCopyTexture -#define WGPU_TEX_DATA_LAYOUT WGPUTextureDataLayout -#else -// New API -#define WGPU_TEX_COPY_INFO WGPUTexelCopyTextureInfo -#define WGPU_TEX_DATA_LAYOUT WGPUTexelCopyBufferLayout -#endif - void TextureManager::init(WGPUDevice device, WGPUQueue queue) { device_ = device; queue_ = queue; @@ -79,22 +70,18 @@ void TextureManager::create_texture(const std::string& name, int width, tex_desc.format = WGPUTextureFormat_RGBA8Unorm; tex_desc.mipLevelCount = 1; tex_desc.sampleCount = 1; -#if defined(DEMO_CROSS_COMPILE_WIN32) - tex_desc.label = nullptr; -#else - tex_desc.label = {nullptr, 0}; -#endif + tex_desc.label = label_view(nullptr); WGPUTexture texture = wgpuDeviceCreateTexture(device_, &tex_desc); // 3. Upload Data - WGPU_TEX_COPY_INFO destination = {}; + GpuTextureCopyInfo destination = {}; destination.texture = texture; destination.mipLevel = 0; destination.origin = {0, 0, 0}; destination.aspect = WGPUTextureAspect_All; - WGPU_TEX_DATA_LAYOUT source_layout = {}; + GpuTextureDataLayout source_layout = {}; source_layout.offset = 0; source_layout.bytesPerRow = width * 4; source_layout.rowsPerImage = height; |
