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;
135 ds.freeTextureHandle =
true;
142 view_desc.
texture = textureHandle;
153 if(graphicsDevice->use_error_scope){
154 wgpuDevicePushErrorScope(graphicsDevice->device, graphicsDevice->filter);
162 WGPUTextureViewDescriptor desc = WGPU_TEXTURE_VIEW_DESCRIPTOR_INIT;
163 desc.format = texture.view_format;
164 desc.dimension = texture.view_dimension;
166 switch(desc.dimension){
167 case WGPUTextureViewDimension_2DArray:
168 case WGPUTextureViewDimension_CubeArray:
169 case WGPUTextureViewDimension_Cube:
170 desc.dimension = WGPUTextureViewDimension_2D;
177 desc.mipLevelCount = 1;
180 desc.aspect = WGPUTextureAspect_All;
181 depth_stencil_target.view = wgpuTextureCreateView(texture.texture, &desc);
182 counters.texture_view++;
183 depth_stencil_target.format = texture.view_format;
184 depth_stencil_target.samples = texture.samples;
187 if(graphicsDevice->use_error_scope){
188 WGPUPopErrorScopeCallbackInfo info = WGPU_POP_ERROR_SCOPE_CALLBACK_INFO_INIT;
189 info.mode = WGPUCallbackMode_AllowSpontaneous;
190 info.callback = render_targets_error_callback;
191 info.userdata1 =
this;
192 info.userdata2 = graphicsDevice;
193 WGPUFuture future = wgpuDevicePopErrorScope(graphicsDevice->device, info);
197 return depth_stencil_targets.addResource(std::move(depth_stencil_target));
202 if(!HandleIsValid(handle))
return;
205 wgpuTextureViewRelease(target.view);
206 counters.texture_view--;
207 if(target.freeTextureHandle){
210 depth_stencil_targets.removeResource(handle);
216 std::vector<RenderTargetHandle> handles;
217 for (
auto& resource : render_targets) {
218 handles.push_back(render_targets.getHandle(resource));
220 for (
auto& handle : handles) {
225 std::vector<DepthStencilHandle> handles;
226 for (
auto& resource : depth_stencil_targets) {
227 handles.push_back(depth_stencil_targets.getHandle(resource));
229 for (
auto& handle : handles) {
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 releaseResources() override
Release all allocated render target resources.
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 void releaseTexture(TextureHandle textureHandle) override
Release the texture with the given textureHandle.
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.