Rearrange the code slightly so it is easier to add other backends

This commit is contained in:
Magnus Norddahl 2019-12-23 20:09:38 +01:00
parent 84efdad49e
commit ef6d217623
25 changed files with 1697 additions and 1428 deletions

268
Source/Native/Backend.cpp Normal file
View file

@ -0,0 +1,268 @@
/*
** BuilderNative Renderer
** Copyright (c) 2019 Magnus Norddahl
**
** This software is provided 'as-is', without any express or implied
** warranty. In no event will the authors be held liable for any damages
** arising from the use of this software.
**
** Permission is granted to anyone to use this software for any purpose,
** including commercial applications, and to alter it and redistribute it
** freely, subject to the following restrictions:
**
** 1. The origin of this software must not be misrepresented; you must not
** claim that you wrote the original software. If you use this software
** in a product, an acknowledgment in the product documentation would be
** appreciated but is not required.
** 2. Altered source versions must be plainly marked as such, and must not be
** misrepresented as being the original software.
** 3. This notice may not be removed or altered from any source distribution.
*/
#include "Precomp.h"
#include "Backend.h"
#include "OpenGL/GLBackend.h"
Backend* Backend::Get()
{
static std::unique_ptr<Backend> backend;
if (!backend)
backend.reset(new GLBackend());
return backend.get();
}
/////////////////////////////////////////////////////////////////////////////
extern "C"
{
RenderDevice* RenderDevice_New(void* disp, void* window)
{
return Backend::Get()->NewRenderDevice(disp, window);
}
void RenderDevice_Delete(RenderDevice* device)
{
Backend::Get()->DeleteRenderDevice(device);
}
const char* RenderDevice_GetError(RenderDevice* device)
{
return device->GetError();
}
void RenderDevice_DeclareUniform(RenderDevice* device, UniformName name, const char* variablename, UniformType type)
{
device->DeclareUniform(name, variablename, type);
}
void RenderDevice_DeclareShader(RenderDevice* device, ShaderName index, const char* name, const char* vertexshader, const char* fragmentshader)
{
device->DeclareShader(index, name, vertexshader, fragmentshader);
}
void RenderDevice_SetShader(RenderDevice* device, ShaderName name)
{
device->SetShader(name);
}
void RenderDevice_SetUniform(RenderDevice* device, UniformName name, const void* values, int count)
{
device->SetUniform(name, values, count);
}
void RenderDevice_SetVertexBuffer(RenderDevice* device, VertexBuffer* buffer)
{
device->SetVertexBuffer(buffer);
}
void RenderDevice_SetIndexBuffer(RenderDevice* device, IndexBuffer* buffer)
{
device->SetIndexBuffer(buffer);
}
void RenderDevice_SetAlphaBlendEnable(RenderDevice* device, bool value)
{
device->SetAlphaBlendEnable(value);
}
void RenderDevice_SetAlphaTestEnable(RenderDevice* device, bool value)
{
device->SetAlphaTestEnable(value);
}
void RenderDevice_SetCullMode(RenderDevice* device, Cull mode)
{
device->SetCullMode(mode);
}
void RenderDevice_SetBlendOperation(RenderDevice* device, BlendOperation op)
{
device->SetBlendOperation(op);
}
void RenderDevice_SetSourceBlend(RenderDevice* device, Blend blend)
{
device->SetSourceBlend(blend);
}
void RenderDevice_SetDestinationBlend(RenderDevice* device, Blend blend)
{
device->SetDestinationBlend(blend);
}
void RenderDevice_SetFillMode(RenderDevice* device, FillMode mode)
{
device->SetFillMode(mode);
}
void RenderDevice_SetMultisampleAntialias(RenderDevice* device, bool value)
{
device->SetMultisampleAntialias(value);
}
void RenderDevice_SetZEnable(RenderDevice* device, bool value)
{
device->SetZEnable(value);
}
void RenderDevice_SetZWriteEnable(RenderDevice* device, bool value)
{
device->SetZWriteEnable(value);
}
void RenderDevice_SetTexture(RenderDevice* device, Texture* texture)
{
device->SetTexture(texture);
}
void RenderDevice_SetSamplerFilter(RenderDevice* device, TextureFilter minfilter, TextureFilter magfilter, TextureFilter mipfilter, float maxanisotropy)
{
device->SetSamplerFilter(minfilter, magfilter, mipfilter, maxanisotropy);
}
void RenderDevice_SetSamplerState(RenderDevice* device, TextureAddress address)
{
device->SetSamplerState(address);
}
bool RenderDevice_Draw(RenderDevice* device, PrimitiveType type, int startIndex, int primitiveCount)
{
return device->Draw(type, startIndex, primitiveCount);
}
bool RenderDevice_DrawIndexed(RenderDevice* device, PrimitiveType type, int startIndex, int primitiveCount)
{
return device->DrawIndexed(type, startIndex, primitiveCount);
}
bool RenderDevice_DrawData(RenderDevice* device, PrimitiveType type, int startIndex, int primitiveCount, const void* data)
{
return device->DrawData(type, startIndex, primitiveCount, data);
}
bool RenderDevice_StartRendering(RenderDevice* device, bool clear, int backcolor, Texture* target, bool usedepthbuffer)
{
return device->StartRendering(clear, backcolor, target, usedepthbuffer);
}
bool RenderDevice_FinishRendering(RenderDevice* device)
{
return device->FinishRendering();
}
bool RenderDevice_Present(RenderDevice* device)
{
return device->Present();
}
bool RenderDevice_ClearTexture(RenderDevice* device, int backcolor, Texture* texture)
{
return device->ClearTexture(backcolor, texture);
}
bool RenderDevice_CopyTexture(RenderDevice* device, Texture* dst, CubeMapFace face)
{
return device->CopyTexture(dst, face);
}
bool RenderDevice_SetVertexBufferData(RenderDevice* device, VertexBuffer* buffer, void* data, int64_t size, VertexFormat format)
{
return device->SetVertexBufferData(buffer, data, size, format);
}
bool RenderDevice_SetVertexBufferSubdata(RenderDevice* device, VertexBuffer* buffer, int64_t destOffset, void* data, int64_t size)
{
return device->SetVertexBufferSubdata(buffer, destOffset, data, size);
}
bool RenderDevice_SetIndexBufferData(RenderDevice* device, IndexBuffer* buffer, void* data, int64_t size)
{
return device->SetIndexBufferData(buffer, data, size);
}
bool RenderDevice_SetPixels(RenderDevice* device, Texture* texture, const void* data)
{
return device->SetPixels(texture, data);
}
bool RenderDevice_SetCubePixels(RenderDevice* device, Texture* texture, CubeMapFace face, const void* data)
{
return device->SetCubePixels(texture, face, data);
}
void* RenderDevice_MapPBO(RenderDevice* device, Texture* texture)
{
return device->MapPBO(texture);
}
bool RenderDevice_UnmapPBO(RenderDevice* device, Texture* texture)
{
return device->UnmapPBO(texture);
}
////////////////////////////////////////////////////////////////////////////
IndexBuffer* IndexBuffer_New()
{
return Backend::Get()->NewIndexBuffer();
}
void IndexBuffer_Delete(IndexBuffer* buffer)
{
Backend::Get()->DeleteIndexBuffer(buffer);
}
////////////////////////////////////////////////////////////////////////////
VertexBuffer* VertexBuffer_New()
{
return Backend::Get()->NewVertexBuffer();
}
void VertexBuffer_Delete(VertexBuffer* buffer)
{
Backend::Get()->DeleteVertexBuffer(buffer);
}
////////////////////////////////////////////////////////////////////////////
Texture* Texture_New()
{
return Backend::Get()->NewTexture();
}
void Texture_Delete(Texture* tex)
{
return Backend::Get()->DeleteTexture(tex);
}
void Texture_Set2DImage(Texture* tex, int width, int height)
{
tex->Set2DImage(width, height);
}
void Texture_SetCubeImage(Texture* tex, int size)
{
tex->SetCubeImage(size);
}
}

