Skip to content

Commit

Permalink
Use CreateGeometryPrimitiveBuffers function to create cube vertex buf…
Browse files Browse the repository at this point in the history
…fers
  • Loading branch information
TheMostDiligent committed Dec 29, 2024
1 parent fc35d5e commit 3e9833f
Show file tree
Hide file tree
Showing 32 changed files with 133 additions and 244 deletions.
170 changes: 39 additions & 131 deletions Tutorials/Common/src/TexturedCube.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2023 Diligent Graphics LLC
* Copyright 2019-2024 Diligent Graphics LLC
* Copyright 2015-2019 Egor Yusov
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -31,144 +31,53 @@
#include "BasicMath.hpp"
#include "TextureUtilities.h"
#include "GraphicsTypesX.hpp"
#include "GraphicsUtilities.h"

namespace Diligent
{

namespace TexturedCube
{


// (-1,+1,+1)________________(+1,+1,+1) Z
// /| /| | Y
// / | / | | /
// / | / | | /
// / | / | | /
//(-1,-1,+1) /____|__________/(+1,-1,+1) | /
// | |__________|____| | /
// | /(-1,+1,-1) | /(+1,+1,-1) |----------------> X
// | / | /
// | / | /
// |/ | /
// /_______________|/
// (-1,-1,-1) (+1,-1,-1)
//

const std::array<float3, NumVertices> Positions = {
float3{-1, -1, -1}, float3{-1, +1, -1}, float3{+1, +1, -1}, float3{+1, -1, -1}, // Bottom
float3{-1, -1, -1}, float3{-1, -1, +1}, float3{+1, -1, +1}, float3{+1, -1, -1}, // Front
float3{+1, -1, -1}, float3{+1, -1, +1}, float3{+1, +1, +1}, float3{+1, +1, -1}, // Right
float3{+1, +1, -1}, float3{+1, +1, +1}, float3{-1, +1, +1}, float3{-1, +1, -1}, // Back
float3{-1, +1, -1}, float3{-1, +1, +1}, float3{-1, -1, +1}, float3{-1, -1, -1}, // Left
float3{-1, -1, +1}, float3{+1, -1, +1}, float3{+1, +1, +1}, float3{-1, +1, +1} // Top
};

const std::array<float2, NumVertices> Texcoords = {
float2{0, 1}, float2{0, 0}, float2{1, 0}, float2{1, 1}, // Bottom
float2{0, 1}, float2{0, 0}, float2{1, 0}, float2{1, 1}, // Front
float2{0, 1}, float2{1, 1}, float2{1, 0}, float2{0, 0}, // Right
float2{0, 1}, float2{0, 0}, float2{1, 0}, float2{1, 1}, // Back
float2{1, 0}, float2{0, 0}, float2{0, 1}, float2{1, 1}, // Left
float2{1, 1}, float2{0, 1}, float2{0, 0}, float2{1, 0} // Top
};

const std::array<float3, NumVertices> Normals = {
float3{0, 0, -1}, float3{0, 0, -1}, float3{0, 0, -1}, float3{0, 0, -1}, // Bottom
float3{0, -1, 0}, float3{0, -1, 0}, float3{0, -1, 0}, float3{0, -1, 0}, // Front
float3{+1, 0, 0}, float3{+1, 0, 0}, float3{+1, 0, 0}, float3{+1, 0, 0}, // Right
float3{0, +1, 0}, float3{0, +1, 0}, float3{0, +1, 0}, float3{0, +1, 0}, // Back
float3{-1, 0, 0}, float3{-1, 0, 0}, float3{-1, 0, 0}, float3{-1, 0, 0}, // Left
float3{0, 0, +1}, float3{0, 0, +1}, float3{0, 0, +1}, float3{0, 0, +1} // Top
};

// clang-format off
const std::array<Uint32, NumIndices> Indices =
RefCntAutoPtr<IBuffer> CreateVertexBuffer(IRenderDevice* pDevice,
GEOMETRY_PRIMITIVE_VERTEX_FLAGS Components,
BIND_FLAGS BindFlags,
BUFFER_MODE Mode)
{
2,0,1, 2,3,0,
4,6,5, 4,7,6,
8,10,9, 8,11,10,
12,14,13, 12,15,14,
16,18,17, 16,19,18,
20,21,22, 20,22,23
};
// clang-format on

RefCntAutoPtr<IBuffer> CreateVertexBuffer(IRenderDevice* pDevice,
VERTEX_COMPONENT_FLAGS Components,
BIND_FLAGS BindFlags,
BUFFER_MODE Mode)
{
VERIFY_EXPR(Components != VERTEX_COMPONENT_FLAG_NONE);
const Uint32 TotalVertexComponents =
((Components & VERTEX_COMPONENT_FLAG_POSITION) ? 3 : 0) +
((Components & VERTEX_COMPONENT_FLAG_NORMAL) ? 3 : 0) +
((Components & VERTEX_COMPONENT_FLAG_TEXCOORD) ? 2 : 0);

std::vector<float> VertexData(size_t{TotalVertexComponents} * NumVertices);

auto it = VertexData.begin();
for (Uint32 v = 0; v < NumVertices; ++v)
{
if (Components & VERTEX_COMPONENT_FLAG_POSITION)
{
const auto& Pos{Positions[v]};
*(it++) = Pos.x;
*(it++) = Pos.y;
*(it++) = Pos.z;
}
if (Components & VERTEX_COMPONENT_FLAG_NORMAL)
{
const auto& N{Normals[v]};
*(it++) = N.x;
*(it++) = N.y;
*(it++) = N.z;
}
if (Components & VERTEX_COMPONENT_FLAG_TEXCOORD)
{
const auto& UV{Texcoords[v]};
*(it++) = UV.x;
*(it++) = UV.y;
}
}
VERIFY_EXPR(it == VertexData.end());

BufferDesc VertBuffDesc;
VertBuffDesc.Name = "Cube vertex buffer";
VertBuffDesc.Usage = USAGE_IMMUTABLE;
VertBuffDesc.BindFlags = BindFlags;
VertBuffDesc.Size = static_cast<Uint64>(VertexData.size() * sizeof(VertexData[0]));
VertBuffDesc.Mode = Mode;
if (Mode != BUFFER_MODE_UNDEFINED)
{
VertBuffDesc.ElementByteStride = TotalVertexComponents * sizeof(VertexData[0]);
}

BufferData VBData;
VBData.pData = VertexData.data();
VBData.DataSize = VertBuffDesc.Size;
RefCntAutoPtr<IBuffer> pCubeVertexBuffer;

pDevice->CreateBuffer(VertBuffDesc, &VBData, &pCubeVertexBuffer);

return pCubeVertexBuffer;
GeometryPrimitiveBuffersCreateInfo CubeBuffersCI;
CubeBuffersCI.VertexBufferBindFlags = BindFlags;
CubeBuffersCI.VertexBufferMode = Mode;

GeometryPrimitiveInfo CubePrimInfo;
RefCntAutoPtr<IBuffer> pVertices;
CreateGeometryPrimitiveBuffers(
pDevice,
CubeGeometryPrimitiveAttributes{2.f, Components},
&CubeBuffersCI,
&pVertices,
nullptr,
&CubePrimInfo);
VERIFY_EXPR(CubePrimInfo.NumVertices == 24 && CubePrimInfo.NumIndices == 36);
return pVertices;
}

RefCntAutoPtr<IBuffer> CreateIndexBuffer(IRenderDevice* pDevice, BIND_FLAGS BindFlags, BUFFER_MODE Mode)
{
BufferDesc IndBuffDesc;
IndBuffDesc.Name = "Cube index buffer";
IndBuffDesc.Usage = USAGE_IMMUTABLE;
IndBuffDesc.BindFlags = BindFlags;
IndBuffDesc.Size = sizeof(Indices);
IndBuffDesc.Mode = Mode;
if (Mode != BUFFER_MODE_UNDEFINED)
IndBuffDesc.ElementByteStride = sizeof(Indices[0]);
BufferData IBData;
IBData.pData = Indices.data();
IBData.DataSize = NumIndices * sizeof(Indices[0]);
RefCntAutoPtr<IBuffer> pBuffer;
pDevice->CreateBuffer(IndBuffDesc, &IBData, &pBuffer);
return pBuffer;
GeometryPrimitiveBuffersCreateInfo CubeBuffersCI;
CubeBuffersCI.IndexBufferBindFlags = BindFlags;
CubeBuffersCI.IndexBufferMode = Mode;

GeometryPrimitiveInfo CubePrimInfo;
RefCntAutoPtr<IBuffer> pIndices;
CreateGeometryPrimitiveBuffers(
pDevice,
CubeGeometryPrimitiveAttributes{},
&CubeBuffersCI,
nullptr,
&pIndices,
&CubePrimInfo);
VERIFY_EXPR(CubePrimInfo.NumVertices == 24 && CubePrimInfo.NumIndices == 36);
return pIndices;
}

RefCntAutoPtr<ITexture> LoadTexture(IRenderDevice* pDevice, const char* Path)
Expand All @@ -180,7 +89,6 @@ RefCntAutoPtr<ITexture> LoadTexture(IRenderDevice* pDevice, const char* Path)
return pTex;
}


RefCntAutoPtr<IPipelineState> CreatePipelineState(const CreatePSOInfo& CreateInfo, bool ConvertPSOutputToGamma)
{
GraphicsPipelineStateCreateInfo PSOCreateInfo;
Expand Down Expand Up @@ -253,11 +161,11 @@ RefCntAutoPtr<IPipelineState> CreatePipelineState(const CreatePSOInfo& CreateInf
InputLayoutDescX InputLayout;

Uint32 Attrib = 0;
if (CreateInfo.Components & VERTEX_COMPONENT_FLAG_POSITION)
if (CreateInfo.Components & GEOMETRY_PRIMITIVE_VERTEX_FLAG_POSITION)
InputLayout.Add(Attrib++, 0u, 3u, VT_FLOAT32, False);
if (CreateInfo.Components & VERTEX_COMPONENT_FLAG_NORMAL)
if (CreateInfo.Components & GEOMETRY_PRIMITIVE_VERTEX_FLAG_NORMAL)
InputLayout.Add(Attrib++, 0u, 3u, VT_FLOAT32, False);
if (CreateInfo.Components & VERTEX_COMPONENT_FLAG_TEXCOORD)
if (CreateInfo.Components & GEOMETRY_PRIMITIVE_VERTEX_FLAG_TEXCOORD)
InputLayout.Add(Attrib++, 0u, 2u, VT_FLOAT32, False);

for (Uint32 i = 0; i < CreateInfo.NumExtraLayoutElements; ++i)
Expand Down
40 changes: 7 additions & 33 deletions Tutorials/Common/src/TexturedCube.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2023 Diligent Graphics LLC
* Copyright 2019-2024 Diligent Graphics LLC
* Copyright 2015-2019 Egor Yusov
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -33,35 +33,18 @@
#include "Buffer.h"
#include "RefCntAutoPtr.hpp"
#include "BasicMath.hpp"
#include "GeometryPrimitives.h"

namespace Diligent
{

namespace TexturedCube
{

enum VERTEX_COMPONENT_FLAGS : Uint32
{
VERTEX_COMPONENT_FLAG_NONE = 0x00,
VERTEX_COMPONENT_FLAG_POSITION = 0x01,
VERTEX_COMPONENT_FLAG_NORMAL = 0x02,
VERTEX_COMPONENT_FLAG_TEXCOORD = 0x04,

VERTEX_COMPONENT_FLAG_POS_UV =
VERTEX_COMPONENT_FLAG_POSITION |
VERTEX_COMPONENT_FLAG_TEXCOORD,

VERTEX_COMPONENT_FLAG_POS_NORM_UV =
VERTEX_COMPONENT_FLAG_POSITION |
VERTEX_COMPONENT_FLAG_NORMAL |
VERTEX_COMPONENT_FLAG_TEXCOORD
};
DEFINE_FLAG_ENUM_OPERATORS(VERTEX_COMPONENT_FLAGS);

RefCntAutoPtr<IBuffer> CreateVertexBuffer(IRenderDevice* pDevice,
VERTEX_COMPONENT_FLAGS Components,
BIND_FLAGS BindFlags = BIND_VERTEX_BUFFER,
BUFFER_MODE Mode = BUFFER_MODE_UNDEFINED);
RefCntAutoPtr<IBuffer> CreateVertexBuffer(IRenderDevice* pDevice,
GEOMETRY_PRIMITIVE_VERTEX_FLAGS Components,
BIND_FLAGS BindFlags = BIND_VERTEX_BUFFER,
BUFFER_MODE Mode = BUFFER_MODE_UNDEFINED);
RefCntAutoPtr<IBuffer> CreateIndexBuffer(IRenderDevice* pDevice,
BIND_FLAGS BindFlags = BIND_INDEX_BUFFER,
BUFFER_MODE Mode = BUFFER_MODE_UNDEFINED);
Expand All @@ -75,22 +58,13 @@ struct CreatePSOInfo
IShaderSourceInputStreamFactory* pShaderSourceFactory = nullptr;
const char* VSFilePath = nullptr;
const char* PSFilePath = nullptr;
VERTEX_COMPONENT_FLAGS Components = VERTEX_COMPONENT_FLAG_NONE;
GEOMETRY_PRIMITIVE_VERTEX_FLAGS Components = GEOMETRY_PRIMITIVE_VERTEX_FLAG_NONE;
LayoutElement* ExtraLayoutElements = nullptr;
Uint32 NumExtraLayoutElements = 0;
Uint8 SampleCount = 1;
};
RefCntAutoPtr<IPipelineState> CreatePipelineState(const CreatePSOInfo& CreateInfo, bool ConvertPSOutputToGamma = false);

static constexpr Uint32 NumVertices = 4 * 6;
static constexpr Uint32 NumIndices = 3 * 2 * 6;

extern const std::array<float3, NumVertices> Positions;
extern const std::array<float2, NumVertices> Texcoords;
extern const std::array<float3, NumVertices> Normals;

extern const std::array<Uint32, NumIndices> Indices;

} // namespace TexturedCube

} // namespace Diligent
1 change: 0 additions & 1 deletion Tutorials/Tutorial03_Texturing-C/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,3 @@ set(ASSETS
)

add_sample_app("Tutorial03_Texturing-C" "DiligentSamples/Tutorials" "${SOURCE}" "${INCLUDE}" "${SHADERS}" "${ASSETS}")
target_link_libraries(Tutorial03_Texturing-C PRIVATE Diligent-TextureLoader)
1 change: 0 additions & 1 deletion Tutorials/Tutorial03_Texturing/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,3 @@ set(ASSETS
)

add_sample_app("Tutorial03_Texturing" "DiligentSamples/Tutorials" "${SOURCE}" "${INCLUDE}" "${SHADERS}" "${ASSETS}")
target_link_libraries(Tutorial03_Texturing PRIVATE Diligent-TextureLoader)
1 change: 0 additions & 1 deletion Tutorials/Tutorial04_Instancing/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,3 @@ set(ASSETS
)

add_sample_app("Tutorial04_Instancing" "DiligentSamples/Tutorials" "${SOURCE}" "${INCLUDE}" "${SHADERS}" "${ASSETS}")
target_link_libraries(Tutorial04_Instancing PRIVATE Diligent-TextureLoader)
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ void Tutorial04_Instancing::Initialize(const SampleInitInfo& InitInfo)
CreatePipelineState();

// Load textured cube
m_CubeVertexBuffer = TexturedCube::CreateVertexBuffer(m_pDevice, TexturedCube::VERTEX_COMPONENT_FLAG_POS_UV);
m_CubeVertexBuffer = TexturedCube::CreateVertexBuffer(m_pDevice, GEOMETRY_PRIMITIVE_VERTEX_FLAG_POS_TEX);
m_CubeIndexBuffer = TexturedCube::CreateIndexBuffer(m_pDevice);
m_TextureSRV = TexturedCube::LoadTexture(m_pDevice, "DGLogo.png")->GetDefaultView(TEXTURE_VIEW_SHADER_RESOURCE);
// Set cube texture SRV in the SRB
Expand Down
1 change: 0 additions & 1 deletion Tutorials/Tutorial05_TextureArray/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,3 @@ set(ASSETS
)

add_sample_app("Tutorial05_TextureArray" "DiligentSamples/Tutorials" "${SOURCE}" "${INCLUDE}" "${SHADERS}" "${ASSETS}")
target_link_libraries(Tutorial05_TextureArray PRIVATE Diligent-TextureLoader)
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ void Tutorial05_TextureArray::Initialize(const SampleInitInfo& InitInfo)
CreatePipelineState();

// Load cube vertex and index buffers
m_CubeVertexBuffer = TexturedCube::CreateVertexBuffer(m_pDevice, TexturedCube::VERTEX_COMPONENT_FLAG_POS_UV);
m_CubeVertexBuffer = TexturedCube::CreateVertexBuffer(m_pDevice, GEOMETRY_PRIMITIVE_VERTEX_FLAG_POS_TEX);
m_CubeIndexBuffer = TexturedCube::CreateIndexBuffer(m_pDevice);

CreateInstanceBuffer();
Expand Down
3 changes: 1 addition & 2 deletions Tutorials/Tutorial06_Multithreading/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ set(ASSETS
)

add_sample_app("Tutorial06_Multithreading" "DiligentSamples/Tutorials" "${SOURCE}" "${INCLUDE}" "${SHADERS}" "${ASSETS}")
target_link_libraries(Tutorial06_Multithreading PRIVATE Diligent-TextureLoader)
if(PLATFORM_LINUX)
target_link_libraries(Tutorial06_Multithreading PRIVATE pthread)
endif()
endif()
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ void Tutorial06_Multithreading::CreatePipelineState(std::vector<StateTransitionD
CubePsoCI.pShaderSourceFactory = pShaderSourceFactory;
CubePsoCI.VSFilePath = "cube.vsh";
CubePsoCI.PSFilePath = "cube.psh";
CubePsoCI.Components = TexturedCube::VERTEX_COMPONENT_FLAG_POS_UV;
CubePsoCI.Components = GEOMETRY_PRIMITIVE_VERTEX_FLAG_POS_TEX;

m_pPSO = TexturedCube::CreatePipelineState(CubePsoCI, m_ConvertPSOutputToGamma);

Expand Down Expand Up @@ -164,7 +164,7 @@ void Tutorial06_Multithreading::Initialize(const SampleInitInfo& InitInfo)
CreatePipelineState(Barriers);

// Load textured cube
m_CubeVertexBuffer = TexturedCube::CreateVertexBuffer(m_pDevice, TexturedCube::VERTEX_COMPONENT_FLAG_POS_UV);
m_CubeVertexBuffer = TexturedCube::CreateVertexBuffer(m_pDevice, GEOMETRY_PRIMITIVE_VERTEX_FLAG_POS_TEX);
m_CubeIndexBuffer = TexturedCube::CreateIndexBuffer(m_pDevice);
// Explicitly transition vertex and index buffers to required states
Barriers.emplace_back(m_CubeVertexBuffer, RESOURCE_STATE_UNKNOWN, RESOURCE_STATE_VERTEX_BUFFER, STATE_TRANSITION_FLAG_UPDATE_STATE);
Expand Down
1 change: 0 additions & 1 deletion Tutorials/Tutorial07_GeometryShader/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,3 @@ set(ASSETS
)

add_sample_app("Tutorial07_GeometryShader" "DiligentSamples/Tutorials" "${SOURCE}" "${INCLUDE}" "${SHADERS}" "${ASSETS}")
target_link_libraries(Tutorial07_GeometryShader PRIVATE Diligent-TextureLoader)
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ void Tutorial07_GeometryShader::Initialize(const SampleInitInfo& InitInfo)
CreatePipelineState();

// Load textured cube
m_CubeVertexBuffer = TexturedCube::CreateVertexBuffer(m_pDevice, TexturedCube::VERTEX_COMPONENT_FLAG_POS_UV);
m_CubeVertexBuffer = TexturedCube::CreateVertexBuffer(m_pDevice, GEOMETRY_PRIMITIVE_VERTEX_FLAG_POS_TEX);
m_CubeIndexBuffer = TexturedCube::CreateIndexBuffer(m_pDevice);
m_TextureSRV = TexturedCube::LoadTexture(m_pDevice, "DGLogo.png")->GetDefaultView(TEXTURE_VIEW_SHADER_RESOURCE);
m_SRB->GetVariableByName(SHADER_TYPE_PIXEL, "g_Texture")->Set(m_TextureSRV);
Expand Down
1 change: 0 additions & 1 deletion Tutorials/Tutorial08_Tessellation/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,3 @@ set(ASSETS
)

add_sample_app("Tutorial08_Tessellation" "DiligentSamples/Tutorials" "${SOURCE}" "${INCLUDE}" "${SHADERS}" "${ASSETS}")
target_link_libraries(Tutorial08_Tessellation PRIVATE Diligent-TextureLoader)
3 changes: 1 addition & 2 deletions Tutorials/Tutorial09_Quads/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ set(ASSETS
)

add_sample_app("Tutorial09_Quads" "DiligentSamples/Tutorials" "${SOURCE}" "${INCLUDE}" "${SHADERS}" "${ASSETS}")
target_link_libraries(Tutorial09_Quads PRIVATE Diligent-TextureLoader)
if(PLATFORM_LINUX)
target_link_libraries(Tutorial09_Quads PRIVATE pthread)
endif()
endif()
3 changes: 1 addition & 2 deletions Tutorials/Tutorial10_DataStreaming/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ set(ASSETS
)

add_sample_app("Tutorial10_DataStreaming" "DiligentSamples/Tutorials" "${SOURCE}" "${INCLUDE}" "${SHADERS}" "${ASSETS}")
target_link_libraries(Tutorial10_DataStreaming PRIVATE Diligent-TextureLoader)
if(PLATFORM_LINUX)
target_link_libraries(Tutorial10_DataStreaming PRIVATE pthread)
endif()
endif()
1 change: 0 additions & 1 deletion Tutorials/Tutorial11_ResourceUpdates/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,3 @@ set(ASSETS
)

add_sample_app("Tutorial11_ResourceUpdates" "DiligentSamples/Tutorials" "${SOURCE}" "${INCLUDE}" "${SHADERS}" "${ASSETS}")
target_link_libraries(Tutorial11_ResourceUpdates PRIVATE Diligent-TextureLoader)
Loading

0 comments on commit 3e9833f

Please sign in to comment.