diff --git a/src/engine_lib/private/game/nodes/MeshNode.cpp b/src/engine_lib/private/game/nodes/MeshNode.cpp index 722f7d017..935ab99c1 100644 --- a/src/engine_lib/private/game/nodes/MeshNode.cpp +++ b/src/engine_lib/private/game/nodes/MeshNode.cpp @@ -257,7 +257,8 @@ namespace ne { auto result = pResourceManager->createResourceWithData( std::format("mesh node \"{}\" vertex buffer", getNodeName()), meshData.getVertices()->data(), - meshData.getVertices()->size() * sizeof(MeshVertex), + sizeof(MeshVertex), + meshData.getVertices()->size(), ResourceUsageType::VERTEX_BUFFER, true); if (std::holds_alternative(result)) { @@ -274,7 +275,8 @@ namespace ne { result = pResourceManager->createResourceWithData( std::format("mesh node \"{}\" index buffer for material slot {}", getNodeName(), i), pIndices->at(i).data(), - pIndices->at(i).size() * sizeof(MeshData::meshindex_t), + sizeof(MeshData::meshindex_t), + pIndices->at(i).size(), ResourceUsageType::INDEX_BUFFER, true); if (std::holds_alternative(result)) { diff --git a/src/engine_lib/private/render/directx/resources/DirectXResourceManager.cpp b/src/engine_lib/private/render/directx/resources/DirectXResourceManager.cpp index cad210ad6..46383fbcf 100644 --- a/src/engine_lib/private/render/directx/resources/DirectXResourceManager.cpp +++ b/src/engine_lib/private/render/directx/resources/DirectXResourceManager.cpp @@ -164,9 +164,13 @@ namespace ne { std::variant, Error> DirectXResourceManager::createResourceWithData( const std::string& sResourceName, const void* pBufferData, - size_t iDataSizeInBytes, + size_t iElementSizeInBytes, + size_t iElementCount, ResourceUsageType usage, bool bIsShaderReadWriteResource) { + // Calculate final resource size. + const auto iDataSizeInBytes = iElementSizeInBytes * iElementCount; + // Prepare final resource description. const auto finalResourceDescription = CD3DX12_RESOURCE_DESC::Buffer( iDataSizeInBytes, @@ -185,7 +189,13 @@ namespace ne { // Create resource. return createResourceWithData( - sResourceName, finalResourceDescription, vSubresourcesToCopy, uploadResourceDescription, false); + sResourceName, + finalResourceDescription, + vSubresourcesToCopy, + uploadResourceDescription, + false, + iElementSizeInBytes, + iElementCount); } size_t DirectXResourceManager::getTotalVideoMemoryInMb() const { @@ -273,7 +283,9 @@ namespace ne { const D3D12_RESOURCE_DESC& finalResourceDescription, const std::vector& vSubresourcesToCopy, const D3D12_RESOURCE_DESC& uploadResourceDescription, - bool bIsTextureResource) { + bool bIsTextureResource, + size_t iElementSizeInBytes, + size_t iElementCount) { // In order to create a GPU resource with our data from the CPU // we have to do a few steps: // 1. Create a GPU resource with DEFAULT heap type (CPU read-only heap) AKA resulting resource. @@ -292,7 +304,9 @@ namespace ne { allocationDesc, finalResourceDescription, initialFinalResourceState, - {}); + {}, + iElementSizeInBytes, + iElementCount); if (std::holds_alternative(result)) { auto err = std::get(std::move(result)); err.addCurrentLocationToErrorStack(); @@ -311,7 +325,9 @@ namespace ne { allocationDesc, uploadResourceDescription, initialUploadResourceState, - {}); + {}, + iElementSizeInBytes, + iElementCount); if (std::holds_alternative(result)) { auto err = std::get(std::move(result)); err.addCurrentLocationToErrorStack(); diff --git a/src/engine_lib/private/render/directx/resources/DirectXResourceManager.h b/src/engine_lib/private/render/directx/resources/DirectXResourceManager.h index 5c558d9c1..37fb0418c 100644 --- a/src/engine_lib/private/render/directx/resources/DirectXResourceManager.h +++ b/src/engine_lib/private/render/directx/resources/DirectXResourceManager.h @@ -54,7 +54,7 @@ namespace ne { const std::string& sResourceName, const std::filesystem::path& pathToTextureFile) override; /** - * Creates a new GPU resource with available CPU write access (only write not read), + * Creates a new GPU resource with available CPU write access (only CPU write not read), * typically used for resources that needs to be frequently updated from the CPU side. * * Example: @@ -100,22 +100,26 @@ namespace ne { * auto result = pResourceManager->createResourceWithData( * "mesh vertex buffer", * vVertices.data(), - * vVertices.size() * sizeof(glm::vec3), + * sizeof(glm::vec3), + * vVertices.size(), * true); * @endcode * * @param sResourceName Resource name, used for logging. * @param pBufferData Pointer to the data that the new resource will contain. - * @param iDataSizeInBytes Size in bytes of the data (resource size). - * @param usage Ignored. - * @param bIsShaderReadWriteResource Whether the new resource allows unordered access or not. + * @param iElementSizeInBytes Size of one buffer element in bytes. + * @param iElementCount Number of elements in the resulting buffer. + * @param usage Describes how you plan to use this resource. + * @param bIsShaderReadWriteResource Specify `true` if you plan to modify the resource + * from shaders, otherwise `false`. * * @return Error if something went wrong, otherwise created resource with filled data. */ virtual std::variant, Error> createResourceWithData( const std::string& sResourceName, const void* pBufferData, - size_t iDataSizeInBytes, + size_t iElementSizeInBytes, + size_t iElementCount, ResourceUsageType usage, bool bIsShaderReadWriteResource) override; @@ -231,12 +235,14 @@ namespace ne { /** * Creates a new GPU resource and fills it with the specified data. * - * @param finalResourceDescription Description of the final resource to create. * @param sResourceName Resource name, used for logging. + * @param finalResourceDescription Description of the final resource to create. * @param vSubresourcesToCopy Describes the data that the resulting resource should have. * @param uploadResourceDescription Description of the upload/staging resource. * @param bIsTextureResource `true` if the final resource will be used as a read-only * texture in pixel shader, `false` if the final resource is not a texture. + * @param iElementSizeInBytes Optional size of one buffer element in bytes. + * @param iElementCount Optional number of elements in the resulting buffer. * * @return Error if something went wrong, otherwise created resource with filled data. */ @@ -245,7 +251,9 @@ namespace ne { const D3D12_RESOURCE_DESC& finalResourceDescription, const std::vector& vSubresourcesToCopy, const D3D12_RESOURCE_DESC& uploadResourceDescription, - bool bIsTextureResource); + bool bIsTextureResource, + size_t iElementSizeInBytes = 0, + size_t iElementCount = 0); /** Allocator for GPU resources. */ ComPtr pMemoryAllocator; diff --git a/src/engine_lib/private/render/general/resources/GpuResourceManager.h b/src/engine_lib/private/render/general/resources/GpuResourceManager.h index fe2c15700..107c242a4 100644 --- a/src/engine_lib/private/render/general/resources/GpuResourceManager.h +++ b/src/engine_lib/private/render/general/resources/GpuResourceManager.h @@ -54,7 +54,7 @@ namespace ne { const std::string& sResourceName, const std::filesystem::path& pathToTextureFile) = 0; /** - * Creates a new GPU resource with available CPU write access (only write not read), + * Creates a new GPU resource with available CPU write access (only CPU write not read), * typically used for resources that needs to be frequently updated from the CPU side. * * Example: @@ -97,13 +97,15 @@ namespace ne { * auto result = pResourceManager->createResourceWithData( * "mesh vertex buffer", * vVertices.data(), - * vVertices.size() * sizeof(glm::vec3), + * sizeof(glm::vec3), + * vVertices.size(), * true); * @endcode * * @param sResourceName Resource name, used for logging. * @param pBufferData Pointer to the data that the new resource will contain. - * @param iDataSizeInBytes Size in bytes of the data (resource size). + * @param iElementSizeInBytes Size of one buffer element in bytes. + * @param iElementCount Number of elements in the resulting buffer. * @param usage Describes how you plan to use this resource. * @param bIsShaderReadWriteResource Specify `true` if you plan to modify the resource * from shaders, otherwise `false`. @@ -113,7 +115,8 @@ namespace ne { virtual std::variant, Error> createResourceWithData( const std::string& sResourceName, const void* pBufferData, - size_t iDataSizeInBytes, + size_t iElementSizeInBytes, + size_t iElementCount, ResourceUsageType usage, bool bIsShaderReadWriteResource) = 0; diff --git a/src/engine_lib/private/render/vulkan/resources/VulkanResourceManager.cpp b/src/engine_lib/private/render/vulkan/resources/VulkanResourceManager.cpp index b14802c63..0c8ad07a7 100644 --- a/src/engine_lib/private/render/vulkan/resources/VulkanResourceManager.cpp +++ b/src/engine_lib/private/render/vulkan/resources/VulkanResourceManager.cpp @@ -299,9 +299,13 @@ namespace ne { std::variant, Error> VulkanResourceManager::createResourceWithData( const std::string& sResourceName, const void* pBufferData, - size_t iDataSizeInBytes, + size_t iElementSizeInBytes, + size_t iElementCount, ResourceUsageType usage, bool bIsShaderReadWriteResource) { + // Calculate final data size. + const auto iDataSizeInBytes = iElementSizeInBytes * iElementCount; + // Create an upload resource for uploading data. auto uploadResourceResult = createResourceWithCpuWriteAccess(sResourceName, iDataSizeInBytes, 1, {}); if (std::holds_alternative(uploadResourceResult)) { diff --git a/src/engine_lib/private/render/vulkan/resources/VulkanResourceManager.h b/src/engine_lib/private/render/vulkan/resources/VulkanResourceManager.h index 0bc9fd5c7..a9450c6ea 100644 --- a/src/engine_lib/private/render/vulkan/resources/VulkanResourceManager.h +++ b/src/engine_lib/private/render/vulkan/resources/VulkanResourceManager.h @@ -151,13 +151,15 @@ namespace ne { * auto result = pResourceManager->createResourceWithData( * "mesh vertex buffer", * vVertices.data(), - * vVertices.size() * sizeof(glm::vec3), + * sizeof(glm::vec3), + * vVertices.size(), * true); * @endcode * * @param sResourceName Resource name, used for logging. * @param pBufferData Pointer to the data that the new resource will contain. - * @param iDataSizeInBytes Size in bytes of the data (resource size). + * @param iElementSizeInBytes Size of one buffer element in bytes. + * @param iElementCount Number of elements in the resulting buffer. * @param usage Describes how you plan to use this resource. * @param bIsShaderReadWriteResource Specify `true` if you plan to modify the resource * from shaders, otherwise `false`. @@ -167,7 +169,8 @@ namespace ne { virtual std::variant, Error> createResourceWithData( const std::string& sResourceName, const void* pBufferData, - size_t iDataSizeInBytes, + size_t iElementSizeInBytes, + size_t iElementCount, ResourceUsageType usage, bool bIsShaderReadWriteResource) override;