130
Source/Native/Backend.h Normal file
View file

@ -0,0 +1,130 @@
/*
** BuilderNative Renderer
** Copyright (c) 2019 Magnus Norddahl
**
** This software is provided 'as-is', without any express or implied
** warranty. In no event will the authors be held liable for any damages
** arising from the use of this software.
**
** Permission is granted to anyone to use this software for any purpose,
** including commercial applications, and to alter it and redistribute it
** freely, subject to the following restrictions:
**
** 1. The origin of this software must not be misrepresented; you must not
** claim that you wrote the original software. If you use this software
** in a product, an acknowledgment in the product documentation would be
** appreciated but is not required.
** 2. Altered source versions must be plainly marked as such, and must not be
** misrepresented as being the original software.
** 3. This notice may not be removed or altered from any source distribution.
*/
#pragma once
#include <string>
#include <mutex>
enum class CubeMapFace : int { PositiveX, PositiveY, PositiveZ, NegativeX, NegativeY, NegativeZ };
enum class VertexFormat : int32_t { Flat, World };
enum class DeclarationUsage : int32_t { Position, Color, TextureCoordinate, Normal };
enum class Cull : int { None, Clockwise };
enum class Blend : int { InverseSourceAlpha, SourceAlpha, One };
enum class BlendOperation : int { Add, ReverseSubtract };
enum class FillMode : int { Solid, Wireframe };
enum class TextureAddress : int { Wrap, Clamp };
enum class ShaderFlags : int { None, Debug };
enum class PrimitiveType : int { LineList, TriangleList, TriangleStrip };
enum class TextureFilter : int { None, Point, Linear, Anisotropic };
enum class UniformType : int { Vec4f, Vec3f, Vec2f, Float, Mat4 };
typedef int UniformName;
typedef int ShaderName;
class VertexBuffer;
class IndexBuffer;
class Texture;
class RenderDevice
{
public:
virtual ~RenderDevice() = default;
virtual const char* GetError() = 0;
virtual void DeclareUniform(UniformName name, const char* glslname, UniformType type) = 0;
virtual void DeclareShader(ShaderName index, const char* name, const char* vertexshader, const char* fragmentshader) = 0;
virtual void SetShader(ShaderName name) = 0;
virtual void SetUniform(UniformName name, const void* values, int count) = 0;
virtual void SetVertexBuffer(VertexBuffer* buffer) = 0;
virtual void SetIndexBuffer(IndexBuffer* buffer) = 0;
virtual void SetAlphaBlendEnable(bool value) = 0;
virtual void SetAlphaTestEnable(bool value) = 0;
virtual void SetCullMode(Cull mode) = 0;
virtual void SetBlendOperation(BlendOperation op) = 0;
virtual void SetSourceBlend(Blend blend) = 0;
virtual void SetDestinationBlend(Blend blend) = 0;
virtual void SetFillMode(FillMode mode) = 0;
virtual void SetMultisampleAntialias(bool value) = 0;
virtual void SetZEnable(bool value) = 0;
virtual void SetZWriteEnable(bool value) = 0;
virtual void SetTexture(Texture* texture) = 0;
virtual void SetSamplerFilter(TextureFilter minfilter, TextureFilter magfilter, TextureFilter mipfilter, float maxanisotropy) = 0;
virtual void SetSamplerState(TextureAddress address) = 0;
virtual bool Draw(PrimitiveType type, int startIndex, int primitiveCount) = 0;
virtual bool DrawIndexed(PrimitiveType type, int startIndex, int primitiveCount) = 0;
virtual bool DrawData(PrimitiveType type, int startIndex, int primitiveCount, const void* data) = 0;
virtual bool StartRendering(bool clear, int backcolor, Texture* target, bool usedepthbuffer) = 0;
virtual bool FinishRendering() = 0;
virtual bool Present() = 0;
virtual bool ClearTexture(int backcolor, Texture* texture) = 0;
virtual bool CopyTexture(Texture* dst, CubeMapFace face) = 0;
virtual bool SetVertexBufferData(VertexBuffer* buffer, void* data, int64_t size, VertexFormat format) = 0;
virtual bool SetVertexBufferSubdata(VertexBuffer* buffer, int64_t destOffset, void* data, int64_t size) = 0;
virtual bool SetIndexBufferData(IndexBuffer* buffer, void* data, int64_t size) = 0;
virtual bool SetPixels(Texture* texture, const void* data) = 0;
virtual bool SetCubePixels(Texture* texture, CubeMapFace face, const void* data) = 0;
virtual void* MapPBO(Texture* texture) = 0;
virtual bool UnmapPBO(Texture* texture) = 0;
};
class VertexBuffer
{
public:
virtual ~VertexBuffer() = default;
static const int FlatStride = 24;
static const int WorldStride = 36;
};
class IndexBuffer
{
public:
virtual ~IndexBuffer() = default;
};
class Texture
{
public:
virtual ~Texture() = default;
virtual void Set2DImage(int width, int height) = 0;
virtual void SetCubeImage(int size) = 0;
};
class Backend
{
public:
static Backend* Get();
virtual RenderDevice* NewRenderDevice(void* disp, void* window) = 0;
virtual void DeleteRenderDevice(RenderDevice* device) = 0;
virtual VertexBuffer* NewVertexBuffer() = 0;
virtual void DeleteVertexBuffer(VertexBuffer* buffer) = 0;
virtual IndexBuffer* NewIndexBuffer() = 0;
virtual void DeleteIndexBuffer(IndexBuffer* buffer) = 0;
virtual Texture* NewTexture() = 0;
virtual void DeleteTexture(Texture* texture) = 0;
};

