From dbcb157efb0d8d1210310a5ab3c657010d34bf28 Mon Sep 17 00:00:00 2001 From: Yun Hsiao Wu Date: Tue, 19 Oct 2021 14:14:46 +0800 Subject: [PATCH] misc gfx fixes (#3991) * better hashing strat * gfx: unify states hash * vk: upload texture in multiple chunks if needed * gfx: states lifecycle management --- native/cmake/predefine.cmake | 4 +- native/cocos/base/threading/MessageQueue.h | 10 ++ native/cocos/math/MathBase.h | 11 ++ native/cocos/math/Vec4.cpp | 99 +++++++----- native/cocos/math/Vec4.h | 7 +- .../renderer/frame-graph/ResourceAllocator.h | 14 +- .../cocos/renderer/gfx-agent/DeviceAgent.cpp | 12 +- native/cocos/renderer/gfx-agent/DeviceAgent.h | 6 +- .../cocos/renderer/gfx-base/GFXDef-common.h | 2 +- native/cocos/renderer/gfx-base/GFXDef.cpp | 143 +++++++++++++++++- native/cocos/renderer/gfx-base/GFXDef.h | 45 +++--- native/cocos/renderer/gfx-base/GFXDevice.cpp | 21 +++ native/cocos/renderer/gfx-base/GFXDevice.h | 42 ++--- native/cocos/renderer/gfx-base/GFXQueryPool.h | 4 +- .../gfx-base/states/GFXGlobalBarrier.cpp | 9 +- .../gfx-base/states/GFXGlobalBarrier.h | 2 +- .../renderer/gfx-base/states/GFXSampler.cpp | 14 +- .../renderer/gfx-base/states/GFXSampler.h | 2 +- .../gfx-base/states/GFXTextureBarrier.cpp | 12 +- .../gfx-base/states/GFXTextureBarrier.h | 2 +- .../cocos/renderer/gfx-empty/EmptyDevice.cpp | 12 -- native/cocos/renderer/gfx-empty/EmptyDevice.h | 4 - .../cocos/renderer/gfx-gles2/GLES2Device.cpp | 12 +- native/cocos/renderer/gfx-gles2/GLES2Device.h | 4 +- .../gfx-gles2/states/GLES2Sampler.cpp | 2 +- .../renderer/gfx-gles2/states/GLES2Sampler.h | 2 +- .../renderer/gfx-gles3/GLES3Commands.cpp | 6 +- .../cocos/renderer/gfx-gles3/GLES3Device.cpp | 12 +- native/cocos/renderer/gfx-gles3/GLES3Device.h | 5 +- .../renderer/gfx-gles3/GLES3GPUObjects.h | 2 +- .../gfx-gles3/states/GLES3GlobalBarrier.cpp | 2 +- .../gfx-gles3/states/GLES3GlobalBarrier.h | 2 +- .../gfx-gles3/states/GLES3Sampler.cpp | 2 +- .../renderer/gfx-gles3/states/GLES3Sampler.h | 2 +- native/cocos/renderer/gfx-metal/MTLDevice.h | 4 +- native/cocos/renderer/gfx-metal/MTLDevice.mm | 12 +- native/cocos/renderer/gfx-metal/MTLSampler.h | 2 +- native/cocos/renderer/gfx-metal/MTLSampler.mm | 2 +- .../gfx-validator/DeviceValidator.cpp | 12 +- .../renderer/gfx-validator/DeviceValidator.h | 6 +- .../gfx-validator/TextureValidator.cpp | 4 - .../cocos/renderer/gfx-vulkan/VKCommands.cpp | 95 +++++++----- native/cocos/renderer/gfx-vulkan/VKDevice.cpp | 12 +- native/cocos/renderer/gfx-vulkan/VKDevice.h | 6 +- .../cocos/renderer/gfx-vulkan/VKGPUObjects.h | 5 +- .../gfx-vulkan/states/VKGlobalBarrier.cpp | 2 +- .../gfx-vulkan/states/VKGlobalBarrier.h | 2 +- .../renderer/gfx-vulkan/states/VKSampler.cpp | 2 +- .../renderer/gfx-vulkan/states/VKSampler.h | 2 +- .../gfx-vulkan/states/VKTextureBarrier.cpp | 2 +- .../gfx-vulkan/states/VKTextureBarrier.h | 2 +- .../renderer/pipeline/ClusterLightCulling.cpp | 2 - .../renderer/pipeline/RenderPipeline.cpp | 24 +-- .../cocos/renderer/pipeline/RenderPipeline.h | 14 +- .../renderer/pipeline/common/BloomStage.cpp | 2 +- .../pipeline/deferred/DeferredPipeline.cpp | 2 + .../pipeline/forward/ForwardPipeline.cpp | 2 + .../pipeline/forward/ForwardPipeline.h | 5 - .../pipeline/forward/ForwardStage.cpp | 2 +- .../renderer/pipeline/shadow/ShadowFlow.cpp | 2 +- 60 files changed, 446 insertions(+), 311 deletions(-) diff --git a/native/cmake/predefine.cmake b/native/cmake/predefine.cmake index cd394723c2..fb7f8e3ba4 100644 --- a/native/cmake/predefine.cmake +++ b/native/cmake/predefine.cmake @@ -42,6 +42,8 @@ add_definitions(-DCC_PLATFORM_ANDROID=${CC_PLATFORM_ANDROID}) add_definitions(-DCC_PLATFORM_OHOS=${CC_PLATFORM_OHOS}) add_definitions(-DCC_PLATFORM=${CC_PLATFORM}) +include_directories(${CMAKE_CURRENT_LIST_DIR}/../external/sources) + # simplify generator condition, please use them everywhere if(CMAKE_GENERATOR STREQUAL Xcode) set(XCODE TRUE) @@ -97,7 +99,7 @@ macro(cocos_source_files) set(ACCEPT_MN OFF) set(MODULE_NAME "COCOS2D") foreach(src IN LISTS list_var) - if(ACCEPT_MN) + if(ACCEPT_MN) set(MODULE_NAME "${src}") set(ACCEPT_MN OFF) elseif("NO_WERROR" STREQUAL "${src}") diff --git a/native/cocos/base/threading/MessageQueue.h b/native/cocos/base/threading/MessageQueue.h index 4b1b239387..a2f6898332 100644 --- a/native/cocos/base/threading/MessageQueue.h +++ b/native/cocos/base/threading/MessageQueue.h @@ -66,6 +66,11 @@ private: friend class MessageQueue; }; +// structs may be padded +#if (CC_COMPILER == CC_COMPILER_MSVC) + #pragma warning(disable : 4324) +#endif + struct ALIGNAS(64) WriterContext final { uint8_t * currentMemoryChunk{nullptr}; Message * lastMessage{nullptr}; @@ -156,6 +161,11 @@ private: ChunkQueue _chunkFreeQueue{}; }; +// structs may be padded +#if (CC_COMPILER == CC_COMPILER_MSVC) + #pragma warning(default : 4324) +#endif + uint8_t *allocateImpl(uint32_t allocatedSize, uint32_t requestSize) noexcept; void pushMessages() noexcept; diff --git a/native/cocos/math/MathBase.h b/native/cocos/math/MathBase.h index 1ec3dbabff..073aa022d2 100644 --- a/native/cocos/math/MathBase.h +++ b/native/cocos/math/MathBase.h @@ -69,6 +69,17 @@ #define USING_NS_CC_MATH #endif +NS_CC_MATH_BEGIN + +template ::value>> +struct Hasher final { size_t operator()(const T& info) const; }; + +// make this boost::hash compatible +template ::value>> +size_t hash_value(const T& info) { return Hasher()(info); } // NOLINT(readability-identifier-naming) + +NS_CC_MATH_END + /** * end of base group * @} diff --git a/native/cocos/math/Vec4.cpp b/native/cocos/math/Vec4.cpp index 0c2a282d3b..5827aa3cbe 100644 --- a/native/cocos/math/Vec4.cpp +++ b/native/cocos/math/Vec4.cpp @@ -20,19 +20,21 @@ This file was modified to fit the cocos2d-x project */ +#include + #include "math/Vec4.h" #include -#include "math/MathUtil.h" #include "base/Macros.h" +#include "math/MathUtil.h" NS_CC_MATH_BEGIN Vec4::Vec4() -: x(0.0f), - y(0.0f), - z(0.0f), - w(0.0f) { +: x(0.0F), + y(0.0F), + z(0.0F), + w(0.0F) { } Vec4::Vec4(float xx, float yy, float zz, float ww) @@ -56,26 +58,23 @@ Vec4::Vec4(const Vec4 ©) { Vec4 Vec4::fromColor(unsigned int color) { float components[4]; - int componentIndex = 0; + int componentIndex = 0; for (int i = 3; i >= 0; --i) { - int component = (color >> i * 8) & 0x000000ff; + uint32_t component = (color >> i * 8) & 0x000000ff; - components[componentIndex++] = static_cast(component) / 255.0f; + components[componentIndex++] = static_cast(component) / 255.0F; } Vec4 value(components); return value; } -Vec4::~Vec4() { -} - bool Vec4::isZero() const { - return x == 0.0f && y == 0.0f && z == 0.0f && w == 0.0f; + return x == 0.0F && y == 0.0F && z == 0.0F && w == 0.0F; } bool Vec4::isOne() const { - return x == 1.0f && y == 1.0f && z == 1.0f && w == 1.0f; + return x == 1.0F && y == 1.0F && z == 1.0F && w == 1.0F; } float Vec4::angle(const Vec4 &v1, const Vec4 &v2) { @@ -106,29 +105,37 @@ void Vec4::clamp(const Vec4 &min, const Vec4 &max) { GP_ASSERT(!(min.x > max.x || min.y > max.y || min.z > max.z || min.w > max.w)); // Clamp the x value. - if (x < min.x) + if (x < min.x) { x = min.x; - if (x > max.x) +} + if (x > max.x) { x = max.x; +} // Clamp the y value. - if (y < min.y) + if (y < min.y) { y = min.y; - if (y > max.y) +} + if (y > max.y) { y = max.y; +} // Clamp the z value. - if (z < min.z) + if (z < min.z) { z = min.z; - if (z > max.z) +} + if (z > max.z) { z = max.z; +} // Clamp the z value. - if (w < min.w) + if (w < min.w) { w = min.w; - if (w > max.w) +} + if (w > max.w) { w = max.w; } +} void Vec4::clamp(const Vec4 &v, const Vec4 &min, const Vec4 &max, Vec4 *dst) { GP_ASSERT(dst); @@ -136,32 +143,40 @@ void Vec4::clamp(const Vec4 &v, const Vec4 &min, const Vec4 &max, Vec4 *dst) { // Clamp the x value. dst->x = v.x; - if (dst->x < min.x) + if (dst->x < min.x) { dst->x = min.x; - if (dst->x > max.x) +} + if (dst->x > max.x) { dst->x = max.x; +} // Clamp the y value. dst->y = v.y; - if (dst->y < min.y) + if (dst->y < min.y) { dst->y = min.y; - if (dst->y > max.y) +} + if (dst->y > max.y) { dst->y = max.y; +} // Clamp the z value. dst->z = v.z; - if (dst->z < min.z) + if (dst->z < min.z) { dst->z = min.z; - if (dst->z > max.z) +} + if (dst->z > max.z) { dst->z = max.z; +} // Clamp the w value. dst->w = v.w; - if (dst->w < min.w) + if (dst->w < min.w) { dst->w = min.w; - if (dst->w > max.w) +} + if (dst->w > max.w) { dst->w = max.w; } +} float Vec4::distance(const Vec4 &v) const { float dx = v.x - x; @@ -207,15 +222,17 @@ void Vec4::negate() { void Vec4::normalize() { float n = x * x + y * y + z * z + w * w; // Already normalized. - if (n == 1.0f) + if (n == 1.0F) { return; +} n = std::sqrt(n); // Too close to zero. - if (n < MATH_TOLERANCE) + if (n < MATH_TOLERANCE) { return; +} - n = 1.0f / n; + n = 1.0F / n; x *= n; y *= n; z *= n; @@ -281,11 +298,17 @@ void Vec4::subtract(const Vec4 &v1, const Vec4 &v2, Vec4 *dst) { dst->w = v1.w - v2.w; } -const Vec4 Vec4::ZERO = Vec4(0.0f, 0.0f, 0.0f, 0.0f); -const Vec4 Vec4::ONE = Vec4(1.0f, 1.0f, 1.0f, 1.0f); -const Vec4 Vec4::UNIT_X = Vec4(1.0f, 0.0f, 0.0f, 0.0f); -const Vec4 Vec4::UNIT_Y = Vec4(0.0f, 1.0f, 0.0f, 0.0f); -const Vec4 Vec4::UNIT_Z = Vec4(0.0f, 0.0f, 1.0f, 0.0f); -const Vec4 Vec4::UNIT_W = Vec4(0.0f, 0.0f, 0.0f, 1.0f); +const Vec4 Vec4::ZERO = Vec4(0.0F, 0.0F, 0.0F, 0.0F); +const Vec4 Vec4::ONE = Vec4(1.0F, 1.0F, 1.0F, 1.0F); +const Vec4 Vec4::UNIT_X = Vec4(1.0F, 0.0F, 0.0F, 0.0F); +const Vec4 Vec4::UNIT_Y = Vec4(0.0F, 1.0F, 0.0F, 0.0F); +const Vec4 Vec4::UNIT_Z = Vec4(0.0F, 0.0F, 1.0F, 0.0F); +const Vec4 Vec4::UNIT_W = Vec4(0.0F, 0.0F, 0.0F, 1.0F); + +template <> +size_t Hasher::operator()(const Vec4 &v) const { + return boost::hash_range(reinterpret_cast(&v.x), + reinterpret_cast(&v.x + 4)); +} NS_CC_MATH_END diff --git a/native/cocos/math/Vec4.h b/native/cocos/math/Vec4.h index c1c7e743b2..613bc1abb5 100644 --- a/native/cocos/math/Vec4.h +++ b/native/cocos/math/Vec4.h @@ -95,7 +95,7 @@ public: * * @param array An array containing the elements of the vector in the order x, y, z, w. */ - Vec4(const float *array); + explicit Vec4(const float *src); /** * Constructs a vector that describes the direction between the specified points. @@ -124,11 +124,6 @@ public: */ static Vec4 fromColor(unsigned int color); - /** - * Destructor. - */ - ~Vec4(); - /** * Indicates whether this vector contains all zeros. * diff --git a/native/cocos/renderer/frame-graph/ResourceAllocator.h b/native/cocos/renderer/frame-graph/ResourceAllocator.h index 1627971221..ad9c3943a8 100644 --- a/native/cocos/renderer/frame-graph/ResourceAllocator.h +++ b/native/cocos/renderer/frame-graph/ResourceAllocator.h @@ -121,31 +121,31 @@ void ResourceAllocator(deadpool.size()); if (!count) { return; } - size_t destroyBegin = count - 1; + int destroyBegin = count - 1; - for (size_t i = 0; i < count; ++i) { + for (int i = 0; i < count; ++i) { if (_age - deadpool[i].age < unusedFrameCount) { continue; } - size_t j = destroyBegin; + int j = destroyBegin; for (; j > i; --j) { if (_age - deadpool[j].age < unusedFrameCount) { std::swap(deadpool[i], deadpool[j]); - destroyBegin = j; + destroyBegin = j - 1; break; } } if (i >= j) { - destroyBegin = i; + destroyBegin = i - 1; } if (i >= destroyBegin) { @@ -153,7 +153,7 @@ void ResourceAllocatorcreateSampler(info, hash); +Sampler *DeviceAgent::getSampler(const SamplerInfo &info) { + return _actor->getSampler(info); } -GlobalBarrier *DeviceAgent::createGlobalBarrier(const GlobalBarrierInfo &info, size_t hash) { - return _actor->createGlobalBarrier(info, hash); +GlobalBarrier *DeviceAgent::getGlobalBarrier(const GlobalBarrierInfo &info) { + return _actor->getGlobalBarrier(info); } -TextureBarrier *DeviceAgent::createTextureBarrier(const TextureBarrierInfo &info, size_t hash) { - return _actor->createTextureBarrier(info, hash); +TextureBarrier *DeviceAgent::getTextureBarrier(const TextureBarrierInfo &info) { + return _actor->getTextureBarrier(info); } void DeviceAgent::copyBuffersToTexture(const uint8_t *const *buffers, Texture *dst, const BufferTextureCopy *regions, uint32_t count) { diff --git a/native/cocos/renderer/gfx-agent/DeviceAgent.h b/native/cocos/renderer/gfx-agent/DeviceAgent.h index fef1e12540..2e7dd19ccc 100644 --- a/native/cocos/renderer/gfx-agent/DeviceAgent.h +++ b/native/cocos/renderer/gfx-agent/DeviceAgent.h @@ -82,9 +82,9 @@ public: PipelineLayout * createPipelineLayout() override; PipelineState * createPipelineState() override; - Sampler * createSampler(const SamplerInfo &info, size_t hash) override; - GlobalBarrier * createGlobalBarrier(const GlobalBarrierInfo &info, size_t hash) override; - TextureBarrier *createTextureBarrier(const TextureBarrierInfo &info, size_t hash) override; + Sampler * getSampler(const SamplerInfo &info) override; + GlobalBarrier * getGlobalBarrier(const GlobalBarrierInfo &info) override; + TextureBarrier *getTextureBarrier(const TextureBarrierInfo &info) override; void copyBuffersToTexture(const uint8_t *const *buffers, Texture *dst, const BufferTextureCopy *regions, uint32_t count) override; void copyTextureToBuffers(Texture *src, uint8_t *const *buffers, const BufferTextureCopy *region, uint32_t count) override; diff --git a/native/cocos/renderer/gfx-base/GFXDef-common.h b/native/cocos/renderer/gfx-base/GFXDef-common.h index 2c540b66c8..6368aeb11e 100644 --- a/native/cocos/renderer/gfx-base/GFXDef-common.h +++ b/native/cocos/renderer/gfx-base/GFXDef-common.h @@ -976,7 +976,7 @@ struct ALIGNAS(8) TextureViewInfo { #endif }; -struct SamplerInfo { +struct ALIGNAS(8) SamplerInfo { Filter minFilter{Filter::LINEAR}; Filter magFilter{Filter::LINEAR}; Filter mipFilter{Filter::NONE}; diff --git a/native/cocos/renderer/gfx-base/GFXDef.cpp b/native/cocos/renderer/gfx-base/GFXDef.cpp index b9d2f6a8c1..0d8a06772d 100644 --- a/native/cocos/renderer/gfx-base/GFXDef.cpp +++ b/native/cocos/renderer/gfx-base/GFXDef.cpp @@ -125,6 +125,41 @@ size_t Hasher::operator()(const BufferInfo& info) const { return quickHashTrivialStruct(&info); } +template <> +size_t Hasher::operator()(const SamplerInfo& info) const { + // return quickHashTrivialStruct(&info); + + // the hash may be used to reconstruct the original struct + auto hash = static_cast(info.minFilter); + hash |= static_cast(info.magFilter) << 2; + hash |= static_cast(info.mipFilter) << 4; + hash |= static_cast(info.addressU) << 6; + hash |= static_cast(info.addressV) << 8; + hash |= static_cast(info.addressW) << 10; + hash |= static_cast(info.maxAnisotropy) << 12; + hash |= static_cast(info.cmpFunc) << 16; + return hash; +} + +template <> +size_t Hasher::operator()(const GlobalBarrierInfo& info) const { + size_t seed = 2; + boost::hash_combine(seed, info.prevAccesses); + boost::hash_combine(seed, info.nextAccesses); + return seed; +} + +template <> +size_t Hasher::operator()(const TextureBarrierInfo& info) const { + size_t seed = 5; + boost::hash_combine(seed, info.prevAccesses); + boost::hash_combine(seed, info.nextAccesses); + boost::hash_combine(seed, info.discardContents); + boost::hash_combine(seed, info.srcQueue); + boost::hash_combine(seed, info.dstQueue); + return seed; +} + bool operator==(const ColorAttachment& lhs, const ColorAttachment& rhs) { return lhs.format == rhs.format && lhs.sampleCount == rhs.sampleCount && @@ -186,9 +221,6 @@ bool operator==(const Viewport& lhs, const Viewport& rhs) { lhs.minDepth == rhs.minDepth && lhs.maxDepth == rhs.maxDepth; } -bool operator!=(const Viewport& lhs, const Viewport& rhs) { - return !(lhs == rhs); -} bool operator==(const Rect& lhs, const Rect& rhs) { return lhs.x == rhs.x && @@ -196,9 +228,6 @@ bool operator==(const Rect& lhs, const Rect& rhs) { lhs.width == rhs.width && lhs.height == rhs.height; } -bool operator!=(const Rect& lhs, const Rect& rhs) { - return !(lhs == rhs); -} bool operator==(const Color& lhs, const Color& rhs) { return lhs.x == rhs.x && @@ -237,6 +266,23 @@ bool operator==(const BufferInfo& lhs, const BufferInfo& rhs) { return !memcmp(&lhs, &rhs, sizeof(BufferInfo)); } +bool operator==(const SamplerInfo& lhs, const SamplerInfo& rhs) { + return !memcmp(&lhs, &rhs, sizeof(SamplerInfo)); +} + +bool operator==(const GlobalBarrierInfo& lhs, const GlobalBarrierInfo& rhs) { + return lhs.prevAccesses == rhs.prevAccesses && + lhs.nextAccesses == rhs.nextAccesses; +} + +bool operator==(const TextureBarrierInfo& lhs, const TextureBarrierInfo& rhs) { + return lhs.prevAccesses == rhs.prevAccesses && + lhs.nextAccesses == rhs.nextAccesses && + lhs.discardContents == rhs.discardContents && + lhs.srcQueue == rhs.srcQueue && + lhs.dstQueue == rhs.dstQueue; +} + const FormatInfo GFX_FORMAT_INFOS[] = { {"UNKNOWN", 0, 0, FormatType::NONE, false, false, false, false}, {"A8", 1, 1, FormatType::UNORM, true, false, false, false}, @@ -466,6 +512,91 @@ uint32_t formatSize(Format format, uint32_t width, uint32_t height, uint32_t dep } } +std::pair formatAlignment(Format format) { + switch (format) { + case Format::BC1: + case Format::BC1_ALPHA: + case Format::BC1_SRGB: + case Format::BC1_SRGB_ALPHA: + case Format::BC2: + case Format::BC2_SRGB: + case Format::BC3: + case Format::BC3_SRGB: + case Format::BC4: + case Format::BC4_SNORM: + case Format::BC6H_SF16: + case Format::BC6H_UF16: + case Format::BC7: + case Format::BC7_SRGB: + case Format::BC5: + case Format::BC5_SNORM: + case Format::ETC_RGB8: + case Format::ETC2_RGB8: + case Format::ETC2_SRGB8: + case Format::ETC2_RGB8_A1: + case Format::EAC_R11: + case Format::EAC_R11SN: + case Format::ETC2_RGBA8: + case Format::ETC2_SRGB8_A1: + case Format::EAC_RG11: + case Format::EAC_RG11SN: + case Format::PVRTC_RGB2: + case Format::PVRTC_RGBA2: + case Format::PVRTC2_2BPP: + return std::make_pair(4, 4); + + case Format::PVRTC_RGB4: + case Format::PVRTC_RGBA4: + case Format::PVRTC2_4BPP: + return std::make_pair(2, 2); + + case Format::ASTC_RGBA_4X4: + case Format::ASTC_SRGBA_4X4: + return std::make_pair(4, 4); + case Format::ASTC_RGBA_5X4: + case Format::ASTC_SRGBA_5X4: + return std::make_pair(5, 4); + case Format::ASTC_RGBA_5X5: + case Format::ASTC_SRGBA_5X5: + return std::make_pair(5, 5); + case Format::ASTC_RGBA_6X5: + case Format::ASTC_SRGBA_6X5: + return std::make_pair(6, 5); + case Format::ASTC_RGBA_6X6: + case Format::ASTC_SRGBA_6X6: + return std::make_pair(6, 6); + case Format::ASTC_RGBA_8X5: + case Format::ASTC_SRGBA_8X5: + return std::make_pair(8, 5); + case Format::ASTC_RGBA_8X6: + case Format::ASTC_SRGBA_8X6: + return std::make_pair(8, 6); + case Format::ASTC_RGBA_8X8: + case Format::ASTC_SRGBA_8X8: + return std::make_pair(8, 8); + case Format::ASTC_RGBA_10X5: + case Format::ASTC_SRGBA_10X5: + return std::make_pair(10, 5); + case Format::ASTC_RGBA_10X6: + case Format::ASTC_SRGBA_10X6: + return std::make_pair(10, 6); + case Format::ASTC_RGBA_10X8: + case Format::ASTC_SRGBA_10X8: + return std::make_pair(10, 8); + case Format::ASTC_RGBA_10X10: + case Format::ASTC_SRGBA_10X10: + return std::make_pair(10, 10); + case Format::ASTC_RGBA_12X10: + case Format::ASTC_SRGBA_12X10: + return std::make_pair(12, 10); + case Format::ASTC_RGBA_12X12: + case Format::ASTC_SRGBA_12X12: + return std::make_pair(12, 12); + default: + return std::make_pair(1, 1); + } +} + const uint32_t GFX_TYPE_SIZES[] = { 0, // UNKNOWN 4, // BOOL diff --git a/native/cocos/renderer/gfx-base/GFXDef.h b/native/cocos/renderer/gfx-base/GFXDef.h index 9bb3555ec1..6272e4f3b1 100644 --- a/native/cocos/renderer/gfx-base/GFXDef.h +++ b/native/cocos/renderer/gfx-base/GFXDef.h @@ -38,24 +38,29 @@ struct Hasher final { size_t operator()(const T& info) const; }; template ::value>> size_t hash_value(const T& info) { return Hasher()(info); } // NOLINT(readability-identifier-naming) -bool operator==(const ColorAttachment& lhs, const ColorAttachment& rhs); -bool operator==(const DepthStencilAttachment& lhs, const DepthStencilAttachment& rhs); -bool operator==(const SubpassInfo& lhs, const SubpassInfo& rhs); -bool operator==(const SubpassDependency& lhs, const SubpassDependency& rhs); -bool operator==(const RenderPassInfo& lhs, const RenderPassInfo& rhs); -bool operator==(const FramebufferInfo& lhs, const FramebufferInfo& rhs); -bool operator==(const Viewport& lhs, const Viewport& rhs); -bool operator==(const Rect& lhs, const Rect& rhs); -bool operator==(const Color& lhs, const Color& rhs); -bool operator==(const Offset& lhs, const Offset& rhs); -bool operator==(const Extent& lhs, const Extent& rhs); -bool operator==(const Size& lhs, const Size& rhs); -bool operator==(const TextureInfo& lhs, const TextureInfo& rhs); -bool operator==(const TextureViewInfo& lhs, const TextureViewInfo& rhs); -bool operator==(const BufferInfo& lhs, const BufferInfo& rhs); +#define DEFINE_CMP_OP(type) \ + bool operator==(const type& lhs, const type& rhs); \ + inline bool operator!=(const type& lhs, const type& rhs) { return !(lhs == rhs); } -bool operator!=(const Viewport& lhs, const Viewport& rhs); -bool operator!=(const Rect& lhs, const Rect& rhs); +DEFINE_CMP_OP(DepthStencilAttachment) +DEFINE_CMP_OP(SubpassInfo) +DEFINE_CMP_OP(SubpassDependency) +DEFINE_CMP_OP(RenderPassInfo) +DEFINE_CMP_OP(FramebufferInfo) +DEFINE_CMP_OP(Viewport) +DEFINE_CMP_OP(Rect) +DEFINE_CMP_OP(Color) +DEFINE_CMP_OP(Offset) +DEFINE_CMP_OP(Extent) +DEFINE_CMP_OP(Size) +DEFINE_CMP_OP(TextureInfo) +DEFINE_CMP_OP(TextureViewInfo) +DEFINE_CMP_OP(BufferInfo) +DEFINE_CMP_OP(SamplerInfo) +DEFINE_CMP_OP(GlobalBarrierInfo) +DEFINE_CMP_OP(TextureBarrierInfo) + +#undef DEFINE_CMP_OP struct SwapchainTextureInfo final { Swapchain* swapchain{nullptr}; @@ -91,9 +96,11 @@ constexpr uint32_t DRAW_INFO_SIZE = 28U; extern const FormatInfo GFX_FORMAT_INFOS[]; extern const uint32_t GFX_TYPE_SIZES[]; -extern uint32_t formatSize(Format format, uint32_t width, uint32_t height, uint32_t depth); +std::pair formatAlignment(Format format); -extern uint32_t formatSurfaceSize(Format format, uint32_t width, uint32_t height, uint32_t depth, uint32_t mips); +uint32_t formatSize(Format format, uint32_t width, uint32_t height, uint32_t depth); + +uint32_t formatSurfaceSize(Format format, uint32_t width, uint32_t height, uint32_t depth, uint32_t mips); } // namespace gfx } // namespace cc diff --git a/native/cocos/renderer/gfx-base/GFXDevice.cpp b/native/cocos/renderer/gfx-base/GFXDevice.cpp index ad00683f53..dc7ec5c6e4 100644 --- a/native/cocos/renderer/gfx-base/GFXDevice.cpp +++ b/native/cocos/renderer/gfx-base/GFXDevice.cpp @@ -101,5 +101,26 @@ void Device::createSurface(void *windowHandle) { } } +Sampler *Device::getSampler(const SamplerInfo &info) { + if (!_samplers.count(info)) { + _samplers[info] = createSampler(info); + } + return _samplers[info]; +} + +GlobalBarrier *Device::getGlobalBarrier(const GlobalBarrierInfo &info) { + if (!_globalBarriers.count(info)) { + _globalBarriers[info] = createGlobalBarrier(info); + } + return _globalBarriers[info]; +} + +TextureBarrier *Device::getTextureBarrier(const TextureBarrierInfo &info) { + if (!_textureBarriers.count(info)) { + _textureBarriers[info] = createTextureBarrier(info); + } + return _textureBarriers[info]; +} + } // namespace gfx } // namespace cc diff --git a/native/cocos/renderer/gfx-base/GFXDevice.h b/native/cocos/renderer/gfx-base/GFXDevice.h index cd04580ab5..73639c4bed 100644 --- a/native/cocos/renderer/gfx-base/GFXDevice.h +++ b/native/cocos/renderer/gfx-base/GFXDevice.h @@ -84,9 +84,9 @@ public: inline PipelineLayout * createPipelineLayout(const PipelineLayoutInfo &info); inline PipelineState * createPipelineState(const PipelineStateInfo &info); - inline Sampler * getSampler(const SamplerInfo &info); - inline GlobalBarrier * getGlobalBarrier(const GlobalBarrierInfo &info); - inline TextureBarrier *getTextureBarrier(const TextureBarrierInfo &info); + virtual Sampler * getSampler(const SamplerInfo &info); + virtual GlobalBarrier * getGlobalBarrier(const GlobalBarrierInfo &info); + virtual TextureBarrier *getTextureBarrier(const TextureBarrierInfo &info); virtual void copyBuffersToTexture(const uint8_t *const *buffers, Texture *dst, const BufferTextureCopy *regions, uint32_t count) = 0; virtual void copyTextureToBuffers(Texture *src, uint8_t *const *buffers, const BufferTextureCopy *region, uint32_t count) = 0; @@ -138,9 +138,9 @@ protected: virtual PipelineLayout * createPipelineLayout() = 0; virtual PipelineState * createPipelineState() = 0; - virtual Sampler * createSampler(const SamplerInfo &info, size_t hash) = 0; - virtual GlobalBarrier * createGlobalBarrier(const GlobalBarrierInfo &info, size_t hash) = 0; - virtual TextureBarrier *createTextureBarrier(const TextureBarrierInfo &info, size_t hash) = 0; + virtual Sampler * createSampler(const SamplerInfo &info) { return CC_NEW(Sampler(info)); } + virtual GlobalBarrier * createGlobalBarrier(const GlobalBarrierInfo &info) { return CC_NEW(GlobalBarrier(info)); } + virtual TextureBarrier *createTextureBarrier(const TextureBarrierInfo &info) { return CC_NEW(TextureBarrier(info)); } // For context switching between threads virtual void bindContext(bool bound) {} @@ -165,9 +165,9 @@ protected: uint32_t _numTriangles{0U}; MemoryStatus _memoryStatus; - unordered_map _samplers; - unordered_map _globalBarriers; - unordered_map _textureBarriers; + unordered_map> _samplers; + unordered_map> _globalBarriers; + unordered_map> _textureBarriers; private: vector _swapchains; @@ -272,30 +272,6 @@ PipelineState *Device::createPipelineState(const PipelineStateInfo &info) { return res; } -Sampler *Device::getSampler(const SamplerInfo &info) { - size_t hash = gfx::Sampler::computeHash(info); - if (!_samplers.count(hash)) { - _samplers[hash] = createSampler(info, hash); - } - return _samplers[hash]; -} - -GlobalBarrier *Device::getGlobalBarrier(const GlobalBarrierInfo &info) { - size_t hash = gfx::GlobalBarrier::computeHash(info); - if (!_globalBarriers.count(hash)) { - _globalBarriers[hash] = createGlobalBarrier(info, hash); - } - return _globalBarriers[hash]; -} - -TextureBarrier *Device::getTextureBarrier(const TextureBarrierInfo &info) { - size_t hash = gfx::TextureBarrier::computeHash(info); - if (!_textureBarriers.count(hash)) { - _textureBarriers[hash] = createTextureBarrier(info, hash); - } - return _textureBarriers[hash]; -} - void Device::copyBuffersToTexture(const BufferDataList &buffers, Texture *dst, const BufferTextureCopyList ®ions) { copyBuffersToTexture(buffers.data(), dst, regions.data(), utils::toUint(regions.size())); } diff --git a/native/cocos/renderer/gfx-base/GFXQueryPool.h b/native/cocos/renderer/gfx-base/GFXQueryPool.h index c4741f0c7a..244b30086f 100644 --- a/native/cocos/renderer/gfx-base/GFXQueryPool.h +++ b/native/cocos/renderer/gfx-base/GFXQueryPool.h @@ -53,8 +53,8 @@ public: void initialize(const QueryPoolInfo &info); void destroy(); - bool hasResult(uint32_t id) { return _results.count(id) != 0; } - uint64_t getResult(uint32_t id) { return _results[id]; } + inline bool hasResult(uint32_t id) { return _results.count(id) != 0; } + inline uint64_t getResult(uint32_t id) { return _results[id]; } inline QueryType getType() const { return _type; } inline uint32_t getMaxQueryObjects() const { return _maxQueryObjects; } diff --git a/native/cocos/renderer/gfx-base/states/GFXGlobalBarrier.cpp b/native/cocos/renderer/gfx-base/states/GFXGlobalBarrier.cpp index 5b2684cf73..da50344ab9 100644 --- a/native/cocos/renderer/gfx-base/states/GFXGlobalBarrier.cpp +++ b/native/cocos/renderer/gfx-base/states/GFXGlobalBarrier.cpp @@ -33,17 +33,14 @@ namespace cc { namespace gfx { -GlobalBarrier::GlobalBarrier(const GlobalBarrierInfo &info, size_t hash) +GlobalBarrier::GlobalBarrier(const GlobalBarrierInfo &info) : GFXObject(ObjectType::GLOBAL_BARRIER) { _info = info; - _hash = hash; + _hash = computeHash(info); } size_t GlobalBarrier::computeHash(const GlobalBarrierInfo &info) { - size_t seed = 2; - boost::hash_combine(seed, info.prevAccesses); - boost::hash_combine(seed, info.nextAccesses); - return seed; + return Hasher()(info); } } // namespace gfx diff --git a/native/cocos/renderer/gfx-base/states/GFXGlobalBarrier.h b/native/cocos/renderer/gfx-base/states/GFXGlobalBarrier.h index 8b9d696d51..663a31aad1 100644 --- a/native/cocos/renderer/gfx-base/states/GFXGlobalBarrier.h +++ b/native/cocos/renderer/gfx-base/states/GFXGlobalBarrier.h @@ -32,7 +32,7 @@ namespace gfx { class CC_DLL GlobalBarrier : public GFXObject { public: - GlobalBarrier(const GlobalBarrierInfo &info, size_t hash); + explicit GlobalBarrier(const GlobalBarrierInfo &info); static size_t computeHash(const GlobalBarrierInfo &info); diff --git a/native/cocos/renderer/gfx-base/states/GFXSampler.cpp b/native/cocos/renderer/gfx-base/states/GFXSampler.cpp index d3e8739909..4679f49c9c 100644 --- a/native/cocos/renderer/gfx-base/states/GFXSampler.cpp +++ b/native/cocos/renderer/gfx-base/states/GFXSampler.cpp @@ -30,22 +30,14 @@ namespace cc { namespace gfx { -Sampler::Sampler(const SamplerInfo &info, size_t hash) +Sampler::Sampler(const SamplerInfo &info) : GFXObject(ObjectType::SAMPLER) { _info = info; - _hash = hash; + _hash = computeHash(info); } size_t Sampler::computeHash(const SamplerInfo &info) { - size_t hash = toNumber(info.minFilter); - hash |= (toNumber(info.magFilter) << 2); - hash |= (toNumber(info.mipFilter) << 4); - hash |= (toNumber(info.addressU) << 6); - hash |= (toNumber(info.addressV) << 8); - hash |= (toNumber(info.addressW) << 10); - hash |= (info.maxAnisotropy << 12); - hash |= (toNumber(info.cmpFunc) << 16); - return hash; + return Hasher()(info); } } // namespace gfx diff --git a/native/cocos/renderer/gfx-base/states/GFXSampler.h b/native/cocos/renderer/gfx-base/states/GFXSampler.h index 59b49f5dc0..678be3e034 100644 --- a/native/cocos/renderer/gfx-base/states/GFXSampler.h +++ b/native/cocos/renderer/gfx-base/states/GFXSampler.h @@ -33,7 +33,7 @@ namespace gfx { class CC_DLL Sampler : public GFXObject { public: - Sampler(const SamplerInfo &info, size_t hash); + explicit Sampler(const SamplerInfo &info); static size_t computeHash(const SamplerInfo &info); diff --git a/native/cocos/renderer/gfx-base/states/GFXTextureBarrier.cpp b/native/cocos/renderer/gfx-base/states/GFXTextureBarrier.cpp index f7db9c92b6..f270c01ca3 100644 --- a/native/cocos/renderer/gfx-base/states/GFXTextureBarrier.cpp +++ b/native/cocos/renderer/gfx-base/states/GFXTextureBarrier.cpp @@ -34,20 +34,14 @@ namespace cc { namespace gfx { -TextureBarrier::TextureBarrier(const TextureBarrierInfo &info, size_t hash) +TextureBarrier::TextureBarrier(const TextureBarrierInfo &info) : GFXObject(ObjectType::TEXTURE_BARRIER) { _info = info; - _hash = hash; + _hash = computeHash(info); } size_t TextureBarrier::computeHash(const TextureBarrierInfo &info) { - size_t seed = 5; - boost::hash_combine(seed, info.prevAccesses); - boost::hash_combine(seed, info.nextAccesses); - boost::hash_combine(seed, info.discardContents); - boost::hash_combine(seed, info.srcQueue); - boost::hash_combine(seed, info.dstQueue); - return seed; + return Hasher()(info); } } // namespace gfx diff --git a/native/cocos/renderer/gfx-base/states/GFXTextureBarrier.h b/native/cocos/renderer/gfx-base/states/GFXTextureBarrier.h index 146f8748d9..f9c859f790 100644 --- a/native/cocos/renderer/gfx-base/states/GFXTextureBarrier.h +++ b/native/cocos/renderer/gfx-base/states/GFXTextureBarrier.h @@ -32,7 +32,7 @@ namespace gfx { class CC_DLL TextureBarrier : public GFXObject { public: - TextureBarrier(const TextureBarrierInfo &info, size_t hash); + explicit TextureBarrier(const TextureBarrierInfo &info); static size_t computeHash(const TextureBarrierInfo &info); diff --git a/native/cocos/renderer/gfx-empty/EmptyDevice.cpp b/native/cocos/renderer/gfx-empty/EmptyDevice.cpp index eabbb96594..c83437fc4c 100644 --- a/native/cocos/renderer/gfx-empty/EmptyDevice.cpp +++ b/native/cocos/renderer/gfx-empty/EmptyDevice.cpp @@ -145,18 +145,6 @@ PipelineState *EmptyDevice::createPipelineState() { return CC_NEW(EmptyPipelineState()); } -Sampler *EmptyDevice::createSampler(const SamplerInfo &info, size_t hash) { - return CC_NEW(Sampler(info, hash)); -} - -GlobalBarrier *EmptyDevice::createGlobalBarrier(const GlobalBarrierInfo &info, size_t hash) { - return CC_NEW(GlobalBarrier(info, hash)); -} - -TextureBarrier *EmptyDevice::createTextureBarrier(const TextureBarrierInfo &info, size_t hash) { - return CC_NEW(TextureBarrier(info, hash)); -} - void EmptyDevice::copyBuffersToTexture(const uint8_t *const *buffers, Texture *dst, const BufferTextureCopy *regions, uint32_t count) { } diff --git a/native/cocos/renderer/gfx-empty/EmptyDevice.h b/native/cocos/renderer/gfx-empty/EmptyDevice.h index d61a143286..fecac2269f 100644 --- a/native/cocos/renderer/gfx-empty/EmptyDevice.h +++ b/native/cocos/renderer/gfx-empty/EmptyDevice.h @@ -74,10 +74,6 @@ public: PipelineLayout * createPipelineLayout() override; PipelineState * createPipelineState() override; - Sampler * createSampler(const SamplerInfo &info, size_t hash) override; - GlobalBarrier * createGlobalBarrier(const GlobalBarrierInfo &info, size_t hash) override; - TextureBarrier *createTextureBarrier(const TextureBarrierInfo &info, size_t hash) override; - void copyBuffersToTexture(const uint8_t *const *buffers, Texture *dst, const BufferTextureCopy *regions, uint32_t count) override; void copyTextureToBuffers(Texture *src, uint8_t *const *buffers, const BufferTextureCopy *region, uint32_t count) override; void getQueryPoolResults(QueryPool *queryPool) override; diff --git a/native/cocos/renderer/gfx-gles2/GLES2Device.cpp b/native/cocos/renderer/gfx-gles2/GLES2Device.cpp index 020dcba7c4..30abe48dae 100644 --- a/native/cocos/renderer/gfx-gles2/GLES2Device.cpp +++ b/native/cocos/renderer/gfx-gles2/GLES2Device.cpp @@ -336,16 +336,8 @@ PipelineState *GLES2Device::createPipelineState() { return CC_NEW(GLES2PipelineState); } -Sampler *GLES2Device::createSampler(const SamplerInfo &info, size_t hash) { - return CC_NEW(GLES2Sampler(info, hash)); -} - -GlobalBarrier *GLES2Device::createGlobalBarrier(const GlobalBarrierInfo &info, size_t hash) { - return CC_NEW(GlobalBarrier(info, hash)); -} - -TextureBarrier *GLES2Device::createTextureBarrier(const TextureBarrierInfo &info, size_t hash) { - return CC_NEW(TextureBarrier(info, hash)); +Sampler *GLES2Device::createSampler(const SamplerInfo &info) { + return CC_NEW(GLES2Sampler(info)); } void GLES2Device::copyBuffersToTexture(const uint8_t *const *buffers, Texture *dst, const BufferTextureCopy *regions, uint32_t count) { diff --git a/native/cocos/renderer/gfx-gles2/GLES2Device.h b/native/cocos/renderer/gfx-gles2/GLES2Device.h index 77844d03a7..9092e508ed 100644 --- a/native/cocos/renderer/gfx-gles2/GLES2Device.h +++ b/native/cocos/renderer/gfx-gles2/GLES2Device.h @@ -102,9 +102,7 @@ protected: PipelineLayout * createPipelineLayout() override; PipelineState * createPipelineState() override; - Sampler * createSampler(const SamplerInfo &info, size_t hash) override; - GlobalBarrier * createGlobalBarrier(const GlobalBarrierInfo &info, size_t hash) override; - TextureBarrier *createTextureBarrier(const TextureBarrierInfo &info, size_t hash) override; + Sampler * createSampler(const SamplerInfo &info) override; void copyBuffersToTexture(const uint8_t *const *buffers, Texture *dst, const BufferTextureCopy *regions, uint32_t count) override; void copyTextureToBuffers(Texture *src, uint8_t *const *buffers, const BufferTextureCopy *region, uint32_t count) override; diff --git a/native/cocos/renderer/gfx-gles2/states/GLES2Sampler.cpp b/native/cocos/renderer/gfx-gles2/states/GLES2Sampler.cpp index b99efc9035..82ff2cfd35 100644 --- a/native/cocos/renderer/gfx-gles2/states/GLES2Sampler.cpp +++ b/native/cocos/renderer/gfx-gles2/states/GLES2Sampler.cpp @@ -30,7 +30,7 @@ namespace cc { namespace gfx { -GLES2Sampler::GLES2Sampler(const SamplerInfo &info, size_t hash) : Sampler(info, hash) { +GLES2Sampler::GLES2Sampler(const SamplerInfo &info) : Sampler(info) { _typedID = generateObjectID(); _gpuSampler = CC_NEW(GLES2GPUSampler); diff --git a/native/cocos/renderer/gfx-gles2/states/GLES2Sampler.h b/native/cocos/renderer/gfx-gles2/states/GLES2Sampler.h index 02fbe639da..efe99dea96 100644 --- a/native/cocos/renderer/gfx-gles2/states/GLES2Sampler.h +++ b/native/cocos/renderer/gfx-gles2/states/GLES2Sampler.h @@ -36,7 +36,7 @@ class GLES2GPUSampler; class CC_GLES2_API GLES2Sampler final : public Sampler { public: - GLES2Sampler(const SamplerInfo &info, size_t hash); + explicit GLES2Sampler(const SamplerInfo &info); ~GLES2Sampler() override; inline GLES2GPUSampler *gpuSampler() const { return _gpuSampler; } diff --git a/native/cocos/renderer/gfx-gles3/GLES3Commands.cpp b/native/cocos/renderer/gfx-gles3/GLES3Commands.cpp index e25f242690..d7c98f28d2 100644 --- a/native/cocos/renderer/gfx-gles3/GLES3Commands.cpp +++ b/native/cocos/renderer/gfx-gles3/GLES3Commands.cpp @@ -1817,12 +1817,12 @@ void cmdFuncGLES3Query(GLES3Device * /*device*/, GLES3QueryPool *queryPool, GLES std::unordered_map mapResults; for (auto queryId = 0U; queryId < queryCount; queryId++) { - uint32_t id = queryPool->getId(queryId); - auto iter = mapResults.find(id); + uint32_t glID = queryPool->getId(queryId); + auto iter = mapResults.find(glID); if (iter != mapResults.end()) { iter->second += results[queryId]; } else { - mapResults[id] = results[queryId]; + mapResults[glID] = results[queryId]; } } diff --git a/native/cocos/renderer/gfx-gles3/GLES3Device.cpp b/native/cocos/renderer/gfx-gles3/GLES3Device.cpp index d998321447..e6498b9770 100644 --- a/native/cocos/renderer/gfx-gles3/GLES3Device.cpp +++ b/native/cocos/renderer/gfx-gles3/GLES3Device.cpp @@ -330,16 +330,12 @@ PipelineState *GLES3Device::createPipelineState() { return CC_NEW(GLES3PipelineState); } -Sampler *GLES3Device::createSampler(const SamplerInfo &info, size_t hash) { - return CC_NEW(GLES3Sampler(info, hash)); +Sampler *GLES3Device::createSampler(const SamplerInfo &info) { + return CC_NEW(GLES3Sampler(info)); } -GlobalBarrier *GLES3Device::createGlobalBarrier(const GlobalBarrierInfo &info, size_t hash) { - return CC_NEW(GLES3GlobalBarrier(info, hash)); -} - -TextureBarrier *GLES3Device::createTextureBarrier(const TextureBarrierInfo &info, size_t hash) { - return CC_NEW(TextureBarrier(info, hash)); +GlobalBarrier *GLES3Device::createGlobalBarrier(const GlobalBarrierInfo &info) { + return CC_NEW(GLES3GlobalBarrier(info)); } void GLES3Device::copyBuffersToTexture(const uint8_t *const *buffers, Texture *dst, const BufferTextureCopy *regions, uint32_t count) { diff --git a/native/cocos/renderer/gfx-gles3/GLES3Device.h b/native/cocos/renderer/gfx-gles3/GLES3Device.h index ac0337114c..b368ca960a 100644 --- a/native/cocos/renderer/gfx-gles3/GLES3Device.h +++ b/native/cocos/renderer/gfx-gles3/GLES3Device.h @@ -100,9 +100,8 @@ protected: PipelineLayout * createPipelineLayout() override; PipelineState * createPipelineState() override; - Sampler * createSampler(const SamplerInfo &info, size_t hash) override; - GlobalBarrier * createGlobalBarrier(const GlobalBarrierInfo &info, size_t hash) override; - TextureBarrier *createTextureBarrier(const TextureBarrierInfo &info, size_t hash) override; + Sampler * createSampler(const SamplerInfo &info) override; + GlobalBarrier * createGlobalBarrier(const GlobalBarrierInfo &info) override; void copyBuffersToTexture(const uint8_t *const *buffers, Texture *dst, const BufferTextureCopy *regions, uint32_t count) override; void copyTextureToBuffers(Texture *src, uint8_t *const *buffers, const BufferTextureCopy *region, uint32_t count) override; diff --git a/native/cocos/renderer/gfx-gles3/GLES3GPUObjects.h b/native/cocos/renderer/gfx-gles3/GLES3GPUObjects.h index 6330f0160d..2ff2ecb14e 100644 --- a/native/cocos/renderer/gfx-gles3/GLES3GPUObjects.h +++ b/native/cocos/renderer/gfx-gles3/GLES3GPUObjects.h @@ -108,7 +108,7 @@ public: return glQueryIds[queryId]; } - return -1; + return ~0U; } }; diff --git a/native/cocos/renderer/gfx-gles3/states/GLES3GlobalBarrier.cpp b/native/cocos/renderer/gfx-gles3/states/GLES3GlobalBarrier.cpp index 6a20fd527f..7e3a2d34da 100644 --- a/native/cocos/renderer/gfx-gles3/states/GLES3GlobalBarrier.cpp +++ b/native/cocos/renderer/gfx-gles3/states/GLES3GlobalBarrier.cpp @@ -29,7 +29,7 @@ namespace cc { namespace gfx { -GLES3GlobalBarrier::GLES3GlobalBarrier(const GlobalBarrierInfo &info, size_t hash) : GlobalBarrier(info, hash) { +GLES3GlobalBarrier::GLES3GlobalBarrier(const GlobalBarrierInfo &info) : GlobalBarrier(info) { _typedID = generateObjectID(); _gpuBarrier = CC_NEW(GLES3GPUGlobalBarrier); diff --git a/native/cocos/renderer/gfx-gles3/states/GLES3GlobalBarrier.h b/native/cocos/renderer/gfx-gles3/states/GLES3GlobalBarrier.h index 4f4e8a2604..8541ed6030 100644 --- a/native/cocos/renderer/gfx-gles3/states/GLES3GlobalBarrier.h +++ b/native/cocos/renderer/gfx-gles3/states/GLES3GlobalBarrier.h @@ -35,7 +35,7 @@ class GLES3GPUGlobalBarrier; class CC_GLES3_API GLES3GlobalBarrier : public GlobalBarrier { public: - explicit GLES3GlobalBarrier(const GlobalBarrierInfo &info, size_t hash); + explicit GLES3GlobalBarrier(const GlobalBarrierInfo &info); ~GLES3GlobalBarrier() override; inline const GLES3GPUGlobalBarrier *gpuBarrier() const { return _gpuBarrier; } diff --git a/native/cocos/renderer/gfx-gles3/states/GLES3Sampler.cpp b/native/cocos/renderer/gfx-gles3/states/GLES3Sampler.cpp index 837178d389..6636294ea8 100644 --- a/native/cocos/renderer/gfx-gles3/states/GLES3Sampler.cpp +++ b/native/cocos/renderer/gfx-gles3/states/GLES3Sampler.cpp @@ -30,7 +30,7 @@ namespace cc { namespace gfx { -GLES3Sampler::GLES3Sampler(const SamplerInfo &info, size_t hash) : Sampler(info, hash) { +GLES3Sampler::GLES3Sampler(const SamplerInfo &info) : Sampler(info) { _typedID = generateObjectID(); _gpuSampler = CC_NEW(GLES3GPUSampler); diff --git a/native/cocos/renderer/gfx-gles3/states/GLES3Sampler.h b/native/cocos/renderer/gfx-gles3/states/GLES3Sampler.h index e49a0fa234..497052d6c1 100644 --- a/native/cocos/renderer/gfx-gles3/states/GLES3Sampler.h +++ b/native/cocos/renderer/gfx-gles3/states/GLES3Sampler.h @@ -35,7 +35,7 @@ class GLES3GPUSampler; class CC_GLES3_API GLES3Sampler final : public Sampler { public: - explicit GLES3Sampler(const SamplerInfo &info, size_t hash); + explicit GLES3Sampler(const SamplerInfo &info); ~GLES3Sampler() override; inline GLES3GPUSampler *gpuSampler() const { return _gpuSampler; } diff --git a/native/cocos/renderer/gfx-metal/MTLDevice.h b/native/cocos/renderer/gfx-metal/MTLDevice.h index 1f3b59bf0e..b525aad357 100644 --- a/native/cocos/renderer/gfx-metal/MTLDevice.h +++ b/native/cocos/renderer/gfx-metal/MTLDevice.h @@ -109,9 +109,7 @@ protected: DescriptorSetLayout *createDescriptorSetLayout() override; PipelineLayout *createPipelineLayout() override; PipelineState *createPipelineState() override; - GlobalBarrier *createGlobalBarrier(const GlobalBarrierInfo &info, size_t hash) override; - TextureBarrier *createTextureBarrier(const TextureBarrierInfo& info, size_t hash) override; - Sampler *createSampler(const SamplerInfo& info, size_t hash) override; + Sampler *createSampler(const SamplerInfo& info) override; Swapchain *createSwapchain() override; void copyBuffersToTexture(const uint8_t *const *buffers, Texture *dst, const BufferTextureCopy *regions, uint count) override; void copyTextureToBuffers(Texture *src, uint8_t *const *buffers, const BufferTextureCopy *region, uint count) override; diff --git a/native/cocos/renderer/gfx-metal/MTLDevice.mm b/native/cocos/renderer/gfx-metal/MTLDevice.mm index 0ac250d119..84aaddf06b 100644 --- a/native/cocos/renderer/gfx-metal/MTLDevice.mm +++ b/native/cocos/renderer/gfx-metal/MTLDevice.mm @@ -318,16 +318,8 @@ PipelineState *CCMTLDevice::createPipelineState() { return CC_NEW(CCMTLPipelineState); } -GlobalBarrier *CCMTLDevice::createGlobalBarrier(const GlobalBarrierInfo& info, size_t hash) { - return new GlobalBarrier(info, hash); -} - -TextureBarrier *CCMTLDevice::createTextureBarrier(const TextureBarrierInfo& info, size_t hash) { - return new TextureBarrier(info, hash); -} - -Sampler *CCMTLDevice::createSampler(const SamplerInfo &info, size_t hash) { - return new CCMTLSampler(info, hash); +Sampler *CCMTLDevice::createSampler(const SamplerInfo &info) { + return new CCMTLSampler(info); } Swapchain *CCMTLDevice::createSwapchain() { diff --git a/native/cocos/renderer/gfx-metal/MTLSampler.h b/native/cocos/renderer/gfx-metal/MTLSampler.h index dc4616eaca..01be302526 100644 --- a/native/cocos/renderer/gfx-metal/MTLSampler.h +++ b/native/cocos/renderer/gfx-metal/MTLSampler.h @@ -34,7 +34,7 @@ namespace gfx { class CCMTLSampler final : public Sampler { public: - explicit CCMTLSampler(const SamplerInfo& info, size_t hash); + explicit CCMTLSampler(const SamplerInfo& info); ~CCMTLSampler(); CCMTLSampler() = delete; CCMTLSampler(const CCMTLSampler &)=delete; diff --git a/native/cocos/renderer/gfx-metal/MTLSampler.mm b/native/cocos/renderer/gfx-metal/MTLSampler.mm index a6a9de221f..d5ef6ce131 100644 --- a/native/cocos/renderer/gfx-metal/MTLSampler.mm +++ b/native/cocos/renderer/gfx-metal/MTLSampler.mm @@ -34,7 +34,7 @@ namespace cc { namespace gfx { -CCMTLSampler::CCMTLSampler(const SamplerInfo& info, size_t hash) : Sampler(info, hash) { +CCMTLSampler::CCMTLSampler(const SamplerInfo& info) : Sampler(info) { _typedID = generateObjectID(); MTLSamplerDescriptor *descriptor = [[MTLSamplerDescriptor alloc] init]; #if (CC_PLATFORM == CC_PLATFORM_MAC_OSX) diff --git a/native/cocos/renderer/gfx-validator/DeviceValidator.cpp b/native/cocos/renderer/gfx-validator/DeviceValidator.cpp index dbfc4c027c..78b671d8e5 100644 --- a/native/cocos/renderer/gfx-validator/DeviceValidator.cpp +++ b/native/cocos/renderer/gfx-validator/DeviceValidator.cpp @@ -243,16 +243,16 @@ PipelineState *DeviceValidator::createPipelineState() { return result; } -Sampler *DeviceValidator::createSampler(const SamplerInfo &info, size_t hash) { - return _actor->createSampler(info, hash); +Sampler *DeviceValidator::getSampler(const SamplerInfo &info) { + return _actor->getSampler(info); } -GlobalBarrier *DeviceValidator::createGlobalBarrier(const GlobalBarrierInfo &info, size_t hash) { - return _actor->createGlobalBarrier(info, hash); +GlobalBarrier *DeviceValidator::getGlobalBarrier(const GlobalBarrierInfo &info) { + return _actor->getGlobalBarrier(info); } -TextureBarrier *DeviceValidator::createTextureBarrier(const TextureBarrierInfo &info, size_t hash) { - return _actor->createTextureBarrier(info, hash); +TextureBarrier *DeviceValidator::getTextureBarrier(const TextureBarrierInfo &info) { + return _actor->getTextureBarrier(info); } void DeviceValidator::copyBuffersToTexture(const uint8_t *const *buffers, Texture *dst, const BufferTextureCopy *regions, uint32_t count) { diff --git a/native/cocos/renderer/gfx-validator/DeviceValidator.h b/native/cocos/renderer/gfx-validator/DeviceValidator.h index 87e84dc46b..0a1eeaa22f 100644 --- a/native/cocos/renderer/gfx-validator/DeviceValidator.h +++ b/native/cocos/renderer/gfx-validator/DeviceValidator.h @@ -73,9 +73,9 @@ public: PipelineLayout * createPipelineLayout() override; PipelineState * createPipelineState() override; - Sampler * createSampler(const SamplerInfo &info, size_t hash) override; - GlobalBarrier * createGlobalBarrier(const GlobalBarrierInfo &info, size_t hash) override; - TextureBarrier *createTextureBarrier(const TextureBarrierInfo &info, size_t hash) override; + Sampler * getSampler(const SamplerInfo &info) override; + GlobalBarrier * getGlobalBarrier(const GlobalBarrierInfo &info) override; + TextureBarrier *getTextureBarrier(const TextureBarrierInfo &info) override; void copyBuffersToTexture(const uint8_t *const *buffers, Texture *dst, const BufferTextureCopy *regions, uint32_t count) override; void copyTextureToBuffers(Texture *src, uint8_t *const *buffers, const BufferTextureCopy *region, uint32_t count) override; diff --git a/native/cocos/renderer/gfx-validator/TextureValidator.cpp b/native/cocos/renderer/gfx-validator/TextureValidator.cpp index 592474c0f5..b27c612dc9 100644 --- a/native/cocos/renderer/gfx-validator/TextureValidator.cpp +++ b/native/cocos/renderer/gfx-validator/TextureValidator.cpp @@ -57,10 +57,6 @@ void TextureValidator::doInit(const TextureInfo &info) { CCASSERT(!featureCheckMap.count(_info.format) || DeviceValidator::getInstance()->hasFeature(featureCheckMap[_info.format]), "unsupported format"); - // Potentially inefficient - static const TextureUsageBit INEFFICIENT_MASK{TextureUsageBit::INPUT_ATTACHMENT | TextureUsageBit::SAMPLED}; - CCASSERT((info.usage & INEFFICIENT_MASK) != INEFFICIENT_MASK, "Both SAMPLED and INPUT_ATTACHMENT are specified?"); - /////////// execute /////////// _actor->initialize(info); diff --git a/native/cocos/renderer/gfx-vulkan/VKCommands.cpp b/native/cocos/renderer/gfx-vulkan/VKCommands.cpp index 9f7de9d4f2..f47b732b99 100644 --- a/native/cocos/renderer/gfx-vulkan/VKCommands.cpp +++ b/native/cocos/renderer/gfx-vulkan/VKCommands.cpp @@ -316,17 +316,27 @@ struct SubpassDependencyManager final { _hashes.clear(); } - void append(const VkSubpassDependency2 &dependency) { - // only cares about src/dst attributes - size_t hash = boost::hash_range(reinterpret_cast(&dependency.srcSubpass), - reinterpret_cast(&dependency.dependencyFlags)); - if (_hashes.count(hash)) return; - subpassDependencies.push_back(dependency); - _hashes.insert(hash); + void append(const VkSubpassDependency2 &info) { + if (_hashes.count(info)) return; + subpassDependencies.push_back(info); + _hashes.insert(info); } private: - unordered_set _hashes; + // only the src/dst attributes differs + struct DependencyHasher { + size_t operator()(const VkSubpassDependency2 &info) const { + return boost::hash_range(reinterpret_cast(&info.srcSubpass), + reinterpret_cast(&info.dependencyFlags)); + } + }; + struct DependencyComparer { + size_t operator()(const VkSubpassDependency2 &lhs, const VkSubpassDependency2 &rhs) const { + auto size = static_cast(reinterpret_cast(&lhs.dependencyFlags) - reinterpret_cast(&lhs.srcSubpass)); + return !memcmp(&lhs.srcSubpass, &rhs.srcSubpass, size); + } + }; + unordered_set _hashes; }; void cmdFuncCCVKCreateRenderPass(CCVKDevice *device, CCVKGPURenderPass *gpuRenderPass) { @@ -651,7 +661,8 @@ void cmdFuncCCVKCreateRenderPass(CCVKDevice *device, CCVKGPURenderPass *gpuRende dependency.srcStageMask |= info.stageMask; dependency.dstStageMask |= dstStage; dependency.srcAccessMask |= info.hasWriteAccess ? info.accessMask : 0; - dependency.dstAccessMask |= (desc.loadOp == VK_ATTACHMENT_LOAD_OP_CLEAR ? dstAccessWrite : dstAccessRead); + dependency.dstAccessMask |= dstAccessRead; + if (desc.loadOp == VK_ATTACHMENT_LOAD_OP_CLEAR || desc.initialLayout != ref.layout) dependency.dstAccessMask |= dstAccessWrite; return true; } return false; @@ -1247,39 +1258,45 @@ void cmdFuncCCVKCopyBuffersToTexture(CCVKDevice *device, const uint8_t *const *b 0, 1, &vkBarrier, 0, nullptr, 0, nullptr); } - uint32_t totalSize = 0U; - vector regionSizes(count); for (size_t i = 0U; i < count; ++i) { - const BufferTextureCopy ®ion = regions[i]; - uint32_t w = region.buffStride > 0 ? region.buffStride : region.texExtent.width; - uint32_t h = region.buffTexHeight > 0 ? region.buffTexHeight : region.texExtent.height; - totalSize += regionSizes[i] = formatSize(gpuTexture->format, w, h, region.texExtent.depth); + const BufferTextureCopy ®ion{regions[i]}; + + uint32_t regionWidth = region.buffStride > 0 ? region.buffStride : region.texExtent.width; + uint32_t regionHeight = region.buffTexHeight > 0 ? region.buffTexHeight : region.texExtent.height; + size_t regionSize = formatSize(gpuTexture->format, regionWidth, regionHeight, region.texExtent.depth); + + // calculate the max height to upload per staging buffer chunk + uint32_t stepHeight = regionHeight; + size_t stepSize = regionSize; + uint32_t heightAlignment = formatAlignment(gpuTexture->format).second; // for compressed textures + while (stepSize > CCVKGPUStagingBufferPool::CHUNK_SIZE) { + stepHeight = utils::alignTo((stepHeight - 1) / 2 + 1, heightAlignment); + stepSize = formatSize(gpuTexture->format, regionWidth, stepHeight, region.texExtent.depth); + } + + // upload in chunks + for (size_t h = 0U, s = 0U; h < regionHeight; h += stepHeight, s += stepSize) { + auto heightOffset = static_cast(h); + size_t curSize = std::min(regionSize - s, stepSize); + + CCVKGPUBuffer stagingBuffer; + stagingBuffer.size = curSize; + device->gpuStagingBufferPool()->alloc(&stagingBuffer, GFX_FORMAT_INFOS[toNumber(gpuTexture->format)].size); + memcpy(stagingBuffer.mappedData, buffers[i] + s, curSize); + + VkBufferImageCopy stagingRegion; + stagingRegion.bufferOffset = stagingBuffer.startOffset; + stagingRegion.bufferRowLength = region.buffStride; + stagingRegion.bufferImageHeight = region.buffTexHeight; + stagingRegion.imageSubresource = {gpuTexture->aspectMask, region.texSubres.mipLevel, region.texSubres.baseArrayLayer, region.texSubres.layerCount}; + stagingRegion.imageOffset = {region.texOffset.x, region.texOffset.y + heightOffset, region.texOffset.z}; + stagingRegion.imageExtent = {region.texExtent.width, std::min(stepHeight, regionHeight - heightOffset), region.texExtent.depth}; + + vkCmdCopyBufferToImage(gpuCommandBuffer->vkCommandBuffer, stagingBuffer.vkBuffer, gpuTexture->vkImage, + VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &stagingRegion); + } } - CCVKGPUBuffer stagingBuffer; - stagingBuffer.size = totalSize; - uint32_t texelSize = GFX_FORMAT_INFOS[toNumber(gpuTexture->format)].size; - device->gpuStagingBufferPool()->alloc(&stagingBuffer, texelSize); - - vector stagingRegions(count); - VkDeviceSize offset = 0; - for (size_t i = 0U; i < count; ++i) { - const BufferTextureCopy ®ion = regions[i]; - VkBufferImageCopy & stagingRegion = stagingRegions[i]; - stagingRegion.bufferOffset = stagingBuffer.startOffset + offset; - stagingRegion.bufferRowLength = region.buffStride; - stagingRegion.bufferImageHeight = region.buffTexHeight; - stagingRegion.imageSubresource = {gpuTexture->aspectMask, region.texSubres.mipLevel, region.texSubres.baseArrayLayer, region.texSubres.layerCount}; - stagingRegion.imageOffset = {region.texOffset.x, region.texOffset.y, region.texOffset.z}; - stagingRegion.imageExtent = {region.texExtent.width, region.texExtent.height, region.texExtent.depth}; - - memcpy(stagingBuffer.mappedData + offset, buffers[i], regionSizes[i]); - offset += regionSizes[i]; - } - - vkCmdCopyBufferToImage(gpuCommandBuffer->vkCommandBuffer, stagingBuffer.vkBuffer, gpuTexture->vkImage, - VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, utils::toUint(stagingRegions.size()), stagingRegions.data()); - if (hasFlag(gpuTexture->flags, TextureFlags::GEN_MIPMAP)) { VkFormatProperties formatProperties; vkGetPhysicalDeviceFormatProperties(device->gpuContext()->physicalDevice, mapVkFormat(gpuTexture->format, device->gpuDevice()), &formatProperties); diff --git a/native/cocos/renderer/gfx-vulkan/VKDevice.cpp b/native/cocos/renderer/gfx-vulkan/VKDevice.cpp index c558249857..deacc2f90c 100644 --- a/native/cocos/renderer/gfx-vulkan/VKDevice.cpp +++ b/native/cocos/renderer/gfx-vulkan/VKDevice.cpp @@ -736,16 +736,16 @@ PipelineState *CCVKDevice::createPipelineState() { return CC_NEW(CCVKPipelineState); } -Sampler *CCVKDevice::createSampler(const SamplerInfo &info, size_t hash) { - return CC_NEW(CCVKSampler(info, hash)); +Sampler *CCVKDevice::createSampler(const SamplerInfo &info) { + return CC_NEW(CCVKSampler(info)); } -GlobalBarrier *CCVKDevice::createGlobalBarrier(const GlobalBarrierInfo &info, size_t hash) { - return CC_NEW(CCVKGlobalBarrier(info, hash)); +GlobalBarrier *CCVKDevice::createGlobalBarrier(const GlobalBarrierInfo &info) { + return CC_NEW(CCVKGlobalBarrier(info)); } -TextureBarrier *CCVKDevice::createTextureBarrier(const TextureBarrierInfo &info, size_t hash) { - return CC_NEW(CCVKTextureBarrier(info, hash)); +TextureBarrier *CCVKDevice::createTextureBarrier(const TextureBarrierInfo &info) { + return CC_NEW(CCVKTextureBarrier(info)); } void CCVKDevice::copyBuffersToTexture(const uint8_t *const *buffers, Texture *dst, const BufferTextureCopy *regions, uint32_t count) { diff --git a/native/cocos/renderer/gfx-vulkan/VKDevice.h b/native/cocos/renderer/gfx-vulkan/VKDevice.h index 99279781a0..7353428235 100644 --- a/native/cocos/renderer/gfx-vulkan/VKDevice.h +++ b/native/cocos/renderer/gfx-vulkan/VKDevice.h @@ -120,9 +120,9 @@ protected: PipelineLayout * createPipelineLayout() override; PipelineState * createPipelineState() override; - Sampler * createSampler(const SamplerInfo &info, size_t hash) override; - GlobalBarrier * createGlobalBarrier(const GlobalBarrierInfo &info, size_t hash) override; - TextureBarrier *createTextureBarrier(const TextureBarrierInfo &info, size_t hash) override; + Sampler * createSampler(const SamplerInfo &info) override; + GlobalBarrier * createGlobalBarrier(const GlobalBarrierInfo &info) override; + TextureBarrier *createTextureBarrier(const TextureBarrierInfo &info) override; void copyBuffersToTexture(const uint8_t *const *buffers, Texture *dst, const BufferTextureCopy *regions, uint32_t count) override; void copyTextureToBuffers(Texture *src, uint8_t *const *buffers, const BufferTextureCopy *region, uint32_t count) override; diff --git a/native/cocos/renderer/gfx-vulkan/VKGPUObjects.h b/native/cocos/renderer/gfx-vulkan/VKGPUObjects.h index f779078ffa..6fe36db2a1 100644 --- a/native/cocos/renderer/gfx-vulkan/VKGPUObjects.h +++ b/native/cocos/renderer/gfx-vulkan/VKGPUObjects.h @@ -751,9 +751,10 @@ private: /** * Staging buffer pool, based on multiple fix-sized VkBuffer blocks. */ -constexpr size_t CHUNK_SIZE = 32 * 1024 * 1024; // 32M per block by default class CCVKGPUStagingBufferPool final : public Object { public: + static constexpr size_t CHUNK_SIZE = 16 * 1024 * 1024; // 16M per block by default + explicit CCVKGPUStagingBufferPool(CCVKGPUDevice *device) : _device(device) { } @@ -767,6 +768,8 @@ public: void alloc(CCVKGPUBuffer *gpuBuffer) { alloc(gpuBuffer, 1U); } void alloc(CCVKGPUBuffer *gpuBuffer, uint32_t alignment) { + CCASSERT(gpuBuffer->size <= CHUNK_SIZE, "required size exceeds single chunk size"); + size_t bufferCount = _pool.size(); Buffer * buffer = nullptr; VkDeviceSize offset = 0U; diff --git a/native/cocos/renderer/gfx-vulkan/states/VKGlobalBarrier.cpp b/native/cocos/renderer/gfx-vulkan/states/VKGlobalBarrier.cpp index 3d317a3395..3de225d47b 100644 --- a/native/cocos/renderer/gfx-vulkan/states/VKGlobalBarrier.cpp +++ b/native/cocos/renderer/gfx-vulkan/states/VKGlobalBarrier.cpp @@ -29,7 +29,7 @@ namespace cc { namespace gfx { -CCVKGlobalBarrier::CCVKGlobalBarrier(const GlobalBarrierInfo &info, size_t hash) : GlobalBarrier(info, hash) { +CCVKGlobalBarrier::CCVKGlobalBarrier(const GlobalBarrierInfo &info) : GlobalBarrier(info) { _typedID = generateObjectID(); _gpuBarrier = CC_NEW(CCVKGPUGlobalBarrier); diff --git a/native/cocos/renderer/gfx-vulkan/states/VKGlobalBarrier.h b/native/cocos/renderer/gfx-vulkan/states/VKGlobalBarrier.h index 15bea13a5b..cc6f0eabc4 100644 --- a/native/cocos/renderer/gfx-vulkan/states/VKGlobalBarrier.h +++ b/native/cocos/renderer/gfx-vulkan/states/VKGlobalBarrier.h @@ -35,7 +35,7 @@ class CCVKGPUGlobalBarrier; class CC_VULKAN_API CCVKGlobalBarrier : public GlobalBarrier { public: - explicit CCVKGlobalBarrier(const GlobalBarrierInfo &info, size_t hash); + explicit CCVKGlobalBarrier(const GlobalBarrierInfo &info); ~CCVKGlobalBarrier() override; inline const CCVKGPUGlobalBarrier *gpuBarrier() const { return _gpuBarrier; } diff --git a/native/cocos/renderer/gfx-vulkan/states/VKSampler.cpp b/native/cocos/renderer/gfx-vulkan/states/VKSampler.cpp index 59bfda957d..580f4e9843 100644 --- a/native/cocos/renderer/gfx-vulkan/states/VKSampler.cpp +++ b/native/cocos/renderer/gfx-vulkan/states/VKSampler.cpp @@ -30,7 +30,7 @@ namespace cc { namespace gfx { -CCVKSampler::CCVKSampler(const SamplerInfo &info, size_t hash) : Sampler(info, hash) { +CCVKSampler::CCVKSampler(const SamplerInfo &info) : Sampler(info) { _typedID = generateObjectID(); _gpuSampler = CC_NEW(CCVKGPUSampler); diff --git a/native/cocos/renderer/gfx-vulkan/states/VKSampler.h b/native/cocos/renderer/gfx-vulkan/states/VKSampler.h index de9cfb60d1..0262bc3a42 100644 --- a/native/cocos/renderer/gfx-vulkan/states/VKSampler.h +++ b/native/cocos/renderer/gfx-vulkan/states/VKSampler.h @@ -35,7 +35,7 @@ class CCVKGPUSampler; class CC_VULKAN_API CCVKSampler final : public Sampler { public: - explicit CCVKSampler(const SamplerInfo &info, size_t hash); + explicit CCVKSampler(const SamplerInfo &info); ~CCVKSampler() override; inline CCVKGPUSampler *gpuSampler() const { return _gpuSampler; } diff --git a/native/cocos/renderer/gfx-vulkan/states/VKTextureBarrier.cpp b/native/cocos/renderer/gfx-vulkan/states/VKTextureBarrier.cpp index 65d2c75b32..874d6e1851 100644 --- a/native/cocos/renderer/gfx-vulkan/states/VKTextureBarrier.cpp +++ b/native/cocos/renderer/gfx-vulkan/states/VKTextureBarrier.cpp @@ -30,7 +30,7 @@ namespace cc { namespace gfx { -CCVKTextureBarrier::CCVKTextureBarrier(const TextureBarrierInfo &info, size_t hash) : TextureBarrier(info, hash) { +CCVKTextureBarrier::CCVKTextureBarrier(const TextureBarrierInfo &info) : TextureBarrier(info) { _typedID = generateObjectID(); _gpuBarrier = CC_NEW(CCVKGPUTextureBarrier); diff --git a/native/cocos/renderer/gfx-vulkan/states/VKTextureBarrier.h b/native/cocos/renderer/gfx-vulkan/states/VKTextureBarrier.h index 021661f727..d4f964b8f7 100644 --- a/native/cocos/renderer/gfx-vulkan/states/VKTextureBarrier.h +++ b/native/cocos/renderer/gfx-vulkan/states/VKTextureBarrier.h @@ -35,7 +35,7 @@ class CCVKGPUTextureBarrier; class CC_VULKAN_API CCVKTextureBarrier : public TextureBarrier { public: - explicit CCVKTextureBarrier(const TextureBarrierInfo &info, size_t hash); + explicit CCVKTextureBarrier(const TextureBarrierInfo &info); ~CCVKTextureBarrier() override; inline const CCVKGPUTextureBarrier *gpuBarrier() const { return _gpuBarrier; } diff --git a/native/cocos/renderer/pipeline/ClusterLightCulling.cpp b/native/cocos/renderer/pipeline/ClusterLightCulling.cpp index ed3117e537..9e5524be4c 100644 --- a/native/cocos/renderer/pipeline/ClusterLightCulling.cpp +++ b/native/cocos/renderer/pipeline/ClusterLightCulling.cpp @@ -60,8 +60,6 @@ ClusterLightCulling::~ClusterLightCulling() { CC_SAFE_DESTROY(_cullingDescriptorSet); CC_SAFE_DESTROY(_constantsBuffer); - - delete _resetBarrier; } void ClusterLightCulling::initialize(gfx::Device* dev) { diff --git a/native/cocos/renderer/pipeline/RenderPipeline.cpp b/native/cocos/renderer/pipeline/RenderPipeline.cpp index c6927053f3..3041152bc5 100644 --- a/native/cocos/renderer/pipeline/RenderPipeline.cpp +++ b/native/cocos/renderer/pipeline/RenderPipeline.cpp @@ -29,6 +29,7 @@ #include "PipelineStateManager.h" #include "RenderFlow.h" #include "RenderPipeline.h" +#include "frame-graph/FrameGraph.h" #include "gfx-base/GFXCommandBuffer.h" #include "gfx-base/GFXDescriptorSet.h" #include "gfx-base/GFXDescriptorSetLayout.h" @@ -106,6 +107,8 @@ void RenderPipeline::render(const vector &cameras) { flow->render(camera); } } + + RenderPipeline::framegraphGC(); } void RenderPipeline::destroyQuadInputAssembler() { @@ -145,8 +148,6 @@ void RenderPipeline::destroy() { CC_SAFE_DESTROY(_defaultTexture); - CC_SAFE_DELETE(_defaultTexture); - PipelineStateManager::destroyAll(); InstancedBuffer::destroyInstancedBuffer(); } @@ -179,10 +180,7 @@ gfx::InputAssembler *RenderPipeline::getIAByRenderArea(const gfx::Rect &renderAr static_cast(renderArea.height) / bufferHeight, }; - size_t hash = boost::hash_range(reinterpret_cast(&viewport.x), - reinterpret_cast(&viewport.x + 4)); - - const auto iter = _quadIA.find(hash); + const auto iter = _quadIA.find(viewport); if (iter != _quadIA.end()) { return iter->second; } @@ -191,7 +189,7 @@ gfx::InputAssembler *RenderPipeline::getIAByRenderArea(const gfx::Rect &renderAr gfx::InputAssembler *ia = nullptr; createQuadInputAssembler(_quadIB, &vb, &ia); _quadVB.push_back(vb); - _quadIA[hash] = ia; + _quadIA[viewport] = ia; updateQuadVertexData(viewport, vb); @@ -208,10 +206,6 @@ bool RenderPipeline::createQuadInputAssembler(gfx::Buffer *quadIB, gfx::Buffer * gfx::MemoryUsageBit::DEVICE, vbSize, vbStride}); } - if (*quadVB == nullptr) { - return false; - } - // step 2 create input assembler gfx::InputAssemblerInfo info; info.attributes.push_back({"a_position", gfx::Format::RG32F}); @@ -333,5 +327,13 @@ bool RenderPipeline::isOccluded(const scene::Camera *camera, const scene::SubMod return _queryPools[0]->getResult(id) == 0; } +void RenderPipeline::framegraphGC() { + static uint64_t frameCount{0U}; + static constexpr uint32_t INTERVAL_IN_SECONDS = 30; + if (++frameCount % (INTERVAL_IN_SECONDS * 60)) { + framegraph::FrameGraph::gc(INTERVAL_IN_SECONDS * 60); + } +} + } // namespace pipeline } // namespace cc diff --git a/native/cocos/renderer/pipeline/RenderPipeline.h b/native/cocos/renderer/pipeline/RenderPipeline.h index d653e25bc8..74df2cf75b 100644 --- a/native/cocos/renderer/pipeline/RenderPipeline.h +++ b/native/cocos/renderer/pipeline/RenderPipeline.h @@ -121,6 +121,8 @@ protected: void generateConstantMacros(); void destroyQuadInputAssembler(); + static void framegraphGC(); + gfx::CommandBufferList _commandBuffers; gfx::QueryPoolList _queryPools; RenderFlowList _flows; @@ -137,12 +139,12 @@ protected: scene::Model * _profiler{nullptr}; // has not initBuiltinRes, // create temporary default Texture to binding sampler2d - gfx::Texture * _defaultTexture{nullptr}; - uint _width{0}; - uint _height{0}; - gfx::Buffer * _quadIB{nullptr}; - std::vector _quadVB; - std::unordered_map _quadIA; + gfx::Texture * _defaultTexture{nullptr}; + uint _width{0}; + uint _height{0}; + gfx::Buffer * _quadIB{nullptr}; + std::vector _quadVB; + std::unordered_map> _quadIA; framegraph::FrameGraph _fg; unordered_map _renderPasses; diff --git a/native/cocos/renderer/pipeline/common/BloomStage.cpp b/native/cocos/renderer/pipeline/common/BloomStage.cpp index ee9824c8d3..2bc2f1edd2 100644 --- a/native/cocos/renderer/pipeline/common/BloomStage.cpp +++ b/native/cocos/renderer/pipeline/common/BloomStage.cpp @@ -144,7 +144,7 @@ void BloomStage::render(scene::Camera *camera) { colorAttachmentInfo.usage = framegraph::RenderTargetAttachment::Usage::COLOR; colorAttachmentInfo.loadOp = gfx::LoadOp::CLEAR; colorAttachmentInfo.clearColor = _clearColors[0]; - colorAttachmentInfo.endAccesses = {gfx::AccessType::FRAGMENT_SHADER_WRITE}; + colorAttachmentInfo.endAccesses = {gfx::AccessType::FRAGMENT_SHADER_READ_TEXTURE}; uint insertPoint = static_cast(CommonInsertPoint::DIP_BLOOM); diff --git a/native/cocos/renderer/pipeline/deferred/DeferredPipeline.cpp b/native/cocos/renderer/pipeline/deferred/DeferredPipeline.cpp index 4583bdc19d..51edbf918b 100644 --- a/native/cocos/renderer/pipeline/deferred/DeferredPipeline.cpp +++ b/native/cocos/renderer/pipeline/deferred/DeferredPipeline.cpp @@ -118,6 +118,8 @@ void DeferredPipeline::render(const vector &cameras) { _commandBuffers[0]->end(); _device->flushCommands(_commandBuffers); _device->getQueue()->submit(_commandBuffers); + + RenderPipeline::framegraphGC(); } bool DeferredPipeline::activeRenderer(gfx::Swapchain *swapchain) { diff --git a/native/cocos/renderer/pipeline/forward/ForwardPipeline.cpp b/native/cocos/renderer/pipeline/forward/ForwardPipeline.cpp index ede3f9b9b7..73961d3249 100644 --- a/native/cocos/renderer/pipeline/forward/ForwardPipeline.cpp +++ b/native/cocos/renderer/pipeline/forward/ForwardPipeline.cpp @@ -112,6 +112,8 @@ void ForwardPipeline::render(const vector &cameras) { _commandBuffers[0]->end(); _device->flushCommands(_commandBuffers); _device->getQueue()->submit(_commandBuffers); + + RenderPipeline::framegraphGC(); } bool ForwardPipeline::activeRenderer(gfx::Swapchain *swapchain) { diff --git a/native/cocos/renderer/pipeline/forward/ForwardPipeline.h b/native/cocos/renderer/pipeline/forward/ForwardPipeline.h index f070145fd4..9b1948baa5 100644 --- a/native/cocos/renderer/pipeline/forward/ForwardPipeline.h +++ b/native/cocos/renderer/pipeline/forward/ForwardPipeline.h @@ -53,8 +53,6 @@ public: inline const gfx::BufferList &getLightBuffers() const { return _lightBuffers; } inline const UintList & getLightIndexOffsets() const { return _lightIndexOffsets; } inline const UintList & getLightIndices() const { return _lightIndices; } - inline uint getWidth() const { return _width; } - inline uint getHeight() const { return _height; } static framegraph::StringHandle fgStrHandleForwardColorTexture; static framegraph::StringHandle fgStrHandleForwardDepthTexture; @@ -70,9 +68,6 @@ private: gfx::BufferList _lightBuffers; UintList _lightIndexOffsets; UintList _lightIndices; - - uint _width = 0; - uint _height = 0; }; } // namespace pipeline diff --git a/native/cocos/renderer/pipeline/forward/ForwardStage.cpp b/native/cocos/renderer/pipeline/forward/ForwardStage.cpp index 21b1e44f84..6363e6c18a 100644 --- a/native/cocos/renderer/pipeline/forward/ForwardStage.cpp +++ b/native/cocos/renderer/pipeline/forward/ForwardStage.cpp @@ -179,7 +179,7 @@ void ForwardStage::render(scene::Camera *camera) { colorAttachmentInfo.loadOp = gfx::LoadOp::LOAD; } } - colorAttachmentInfo.beginAccesses = {gfx::AccessType::COLOR_ATTACHMENT_WRITE}; + colorAttachmentInfo.beginAccesses = {gfx::AccessType::FRAGMENT_SHADER_READ_TEXTURE}; colorAttachmentInfo.endAccesses = {gfx::AccessType::FRAGMENT_SHADER_READ_TEXTURE}; data.outputTex = builder.write(data.outputTex, colorAttachmentInfo); builder.writeToBlackboard(RenderPipeline::fgStrHandleOutColorTexture, data.outputTex); diff --git a/native/cocos/renderer/pipeline/shadow/ShadowFlow.cpp b/native/cocos/renderer/pipeline/shadow/ShadowFlow.cpp index 91710aa7a2..a8832a83aa 100644 --- a/native/cocos/renderer/pipeline/shadow/ShadowFlow.cpp +++ b/native/cocos/renderer/pipeline/shadow/ShadowFlow.cpp @@ -249,7 +249,7 @@ void ShadowFlow::initShadowFrameBuffer(RenderPipeline *pipeline, const scene::Li void ShadowFlow::destroy() { _renderPass = nullptr; for (const auto &rpPair : renderPassHashMap) { - rpPair.second->destroy(); + CC_DELETE(rpPair.second); } renderPassHashMap.clear();