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

View file

@ -1,47 +1,87 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup> <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="Precomp.cpp" />
<ClCompile Include="Shader.cpp" />
<ClCompile Include="ShaderManager.cpp" />
<ClCompile Include="RawMouse.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>
<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="Precomp.h" />
<ClInclude Include="Shader.h" />
<ClInclude Include="RawMouse.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>
<ItemGroup> <ItemGroup>
<None Include="exports.def" /> <None Include="exports.def" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Filter Include="gl_load"> <Filter Include="OpenGL">
<UniqueIdentifier>{6455e9b5-8f21-4621-8d8d-c16489a2b548}</UniqueIdentifier> <UniqueIdentifier>{116db6ea-72bb-4d44-8563-ba39091f2f0f}</UniqueIdentifier>
</Filter>
<Filter Include="OpenGL\gl_load">
<UniqueIdentifier>{0acc1cd1-c19f-4410-95aa-718746ce8eaf}</UniqueIdentifier>
</Filter> </Filter>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Text Include="gl_load\gl_extlist.txt"> <Text Include="OpenGL\gl_load\gl_extlist.txt">
<Filter>gl_load</Filter> <Filter>OpenGL\gl_load</Filter>
</Text> </Text>
</ItemGroup> </ItemGroup>
</Project> </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 "Precomp.h"
#include "IndexBuffer.h" #include "GLIndexBuffer.h"
#include "RenderDevice.h" #include "GLRenderDevice.h"
IndexBuffer::~IndexBuffer() GLIndexBuffer::~GLIndexBuffer()
{ {
if (Device && mBuffer != 0) if (Device && mBuffer != 0)
{ {
@ -32,26 +32,9 @@ IndexBuffer::~IndexBuffer()
} }
} }
GLuint IndexBuffer::GetBuffer() GLuint GLIndexBuffer::GetBuffer()
{ {
if (mBuffer == 0) if (mBuffer == 0)
glGenBuffers(1, &mBuffer); glGenBuffers(1, &mBuffer);
return 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 #pragma once
class RenderDevice; #include "../Backend.h"
class IndexBuffer class GLRenderDevice;
class GLIndexBuffer : public IndexBuffer
{ {
public: public:
~IndexBuffer(); ~GLIndexBuffer();
GLuint GetBuffer(); GLuint GetBuffer();
RenderDevice* Device = nullptr; GLRenderDevice* Device = nullptr;
private: private:
GLuint mBuffer = 0; GLuint mBuffer = 0;

View file

@ -20,11 +20,11 @@
*/ */
#include "Precomp.h" #include "Precomp.h"
#include "RenderDevice.h" #include "GLRenderDevice.h"
#include "VertexBuffer.h" #include "GLVertexBuffer.h"
#include "IndexBuffer.h" #include "GLIndexBuffer.h"
#include "Texture.h" #include "GLTexture.h"
#include "ShaderManager.h" #include "GLShaderManager.h"
#include <stdexcept> #include <stdexcept>
#include <cstdarg> #include <cstdarg>
#include <algorithm> #include <algorithm>
@ -41,7 +41,7 @@ static void APIENTRY GLLogCallback(GLenum source, GLenum type, GLuint id,
fclose(f); fclose(f);
} }
RenderDevice::RenderDevice(void* disp, void* window) GLRenderDevice::GLRenderDevice(void* disp, void* window)
{ {
Context = IOpenGLContext::Create(disp, window); Context = IOpenGLContext::Create(disp, window);
if (Context) if (Context)
@ -57,12 +57,12 @@ RenderDevice::RenderDevice(void* disp, void* window)
glGenBuffers(1, &mStreamVertexBuffer); glGenBuffers(1, &mStreamVertexBuffer);
glBindVertexArray(mStreamVAO); glBindVertexArray(mStreamVAO);
glBindBuffer(GL_ARRAY_BUFFER, mStreamVertexBuffer); glBindBuffer(GL_ARRAY_BUFFER, mStreamVertexBuffer);
SharedVertexBuffer::SetupFlatVAO(); GLSharedVertexBuffer::SetupFlatVAO();
int i = 0; int i = 0;
for (auto& sharedbuf : mSharedVertexBuffers) for (auto& sharedbuf : mSharedVertexBuffers)
{ {
sharedbuf.reset(new SharedVertexBuffer((VertexFormat)i, (int64_t)16 * 1024 * 1024)); sharedbuf.reset(new GLSharedVertexBuffer((VertexFormat)i, (int64_t)16 * 1024 * 1024));
glBindBuffer(GL_ARRAY_BUFFER, sharedbuf->GetBuffer()); glBindBuffer(GL_ARRAY_BUFFER, sharedbuf->GetBuffer());
glBufferData(GL_ARRAY_BUFFER, sharedbuf->Size, nullptr, GL_STATIC_DRAW); glBufferData(GL_ARRAY_BUFFER, sharedbuf->Size, nullptr, GL_STATIC_DRAW);
i++; i++;
@ -70,13 +70,13 @@ RenderDevice::RenderDevice(void* disp, void* window)
glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ARRAY_BUFFER, 0);
mShaderManager = std::make_unique<ShaderManager>(); mShaderManager = std::make_unique<GLShaderManager>();
CheckGLError(); CheckGLError();
} }
} }
RenderDevice::~RenderDevice() GLRenderDevice::~GLRenderDevice()
{ {
if (Context) if (Context)
{ {
@ -89,7 +89,7 @@ RenderDevice::~RenderDevice()
for (auto& sharedbuf : mSharedVertexBuffers) for (auto& sharedbuf : mSharedVertexBuffers)
{ {
for (VertexBuffer* buf : sharedbuf->VertexBuffers) for (GLVertexBuffer* buf : sharedbuf->VertexBuffers)
buf->Device = nullptr; buf->Device = nullptr;
GLuint handle = sharedbuf->GetBuffer(); GLuint handle = sharedbuf->GetBuffer();
@ -112,14 +112,15 @@ RenderDevice::~RenderDevice()
} }
} }
void RenderDevice::DeclareShader(ShaderName index, const char* name, const char* vertexshader, const char* fragmentshader) void GLRenderDevice::DeclareShader(ShaderName index, const char* name, const char* vertexshader, const char* fragmentshader)
{ {
if (!mContextIsCurrent) Context->MakeCurrent(); if (!mContextIsCurrent) Context->MakeCurrent();
mShaderManager->DeclareShader(index, name, vertexshader, fragmentshader); mShaderManager->DeclareShader(index, name, vertexshader, fragmentshader);
} }
void RenderDevice::SetVertexBuffer(VertexBuffer* buffer) void GLRenderDevice::SetVertexBuffer(VertexBuffer* ibuffer)
{ {
GLVertexBuffer* buffer = static_cast<GLVertexBuffer*>(ibuffer);
if (buffer != nullptr) if (buffer != nullptr)
{ {
mVertexBufferStartIndex = buffer->BufferStartIndex; mVertexBufferStartIndex = buffer->BufferStartIndex;
@ -142,17 +143,17 @@ void RenderDevice::SetVertexBuffer(VertexBuffer* buffer)
} }
} }
void RenderDevice::SetIndexBuffer(IndexBuffer* buffer) void GLRenderDevice::SetIndexBuffer(IndexBuffer* buffer)
{ {
if (mIndexBuffer != buffer) if (mIndexBuffer != buffer)
{ {
mIndexBuffer = buffer; mIndexBuffer = static_cast<GLIndexBuffer*>(buffer);
mNeedApply = true; mNeedApply = true;
mIndexBufferChanged = true; mIndexBufferChanged = true;
} }
} }
void RenderDevice::SetAlphaBlendEnable(bool value) void GLRenderDevice::SetAlphaBlendEnable(bool value)
{ {
if (mAlphaBlend != value) if (mAlphaBlend != value)
{ {
@ -162,7 +163,7 @@ void RenderDevice::SetAlphaBlendEnable(bool value)
} }
} }
void RenderDevice::SetAlphaTestEnable(bool value) void GLRenderDevice::SetAlphaTestEnable(bool value)
{ {
if (mAlphaTest != value) if (mAlphaTest != value)
{ {
@ -173,7 +174,7 @@ void RenderDevice::SetAlphaTestEnable(bool value)
} }
} }
void RenderDevice::SetCullMode(Cull mode) void GLRenderDevice::SetCullMode(Cull mode)
{ {
if (mCullMode != mode) if (mCullMode != mode)
{ {
@ -183,7 +184,7 @@ void RenderDevice::SetCullMode(Cull mode)
} }
} }
void RenderDevice::SetBlendOperation(BlendOperation op) void GLRenderDevice::SetBlendOperation(BlendOperation op)
{ {
if (mBlendOperation != op) if (mBlendOperation != op)
{ {
@ -193,7 +194,7 @@ void RenderDevice::SetBlendOperation(BlendOperation op)
} }
} }
void RenderDevice::SetSourceBlend(Blend blend) void GLRenderDevice::SetSourceBlend(Blend blend)
{ {
if (mSourceBlend != blend) if (mSourceBlend != blend)
{ {
@ -203,7 +204,7 @@ void RenderDevice::SetSourceBlend(Blend blend)
} }
} }
void RenderDevice::SetDestinationBlend(Blend blend) void GLRenderDevice::SetDestinationBlend(Blend blend)
{ {
if (mDestinationBlend != blend) if (mDestinationBlend != blend)
{ {
@ -213,7 +214,7 @@ void RenderDevice::SetDestinationBlend(Blend blend)
} }
} }
void RenderDevice::SetFillMode(FillMode mode) void GLRenderDevice::SetFillMode(FillMode mode)
{ {
if (mFillMode != mode) if (mFillMode != mode)
{ {
@ -223,11 +224,11 @@ void RenderDevice::SetFillMode(FillMode mode)
} }
} }
void RenderDevice::SetMultisampleAntialias(bool value) void GLRenderDevice::SetMultisampleAntialias(bool value)
{ {
} }
void RenderDevice::SetZEnable(bool value) void GLRenderDevice::SetZEnable(bool value)
{ {
if (mDepthTest != value) if (mDepthTest != value)
{ {
@ -237,7 +238,7 @@ void RenderDevice::SetZEnable(bool value)
} }
} }
void RenderDevice::SetZWriteEnable(bool value) void GLRenderDevice::SetZWriteEnable(bool value)
{ {
if (mDepthWrite != value) if (mDepthWrite != value)
{ {
@ -247,17 +248,17 @@ void RenderDevice::SetZWriteEnable(bool value)
} }
} }
void RenderDevice::SetTexture(Texture* texture) void GLRenderDevice::SetTexture(Texture* texture)
{ {
if (mTextureUnit.Tex != texture) if (mTextureUnit.Tex != texture)
{ {
mTextureUnit.Tex = texture; mTextureUnit.Tex = static_cast<GLTexture*>(texture);
mNeedApply = true; mNeedApply = true;
mTexturesChanged = true; mTexturesChanged = true;
} }
} }
void RenderDevice::SetSamplerFilter(TextureFilter minfilter, TextureFilter magfilter, TextureFilter mipfilter, float maxanisotropy) void GLRenderDevice::SetSamplerFilter(TextureFilter minfilter, TextureFilter magfilter, TextureFilter mipfilter, float maxanisotropy)
{ {
SamplerFilterKey key; SamplerFilterKey key;
key.MinFilter = GetGLMinFilter(minfilter, mipfilter); key.MinFilter = GetGLMinFilter(minfilter, mipfilter);
@ -273,7 +274,7 @@ void RenderDevice::SetSamplerFilter(TextureFilter minfilter, TextureFilter magfi
} }
} }
GLint RenderDevice::GetGLMinFilter(TextureFilter filter, TextureFilter mipfilter) GLint GLRenderDevice::GetGLMinFilter(TextureFilter filter, TextureFilter mipfilter)
{ {
if (mipfilter == TextureFilter::Linear) if (mipfilter == TextureFilter::Linear)
{ {
@ -298,7 +299,7 @@ GLint RenderDevice::GetGLMinFilter(TextureFilter filter, TextureFilter mipfilter
} }
} }
void RenderDevice::SetSamplerState(TextureAddress address) void GLRenderDevice::SetSamplerState(TextureAddress address)
{ {
if (mTextureUnit.WrapMode != address) if (mTextureUnit.WrapMode != address)
{ {
@ -308,7 +309,7 @@ void RenderDevice::SetSamplerState(TextureAddress address)
} }
} }
bool RenderDevice::Draw(PrimitiveType type, int startIndex, int primitiveCount) bool GLRenderDevice::Draw(PrimitiveType type, int startIndex, int primitiveCount)
{ {
static const int modes[] = { GL_LINES, GL_TRIANGLES, GL_TRIANGLE_STRIP }; static const int modes[] = { GL_LINES, GL_TRIANGLES, GL_TRIANGLE_STRIP };
static const int toVertexCount[] = { 2, 3, 1 }; static const int toVertexCount[] = { 2, 3, 1 };
@ -319,7 +320,7 @@ bool RenderDevice::Draw(PrimitiveType type, int startIndex, int primitiveCount)
return CheckGLError(); return CheckGLError();
} }
bool RenderDevice::DrawIndexed(PrimitiveType type, int startIndex, int primitiveCount) bool GLRenderDevice::DrawIndexed(PrimitiveType type, int startIndex, int primitiveCount)
{ {
static const int modes[] = { GL_LINES, GL_TRIANGLES, GL_TRIANGLE_STRIP }; static const int modes[] = { GL_LINES, GL_TRIANGLES, GL_TRIANGLE_STRIP };
static const int toVertexCount[] = { 2, 3, 1 }; static const int toVertexCount[] = { 2, 3, 1 };
@ -330,7 +331,7 @@ bool RenderDevice::DrawIndexed(PrimitiveType type, int startIndex, int primitive
return CheckGLError(); return CheckGLError();
} }
bool RenderDevice::DrawData(PrimitiveType type, int startIndex, int primitiveCount, const void* data) bool GLRenderDevice::DrawData(PrimitiveType type, int startIndex, int primitiveCount, const void* data)
{ {
static const int modes[] = { GL_LINES, GL_TRIANGLES, GL_TRIANGLE_STRIP }; static const int modes[] = { GL_LINES, GL_TRIANGLES, GL_TRIANGLE_STRIP };
static const int toVertexCount[] = { 2, 3, 1 }; static const int toVertexCount[] = { 2, 3, 1 };
@ -341,7 +342,7 @@ bool RenderDevice::DrawData(PrimitiveType type, int startIndex, int primitiveCou
if (mNeedApply && !ApplyChanges()) return false; if (mNeedApply && !ApplyChanges()) return false;
glBindBuffer(GL_ARRAY_BUFFER, mStreamVertexBuffer); glBindBuffer(GL_ARRAY_BUFFER, mStreamVertexBuffer);
glBufferData(GL_ARRAY_BUFFER, vertcount * (size_t)SharedVertexBuffer::FlatStride, static_cast<const uint8_t*>(data) + startIndex * (size_t)SharedVertexBuffer::FlatStride, GL_STREAM_DRAW); glBufferData(GL_ARRAY_BUFFER, vertcount * (size_t)VertexBuffer::FlatStride, static_cast<const uint8_t*>(data) + startIndex * (size_t)VertexBuffer::FlatStride, GL_STREAM_DRAW);
glBindVertexArray(mStreamVAO); glBindVertexArray(mStreamVAO);
glDrawArrays(modes[(int)type], 0, vertcount); glDrawArrays(modes[(int)type], 0, vertcount);
if (!CheckGLError()) return false; if (!CheckGLError()) return false;
@ -349,11 +350,12 @@ bool RenderDevice::DrawData(PrimitiveType type, int startIndex, int primitiveCou
return ApplyVertexBuffer(); return ApplyVertexBuffer();
} }
bool RenderDevice::StartRendering(bool clear, int backcolor, Texture* target, bool usedepthbuffer) bool GLRenderDevice::StartRendering(bool clear, int backcolor, Texture* itarget, bool usedepthbuffer)
{ {
Context->MakeCurrent(); Context->MakeCurrent();
mContextIsCurrent = true; mContextIsCurrent = true;
GLTexture* target = static_cast<GLTexture*>(itarget);
if (target) if (target)
{ {
GLuint framebuffer = 0; GLuint framebuffer = 0;
@ -406,13 +408,13 @@ bool RenderDevice::StartRendering(bool clear, int backcolor, Texture* target, bo
return CheckGLError(); return CheckGLError();
} }
bool RenderDevice::FinishRendering() bool GLRenderDevice::FinishRendering()
{ {
mContextIsCurrent = false; mContextIsCurrent = false;
return true; return true;
} }
bool RenderDevice::Present() bool GLRenderDevice::Present()
{ {
Context->MakeCurrent(); Context->MakeCurrent();
Context->SwapBuffers(); Context->SwapBuffers();
@ -420,14 +422,16 @@ bool RenderDevice::Present()
return CheckGLError(); return CheckGLError();
} }
bool RenderDevice::ClearTexture(int backcolor, Texture* texture) bool GLRenderDevice::ClearTexture(int backcolor, Texture* texture)
{ {
if (!StartRendering(true, backcolor, texture, false)) return false; if (!StartRendering(true, backcolor, texture, false)) return false;
return FinishRendering(); return FinishRendering();
} }
bool RenderDevice::CopyTexture(Texture* dst, CubeMapFace face) bool GLRenderDevice::CopyTexture(Texture* idst, CubeMapFace face)
{ {
GLTexture* dst = static_cast<GLTexture*>(idst);
static const GLenum facegl[] = { static const GLenum facegl[] = {
GL_TEXTURE_CUBE_MAP_POSITIVE_X, GL_TEXTURE_CUBE_MAP_POSITIVE_X,
GL_TEXTURE_CUBE_MAP_POSITIVE_Y, GL_TEXTURE_CUBE_MAP_POSITIVE_Y,
@ -451,26 +455,26 @@ bool RenderDevice::CopyTexture(Texture* dst, CubeMapFace face)
return result; return result;
} }
void RenderDevice::GarbageCollectBuffer(int size, VertexFormat format) void GLRenderDevice::GarbageCollectBuffer(int size, VertexFormat format)
{ {
auto& sharedbuf = mSharedVertexBuffers[(int)format]; auto& sharedbuf = mSharedVertexBuffers[(int)format];
if (sharedbuf->NextPos + size <= sharedbuf->Size) if (sharedbuf->NextPos + size <= sharedbuf->Size)
return; return;
GLint oldarray = 0, oldvao = 0;
glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &oldarray);
glGetIntegerv(GL_VERTEX_ARRAY_BINDING, &oldvao);
int totalSize = size; int totalSize = size;
for (VertexBuffer* buf : sharedbuf->VertexBuffers) for (GLVertexBuffer* buf : sharedbuf->VertexBuffers)
totalSize += buf->Size; totalSize += buf->Size;
// If buffer is only half full we only need to GC. Otherwise we also need to expand the buffer size. // If buffer is only half full we only need to GC. Otherwise we also need to expand the buffer size.
int newSize = std::max(totalSize, sharedbuf->Size); int newSize = std::max(totalSize, sharedbuf->Size);
if (newSize < totalSize * 2) newSize *= 2; if (newSize < totalSize * 2) newSize *= 2;
std::unique_ptr<SharedVertexBuffer> old = std::move(sharedbuf); std::unique_ptr<GLSharedVertexBuffer> old = std::move(sharedbuf);
sharedbuf.reset(new SharedVertexBuffer(format, newSize)); sharedbuf.reset(new GLSharedVertexBuffer(format, newSize));
GLint oldarray = 0, oldvao = 0;
glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &oldarray);
glGetIntegerv(GL_VERTEX_ARRAY_BINDING, &oldvao);
glBindBuffer(GL_ARRAY_BUFFER, sharedbuf->GetBuffer()); glBindBuffer(GL_ARRAY_BUFFER, sharedbuf->GetBuffer());
glBufferData(GL_ARRAY_BUFFER, sharedbuf->Size, nullptr, GL_STATIC_DRAW); glBufferData(GL_ARRAY_BUFFER, sharedbuf->Size, nullptr, GL_STATIC_DRAW);
@ -478,11 +482,11 @@ void RenderDevice::GarbageCollectBuffer(int size, VertexFormat format)
glBindBuffer(GL_COPY_READ_BUFFER, old->GetBuffer()); glBindBuffer(GL_COPY_READ_BUFFER, old->GetBuffer());
// Copy all ranges still in use to the new buffer // Copy all ranges still in use to the new buffer
int stride = (format == VertexFormat::Flat ? SharedVertexBuffer::FlatStride : SharedVertexBuffer::WorldStride); int stride = (format == VertexFormat::Flat ? VertexBuffer::FlatStride : VertexBuffer::WorldStride);
int readPos = 0; int readPos = 0;
int writePos = 0; int writePos = 0;
int copySize = 0; int copySize = 0;
for (VertexBuffer* buf : old->VertexBuffers) for (GLVertexBuffer* buf : old->VertexBuffers)
{ {
if (buf->BufferOffset != readPos + copySize) if (buf->BufferOffset != readPos + copySize)
{ {
@ -518,10 +522,12 @@ void RenderDevice::GarbageCollectBuffer(int size, VertexFormat format)
mNeedApply = true; mNeedApply = true;
} }
bool RenderDevice::SetVertexBufferData(VertexBuffer* buffer, void* data, int64_t size, VertexFormat format) bool GLRenderDevice::SetVertexBufferData(VertexBuffer* ibuffer, void* data, int64_t size, VertexFormat format)
{ {
if (!mContextIsCurrent) Context->MakeCurrent(); if (!mContextIsCurrent) Context->MakeCurrent();
GLVertexBuffer* buffer = static_cast<GLVertexBuffer*>(ibuffer);
if (buffer->Device) if (buffer->Device)
{ {
buffer->Device->mSharedVertexBuffers[(int)buffer->Format]->VertexBuffers.erase(buffer->ListIt); buffer->Device->mSharedVertexBuffers[(int)buffer->Format]->VertexBuffers.erase(buffer->ListIt);
@ -541,7 +547,7 @@ bool RenderDevice::SetVertexBufferData(VertexBuffer* buffer, void* data, int64_t
buffer->Size = size; buffer->Size = size;
buffer->Format = format; buffer->Format = format;
buffer->BufferOffset = sharedbuf->NextPos; buffer->BufferOffset = sharedbuf->NextPos;
buffer->BufferStartIndex = buffer->BufferOffset / (format == VertexFormat::Flat ? SharedVertexBuffer::FlatStride : SharedVertexBuffer::WorldStride); buffer->BufferStartIndex = buffer->BufferOffset / (format == VertexFormat::Flat ? VertexBuffer::FlatStride : VertexBuffer::WorldStride);
sharedbuf->NextPos += size; sharedbuf->NextPos += size;
glBufferSubData(GL_ARRAY_BUFFER, buffer->BufferOffset, size, data); glBufferSubData(GL_ARRAY_BUFFER, buffer->BufferOffset, size, data);
@ -550,9 +556,10 @@ bool RenderDevice::SetVertexBufferData(VertexBuffer* buffer, void* data, int64_t
return result; return result;
} }
bool RenderDevice::SetVertexBufferSubdata(VertexBuffer* buffer, int64_t destOffset, void* data, int64_t size) bool GLRenderDevice::SetVertexBufferSubdata(VertexBuffer* ibuffer, int64_t destOffset, void* data, int64_t size)
{ {
if (!mContextIsCurrent) Context->MakeCurrent(); if (!mContextIsCurrent) Context->MakeCurrent();
GLVertexBuffer* buffer = static_cast<GLVertexBuffer*>(ibuffer);
GLint oldbinding = 0; GLint oldbinding = 0;
glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &oldbinding); glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &oldbinding);
glBindBuffer(GL_ARRAY_BUFFER, mSharedVertexBuffers[(int)buffer->Format]->GetBuffer()); glBindBuffer(GL_ARRAY_BUFFER, mSharedVertexBuffers[(int)buffer->Format]->GetBuffer());
@ -562,9 +569,10 @@ bool RenderDevice::SetVertexBufferSubdata(VertexBuffer* buffer, int64_t destOffs
return result; return result;
} }
bool RenderDevice::SetIndexBufferData(IndexBuffer* buffer, void* data, int64_t size) bool GLRenderDevice::SetIndexBufferData(IndexBuffer* ibuffer, void* data, int64_t size)
{ {
if (!mContextIsCurrent) Context->MakeCurrent(); if (!mContextIsCurrent) Context->MakeCurrent();
GLIndexBuffer* buffer = static_cast<GLIndexBuffer*>(ibuffer);
GLint oldbinding = 0; GLint oldbinding = 0;
glGetIntegerv(GL_ELEMENT_ARRAY_BUFFER_BINDING, &oldbinding); glGetIntegerv(GL_ELEMENT_ARRAY_BUFFER_BINDING, &oldbinding);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer->GetBuffer()); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer->GetBuffer());
@ -574,21 +582,24 @@ bool RenderDevice::SetIndexBufferData(IndexBuffer* buffer, void* data, int64_t s
return result; return result;
} }
bool RenderDevice::SetPixels(Texture* texture, const void* data) bool GLRenderDevice::SetPixels(Texture* itexture, const void* data)
{ {
GLTexture* texture = static_cast<GLTexture*>(itexture);
texture->SetPixels(data); texture->SetPixels(data);
return InvalidateTexture(texture); return InvalidateTexture(texture);
} }
bool RenderDevice::SetCubePixels(Texture* texture, CubeMapFace face, const void* data) bool GLRenderDevice::SetCubePixels(Texture* itexture, CubeMapFace face, const void* data)
{ {
GLTexture* texture = static_cast<GLTexture*>(itexture);
texture->SetCubePixels(face, data); texture->SetCubePixels(face, data);
return InvalidateTexture(texture); return InvalidateTexture(texture);
} }
void* RenderDevice::MapPBO(Texture* texture) void* GLRenderDevice::MapPBO(Texture* itexture)
{ {
if (!mContextIsCurrent) Context->MakeCurrent(); if (!mContextIsCurrent) Context->MakeCurrent();
GLTexture* texture = static_cast<GLTexture*>(itexture);
GLint pbo = texture->GetPBO(this); GLint pbo = texture->GetPBO(this);
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pbo); glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pbo);
void* buf = glMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_WRITE_ONLY); void* buf = glMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_WRITE_ONLY);
@ -601,9 +612,10 @@ void* RenderDevice::MapPBO(Texture* texture)
return buf; return buf;
} }
bool RenderDevice::UnmapPBO(Texture* texture) bool GLRenderDevice::UnmapPBO(Texture* itexture)
{ {
if (!mContextIsCurrent) Context->MakeCurrent(); if (!mContextIsCurrent) Context->MakeCurrent();
GLTexture* texture = static_cast<GLTexture*>(itexture);
GLint pbo = texture->GetPBO(this); GLint pbo = texture->GetPBO(this);
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pbo); glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pbo);
glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER); glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER);
@ -615,7 +627,7 @@ bool RenderDevice::UnmapPBO(Texture* texture)
return result; return result;
} }
bool RenderDevice::InvalidateTexture(Texture* texture) bool GLRenderDevice::InvalidateTexture(GLTexture* texture)
{ {
if (texture->IsTextureCreated()) if (texture->IsTextureCreated())
{ {
@ -632,7 +644,7 @@ bool RenderDevice::InvalidateTexture(Texture* texture)
} }
} }
bool RenderDevice::CheckGLError() bool GLRenderDevice::CheckGLError()
{ {
if (!Context->IsCurrent()) if (!Context->IsCurrent())
{ {
@ -647,7 +659,7 @@ bool RenderDevice::CheckGLError()
return false; return false;
} }
void RenderDevice::SetError(const char* fmt, ...) void GLRenderDevice::SetError(const char* fmt, ...)
{ {
va_list va; va_list va;
va_start(va, fmt); va_start(va, fmt);
@ -658,14 +670,14 @@ void RenderDevice::SetError(const char* fmt, ...)
mLastError = mSetErrorBuffer; mLastError = mSetErrorBuffer;
} }
const char* RenderDevice::GetError() const char* GLRenderDevice::GetError()
{ {
mReturnError.swap(mLastError); mReturnError.swap(mLastError);
mLastError.clear(); mLastError.clear();
return mReturnError.c_str(); return mReturnError.c_str();
} }
Shader* RenderDevice::GetActiveShader() GLShader* GLRenderDevice::GetActiveShader()
{ {
if (mAlphaTest) if (mAlphaTest)
return &mShaderManager->AlphaTestShaders[(int)mShaderName]; return &mShaderManager->AlphaTestShaders[(int)mShaderName];
@ -673,7 +685,7 @@ Shader* RenderDevice::GetActiveShader()
return &mShaderManager->Shaders[(int)mShaderName]; return &mShaderManager->Shaders[(int)mShaderName];
} }
void RenderDevice::SetShader(ShaderName name) void GLRenderDevice::SetShader(ShaderName name)
{ {
if (name != mShaderName) if (name != mShaderName)
{ {
@ -684,7 +696,7 @@ void RenderDevice::SetShader(ShaderName name)
} }
} }
void RenderDevice::SetUniform(UniformName name, const void* values, int count) void GLRenderDevice::SetUniform(UniformName name, const void* values, int count)
{ {
float* dest = mUniformData.data() + mUniformInfo[(int)name].Offset; float* dest = mUniformData.data() + mUniformInfo[(int)name].Offset;
if (memcmp(dest, values, sizeof(float) * count) != 0) if (memcmp(dest, values, sizeof(float) * count) != 0)
@ -696,7 +708,7 @@ void RenderDevice::SetUniform(UniformName name, const void* values, int count)
} }
} }
bool RenderDevice::ApplyChanges() bool GLRenderDevice::ApplyChanges()
{ {
if (mShaderChanged && !ApplyShader()) return false; if (mShaderChanged && !ApplyShader()) return false;
if (mVertexBufferChanged && !ApplyVertexBuffer()) return false; if (mVertexBufferChanged && !ApplyVertexBuffer()) return false;
@ -711,15 +723,15 @@ bool RenderDevice::ApplyChanges()
return true; return true;
} }
bool RenderDevice::ApplyViewport() bool GLRenderDevice::ApplyViewport()
{ {
glViewport(0, 0, mViewportWidth, mViewportHeight); glViewport(0, 0, mViewportWidth, mViewportHeight);
return CheckGLError(); return CheckGLError();
} }
bool RenderDevice::ApplyShader() bool GLRenderDevice::ApplyShader()
{ {
Shader* curShader = GetActiveShader(); GLShader* curShader = GetActiveShader();
if (!curShader->CheckCompile(this)) if (!curShader->CheckCompile(this))
{ {
SetError("Failed to bind shader:\r\n%s", curShader->GetCompileError().c_str()); SetError("Failed to bind shader:\r\n%s", curShader->GetCompileError().c_str());
@ -732,7 +744,7 @@ bool RenderDevice::ApplyShader()
return CheckGLError(); return CheckGLError();
} }
bool RenderDevice::ApplyRasterizerState() bool GLRenderDevice::ApplyRasterizerState()
{ {
if (mCullMode == Cull::None) if (mCullMode == Cull::None)
{ {
@ -752,7 +764,7 @@ bool RenderDevice::ApplyRasterizerState()
return CheckGLError(); return CheckGLError();
} }
bool RenderDevice::ApplyBlendState() bool GLRenderDevice::ApplyBlendState()
{ {
if (mAlphaBlend) if (mAlphaBlend)
{ {
@ -773,7 +785,7 @@ bool RenderDevice::ApplyBlendState()
return CheckGLError(); return CheckGLError();
} }
bool RenderDevice::ApplyDepthState() bool GLRenderDevice::ApplyDepthState()
{ {
if (mDepthTest) if (mDepthTest)
{ {
@ -791,7 +803,7 @@ bool RenderDevice::ApplyDepthState()
return CheckGLError(); return CheckGLError();
} }
bool RenderDevice::ApplyIndexBuffer() bool GLRenderDevice::ApplyIndexBuffer()
{ {
if (mIndexBuffer) if (mIndexBuffer)
{ {
@ -807,7 +819,7 @@ bool RenderDevice::ApplyIndexBuffer()
return CheckGLError(); return CheckGLError();
} }
bool RenderDevice::ApplyVertexBuffer() bool GLRenderDevice::ApplyVertexBuffer()
{ {
if (mVertexBuffer != -1) if (mVertexBuffer != -1)
glBindVertexArray(mSharedVertexBuffers[mVertexBuffer]->GetVAO()); glBindVertexArray(mSharedVertexBuffers[mVertexBuffer]->GetVAO());
@ -817,7 +829,7 @@ bool RenderDevice::ApplyVertexBuffer()
return CheckGLError(); return CheckGLError();
} }
void RenderDevice::DeclareUniform(UniformName name, const char* glslname, UniformType type) void GLRenderDevice::DeclareUniform(UniformName name, const char* glslname, UniformType type)
{ {
size_t index = (size_t)name; size_t index = (size_t)name;
if (mUniformInfo.size() <= index) if (mUniformInfo.size() <= index)
@ -831,9 +843,9 @@ void RenderDevice::DeclareUniform(UniformName name, const char* glslname, Unifor
mUniformData.resize(mUniformData.size() + (type == UniformType::Mat4 ? 16 : 4)); mUniformData.resize(mUniformData.size() + (type == UniformType::Mat4 ? 16 : 4));
} }
bool RenderDevice::ApplyUniforms() bool GLRenderDevice::ApplyUniforms()
{ {
Shader* shader = GetActiveShader(); GLShader* shader = GetActiveShader();
GLuint* locations = shader->UniformLocations.data(); GLuint* locations = shader->UniformLocations.data();
int* lastupdates = shader->UniformLastUpdates.data(); int* lastupdates = shader->UniformLastUpdates.data();
@ -862,7 +874,7 @@ bool RenderDevice::ApplyUniforms()
return CheckGLError(); return CheckGLError();
} }
bool RenderDevice::ApplyTextures() bool GLRenderDevice::ApplyTextures()
{ {
glActiveTexture(GL_TEXTURE0); glActiveTexture(GL_TEXTURE0);
if (mTextureUnit.Tex) if (mTextureUnit.Tex)
@ -900,42 +912,42 @@ bool RenderDevice::ApplyTextures()
return CheckGLError(); return CheckGLError();
} }
std::mutex& RenderDevice::GetMutex() std::mutex& GLRenderDevice::GetMutex()
{ {
static std::mutex m; static std::mutex m;
return m; return m;
} }
void RenderDevice::DeleteObject(VertexBuffer* buffer) void GLRenderDevice::DeleteObject(GLVertexBuffer* buffer)
{ {
std::unique_lock<std::mutex> lock(RenderDevice::GetMutex()); std::unique_lock<std::mutex> lock(GLRenderDevice::GetMutex());
if (buffer->Device) if (buffer->Device)
buffer->Device->mDeleteList.VertexBuffers.push_back(buffer); buffer->Device->mDeleteList.VertexBuffers.push_back(buffer);
else else
delete buffer; delete buffer;
} }
void RenderDevice::DeleteObject(IndexBuffer* buffer) void GLRenderDevice::DeleteObject(GLIndexBuffer* buffer)
{ {
std::unique_lock<std::mutex> lock(RenderDevice::GetMutex()); std::unique_lock<std::mutex> lock(GLRenderDevice::GetMutex());
if (buffer->Device) if (buffer->Device)
buffer->Device->mDeleteList.IndexBuffers.push_back(buffer); buffer->Device->mDeleteList.IndexBuffers.push_back(buffer);
else else
delete buffer; delete buffer;
} }
void RenderDevice::DeleteObject(Texture* texture) void GLRenderDevice::DeleteObject(GLTexture* texture)
{ {
std::unique_lock<std::mutex> lock(RenderDevice::GetMutex()); std::unique_lock<std::mutex> lock(GLRenderDevice::GetMutex());
if (texture->Device) if (texture->Device)
texture->Device->mDeleteList.Textures.push_back(texture); texture->Device->mDeleteList.Textures.push_back(texture);
else else
delete texture; delete texture;
} }
void RenderDevice::ProcessDeleteList() void GLRenderDevice::ProcessDeleteList()
{ {
std::unique_lock<std::mutex> lock(RenderDevice::GetMutex()); std::unique_lock<std::mutex> lock(GLRenderDevice::GetMutex());
for (auto buffer : mDeleteList.IndexBuffers) delete buffer; for (auto buffer : mDeleteList.IndexBuffers) delete buffer;
for (auto buffer : mDeleteList.VertexBuffers) delete buffer; for (auto buffer : mDeleteList.VertexBuffers) delete buffer;
for (auto texture : mDeleteList.Textures) delete texture; for (auto texture : mDeleteList.Textures) delete texture;
@ -943,204 +955,3 @@ void RenderDevice::ProcessDeleteList()
mDeleteList.VertexBuffers.clear(); mDeleteList.VertexBuffers.clear();
mDeleteList.Textures.clear(); mDeleteList.Textures.clear();
} }
/////////////////////////////////////////////////////////////////////////////
extern "C"
{
RenderDevice* RenderDevice_New(void* disp, void* window)
{
RenderDevice *device = new RenderDevice(disp, window);
if (!device->Context)
{
delete device;
return nullptr;
}
else
{
return device;
}
}
void RenderDevice_Delete(RenderDevice* device)
{
delete 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);
}
}

View file

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

View file

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

View file

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

View file

@ -20,9 +20,9 @@
*/ */
#include "Precomp.h" #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) 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); AlphaTestShaders[i].Setup(name, vs, ps, true);
} }
void ShaderManager::ReleaseResources() void GLShaderManager::ReleaseResources()
{ {
for (size_t i = 0; i < Shaders.size(); i++) for (size_t i = 0; i < Shaders.size(); i++)
{ {

View file

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

View file

@ -20,47 +20,47 @@
*/ */
#include "Precomp.h" #include "Precomp.h"
#include "Texture.h" #include "GLTexture.h"
#include "RenderDevice.h" #include "GLRenderDevice.h"
#include <stdexcept> #include <stdexcept>
Texture::Texture() GLTexture::GLTexture()
{ {
} }
Texture::~Texture() GLTexture::~GLTexture()
{ {
if (Device) if (Device)
Invalidate(); Invalidate();
} }
void Texture::Set2DImage(int width, int height) void GLTexture::Set2DImage(int width, int height)
{ {
mCubeTexture = false; mCubeTexture = false;
mWidth = width; mWidth = width;
mHeight = height; mHeight = height;
} }
void Texture::SetCubeImage(int size) void GLTexture::SetCubeImage(int size)
{ {
mCubeTexture = true; mCubeTexture = true;
mWidth = size; mWidth = size;
mHeight = size; mHeight = size;
} }
void Texture::SetPixels(const void* data) void GLTexture::SetPixels(const void* data)
{ {
mPixels[0].resize(mWidth * (size_t)mHeight); mPixels[0].resize(mWidth * (size_t)mHeight);
memcpy(mPixels[0].data(), data, sizeof(uint32_t) * mWidth * 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); mPixels[(int)face].resize(mWidth * (size_t)mHeight);
memcpy(mPixels[(int)face].data(), data, sizeof(uint32_t) * mWidth * mHeight); memcpy(mPixels[(int)face].data(), data, sizeof(uint32_t) * mWidth * mHeight);
} }
void Texture::Invalidate() void GLTexture::Invalidate()
{ {
if (mDepthRenderbuffer) glDeleteRenderbuffers(1, &mDepthRenderbuffer); if (mDepthRenderbuffer) glDeleteRenderbuffers(1, &mDepthRenderbuffer);
if (mFramebuffer) glDeleteFramebuffers(1, &mFramebuffer); if (mFramebuffer) glDeleteFramebuffers(1, &mFramebuffer);
@ -73,7 +73,7 @@ void Texture::Invalidate()
Device = nullptr; Device = nullptr;
} }
GLuint Texture::GetTexture(RenderDevice* device) GLuint GLTexture::GetTexture(GLRenderDevice* device)
{ {
if (mTexture == 0) if (mTexture == 0)
{ {
@ -122,7 +122,7 @@ GLuint Texture::GetTexture(RenderDevice* device)
return mTexture; return mTexture;
} }
GLuint Texture::GetFramebuffer(RenderDevice* device, bool usedepthbuffer) GLuint GLTexture::GetFramebuffer(GLRenderDevice* device, bool usedepthbuffer)
{ {
if (!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) if (mPBO == 0)
{ {
@ -177,30 +177,3 @@ GLuint Texture::GetPBO(RenderDevice* device)
return mPBO; 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 #pragma once
enum class CubeMapFace : int #include "../Backend.h"
{
PositiveX,
PositiveY,
PositiveZ,
NegativeX,
NegativeY,
NegativeZ
};
class RenderDevice; class GLRenderDevice;
class Texture class GLTexture : public Texture
{ {
public: public:
Texture(); GLTexture();
~Texture(); ~GLTexture();
void Set2DImage(int width, int height); void Set2DImage(int width, int height) override;
void SetCubeImage(int size); void SetCubeImage(int size) override;
void SetPixels(const void* data); void SetPixels(const void* data);
void SetCubePixels(CubeMapFace face, const void* data); void SetCubePixels(CubeMapFace face, const void* data);
@ -52,11 +44,11 @@ public:
bool IsTextureCreated() const { return mTexture; } bool IsTextureCreated() const { return mTexture; }
void Invalidate(); void Invalidate();
GLuint GetTexture(RenderDevice* device); GLuint GetTexture(GLRenderDevice* device);
GLuint GetFramebuffer(RenderDevice* device, bool usedepthbuffer); GLuint GetFramebuffer(GLRenderDevice* device, bool usedepthbuffer);
GLuint GetPBO(RenderDevice* device); GLuint GetPBO(GLRenderDevice* device);
RenderDevice* Device = nullptr; GLRenderDevice* Device = nullptr;
private: private:
int mWidth = 0; int mWidth = 0;
@ -67,6 +59,5 @@ private:
GLuint mTexture = 0; GLuint mTexture = 0;
GLuint mFramebuffer = 0; GLuint mFramebuffer = 0;
GLuint mDepthRenderbuffer = 0; GLuint mDepthRenderbuffer = 0;
//
GLuint mPBO = 0; GLuint mPBO = 0;
}; };

View file

@ -20,22 +20,18 @@
*/ */
#include "Precomp.h" #include "Precomp.h"
#include "VertexBuffer.h" #include "GLVertexBuffer.h"
#include "Shader.h" #include "GLShader.h"
#include "RenderDevice.h" #include "GLRenderDevice.h"
SharedVertexBuffer::SharedVertexBuffer(VertexFormat format, int size) : Format(format), Size(size) GLuint GLSharedVertexBuffer::GetBuffer()
{
}
GLuint SharedVertexBuffer::GetBuffer()
{ {
if (mBuffer == 0) if (mBuffer == 0)
glGenBuffers(1, &mBuffer); glGenBuffers(1, &mBuffer);
return mBuffer; return mBuffer;
} }
GLuint SharedVertexBuffer::GetVAO() GLuint GLSharedVertexBuffer::GetVAO()
{ {
if (!mVAO) if (!mVAO)
{ {
@ -51,31 +47,31 @@ GLuint SharedVertexBuffer::GetVAO()
return mVAO; return mVAO;
} }
void SharedVertexBuffer::SetupFlatVAO() void GLSharedVertexBuffer::SetupFlatVAO()
{ {
glEnableVertexAttribArray((int)DeclarationUsage::Position); glEnableVertexAttribArray((int)DeclarationUsage::Position);
glEnableVertexAttribArray((int)DeclarationUsage::Color); glEnableVertexAttribArray((int)DeclarationUsage::Color);
glEnableVertexAttribArray((int)DeclarationUsage::TextureCoordinate); glEnableVertexAttribArray((int)DeclarationUsage::TextureCoordinate);
glVertexAttribPointer((int)DeclarationUsage::Position, 3, GL_FLOAT, GL_FALSE, FlatStride, (const void*)0); 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, FlatStride, (const void*)12); 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, FlatStride, (const void*)16); 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::Position);
glEnableVertexAttribArray((int)DeclarationUsage::Color); glEnableVertexAttribArray((int)DeclarationUsage::Color);
glEnableVertexAttribArray((int)DeclarationUsage::TextureCoordinate); glEnableVertexAttribArray((int)DeclarationUsage::TextureCoordinate);
glEnableVertexAttribArray((int)DeclarationUsage::Normal); glEnableVertexAttribArray((int)DeclarationUsage::Normal);
glVertexAttribPointer((int)DeclarationUsage::Position, 3, GL_FLOAT, GL_FALSE, WorldStride, (const void*)0); 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, WorldStride, (const void*)12); 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, WorldStride, (const void*)16); glVertexAttribPointer((int)DeclarationUsage::TextureCoordinate, 2, GL_FLOAT, GL_FALSE, VertexBuffer::WorldStride, (const void*)16);
glVertexAttribPointer((int)DeclarationUsage::Normal, 3, GL_FLOAT, GL_FALSE, WorldStride, (const void*)24); glVertexAttribPointer((int)DeclarationUsage::Normal, 3, GL_FLOAT, GL_FALSE, VertexBuffer::WorldStride, (const void*)24);
} }
///////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////
VertexBuffer::~VertexBuffer() GLVertexBuffer::~GLVertexBuffer()
{ {
if (Device) if (Device)
{ {
@ -83,20 +79,3 @@ VertexBuffer::~VertexBuffer()
Device = nullptr; 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> #include <list>
enum class VertexFormat : int32_t { Flat, World }; #include "../Backend.h"
class RenderDevice; class GLRenderDevice;
class VertexBuffer; class GLVertexBuffer;
class SharedVertexBuffer class GLSharedVertexBuffer
{ {
public: public:
SharedVertexBuffer(VertexFormat format, int size); GLSharedVertexBuffer(VertexFormat format, int size) : Format(format), Size(size) { }
GLuint GetBuffer(); GLuint GetBuffer();
GLuint GetVAO(); GLuint GetVAO();
@ -41,10 +41,7 @@ public:
int NextPos = 0; int NextPos = 0;
int Size = 0; int Size = 0;
std::list<VertexBuffer*> VertexBuffers; std::list<GLVertexBuffer*> VertexBuffers;
static const int FlatStride = 24;
static const int WorldStride = 36;
static void SetupFlatVAO(); static void SetupFlatVAO();
static void SetupWorldVAO(); static void SetupWorldVAO();
@ -54,15 +51,15 @@ private:
GLuint mVAO = 0; GLuint mVAO = 0;
}; };
class VertexBuffer class GLVertexBuffer : public VertexBuffer
{ {
public: public:
~VertexBuffer(); ~GLVertexBuffer();
VertexFormat Format = VertexFormat::Flat; VertexFormat Format = VertexFormat::Flat;
RenderDevice* Device = nullptr; GLRenderDevice* Device = nullptr;
std::list<VertexBuffer*>::iterator ListIt; std::list<GLVertexBuffer*>::iterator ListIt;
int BufferOffset = 0; int BufferOffset = 0;
int BufferStartIndex = 0; int BufferStartIndex = 0;

View file

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

View file

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