mirror of
https://github.com/cocos/cocos4.git
synced 2026-11-21 08:02:39 +08:00
Add a new class to manage a set of default device resources. (#12936)
This commit is contained in:
+102
-1
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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};
|
||||
|
||||
@@ -126,5 +126,74 @@ BufferBarrier *Device::getBufferBarrier(const BufferBarrierInfo &info) {
|
||||
return _bufferBarriers[info];
|
||||
}
|
||||
|
||||
DefaultResource::DefaultResource(Device *device) {
|
||||
uint32_t bufferSize = 64;
|
||||
ccstd::vector<uint8_t> 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
|
||||
|
||||
@@ -191,6 +191,23 @@ private:
|
||||
ccstd::vector<Swapchain *> _swapchains; // weak reference
|
||||
};
|
||||
|
||||
class DefaultResource {
|
||||
public:
|
||||
explicit DefaultResource(Device *device);
|
||||
|
||||
~DefaultResource() = default;
|
||||
|
||||
const Texture *getTexture(TextureType type) const;
|
||||
|
||||
private:
|
||||
IntrusivePtr<Texture> _texture1D;
|
||||
IntrusivePtr<Texture> _texture2D;
|
||||
IntrusivePtr<Texture> _texture1DArray;
|
||||
IntrusivePtr<Texture> _texture2DArray;
|
||||
IntrusivePtr<Texture> _textureCube;
|
||||
IntrusivePtr<Texture> _texture3D;
|
||||
};
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
|
||||
CommandBuffer *Device::createCommandBuffer(const CommandBufferInfo &info) {
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user