View file

@ -192,15 +192,21 @@
<None Include="exports.def" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="gl_load\gl_load.c">
<ClCompile Include="Matrix.cpp" />
<ClCompile Include="OpenGL\GLBackend.cpp" />
<ClCompile Include="OpenGL\GLRenderDevice.cpp" />
<ClCompile Include="OpenGL\GLIndexBuffer.cpp" />
<ClCompile Include="OpenGL\GLShader.cpp" />
<ClCompile Include="OpenGL\GLShaderManager.cpp" />
<ClCompile Include="OpenGL\GLTexture.cpp" />
<ClCompile Include="OpenGL\GLVertexBuffer.cpp" />
<ClCompile Include="OpenGL\gl_load\gl_load.c">
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">NotUsing</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">NotUsing</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">NotUsing</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|x64'">NotUsing</PrecompiledHeader>
</ClCompile>
<ClCompile Include="IndexBuffer.cpp" />
<ClCompile Include="Matrix.cpp" />
<ClCompile Include="OpenGLContext.cpp" />
<ClCompile Include="OpenGL\OpenGLContext.cpp" />
<ClCompile Include="Precomp.cpp">
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Create</PrecompiledHeader>
@ -208,26 +214,25 @@
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Create</PrecompiledHeader>
</ClCompile>
<ClCompile Include="RawMouse.cpp" />
<ClCompile Include="RenderDevice.cpp" />
<ClCompile Include="Shader.cpp" />
<ClCompile Include="ShaderManager.cpp" />
<ClCompile Include="Texture.cpp" />
<ClCompile Include="VertexBuffer.cpp" />
<ClCompile Include="Backend.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="gl_load\gl_load.h" />
<ClInclude Include="gl_load\gl_system.h" />
<ClInclude Include="IndexBuffer.h" />
<ClInclude Include="OpenGLContext.h" />
<ClInclude Include="OpenGL\GLBackend.h" />
<ClInclude Include="OpenGL\GLRenderDevice.h" />
<ClInclude Include="OpenGL\GLIndexBuffer.h" />
<ClInclude Include="OpenGL\GLShader.h" />
<ClInclude Include="OpenGL\GLShaderManager.h" />
<ClInclude Include="OpenGL\GLTexture.h" />
<ClInclude Include="OpenGL\GLVertexBuffer.h" />
<ClInclude Include="OpenGL\gl_load\gl_load.h" />
<ClInclude Include="OpenGL\gl_load\gl_system.h" />
<ClInclude Include="OpenGL\OpenGLContext.h" />
<ClInclude Include="Precomp.h" />
<ClInclude Include="RawMouse.h" />
<ClInclude Include="RenderDevice.h" />
<ClInclude Include="Shader.h" />
<ClInclude Include="Texture.h" />
<ClInclude Include="VertexBuffer.h" />
<ClInclude Include="Backend.h" />
</ItemGroup>
<ItemGroup>
<Text Include="gl_load\gl_extlist.txt" />
<Text Include="OpenGL\gl_load\gl_extlist.txt" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">

View file

