Skip to content

Commit c3e72f4

Browse files
Graphics Utilities: added CreateGeometryPrimitiveBuffers function
1 parent 00a8777 commit c3e72f4

File tree

4 files changed

+144
-9
lines changed

4 files changed

+144
-9
lines changed

Common/interface/GeometryPrimitives.h

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -142,21 +142,23 @@ struct GeometryPrimitiveInfo
142142
/// The size of the vertex in bytes.
143143
Uint32 VertexSize DEFAULT_INITIALIZER(0);
144144
};
145+
typedef struct GeometryPrimitiveInfo GeometryPrimitiveInfo;
145146

146147
/// Returns the size of the geometry primitive vertex in bytes.
147148
Uint32 GetGeometryPrimitiveVertexSize(GEOMETRY_PRIMITIVE_VERTEX_FLAGS VertexFlags);
148149

149150
/// Creates a geometry primitive
150151
///
151-
/// \param [in] Attribs Geometry primitive attributes, see Diligent::GeometryPrimitiveAttributes.
152-
/// \param [out] ppVertices Address of the memory location where the pointer to the output vertex data blob will be stored.
153-
/// The vertex components are stored as interleaved floating-point values.
154-
/// For example, if VertexFlags = GEOMETRY_PRIMITIVE_VERTEX_FLAG_POS_NORM, the vertex data will
155-
/// be stored as follows:
156-
/// P0, N0, P1, N1, ..., Pn, Nn.
157-
/// \param [out] ppIndices Address of the memory location where the pointer to the output index data blob will be stored.
158-
/// Index data is stored as 32-bit unsigned integers representing the triangle list.
159-
/// \param [out] pInfo Address of the memory location where the pointer to the output geometry primitive info will be stored.
152+
/// \param [in] Attribs - Geometry primitive attributes, see Diligent::GeometryPrimitiveAttributes.
153+
/// \param [out] ppVertices - Address of the memory location where the pointer to the output vertex data blob will be stored.
154+
/// The vertex components are stored as interleaved floating-point values.
155+
/// For example, if VertexFlags = GEOMETRY_PRIMITIVE_VERTEX_FLAG_POS_NORM, the vertex data will
156+
/// be stored as follows:
157+
/// P0, N0, P1, N1, ..., Pn, Nn.
158+
/// \param [out] ppIndices - Address of the memory location where the pointer to the output index data blob will be stored.
159+
/// Index data is stored as 32-bit unsigned integers representing the triangle list.
160+
/// \param [out] pInfo - A pointer to the structure that will receive information about the created geometry primitive.
161+
/// See Diligent::GeometryPrimitiveInfo.
160162
void DILIGENT_GLOBAL_FUNCTION(CreateGeometryPrimitive)(const GeometryPrimitiveAttributes REF Attribs,
161163
IDataBlob** ppVertices,
162164
IDataBlob** ppIndices,

Common/src/GeometryPrimitives.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,10 @@ void CreateGeometryPrimitive(const GeometryPrimitiveAttributes& Attribs,
219219
IDataBlob** ppIndices,
220220
GeometryPrimitiveInfo* pInfo)
221221
{
222+
DEV_CHECK_ERR(ppVertices == nullptr || *ppVertices == nullptr, "*ppVertices is not null which may cause memory leaks");
223+
DEV_CHECK_ERR(ppIndices == nullptr || *ppIndices == nullptr, "*ppIndices is not null which may cause memory leaks");
224+
225+
static_assert(GEOMETRY_PRIMITIVE_TYPE_LAST == 1, "Please update the switch below to handle the new geometry primitive type");
222226
switch (Attribs.Type)
223227
{
224228
case GEOMETRY_PRIMITIVE_TYPE_UNDEFINED:

Graphics/GraphicsTools/interface/GraphicsUtilities.h

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "../../GraphicsEngine/interface/Texture.h"
3434
#include "../../GraphicsEngine/interface/Buffer.h"
3535
#include "../../GraphicsEngine/interface/RenderDevice.h"
36+
#include "../../../Common/interface/GeometryPrimitives.h"
3637

3738
DILIGENT_BEGIN_NAMESPACE(Diligent)
3839

@@ -218,6 +219,53 @@ int64_t DILIGENT_GLOBAL_FUNCTION(GetNativeTextureFormat)(TEXTURE_FORMAT TexForma
218219
/// Returns the texture format for the given native format (e.g. DXGI_FORMAT, VkFormat) and device type.
219220
TEXTURE_FORMAT DILIGENT_GLOBAL_FUNCTION(GetTextureFormatFromNative)(int64_t NativeFormat, enum RENDER_DEVICE_TYPE DeviceType);
220221

222+
223+
/// Geometry primitive buffers creation info
224+
struct GeometryPrimitiveBuffersCreateInfo
225+
{
226+
/// Vertex buffer usage.
227+
USAGE VertexBufferUsage DEFAULT_INITIALIZER(USAGE_DEFAULT);
228+
229+
/// Index buffer usage.
230+
USAGE IndexBufferUsage DEFAULT_INITIALIZER(USAGE_DEFAULT);
231+
232+
/// Vertex buffer bind flags.
233+
BIND_FLAGS VertexBufferBindFlags DEFAULT_INITIALIZER(BIND_VERTEX_BUFFER);
234+
235+
/// Index buffer bind flags.
236+
BIND_FLAGS IndexBufferBindFlags DEFAULT_INITIALIZER(BIND_INDEX_BUFFER);
237+
238+
/// Vertex buffer mode.
239+
BUFFER_MODE VertexBufferMode DEFAULT_INITIALIZER(BUFFER_MODE_UNDEFINED);
240+
241+
/// Index buffer mode.
242+
BUFFER_MODE IndexBufferMode DEFAULT_INITIALIZER(BUFFER_MODE_UNDEFINED);
243+
244+
/// Vertex buffer CPU access flags.
245+
CPU_ACCESS_FLAGS VertexBufferCPUAccessFlags DEFAULT_INITIALIZER(CPU_ACCESS_NONE);
246+
247+
/// Index buffer CPU access flags.
248+
CPU_ACCESS_FLAGS IndexBufferCPUAccessFlags DEFAULT_INITIALIZER(CPU_ACCESS_NONE);
249+
};
250+
typedef struct GeometryPrimitiveBuffersCreateInfo GeometryPrimitiveBuffersCreateInfo;
251+
252+
/// Creates vertex and index buffers for a geometry primitive (see Diligent::CreateGeometryPrimitive)
253+
///
254+
/// \param [in] pDevice - A pointer to the render device that will be used to create the buffers.
255+
/// \param [in] Attribs - Geometry primitive attributes, see Diligent::GeometryPrimitiveAttributes.
256+
/// \param [in] pBufferCI - Optional buffer create info, see Diligent::GeometryPrimitiveBufferCreateInfo.
257+
/// If null, default values are used.
258+
/// \param [out] ppVertices - Address of the memory location where the pointer to the vertex buffer will be stored.
259+
/// \param [out] ppIndices - Address of the memory location where the pointer to the index buffer will be stored.
260+
/// \param [out] pInfo - A pointer to the structure that will receive information about the created geometry primitive.
261+
/// See Diligent::GeometryPrimitiveInfo.
262+
void DILIGENT_GLOBAL_FUNCTION(CreateGeometryPrimitiveBuffers)(IRenderDevice* pDevice,
263+
const GeometryPrimitiveAttributes REF Attribs,
264+
const GeometryPrimitiveBuffersCreateInfo* pBufferCI,
265+
IBuffer** ppVertices,
266+
IBuffer** ppIndices,
267+
GeometryPrimitiveInfo* pInfo DEFAULT_VALUE(nullptr));
268+
221269
#include "../../../Primitives/interface/UndefRefMacro.h"
222270

223271
DILIGENT_END_NAMESPACE // namespace Diligent

Graphics/GraphicsTools/src/GraphicsUtilities.cpp

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include <algorithm>
2929
#include <cmath>
3030
#include <limits>
31+
#include <atomic>
3132

3233
#include "GraphicsUtilities.h"
3334
#include "DebugUtilities.hpp"
@@ -659,6 +660,76 @@ TEXTURE_FORMAT GetTextureFormatFromNative(int64_t NativeFormat, RENDER_DEVICE_TY
659660
}
660661
}
661662

663+
void CreateGeometryPrimitiveBuffers(IRenderDevice* pDevice,
664+
const GeometryPrimitiveAttributes& Attribs,
665+
const GeometryPrimitiveBuffersCreateInfo* pBufferCI,
666+
IBuffer** ppVertices,
667+
IBuffer** ppIndices,
668+
GeometryPrimitiveInfo* pInfo)
669+
{
670+
static_assert(GEOMETRY_PRIMITIVE_TYPE_LAST == 1, "Please handle the new primitive type");
671+
672+
RefCntAutoPtr<IDataBlob> pVertexData;
673+
RefCntAutoPtr<IDataBlob> pIndexData;
674+
CreateGeometryPrimitive(Attribs,
675+
ppVertices != nullptr ? pVertexData.RawDblPtr() : nullptr,
676+
ppIndices != nullptr ? pIndexData.RawDblPtr() : nullptr,
677+
pInfo);
678+
679+
static constexpr GeometryPrimitiveBuffersCreateInfo DefaultCI{};
680+
if (pBufferCI == nullptr)
681+
pBufferCI = &DefaultCI;
682+
683+
const char* PrimTypeStr = "";
684+
switch (Attribs.Type)
685+
{
686+
case GEOMETRY_PRIMITIVE_TYPE_CUBE: PrimTypeStr = "Cube"; break;
687+
default: UNEXPECTED("Unexpected primitive type");
688+
}
689+
690+
static std::atomic<int> PrimCounter{0};
691+
const int PrimId = PrimCounter.fetch_add(1);
692+
if (pVertexData)
693+
{
694+
const std::string Name = std::string{"Geometry primitive "} + std::to_string(PrimId) + " (" + PrimTypeStr + ")";
695+
696+
BufferDesc VBDesc;
697+
VBDesc.Name = Name.c_str();
698+
VBDesc.Size = pVertexData->GetSize();
699+
VBDesc.BindFlags = pBufferCI->VertexBufferBindFlags;
700+
VBDesc.Usage = pBufferCI->VertexBufferUsage;
701+
VBDesc.CPUAccessFlags = pBufferCI->VertexBufferCPUAccessFlags;
702+
VBDesc.Mode = pBufferCI->VertexBufferMode;
703+
if (VBDesc.Mode != BUFFER_MODE_UNDEFINED)
704+
{
705+
VBDesc.ElementByteStride = GetGeometryPrimitiveVertexSize(Attribs.VertexFlags);
706+
}
707+
708+
BufferData VBData{pVertexData->GetDataPtr(), pVertexData->GetSize()};
709+
pDevice->CreateBuffer(VBDesc, &VBData, ppVertices);
710+
}
711+
712+
if (pIndexData)
713+
{
714+
const std::string Name = std::string{"Geometry primitive "} + std::to_string(PrimId) + " (" + PrimTypeStr + ")";
715+
716+
BufferDesc IBDesc;
717+
IBDesc.Name = Name.c_str();
718+
IBDesc.Size = pIndexData->GetSize();
719+
IBDesc.BindFlags = pBufferCI->IndexBufferBindFlags;
720+
IBDesc.Usage = pBufferCI->IndexBufferUsage;
721+
IBDesc.CPUAccessFlags = pBufferCI->IndexBufferCPUAccessFlags;
722+
IBDesc.Mode = pBufferCI->IndexBufferMode;
723+
if (IBDesc.Mode != BUFFER_MODE_UNDEFINED)
724+
{
725+
IBDesc.ElementByteStride = sizeof(Uint32);
726+
}
727+
728+
BufferData IBData{pIndexData->GetDataPtr(), pIndexData->GetSize()};
729+
pDevice->CreateBuffer(IBDesc, &IBData, ppIndices);
730+
}
731+
}
732+
662733
} // namespace Diligent
663734

664735

@@ -745,4 +816,14 @@ extern "C"
745816
{
746817
return Diligent::GetTextureFormatFromNative(NativeFormat, DeviceType);
747818
}
819+
820+
void Diligent_CreateGeometryPrimitiveBuffers(Diligent::IRenderDevice* pDevice,
821+
const Diligent::GeometryPrimitiveAttributes& Attribs,
822+
const Diligent::GeometryPrimitiveBuffersCreateInfo* pBufferCI,
823+
Diligent::IBuffer** ppVertices,
824+
Diligent::IBuffer** ppIndices,
825+
Diligent::GeometryPrimitiveInfo* pInfo)
826+
{
827+
Diligent::CreateGeometryPrimitiveBuffers(pDevice, Attribs, pBufferCI, ppVertices, ppIndices, pInfo);
828+
}
748829
}

0 commit comments

Comments
 (0)