Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Buffer #3

Merged
merged 2 commits into from
May 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ OPTION(STAGE_LOGGING_WARN OFF)
OPTION(STAGE_LOGGING_LOG OFF)
OPTION(STAGE_LOGGING_OFF OFF)

OPTION(STAGE_API_USAGE_OPENGL OFF)
OPTION(STAGE_API_USAGE_METAL OFF)

add_subdirectory(dependencies)
add_subdirectory(src)

Expand Down
4 changes: 0 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,6 @@ cmake --build build -j
### Build Options
The CMake build offers several options to customize the build:

**Data Alignment**
* `STAGE_API_USAGE_OPENGL` - Stage will try to align types to 16 bytes
* `STAGE_API_USAGE_METAL` - Stage will perform no explicit data alignment

**Logging**
* `STAGE_LOGGING_WARN` - Turn on warning log messages.
* `STAGE_LOGGING_LOG` - Turn on all log messages.
Expand Down
8 changes: 0 additions & 8 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,6 @@ if (STAGE_LOGGING_OFF)
set (STAGE_COMPILE_DEFINITIONS ${STAGE_COMPILE_DEFINITIONS} STAGE_LOGGING_OFF)
endif()

if (STAGE_API_USAGE_OPENGL)
set (STAGE_COMPILE_DEFINITIONS ${STAGE_COMPILE_DEFINITIONS} STAGE_API_USAGE_OPENGL)
endif()
if (STAGE_API_USAGE_METAL)
set (STAGE_COMPILE_DEFINITIONS ${STAGE_COMPILE_DEFINITIONS} STAGE_API_USAGE_METAL)
endif()

target_compile_definitions(stage PUBLIC ${STAGE_COMPILE_DEFINITIONS})

