Skip to content

Commit

Permalink
Implemented single vertex buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
krupitskas committed Nov 20, 2024
1 parent 2424a07 commit 0802051
Show file tree
Hide file tree
Showing 5 changed files with 187 additions and 14 deletions.
7 changes: 3 additions & 4 deletions Yasno/Graphics/Primitive.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ namespace ysn
{
std::string name;

uint32_t vertex_count = 0;
//uint64_t vertex_offset_in_bytes = 0;

DXGI_FORMAT format;
D3D12_VERTEX_BUFFER_VIEW vertex_buffer_view;
};
Expand All @@ -28,10 +25,12 @@ namespace ysn
PsoId shadow_pso_id = -1;

D3D_PRIMITIVE_TOPOLOGY topology;

D3D12_INDEX_BUFFER_VIEW index_buffer_view;
D3D12_VERTEX_BUFFER_VIEW vertex_buffer_view;

uint32_t index_count = 0;
//uint64_t index_offset_in_bytes = 0;
uint32_t vertex_count = 0;

int material_id = -1;

Expand Down
2 changes: 2 additions & 0 deletions Yasno/Graphics/RenderScene.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ namespace ysn

// Temp GPU resources for indices and vertices
uint32_t indices_count = 0;
uint32_t vertices_count = 0;
GpuBuffer indices_buffer;
GpuBuffer vertices_buffer;
};
}

2 changes: 1 addition & 1 deletion Yasno/Renderer/RaytracingContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ void ysn::RaytracingContext::CreateAccelerationStructures(wil::com_ptr<ID3D12Gra

const Attribute& attribute = primitive.attributes.at("POSITION");

blas_input.vertex_count = attribute.vertex_count;
blas_input.vertex_count = primitive.vertex_count;
blas_input.index_count = primitive.index_count;

blas_input.vertex_buffer_view = attribute.vertex_buffer_view;
Expand Down
142 changes: 133 additions & 9 deletions Yasno/System/GltfLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <memory>
#include <unordered_set>
#include <algorithm>
#include <map>

#include <d3d12.h>
#include <wrl.h>
Expand Down Expand Up @@ -30,6 +31,7 @@ struct LoadGltfContext
struct BuildMeshResult
{
uint32_t mesh_indices_count = 0;
uint32_t mesh_vertices_count = 0;
};

static bool BuildBuffers(ysn::Model& model, LoadGltfContext& build_context, const tinygltf::Model& gltf_model)
Expand Down Expand Up @@ -592,20 +594,140 @@ static void BuildAttributesAccessors(ysn::Primitive& primitive,
vertex_buffer_view.StrideInBytes = gltf_accessor.ByteStride(gltf_buffer_view);

render_attribute.vertex_buffer_view = vertex_buffer_view;
render_attribute.vertex_count = static_cast<uint32_t>(gltf_accessor.count);

//if (gltf_attribute_name == "POSITION")
//{
// primitive->vertex_count = = static_cast<uint32_t>(gltf_accessor.count);;
// primitive->vertex_buffer = pGltfBuffers[gltf_buffer_view.buffer];
// primitive->vertex_stride = render_attribute.vertexBufferView.StrideInBytes;
// primitive->vertex_offset_in_bytes = gltf_buffer_view.byteOffset + gltf_accessor.byteOffset;
//}

primitive.attributes.emplace(gltf_attribute_name, render_attribute);
}
}

