1#include "RenderTargetsWebGPU.h"
3#include "GraphicsDeviceWebGPU.h"
4#include "Foundation/Logging/Logger.h"
11 void render_targets_error_callback(WGPUPopErrorScopeStatus status, WGPUErrorType type, WGPUStringView message,
void* userdata1,
void* userdata2)
17 if(status == WGPUPopErrorScopeStatus_CallbackCancelled){
18 LOG_ERROR(logger,
"WebGPU render targets err status: Cancelled");
20 if(status == WGPUPopErrorScopeStatus_Error){
21 LOG_ERROR(logger,
"WebGPU render targets err status: Error");
23 if(message.data && message.length){
24 LOG_ERROR(logger,
"WebGPU render targets err (%d) %.*s", type, WGPUStringViewFormat(message));
31 void RenderTargetsWebGPU::initialize(GraphicsDeviceWebGPU *graphicsDeviceIn)
33 graphicsDevice = graphicsDeviceIn;
41 if(graphicsDevice->use_error_scope){
42 wgpuDevicePushErrorScope(graphicsDevice->device, graphicsDevice->filter);
45 for(
size_t i=0; i<numViews; i++){
47 assert(HandleIsValid(set.
texture));
50 WGPUTextureViewDescriptor desc = WGPU_TEXTURE_VIEW_DESCRIPTOR_INIT;
51 desc.format = texture.view_format;
52 desc.dimension = texture.view_dimension;
54 switch(desc.dimension){
55 case WGPUTextureViewDimension_2DArray:
56 case WGPUTextureViewDimension_Cube:
57 case WGPUTextureViewDimension_CubeArray:
58 desc.dimension = WGPUTextureViewDimension_2D;
65 desc.mipLevelCount = 1;
68 desc.aspect = WGPUTextureAspect_All;
69 render_target.view[i] = wgpuTextureCreateView(texture.texture, &desc);
70 counters.texture_view++;
71 render_target.textureHandle[i] = set.
texture;
72 render_target.format[i] = texture.view_format;
74 render_target.count = (uint32_t)numViews;
79 render_target.width = texture.width;
80 render_target.height = texture.height;
81 render_target.samples = texture.samples;
84 if(graphicsDevice->use_error_scope){
85 WGPUPopErrorScopeCallbackInfo info = WGPU_POP_ERROR_SCOPE_CALLBACK_INFO_INIT;
86 info.mode = WGPUCallbackMode_AllowSpontaneous;
87 info.callback = render_targets_error_callback;
88 info.userdata1 =
this;
89 info.userdata2 = graphicsDevice;
90 WGPUFuture future = wgpuDevicePopErrorScope(graphicsDevice->device, info);
94 return render_targets.addResource(std::move(render_target));
99 if(!HandleIsValid(handle))
return;
102 for(
size_t i=0; i<target.count; i++){
103 wgpuTextureViewRelease(target.view[i]);
104 counters.texture_view--;
106 render_targets.removeResource(handle);
112 assert(target.width);
113 assert(target.height);
114 assert(target.samples);
116 tex_desc.target = ResourceDimensions::Texture2D;
117 tex_desc.width = target.width;
118 tex_desc.height = target.height;
123 tex_desc.samples = target.samples;
124 tex_desc.format = TextureFormat::D32_FLOAT;
128 view_desc.
texture = textureHandle;
134 ds.textureHandle = textureHandle;
141 view_desc.
texture = textureHandle;
152 if(graphicsDevice->use_error_scope){
153 wgpuDevicePushErrorScope(graphicsDevice->device, graphicsDevice->filter);
161 WGPUTextureViewDescriptor desc = WGPU_TEXTURE_VIEW_DESCRIPTOR_INIT;
162 desc.format = texture.view_format;
163 desc.dimension = texture.view_dimension;
165 switch(desc.dimension){
166 case WGPUTextureViewDimension_2DArray:
167 case WGPUTextureViewDimension_CubeArray:
168 case WGPUTextureViewDimension_Cube:
169 desc.dimension = WGPUTextureViewDimension_2D;
176 desc.mipLevelCount = 1;
179 desc.aspect = WGPUTextureAspect_All;
180 depth_stencil_target.view = wgpuTextureCreateView(texture.texture, &desc);
181 counters.texture_view++;
182 depth_stencil_target.format = texture.view_format;
183 depth_stencil_target.samples = texture.samples;
186 if(graphicsDevice->use_error_scope){
187 WGPUPopErrorScopeCallbackInfo info = WGPU_POP_ERROR_SCOPE_CALLBACK_INFO_INIT;
188 info.mode = WGPUCallbackMode_AllowSpontaneous;
189 info.callback = render_targets_error_callback;
190 info.userdata1 =
this;
191 info.userdata2 = graphicsDevice;
192 WGPUFuture future = wgpuDevicePopErrorScope(graphicsDevice->device, info);
196 return depth_stencil_targets.addResource(std::move(depth_stencil_target));
201 if(!HandleIsValid(handle))
return;
204 wgpuTextureViewRelease(target.view);
205 counters.texture_view--;
206 depth_stencil_targets.removeResource(handle);
Log implementation class.
virtual void releaseRenderTarget(RenderTargetHandle handle) override
Release the render target with the given renderTargetHandle.
virtual DepthStencilHandle createDepthStencilTarget(const RenderTargetHandle handle) override
Creates a depth/stencil target to back the render target with the given handle.
virtual void releaseDepthStencilTarget(DepthStencilHandle handle) override
Release the depth target with the given depthStencilHandle.
virtual RenderTargetHandle createRenderTarget(const RenderTargetViewDescription *renderTargetViews, const size_t numViews) override
Create a render target using the given view descriptions.
virtual TextureHandle loadTexture(const TextureDescription &desc, const TextureData *data) override
Load a texture from the given description.
constexpr Log getLogger(const char(&name)[LEN]) noexcept
Contains all Cogs related functionality.
Describes a single depth stencil view and which resources to use from the underlying texture.
TextureHandle texture
Texture handle.
uint16_t numLayers
Number of available layers to write to.
uint8_t levelIndex
Index of the mipmap level to render to.
uint16_t layerIndex
Index of the first layer (if array) to write depth to.
Describes a single render target view and which resources to use from the underlying texture.
uint16_t layerIndex
Index of the first layer (if array) to render to.
uint16_t numLayers
Number of available layers to render to.
TextureHandle texture
Texture handle.
uint8_t levelIndex
Index of the mipmap level to render to.
@ DepthBuffer
The texture can be used as a depth target and have depth buffer values written into.