2019-12-22 23:13:09 +00:00
|
|
|
/*
|
|
|
|
** 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.
|
|
|
|
*/
|
|
|
|
|
2019-08-09 04:18:08 +00:00
|
|
|
#pragma once
|
|
|
|
|
2019-12-23 19:09:38 +00:00
|
|
|
#include "../Backend.h"
|
2019-08-10 00:32:08 +00:00
|
|
|
#include "OpenGLContext.h"
|
2019-12-23 19:09:38 +00:00
|
|
|
|
|
|
|
class GLSharedVertexBuffer;
|
|
|
|
class GLShader;
|
|
|
|
class GLShaderManager;
|
|
|
|
class GLVertexBuffer;
|
|
|
|
class GLIndexBuffer;
|
|
|
|
class GLTexture;
|
|
|
|
|
|
|
|
class GLRenderDevice : public RenderDevice
|
2019-08-09 04:18:08 +00:00
|
|
|
{
|
|
|
|
public:
|
2019-12-23 19:09:38 +00:00
|
|
|
GLRenderDevice(void* disp, void* window);
|
|
|
|
~GLRenderDevice();
|
|
|
|
|
|
|
|
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) 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) override;
|
|
|
|
bool SetCubePixels(Texture* texture, CubeMapFace face, const void* data) override;
|
|
|
|
void* MapPBO(Texture* texture) override;
|
|
|
|
bool UnmapPBO(Texture* texture) override;
|
|
|
|
|
|
|
|
bool InvalidateTexture(GLTexture* texture);
|
2019-08-15 00:52:21 +00:00
|
|
|
|
2019-12-22 22:44:58 +00:00
|
|
|
void GarbageCollectBuffer(int size, VertexFormat format);
|
|
|
|
|
2019-12-20 02:39:06 +00:00
|
|
|
bool ApplyViewport();
|
|
|
|
bool ApplyChanges();
|
|
|
|
bool ApplyVertexBuffer();
|
|
|
|
bool ApplyIndexBuffer();
|
|
|
|
bool ApplyShader();
|
|
|
|
bool ApplyUniforms();
|
|
|
|
bool ApplyTextures();
|
|
|
|
bool ApplyRasterizerState();
|
|
|
|
bool ApplyBlendState();
|
|
|
|
bool ApplyDepthState();
|
2019-08-10 05:46:29 +00:00
|
|
|
|
2019-12-16 04:54:44 +00:00
|
|
|
bool CheckGLError();
|
2019-12-15 21:53:33 +00:00
|
|
|
void SetError(const char* fmt, ...);
|
|
|
|
const char* GetError();
|
2019-08-12 06:33:40 +00:00
|
|
|
|
2019-12-23 19:09:38 +00:00
|
|
|
GLShader* GetActiveShader();
|
2019-08-16 02:10:03 +00:00
|
|
|
|
2019-08-14 11:51:05 +00:00
|
|
|
GLint GetGLMinFilter(TextureFilter filter, TextureFilter mipfilter);
|
|
|
|
|
2019-12-22 22:44:58 +00:00
|
|
|
static std::mutex& GetMutex();
|
2019-12-23 19:09:38 +00:00
|
|
|
static void DeleteObject(GLVertexBuffer* buffer);
|
|
|
|
static void DeleteObject(GLIndexBuffer* buffer);
|
|
|
|
static void DeleteObject(GLTexture* texture);
|
2019-12-22 22:44:58 +00:00
|
|
|
|
|
|
|
void ProcessDeleteList();
|
|
|
|
|
2019-08-30 06:45:14 +00:00
|
|
|
std::unique_ptr<IOpenGLContext> Context;
|
2019-08-10 05:46:29 +00:00
|
|
|
|
2019-12-22 22:44:58 +00:00
|
|
|
struct DeleteList
|
|
|
|
{
|
2019-12-23 19:09:38 +00:00
|
|
|
std::vector<GLVertexBuffer*> VertexBuffers;
|
|
|
|
std::vector<GLIndexBuffer*> IndexBuffers;
|
|
|
|
std::vector<GLTexture*> Textures;
|
2019-12-22 22:44:58 +00:00
|
|
|
} mDeleteList;
|
|
|
|
|
2019-08-14 11:51:05 +00:00
|
|
|
struct TextureUnit
|
2019-08-10 05:46:29 +00:00
|
|
|
{
|
2019-12-23 19:09:38 +00:00
|
|
|
GLTexture* Tex = nullptr;
|
2019-12-18 02:24:09 +00:00
|
|
|
TextureAddress WrapMode = TextureAddress::Wrap;
|
|
|
|
GLuint SamplerHandle = 0;
|
2019-08-17 01:21:11 +00:00
|
|
|
} mTextureUnit;
|
2019-08-12 06:33:40 +00:00
|
|
|
|
2019-12-18 02:24:09 +00:00
|
|
|
struct SamplerFilterKey
|
|
|
|
{
|
|
|
|
GLuint MinFilter = 0;
|
|
|
|
GLuint MagFilter = 0;
|
|
|
|
float MaxAnisotropy = 0.0f;
|
|
|
|
|
|
|
|
bool operator<(const SamplerFilterKey& b) const { return memcmp(this, &b, sizeof(SamplerFilterKey)) < 0; }
|
|
|
|
bool operator==(const SamplerFilterKey& b) const { return memcmp(this, &b, sizeof(SamplerFilterKey)) == 0; }
|
|
|
|
bool operator!=(const SamplerFilterKey& b) const { return memcmp(this, &b, sizeof(SamplerFilterKey)) != 0; }
|
|
|
|
};
|
|
|
|
|
|
|
|
struct SamplerFilter
|
|
|
|
{
|
|
|
|
GLuint WrapModes[2] = { 0, 0 };
|
|
|
|
};
|
|
|
|
|
|
|
|
std::map<SamplerFilterKey, SamplerFilter> mSamplers;
|
|
|
|
SamplerFilterKey mSamplerFilterKey;
|
|
|
|
SamplerFilter* mSamplerFilter = nullptr;
|
|
|
|
|
2019-12-18 01:27:49 +00:00
|
|
|
int mVertexBuffer = -1;
|
|
|
|
int64_t mVertexBufferStartIndex = 0;
|
|
|
|
|
2019-12-23 19:09:38 +00:00
|
|
|
GLIndexBuffer* mIndexBuffer = nullptr;
|
2019-08-12 06:33:40 +00:00
|
|
|
|
2019-12-23 19:09:38 +00:00
|
|
|
std::unique_ptr<GLSharedVertexBuffer> mSharedVertexBuffers[2];
|
2019-12-18 01:27:49 +00:00
|
|
|
|
2019-12-23 19:09:38 +00:00
|
|
|
std::unique_ptr<GLShaderManager> mShaderManager;
|
2019-09-29 16:57:51 +00:00
|
|
|
ShaderName mShaderName = {};
|
2019-08-14 10:36:33 +00:00
|
|
|
|
2019-12-18 03:22:47 +00:00
|
|
|
struct UniformInfo
|
|
|
|
{
|
|
|
|
std::string Name;
|
|
|
|
UniformType Type = {};
|
|
|
|
int Offset = 0;
|
|
|
|
int LastUpdate = 0;
|
|
|
|
};
|
|
|
|
|
2019-12-21 01:14:42 +00:00
|
|
|
std::vector<UniformInfo> mUniformInfo;
|
2019-12-18 03:22:47 +00:00
|
|
|
std::vector<float> mUniformData;
|
|
|
|
|
2019-08-16 05:45:34 +00:00
|
|
|
GLuint mStreamVertexBuffer = 0;
|
2019-08-16 11:07:57 +00:00
|
|
|
GLuint mStreamVAO = 0;
|
2019-08-16 05:45:34 +00:00
|
|
|
|
2019-08-12 06:33:40 +00:00
|
|
|
Cull mCullMode = Cull::None;
|
|
|
|
FillMode mFillMode = FillMode::Solid;
|
|
|
|
bool mAlphaTest = false;
|
|
|
|
|
|
|
|
bool mAlphaBlend = false;
|
|
|
|
BlendOperation mBlendOperation = BlendOperation::Add;
|
|
|
|
Blend mSourceBlend = Blend::SourceAlpha;
|
|
|
|
Blend mDestinationBlend = Blend::InverseSourceAlpha;
|
|
|
|
|
|
|
|
bool mDepthTest = false;
|
|
|
|
bool mDepthWrite = false;
|
|
|
|
|
2019-08-10 05:46:29 +00:00
|
|
|
bool mNeedApply = true;
|
2019-08-22 13:46:24 +00:00
|
|
|
bool mShaderChanged = true;
|
|
|
|
bool mUniformsChanged = true;
|
|
|
|
bool mTexturesChanged = true;
|
|
|
|
bool mIndexBufferChanged = true;
|
|
|
|
bool mVertexBufferChanged = true;
|
|
|
|
bool mDepthStateChanged = true;
|
|
|
|
bool mBlendStateChanged = true;
|
|
|
|
bool mRasterizerStateChanged = true;
|
|
|
|
|
|
|
|
bool mContextIsCurrent = false;
|
2019-12-15 16:48:38 +00:00
|
|
|
|
2019-12-20 02:39:06 +00:00
|
|
|
std::string mLastError;
|
|
|
|
std::string mReturnError;
|
|
|
|
char mSetErrorBuffer[4096];
|
2019-12-15 21:53:33 +00:00
|
|
|
|
2019-12-15 16:48:38 +00:00
|
|
|
int mViewportWidth = 0;
|
|
|
|
int mViewportHeight = 0;
|
2019-08-09 04:18:08 +00:00
|
|
|
};
|