install(TARGETS stage EXPORT stageConfig
Expand All @@ -60,7 +53,6 @@ install(FILES
DESTINATION include/stage)

install(FILES
backstage/alignment.h
backstage/camera.h
backstage/image.h
backstage/light.h
Expand Down
7 changes: 0 additions & 7 deletions src/backstage/alignment.h

This file was deleted.

9 changes: 6 additions & 3 deletions src/backstage/buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,23 @@ template<typename T>
struct BufferView {

BufferView() = default;
BufferView(std::shared_ptr<Buffer> source, size_t offset, size_t num_elements, size_t stride = sizeof(T)) : m_offset(offset), m_size(num_elements), m_stride(stride) {
BufferView(std::shared_ptr<Buffer> source, size_t offset, size_t num_elements, size_t stride = sizeof(T), size_t alignment = alignof(T)) : m_offset(offset), m_size(num_elements), m_stride(stride), m_alignment(alignment) {
m_buffer = source;
}

void setBuffer(std::shared_ptr<Buffer> source) {
m_offset = source->size();
m_size = 0;
m_stride = sizeof(T);
m_alignment = alignof(T);
m_buffer = source;
}

void setBuffer(std::shared_ptr<Buffer> source, size_t offset, size_t num_elements, size_t stride = sizeof(T)) {
void setBuffer(std::shared_ptr<Buffer> source, size_t offset, size_t num_elements, size_t stride = sizeof(T), size_t alignment = alignof(T)) {
m_offset = source->size() + offset;
m_size = num_elements;
m_stride = stride;
m_alignment = alignment;
m_buffer = source;
}

Expand Down Expand Up @@ -90,7 +92,7 @@ struct BufferView {
m_size += elements.size();
}

size_t sizeInBytes() const { return m_size * sizeof(T); }
size_t sizeInBytes() const { return m_size * (sizeof(T) + (sizeof(T) % m_alignment)); }
size_t size() const { return m_size; }
size_t offset() const { return m_offset; }
size_t stride() const { return m_stride; }
Expand All @@ -103,6 +105,7 @@ struct BufferView {
std::shared_ptr<Buffer> m_buffer;
size_t m_offset;
size_t m_stride;
size_t m_alignment;
size_t m_size { 0 };

size_t positionInBuffer() {
Expand Down
9 changes: 4 additions & 5 deletions src/backstage/camera.h
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
#pragma once

#include "alignment.h"
#include "math.h"

namespace stage {
namespace backstage {

struct DEVICE_ALIGNED Camera {
DEVICE_ALIGNED stage_vec3f position;
DEVICE_ALIGNED stage_vec3f lookat;
DEVICE_ALIGNED stage_vec3f up;
struct Camera {
stage_vec3f position;
stage_vec3f lookat;
stage_vec3f up;
float fovy;
};

Expand Down
3 changes: 2 additions & 1 deletion src/backstage/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ namespace stage {
namespace backstage {

struct Config {
VertexLayout layout { VertexLayout_Interleaved_VNT };
VertexLayout layout { VertexLayout_Interleaved_VNT };
size_t vertex_alignment { 16 };
};

}
Expand Down
9 changes: 4 additions & 5 deletions src/backstage/light.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#pragma once

#include <cstdint>
#include "alignment.h"
#include "math.h"

namespace stage {
Expand All @@ -15,10 +14,10 @@ namespace backstage {
#define SPHERE_LIGHT 3
#define DISK_LIGHT 4

struct DEVICE_ALIGNED Light {
DEVICE_ALIGNED stage_vec3f L;
DEVICE_ALIGNED stage_vec3f from;
DEVICE_ALIGNED stage_vec3f to;
struct Light {
stage_vec3f L;
stage_vec3f from;
stage_vec3f to;
float radius;
int32_t map_texid;
uint32_t type;
Expand Down
7 changes: 3 additions & 4 deletions src/backstage/material.h
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
#pragma once

#include "alignment.h"
#include "math.h"

namespace stage {
namespace backstage {

struct DEVICE_ALIGNED OpenPBRMaterial {
struct OpenPBRMaterial {

/* Base */
DEVICE_ALIGNED stage_vec3f base_color;
stage_vec3f base_color;
int32_t base_color_texid;
float base_weight;
float base_roughness;
float base_metalness;

/* Specular */
DEVICE_ALIGNED stage_vec3f specular_color;
stage_vec3f specular_color;
float specular_weight;
float specular_roughness;
float specular_anisotropy;
Expand Down
5 changes: 5 additions & 0 deletions src/backstage/math.h
Original file line number Diff line number Diff line change
Expand Up @@ -203,5 +203,10 @@ compMin(const stage_vec3<T>& v) {
return min < v.z ? min : v.z;
}

template<typename T> size_t
sizeofAligned(size_t alignment) {
return sizeof(T) + (sizeof(T) % alignment);
}

}
}
47 changes: 23 additions & 24 deletions src/backstage/mesh.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "mesh.h"
#include "log.h"

namespace stage {
namespace backstage {
Expand All @@ -17,57 +16,57 @@ Geometry::Geometry(Object& parent, std::vector<stage_vec3f> positions, std::vect
switch (layout)
{
case VertexLayout_Block_V:
stride_positions = sizeof(stage_vec3f);
stride_positions = sizeofAligned<stage_vec3f>(parent.alignment());
offset_positions = 0;
stride_material_ids = sizeof(uint32_t);
stride_material_ids = sizeofAligned<uint32_t>(parent.alignment());
offset_material_ids = positions.size() * stride_positions;
break;
case VertexLayout_Block_VN:
stride_positions = sizeof(stage_vec3f);
stride_positions = sizeofAligned<stage_vec3f>(parent.alignment());
offset_positions = 0;
stride_normals = sizeof(stage_vec3f);
stride_normals = sizeofAligned<stage_vec3f>(parent.alignment());
offset_normals = positions.size() * stride_positions;
stride_material_ids = sizeof(uint32_t);
stride_material_ids = sizeofAligned<uint32_t>(parent.alignment());
offset_material_ids = offset_normals + normals.size() * stride_normals;
break;
case VertexLayout_Block_VNT:
stride_positions = sizeof(stage_vec3f);
stride_positions = sizeofAligned<stage_vec3f>(parent.alignment());
offset_positions = 0;
stride_normals = sizeof(stage_vec3f);
stride_normals = sizeofAligned<stage_vec3f>(parent.alignment());
offset_normals = positions.size() * stride_positions;
stride_uvs = sizeof(stage_vec2f);
stride_uvs = sizeofAligned<stage_vec2f>(parent.alignment());
offset_uvs = offset_normals + normals.size() * stride_normals;
stride_material_ids = sizeof(uint32_t);
stride_material_ids = sizeofAligned<uint32_t>(parent.alignment());
offset_material_ids = offset_uvs + uvs.size() * stride_uvs;
break;
case VertexLayout_Interleaved_V:
offset_positions = 0;
offset_material_ids = sizeof(stage_vec3f);
stride_positions = stride_material_ids = sizeof(stage_vec3f) + sizeof(uint32_t);
offset_material_ids = sizeofAligned<stage_vec3f>(parent.alignment());
stride_positions = stride_material_ids = sizeofAligned<stage_vec3f>(parent.alignment()) + sizeofAligned<uint32_t>(parent.alignment());
break;
case VertexLayout_Interleaved_VN:
offset_positions = 0;
offset_normals = sizeof(stage_vec3f);
offset_material_ids = offset_normals + sizeof(stage_vec3f);
stride_positions = stride_normals = stride_material_ids = 2 * sizeof(stage_vec3f) + sizeof(uint32_t);
offset_normals = sizeofAligned<stage_vec3f>(parent.alignment());
offset_material_ids = offset_normals + sizeofAligned<stage_vec3f>(parent.alignment());
stride_positions = stride_normals = stride_material_ids = 2 * sizeofAligned<stage_vec3f>(parent.alignment()) + sizeofAligned<uint32_t>(parent.alignment());
break;
case VertexLayout_Interleaved_VNT:
offset_positions = 0;
offset_normals = sizeof(stage_vec3f);
offset_uvs = offset_normals + sizeof(stage_vec3f);
offset_material_ids = offset_uvs + sizeof(stage_vec2f);
stride_positions = stride_normals = stride_uvs = stride_material_ids = 2 * sizeof(stage_vec3f) + sizeof(stage_vec2f) + sizeof(uint32_t);
offset_normals = sizeofAligned<stage_vec3f>(parent.alignment());
offset_uvs = offset_normals + sizeofAligned<stage_vec3f>(parent.alignment());
offset_material_ids = offset_uvs + sizeofAligned<stage_vec2f>(parent.alignment());
stride_positions = stride_normals = stride_uvs = stride_material_ids = 2 * sizeofAligned<stage_vec3f>(parent.alignment()) + sizeofAligned<stage_vec2f>(parent.alignment()) + sizeofAligned<uint32_t>(parent.alignment());
break;
default:
break;
}

this->positions.setBuffer(parent.data, offset_positions, 0, stride_positions);
this->positions.setBuffer(parent.data, offset_positions, 0, stride_positions, parent.alignment());
if (layout & (VertexLayout_Block_VN | VertexLayout_Interleaved_VN | VertexLayout_Block_VNT | VertexLayout_Interleaved_VNT))
this->normals.setBuffer(parent.data, offset_normals, 0, stride_normals);
this->normals.setBuffer(parent.data, offset_normals, 0, stride_normals, parent.alignment());
if (layout & (VertexLayout_Block_VNT | VertexLayout_Interleaved_VNT))
this->uvs.setBuffer(parent.data, offset_uvs, 0, stride_uvs);
this->material_ids.setBuffer(parent.data, offset_material_ids, 0, stride_material_ids);
this->uvs.setBuffer(parent.data, offset_uvs, 0, stride_uvs, parent.alignment());
this->material_ids.setBuffer(parent.data, offset_material_ids, 0, stride_material_ids, parent.alignment());

this->positions.push_back(positions);
if (layout & (VertexLayout_Block_VN | VertexLayout_Interleaved_VN | VertexLayout_Block_VNT | VertexLayout_Interleaved_VNT))
Expand All @@ -77,6 +76,6 @@ Geometry::Geometry(Object& parent, std::vector<stage_vec3f> positions, std::vect
this->material_ids.push_back(material_ids);
}

Object::Object(VertexLayout layout) : m_layout(layout), data(std::make_shared<Buffer>()) {}
Object::Object(VertexLayout layout, size_t alignment) : m_layout(layout), m_alignment(alignment), data(std::make_shared<Buffer>()) {}
}
}
12 changes: 3 additions & 9 deletions src/backstage/mesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include <cstdint>
#include <vector>
#include "math.h"
#include "alignment.h"
#include "buffer.h"

namespace stage {
Expand All @@ -18,13 +17,6 @@ enum VertexLayout {
VertexLayout_Block_V = 0x020,
};

struct DEVICE_ALIGNED AlignedVertex {
DEVICE_ALIGNED stage_vec3f position;
DEVICE_ALIGNED stage_vec3f normal;
DEVICE_ALIGNED stage_vec2f uv;
uint32_t material_id;
};

struct Object;
struct Geometry {
Geometry(Object& parent, std::vector<stage_vec3f> positions, std::vector<stage_vec3f> normals, std::vector<stage_vec2f> uvs, std::vector<uint32_t> material_ids, std::vector<uint32_t> indices);
Expand All @@ -37,13 +29,15 @@ struct Geometry {
};

struct Object {
Object(VertexLayout layout);
Object(VertexLayout layout, size_t alignment);
std::shared_ptr<Buffer> data;
std::vector<Geometry> geometries;

VertexLayout layout() { return m_layout; }
size_t alignment() { return m_alignment; }
private:
VertexLayout m_layout;
size_t m_alignment;
};

struct ObjectInstance {
Expand Down
Loading