static uint32_t BuildVertexBuffer(ysn::Primitive& primitive, const tinygltf::Primitive& gltf_primitive, const tinygltf::Model& gltf_model)
{
int position_index = -1;
int normal_index = -1;
int tangent_index = -1;
int uv0_index = -1;

if (gltf_primitive.attributes.contains("POSITION"))
{
position_index = gltf_primitive.attributes.at("POSITION");
}

if (gltf_primitive.attributes.contains("NORMAL"))
{
normal_index = gltf_primitive.attributes.at("NORMAL");
}

if (gltf_primitive.attributes.contains("TANGENT"))
{
tangent_index = gltf_primitive.attributes.at("TANGENT");
}

if (gltf_primitive.attributes.contains("TEXCOORD_0"))
{
uv0_index = gltf_primitive.attributes.at("TEXCOORD_0");
}

// AABB
if (position_index > -1)
{
const std::vector<double>& min = gltf_model.accessors[position_index].minValues;
const std::vector<double>& max = gltf_model.accessors[position_index].maxValues;

primitive.bbox.min = { (float)min[0], (float)min[1], (float)min[2] };
primitive.bbox.max = { (float)max[0], (float)max[1], (float)max[2] };
}

// Vertex positions
const tinygltf::Accessor& position_accessor = gltf_model.accessors[position_index];
const tinygltf::BufferView& position_buffer_view = gltf_model.bufferViews[position_accessor.bufferView];
const tinygltf::Buffer& position_buffer = gltf_model.buffers[position_buffer_view.buffer];
const UINT8* position_buffer_address = position_buffer.data.data();
const int position_stride = tinygltf::GetComponentSizeInBytes(position_accessor.componentType) * tinygltf::GetNumComponentsInType(position_accessor.type);
YSN_ASSERT_MSG(position_stride == 12, "GLTF model vertex position stride not equals 12");

// Vertex normals
tinygltf::Accessor normal_accessor;
tinygltf::BufferView normal_buffer_view;
const UINT8* normal_buffer_address = nullptr;
int normal_stride = -1;
if (normal_index > -1)
{
normal_accessor = gltf_model.accessors[normal_index];
normal_buffer_view = gltf_model.bufferViews[normal_accessor.bufferView];
const tinygltf::Buffer& normal_buffer = gltf_model.buffers[normal_buffer_view.buffer];
normal_buffer_address = normal_buffer.data.data();
normal_stride = tinygltf::GetComponentSizeInBytes(normal_accessor.componentType) * tinygltf::GetNumComponentsInType(normal_accessor.type);
YSN_ASSERT_MSG(normal_stride == 12, "GLTF model vertex normal stride not equals 12");
}

// Vertex tangents
tinygltf::Accessor tangent_accessor;
tinygltf::BufferView tangent_buffer_view;
const UINT8* tangent_buffer_address = nullptr;
int tangent_stride = -1;
if (tangent_index > -1)
{
tangent_accessor = gltf_model.accessors[tangent_index];
tangent_buffer_view = gltf_model.bufferViews[tangent_accessor.bufferView];
const tinygltf::Buffer& tangent_buffer = gltf_model.buffers[tangent_buffer_view.buffer];
tangent_buffer_address = tangent_buffer.data.data();
tangent_stride = tinygltf::GetComponentSizeInBytes(tangent_accessor.componentType) * tinygltf::GetNumComponentsInType(tangent_accessor.type);
YSN_ASSERT_MSG(tangent_stride == 16, "GLTF model vertex tangent stride not equals 12");
}

// Vertex texture coordinates
tinygltf::Accessor uv0_accessor;
tinygltf::BufferView uv0_buffer_view;
const UINT8* uv0_buffer_address = nullptr;
int uv0_stride = -1;
if (uv0_index > -1)
{
uv0_accessor = gltf_model.accessors[uv0_index];
uv0_buffer_view = gltf_model.bufferViews[uv0_accessor.bufferView];
const tinygltf::Buffer& uv0Buffer = gltf_model.buffers[uv0_buffer_view.buffer];
uv0_buffer_address = uv0Buffer.data.data();
uv0_stride = tinygltf::GetComponentSizeInBytes(uv0_accessor.componentType) * tinygltf::GetNumComponentsInType(uv0_accessor.type);
YSN_ASSERT_MSG(uv0_stride == 8, "GLTF model vertex uv0 stride not equals 12");
}

primitive.vertices.reserve(position_accessor.count);

// Get the vertex data
for (size_t vertex_index = 0; vertex_index < position_accessor.count; vertex_index++)
{
ysn::Vertex v;

// position
{
const UINT8* address = position_buffer_address + position_buffer_view.byteOffset + position_accessor.byteOffset + (vertex_index * position_stride);
memcpy(&v.position, address, position_stride);
}

if (normal_index > -1)
{
const UINT8* address = normal_buffer_address + normal_buffer_view.byteOffset + normal_accessor.byteOffset + (vertex_index * normal_stride);
memcpy(&v.normal, address, normal_stride);
}

if (tangent_index > -1)
{
const UINT8* address = tangent_buffer_address + tangent_buffer_view.byteOffset + tangent_accessor.byteOffset + (vertex_index * tangent_stride);
memcpy(&v.tangent, address, tangent_stride);
}

if (uv0_index > -1)
{
const UINT8* address = uv0_buffer_address + uv0_buffer_view.byteOffset + uv0_accessor.byteOffset + (vertex_index * uv0_stride);
memcpy(&v.uv0, address, uv0_stride);

// TODO: Add UV adj
}

primitive.vertices.push_back(v);
}

return position_accessor.count;
}

