首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何确保在WebGPU中正确清除背景?

如何确保在WebGPU中正确清除背景?
EN

Stack Overflow用户
提问于 2021-02-04 13:09:59
回答 1查看 94关注 0票数 0

当使用WebGPU渲染时,我需要设置一个神奇的标志来正确清除背景吗?我的渲染运行良好,除了,无论我使用什么设置,我看到最后显示的垃圾是在浏览器窗口中,而不是背景颜色(如果我在附件中设置了一个非零的clearColor,那么它将不断累积相同的颜色,直到它的max‘out):

我通过emscripten CPP接口使用WebGPU,并在Windows上运行Chrome Canary (版本90.0.4407.0 (官方版本) canary (64位))。

我的帧渲染看起来像这样(从js端的requestAnimationFrame调用):

代码语言:javascript
复制
WGPUSwapChain swapChain                 = _pWindow->swapChain();
WGPUTextureView backbufferView          = wgpuSwapChainGetCurrentTextureView(swapChain);
WGPURenderPassDescriptor renderpassInfo = {};
WGPURenderPassColorAttachmentDescriptor colorAttachment = {};
{
    colorAttachment.attachment            = backbufferView;
    colorAttachment.resolveTarget         = nullptr;
    colorAttachment.clearColor            = { 0.0f, 0.0f, 0.0f, 0.0f };
    colorAttachment.loadOp                = WGPULoadOp_Clear;
    colorAttachment.storeOp               = WGPUStoreOp_Store;
    renderpassInfo.colorAttachmentCount   = 1;
    renderpassInfo.colorAttachments       = &colorAttachment;
    renderpassInfo.depthStencilAttachment = nullptr;
}
WGPUCommandBuffer commands;
{
    WGPUCommandEncoder encoder = wgpuDeviceCreateCommandEncoder(_device, nullptr);

    WGPURenderPassEncoder pass = wgpuCommandEncoderBeginRenderPass(encoder, &renderpassInfo);
    wgpuRenderPassEncoderSetPipeline(pass, _pipeline);
    wgpuRenderPassEncoderSetVertexBuffer(pass, 0, _vb, 0, 0);
    wgpuRenderPassEncoderDraw(pass, 3, 1, 0, 0);
    wgpuRenderPassEncoderEndPass(pass);
    wgpuRenderPassEncoderRelease(pass);

    
    commands = wgpuCommandEncoderFinish(encoder, nullptr);
    wgpuCommandEncoderRelease(encoder);
}

wgpuQueueSubmit(_queue, 1, &commands);
wgpuCommandBufferRelease(commands);
wgpuTextureViewRelease(backbufferView);

使用以下设置设置管道:

代码语言:javascript
复制
WGPURenderPipelineDescriptor descriptor = {};
WGPUBlendDescriptor blendDescriptor           = {};
blendDescriptor.operation                     = WGPUBlendOperation_Add;
blendDescriptor.srcFactor                     = WGPUBlendFactor_One;
blendDescriptor.dstFactor                     = WGPUBlendFactor_Zero;
WGPUColorStateDescriptor colorStateDescriptor = {};
colorStateDescriptor.format                   = _colorFormat;
colorStateDescriptor.alphaBlend               = blendDescriptor;
colorStateDescriptor.colorBlend               = blendDescriptor;
colorStateDescriptor.writeMask                = WGPUColorWriteMask_All;

descriptor.colorStateCount = 1;
descriptor.colorStates     = &colorStateDescriptor;

有没有我遗漏的设置,或者这是一个金丝雀bug?

EN

回答 1

Stack Overflow用户

发布于 2021-02-04 13:29:43

有一个0的alpha值,这就把它搞乱了:(在clearColor字段中将它改为1可以很好地工作:

代码语言:javascript
复制
colorAttachment.clearColor            = { 0.0f, 0.0f, 0.0f, 1.0f };
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66039687

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档