IndexBuffer
- IndexBuffer now creates it's own
      indices if non is provided
			
			
This commit is contained in:
		
							parent
							
								
									cd9747ccfe
								
							
						
					
					
						commit
						0510712d6e
					
				
					 7 changed files with 79 additions and 37 deletions
				
			
		| 
						 | 
				
			
			@ -11,15 +11,15 @@
 | 
			
		|||
namespace Light {
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
	VertexBuffer* VertexBuffer::Create(unsigned int stride, unsigned int count, float* vertices, void* sharedContext)
 | 
			
		||||
	VertexBuffer* VertexBuffer::Create(float* vertices, unsigned int stride, unsigned int count, void* sharedContext)
 | 
			
		||||
	{
 | 
			
		||||
		switch (GraphicsContext::GetGraphicsAPI())
 | 
			
		||||
		{
 | 
			
		||||
		case GraphicsAPI::OpenGL:
 | 
			
		||||
			return new glVertexBuffer(count, vertices);
 | 
			
		||||
			return new glVertexBuffer(vertices, count);
 | 
			
		||||
			
 | 
			
		||||
		case GraphicsAPI::DirectX:
 | 
			
		||||
			return new dxVertexBuffer(count, stride, vertices, sharedContext);
 | 
			
		||||
			return new dxVertexBuffer(vertices, stride, count, sharedContext);
 | 
			
		||||
 | 
			
		||||
		default:
 | 
			
		||||
			LT_ENGINE_ASSERT(false, "VertexBuffer::Create: invalid/unsupported GraphicsAPI {}", GraphicsContext::GetGraphicsAPI());
 | 
			
		||||
| 
						 | 
				
			
			@ -27,15 +27,16 @@ namespace Light {
 | 
			
		|||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	IndexBuffer* IndexBuffer::Create(unsigned int count, unsigned int* indices, void* sharedContext)
 | 
			
		||||
	IndexBuffer* IndexBuffer::Create(unsigned int* indices, unsigned int count, void* sharedContext)
 | 
			
		||||
	{
 | 
			
		||||
		switch (GraphicsContext::GetGraphicsAPI())
 | 
			
		||||
		{
 | 
			
		||||
		case GraphicsAPI::OpenGL:
 | 
			
		||||
			return new glIndexBuffer(count, indices);
 | 
			
		||||
			return new glIndexBuffer(indices, count);
 | 
			
		||||
 | 
			
		||||
		case GraphicsAPI::DirectX: LT_WIN(
 | 
			
		||||
			return new dxIndexBuffer(count, indices, sharedContext);
 | 
			
		||||
		)
 | 
			
		||||
			return new dxIndexBuffer(indices, count, sharedContext);)
 | 
			
		||||
 | 
			
		||||
		default:
 | 
			
		||||
			LT_ENGINE_ASSERT(false, "IndexBuffer::Create: invalid/unsupported GraphicsAPI {}", GraphicsContext::GetGraphicsAPI());
 | 
			
		||||
			return nullptr;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -7,7 +7,7 @@ namespace Light {
 | 
			
		|||
	class VertexBuffer
 | 
			
		||||
	{
 | 
			
		||||
	public:
 | 
			
		||||
		static VertexBuffer* Create(unsigned int stride, unsigned int count, float* vertices, void* sharedContext);
 | 
			
		||||
		static VertexBuffer* Create(float* vertices, unsigned int stride, unsigned int count, void* sharedContext);
 | 
			
		||||
 | 
			
		||||
		virtual void* Map() = 0;
 | 
			
		||||
		virtual void UnMap() = 0;
 | 
			
		||||
| 
						 | 
				
			
			@ -22,7 +22,7 @@ namespace Light {
 | 
			
		|||
	class IndexBuffer
 | 
			
		||||
	{
 | 
			
		||||
	public:
 | 
			
		||||
		static IndexBuffer* Create(unsigned int count, unsigned int* indices, void* sharedContext);
 | 
			
		||||
		static IndexBuffer* Create(unsigned int* indices, unsigned int count, void* sharedContext);
 | 
			
		||||
 | 
			
		||||
		virtual void Bind() = 0;
 | 
			
		||||
		virtual void UnBind() = 0;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -15,27 +15,10 @@ namespace Light {
 | 
			
		|||
		s_Context = this;
 | 
			
		||||
 | 
			
		||||
		// QUADRENDERER //
 | 
			
		||||
		unsigned int offset = 0;
 | 
			
		||||
		unsigned int* indices = new unsigned int[LT_MAX_QUAD * 6];
 | 
			
		||||
 | 
			
		||||
		for (int i = 0; i < LT_MAX_QUAD * 6; i += 6)
 | 
			
		||||
		{
 | 
			
		||||
			indices[i + 0] = offset + 0;
 | 
			
		||||
			indices[i + 1] = offset + 1;
 | 
			
		||||
			indices[i + 2] = offset + 2;
 | 
			
		||||
 | 
			
		||||
			indices[i + 3] = offset + 2;
 | 
			
		||||
			indices[i + 4] = offset + 3;
 | 
			
		||||
			indices[i + 5] = offset + 0;
 | 
			
		||||
 | 
			
		||||
			offset += 4;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		m_QuadRenderer.shader = std::unique_ptr<Shader>(Shader::Create("res/vertex.vertex", "res/fragment.fragment", m_SharedContext));
 | 
			
		||||
		m_QuadRenderer.vertexBuffer = std::unique_ptr<VertexBuffer>(VertexBuffer::Create(sizeof(QuadRendererProgram::QuadVertexData), LT_MAX_QUAD * 4, nullptr, m_SharedContext));
 | 
			
		||||
		m_QuadRenderer.vertexBuffer = std::unique_ptr<VertexBuffer>(VertexBuffer::Create(nullptr, sizeof(QuadRendererProgram::QuadVertexData), LT_MAX_QUAD * 4, m_SharedContext));
 | 
			
		||||
		m_QuadRenderer.vertexLayout = std::unique_ptr<VertexLayout>(VertexLayout::Create(m_QuadRenderer.vertexBuffer.get(), m_QuadRenderer.shader.get(), { { "POSITION", VertexElementType::Float3 },{ "COLOR", VertexElementType::Float4 } }, m_SharedContext));
 | 
			
		||||
		m_QuadRenderer.indexBuffer = std::unique_ptr<IndexBuffer>(IndexBuffer::Create(LT_MAX_QUAD * 6, indices, m_SharedContext));
 | 
			
		||||
		delete[] indices;
 | 
			
		||||
		m_QuadRenderer.indexBuffer = std::unique_ptr<IndexBuffer>(IndexBuffer::Create(nullptr, LT_MAX_QUAD * 3, m_SharedContext));
 | 
			
		||||
		// QUADRENDERER //
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -60,7 +43,7 @@ namespace Light {
 | 
			
		|||
			m_QuadRenderer.Map();
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		// local
 | 
			
		||||
		// locals
 | 
			
		||||
		const float xMin = position.x;
 | 
			
		||||
		const float yMin = position.y;
 | 
			
		||||
		const float xMax = position.x + size.x;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -5,7 +5,7 @@
 | 
			
		|||
 | 
			
		||||
namespace Light {
 | 
			
		||||
 | 
			
		||||
	dxVertexBuffer::dxVertexBuffer(unsigned int count, unsigned int stride, float* vertices, void* sharedContext)
 | 
			
		||||
	dxVertexBuffer::dxVertexBuffer(float* vertices, unsigned int stride, unsigned int count, void* sharedContext)
 | 
			
		||||
		: m_Stride(stride)
 | 
			
		||||
	{
 | 
			
		||||
		HRESULT hr;
 | 
			
		||||
| 
						 | 
				
			
			@ -54,7 +54,7 @@ namespace Light {
 | 
			
		|||
	}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
	dxIndexBuffer::dxIndexBuffer(unsigned int count, unsigned int* indices, void* sharedContext)
 | 
			
		||||
	dxIndexBuffer::dxIndexBuffer(unsigned int* indices, unsigned int count, void* sharedContext)
 | 
			
		||||
	{
 | 
			
		||||
		HRESULT hr;
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -64,6 +64,32 @@ namespace Light {
 | 
			
		|||
		m_Device = dxContext->device;
 | 
			
		||||
		m_DeviceContext = dxContext->deviceContext;
 | 
			
		||||
 | 
			
		||||
		bool hasIndices = !!indices;
 | 
			
		||||
		if (!hasIndices)
 | 
			
		||||
		{
 | 
			
		||||
			if (count % 6 != 0)
 | 
			
		||||
			{
 | 
			
		||||
				LT_ENGINE_WARN("dxIndexBuffer::dxIndexBuffer: count should be divisible by 6 when no indices is provided");
 | 
			
		||||
				LT_ENGINE_WARN("dxIndexBuffer::dxIndexBuffer: adding {} to count -> {}", (6 - (count % 6)), count + (6 - (count % 6)));
 | 
			
		||||
				count = count + (6 - (count % 6));
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			indices = new unsigned int[count];
 | 
			
		||||
			unsigned int offset = 0;
 | 
			
		||||
			for (unsigned int i = 0; i < count; i += 6)
 | 
			
		||||
			{
 | 
			
		||||
				indices[i + 0] = offset + 0;
 | 
			
		||||
				indices[i + 1] = offset + 1;
 | 
			
		||||
				indices[i + 2] = offset + 2;
 | 
			
		||||
 | 
			
		||||
				indices[i + 3] = offset + 2;
 | 
			
		||||
				indices[i + 4] = offset + 3;
 | 
			
		||||
				indices[i + 5] = offset + 0;
 | 
			
		||||
 | 
			
		||||
				offset += 4;
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		D3D11_BUFFER_DESC bufferDesc = { 0 };
 | 
			
		||||
		D3D11_SUBRESOURCE_DATA sd = { 0 };
 | 
			
		||||
		bufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;
 | 
			
		||||
| 
						 | 
				
			
			@ -75,6 +101,9 @@ namespace Light {
 | 
			
		|||
		sd.pSysMem = indices;
 | 
			
		||||
 | 
			
		||||
		DXC(m_Device->CreateBuffer(&bufferDesc, &sd, &m_Buffer));
 | 
			
		||||
 | 
			
		||||
		if (!hasIndices)
 | 
			
		||||
			delete[] indices;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	dxIndexBuffer::~dxIndexBuffer()
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -21,7 +21,7 @@ namespace Light {
 | 
			
		|||
		
 | 
			
		||||
		unsigned int m_Stride;
 | 
			
		||||
	public:
 | 
			
		||||
		dxVertexBuffer(unsigned int count, unsigned int stride, float* vertices, void* sharedContext);
 | 
			
		||||
		dxVertexBuffer(float* vertices, unsigned int stride, unsigned int count, void* sharedContext);
 | 
			
		||||
		~dxVertexBuffer();
 | 
			
		||||
 | 
			
		||||
		void* Map() override;
 | 
			
		||||
| 
						 | 
				
			
			@ -39,7 +39,7 @@ namespace Light {
 | 
			
		|||
		Microsoft::WRL::ComPtr<ID3D11Device> m_Device;
 | 
			
		||||
		Microsoft::WRL::ComPtr<ID3D11DeviceContext> m_DeviceContext;
 | 
			
		||||
	public:
 | 
			
		||||
		dxIndexBuffer(unsigned int count, unsigned int* indices, void* sharedContext);
 | 
			
		||||
		dxIndexBuffer(unsigned int* indices, unsigned int count, void* sharedContext);
 | 
			
		||||
		~dxIndexBuffer();
 | 
			
		||||
 | 
			
		||||
		void Bind() override;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -5,7 +5,7 @@
 | 
			
		|||
 | 
			
		||||
namespace Light {
 | 
			
		||||
 | 
			
		||||
	glVertexBuffer::glVertexBuffer(unsigned int count, float* vertices)
 | 
			
		||||
	glVertexBuffer::glVertexBuffer(float* vertices, unsigned int count)
 | 
			
		||||
	{
 | 
			
		||||
		glCreateBuffers(1, &m_BufferID);
 | 
			
		||||
		glNamedBufferData(m_BufferID, count * sizeof(float), vertices, GL_DYNAMIC_DRAW);
 | 
			
		||||
| 
						 | 
				
			
			@ -36,10 +36,39 @@ namespace Light {
 | 
			
		|||
		glBindBuffer(GL_ARRAY_BUFFER, NULL);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	glIndexBuffer::glIndexBuffer(unsigned int count, unsigned int* indices)
 | 
			
		||||
	glIndexBuffer::glIndexBuffer(unsigned int* indices, unsigned int count)
 | 
			
		||||
	{
 | 
			
		||||
		bool hasIndices = !!indices;
 | 
			
		||||
		if (!hasIndices)
 | 
			
		||||
		{
 | 
			
		||||
			if (count % 6 != 0)
 | 
			
		||||
			{
 | 
			
		||||
				LT_ENGINE_WARN("glIndexBuffer::glIndexBuffer: count should be divisible by 6 when no indices is provided");
 | 
			
		||||
				LT_ENGINE_WARN("glIndexBuffer::glIndexBuffer: adding {} to count -> {}", (6 - (count % 6)), count + (6 - (count % 6)));
 | 
			
		||||
				count = count + (6 - (count % 6));
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			indices = new unsigned int[count];
 | 
			
		||||
			unsigned int offset = 0;
 | 
			
		||||
			for (unsigned int i = 0; i < count; i += 6)
 | 
			
		||||
			{
 | 
			
		||||
				indices[i + 0] = offset + 0;
 | 
			
		||||
				indices[i + 1] = offset + 1;
 | 
			
		||||
				indices[i + 2] = offset + 2;
 | 
			
		||||
 | 
			
		||||
				indices[i + 3] = offset + 2;
 | 
			
		||||
				indices[i + 4] = offset + 3;
 | 
			
		||||
				indices[i + 5] = offset + 0;
 | 
			
		||||
 | 
			
		||||
				offset += 4;
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		glCreateBuffers(1, &m_BufferID);
 | 
			
		||||
		glNamedBufferData(m_BufferID, count * sizeof(unsigned int), indices, GL_STATIC_DRAW);
 | 
			
		||||
 | 
			
		||||
		if (!hasIndices)
 | 
			
		||||
			delete[] indices;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	glIndexBuffer::~glIndexBuffer()
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -11,7 +11,7 @@ namespace Light {
 | 
			
		|||
		unsigned int m_BufferID;
 | 
			
		||||
 | 
			
		||||
	public:
 | 
			
		||||
		glVertexBuffer(unsigned int count, float* vertices);
 | 
			
		||||
		glVertexBuffer(float* vertices, unsigned int count);
 | 
			
		||||
		~glVertexBuffer();
 | 
			
		||||
 | 
			
		||||
		void* Map() override;
 | 
			
		||||
| 
						 | 
				
			
			@ -27,7 +27,7 @@ namespace Light {
 | 
			
		|||
		unsigned int m_BufferID;
 | 
			
		||||
 | 
			
		||||
	public:
 | 
			
		||||
		glIndexBuffer(unsigned int count, unsigned int* indices);
 | 
			
		||||
		glIndexBuffer(unsigned int* indices, unsigned int count);
 | 
			
		||||
		~glIndexBuffer();
 | 
			
		||||
 | 
			
		||||
		void Bind() override;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		
		Reference in a new issue