static BuildMeshResult BuildMeshes(ysn::Model& model, const tinygltf::Model& gltf_model)
{
BuildMeshResult result;
Expand All @@ -619,6 +741,7 @@ static BuildMeshResult BuildMeshes(ysn::Model& model, const tinygltf::Model& glt
{
ysn::Primitive primitive;

result.mesh_vertices_count += BuildVertexBuffer(primitive, gltf_primitive, gltf_model);
BuildAttributesAccessors(primitive, model, gltf_model, gltf_primitive.attributes);
result.mesh_indices_count += BuildIndexBuffer(primitive, model, gltf_primitive.indices, gltf_model);
BuildPrimitiveTopology(primitive, gltf_primitive.mode);
Expand Down Expand Up @@ -951,6 +1074,7 @@ namespace ysn

// Write stats
render_scene.indices_count += mesh_result.mesh_indices_count;
render_scene.vertices_count += mesh_result.mesh_vertices_count;

return true;
}
Expand Down
48 changes: 48 additions & 0 deletions Yasno/Yasno/Yasno.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,54 @@ namespace ysn

UploadToGpuBuffer(command_list, m_render_scene.indices_buffer, all_indices_buffer.data(), {}, D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE);
}

// Vertex Buffer
{
const uint32_t vertices_buffer_size = m_render_scene.vertices_count * sizeof(Vertex);

GpuBufferCreateInfo create_info {
.size = vertices_buffer_size,
.heap_type = D3D12_HEAP_TYPE_DEFAULT,
.state = D3D12_RESOURCE_STATE_COPY_DEST
};

const auto vertices_buffer_result = CreateGpuBuffer(create_info, "Vertices Buffer");

if (!vertices_buffer_result.has_value())
{
LogFatal << "Can't create vertices buffer\n";
return false;
}

m_render_scene.vertices_buffer = vertices_buffer_result.value();

std::vector<Vertex> all_vertices_buffer;
all_vertices_buffer.reserve(m_render_scene.vertices_count);

for (auto& model : m_render_scene.models)
{
for (auto& mesh : model.meshes)
{
for (auto& primitive : mesh.primitives)
{
primitive.vertex_buffer_view.BufferLocation = m_render_scene.vertices_buffer.GetGPUVirtualAddress() + all_vertices_buffer.size() * sizeof(Vertex);
primitive.vertex_buffer_view.SizeInBytes = primitive.vertices.size() * sizeof(Vertex);
primitive.vertex_buffer_view.StrideInBytes = sizeof(Vertex);

primitive.vertex_count = primitive.vertices.size();

// Append vertices
all_vertices_buffer.insert(all_vertices_buffer.end(), primitive.vertices.begin(), primitive.vertices.end());
}
}
}

UploadToGpuBuffer(command_list, m_render_scene.vertices_buffer, all_vertices_buffer.data(), {}, D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE);
}

// Material buffer
{
}
}

for (auto& model : m_render_scene.models)
Expand Down

0 comments on commit 0802051

Please sign in to comment.