@ -1,47 +1,87 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<ClCompile Include="IndexBuffer.cpp" />
<ClCompile Include="Matrix.cpp" />
<ClCompile Include="RenderDevice.cpp" />
<ClCompile Include="Texture.cpp" />
<ClCompile Include="VertexBuffer.cpp" />
<ClCompile Include="gl_load\gl_load.c">
<Filter>gl_load</Filter>
</ClCompile>
<ClCompile Include="OpenGLContext.cpp" />
<ClCompile Include="Precomp.cpp" />
<ClCompile Include="Shader.cpp" />
<ClCompile Include="ShaderManager.cpp" />
<ClCompile Include="RawMouse.cpp" />
<ClCompile Include="OpenGL\GLIndexBuffer.cpp">
<Filter>OpenGL</Filter>
</ClCompile>
<ClCompile Include="OpenGL\GLShader.cpp">
<Filter>OpenGL</Filter>
</ClCompile>
<ClCompile Include="OpenGL\GLShaderManager.cpp">
<Filter>OpenGL</Filter>
</ClCompile>
<ClCompile Include="OpenGL\GLTexture.cpp">
<Filter>OpenGL</Filter>
</ClCompile>
<ClCompile Include="OpenGL\GLVertexBuffer.cpp">
<Filter>OpenGL</Filter>
</ClCompile>
<ClCompile Include="OpenGL\OpenGLContext.cpp">
<Filter>OpenGL</Filter>
</ClCompile>
<ClCompile Include="OpenGL\gl_load\gl_load.c">
<Filter>OpenGL\gl_load</Filter>
</ClCompile>
<ClCompile Include="Backend.cpp" />
<ClCompile Include="OpenGL\GLRenderDevice.cpp">
<Filter>OpenGL</Filter>
</ClCompile>
<ClCompile Include="OpenGL\GLBackend.cpp">
<Filter>OpenGL</Filter>
</ClCompile>
<ClCompile Include="Matrix.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="IndexBuffer.h" />
<ClInclude Include="RenderDevice.h" />
<ClInclude Include="Texture.h" />
<ClInclude Include="VertexBuffer.h" />
<ClInclude Include="gl_load\gl_load.h">
<Filter>gl_load</Filter>
</ClInclude>
<ClInclude Include="gl_load\gl_system.h">
<Filter>gl_load</Filter>
</ClInclude>
<ClInclude Include="OpenGLContext.h" />
<ClInclude Include="Precomp.h" />
<ClInclude Include="Shader.h" />
<ClInclude Include="RawMouse.h" />
<ClInclude Include="OpenGL\GLIndexBuffer.h">
<Filter>OpenGL</Filter>
</ClInclude>
<ClInclude Include="OpenGL\GLShader.h">
<Filter>OpenGL</Filter>
</ClInclude>
<ClInclude Include="OpenGL\GLShaderManager.h">
<Filter>OpenGL</Filter>
</ClInclude>
<ClInclude Include="OpenGL\GLTexture.h">
<Filter>OpenGL</Filter>
</ClInclude>
<ClInclude Include="OpenGL\GLVertexBuffer.h">
<Filter>OpenGL</Filter>
</ClInclude>
<ClInclude Include="OpenGL\OpenGLContext.h">
<Filter>OpenGL</Filter>
</ClInclude>
<ClInclude Include="OpenGL\gl_load\gl_load.h">
<Filter>OpenGL\gl_load</Filter>
</ClInclude>
<ClInclude Include="OpenGL\gl_load\gl_system.h">
<Filter>OpenGL\gl_load</Filter>
</ClInclude>
<ClInclude Include="Backend.h" />
<ClInclude Include="OpenGL\GLRenderDevice.h">
<Filter>OpenGL</Filter>
</ClInclude>
<ClInclude Include="OpenGL\GLBackend.h">
<Filter>OpenGL</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="exports.def" />
</ItemGroup>
<ItemGroup>
<Filter Include="gl_load">
<UniqueIdentifier>{6455e9b5-8f21-4621-8d8d-c16489a2b548}</UniqueIdentifier>
<Filter Include="OpenGL">
<UniqueIdentifier>{116db6ea-72bb-4d44-8563-ba39091f2f0f}</UniqueIdentifier>
</Filter>
<Filter Include="OpenGL\gl_load">
<UniqueIdentifier>{0acc1cd1-c19f-4410-95aa-718746ce8eaf}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<Text Include="gl_load\gl_extlist.txt">
<Filter>gl_load</Filter>
<Text Include="OpenGL\gl_load\gl_extlist.txt">
<Filter>OpenGL\gl_load</Filter>
</Text>
</ItemGroup>
</Project>

View file

@ -0,0 +1,76 @@
/*
** BuilderNative Renderer
** Copyright (c) 2019 Magnus Norddahl
**
** This software is provided 'as-is', without any express or implied
** warranty. In no event will the authors be held liable for any damages
** arising from the use of this software.
**
** Permission is granted to anyone to use this software for any purpose,
** including commercial applications, and to alter it and redistribute it
** freely, subject to the following restrictions:
**
** 1. The origin of this software must not be misrepresented; you must not
** claim that you wrote the original software. If you use this software
** in a product, an acknowledgment in the product documentation would be
** appreciated but is not required.
** 2. Altered source versions must be plainly marked as such, and must not be
** misrepresented as being the original software.
** 3. This notice may not be removed or altered from any source distribution.
*/
#include "Precomp.h"
#include "GLBackend.h"
#include "GLRenderDevice.h"
#include "GLVertexBuffer.h"
#include "GLIndexBuffer.h"
#include "GLTexture.h"
RenderDevice* GLBackend::NewRenderDevice(void* disp, void* window)
{
GLRenderDevice* device = new GLRenderDevice(disp, window);
if (!device->Context)
{
delete device;
return nullptr;
}
else
{
return device;
}
}
void GLBackend::DeleteRenderDevice(RenderDevice* device)
{
delete device;
}
VertexBuffer* GLBackend::NewVertexBuffer()
{
return new GLVertexBuffer();
}
void GLBackend::DeleteVertexBuffer(VertexBuffer* buffer)
{
GLRenderDevice::DeleteObject(static_cast<GLVertexBuffer*>(buffer));
}
IndexBuffer* GLBackend::NewIndexBuffer()
{
return new GLIndexBuffer();
}
void GLBackend::DeleteIndexBuffer(IndexBuffer* buffer)
{
GLRenderDevice::DeleteObject(static_cast<GLIndexBuffer*>(buffer));
}
Texture* GLBackend::NewTexture()
{
return new GLTexture();
}
void GLBackend::DeleteTexture(Texture* texture)
{
GLRenderDevice::DeleteObject(static_cast<GLTexture*>(texture));
}

View file

@ -0,0 +1,40 @@
/*
** BuilderNative Renderer
** Copyright (c) 2019 Magnus Norddahl
**
** This software is provided 'as-is', without any express or implied
** warranty. In no event will the authors be held liable for any damages
** arising from the use of this software.
**
** Permission is granted to anyone to use this software for any purpose,
** including commercial applications, and to alter it and redistribute it
** freely, subject to the following restrictions:
**
** 1. The origin of this software must not be misrepresented; you must not
** claim that you wrote the original software. If you use this software
** in a product, an acknowledgment in the product documentation would be
** appreciated but is not required.
** 2. Altered source versions must be plainly marked as such, and must not be
** misrepresented as being the original software.
** 3. This notice may not be removed or altered from any source distribution.
*/
#pragma once
#include "../Backend.h"
class GLBackend : public Backend
{
public:
RenderDevice* NewRenderDevice(void* disp, void* window) override;
void DeleteRenderDevice(RenderDevice* device) override;
VertexBuffer* NewVertexBuffer() override;
void DeleteVertexBuffer(VertexBuffer* buffer) override;
IndexBuffer* NewIndexBuffer() override;
void DeleteIndexBuffer(IndexBuffer* buffer) override;
Texture* NewTexture() override;
void DeleteTexture(Texture* texture) override;
};

