Skip to content

Commit

Permalink
RTT multiple color attachments
Browse files Browse the repository at this point in the history
  • Loading branch information
GrigoryGraborenko committed Feb 12, 2025
1 parent 870cc17 commit aabe159
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 19 deletions.
9 changes: 9 additions & 0 deletions Src/EditorViewers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1154,7 +1154,11 @@ Token Scrapbook2D::ActivateRTT(void) {
auto& self = Singleton();
bool reset = self.m_NeedsReset;
self.m_NeedsReset = false;
#ifdef NESHNY_WEBGPU
return self.m_RTT.Activate({ WebGPUPipeline::AttachmentMode::RGBA }, true, self.m_Width, self.m_Height, reset);
#else
return self.m_RTT.Activate({ RTT::Mode::RGBA }, true, self.m_Width, self.m_Height, reset);
#endif
}

////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -1271,7 +1275,12 @@ Token Scrapbook3D::ActivateRTT(void) {
auto& self = Singleton();
bool reset = self.m_NeedsReset;
self.m_NeedsReset = false;

#ifdef NESHNY_WEBGPU
return self.m_RTT.Activate({ WebGPUPipeline::AttachmentMode::RGBA }, true, self.m_Width, self.m_Height, reset);
#else
return self.m_RTT.Activate({ RTT::Mode::RGBA }, true, self.m_Width, self.m_Height, reset);
#endif
}

