Skip to content

Commit

Permalink
NativePipeline external texture
Browse files Browse the repository at this point in the history
  • Loading branch information
bluesky013 committed Aug 14, 2023
1 parent 4ca3c88 commit f08f274
Show file tree
Hide file tree
Showing 13 changed files with 272 additions and 59 deletions.
2 changes: 1 addition & 1 deletion cocos/asset/assets/simple-texture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
33 changes: 32 additions & 1 deletion cocos/rendering/custom/pipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion cocos/rendering/custom/render-graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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 {
Expand Down
88 changes: 82 additions & 6 deletions cocos/rendering/custom/web-pipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -830,6 +830,39 @@ function getFirstChildLayoutName (lg: LayoutGraphData, parentID: number): string
return '';
}

function getTextureType(dimension: ResourceDimension, arraySize: number): TextureType {

Check warning on line 833 in cocos/rendering/custom/web-pipeline.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Missing space before function parentheses

Check warning on line 833 in cocos/rendering/custom/web-pipeline.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Missing space before function parentheses
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 {

Check warning on line 849 in cocos/rendering/custom/web-pipeline.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Missing space before function parentheses

Check warning on line 849 in cocos/rendering/custom/web-pipeline.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Missing space before function parentheses
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);
Expand Down Expand Up @@ -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 {

Check warning on line 1514 in cocos/rendering/custom/web-pipeline.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Missing space before function parentheses

Check warning on line 1514 in cocos/rendering/custom/web-pipeline.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Missing space before function parentheses
const desc = new ResourceDesc();
desc.dimension = ResourceDimension.BUFFER;
desc.width = size;
desc.flags = flags;
return this._resourceGraph.addVertex<ResourceGraphValue.Managed>(
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 {

Check warning on line 1531 in cocos/rendering/custom/web-pipeline.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Missing space before function parentheses

Check warning on line 1531 in cocos/rendering/custom/web-pipeline.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Missing space before function parentheses
this.updateResource(name, Format.UNKNOWN, size, 0, 0, 0, 0, SampleCount.X1);
}

public addExternalTexture(name: string, texture: Texture, flags: ResourceFlags): number {

Check warning on line 1535 in cocos/rendering/custom/web-pipeline.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Missing space before function parentheses

Check warning on line 1535 in cocos/rendering/custom/web-pipeline.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Missing space before function parentheses
throw new Error('Method not implemented.');
}

public updateExternalTexture(name: string, texture: Texture): void {

Check warning on line 1539 in cocos/rendering/custom/web-pipeline.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Missing space before function parentheses
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 {

Check warning on line 1543 in cocos/rendering/custom/web-pipeline.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Missing space before function parentheses
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>(
ResourceGraphValue.Managed,
new ManagedResource(),
Expand All @@ -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) {

Check failure on line 1566 in cocos/rendering/custom/web-pipeline.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Missing return type on function

Check warning on line 1566 in cocos/rendering/custom/web-pipeline.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Missing space before function parentheses
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;
Expand Down
2 changes: 1 addition & 1 deletion native/cocos/core/assets/SimpleTexture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
25 changes: 16 additions & 9 deletions native/cocos/renderer/pipeline/custom/FrameGraphDispatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -1530,15 +1533,16 @@ 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;
view.indexOrFirstMipLevel = range.mipLevel;
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;
}
Expand Down Expand Up @@ -1652,15 +1656,18 @@ 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;
const auto &targetTraits = get(ResourceGraph::TraitsTag{}, resg, targetResID);
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{},
Expand Down
29 changes: 29 additions & 0 deletions native/cocos/renderer/pipeline/custom/LayoutGraphUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 4 additions & 0 deletions native/cocos/renderer/pipeline/custom/LayoutGraphUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading

0 comments on commit f08f274

Please sign in to comment.