From f08f274f0971455e46f4e99feced75d62c3a6690 Mon Sep 17 00:00:00 2001 From: zach Date: Wed, 9 Aug 2023 18:07:28 +0800 Subject: [PATCH] NativePipeline external texture --- cocos/asset/assets/simple-texture.ts | 2 +- cocos/rendering/custom/pipeline.ts | 33 ++++++- cocos/rendering/custom/render-graph.ts | 3 +- cocos/rendering/custom/web-pipeline.ts | 88 ++++++++++++++++-- native/cocos/core/assets/SimpleTexture.cpp | 2 +- .../pipeline/custom/FrameGraphDispatcher.cpp | 25 ++++-- .../pipeline/custom/LayoutGraphUtils.cpp | 29 ++++++ .../pipeline/custom/LayoutGraphUtils.h | 4 + .../pipeline/custom/NativePipeline.cpp | 89 ++++++++++++++++++- .../pipeline/custom/NativePipelineTypes.h | 6 ++ .../pipeline/custom/NativeResourceGraph.cpp | 42 ++------- .../pipeline/custom/RenderGraphTypes.h | 2 + .../pipeline/custom/RenderInterfaceTypes.h | 6 ++ 13 files changed, 272 insertions(+), 59 deletions(-) diff --git a/cocos/asset/assets/simple-texture.ts b/cocos/asset/assets/simple-texture.ts index 9d902b38896..1bd3e8907c0 100644 --- a/cocos/asset/assets/simple-texture.ts +++ b/cocos/asset/assets/simple-texture.ts @@ -310,7 +310,7 @@ export class SimpleTexture extends TextureBase { } } const textureCreateInfo = this._getGfxTextureCreateInfo({ - usage: TextureUsageBit.SAMPLED | TextureUsageBit.TRANSFER_DST, + usage: TextureUsageBit.SAMPLED | TextureUsageBit.TRANSFER_DST | TextureUsageBit.COLOR_ATTACHMENT, format: this._getGFXFormat(), levelCount: this._mipmapLevel, flags, diff --git a/cocos/rendering/custom/pipeline.ts b/cocos/rendering/custom/pipeline.ts index 68e6d54d984..d12aadac447 100644 --- a/cocos/rendering/custom/pipeline.ts +++ b/cocos/rendering/custom/pipeline.ts @@ -32,7 +32,7 @@ import { Material } from '../../asset/assets'; import { Camera } from '../../render-scene/scene/camera'; import { DirectionalLight } from '../../render-scene/scene/directional-light'; import { GeometryRenderer } from '../geometry-renderer'; -import { Buffer, BufferInfo, ClearFlagBit, Color, CommandBuffer, DescriptorSet, DescriptorSetLayout, Device, Format, LoadOp, ResolveMode, SampleCount, Sampler, ShaderStageFlagBit, StoreOp, Swapchain, Texture, TextureInfo, Viewport } from '../../gfx'; +import { Buffer, BufferInfo, ClearFlagBit, Color, CommandBuffer, DescriptorSet, DescriptorSetLayout, Device, Format, LoadOp, ResolveMode, SampleCount, Sampler, ShaderStageFlagBit, StoreOp, Swapchain, Texture, TextureInfo, TextureType, Viewport } from '../../gfx'; import { GlobalDSManager } from '../global-descriptor-set-manager'; import { Mat4, Quat, Vec2, Vec4 } from '../../core/math'; import { MacroRecord } from '../../render-scene/core/pass-utils'; @@ -684,6 +684,37 @@ export interface BasicPipeline extends PipelineRuntime { width: number, height: number, format?: Format): void; + addBuffer( + name: string, + size: number, + flags: ResourceFlags, + residency: ResourceResidency): number; + updateBuffer( + name: string, + size: number): void; + addExternalTexture(name: string, texture: Texture, flags: ResourceFlags): number; + updateExternalTexture(name: string, texture: Texture): void; + addTexture ( + name: string, + textureType: TextureType, + format: Format, + width: number, + height: number, + depth: number, + arraySize: number, + mipLevels: number, + sampleCount: SampleCount, + flags: ResourceFlags, + residency: ResourceResidency): number; + updateTexture ( + name: string, + format: Format, + width: number, + height: number, + depth: number, + arraySize: number, + mipLevels: number, + sampleCount: SampleCount): void; addResource ( name: string, dimension: ResourceDimension, diff --git a/cocos/rendering/custom/render-graph.ts b/cocos/rendering/custom/render-graph.ts index 41078871395..fa3f241f839 100644 --- a/cocos/rendering/custom/render-graph.ts +++ b/cocos/rendering/custom/render-graph.ts @@ -31,7 +31,7 @@ import { AdjI, AdjacencyGraph, BidirectionalGraph, ComponentGraph, ED, InEI, MutableGraph, MutableReferenceGraph, NamedGraph, OutE, OutEI, PolymorphicGraph, PropertyGraph, PropertyMap, ReferenceGraph, UuidGraph, VertexListGraph, directional, parallel, reindexEdgeList, traversal } from './graph'; import { Material } from '../../asset/assets'; import { Camera } from '../../render-scene/scene/camera'; -import { AccessFlagBit, Buffer, ClearFlagBit, Color, Format, Framebuffer, LoadOp, RenderPass, SampleCount, Sampler, SamplerInfo, ShaderStageFlagBit, StoreOp, Swapchain, Texture, TextureFlagBit, Viewport } from '../../gfx'; +import { AccessFlagBit, Buffer, ClearFlagBit, Color, Format, Framebuffer, LoadOp, RenderPass, SampleCount, Sampler, SamplerInfo, ShaderStageFlagBit, StoreOp, Swapchain, Texture, TextureFlagBit, Viewport, TextureType } from '../../gfx'; import { AccessType, AttachmentType, ClearValueType, CopyPair, LightInfo, MovePair, QueueHint, ResolvePair, ResourceDimension, ResourceFlags, ResourceResidency, SceneFlags, UploadPair } from './types'; import { RenderScene } from '../../render-scene/core/render-scene'; import { RenderWindow } from '../../render-scene/core/render-window'; @@ -117,6 +117,7 @@ export class ResourceDesc { sampleCount: SampleCount = SampleCount.X1; textureFlags: TextureFlagBit = TextureFlagBit.NONE; flags: ResourceFlags = ResourceFlags.NONE; + viewType: TextureType = TextureType.TEX2D; } export class ResourceTraits { diff --git a/cocos/rendering/custom/web-pipeline.ts b/cocos/rendering/custom/web-pipeline.ts index fa46e3dab45..a38eb836ff5 100644 --- a/cocos/rendering/custom/web-pipeline.ts +++ b/cocos/rendering/custom/web-pipeline.ts @@ -20,12 +20,12 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -****************************************************************************/ + ****************************************************************************/ /* eslint-disable max-len */ import { systemInfo } from 'pal/system-info'; import { DEBUG } from 'internal:constants'; -import { Buffer, DescriptorSetLayout, Device, Feature, Format, FormatFeatureBit, Sampler, Swapchain, Texture, ClearFlagBit, DescriptorSet, deviceManager, Viewport, API, CommandBuffer, Type, SamplerInfo, Filter, Address, DescriptorSetInfo, LoadOp, StoreOp, ShaderStageFlagBit, BufferInfo, TextureInfo, UniformBlock, ResolveMode, SampleCount, Color } from '../../gfx'; +import { Buffer, DescriptorSetLayout, Device, Feature, Format, FormatFeatureBit, Sampler, Swapchain, Texture, ClearFlagBit, DescriptorSet, deviceManager, Viewport, API, CommandBuffer, Type, SamplerInfo, Filter, Address, DescriptorSetInfo, LoadOp, StoreOp, ShaderStageFlagBit, BufferInfo, TextureInfo, TextureType, UniformBlock, ResolveMode, SampleCount, Color } from '../../gfx'; import { Mat4, Quat, toRadian, Vec2, Vec3, Vec4, assert, macro, cclegacy, IVec4Like, IMat4Like, IVec2Like, Color as CoreColor } from '../../core'; import { AccessType, AttachmentType, CopyPair, LightInfo, LightingMode, MovePair, QueueHint, ResolvePair, ResourceDimension, ResourceFlags, ResourceResidency, SceneFlags, UpdateFrequency } from './types'; import { ComputeView, RasterView, Blit, ClearView, ComputePass, CopyPass, Dispatch, ManagedBuffer, ManagedResource, MovePass, RasterPass, RasterSubpass, RenderData, RenderGraph, RenderGraphComponent, RenderGraphValue, RenderQueue, RenderSwapchain, ResourceDesc, ResourceGraph, ResourceGraphValue, ResourceStates, ResourceTraits, SceneData, Subpass } from './render-graph'; @@ -830,6 +830,39 @@ function getFirstChildLayoutName (lg: LayoutGraphData, parentID: number): string return ''; } +function getTextureType(dimension: ResourceDimension, arraySize: number): TextureType { + switch (dimension) { + case ResourceDimension.TEXTURE1D: + return arraySize > 1 ? TextureType.TEX1D_ARRAY : TextureType.TEX1D; + case ResourceDimension.TEXTURE2D: + return arraySize > 1 ? TextureType.TEX2D_ARRAY : TextureType.TEX2D; + case ResourceDimension.TEXTURE3D: + return TextureType.TEX3D; + case ResourceDimension.BUFFER: + return TextureType.TEX2D; + default: + break; + } + return TextureType.TEX2D; +} + +function getResourceDimension(type: TextureType): ResourceDimension { + switch (type) { + case TextureType.TEX1D: + case TextureType.TEX1D_ARRAY: + return ResourceDimension.TEXTURE1D; + case TextureType.TEX2D: + case TextureType.TEX2D_ARRAY: + case TextureType.CUBE: + return ResourceDimension.TEXTURE2D; + case TextureType.TEX3D: + return ResourceDimension.TEXTURE3D; + default: + break; + } + return ResourceDimension.TEXTURE2D; +} + export class WebRenderQueueBuilder extends WebSetter implements RenderQueueBuilder { constructor (data: RenderData, renderGraph: RenderGraph, layoutGraph: LayoutGraphData, vertID: number, queue: RenderQueue, pipeline: PipelineSceneData) { super(data, layoutGraph); @@ -1477,16 +1510,47 @@ export class WebPipeline implements BasicPipeline { desc.width = width; desc.height = height; } - addResource (name: string, dimension: ResourceDimension, format: Format, width: number, height: number, depth: number, arraySize: number, mipLevels: number, sampleCount: SampleCount, flags: ResourceFlags, residency: ResourceResidency): number { + + public addBuffer(name: string, size: number, flags: ResourceFlags, residency: ResourceResidency): number { + const desc = new ResourceDesc(); + desc.dimension = ResourceDimension.BUFFER; + desc.width = size; + desc.flags = flags; + return this._resourceGraph.addVertex( + ResourceGraphValue.Managed, + new ManagedResource(), + name, + + desc, + new ResourceTraits(residency), + new ResourceStates(), + new SamplerInfo(Filter.LINEAR, Filter.LINEAR, Filter.NONE, Address.CLAMP, Address.CLAMP, Address.CLAMP), + ); + } + + public updateBuffer(name: string, size: number): void { + this.updateResource(name, Format.UNKNOWN, size, 0, 0, 0, 0, SampleCount.X1); + } + + public addExternalTexture(name: string, texture: Texture, flags: ResourceFlags): number { + throw new Error('Method not implemented.'); + } + + public updateExternalTexture(name: string, texture: Texture): void { + throw new Error('Method not implemented.'); + } + + public addTexture(name: string, textureType: TextureType, format: Format, width: number, height: number, depth: number, arraySize: number, mipLevels: number, sampleCount: SampleCount, flags: ResourceFlags, residency: ResourceResidency): number { const desc = new ResourceDesc(); - desc.dimension = dimension; + desc.dimension = getResourceDimension(textureType); desc.width = width; desc.height = height; - desc.depthOrArraySize = dimension === ResourceDimension.TEXTURE3D ? depth : arraySize; + desc.depthOrArraySize = desc.dimension === ResourceDimension.TEXTURE3D ? depth : arraySize; desc.mipLevels = mipLevels; desc.format = format; desc.sampleCount = sampleCount; desc.flags = flags; + desc.viewType = textureType; return this._resourceGraph.addVertex( ResourceGraphValue.Managed, new ManagedResource(), @@ -1498,7 +1562,19 @@ export class WebPipeline implements BasicPipeline { new SamplerInfo(Filter.LINEAR, Filter.LINEAR, Filter.NONE, Address.CLAMP, Address.CLAMP, Address.CLAMP), ); } - updateResource (name: string, format: Format, width: number, height: number, depth: number, arraySize: number, mipLevels: number, sampleCount: SampleCount): void { + + public updateTexture(name: string, format: Format, width: number, height: number, depth: number, arraySize: number, mipLevels: number, sampleCount: SampleCount) { + this.updateResource(name, format, width, height, depth, arraySize, mipLevels, sampleCount); + } + + public addResource (name: string, dimension: ResourceDimension, format: Format, width: number, height: number, depth: number, arraySize: number, mipLevels: number, sampleCount: SampleCount, flags: ResourceFlags, residency: ResourceResidency): number { + if (dimension === ResourceDimension.BUFFER) { + return this.addBuffer(name, width, flags, residency); + } else { + return this.addTexture(name, getTextureType(dimension, arraySize), format, width, height, depth, arraySize, mipLevels, sampleCount, flags, residency); + } + } + public updateResource (name: string, format: Format, width: number, height: number, depth: number, arraySize: number, mipLevels: number, sampleCount: SampleCount): void { const resId = this.resourceGraph.vertex(name); const desc = this.resourceGraph.getDesc(resId); desc.width = width; diff --git a/native/cocos/core/assets/SimpleTexture.cpp b/native/cocos/core/assets/SimpleTexture.cpp index 145aae30126..74e0018b1fe 100644 --- a/native/cocos/core/assets/SimpleTexture.cpp +++ b/native/cocos/core/assets/SimpleTexture.cpp @@ -140,7 +140,7 @@ void SimpleTexture::createTexture(gfx::Device *device) { } auto textureCreateInfo = getGfxTextureCreateInfo( - gfx::TextureUsageBit::SAMPLED | gfx::TextureUsageBit::TRANSFER_DST, + gfx::TextureUsageBit::SAMPLED | gfx::TextureUsageBit::TRANSFER_DST | gfx::TextureUsageBit::COLOR_ATTACHMENT, getGFXFormat(), _mipmapLevel, flags); diff --git a/native/cocos/renderer/pipeline/custom/FrameGraphDispatcher.cpp b/native/cocos/renderer/pipeline/custom/FrameGraphDispatcher.cpp index 60babdea94f..8c952dda9ae 100644 --- a/native/cocos/renderer/pipeline/custom/FrameGraphDispatcher.cpp +++ b/native/cocos/renderer/pipeline/custom/FrameGraphDispatcher.cpp @@ -189,6 +189,9 @@ ResourceGraph::vertex_descriptor locateSubres(const ccstd::pmr::string& originNa const ResourceGraph &resg, uint32_t basePlane, boost::container::pmr::memory_resource *scratch) { + if (basePlane == 0xFFFFFFFF) { + return findVertex(originName, resg); + } const auto &resName = getSubresNameByPlane(originName, basePlane, resg, scratch); return findVertex(resName, resg); } @@ -1488,12 +1491,12 @@ bool moveValidation(const MovePass& pass, ResourceAccessGraph& rag, const Resour } // full check - std::for_each(targets.begin(), targets.end(), [&](const ccstd::pmr::string &target) { - ResourceNode &resNode = rag.movedTargetStatus[target]; - resNode.full |= std::all_of(resNode.planes.begin(), resNode.planes.end(), [](const auto &textureNode) { - return textureNode.full; - }); - }); + // std::for_each(targets.begin(), targets.end(), [&](const ccstd::pmr::string &target) { + // ResourceNode &resNode = rag.movedTargetStatus[target]; + // resNode.full |= std::all_of(resNode.planes.begin(), resNode.planes.end(), [](const auto &textureNode) { + // return textureNode.full; + // }); + // }); return check; } @@ -1530,7 +1533,7 @@ gfx::SamplerInfo makePointSamplerInfo() { return gfx::SamplerInfo{gfx::Filter::POINT, gfx::Filter::POINT, gfx::Filter::POINT}; } -SubresourceView makeSubresourceView(const ResourceDesc& desc, const gfx::ResourceRange& range) { +SubresourceView makeSubresourceView(const ResourceDesc& srcDesc, const ResourceDesc &targetDesc, const gfx::ResourceRange& range) { SubresourceView view{}; view.firstArraySlice = range.firstSlice; view.numArraySlices = range.numSlices; @@ -1538,7 +1541,8 @@ SubresourceView makeSubresourceView(const ResourceDesc& desc, const gfx::Resourc view.numMipLevels = range.levelCount; view.firstPlane = range.basePlane; view.numPlanes = range.planeCount; - view.format = desc.format; + view.format = targetDesc.format; + view.viewType = srcDesc.viewType; view.textureView = nullptr; return view; } @@ -1652,7 +1656,10 @@ void subresourceAnalysis(ResourceAccessGraph& rag, ResourceGraph& resg) { } } else { for (const auto &[rangeStr, subres] : subreses) { + auto descResViewID = findVertex(subres, resg); auto targetResID = rag.resourceIndex.at(resName); + + const auto &srcDesc = get(ResourceGraph::DescTag{}, resg, descResViewID); const auto &targetName = get(ResourceGraph::NameTag{}, resg, targetResID); const auto &targetDesc = get(ResourceGraph::DescTag{}, resg, targetResID); const auto &srcResourceRange = rag.movedSourceStatus.at(subres).range; @@ -1660,7 +1667,7 @@ void subresourceAnalysis(ResourceAccessGraph& rag, ResourceGraph& resg) { const auto &indexName = concatResName(targetName, subres, rag.resource()); auto subresID = findVertex(indexName, resg); if (subresID == ResourceGraph::null_vertex()) { - const auto &subView = makeSubresourceView(targetDesc, srcResourceRange); + const auto &subView = makeSubresourceView(srcDesc, targetDesc, srcResourceRange); // register to resourcegraph subresID = addVertex( SubresourceViewTag{}, diff --git a/native/cocos/renderer/pipeline/custom/LayoutGraphUtils.cpp b/native/cocos/renderer/pipeline/custom/LayoutGraphUtils.cpp index 1854385cd5c..74c230b1dba 100644 --- a/native/cocos/renderer/pipeline/custom/LayoutGraphUtils.cpp +++ b/native/cocos/renderer/pipeline/custom/LayoutGraphUtils.cpp @@ -477,6 +477,35 @@ void printLayoutGraphData( } } +gfx::TextureType getTextureType(ResourceDimension dimension, uint32_t arraySize) { + switch (dimension) { + case ResourceDimension::TEXTURE1D: + return arraySize > 1 ? gfx::TextureType::TEX1D_ARRAY : gfx::TextureType::TEX1D; + case ResourceDimension::TEXTURE2D: + return arraySize > 1 ? gfx::TextureType::TEX2D_ARRAY : gfx::TextureType::TEX2D; + case ResourceDimension::TEXTURE3D: + return gfx::TextureType::TEX3D; + case ResourceDimension::BUFFER: + default: + return gfx::TextureType::TEX2D; + } +} + +ResourceDimension getResourceDimension(gfx::TextureType type) { + switch (type) { + case gfx::TextureType::TEX1D: + case gfx::TextureType::TEX1D_ARRAY: + return ResourceDimension::TEXTURE1D; + case gfx::TextureType::TEX3D: + return ResourceDimension::TEXTURE3D; + case gfx::TextureType::TEX2D: + case gfx::TextureType::TEX2D_ARRAY: + case gfx::TextureType::CUBE: + default: + return ResourceDimension::TEXTURE2D; + } +} + } // namespace render } // namespace cc diff --git a/native/cocos/renderer/pipeline/custom/LayoutGraphUtils.h b/native/cocos/renderer/pipeline/custom/LayoutGraphUtils.h index 389fd95dbd7..acb5780ebe8 100644 --- a/native/cocos/renderer/pipeline/custom/LayoutGraphUtils.h +++ b/native/cocos/renderer/pipeline/custom/LayoutGraphUtils.h @@ -65,6 +65,10 @@ void printLayoutGraphData( const LayoutGraphData& lg, std::ostream& oss, boost::container::pmr::memory_resource* scratch); +gfx::TextureType getTextureType(ResourceDimension dimension, uint32_t arraySize); +ResourceDimension getResourceDimension(gfx::TextureType type); + + } // namespace render } // namespace cc diff --git a/native/cocos/renderer/pipeline/custom/NativePipeline.cpp b/native/cocos/renderer/pipeline/custom/NativePipeline.cpp index f747e151f36..60f12c6df26 100644 --- a/native/cocos/renderer/pipeline/custom/NativePipeline.cpp +++ b/native/cocos/renderer/pipeline/custom/NativePipeline.cpp @@ -122,6 +122,47 @@ bool NativePipeline::containsResource(const ccstd::string &name) const { return contains(name.c_str(), resourceGraph); } +uint32_t NativePipeline::addExternalTexture(const ccstd::string &name, gfx::Texture *texture, ResourceFlags flags) { + auto &texInfo = texture->getInfo(); + ResourceDesc desc{}; + desc.dimension = getResourceDimension(texInfo.type); + desc.width = texInfo.width; + desc.height = texInfo.height; + desc.depthOrArraySize = desc.dimension == ResourceDimension::TEXTURE3D ? texInfo.depth : texInfo.layerCount; + desc.mipLevels = texInfo.levelCount; + desc.format = texture->getFormat(); + desc.sampleCount = gfx::SampleCount::X1; + desc.textureFlags = texture->getInfo().flags; + desc.flags = flags; + desc.viewType = texInfo.type; + + return addVertex( + PersistentTextureTag{}, + std::forward_as_tuple(name.c_str()), + std::forward_as_tuple(desc), + std::forward_as_tuple(ResourceTraits{ResourceResidency::EXTERNAL}), + std::forward_as_tuple(), + std::forward_as_tuple(), + std::forward_as_tuple(texture), + resourceGraph); +} + +void NativePipeline::updateExternalTexture(const ccstd::string &name, gfx::Texture *texture) { + auto resID = findVertex(ccstd::pmr::string(name, get_allocator()), resourceGraph); + if (resID == ResourceGraph::null_vertex()) { + return; + } + auto &desc = get(ResourceGraph::DescTag{}, resourceGraph, resID); + visitObject( + resID, resourceGraph, + [&](IntrusivePtr &tex) { + desc.width = texture->getWidth(); + desc.height = texture->getHeight(); + tex = texture; + }, + [](const auto & /*res*/) {}); +} + // NOLINTNEXTLINE(bugprone-easily-swappable-parameters) uint32_t NativePipeline::addRenderWindow(const ccstd::string &name, gfx::Format format, uint32_t width, uint32_t height, scene::RenderWindow *renderWindow) { ResourceDesc desc{}; @@ -253,10 +294,8 @@ uint32_t NativePipeline::addDepthStencil(const ccstd::string &name, gfx::Format return resID; } -uint32_t NativePipeline::addResource(const ccstd::string& name, ResourceDimension dimension, - gfx::Format format, - uint32_t width, uint32_t height, uint32_t depth, uint32_t arraySize, uint32_t mipLevels, - gfx::SampleCount sampleCount, ResourceFlags flags, ResourceResidency residency) { +uint32_t NativePipeline::addTexture(const ccstd::string &name, gfx::TextureType type, gfx::Format format, uint32_t width, uint32_t height, uint32_t depth, uint32_t arraySize, uint32_t mipLevels, gfx::SampleCount sampleCount, ResourceFlags flags, ResourceResidency residency) { + const auto dimension = getResourceDimension(type); ResourceDesc desc{ dimension, 0, @@ -268,6 +307,7 @@ uint32_t NativePipeline::addResource(const ccstd::string& name, ResourceDimensio sampleCount, residency == ResourceResidency::MEMORYLESS ? gfx::TextureFlagBit::LAZILY_ALLOCATED : gfx::TextureFlagBit::NONE, flags, + type }; return addVertex( ManagedTextureTag{}, @@ -280,6 +320,41 @@ uint32_t NativePipeline::addResource(const ccstd::string& name, ResourceDimensio resourceGraph); } +void NativePipeline::updateTexture(const ccstd::string &name, gfx::Format format, uint32_t width, uint32_t height, uint32_t depth, uint32_t arraySize, uint32_t mipLevels, gfx::SampleCount sampleCount) { + updateResource(name, format, width, height, depth, arraySize, mipLevels, sampleCount); +} + +uint32_t NativePipeline::addBuffer(const ccstd::string &name, uint32_t size, ResourceFlags flags, ResourceResidency residency) { + ResourceDesc desc = {}; + desc.dimension = ResourceDimension::BUFFER; + desc.width = size; + desc.flags = flags; + return addVertex( + ManagedBufferTag{}, + std::forward_as_tuple(name.c_str()), + std::forward_as_tuple(desc), + std::forward_as_tuple(ResourceTraits{residency}), + std::forward_as_tuple(), + std::forward_as_tuple(), + std::forward_as_tuple(), + resourceGraph); +} + +void NativePipeline::updateBuffer(const ccstd::string &name, uint32_t size) { + updateResource(name, gfx::Format::UNKNOWN, size, 0, 0, 0, 0, gfx::SampleCount::X1); +} + +uint32_t NativePipeline::addResource(const ccstd::string& name, ResourceDimension dimension, + gfx::Format format, + uint32_t width, uint32_t height, uint32_t depth, uint32_t arraySize, uint32_t mipLevels, + gfx::SampleCount sampleCount, ResourceFlags flags, ResourceResidency residency) { + if (dimension == ResourceDimension::BUFFER) { + return addBuffer(name, width, flags, residency); + } else { + return addTexture(name, getTextureType(dimension, arraySize), format, width, height, depth, arraySize, mipLevels, sampleCount, flags, residency); + } +} + void NativePipeline::updateResource(const ccstd::string& name, gfx::Format format, uint32_t width, uint32_t height, uint32_t depth, uint32_t arraySize, uint32_t mipLevels, // NOLINT(bugprone-easily-swappable-parameters) gfx::SampleCount sampleCount) { @@ -311,6 +386,12 @@ void NativePipeline::updateResource(const ccstd::string& name, gfx::Format forma resourceGraph.invalidatePersistentRenderPassAndFramebuffer(tex.texture.get()); } }, + [&](ManagedBuffer &buffer) { + bool invalidate = desc.width != width; + if (invalidate) { + desc.width = width; + } + }, [](const auto & /*res*/) {}); } diff --git a/native/cocos/renderer/pipeline/custom/NativePipelineTypes.h b/native/cocos/renderer/pipeline/custom/NativePipelineTypes.h index 1fdb0ebed7d..2f5704b28f8 100644 --- a/native/cocos/renderer/pipeline/custom/NativePipelineTypes.h +++ b/native/cocos/renderer/pipeline/custom/NativePipelineTypes.h @@ -1357,12 +1357,18 @@ class NativePipeline final : public Pipeline { void beginSetup() override; void endSetup() override; bool containsResource(const ccstd::string &name) const override; + uint32_t addExternalTexture(const ccstd::string &name, gfx::Texture *texture, ResourceFlags flags) override; + void updateExternalTexture(const ccstd::string &name, gfx::Texture *texture) override; uint32_t addRenderWindow(const ccstd::string &name, gfx::Format format, uint32_t width, uint32_t height, scene::RenderWindow *renderWindow) override; void updateRenderWindow(const ccstd::string &name, scene::RenderWindow *renderWindow) override; uint32_t addRenderTarget(const ccstd::string &name, gfx::Format format, uint32_t width, uint32_t height, ResourceResidency residency) override; uint32_t addDepthStencil(const ccstd::string &name, gfx::Format format, uint32_t width, uint32_t height, ResourceResidency residency) override; void updateRenderTarget(const ccstd::string &name, uint32_t width, uint32_t height, gfx::Format format) override; void updateDepthStencil(const ccstd::string &name, uint32_t width, uint32_t height, gfx::Format format) override; + uint32_t addTexture(const ccstd::string &name, gfx::TextureType type, gfx::Format format, uint32_t width, uint32_t height, uint32_t depth, uint32_t arraySize, uint32_t mipLevels, gfx::SampleCount sampleCount, ResourceFlags flags, ResourceResidency residency) override; + void updateTexture(const ccstd::string &name, gfx::Format format, uint32_t width, uint32_t height, uint32_t depth, uint32_t arraySize, uint32_t mipLevels, gfx::SampleCount sampleCount) override; + uint32_t addBuffer(const ccstd::string &name, uint32_t size, ResourceFlags flags, ResourceResidency residency) override; + void updateBuffer(const ccstd::string &name, uint32_t size) override; uint32_t addResource(const ccstd::string &name, ResourceDimension dimension, gfx::Format format, uint32_t width, uint32_t height, uint32_t depth, uint32_t arraySize, uint32_t mipLevels, gfx::SampleCount sampleCount, ResourceFlags flags, ResourceResidency residency) override; void updateResource(const ccstd::string &name, gfx::Format format, uint32_t width, uint32_t height, uint32_t depth, uint32_t arraySize, uint32_t mipLevels, gfx::SampleCount sampleCount) override; void beginFrame() override; diff --git a/native/cocos/renderer/pipeline/custom/NativeResourceGraph.cpp b/native/cocos/renderer/pipeline/custom/NativeResourceGraph.cpp index 1086f449745..c514d0030e2 100644 --- a/native/cocos/renderer/pipeline/custom/NativeResourceGraph.cpp +++ b/native/cocos/renderer/pipeline/custom/NativeResourceGraph.cpp @@ -99,33 +99,6 @@ gfx::BufferInfo getBufferInfo(const ResourceDesc& desc) { gfx::TextureInfo getTextureInfo(const ResourceDesc& desc, bool bCube = false) { using namespace gfx; // NOLINT(google-build-using-namespace) - // type - auto type = TextureType::TEX1D; - switch (desc.dimension) { - case ResourceDimension::TEXTURE1D: - if (desc.depthOrArraySize > 1) { - type = TextureType::TEX1D_ARRAY; - } else { - type = TextureType::TEX1D; - } - break; - case ResourceDimension::TEXTURE2D: - if (desc.depthOrArraySize > 1) { - if (bCube) { - type = TextureType::CUBE; - } else { - type = TextureType::TEX2D_ARRAY; - } - } else { - type = TextureType::TEX2D; - } - break; - case ResourceDimension::TEXTURE3D: - type = TextureType::TEX3D; - break; - case ResourceDimension::BUFFER: - CC_EXPECTS(false); - } // usage TextureUsage usage = TextureUsage::NONE; @@ -156,28 +129,26 @@ gfx::TextureInfo getTextureInfo(const ResourceDesc& desc, bool bCube = false) { } return { - type, + desc.viewType, usage, desc.format, desc.width, desc.height, desc.textureFlags, - type == TextureType::TEX3D ? 1U : desc.depthOrArraySize, + desc.viewType == TextureType::TEX3D ? 1U : desc.depthOrArraySize, desc.mipLevels, desc.sampleCount, - type == TextureType::TEX3D ? desc.depthOrArraySize : 1U, + desc.viewType == TextureType::TEX3D ? desc.depthOrArraySize : 1U, nullptr, }; } -gfx::TextureViewInfo getTextureViewInfo(const SubresourceView& subresView, const ResourceDesc& desc, bool bCube = false) { +gfx::TextureViewInfo getTextureViewInfo(const SubresourceView& subresView, bool bCube = false) { using namespace gfx; // NOLINT(google-build-using-namespace) - const auto& textureInfo = getTextureInfo(desc, bCube); - return { nullptr, - textureInfo.type, + subresView.viewType, subresView.format, subresView.indexOrFirstMipLevel, subresView.numMipLevels, @@ -280,9 +251,8 @@ void ResourceGraph::mount(gfx::Device* device, vertex_descriptor vertID) { CC_ENSURES(!resg.isTextureView(parentID)); mount(device, parentID); // NOLINT(misc-no-recursion) auto* parentTexture = resg.getTexture(parentID); - const auto& desc = get(ResourceGraph::DescTag{}, resg, vertID); if (!view.textureView) { - auto textureViewInfo = getTextureViewInfo(originView, desc); + auto textureViewInfo = getTextureViewInfo(originView); textureViewInfo.texture = parentTexture; view.textureView = device->createTexture(textureViewInfo); } diff --git a/native/cocos/renderer/pipeline/custom/RenderGraphTypes.h b/native/cocos/renderer/pipeline/custom/RenderGraphTypes.h index acf43826fd8..75abe2d1bf1 100644 --- a/native/cocos/renderer/pipeline/custom/RenderGraphTypes.h +++ b/native/cocos/renderer/pipeline/custom/RenderGraphTypes.h @@ -169,6 +169,7 @@ struct ResourceDesc { gfx::SampleCount sampleCount{gfx::SampleCount::X1}; gfx::TextureFlagBit textureFlags{gfx::TextureFlagBit::NONE}; ResourceFlags flags{ResourceFlags::NONE}; + gfx::TextureType viewType{gfx::TextureType::TEX2D}; }; struct ResourceTraits { @@ -515,6 +516,7 @@ struct SubresourceView { uint16_t numArraySlices{0}; uint16_t firstPlane{0}; uint16_t numPlanes{0}; + gfx::TextureType viewType; }; struct ResourceGraph { diff --git a/native/cocos/renderer/pipeline/custom/RenderInterfaceTypes.h b/native/cocos/renderer/pipeline/custom/RenderInterfaceTypes.h index 77845fd5c64..29e13290601 100644 --- a/native/cocos/renderer/pipeline/custom/RenderInterfaceTypes.h +++ b/native/cocos/renderer/pipeline/custom/RenderInterfaceTypes.h @@ -773,6 +773,12 @@ class BasicPipeline : public PipelineRuntime { virtual void updateDepthStencil(const ccstd::string &name, uint32_t width, uint32_t height, gfx::Format format) = 0; virtual uint32_t addResource(const ccstd::string &name, ResourceDimension dimension, gfx::Format format, uint32_t width, uint32_t height, uint32_t depth, uint32_t arraySize, uint32_t mipLevels, gfx::SampleCount sampleCount, ResourceFlags flags, ResourceResidency residency) = 0; virtual void updateResource(const ccstd::string &name, gfx::Format format, uint32_t width, uint32_t height, uint32_t depth, uint32_t arraySize, uint32_t mipLevels, gfx::SampleCount sampleCount) = 0; + virtual uint32_t addTexture(const ccstd::string &name, gfx::TextureType type, gfx::Format format, uint32_t width, uint32_t height, uint32_t depth, uint32_t arraySize, uint32_t mipLevels, gfx::SampleCount sampleCount, ResourceFlags flags, ResourceResidency residency) = 0; + virtual void updateTexture(const ccstd::string &name, gfx::Format format, uint32_t width, uint32_t height, uint32_t depth, uint32_t arraySize, uint32_t mipLevels, gfx::SampleCount sampleCount) = 0; + virtual uint32_t addBuffer(const ccstd::string &name, uint32_t size, ResourceFlags flags, ResourceResidency residency) = 0; + virtual void updateBuffer(const ccstd::string &name, uint32_t size) = 0; + virtual uint32_t addExternalTexture(const ccstd::string &name, gfx::Texture *texture, ResourceFlags flags) = 0; + virtual void updateExternalTexture(const ccstd::string &name, gfx::Texture *texture) = 0; /** * @engineInternal * @en Begin rendering one frame