////////////////////////////////////////////////////////////////////////////////
Expand Down
32 changes: 21 additions & 11 deletions Src/WebGPU/WGPUUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -672,11 +672,19 @@ void WebGPUPipeline::FinalizeRender(std::string_view shader_name, WebGPURenderBu
blend.alpha.srcFactor = params.p_AlphaBlend.srcFactor;
blend.alpha.dstFactor = params.p_AlphaBlend.dstFactor;

WGPUColorTargetState color_target = {};
color_target.nextInChain = nullptr;
color_target.format = WGPUTextureFormat_BGRA8Unorm;
color_target.blend = &blend;
color_target.writeMask = WGPUColorWriteMask_All;
std::vector<WGPUColorTargetState> color_targets;
for (auto attach: params.p_Attachments) {
WGPUColorTargetState color_target;
color_target.nextInChain = nullptr;
if (attach == AttachmentMode::RGBA) {
color_target.format = WGPUTextureFormat_BGRA8Unorm;
} else {
throw "Non-RGBA attachment modes not implemented yet";
}
color_target.blend = &blend;
color_target.writeMask = WGPUColorWriteMask_All;
color_targets.push_back(color_target);
}

WGPUDepthStencilState depth_state = {};
depth_state.nextInChain = nullptr;
Expand All @@ -692,8 +700,8 @@ void WebGPUPipeline::FinalizeRender(std::string_view shader_name, WebGPURenderBu
WGPUFragmentState fragment = {};
fragment.module = shader;
fragment.entryPoint = "frag_main";
fragment.targetCount = 1;
fragment.targets = &color_target;
fragment.targetCount = color_targets.size();
fragment.targets = color_targets.data();

WGPURenderPipelineDescriptor desc = {};
desc.fragment = &fragment;
Expand Down Expand Up @@ -931,6 +939,7 @@ void WebGPUPipeline::Compute(int calls, iVec3 workgroup_size, std::optional<std:
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
WebGPURTT::WebGPURTT(void) :
m_PassDescriptor{}
,m_DepthDesc {}
Expand All @@ -946,7 +955,7 @@ WebGPURTT::WebGPURTT(void) :
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Token WebGPURTT::Activate(std::vector<Mode> color_attachments, bool capture_depth_stencil, int width, int height, bool clear) {
Token WebGPURTT::Activate(std::vector<WebGPUPipeline::AttachmentMode> color_attachments, bool capture_depth_stencil, int width, int height, bool clear, WGPUTextureView existing_depth_tex) {

int num_color_tex = (int)color_attachments.size();
bool modes_same = color_attachments.size() == m_Modes.size();
Expand All @@ -972,9 +981,10 @@ Token WebGPURTT::Activate(std::vector<Mode> color_attachments, bool capture_dept
tex->Init2D(m_Width, m_Height, WGPUTextureFormat_BGRA8Unorm, WGPUTextureUsage_RenderAttachment | WGPUTextureUsage_TextureBinding, 1);
m_ColorTextures.push_back(tex);
}
if (m_CaptureDepthStencil) {
if (m_CaptureDepthStencil && (existing_depth_tex == nullptr)) {
m_DepthTex = new WebGPUTexture();
m_DepthTex->InitDepth(m_Width, m_Height);
existing_depth_tex = m_DepthTex->GetTextureView();
}

m_ColorDescriptors.resize(num_color_tex);
Expand All @@ -995,8 +1005,8 @@ Token WebGPURTT::Activate(std::vector<Mode> color_attachments, bool capture_dept
m_PassDescriptor.colorAttachmentCount = num_color_tex;
m_PassDescriptor.colorAttachments = &m_ColorDescriptors[0];

if (m_DepthTex) {
m_DepthDesc.view = m_DepthTex->GetTextureView();
if (existing_depth_tex) {
m_DepthDesc.view = existing_depth_tex;
m_PassDescriptor.depthStencilAttachment = &m_DepthDesc;
} else {
m_PassDescriptor.depthStencilAttachment = nullptr;
Expand Down
19 changes: 11 additions & 8 deletions Src/WebGPU/WGPUUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,12 @@ class WebGPUPipeline {
COMPUTE
};

enum class AttachmentMode {
RGBA
//,RGBA_FLOAT32
//,RGBA_FLOAT16
};

struct RenderParams {
RenderParams() {};

Expand All @@ -384,6 +390,8 @@ class WebGPUPipeline {

WGPUFrontFace p_FrontFace = WGPUFrontFace_CCW;
WGPUCullMode p_CullMode = WGPUCullMode_None;

std::vector<WebGPUPipeline::AttachmentMode> p_Attachments = { AttachmentMode::RGBA };
};

WebGPUPipeline ( void ) : m_Type(Type::UNKNOWN) {}
Expand Down Expand Up @@ -433,16 +441,10 @@ class WebGPURTT {

public:

enum class Mode {
RGBA
//,RGBA_FLOAT32
//,RGBA_FLOAT16
};

WebGPURTT ( void );
~WebGPURTT ( void ) { Destroy(); }

Token Activate ( std::vector<Mode> color_attachments, bool capture_depth_stencil, int width, int height, bool clear = true );
Token Activate ( std::vector<WebGPUPipeline::AttachmentMode> color_attachments, bool capture_depth_stencil, int width, int height, bool clear = true, WGPUTextureView existing_depth_tex = nullptr );
Token Activate ( std::vector<WGPUTextureView> color_attachments, WGPUTextureView depth_tex, bool clear = true );

void Render ( WebGPUPipeline* pipeline, int instances = 1 );
Expand All @@ -453,18 +455,19 @@ class WebGPURTT {
inline WebGPUTexture* GetDepthTex ( void ) { return m_DepthTex ? m_DepthTex : nullptr; }
inline WGPUTextureView GetColorTexView ( int index ) { return index >= m_ColorTextures.size() ? nullptr : m_ColorTextures[index]->GetTextureView(); }
inline WGPUTextureView GetDepthTexView ( void ) { return m_DepthTex ? m_DepthTex->GetTextureView() : nullptr; }
inline auto GetAttachmentModes ( void ) { return m_Modes; }

private:

void Destroy ( void );

std::vector<Mode> m_Modes = {};
bool m_CaptureDepthStencil = false;
int m_Width = 0;
int m_Height = 0;
std::vector<WebGPUTexture*> m_ColorTextures;
WebGPUTexture* m_DepthTex = nullptr;

std::vector<WebGPUPipeline::AttachmentMode> m_Modes = {};
std::vector<WGPURenderPassColorAttachment> m_ColorDescriptors;
WGPURenderPassDepthStencilAttachment m_DepthDesc;
WGPURenderPassDescriptor m_PassDescriptor;
Expand Down

0 comments on commit aabe159

Please sign in to comment.