View file

@ -20,10 +20,10 @@
*/
#include "Precomp.h"
#include "IndexBuffer.h"
#include "RenderDevice.h"
#include "GLIndexBuffer.h"
#include "GLRenderDevice.h"
IndexBuffer::~IndexBuffer()
GLIndexBuffer::~GLIndexBuffer()
{
if (Device && mBuffer != 0)
{
@ -32,26 +32,9 @@ IndexBuffer::~IndexBuffer()
}
}
GLuint IndexBuffer::GetBuffer()
GLuint GLIndexBuffer::GetBuffer()
{
if (mBuffer == 0)
glGenBuffers(1, &mBuffer);
return mBuffer;
}
/////////////////////////////////////////////////////////////////////////////
extern "C"
{
IndexBuffer* IndexBuffer_New()
{
return new IndexBuffer();
}
void IndexBuffer_Delete(IndexBuffer* buffer)
{
RenderDevice::DeleteObject(buffer);
}
}

View file

@ -21,16 +21,18 @@
#pragma once
class RenderDevice;
#include "../Backend.h"
class IndexBuffer
class GLRenderDevice;
class GLIndexBuffer : public IndexBuffer
{
public:
~IndexBuffer();
~GLIndexBuffer();
GLuint GetBuffer();
RenderDevice* Device = nullptr;
GLRenderDevice* Device = nullptr;
private:
GLuint mBuffer = 0;

View file

@ -21,84 +21,60 @@
#pragma once
#include "../Backend.h"
#include "OpenGLContext.h"
#include <string>
#include <mutex>
class SharedVertexBuffer;
class VertexBuffer;
class IndexBuffer;
class Texture;
class ShaderManager;
class Shader;
enum class CubeMapFace;
enum class VertexFormat;
class GLSharedVertexBuffer;
class GLShader;
class GLShaderManager;
class GLVertexBuffer;
class GLIndexBuffer;
class GLTexture;
enum class Cull : int { None, Clockwise };
enum class Blend : int { InverseSourceAlpha, SourceAlpha, One };
enum class BlendOperation : int { Add, ReverseSubtract };
enum class FillMode : int { Solid, Wireframe };
enum class TextureAddress : int { Wrap, Clamp };
enum class ShaderFlags : int { None, Debug };
enum class PrimitiveType : int { LineList, TriangleList, TriangleStrip };
enum class TextureFilter : int { None, Point, Linear, Anisotropic };
typedef int UniformName;
typedef int ShaderName;
enum class UniformType : int
{
Vec4f,
Vec3f,
Vec2f,
Float,
Mat4
};
class RenderDevice
class GLRenderDevice : public RenderDevice
{
public:
RenderDevice(void* disp, void* window);
~RenderDevice();
GLRenderDevice(void* disp, void* window);
~GLRenderDevice();
void DeclareUniform(UniformName name, const char* glslname, UniformType type);
void DeclareShader(ShaderName index, const char* name, const char* vertexshader, const char* fragmentshader);
void SetShader(ShaderName name);
void SetUniform(UniformName name, const void* values, int count);
void SetVertexBuffer(VertexBuffer* buffer);
void SetIndexBuffer(IndexBuffer* buffer);
void SetAlphaBlendEnable(bool value);
void SetAlphaTestEnable(bool value);
void SetCullMode(Cull mode);
void SetBlendOperation(BlendOperation op);
void SetSourceBlend(Blend blend);
void SetDestinationBlend(Blend blend);
void SetFillMode(FillMode mode);
void SetMultisampleAntialias(bool value);
void SetZEnable(bool value);
void SetZWriteEnable(bool value);
void SetTexture(Texture* texture);
void SetSamplerFilter(TextureFilter minfilter, TextureFilter magfilter, TextureFilter mipfilter, float maxanisotropy);
void SetSamplerState(TextureAddress address);
bool Draw(PrimitiveType type, int startIndex, int primitiveCount);
bool DrawIndexed(PrimitiveType type, int startIndex, int primitiveCount);
bool DrawData(PrimitiveType type, int startIndex, int primitiveCount, const void* data);
bool StartRendering(bool clear, int backcolor, Texture* target, bool usedepthbuffer);
bool FinishRendering();
bool Present();
bool ClearTexture(int backcolor, Texture* texture);
bool CopyTexture(Texture* dst, CubeMapFace face);
void DeclareUniform(UniformName name, const char* glslname, UniformType type) override;
void DeclareShader(ShaderName index, const char* name, const char* vertexshader, const char* fragmentshader) override;
void SetShader(ShaderName name) override;
void SetUniform(UniformName name, const void* values, int count) override;
void SetVertexBuffer(VertexBuffer* buffer) override;
void SetIndexBuffer(IndexBuffer* buffer) override;
void SetAlphaBlendEnable(bool value) override;
void SetAlphaTestEnable(bool value) override;
void SetCullMode(Cull mode) override;
void SetBlendOperation(BlendOperation op) override;
void SetSourceBlend(Blend blend) override;
void SetDestinationBlend(Blend blend) override;
void SetFillMode(FillMode mode) override;
void SetMultisampleAntialias(bool value) override;
void SetZEnable(bool value) override;
void SetZWriteEnable(bool value) override;
void SetTexture(Texture* texture) override;
void SetSamplerFilter(TextureFilter minfilter, TextureFilter magfilter, TextureFilter mipfilter, float maxanisotropy) override;
void SetSamplerState(TextureAddress address) override;
bool Draw(PrimitiveType type, int startIndex, int primitiveCount) override;
bool DrawIndexed(PrimitiveType type, int startIndex, int primitiveCount) override;
bool DrawData(PrimitiveType type, int startIndex, int primitiveCount, const void* data) override;
bool StartRendering(bool clear, int backcolor, Texture* target, bool usedepthbuffer) override;
bool FinishRendering() override;
bool Present() override;
bool ClearTexture(int backcolor, Texture* texture) override;
bool CopyTexture(Texture* dst, CubeMapFace face) override;
bool SetVertexBufferData(VertexBuffer* buffer, void* data, int64_t size, VertexFormat format);
bool SetVertexBufferSubdata(VertexBuffer* buffer, int64_t destOffset, void* data, int64_t size);
bool SetIndexBufferData(IndexBuffer* buffer, void* data, int64_t size);
bool SetVertexBufferData(VertexBuffer* buffer, void* data, int64_t size, VertexFormat format) override;
bool SetVertexBufferSubdata(VertexBuffer* buffer, int64_t destOffset, void* data, int64_t size) override;
bool SetIndexBufferData(IndexBuffer* buffer, void* data, int64_t size) override;
bool SetPixels(Texture* texture, const void* data);
bool SetCubePixels(Texture* texture, CubeMapFace face, const void* data);
void* MapPBO(Texture* texture);
bool UnmapPBO(Texture* texture);
bool SetPixels(Texture* texture, const void* data) override;
bool SetCubePixels(Texture* texture, CubeMapFace face, const void* data) override;
void* MapPBO(Texture* texture) override;
bool UnmapPBO(Texture* texture) override;
bool InvalidateTexture(Texture* texture);
bool InvalidateTexture(GLTexture* texture);
void GarbageCollectBuffer(int size, VertexFormat format);
@ -117,14 +93,14 @@ public:
void SetError(const char* fmt, ...);
const char* GetError();
Shader* GetActiveShader();
GLShader* GetActiveShader();
GLint GetGLMinFilter(TextureFilter filter, TextureFilter mipfilter);
static std::mutex& GetMutex();
static void DeleteObject(VertexBuffer* buffer);
static void DeleteObject(IndexBuffer* buffer);
static void DeleteObject(Texture* texture);
static void DeleteObject(GLVertexBuffer* buffer);
static void DeleteObject(GLIndexBuffer* buffer);
static void DeleteObject(GLTexture* texture);
void ProcessDeleteList();
@ -132,14 +108,14 @@ public:
struct DeleteList
{
std::vector<VertexBuffer*> VertexBuffers;
std::vector<IndexBuffer*> IndexBuffers;
std::vector<Texture*> Textures;
std::vector<GLVertexBuffer*> VertexBuffers;
std::vector<GLIndexBuffer*> IndexBuffers;
std::vector<GLTexture*> Textures;
} mDeleteList;
struct TextureUnit
{
Texture* Tex = nullptr;
GLTexture* Tex = nullptr;
TextureAddress WrapMode = TextureAddress::Wrap;
GLuint SamplerHandle = 0;
} mTextureUnit;
@ -167,11 +143,11 @@ public:
int mVertexBuffer = -1;
int64_t mVertexBufferStartIndex = 0;
IndexBuffer* mIndexBuffer = nullptr;
GLIndexBuffer* mIndexBuffer = nullptr;
std::unique_ptr<SharedVertexBuffer> mSharedVertexBuffers[2];
std::unique_ptr<GLSharedVertexBuffer> mSharedVertexBuffers[2];
std::unique_ptr<ShaderManager> mShaderManager;
std::unique_ptr<GLShaderManager> mShaderManager;
ShaderName mShaderName = {};
struct UniformInfo

View file

@ -20,11 +20,11 @@
*/
#include "Precomp.h"
#include "Shader.h"
#include "RenderDevice.h"
#include "GLShader.h"
#include "GLRenderDevice.h"
#include <stdexcept>
void Shader::Setup(const std::string& identifier, const std::string& vertexShader, const std::string& fragmentShader, bool alphatest)
void GLShader::Setup(const std::string& identifier, const std::string& vertexShader, const std::string& fragmentShader, bool alphatest)
{
mIdentifier = identifier;
mVertexText = vertexShader;
@ -32,7 +32,7 @@ void Shader::Setup(const std::string& identifier, const std::string& vertexShade
mAlphatest = alphatest;
}
bool Shader::CheckCompile(RenderDevice* device)
bool GLShader::CheckCompile(GLRenderDevice* device)
{
bool firstCall = !mProgramBuilt;
if (firstCall)
@ -47,7 +47,7 @@ bool Shader::CheckCompile(RenderDevice* device)
return !mErrors.size();
}
std::string Shader::GetCompileError()
std::string GLShader::GetCompileError()
{
std::string lines = "Error compiling ";
if (!mVertexShader)
@ -66,7 +66,7 @@ std::string Shader::GetCompileError()
return lines;
}
void Shader::Bind()
void GLShader::Bind()
{
if (!mProgram || !mProgramBuilt || mErrors.size())
return;
@ -74,7 +74,7 @@ void Shader::Bind()
glUseProgram(mProgram);
}
void Shader::CreateProgram(RenderDevice* device)
void GLShader::CreateProgram(GLRenderDevice* device)
{
const char* prefixNAT = R"(
#version 150
@ -136,7 +136,7 @@ void Shader::CreateProgram(RenderDevice* device)
}
}
GLuint Shader::CompileShader(const std::string& code, GLenum type)
GLuint GLShader::CompileShader(const std::string& code, GLenum type)
{
GLuint shader = glCreateShader(type);
const GLchar* sources[] = { (GLchar*)code.data() };
@ -159,7 +159,7 @@ GLuint Shader::CompileShader(const std::string& code, GLenum type)
return shader;
}
void Shader::ReleaseResources()
void GLShader::ReleaseResources()
{
if (mProgram)
glDeleteProgram(mProgram);

View file

@ -22,17 +22,15 @@
#pragma once
#include <string>
#include "RenderDevice.h"
#include "GLRenderDevice.h"
enum class DeclarationUsage : int32_t { Position, Color, TextureCoordinate, Normal };
class Shader
class GLShader
{
public:
void ReleaseResources();
void Setup(const std::string& identifier, const std::string& vertexShader, const std::string& fragmentShader, bool alphatest);
bool CheckCompile(RenderDevice *device);
bool CheckCompile(GLRenderDevice *device);
void Bind();
std::string GetCompileError();
@ -41,7 +39,7 @@ public:
std::vector<GLuint> UniformLocations;
private:
void CreateProgram(RenderDevice* device);
void CreateProgram(GLRenderDevice* device);
GLuint CompileShader(const std::string& code, GLenum type);
std::string mIdentifier;

View file

@ -20,9 +20,9 @@
*/
#include "Precomp.h"
#include "ShaderManager.h"
#include "GLShaderManager.h"
void ShaderManager::DeclareShader(int i, const char* name, const char* vs, const char* ps)
void GLShaderManager::DeclareShader(int i, const char* name, const char* vs, const char* ps)
{
if (Shaders.size() <= (size_t)i)
{
@ -34,7 +34,7 @@ void ShaderManager::DeclareShader(int i, const char* name, const char* vs, const
AlphaTestShaders[i].Setup(name, vs, ps, true);
}
void ShaderManager::ReleaseResources()
void GLShaderManager::ReleaseResources()
{
for (size_t i = 0; i < Shaders.size(); i++)
{

View file

@ -1,14 +1,14 @@
#pragma once
#include "Shader.h"
#include "GLShader.h"
class ShaderManager
class GLShaderManager
{
public:
void ReleaseResources();
void DeclareShader(int index, const char* name, const char* vs, const char* ps);
std::vector<Shader> AlphaTestShaders;
std::vector<Shader> Shaders;
std::vector<GLShader> AlphaTestShaders;
std::vector<GLShader> Shaders;
};

View file

@ -20,47 +20,47 @@
*/
#include "Precomp.h"
#include "Texture.h"
#include "RenderDevice.h"
#include "GLTexture.h"
#include "GLRenderDevice.h"
#include <stdexcept>
Texture::Texture()
GLTexture::GLTexture()
{
}
Texture::~Texture()
GLTexture::~GLTexture()
{
if (Device)
Invalidate();
}
void Texture::Set2DImage(int width, int height)
void GLTexture::Set2DImage(int width, int height)
{
mCubeTexture = false;
mWidth = width;
mHeight = height;
}
void Texture::SetCubeImage(int size)
void GLTexture::SetCubeImage(int size)
{
mCubeTexture = true;
mWidth = size;
mHeight = size;
}
void Texture::SetPixels(const void* data)
void GLTexture::SetPixels(const void* data)
{
mPixels[0].resize(mWidth * (size_t)mHeight);
memcpy(mPixels[0].data(), data, sizeof(uint32_t) * mWidth * mHeight);
}
void Texture::SetCubePixels(CubeMapFace face, const void* data)
void GLTexture::SetCubePixels(CubeMapFace face, const void* data)
{
mPixels[(int)face].resize(mWidth * (size_t)mHeight);
memcpy(mPixels[(int)face].data(), data, sizeof(uint32_t) * mWidth * mHeight);
}
void Texture::Invalidate()
void GLTexture::Invalidate()
{
if (mDepthRenderbuffer) glDeleteRenderbuffers(1, &mDepthRenderbuffer);
if (mFramebuffer) glDeleteFramebuffers(1, &mFramebuffer);
@ -73,7 +73,7 @@ void Texture::Invalidate()
Device = nullptr;
}
GLuint Texture::GetTexture(RenderDevice* device)
GLuint GLTexture::GetTexture(GLRenderDevice* device)
{
if (mTexture == 0)
{
@ -122,7 +122,7 @@ GLuint Texture::GetTexture(RenderDevice* device)
return mTexture;
}
GLuint Texture::GetFramebuffer(RenderDevice* device, bool usedepthbuffer)
GLuint GLTexture::GetFramebuffer(GLRenderDevice* device, bool usedepthbuffer)
{
if (!usedepthbuffer)
{
@ -164,7 +164,7 @@ GLuint Texture::GetFramebuffer(RenderDevice* device, bool usedepthbuffer)
}
}
GLuint Texture::GetPBO(RenderDevice* device)
GLuint GLTexture::GetPBO(GLRenderDevice* device)
{
if (mPBO == 0)
{
@ -177,30 +177,3 @@ GLuint Texture::GetPBO(RenderDevice* device)
return mPBO;
}
/////////////////////////////////////////////////////////////////////////////
extern "C"
{
Texture* Texture_New()
{
return new Texture();
}
void Texture_Delete(Texture* tex)
{
RenderDevice::DeleteObject(tex);
}
void Texture_Set2DImage(Texture* handle, int width, int height)
{
handle->Set2DImage(width, height);
}
void Texture_SetCubeImage(Texture* handle, int size)
{
handle->SetCubeImage(size);
}
}

View file

@ -21,26 +21,18 @@
#pragma once
enum class CubeMapFace : int
{
PositiveX,
PositiveY,
PositiveZ,
NegativeX,
NegativeY,
NegativeZ
};
#include "../Backend.h"
class RenderDevice;
class GLRenderDevice;
class Texture
class GLTexture : public Texture
{
public:
Texture();
~Texture();
GLTexture();
~GLTexture();
void Set2DImage(int width, int height);
void SetCubeImage(int size);
void Set2DImage(int width, int height) override;
void SetCubeImage(int size) override;
void SetPixels(const void* data);
void SetCubePixels(CubeMapFace face, const void* data);
@ -52,11 +44,11 @@ public:
bool IsTextureCreated() const { return mTexture; }
void Invalidate();
GLuint GetTexture(RenderDevice* device);
GLuint GetFramebuffer(RenderDevice* device, bool usedepthbuffer);
GLuint GetPBO(RenderDevice* device);
GLuint GetTexture(GLRenderDevice* device);
GLuint GetFramebuffer(GLRenderDevice* device, bool usedepthbuffer);
GLuint GetPBO(GLRenderDevice* device);
RenderDevice* Device = nullptr;
GLRenderDevice* Device = nullptr;
private:
int mWidth = 0;
@ -67,6 +59,5 @@ private:
GLuint mTexture = 0;
GLuint mFramebuffer = 0;
GLuint mDepthRenderbuffer = 0;
//
GLuint mPBO = 0;
};

View file

@ -20,22 +20,18 @@
*/
#include "Precomp.h"
#include "VertexBuffer.h"
#include "Shader.h"
#include "RenderDevice.h"
#include "GLVertexBuffer.h"
#include "GLShader.h"
#include "GLRenderDevice.h"
SharedVertexBuffer::SharedVertexBuffer(VertexFormat format, int size) : Format(format), Size(size)
{
}
GLuint SharedVertexBuffer::GetBuffer()
GLuint GLSharedVertexBuffer::GetBuffer()
{
if (mBuffer == 0)
glGenBuffers(1, &mBuffer);
return mBuffer;
}
GLuint SharedVertexBuffer::GetVAO()
GLuint GLSharedVertexBuffer::GetVAO()
{
if (!mVAO)
{
@ -51,31 +47,31 @@ GLuint SharedVertexBuffer::GetVAO()
return mVAO;
}
void SharedVertexBuffer::SetupFlatVAO()
void GLSharedVertexBuffer::SetupFlatVAO()
{
glEnableVertexAttribArray((int)DeclarationUsage::Position);
glEnableVertexAttribArray((int)DeclarationUsage::Color);
glEnableVertexAttribArray((int)DeclarationUsage::TextureCoordinate);
glVertexAttribPointer((int)DeclarationUsage::Position, 3, GL_FLOAT, GL_FALSE, FlatStride, (const void*)0);
glVertexAttribPointer((int)DeclarationUsage::Color, GL_BGRA, GL_UNSIGNED_BYTE, GL_TRUE, FlatStride, (const void*)12);
glVertexAttribPointer((int)DeclarationUsage::TextureCoordinate, 2, GL_FLOAT, GL_FALSE, FlatStride, (const void*)16);
glVertexAttribPointer((int)DeclarationUsage::Position, 3, GL_FLOAT, GL_FALSE, VertexBuffer::FlatStride, (const void*)0);
glVertexAttribPointer((int)DeclarationUsage::Color, GL_BGRA, GL_UNSIGNED_BYTE, GL_TRUE, VertexBuffer::FlatStride, (const void*)12);
glVertexAttribPointer((int)DeclarationUsage::TextureCoordinate, 2, GL_FLOAT, GL_FALSE, VertexBuffer::FlatStride, (const void*)16);
}
void SharedVertexBuffer::SetupWorldVAO()
void GLSharedVertexBuffer::SetupWorldVAO()
{
glEnableVertexAttribArray((int)DeclarationUsage::Position);
glEnableVertexAttribArray((int)DeclarationUsage::Color);
glEnableVertexAttribArray((int)DeclarationUsage::TextureCoordinate);
glEnableVertexAttribArray((int)DeclarationUsage::Normal);
glVertexAttribPointer((int)DeclarationUsage::Position, 3, GL_FLOAT, GL_FALSE, WorldStride, (const void*)0);
glVertexAttribPointer((int)DeclarationUsage::Color, GL_BGRA, GL_UNSIGNED_BYTE, GL_TRUE, WorldStride, (const void*)12);
glVertexAttribPointer((int)DeclarationUsage::TextureCoordinate, 2, GL_FLOAT, GL_FALSE, WorldStride, (const void*)16);
glVertexAttribPointer((int)DeclarationUsage::Normal, 3, GL_FLOAT, GL_FALSE, WorldStride, (const void*)24);
glVertexAttribPointer((int)DeclarationUsage::Position, 3, GL_FLOAT, GL_FALSE, VertexBuffer::WorldStride, (const void*)0);
glVertexAttribPointer((int)DeclarationUsage::Color, GL_BGRA, GL_UNSIGNED_BYTE, GL_TRUE, VertexBuffer::WorldStride, (const void*)12);
glVertexAttribPointer((int)DeclarationUsage::TextureCoordinate, 2, GL_FLOAT, GL_FALSE, VertexBuffer::WorldStride, (const void*)16);
glVertexAttribPointer((int)DeclarationUsage::Normal, 3, GL_FLOAT, GL_FALSE, VertexBuffer::WorldStride, (const void*)24);
}
/////////////////////////////////////////////////////////////////////////////
VertexBuffer::~VertexBuffer()
GLVertexBuffer::~GLVertexBuffer()
{
if (Device)
{
@ -83,20 +79,3 @@ VertexBuffer::~VertexBuffer()
Device = nullptr;
}
}
/////////////////////////////////////////////////////////////////////////////
extern "C"
{
VertexBuffer* VertexBuffer_New()
{
return new VertexBuffer();
}
void VertexBuffer_Delete(VertexBuffer* buffer)
{
RenderDevice::DeleteObject(buffer);
}
}

View file

@ -23,15 +23,15 @@
#include <list>
enum class VertexFormat : int32_t { Flat, World };
#include "../Backend.h"
class RenderDevice;
class VertexBuffer;
class GLRenderDevice;
class GLVertexBuffer;
class SharedVertexBuffer
class GLSharedVertexBuffer
{
public:
SharedVertexBuffer(VertexFormat format, int size);
GLSharedVertexBuffer(VertexFormat format, int size) : Format(format), Size(size) { }
GLuint GetBuffer();
GLuint GetVAO();
@ -41,10 +41,7 @@ public:
int NextPos = 0;
int Size = 0;
std::list<VertexBuffer*> VertexBuffers;
static const int FlatStride = 24;
static const int WorldStride = 36;
std::list<GLVertexBuffer*> VertexBuffers;
static void SetupFlatVAO();
static void SetupWorldVAO();
@ -54,15 +51,15 @@ private:
GLuint mVAO = 0;
};
class VertexBuffer
class GLVertexBuffer : public VertexBuffer
{
public:
~VertexBuffer();
~GLVertexBuffer();
VertexFormat Format = VertexFormat::Flat;
RenderDevice* Device = nullptr;
std::list<VertexBuffer*>::iterator ListIt;
GLRenderDevice* Device = nullptr;
std::list<GLVertexBuffer*>::iterator ListIt;
int BufferOffset = 0;
int BufferStartIndex = 0;

View file

@ -24,7 +24,7 @@
#include <fcntl.h>
//GL headers
#include "gl_load/gl_load.h"
#include "OpenGL/gl_load/gl_load.h"
#if defined(__APPLE__)
#include <OpenGL/OpenGL.h>

View file

@ -34,7 +34,7 @@
#undef max
#endif
#include "gl_load/gl_system.h"
#include "OpenGL/gl_load/gl_system.h"
#define APART(x) (static_cast<uint32_t>(x) >> 24)
#define RPART(x) ((static_cast<uint32_t>(x) >> 16) & 0xff)