| // Copyright 2022 The Dawn & Tint Authors | |
| // | |
| // Redistribution and use in source and binary forms, with or without | |
| // modification, are permitted provided that the following conditions are met: | |
| // | |
| // 1. Redistributions of source code must retain the above copyright notice, this | |
| // list of conditions and the following disclaimer. | |
| // | |
| // 2. Redistributions in binary form must reproduce the above copyright notice, | |
| // this list of conditions and the following disclaimer in the documentation | |
| // and/or other materials provided with the distribution. | |
| // | |
| // 3. Neither the name of the copyright holder nor the names of its | |
| // contributors may be used to endorse or promote products derived from | |
| // this software without specific prior written permission. | |
| // | |
| // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
| // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
| // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
| // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | |
| // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
| // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
| // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | |
| // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | |
| // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
| // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| #include "src/dawn/native/ApplyClearWithDrawHelper.h" | |
| #include <limits> | |
| #include <string> | |
| #include <utility> | |
| #include "src/dawn/common/Enumerator.h" | |
| #include "src/dawn/common/Range.h" | |
| #include "src/dawn/common/Strings.h" | |
| #include "src/dawn/native/BindGroup.h" | |
| #include "src/dawn/native/BindGroupLayout.h" | |
| #include "src/dawn/native/Buffer.h" | |
| #include "src/dawn/native/CommandEncoder.h" | |
| #include "src/dawn/native/Device.h" | |
| #include "src/dawn/native/InternalPipelineStore.h" | |
| #include "src/dawn/native/ObjectContentHasher.h" | |
| #include "src/dawn/native/RenderPassEncoder.h" | |
| #include "src/dawn/native/RenderPipeline.h" | |
| #include "src/dawn/native/utils/NativeHelpers.h" | |
| #include "src/dawn/native/webgpu_absl_format.h" | |
| #include "src/utils/compiler.h" | |
| #include "src/utils/numeric.h" | |
| #include "src/utils/span.h" | |
| namespace dawn::native { | |
| namespace { | |
| // General helper functions and data structures for applying clear values with draw | |
| static const char kVSSource[] = DAWN_MULTILINE( | |
| @vertex | |
| fn vs_main(@builtin(vertex_index) vertexIndex : u32) -> @builtin(position) vec4f { | |
| var pos = array( | |
| vec2f(-1.0, -1.0), | |
| vec2f( 3.0, -1.0), | |
| vec2f(-1.0, 3.0)); | |
| return vec4f(pos[vertexIndex], clearValues.depth, 1.0); | |
| } | |
| ); | |
| const char* GetTextureComponentTypeString(DeviceBase* device, wgpu::TextureFormat format) { | |
| DAWN_CHECK(format != wgpu::TextureFormat::Undefined); | |
| const Format& formatInfo = device->GetValidInternalFormat(format); | |
| switch (formatInfo.GetAspectInfo(Aspect::Color).baseType) { | |
| case TextureComponentType::Sint: | |
| return "i32"; | |
| case TextureComponentType::Uint: | |
| return "u32"; | |
| case TextureComponentType::Float: | |
| return "f32"; | |
| } | |
| DAWN_UNREACHABLE(); | |
| } | |
| // Construct the vertex and fragment shaders to apply the input clear values to the corresponding | |
| // attachments of KeyOfApplyClearWithDrawPipelines. | |
| // The shader will look like: | |
| // | |
| // enable chromium_experimental_pixel_local; | |
| // struct PLS { | |
| // a0 : u32, | |
| // a1 : u32, | |
| // a2 : u32, | |
| // a3 : u32, | |
| // } | |
| // | |
| // var<pixel_local> pls : PLS; | |
| // struct Output { | |
| // @location(0) output0 : vec4<f32>, | |
| // } | |
| // struct ClearValues { | |
| // color0 : vec4<f32>, | |
| // depth : f32, | |
| // } | |
| // | |
| // @group(0) @binding(0) var<uniform> clearValues : ClearValues; | |
| // | |
| // @vertex | |
| // fn vs_main(@builtin(vertex_index) vertexIndex : u32) -> @builtin(position) vec4f { | |
| // var pos = array( | |
| // vec2f(-1.0, -1.0), | |
| // vec2f( 3.0, -1.0), | |
| // vec2f(-1.0, 3.0)); | |
| // return vec4f(pos[vertexIndex], clearValues.depth, 1.0); | |
| // } | |
| // | |
| // @fragment | |
| // fn fs_main() -> Output { | |
| // var output : Output; | |
| // output.output0 = clearValues.color0; | |
| // _ = pls.a0; | |
| // | |
| // return output; | |
| // } | |
| std::string ConstructShader(DeviceBase* device, const KeyOfApplyClearWithDrawPipelines& key) { | |
| std::ostringstream outputDeclarationStream; | |
| std::ostringstream clearValueUniformBufferDeclarationStream; | |
| std::ostringstream assignOutputStream; | |
| if (key.colorTargetsToApplyClearColorValue.any()) { | |
| outputDeclarationStream << "struct Output {\n"; | |
| } | |
| clearValueUniformBufferDeclarationStream << "struct ClearValues {\n"; | |
| // Only generate the assignments we need. | |
| for (auto i : key.colorTargetsToApplyClearColorValue) { | |
| wgpu::TextureFormat currentFormat = key.colorTargetFormats[i]; | |
| DAWN_CHECK(currentFormat != wgpu::TextureFormat::Undefined); | |
| const char* type = GetTextureComponentTypeString(device, currentFormat); | |
| outputDeclarationStream << absl::StrFormat(" @location(%u) output%u : vec4<%s>,\n", i, i, | |
| type); | |
| clearValueUniformBufferDeclarationStream | |
| << absl::StrFormat(" color%u : vec4<%s>,\n", i, type); | |
| assignOutputStream << absl::StrFormat(" output.output%u = clearValues.color%u;\n", i, i); | |
| } | |
| clearValueUniformBufferDeclarationStream << " depth : f32,\n"; | |
| if (key.colorTargetsToApplyClearColorValue.any()) { | |
| outputDeclarationStream << "}\n"; | |
| } | |
| clearValueUniformBufferDeclarationStream << "}\n"; | |
| std::ostringstream shaderStream; | |
| if (key.hasPLS) { | |
| std::vector<const char*> plsTypes; | |
| const size_t plsSlotCount = | |
| checked_cast<size_t>(key.totalPixelLocalStorageSize / kPLSSlotByteSize); | |
| plsTypes.resize(plsSlotCount, "u32"); | |
| for (const auto& attachment : key.plsAttachments) { | |
| switch (attachment.format) { | |
| case wgpu::TextureFormat::R32Uint: | |
| plsTypes[checked_cast<size_t>(attachment.offset / kPLSSlotByteSize)] = "u32"; | |
| break; | |
| case wgpu::TextureFormat::R32Sint: | |
| plsTypes[checked_cast<size_t>(attachment.offset / kPLSSlotByteSize)] = "i32"; | |
| break; | |
| case wgpu::TextureFormat::R32Float: | |
| plsTypes[checked_cast<size_t>(attachment.offset / kPLSSlotByteSize)] = "f32"; | |
| break; | |
| default: | |
| DAWN_UNREACHABLE(); | |
| } | |
| } | |
| shaderStream << "enable chromium_experimental_pixel_local;"; | |
| std::ostringstream plsDeclarationStream; | |
| plsDeclarationStream << "struct PLS {\n"; | |
| for (size_t i = 0; i < plsSlotCount; ++i) { | |
| plsDeclarationStream << absl::StrFormat(" a%u : %s,\n", i, plsTypes[i]); | |
| } | |
| plsDeclarationStream << "}\n"; | |
| if (plsSlotCount) { | |
| // Read the PLS with a phony-assignment to mark the PLS as statically used for | |
| // compatibility with the render pass. | |
| assignOutputStream << "_ = pls.a0;"; | |
| } | |
| shaderStream << plsDeclarationStream.str() << "var<pixel_local> pls : PLS;"; | |
| } | |
| shaderStream << outputDeclarationStream.str() << clearValueUniformBufferDeclarationStream.str(); | |
| shaderStream << "@group(0) @binding(0) var<uniform> clearValues : ClearValues;\n"; | |
| shaderStream << kVSSource; | |
| if (key.colorTargetsToApplyClearColorValue.any()) { | |
| shaderStream << DAWN_MULTILINE( | |
| @fragment | |
| fn fs_main() -> Output { | |
| var output : Output; | |
| ); | |
| shaderStream << assignOutputStream.str(); | |
| shaderStream << DAWN_MULTILINE( | |
| return output; | |
| } | |
| ); | |
| } else { | |
| // When only clearing depth, depth is written via the vertex shader's @builtin(position).z | |
| // and all color targets have ColorWriteMask::None, so the fragment shader does not need to | |
| // write any outputs (it only needs to touch PLS if PLS is active in the render pass). | |
| shaderStream << "@fragment\nfn fs_main() {\n" << assignOutputStream.str() << "}\n"; | |
| } | |
| return shaderStream.str(); | |
| } | |
| RenderPipelineBase* GetCachedPipeline(InternalPipelineStore* store, | |
| const KeyOfApplyClearWithDrawPipelines& key) { | |
| auto iter = store->applyClearWithDrawPipelines.find(key); | |
| if (iter != store->applyClearWithDrawPipelines.end()) { | |
| return iter->second.Get(); | |
| } | |
| return nullptr; | |
| } | |
| ResultOrError<RenderPipelineBase*> GetOrCreateApplyClearWithDrawPipeline( | |
| DeviceBase* device, | |
| const KeyOfApplyClearWithDrawPipelines& key) { | |
| InternalPipelineStore* store = device->GetInternalPipelineStore(); | |
| RenderPipelineBase* cachedPipeline = GetCachedPipeline(store, key); | |
| if (cachedPipeline != nullptr) { | |
| return cachedPipeline; | |
| } | |
| std::string shader = ConstructShader(device, key); | |
| Ref<ShaderModuleBase> shaderModule; | |
| DAWN_TRY_ASSIGN(shaderModule, utils::CreateShaderModule(device, shader.c_str())); | |
| // Prepare the vertex stage | |
| VertexState vertex = {}; | |
| vertex.module = shaderModule.Get(); | |
| vertex.entryPoint = "vs_main"; | |
| // Prepare the color states | |
| PerColorAttachment<ColorTargetState> colorTargets = {}; | |
| for (auto [i, target] : Enumerate(colorTargets)) { | |
| target.format = key.colorTargetFormats[i]; | |
| // We shouldn't change the color targets that are not involved in. | |
| if (!key.colorTargetsToApplyClearColorValue[i]) { | |
| target.writeMask = wgpu::ColorWriteMask::None; | |
| } | |
| } | |
| // Prepare the fragment stage | |
| FragmentState fragment = {}; | |
| fragment.module = shaderModule.Get(); | |
| fragment.entryPoint = "fs_main"; | |
| fragment.targets = colorTargets; | |
| // Create RenderPipeline | |
| RenderPipelineDescriptor renderPipelineDesc = {}; | |
| renderPipelineDesc.vertex = vertex; | |
| renderPipelineDesc.fragment = &fragment; | |
| renderPipelineDesc.multisample.count = key.sampleCount; | |
| DepthStencilState depthStencilState = {}; | |
| depthStencilState.depthWriteEnabled = | |
| key.shouldClearDepth ? wgpu::OptionalBool::True : wgpu::OptionalBool::False; | |
| if (key.shouldClearDepth) { | |
| depthStencilState.depthCompare = wgpu::CompareFunction::Always; | |
| } | |
| if (key.depthStencilFormat != wgpu::TextureFormat::Undefined) { | |
| depthStencilState.format = key.depthStencilFormat; | |
| renderPipelineDesc.depthStencil = &depthStencilState; | |
| } | |
| // Build the pipeline layout explicitly as we might need to add PLS information to it. | |
| wgpu::ShaderStage stages = wgpu::ShaderStage::Vertex; | |
| if (key.colorTargetsToApplyClearColorValue.any()) { | |
| stages |= wgpu::ShaderStage::Fragment; | |
| } | |
| Ref<BindGroupLayoutBase> bgl; | |
| DAWN_TRY_ASSIGN( | |
| bgl, utils::MakeBindGroupLayout(device, {{0, stages, wgpu::BufferBindingType::Uniform}})); | |
| PipelineLayoutDescriptor pipelineLayoutDesc{}; | |
| pipelineLayoutDesc.bindGroupLayouts = SpanFromRef<BindGroupIndex>(bgl.Get()); | |
| wgpu::PipelineLayoutPixelLocalStorage pls; | |
| if (key.hasPLS) { | |
| pls.totalPixelLocalStorageSize = key.totalPixelLocalStorageSize; | |
| pls.storageAttachmentCount = key.plsAttachments.size(); | |
| pls.storageAttachments = key.plsAttachments.data(); | |
| pipelineLayoutDesc.nextInChain = &pls; | |
| } | |
| Ref<PipelineLayoutBase> pipelineLayout; | |
| DAWN_TRY_ASSIGN(pipelineLayout, device->CreatePipelineLayout(&pipelineLayoutDesc)); | |
| renderPipelineDesc.layout = pipelineLayout.Get(); | |
| Ref<RenderPipelineBase> pipeline; | |
| DAWN_TRY_ASSIGN(pipeline, device->CreateRenderPipeline(&renderPipelineDesc)); | |
| store->applyClearWithDrawPipelines.emplace(key, std::move(pipeline)); | |
| return GetCachedPipeline(store, key); | |
| } | |
| ResultOrError<Ref<BufferBase>> CreateUniformBufferWithClearValues( | |
| CommandEncoder* encoder, | |
| const RenderPassDescriptor* renderPassDescriptor, | |
| const KeyOfApplyClearWithDrawPipelines& key) { | |
| std::array<std::byte, sizeof(uint32_t) * 4 * kMaxColorAttachments + sizeof(float) * 4> | |
| clearValues = {}; | |
| Span<std::byte> clearValueBuffer = clearValues; | |
| for (auto i : key.colorTargetsToApplyClearColorValue) { | |
| const RenderPassColorAttachment& colorAttachment = | |
| renderPassDescriptor->colorAttachments[i]; | |
| const Format& format = colorAttachment.view->GetFormat(); | |
| TextureComponentType baseType = format.GetAspectInfo(Aspect::Color).baseType; | |
| Color initialClearValue = colorAttachment.clearValue; | |
| Color clearValue = ClampClearColorValueToLegalRange(initialClearValue, format); | |
| switch (baseType) { | |
| case TextureComponentType::Uint: { | |
| Span<uint32_t> clearValueSubBuffer = | |
| ReinterpretSpan<uint32_t>(clearValueBuffer.TakeFirst(sizeof(uint32_t) * 4)); | |
| clearValueSubBuffer[0] = static_cast<uint32_t>(clearValue.r); | |
| clearValueSubBuffer[1] = static_cast<uint32_t>(clearValue.g); | |
| clearValueSubBuffer[2] = static_cast<uint32_t>(clearValue.b); | |
| clearValueSubBuffer[3] = static_cast<uint32_t>(clearValue.a); | |
| break; | |
| } | |
| case TextureComponentType::Sint: { | |
| Span<int32_t> clearValueSubBuffer = | |
| ReinterpretSpan<int32_t>(clearValueBuffer.TakeFirst(sizeof(int32_t) * 4)); | |
| clearValueSubBuffer[0] = static_cast<int32_t>(clearValue.r); | |
| clearValueSubBuffer[1] = static_cast<int32_t>(clearValue.g); | |
| clearValueSubBuffer[2] = static_cast<int32_t>(clearValue.b); | |
| clearValueSubBuffer[3] = static_cast<int32_t>(clearValue.a); | |
| break; | |
| } | |
| case TextureComponentType::Float: { | |
| Span<float> clearValueSubBuffer = | |
| ReinterpretSpan<float>(clearValueBuffer.TakeFirst(sizeof(float) * 4)); | |
| clearValueSubBuffer[0] = static_cast<float>(clearValue.r); | |
| clearValueSubBuffer[1] = static_cast<float>(clearValue.g); | |
| clearValueSubBuffer[2] = static_cast<float>(clearValue.b); | |
| clearValueSubBuffer[3] = static_cast<float>(clearValue.a); | |
| break; | |
| } | |
| } | |
| } | |
| Span<float> clearDepthSubBuffer = | |
| ReinterpretSpan<float>(clearValueBuffer.TakeFirst(sizeof(float) * 4)); | |
| if (key.shouldClearDepth) { | |
| clearDepthSubBuffer[0] = renderPassDescriptor->depthStencilAttachment->depthClearValue; | |
| } | |
| size_t clearValuesSize = clearValues.size() - clearValueBuffer.size(); | |
| DAWN_CHECK(clearValuesSize > 0); | |
| Ref<BufferBase> buffer; | |
| DAWN_TRY_ASSIGN(buffer, | |
| encoder->GetDevice()->GetOrCreateTemporaryUniformBuffer(clearValuesSize)); | |
| buffer->SetLabel("Internal_UniformClearValues"); | |
| encoder->APIWriteBuffer(buffer.Get(), 0, | |
| Span<const std::byte>(clearValues).first(clearValuesSize)); | |
| return std::move(buffer); | |
| } | |
| bool NeedsBigIntClear(const Format& format, const std::array<double, 4>& clearValue) { | |
| // We should only apply this workaround on 32-bit signed and unsigned integer formats. | |
| switch (format.format) { | |
| case wgpu::TextureFormat::R32Sint: | |
| case wgpu::TextureFormat::RG32Sint: | |
| case wgpu::TextureFormat::RGBA32Sint: | |
| case wgpu::TextureFormat::R32Uint: | |
| case wgpu::TextureFormat::RG32Uint: | |
| case wgpu::TextureFormat::RGBA32Uint: | |
| break; | |
| default: | |
| return false; | |
| } | |
| for (uint8_t i = 0; i < format.componentCount; ++i) { | |
| double value = clearValue[i]; | |
| switch (format.GetAspectInfo(Aspect::Color).baseType) { | |
| case TextureComponentType::Uint: { | |
| constexpr double kMaxUintRepresentableInFloat = | |
| 1 << std::numeric_limits<float>::digits; | |
| if (value > kMaxUintRepresentableInFloat) { | |
| return true; | |
| } | |
| break; | |
| } | |
| case TextureComponentType::Sint: { | |
| constexpr double kMaxSintRepresentableInFloat = | |
| 1 << std::numeric_limits<float>::digits; | |
| constexpr double kMinSintRepresentableInFloat = -kMaxSintRepresentableInFloat; | |
| if (value > kMaxSintRepresentableInFloat || value < kMinSintRepresentableInFloat) { | |
| return true; | |
| } | |
| break; | |
| } | |
| case TextureComponentType::Float: | |
| DAWN_UNREACHABLE(); | |
| return false; | |
| } | |
| } | |
| return false; | |
| } | |
| bool ShouldApplyClearColorWithDraw(const DeviceBase* device, | |
| const TextureViewBase* view, | |
| wgpu::LoadOp loadOp, | |
| const std::array<double, 4>& clearValue) { | |
| bool clearWithDraw = device->IsToggleEnabled(Toggle::ClearColorWithDraw); | |
| bool clearWithDrawForBigInt = | |
| device->IsToggleEnabled(Toggle::ApplyClearBigIntegerColorValueWithDraw); | |
| if (!clearWithDraw && !clearWithDrawForBigInt) { | |
| return false; | |
| } | |
| if (view == nullptr) { | |
| return false; | |
| } | |
| if (loadOp != wgpu::LoadOp::Clear) { | |
| return false; | |
| } | |
| if (!clearWithDraw && clearWithDrawForBigInt) { | |
| return NeedsBigIntClear(view->GetFormat(), clearValue); | |
| } | |
| return true; | |
| } | |
| bool ShouldApplyClearDepthWithDraw(const DeviceBase* device, | |
| const TextureViewBase* view, | |
| bool depthReadOnly, | |
| wgpu::LoadOp depthLoadOp) { | |
| if (!device->IsToggleEnabled(Toggle::ClearDepthWithDraw)) { | |
| return false; | |
| } | |
| if (view == nullptr || !view->GetFormat().HasDepth() || depthReadOnly) { | |
| return false; | |
| } | |
| if (depthLoadOp != wgpu::LoadOp::Clear) { | |
| return false; | |
| } | |
| return true; | |
| } | |
| bool GetKeyOfApplyClearWithDrawPipelines( | |
| const DeviceBase* device, | |
| const UnpackedPtr<RenderPassDescriptor>& renderPassDescriptor, | |
| KeyOfApplyClearWithDrawPipelines* key) { | |
| bool clearWithDraw = device->IsToggleEnabled(Toggle::ClearColorWithDraw); | |
| bool clearWithDrawForBigInt = | |
| device->IsToggleEnabled(Toggle::ApplyClearBigIntegerColorValueWithDraw); | |
| bool clearDepthWithDraw = device->IsToggleEnabled(Toggle::ClearDepthWithDraw); | |
| if (!clearWithDraw && !clearWithDrawForBigInt && !clearDepthWithDraw) { | |
| return false; | |
| } | |
| key->colorAttachmentCount = renderPassDescriptor->colorAttachments.size(); | |
| key->colorTargetFormats.fill(wgpu::TextureFormat::Undefined); | |
| for (auto [i, attachment] : Enumerate(renderPassDescriptor->colorAttachments)) { | |
| if (attachment.view == nullptr) { | |
| continue; | |
| } | |
| key->colorTargetFormats[i] = attachment.view->GetFormat().format; | |
| if (key->sampleCount == 0) { | |
| key->sampleCount = attachment.view->GetTexture()->GetSampleCount(); | |
| } else { | |
| DAWN_ASSERT(key->sampleCount == attachment.view->GetTexture()->GetSampleCount()); | |
| } | |
| Color clearValue = attachment.clearValue; | |
| std::array<double, 4> clearValueInArray = { | |
| {clearValue.r, clearValue.g, clearValue.b, clearValue.a}}; | |
| if (ShouldApplyClearColorWithDraw(device, attachment.view, attachment.loadOp, | |
| clearValueInArray)) { | |
| key->colorTargetsToApplyClearColorValue.set(i); | |
| } | |
| } | |
| if (renderPassDescriptor->depthStencilAttachment && | |
| renderPassDescriptor->depthStencilAttachment->view != nullptr) { | |
| const auto* depthStencilAttachment = renderPassDescriptor->depthStencilAttachment; | |
| key->depthStencilFormat = depthStencilAttachment->view->GetFormat().format; | |
| if (key->sampleCount == 0) { | |
| key->sampleCount = depthStencilAttachment->view->GetTexture()->GetSampleCount(); | |
| } | |
| if (ShouldApplyClearDepthWithDraw(device, depthStencilAttachment->view, | |
| depthStencilAttachment->depthReadOnly, | |
| depthStencilAttachment->depthLoadOp)) { | |
| key->shouldClearDepth = true; | |
| } | |
| } | |
| if (key->colorTargetsToApplyClearColorValue.none() && !key->shouldClearDepth) { | |
| return false; | |
| } | |
| if (const auto* pls = renderPassDescriptor.Get<RenderPassPixelLocalStorage>()) { | |
| key->hasPLS = true; | |
| key->totalPixelLocalStorageSize = pls->totalPixelLocalStorageSize; | |
| for (const RenderPassStorageAttachment& attachmentIn : pls->storageAttachments) { | |
| wgpu::PipelineLayoutStorageAttachment attachment{}; | |
| attachment.format = attachmentIn.storage->GetFormat().format; | |
| attachment.offset = attachmentIn.offset; | |
| key->plsAttachments.push_back(std::move(attachment)); | |
| } | |
| // Sort the PLS attachments by offset to make sure the order is deterministic. | |
| std::sort( | |
| key->plsAttachments.begin(), key->plsAttachments.end(), | |
| [](const wgpu::PipelineLayoutStorageAttachment& a, | |
| const wgpu::PipelineLayoutStorageAttachment& b) { return a.offset < b.offset; }); | |
| } | |
| return true; | |
| } | |
| } // namespace | |
| // TODO(crbug.com/364834368): Move handling of workaround at command submission time instead of | |
| // recording time. | |
| ColorAttachmentMask ClearWithDrawHelper::GetAppliedColorAttachments( | |
| const DeviceBase* device, | |
| BeginRenderPassCmd* renderPass) { | |
| ColorAttachmentMask mask; | |
| for (auto i : renderPass->attachmentState->GetColorAttachmentsMask()) { | |
| const auto clearValue = renderPass->colorAttachments[i].clearColor; | |
| std::array<double, 4> clearValueInArray = { | |
| {clearValue.r, clearValue.g, clearValue.b, clearValue.a}}; | |
| if (!ShouldApplyClearColorWithDraw(device, renderPass->colorAttachments[i].view.Get(), | |
| renderPass->colorAttachments[i].loadOp, | |
| clearValueInArray)) { | |
| continue; | |
| } | |
| mask.set(i); | |
| } | |
| return mask; | |
| } | |
| bool ClearWithDrawHelper::IsDepthApplied(const DeviceBase* device, BeginRenderPassCmd* renderPass) { | |
| if (!renderPass->attachmentState->HasDepthStencilAttachment()) { | |
| return false; | |
| } | |
| const auto& attachment = renderPass->depthStencilAttachment; | |
| return ShouldApplyClearDepthWithDraw(device, attachment.view.Get(), attachment.depthReadOnly, | |
| attachment.depthLoadOp); | |
| } | |
| size_t KeyOfApplyClearWithDrawPipelinesHashFunc::operator()( | |
| KeyOfApplyClearWithDrawPipelines key) const { | |
| size_t hash = 0; | |
| HashCombine(&hash, key.colorAttachmentCount); | |
| HashCombine(&hash, key.colorTargetsToApplyClearColorValue); | |
| for (wgpu::TextureFormat format : key.colorTargetFormats) { | |
| HashCombine(&hash, format); | |
| } | |
| HashCombine(&hash, key.sampleCount); | |
| HashCombine(&hash, key.depthStencilFormat); | |
| HashCombine(&hash, key.shouldClearDepth); | |
| HashCombine(&hash, key.hasPLS); | |
| HashCombine(&hash, key.totalPixelLocalStorageSize); | |
| for (const auto attachment : key.plsAttachments) { | |
| HashCombine(&hash, attachment.offset); | |
| HashCombine(&hash, attachment.format); | |
| } | |
| return hash; | |
| } | |
| bool KeyOfApplyClearWithDrawPipelinesEqualityFunc::operator()( | |
| KeyOfApplyClearWithDrawPipelines key1, | |
| KeyOfApplyClearWithDrawPipelines key2) const { | |
| if (key1.colorAttachmentCount != key2.colorAttachmentCount) { | |
| return false; | |
| } | |
| if (key1.colorTargetsToApplyClearColorValue != key2.colorTargetsToApplyClearColorValue) { | |
| return false; | |
| } | |
| for (auto i : Range(key1.colorTargetFormats.size())) { | |
| if (key1.colorTargetFormats[i] != key2.colorTargetFormats[i]) { | |
| return false; | |
| } | |
| } | |
| if (key1.hasPLS != key2.hasPLS || | |
| key1.totalPixelLocalStorageSize != key2.totalPixelLocalStorageSize || | |
| key1.plsAttachments.size() != key2.plsAttachments.size()) { | |
| return false; | |
| } | |
| for (auto i : Range(key1.plsAttachments.size())) { | |
| if (key1.plsAttachments[i].format != key2.plsAttachments[i].format || | |
| key1.plsAttachments[i].offset != key2.plsAttachments[i].offset) { | |
| return false; | |
| } | |
| } | |
| return key1.sampleCount == key2.sampleCount && | |
| key1.depthStencilFormat == key2.depthStencilFormat && | |
| key1.shouldClearDepth == key2.shouldClearDepth; | |
| } | |
| ClearWithDrawHelper::ClearWithDrawHelper() = default; | |
| ClearWithDrawHelper::~ClearWithDrawHelper() = default; | |
| MaybeError ClearWithDrawHelper::Initialize( | |
| CommandEncoder* encoder, | |
| const UnpackedPtr<RenderPassDescriptor>& renderPassDescriptor) { | |
| DeviceBase* device = encoder->GetDevice(); | |
| mShouldRun = GetKeyOfApplyClearWithDrawPipelines(device, renderPassDescriptor, &mKey); | |
| if (!mShouldRun) { | |
| return {}; | |
| } | |
| DAWN_TRY_ASSIGN(mUniformBufferWithClearValues, | |
| CreateUniformBufferWithClearValues(encoder, *renderPassDescriptor, mKey)); | |
| return {}; | |
| } | |
| MaybeError ClearWithDrawHelper::Apply(RenderPassEncoder* renderPassEncoder) { | |
| if (!mShouldRun) { | |
| return {}; | |
| } | |
| DeviceBase* device = renderPassEncoder->GetDevice(); | |
| RenderPipelineBase* pipeline = nullptr; | |
| DAWN_TRY_ASSIGN(pipeline, GetOrCreateApplyClearWithDrawPipeline(device, mKey)); | |
| Ref<BindGroupLayoutBase> layout; | |
| DAWN_TRY_ASSIGN(layout, pipeline->GetBindGroupLayout(0)); | |
| Ref<BindGroupBase> bindGroup; | |
| DAWN_TRY_ASSIGN(bindGroup, | |
| utils::MakeBindGroup(device, layout, {{0, mUniformBufferWithClearValues}}, | |
| UsageValidationMode::Internal)); | |
| renderPassEncoder->APISetBindGroup(0, bindGroup.Get()); | |
| renderPassEncoder->APISetPipeline(pipeline); | |
| renderPassEncoder->APIDraw(3); | |
| return {}; | |
| } | |
| } // namespace dawn::native |