diff --git a/cocos/gfx/base/device.ts b/cocos/gfx/base/device.ts index 83fe68140c..7ef76d0e45 100644 --- a/cocos/gfx/base/device.ts +++ b/cocos/gfx/base/device.ts @@ -28,7 +28,7 @@ import { CommandBufferInfo, BufferInfo, BufferViewInfo, TextureInfo, TextureViewInfo, SamplerInfo, DescriptorSetInfo, ShaderInfo, InputAssemblerInfo, RenderPassInfo, FramebufferInfo, DescriptorSetLayoutInfo, PipelineLayoutInfo, QueueInfo, BufferTextureCopy, DeviceInfo, DeviceCaps, GeneralBarrierInfo, TextureBarrierInfo, BufferBarrierInfo, - SwapchainInfo, BindingMappingInfo, Format, FormatFeature, + SwapchainInfo, BindingMappingInfo, Format, FormatFeature, TextureType, TextureUsageBit, TextureFlagBit, Offset, Extent, SampleCount, TextureSubresLayers, } from './define'; import { Buffer } from './buffer'; import { CommandBuffer } from './command-buffer'; @@ -353,3 +353,104 @@ export abstract class Device { return this._formatFeatures[format]; } } + +export class DefaultResource { + private _texture2D : Texture | null = null; + private _texture3D : Texture | null = null; + private _textureCube : Texture | null = null; + private _texture1DArray : Texture | null = null; + private _texture2DArray : Texture | null = null; + + constructor (device: Device) { + const bufferSize = 64; + // create a new buffer and fill it with a white pixel + const buffer = new Uint8Array(bufferSize); + buffer.fill(255); + if (device.capabilities.maxTextureSize >= 2) { + this._texture2D = device.createTexture(new TextureInfo( + TextureType.TEX2D, + TextureUsageBit.STORAGE | TextureUsageBit.SAMPLED, + Format.RGBA8, + 2, 2, + TextureFlagBit.NONE, + )); + const copyRegion = new BufferTextureCopy(0, 0, 0, new Offset(0, 0, 0), new Extent(2, 2, 1)); + device.copyBuffersToTexture([buffer], this._texture2D, [copyRegion]); + } + if (device.capabilities.maxTextureSize >= 2) { + this._textureCube = device.createTexture(new TextureInfo( + TextureType.CUBE, + TextureUsageBit.STORAGE | TextureUsageBit.SAMPLED, + Format.RGBA8, + 2, 2, + TextureFlagBit.NONE, + 6, + )); + const copyRegion = new BufferTextureCopy(0, 0, 0, new Offset(0, 0, 0), new Extent(2, 2, 1)); + device.copyBuffersToTexture([buffer], this._textureCube, [copyRegion]); + copyRegion.texSubres.baseArrayLayer = 1; + device.copyBuffersToTexture([buffer], this._textureCube, [copyRegion]); + copyRegion.texSubres.baseArrayLayer = 2; + device.copyBuffersToTexture([buffer], this._textureCube, [copyRegion]); + copyRegion.texSubres.baseArrayLayer = 3; + device.copyBuffersToTexture([buffer], this._textureCube, [copyRegion]); + copyRegion.texSubres.baseArrayLayer = 4; + device.copyBuffersToTexture([buffer], this._textureCube, [copyRegion]); + copyRegion.texSubres.baseArrayLayer = 5; + device.copyBuffersToTexture([buffer], this._textureCube, [copyRegion]); + } + if (device.capabilities.max3DTextureSize >= 2) { + this._texture3D = device.createTexture(new TextureInfo( + TextureType.TEX3D, + TextureUsageBit.STORAGE | TextureUsageBit.SAMPLED, + Format.RGBA8, + 2, 2, + TextureFlagBit.NONE, + 1, 1, + SampleCount.ONE, + 2, + )); + const copyRegion = new BufferTextureCopy(0, 0, 0, new Offset(0, 0, 0), new Extent(2, 2, 2), new TextureSubresLayers(0, 0, 1)); + device.copyBuffersToTexture([buffer], this._texture3D, [copyRegion]); + } + if (device.capabilities.maxArrayTextureLayers >= 2) { + this._texture1DArray = device.createTexture(new TextureInfo( + TextureType.TEX1D_ARRAY, + TextureUsageBit.STORAGE | TextureUsageBit.SAMPLED, + Format.RGBA8, + 1, 1, + TextureFlagBit.NONE, + 2, + )); + const copyRegion = new BufferTextureCopy(0, 0, 0, new Offset(0, 0, 0), new Extent(1, 1, 1), new TextureSubresLayers(0, 0, 1)); + device.copyBuffersToTexture([buffer], this._texture1DArray, [copyRegion]); + copyRegion.texSubres.baseArrayLayer = 1; + device.copyBuffersToTexture([buffer], this._texture1DArray, [copyRegion]); + } + if (device.capabilities.maxArrayTextureLayers >= 2) { + this._texture2DArray = device.createTexture(new TextureInfo( + TextureType.TEX2D_ARRAY, + TextureUsageBit.STORAGE | TextureUsageBit.SAMPLED, + Format.RGBA8, + 2, 2, + TextureFlagBit.NONE, + 2, + )); + const copyRegion = new BufferTextureCopy(0, 0, 0, new Offset(0, 0, 0), new Extent(2, 2, 1), new TextureSubresLayers(0, 0, 1)); + device.copyBuffersToTexture([buffer], this._texture2DArray, [copyRegion]); + copyRegion.texSubres.baseArrayLayer = 1; + device.copyBuffersToTexture([buffer], this._texture2DArray, [copyRegion]); + } + } + + public getTexture (type : TextureType) { + switch (type) { + case TextureType.TEX2D: return this._texture2D; + case TextureType.TEX3D: return this._texture3D; + case TextureType.CUBE: return this._textureCube; + case TextureType.TEX1D_ARRAY: return this._texture1DArray; + case TextureType.TEX2D_ARRAY: return this._texture2DArray; + default: return null; + } + } +} diff --git a/native/cocos/renderer/GFXDeviceManager.h b/native/cocos/renderer/GFXDeviceManager.h index 75ee60a521..3f619fc6f1 100644 --- a/native/cocos/renderer/GFXDeviceManager.h +++ b/native/cocos/renderer/GFXDeviceManager.h @@ -62,7 +62,6 @@ namespace cc { namespace gfx { - class CC_DLL DeviceManager final { static constexpr bool DETACH_DEVICE_THREAD{true}; static constexpr bool FORCE_DISABLE_VALIDATION{false}; diff --git a/native/cocos/renderer/gfx-base/GFXDevice.cpp b/native/cocos/renderer/gfx-base/GFXDevice.cpp index fef0987313..a22c551bf9 100644 --- a/native/cocos/renderer/gfx-base/GFXDevice.cpp +++ b/native/cocos/renderer/gfx-base/GFXDevice.cpp @@ -126,5 +126,74 @@ BufferBarrier *Device::getBufferBarrier(const BufferBarrierInfo &info) { return _bufferBarriers[info]; } +DefaultResource::DefaultResource(Device *device) { + uint32_t bufferSize = 64; + ccstd::vector buffer(bufferSize, 255); + const uint8_t *bufferData = buffer.data(); + if (device->getCapabilities().maxTextureSize >= 2) { + _texture2D = device->createTexture({TextureType::TEX2D, TextureUsageBit::STORAGE | TextureUsageBit::SAMPLED, + Format::RGBA8, 2, 2, TextureFlagBit::NONE}); + BufferTextureCopy region = {0, 0, 0, {0, 0, 0}, {2, 2, 1}, {0, 0, 1}}; + device->copyBuffersToTexture(&bufferData, _texture2D, ®ion, 1); + } + if (device->getCapabilities().maxTextureSize >= 2) { + _textureCube = device->createTexture({TextureType::CUBE, TextureUsageBit::STORAGE | TextureUsageBit::SAMPLED, + Format::RGBA8, 2, 2, TextureFlagBit::NONE, 6}); + BufferTextureCopy region = {0, 0, 0, {0, 0, 0}, {2, 2, 1}, {0, 0, 1}}; + device->copyBuffersToTexture(&bufferData, _textureCube, ®ion, 1); + region.texSubres.baseArrayLayer = 1; + device->copyBuffersToTexture(&bufferData, _textureCube, ®ion, 1); + region.texSubres.baseArrayLayer = 2; + device->copyBuffersToTexture(&bufferData, _textureCube, ®ion, 1); + region.texSubres.baseArrayLayer = 3; + device->copyBuffersToTexture(&bufferData, _textureCube, ®ion, 1); + region.texSubres.baseArrayLayer = 4; + device->copyBuffersToTexture(&bufferData, _textureCube, ®ion, 1); + region.texSubres.baseArrayLayer = 5; + device->copyBuffersToTexture(&bufferData, _textureCube, ®ion, 1); + } + + if (device->getCapabilities().max3DTextureSize >= 2) { + _texture3D = device->createTexture({TextureType::TEX3D, TextureUsageBit::STORAGE | TextureUsageBit::SAMPLED, + Format::RGBA8, 2, 2, TextureFlagBit::NONE, 1, 1, SampleCount::ONE, 2}); + BufferTextureCopy region = {0, 0, 0, {0, 0, 0}, {2, 2, 2}, {0, 0, 1}}; + device->copyBuffersToTexture(&bufferData, _texture3D, ®ion, 1); + } + if (device->getCapabilities().maxArrayTextureLayers >= 2) { + _texture1DArray = device->createTexture({TextureType::TEX1D_ARRAY, TextureUsageBit::STORAGE | TextureUsageBit::SAMPLED, + Format::RGBA8, 1, 1, TextureFlagBit::NONE, 2}); + BufferTextureCopy region = {0, 0, 0, {0, 0, 0}, {1, 1, 1}, {0, 0, 1}}; + device->copyBuffersToTexture(&bufferData, _texture1DArray, ®ion, 1); + region.texSubres.baseArrayLayer = 1; + device->copyBuffersToTexture(&bufferData, _texture1DArray, ®ion, 1); + } + if (device->getCapabilities().maxArrayTextureLayers >= 2) { + _texture2DArray = device->createTexture({TextureType::TEX2D_ARRAY, TextureUsageBit::STORAGE | TextureUsageBit::SAMPLED, + Format::RGBA8, 2, 2, TextureFlagBit::NONE, 2}); + BufferTextureCopy region = {0, 0, 0, {0, 0, 0}, {2, 2, 1}, {0, 0, 1}}; + device->copyBuffersToTexture(&bufferData, _texture2DArray, ®ion, 1); + region.texSubres.baseArrayLayer = 1; + device->copyBuffersToTexture(&bufferData, _texture2DArray, ®ion, 1); + } +} + +const Texture *DefaultResource::getTexture(TextureType type) const { + switch (type) { + case TextureType::TEX2D: + return _texture2D; + case TextureType::CUBE: + return _textureCube; + case TextureType::TEX3D: + return _texture3D; + case TextureType::TEX1D_ARRAY: + return _texture1DArray; + case TextureType::TEX2D_ARRAY: + return _texture2DArray; + default: + CC_ASSERT(false); + return nullptr; + } +} + } // namespace gfx } // namespace cc diff --git a/native/cocos/renderer/gfx-base/GFXDevice.h b/native/cocos/renderer/gfx-base/GFXDevice.h index a7c256b122..8ab53e703f 100644 --- a/native/cocos/renderer/gfx-base/GFXDevice.h +++ b/native/cocos/renderer/gfx-base/GFXDevice.h @@ -191,6 +191,23 @@ private: ccstd::vector _swapchains; // weak reference }; +class DefaultResource { +public: + explicit DefaultResource(Device *device); + + ~DefaultResource() = default; + + const Texture *getTexture(TextureType type) const; + +private: + IntrusivePtr _texture1D; + IntrusivePtr _texture2D; + IntrusivePtr _texture1DArray; + IntrusivePtr _texture2DArray; + IntrusivePtr _textureCube; + IntrusivePtr _texture3D; +}; + ////////////////////////////////////////////////////////////////////////// CommandBuffer *Device::createCommandBuffer(const CommandBufferInfo &info) { diff --git a/native/cocos/renderer/gfx-validator/TextureValidator.cpp b/native/cocos/renderer/gfx-validator/TextureValidator.cpp index d589384690..a75366dda6 100644 --- a/native/cocos/renderer/gfx-validator/TextureValidator.cpp +++ b/native/cocos/renderer/gfx-validator/TextureValidator.cpp @@ -74,6 +74,13 @@ void TextureValidator::doInit(const TextureInfo &info) { } break; } + case TextureType::TEX1D_ARRAY: { + if (std::min(info.width, info.height) > 1 || std::max(info.width, info.height) > DeviceValidator::getInstance()->getCapabilities().maxTextureSize + || info.layerCount > DeviceValidator::getInstance()->getCapabilities().maxArrayTextureLayers) { + CC_ASSERT(false); + } + break; + } case TextureType::TEX2D_ARRAY: { if (std::max(info.width, info.height) > DeviceValidator::getInstance()->getCapabilities().maxTextureSize || info.layerCount > DeviceValidator::getInstance()->getCapabilities().maxArrayTextureLayers) {