Skip to content

Commit

Permalink
Split iDataSizeInBytes into two variables in createResource.
Browse files Browse the repository at this point in the history
  • Loading branch information
Flone-dnb committed Oct 30, 2023
1 parent 25438d1 commit 2bb88fb
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 23 deletions.
6 changes: 4 additions & 2 deletions src/engine_lib/private/game/nodes/MeshNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Error>(result)) {
Expand All @@ -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<Error>(result)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,13 @@ namespace ne {
std::variant<std::unique_ptr<GpuResource>, 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,
Expand All @@ -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 {
Expand Down Expand Up @@ -273,7 +283,9 @@ namespace ne {
const D3D12_RESOURCE_DESC& finalResourceDescription,
const std::vector<D3D12_SUBRESOURCE_DATA>& 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.
Expand All @@ -292,7 +304,9 @@ namespace ne {
allocationDesc,
finalResourceDescription,
initialFinalResourceState,
{});
{},
iElementSizeInBytes,
iElementCount);
if (std::holds_alternative<Error>(result)) {
auto err = std::get<Error>(std::move(result));
err.addCurrentLocationToErrorStack();
Expand All @@ -311,7 +325,9 @@ namespace ne {
allocationDesc,
uploadResourceDescription,
initialUploadResourceState,
{});
{},
iElementSizeInBytes,
iElementCount);
if (std::holds_alternative<Error>(result)) {
auto err = std::get<Error>(std::move(result));
err.addCurrentLocationToErrorStack();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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<std::unique_ptr<GpuResource>, Error> createResourceWithData(
const std::string& sResourceName,
const void* pBufferData,
size_t iDataSizeInBytes,
size_t iElementSizeInBytes,
size_t iElementCount,
ResourceUsageType usage,
bool bIsShaderReadWriteResource) override;

Expand Down Expand Up @@ -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.
*/
Expand All @@ -245,7 +251,9 @@ namespace ne {
const D3D12_RESOURCE_DESC& finalResourceDescription,
const std::vector<D3D12_SUBRESOURCE_DATA>& vSubresourcesToCopy,
const D3D12_RESOURCE_DESC& uploadResourceDescription,
bool bIsTextureResource);
bool bIsTextureResource,
size_t iElementSizeInBytes = 0,
size_t iElementCount = 0);

/** Allocator for GPU resources. */
ComPtr<D3D12MA::Allocator> pMemoryAllocator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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`.
Expand All @@ -113,7 +115,8 @@ namespace ne {
virtual std::variant<std::unique_ptr<GpuResource>, Error> createResourceWithData(
const std::string& sResourceName,
const void* pBufferData,
size_t iDataSizeInBytes,
size_t iElementSizeInBytes,
size_t iElementCount,
ResourceUsageType usage,
bool bIsShaderReadWriteResource) = 0;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,9 +299,13 @@ namespace ne {
std::variant<std::unique_ptr<GpuResource>, 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<Error>(uploadResourceResult)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand All @@ -167,7 +169,8 @@ namespace ne {
virtual std::variant<std::unique_ptr<GpuResource>, Error> createResourceWithData(
const std::string& sResourceName,
const void* pBufferData,
size_t iDataSizeInBytes,
size_t iElementSizeInBytes,
size_t iElementCount,
ResourceUsageType usage,
bool bIsShaderReadWriteResource) override;

Expand Down

0 comments on commit 2bb88fb

Please sign in to comment.