diff --git a/cocos/core/pipeline/shadow/shadow-stage.ts b/cocos/core/pipeline/shadow/shadow-stage.ts index f4e768a0b0..48666dac6c 100644 --- a/cocos/core/pipeline/shadow/shadow-stage.ts +++ b/cocos/core/pipeline/shadow/shadow-stage.ts @@ -110,6 +110,7 @@ export class ShadowStage extends RenderStage { const descriptorSet = this._globalDS!; const cmdBuff = pipeline.commandBuffers[0]; const level = this._level; + const device = pipeline.device; if (!this._light || !this._shadowFrameBuffer) { return; } this._pipeline.pipelineUBO.updateShadowUBOLight(descriptorSet, this._light, level); @@ -125,8 +126,13 @@ export class ShadowStage extends RenderStage { this._renderArea.width = shadowMapSize.x; this._renderArea.height = shadowMapSize.y; } else { + const screenSpaceSignY = device.capabilities.screenSpaceSignY; this._renderArea.x = level % 2 * 0.5 * shadowMapSize.x; - this._renderArea.y = (1 - Math.floor(level / 2)) * 0.5 * shadowMapSize.y; + if (screenSpaceSignY > 1.0) { + this._renderArea.y = (1 - Math.floor(level / 2)) * 0.5 * shadowMapSize.y; + } else { + this._renderArea.y = Math.floor(level / 2) * 0.5 * shadowMapSize.y; + } this._renderArea.width = 0.5 * shadowMapSize.x; this._renderArea.height = 0.5 * shadowMapSize.y; } @@ -142,7 +148,6 @@ export class ShadowStage extends RenderStage { default: } - const device = pipeline.device; const renderPass = this._shadowFrameBuffer.renderPass; cmdBuff.beginRenderPass(renderPass, this._shadowFrameBuffer, this._renderArea, diff --git a/native/cocos/renderer/gfx-wgpu/WGPUCommandBuffer.cpp b/native/cocos/renderer/gfx-wgpu/WGPUCommandBuffer.cpp index 3c3f53fd34..88a0b1a018 100644 --- a/native/cocos/renderer/gfx-wgpu/WGPUCommandBuffer.cpp +++ b/native/cocos/renderer/gfx-wgpu/WGPUCommandBuffer.cpp @@ -87,6 +87,8 @@ void CCWGPUCommandBuffer::begin(RenderPass * /*renderPass*/, uint32_t /*subpass* _numInstances = 0; _gpuCommandBufferObj->wgpuCommandEncoder = wgpuDeviceCreateCommandEncoder(CCWGPUDevice::getInstance()->gpuDeviceObject()->wgpuDevice, nullptr); + + _attachmentSet.clear(); } void CCWGPUCommandBuffer::end() { @@ -113,13 +115,21 @@ void CCWGPUCommandBuffer::beginRenderPass(RenderPass *renderPass, Framebuffer *f _frameBuffer = fbo; CCWGPUFramebuffer *ccFrameBuffer = static_cast(fbo); - const ColorAttachmentList &colorConfigs = renderPass->getColorAttachments(); const DepthStencilAttachment &depthStencilConfig = renderPass->getDepthStencilAttachment(); const TextureList &textures = ccFrameBuffer->getColorTextures(); Texture *dsTexture = ccFrameBuffer->getDepthStencilTexture(); CCWGPUSwapchain *swapchain = ccFrameBuffer->swapchain(); + bool renderingFullScreen = true; + bool needPartialClear = false; + for (size_t i = 0; i < textures.size(); ++i) { + if (renderArea.x != 0 || renderArea.y != 0 || renderArea.width != textures[i]->getWidth() || renderArea.height != textures[i]->getHeight()) { + renderingFullScreen = false; + break; + } + } + WGPURenderPassDescriptor &renderPassDesc = _gpuCommandBufferObj->renderPassDescriptor; ccstd::vector colorAttachments; if (colorConfigs.empty()) { @@ -139,14 +149,19 @@ void CCWGPUCommandBuffer::beginRenderPass(RenderPass *renderPass, Framebuffer *f for (size_t i = 0; i < colorConfigs.size(); i++) { WGPURenderPassColorAttachment *colorAttchments = ccnew WGPURenderPassColorAttachment[colorConfigs.size()]; + auto loadOp = toWGPULoadOp(colorConfigs[i].loadOp); + if (!renderingFullScreen) { + loadOp = _attachmentSet.find(textures[i]) == _attachmentSet.end() ? loadOp : WGPULoadOp_Load; + } WGPURenderPassColorAttachment color = { .view = static_cast(textures[i])->gpuTextureObject()->selfView, .resolveTarget = nullptr, // TODO_Zeqiang: wgpu offscr msaa - .loadOp = toWGPULoadOp(colorConfigs[i].loadOp), + .loadOp = loadOp, .storeOp = toWGPUStoreOp(colorConfigs[i].storeOp), .clearValue = toWGPUColor(colors[i]), }; colorAttachments.emplace_back(color); + _attachmentSet.insert(textures[i]); } } @@ -154,7 +169,7 @@ void CCWGPUCommandBuffer::beginRenderPass(RenderPass *renderPass, Framebuffer *f if (dsTexture) { WGPURenderPassDepthStencilAttachment depthStencil = { .view = static_cast(dsTexture)->gpuTextureObject()->selfView, - .depthLoadOp = WGPULoadOp_Clear, // toWGPULoadOp(depthStencilConfig.depthLoadOp), + .depthLoadOp = toWGPULoadOp(depthStencilConfig.depthLoadOp), .depthStoreOp = toWGPUStoreOp(depthStencilConfig.depthStoreOp), .depthClearValue = depth, .depthReadOnly = false, @@ -170,7 +185,7 @@ void CCWGPUCommandBuffer::beginRenderPass(RenderPass *renderPass, Framebuffer *f } else { WGPURenderPassDepthStencilAttachment depthStencil = { .view = swapchain->gpuSwapchainObject()->swapchainDepthStencil->gpuTextureObject()->selfView, - .depthLoadOp = WGPULoadOp_Clear, // toWGPULoadOp(depthStencilConfig.depthLoadOp), + .depthLoadOp = toWGPULoadOp(depthStencilConfig.depthLoadOp), .depthStoreOp = toWGPUStoreOp(depthStencilConfig.depthStoreOp), .depthClearValue = depth, .depthReadOnly = false, @@ -186,6 +201,12 @@ void CCWGPUCommandBuffer::beginRenderPass(RenderPass *renderPass, Framebuffer *f setViewport({renderArea.x, renderArea.y, renderArea.width, renderArea.height, 0.0F, 1.0F}); setScissor(renderArea); + if (!renderingFullScreen && needPartialClear) { + for (size_t i = 0; i < textures.size(); ++i) { + clearRect(this, textures[i], renderArea.x, renderArea.y, renderArea.width, renderArea.height, colors[i]); + } + } + _gpuCommandBufferObj->renderPassBegan = true; renderPassDesc.colorAttachments = colorAttachments.data(); diff --git a/native/cocos/renderer/gfx-wgpu/WGPUCommandBuffer.h b/native/cocos/renderer/gfx-wgpu/WGPUCommandBuffer.h index 01e7dcff64..63c43d81e7 100644 --- a/native/cocos/renderer/gfx-wgpu/WGPUCommandBuffer.h +++ b/native/cocos/renderer/gfx-wgpu/WGPUCommandBuffer.h @@ -103,6 +103,9 @@ protected: RenderPass *_renderPass = nullptr; Framebuffer *_frameBuffer = nullptr; + // first meet? + std::set _attachmentSet; + // command buffer inner impl // std::queue _renderPassFuncQ; // std::queue _computeFuncQ; diff --git a/native/cocos/renderer/gfx-wgpu/WGPUDevice.cpp b/native/cocos/renderer/gfx-wgpu/WGPUDevice.cpp index 016d3edacc..7d42799fd5 100644 --- a/native/cocos/renderer/gfx-wgpu/WGPUDevice.cpp +++ b/native/cocos/renderer/gfx-wgpu/WGPUDevice.cpp @@ -317,7 +317,7 @@ void CCWGPUDevice::copyBuffersToTexture(const uint8_t *const *buffers, Texture * .origin = WGPUOrigin3D{ static_cast(region.texOffset.x), static_cast(region.texOffset.y), - static_cast(d)}, + static_cast(l)}, .aspect = WGPUTextureAspect_All, }; @@ -338,7 +338,7 @@ void CCWGPUDevice::copyBuffersToTexture(const uint8_t *const *buffers, Texture * .origin = WGPUOrigin3D{ static_cast(region.texOffset.x), static_cast(h), - static_cast(d)}, + static_cast(l)}, .aspect = WGPUTextureAspect_All, }; diff --git a/native/cocos/renderer/gfx-wgpu/WGPUUtils.cpp b/native/cocos/renderer/gfx-wgpu/WGPUUtils.cpp index 42b888b61e..6a49e36cd8 100644 --- a/native/cocos/renderer/gfx-wgpu/WGPUUtils.cpp +++ b/native/cocos/renderer/gfx-wgpu/WGPUUtils.cpp @@ -1,9 +1,11 @@ #include "WGPUUtils.h" +#include "WGPUCommandBuffer.h" #include "WGPUDescriptorSet.h" #include "WGPUDescriptorSetLayout.h" #include "WGPUDevice.h" #include "WGPUObject.h" #include "WGPUPipelineLayout.h" +#include "WGPUTexture.h" namespace cc::gfx { @@ -46,4 +48,208 @@ void createPipelineLayoutFallback(const ccstd::vector& descripto } ccPipelineLayout->_hash = hash; } + +namespace { +struct ClearPassData { + WGPUShaderModule vertShader = wgpuDefaultHandle; + WGPUShaderModule fragShader = wgpuDefaultHandle; + WGPUBindGroupLayout bindGroupLayout = wgpuDefaultHandle; + WGPUPipelineLayout pipelineLayout = wgpuDefaultHandle; + WGPURenderPipeline pipeline = wgpuDefaultHandle; +}; +thread_local ClearPassData clearPassData; +} // namespace + +void clearRect(CommandBuffer* cmdBuff, Texture* texture, uint32_t x, uint32_t y, uint32_t w, uint32_t h, const Color& color) { + auto wgpuDevice = CCWGPUDevice::getInstance()->gpuDeviceObject()->wgpuDevice; + auto* ccTexture = static_cast(texture); + auto format = toWGPUTextureFormat(ccTexture->getFormat()); + auto dimension = toWGPUTextureDimension(ccTexture->getTextureType()); + auto* wgpuTexture = ccTexture->gpuTextureObject()->wgpuTexture; + + if (!clearPassData.vertShader) { + const char* clearQuadVert = R"( +struct VertexOutput { + @builtin(position) Position : vec4, +} + +@vertex +fn vert_main(@builtin(vertex_index) VertexIndex : u32) -> VertexOutput { + var pos = array, 6>( + vec2( 1.0, 1.0), + vec2( 1.0, -1.0), + vec2(-1.0, -1.0), + vec2( 1.0, 1.0), + vec2(-1.0, -1.0), + vec2(-1.0, 1.0) + ); + + var output : VertexOutput; + output.Position = vec4(pos[VertexIndex], 0.0, 1.0); + return output; +} + )"; + + const char* clearQuadFrag = R"( +struct ClearColor { + color: vec4, +} + +@group(0) @binding(0) var clearColor : ClearColor; + +@fragment +fn frag_main(@location(0) fragUV : vec2) -> @location(0) vec4 { + return clearColor; +} + )"; + + WGPUShaderModuleWGSLDescriptor wgslShaderDescVert; + wgslShaderDescVert.source = clearQuadVert; + wgslShaderDescVert.chain.sType = WGPUSType_ShaderModuleWGSLDescriptor; + WGPUShaderModuleDescriptor shaderDescVert; + shaderDescVert.nextInChain = reinterpret_cast(&wgslShaderDescVert); + shaderDescVert.label = "clearQuadVert"; + clearPassData.vertShader = wgpuDeviceCreateShaderModule(wgpuDevice, &shaderDescVert); + + WGPUShaderModuleWGSLDescriptor wgslShaderDescFrag; + wgslShaderDescFrag.source = clearQuadFrag; + wgslShaderDescFrag.chain.sType = WGPUSType_ShaderModuleWGSLDescriptor; + WGPUShaderModuleDescriptor shaderDescFrag; + shaderDescFrag.nextInChain = reinterpret_cast(&wgslShaderDescFrag); + shaderDescFrag.label = "clearQuadFrag"; + clearPassData.fragShader = wgpuDeviceCreateShaderModule(wgpuDevice, &shaderDescFrag); + + WGPUBindGroupLayoutEntry textureEntry; + textureEntry.binding = 0; + textureEntry.visibility = WGPUShaderStage_Fragment; + textureEntry.texture.sampleType = WGPUTextureSampleType_Undefined; + textureEntry.texture.viewDimension = WGPUTextureViewDimension_2D; + textureEntry.texture.multisampled = false; + textureEntry.buffer.type = WGPUBufferBindingType_Uniform; + textureEntry.sampler.type = WGPUSamplerBindingType_Undefined; + textureEntry.storageTexture.access = WGPUStorageTextureAccess_Undefined; + + WGPUBindGroupLayoutDescriptor bgLayoutDesc; + bgLayoutDesc.label = "clearPassBGLayout"; + bgLayoutDesc.entryCount = 1; + bgLayoutDesc.entries = &textureEntry; + clearPassData.bindGroupLayout = wgpuDeviceCreateBindGroupLayout(wgpuDevice, &bgLayoutDesc); + + WGPUPipelineLayoutDescriptor pipelineLayoutDesc; + pipelineLayoutDesc.label = "clearPassPipelineLayout"; + pipelineLayoutDesc.bindGroupLayoutCount = 1; + pipelineLayoutDesc.bindGroupLayouts = &clearPassData.bindGroupLayout; + clearPassData.pipelineLayout = wgpuDeviceCreatePipelineLayout(wgpuDevice, &pipelineLayoutDesc); + + WGPUVertexState vertexState; + vertexState.module = clearPassData.vertShader; + vertexState.entryPoint = "vert_main"; + + WGPUPrimitiveState primitiveState; + primitiveState.topology = WGPUPrimitiveTopology_TriangleList; + primitiveState.stripIndexFormat = WGPUIndexFormat_Undefined; + primitiveState.frontFace = WGPUFrontFace_CCW; + primitiveState.cullMode = WGPUCullMode_None; + + WGPUColorTargetState colorState; + colorState.format = format; + colorState.writeMask = WGPUColorWriteMask_All; + + WGPUFragmentState fragState; + fragState.module = clearPassData.fragShader; + fragState.entryPoint = "frag_main"; + fragState.targetCount = 1; + fragState.targets = &colorState; + + WGPUMultisampleState multisample; + multisample.count = 1; + multisample.alphaToCoverageEnabled = false; + multisample.mask = 0xFFFFFFFF; + + WGPURenderPipelineDescriptor pipelineDesc; + pipelineDesc.label = "clearPassPipeline"; + pipelineDesc.layout = clearPassData.pipelineLayout; + pipelineDesc.vertex = vertexState; + pipelineDesc.primitive = primitiveState; + pipelineDesc.fragment = &fragState; + pipelineDesc.depthStencil = nullptr; + pipelineDesc.multisample = multisample; + clearPassData.pipeline = wgpuDeviceCreateRenderPipeline(wgpuDevice, &pipelineDesc); + } + + CC_ASSERT(i > 0); + auto cmdBuffCommandEncoder = static_cast(cmdBuff)->gpuCommandBufferObject()->wgpuCommandEncoder; + auto commandEncoder = cmdBuffCommandEncoder; + if (!cmdBuffCommandEncoder) { + commandEncoder = wgpuDeviceCreateCommandEncoder(wgpuDevice, nullptr); + } + WGPUTextureViewDescriptor desc; + desc.format = format; + desc.dimension = WGPUTextureViewDimension_2D; + desc.baseMipLevel = 0; + desc.mipLevelCount = 1; + desc.baseArrayLayer = 0; + desc.arrayLayerCount = 1; + desc.aspect = WGPUTextureAspect_All; + auto dstView = wgpuTextureCreateView(wgpuTexture, &desc); + + WGPUBufferDescriptor bufferDesc; + bufferDesc.usage = WGPUBufferUsage_Vertex; + bufferDesc.size = 16; + bufferDesc.mappedAtCreation = true; + auto uniformBuffer = wgpuDeviceCreateBuffer(wgpuDevice, &bufferDesc); + + float colorArr[4] = {color.x, color.y, color.z, color.w}; + auto* mappedBuffer = wgpuBufferGetMappedRange(uniformBuffer, 0, 16); + memcpy(mappedBuffer, colorArr, 16); + wgpuBufferUnmap(uniformBuffer); + + WGPUBindGroupEntry entry; + entry.nextInChain = nullptr; + entry.binding = 0; + entry.sampler = wgpuDefaultHandle; + entry.buffer = uniformBuffer; + entry.textureView = wgpuDefaultHandle; + + WGPUBindGroupDescriptor bindgroupDesc; + bindgroupDesc.layout = clearPassData.bindGroupLayout; + bindgroupDesc.entryCount = 1; + bindgroupDesc.entries = &entry; + auto bindGroup = wgpuDeviceCreateBindGroup(wgpuDevice, &bindgroupDesc); + + WGPURenderPassColorAttachment colorAttachment; + colorAttachment.view = dstView; + colorAttachment.resolveTarget = wgpuDefaultHandle; + colorAttachment.loadOp = WGPULoadOp_Load; + colorAttachment.storeOp = WGPUStoreOp_Store; + // colorAttachment.clearValue = {0.88, 0.88, 0.88, 1.0}; + + WGPURenderPassDescriptor rpDesc; + rpDesc.nextInChain = nullptr; + rpDesc.label = nullptr; + rpDesc.colorAttachmentCount = 1; + rpDesc.colorAttachments = &colorAttachment; + rpDesc.depthStencilAttachment = nullptr; + rpDesc.occlusionQuerySet = nullptr; + rpDesc.timestampWriteCount = 0; + rpDesc.timestampWrites = nullptr; + auto renderPassEncoder = wgpuCommandEncoderBeginRenderPass(commandEncoder, &rpDesc); + + wgpuRenderPassEncoderSetPipeline(renderPassEncoder, clearPassData.pipeline); + wgpuRenderPassEncoderSetBindGroup(renderPassEncoder, 0, bindGroup, 0, nullptr); + wgpuRenderPassEncoderDraw(renderPassEncoder, 6, 1, 0, 0); + wgpuRenderPassEncoderEnd(renderPassEncoder); + wgpuRenderPassEncoderRelease(renderPassEncoder); + wgpuBindGroupRelease(bindGroup); + wgpuTextureViewRelease(dstView); + wgpuBufferRelease(uniformBuffer); + + if (!cmdBuffCommandEncoder) { + auto commandBuffer = wgpuCommandEncoderFinish(commandEncoder, nullptr); + wgpuQueueSubmit(CCWGPUDevice::getInstance()->gpuDeviceObject()->wgpuQueue, 1, &commandBuffer); + wgpuCommandEncoderRelease(commandEncoder); + wgpuCommandBufferRelease(commandBuffer); + } +} + } // namespace cc::gfx diff --git a/native/cocos/renderer/gfx-wgpu/WGPUUtils.h b/native/cocos/renderer/gfx-wgpu/WGPUUtils.h index 9a73bb7946..d53ba3fab7 100644 --- a/native/cocos/renderer/gfx-wgpu/WGPUUtils.h +++ b/native/cocos/renderer/gfx-wgpu/WGPUUtils.h @@ -589,7 +589,7 @@ static WGPUFlags toWGPUColorWriteMask(ColorMask mask) { } static ccstd::string getAdapterTypeName(WGPUAdapterType type) { - switch(type) { + switch (type) { case WGPUAdapterType_DiscreteGPU: return "WGPUAdapterType_DiscreteGPU"; case WGPUAdapterType_IntegratedGPU: @@ -597,14 +597,14 @@ static ccstd::string getAdapterTypeName(WGPUAdapterType type) { case WGPUAdapterType_CPU: return "WGPUAdapterType_CPU"; case WGPUAdapterType_Unknown: - return "WGPUAdapterType_Unknown"; + return "WGPUAdapterType_Unknown"; default: return "unknown adapter by cc.gfx!"; } } static ccstd::string getBackendTypeName(WGPUBackendType type) { - switch(type) { + switch (type) { case WGPUBackendType_Null: return "WGPUBackendType_Null"; case WGPUBackendType_WebGPU: @@ -631,6 +631,10 @@ class PipelineLayout; // descriptor set layout in descriptor set not consistent with the binding in pipeline layout. void createPipelineLayoutFallback(const ccstd::vector& descriptorSets, PipelineLayout* pipelineLayout); +class Texture; +class CommandBuffer; +void clearRect(CommandBuffer* cmdBuffer, Texture* texture, uint32_t x, uint32_t y, uint32_t w, uint32_t h, const Color& color); + static constexpr WGPUColor defaultClearColor{0.2, 0.2, 0.2, 1.0}; static constexpr float defaultClearDepth = 1.0f; diff --git a/native/external-config.json b/native/external-config.json index 6a54257f4e..33b5a243f5 100644 --- a/native/external-config.json +++ b/native/external-config.json @@ -3,6 +3,6 @@ "type": "github", "owner": "cocos-creator", "name": "engine-native-external", - "checkout": "v3.6.2-4" + "checkout": "v3.6.2-5" } -} +} \ No newline at end of file