Created
February 28, 2022 19:13
-
-
Save Journeyman1337/a31fd75ab2042b6d5e765b743bf1a033 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Copyright (c) 2022 Daniel Valcour | |
Permission is hereby granted, free of charge, to any person obtaining a copy of | |
this software and associated documentation files (the "Software"), to deal in | |
the Software without restriction, including without limitation the rights to | |
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | |
the Software, and to permit persons to whom the Software is furnished to do so, | |
subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | |
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | |
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | |
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | |
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
*/ | |
#include <j2d/graphics/VertexLayout.hpp> | |
#include <span> | |
const std::span<j2d::BufferLayout::Element>& j2d::BufferLayout::View::GetElements() const noexcept | |
{ | |
return this->elements; | |
} | |
j2d::BufferLayout::Library::Library(const std::initializer_list<BufferLayout>& layouts) noexcept | |
{ | |
for (auto& layout : layouts) | |
{ | |
this->layout_map.insert(std::make_pair(std::string_view(layout.name), std::move(layout))); | |
} | |
} | |
void j2d::BufferLayout::Library::Add(const BufferLayout& layout) | |
{ | |
this->layout_map.insert(std::make_pair(std::string_view(layout.name), std::move(layout))); | |
} | |
const std::unordered_map<std::string_view, j2d::BufferLayout>& j2d::BufferLayout::Library::GetMap() noexcept | |
{ | |
return this->layout_map; | |
} | |
std::optional<j2d::BufferLayout::View> j2d::BufferLayout::Library::operator[](std::string_view name) | |
{ | |
auto layout_it = this->layout_map.find(name); | |
if (layout_it == this->layout_map.end()) | |
{ | |
return std::nullopt; | |
} | |
return layout_it->second; | |
} | |
std::string_view j2d::BufferLayout::GetName() const noexcept | |
{ | |
return std::string_view(this->name); | |
} | |
const std::vector<j2d::BufferLayout::Element>& j2d::BufferLayout::GetElements() const noexcept | |
{ | |
return this->elements; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Copyright (c) 2022 Daniel Valcour | |
Permission is hereby granted, free of charge, to any person obtaining a copy of | |
this software and associated documentation files (the "Software"), to deal in | |
the Software without restriction, including without limitation the rights to | |
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | |
the Software, and to permit persons to whom the Software is furnished to do so, | |
subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | |
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | |
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | |
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | |
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
*/ | |
#pragma once | |
#include <glm/glm.hpp> | |
#include <type_traits> | |
#include <cstdint> | |
#include <cstddef> | |
#include <vector> | |
#include <span> | |
#include <unordered_map> | |
#include <string> | |
#include <initializer_list> | |
#include <optional> | |
namespace j2d | |
{ | |
class BufferLayout | |
{ | |
public: | |
struct Element | |
{ | |
enum class Type | |
{ | |
None = 0, | |
Half, | |
Half2, | |
Half3, | |
Half4, | |
HMat3, | |
HMat4, | |
Float, | |
Float2, | |
Float3, | |
Float4, | |
Mat3, | |
Mat4, | |
Double, | |
Double2, | |
Double3, | |
Double4, | |
DMat3, | |
DMat4, | |
Byte, | |
Byte2, | |
Byte3, | |
Byte4, | |
SByte, | |
SByte2, | |
SByte3, | |
SByte4, | |
Short, | |
Short1, | |
Short2, | |
Short3, | |
Short4, | |
UShort, | |
UShort1, | |
UShort2, | |
UShort3, | |
UShort4, | |
Int, | |
Int2, | |
Int3, | |
Int4, | |
Uint, | |
Uint2, | |
Uint3, | |
Uint4 | |
}; | |
static constexpr std::size_t GetComponentCount(const Type type) noexcept | |
{ | |
switch (type) | |
{ | |
case Type::None: return 0; | |
case Type::Half: return 1; | |
case Type::Half2: return 2; | |
case Type::Half3: return 3; | |
case Type::Half4: return 4; | |
case Type::HMat3: return 9; | |
case Type::HMat4: return 16; | |
case Type::Float: return 1; | |
case Type::Float2: return 2; | |
case Type::Float3: return 3; | |
case Type::Float4: return 4; | |
case Type::Mat3: return 9; | |
case Type::Mat4: return 16; | |
case Type::Double: return 1; | |
case Type::Double2: return 2; | |
case Type::Double3: return 3; | |
case Type::Double4: return 4; | |
case Type::DMat3: return 9; | |
case Type::DMat4: return 16; | |
case Type::Byte: return 1; | |
case Type::Byte2: return 2; | |
case Type::Byte3: return 3; | |
case Type::Byte4: return 4; | |
case Type::SByte: return 1; | |
case Type::SByte2: return 2; | |
case Type::SByte3: return 3; | |
case Type::SByte4: return 4; | |
case Type::Short: return 1; | |
case Type::Short2: return 2; | |
case Type::Short3: return 3; | |
case Type::Short4: return 4; | |
case Type::UShort: return 1; | |
case Type::UShort2: return 2; | |
case Type::UShort3: return 3; | |
case Type::UShort4: return 4; | |
case Type::Int: return 1; | |
case Type::Int2: return 2; | |
case Type::Int3: return 3; | |
case Type::Int4: return 4; | |
case Type::Uint: return 1; | |
case Type::Uint2: return 2; | |
case Type::Uint3: return 3; | |
case Type::Uint4: return 4; | |
default: return 0; | |
} | |
} | |
static constexpr std::size_t GetComponentSize(const Type type) noexcept | |
{ | |
switch (type) | |
{ | |
case Type::None: return 0; | |
case Type::Half: return sizeof(uint16_t); | |
case Type::Half2: return sizeof(uint16_t); | |
case Type::Half3: return sizeof(uint16_t); | |
case Type::Half4: return sizeof(uint16_t); | |
case Type::HMat3: return sizeof(uint16_t); | |
case Type::HMat4: return sizeof(uint16_t); | |
case Type::Float: return sizeof(float); | |
case Type::Float2: return sizeof(float); | |
case Type::Float3: return sizeof(float); | |
case Type::Float4: return sizeof(float); | |
case Type::Mat3: return sizeof(float);; | |
case Type::Mat4: return sizeof(float); | |
case Type::Double: return sizeof(double); | |
case Type::Double2: return sizeof(double); | |
case Type::Double3: return sizeof(double); | |
case Type::Double4: return sizeof(double); | |
case Type::DMat3: return sizeof(double); | |
case Type::DMat4: return sizeof(double); | |
case Type::Byte: return sizeof(uint8_t); | |
case Type::Byte2: return sizeof(uint8_t); | |
case Type::Byte3: return sizeof(uint8_t); | |
case Type::Byte4: return sizeof(uint8_t); | |
case Type::SByte: return sizeof(int8_t); | |
case Type::SByte2: return sizeof(int8_t); | |
case Type::SByte3: return sizeof(int8_t); | |
case Type::SByte4: return sizeof(int8_t); | |
case Type::Short: return sizeof(int16_t); | |
case Type::Short2: return sizeof(int16_t); | |
case Type::Short3: return sizeof(int16_t); | |
case Type::Short4: return sizeof(int16_t); | |
case Type::UShort: return sizeof(uint16_t); | |
case Type::UShort2: return sizeof(uint16_t); | |
case Type::UShort3: return sizeof(uint16_t); | |
case Type::UShort4: return sizeof(uint16_t); | |
case Type::Int: return sizeof(int32_t); | |
case Type::Int2: return sizeof(int32_t); | |
case Type::Int3: return sizeof(int32_t); | |
case Type::Int4: return sizeof(int32_t); | |
case Type::Uint: return sizeof(uint32_t); | |
case Type::Uint2: return sizeof(uint32_t); | |
case Type::Uint3: return sizeof(uint32_t); | |
case Type::Uint4: return sizeof(uint32_t); | |
default: return 0; | |
} | |
} | |
static constexpr std::size_t GetSize(const Type type) noexcept | |
{ | |
return GetComponentCount(type) * GetComponentSize(type); | |
} | |
Type type; | |
std::string name; | |
bool normalized; | |
constexpr std::size_t GetComponentCount() const noexcept | |
{ | |
return Element::GetComponentCount(this->type); | |
} | |
constexpr std::size_t GetComponentSize() const noexcept | |
{ | |
return Element::GetComponentSize(this->type); | |
} | |
constexpr std::size_t GetSize() const noexcept | |
{ | |
return Element::GetSize(this->type); | |
} | |
}; | |
private: | |
std::string name = ""; | |
std::vector<Element> elements = std::vector<Element>(); | |
public: | |
class View | |
{ | |
private: | |
std::string_view name = ""; | |
std::span<Element> elements = std::span<Element>(); | |
public: | |
constexpr View() noexcept = default; | |
constexpr View(j2d::BufferLayout& layout) noexcept | |
: name(layout.name) | |
, elements(layout.elements.begin(), layout.elements.size()) | |
{} | |
const std::span<Element>& GetElements() const noexcept; | |
}; | |
class Library | |
{ | |
private: | |
std::unordered_map<std::string_view, BufferLayout> layout_map = std::unordered_map<std::string_view, BufferLayout>(); | |
public: | |
constexpr Library() noexcept = default; | |
Library(const std::initializer_list<BufferLayout>& layouts) noexcept; | |
void Add(const BufferLayout& layout); | |
const std::unordered_map<std::string_view, BufferLayout>& GetMap() noexcept; | |
std::optional<BufferLayout::View> operator[](std::string_view name); | |
}; | |
public: | |
constexpr BufferLayout() noexcept = default; | |
constexpr BufferLayout(std::string_view name, std::initializer_list<Element> elements) noexcept | |
: name(name) | |
, elements(elements) | |
{} | |
constexpr BufferLayout(BufferLayout&& other) noexcept | |
: name(std::move(other.name)) | |
, elements(std::move(other.elements)) | |
{} | |
std::string_view GetName() const noexcept; | |
const std::vector<Element>& GetElements() const noexcept; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment