boo/lib/graphicsdev/GL.cpp

1614 lines
55 KiB
C++
Raw Normal View History

#include "boo/graphicsdev/GL.hpp"
#include "boo/graphicsdev/glew.h"
2017-11-08 02:24:07 +00:00
#include "boo/IApplication.hpp"
#include "Common.hpp"
#include <thread>
#include <condition_variable>
2015-11-30 02:37:46 +00:00
#include <array>
2016-12-11 06:18:41 +00:00
#include <unordered_map>
#include <unordered_set>
#include "xxhash.h"
2017-11-09 08:10:43 +00:00
#if _WIN32
#include "../win/WinCommon.hpp"
#endif
2016-03-04 23:02:18 +00:00
#include "logvisor/logvisor.hpp"
2015-10-30 06:26:02 +00:00
2015-12-02 22:04:24 +00:00
#undef min
#undef max
namespace boo
{
2016-03-04 23:02:18 +00:00
static logvisor::Module Log("boo::GL");
class GLDataFactoryImpl;
struct GLShareableShader : IShareableShader<GLDataFactoryImpl, GLShareableShader>
{
GLuint m_shader = 0;
2017-03-05 22:59:58 +00:00
GLShareableShader(GLDataFactoryImpl& fac, uint64_t srcKey, GLuint s)
: IShareableShader(fac, srcKey, 0), m_shader(s) {}
~GLShareableShader() { glDeleteShader(m_shader); }
};
class GLDataFactoryImpl : public GLDataFactory, public GraphicsDataFactoryHead
{
friend struct GLCommandQueue;
friend class GLDataFactory::Context;
IGraphicsContext* m_parent;
uint32_t m_drawSamples;
std::unordered_map<uint64_t, std::unique_ptr<GLShareableShader>> m_sharedShaders;
public:
GLDataFactoryImpl(IGraphicsContext* parent, uint32_t drawSamples)
: m_parent(parent), m_drawSamples(drawSamples) {}
Platform platform() const { return Platform::OpenGL; }
const SystemChar* platformName() const { return _S("OpenGL"); }
void commitTransaction(const FactoryCommitFunc&);
ObjToken<IGraphicsBufferD> newPoolBuffer(BufferUse use, size_t stride, size_t count);
void _unregisterShareableShader(uint64_t srcKey, uint64_t binKey) { m_sharedShaders.erase(srcKey); }
2016-12-10 02:31:50 +00:00
};
static const GLenum USE_TABLE[] =
{
GL_INVALID_ENUM,
GL_ARRAY_BUFFER,
GL_ELEMENT_ARRAY_BUFFER,
GL_UNIFORM_BUFFER
};
class GLGraphicsBufferS : public GraphicsDataNode<IGraphicsBufferS>
{
friend class GLDataFactory;
friend struct GLCommandQueue;
GLuint m_buf;
GLenum m_target;
GLGraphicsBufferS(const ObjToken<BaseGraphicsData>& parent, BufferUse use, const void* data, size_t sz)
: GraphicsDataNode<IGraphicsBufferS>(parent)
{
2015-11-21 01:12:22 +00:00
m_target = USE_TABLE[int(use)];
glGenBuffers(1, &m_buf);
glBindBuffer(m_target, m_buf);
glBufferData(m_target, sz, data, GL_STATIC_DRAW);
}
public:
~GLGraphicsBufferS() { glDeleteBuffers(1, &m_buf); }
2015-10-30 00:00:56 +00:00
void bindVertex() const
{glBindBuffer(GL_ARRAY_BUFFER, m_buf);}
void bindIndex() const
{glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_buf);}
void bindUniform(size_t idx) const
{glBindBufferBase(GL_UNIFORM_BUFFER, idx, m_buf);}
void bindUniformRange(size_t idx, GLintptr off, GLsizeiptr size) const
{glBindBufferRange(GL_UNIFORM_BUFFER, idx, m_buf, off, size);}
};
template<class DataCls>
class GLGraphicsBufferD : public GraphicsDataNode<IGraphicsBufferD, DataCls>
{
friend class GLDataFactory;
friend class GLDataFactoryImpl;
friend struct GLCommandQueue;
2015-10-30 00:00:56 +00:00
GLuint m_bufs[3];
GLenum m_target;
std::unique_ptr<uint8_t[]> m_cpuBuf;
size_t m_cpuSz = 0;
int m_validMask = 0;
GLGraphicsBufferD(const ObjToken<DataCls>& parent, BufferUse use, size_t sz)
: GraphicsDataNode<IGraphicsBufferD, DataCls>(parent),
m_target(USE_TABLE[int(use)]), m_cpuBuf(new uint8_t[sz]), m_cpuSz(sz)
{
2015-10-30 00:00:56 +00:00
glGenBuffers(3, m_bufs);
for (int i=0 ; i<3 ; ++i)
{
glBindBuffer(m_target, m_bufs[i]);
glBufferData(m_target, m_cpuSz, nullptr, GL_STREAM_DRAW);
}
}
public:
~GLGraphicsBufferD() { glDeleteBuffers(3, m_bufs); }
void update(int b)
{
int slot = 1 << b;
if ((slot & m_validMask) == 0)
{
glBindBuffer(m_target, m_bufs[b]);
glBufferSubData(m_target, 0, m_cpuSz, m_cpuBuf.get());
m_validMask |= slot;
}
}
2015-10-30 00:00:56 +00:00
void load(const void* data, size_t sz)
{
size_t bufSz = std::min(sz, m_cpuSz);
memcpy(m_cpuBuf.get(), data, bufSz);
m_validMask = 0;
}
void* map(size_t sz)
{
if (sz < m_cpuSz)
return nullptr;
return m_cpuBuf.get();
}
void unmap()
{
m_validMask = 0;
}
void bindVertex(int b)
{glBindBuffer(GL_ARRAY_BUFFER, m_bufs[b]);}
void bindIndex(int b)
{glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_bufs[b]);}
void bindUniform(size_t idx, int b)
{glBindBufferBase(GL_UNIFORM_BUFFER, idx, m_bufs[b]);}
void bindUniformRange(size_t idx, GLintptr off, GLsizeiptr size, int b)
{glBindBufferRange(GL_UNIFORM_BUFFER, idx, m_bufs[b], off, size);}
};
ObjToken<IGraphicsBufferS>
GLDataFactory::Context::newStaticBuffer(BufferUse use, const void* data, size_t stride, size_t count)
{
return {new GLGraphicsBufferS(m_data, use, data, stride * count)};
}
2017-10-01 04:23:28 +00:00
static void SetClampMode(GLenum target, TextureClampMode clampMode)
{
switch (clampMode)
{
case TextureClampMode::Repeat:
{
glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(target, GL_TEXTURE_WRAP_R, GL_REPEAT);
break;
}
case TextureClampMode::ClampToWhite:
{
glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
glTexParameteri(target, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_BORDER);
GLfloat color[] = {1.f, 1.f, 1.f, 1.f};
glTexParameterfv(target, GL_TEXTURE_BORDER_COLOR, color);
break;
}
}
}
class GLTextureS : public GraphicsDataNode<ITextureS>
{
friend class GLDataFactory;
GLuint m_tex;
GLTextureS(const ObjToken<BaseGraphicsData>& parent, size_t width, size_t height, size_t mips,
2017-10-01 04:23:28 +00:00
TextureFormat fmt, TextureClampMode clampMode, const void* data, size_t sz)
: GraphicsDataNode<ITextureS>(parent)
{
const uint8_t* dataIt = static_cast<const uint8_t*>(data);
glGenTextures(1, &m_tex);
glBindTexture(GL_TEXTURE_2D, m_tex);
2015-11-01 00:06:56 +00:00
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
if (mips > 1)
2016-02-16 19:41:16 +00:00
{
2015-11-01 00:06:56 +00:00
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
2016-02-16 19:41:16 +00:00
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, mips-1);
}
2015-11-01 00:06:56 +00:00
else
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
2017-10-01 04:23:28 +00:00
SetClampMode(GL_TEXTURE_2D, clampMode);
GLenum intFormat, format;
int pxPitch;
bool compressed = false;
switch (fmt)
{
case TextureFormat::RGBA8:
2017-01-29 03:56:17 +00:00
intFormat = GL_RGBA8;
format = GL_RGBA;
pxPitch = 4;
break;
case TextureFormat::I8:
intFormat = GL_R8;
format = GL_RED;
pxPitch = 1;
break;
case TextureFormat::DXT1:
2016-02-16 19:41:16 +00:00
intFormat = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
compressed = true;
break;
default:
2016-03-04 23:02:18 +00:00
Log.report(logvisor::Fatal, "unsupported tex format");
}
if (compressed)
{
for (size_t i=0 ; i<mips ; ++i)
{
size_t dataSz = width * height / 2;
glCompressedTexImage2D(GL_TEXTURE_2D, i, intFormat, width, height, 0, dataSz, dataIt);
dataIt += dataSz;
2017-01-29 03:56:17 +00:00
if (width > 1)
width /= 2;
if (height > 1)
height /= 2;
}
}
else
{
for (size_t i=0 ; i<mips ; ++i)
{
glTexImage2D(GL_TEXTURE_2D, i, intFormat, width, height, 0, format, GL_UNSIGNED_BYTE, dataIt);
dataIt += width * height * pxPitch;
2017-01-29 03:56:17 +00:00
if (width > 1)
width /= 2;
if (height > 1)
height /= 2;
}
}
}
public:
~GLTextureS() { glDeleteTextures(1, &m_tex); }
2015-10-30 00:00:56 +00:00
void bind(size_t idx) const
{
glActiveTexture(GL_TEXTURE0 + idx);
glBindTexture(GL_TEXTURE_2D, m_tex);
}
};
class GLTextureSA : public GraphicsDataNode<ITextureSA>
{
friend class GLDataFactory;
GLuint m_tex;
GLTextureSA(const ObjToken<BaseGraphicsData>& parent, size_t width, size_t height, size_t layers, size_t mips,
2017-10-01 04:23:28 +00:00
TextureFormat fmt, TextureClampMode clampMode, const void* data, size_t sz)
: GraphicsDataNode<ITextureSA>(parent)
{
2017-01-29 03:56:17 +00:00
const uint8_t* dataIt = static_cast<const uint8_t*>(data);
glGenTextures(1, &m_tex);
glBindTexture(GL_TEXTURE_2D_ARRAY, m_tex);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2017-01-29 03:56:17 +00:00
if (mips > 1)
{
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAX_LEVEL, mips-1);
}
else
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
2017-10-01 04:23:28 +00:00
SetClampMode(GL_TEXTURE_2D_ARRAY, clampMode);
2017-01-29 03:56:17 +00:00
GLenum intFormat, format;
int pxPitch;
if (fmt == TextureFormat::RGBA8)
2017-01-29 03:56:17 +00:00
{
intFormat = GL_RGBA8;
format = GL_RGBA;
pxPitch = 4;
}
else if (fmt == TextureFormat::I8)
2017-01-29 03:56:17 +00:00
{
intFormat = GL_R8;
format = GL_RED;
pxPitch = 1;
}
for (size_t i=0 ; i<mips ; ++i)
{
glTexImage3D(GL_TEXTURE_2D_ARRAY, i, intFormat, width, height, layers, 0, format, GL_UNSIGNED_BYTE, dataIt);
dataIt += width * height * layers * pxPitch;
if (width > 1)
width /= 2;
if (height > 1)
height /= 2;
}
}
public:
~GLTextureSA() { glDeleteTextures(1, &m_tex); }
void bind(size_t idx) const
{
glActiveTexture(GL_TEXTURE0 + idx);
glBindTexture(GL_TEXTURE_2D_ARRAY, m_tex);
}
};
class GLTextureD : public GraphicsDataNode<ITextureD>
{
friend class GLDataFactory;
friend struct GLCommandQueue;
2015-11-04 01:02:05 +00:00
GLuint m_texs[3];
std::unique_ptr<uint8_t[]> m_cpuBuf;
size_t m_cpuSz = 0;
GLenum m_intFormat, m_format;
size_t m_width = 0;
size_t m_height = 0;
int m_validMask = 0;
GLTextureD(const ObjToken<BaseGraphicsData>& parent, size_t width, size_t height,
TextureFormat fmt, TextureClampMode clampMode)
: GraphicsDataNode<ITextureD>(parent), m_width(width), m_height(height)
{
int pxPitch = 4;
switch (fmt)
{
case TextureFormat::RGBA8:
m_intFormat = GL_RGBA8;
m_format = GL_RGBA;
pxPitch = 4;
break;
case TextureFormat::I8:
m_intFormat = GL_R8;
m_format = GL_RED;
pxPitch = 1;
break;
default:
Log.report(logvisor::Fatal, "unsupported tex format");
}
m_cpuSz = width * height * pxPitch;
m_cpuBuf.reset(new uint8_t[m_cpuSz]);
glGenTextures(3, m_texs);
for (int i=0 ; i<3 ; ++i)
{
glBindTexture(GL_TEXTURE_2D, m_texs[i]);
glTexImage2D(GL_TEXTURE_2D, 0, m_intFormat, width, height, 0, m_format, GL_UNSIGNED_BYTE, nullptr);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
SetClampMode(GL_TEXTURE_2D, clampMode);
}
}
public:
~GLTextureD() { glDeleteTextures(3, m_texs); }
void update(int b)
{
int slot = 1 << b;
if ((slot & m_validMask) == 0)
{
glBindTexture(GL_TEXTURE_2D, m_texs[b]);
glTexImage2D(GL_TEXTURE_2D, 0, m_intFormat, m_width, m_height, 0, m_format, GL_UNSIGNED_BYTE, m_cpuBuf.get());
m_validMask |= slot;
}
}
void load(const void* data, size_t sz)
{
size_t bufSz = std::min(sz, m_cpuSz);
memcpy(m_cpuBuf.get(), data, bufSz);
m_validMask = 0;
}
void* map(size_t sz)
{
if (sz > m_cpuSz)
return nullptr;
return m_cpuBuf.get();
}
void unmap()
{
m_validMask = 0;
}
2015-11-04 01:02:05 +00:00
void bind(size_t idx, int b)
{
glActiveTexture(GL_TEXTURE0 + idx);
glBindTexture(GL_TEXTURE_2D, m_texs[b]);
}
2015-11-04 01:02:05 +00:00
};
#define MAX_BIND_TEXS 4
class GLTextureR : public GraphicsDataNode<ITextureR>
2015-11-04 01:02:05 +00:00
{
friend class GLDataFactory;
friend struct GLCommandQueue;
struct GLCommandQueue* m_q;
2016-04-02 00:03:36 +00:00
GLuint m_texs[2] = {};
GLuint m_bindTexs[2][MAX_BIND_TEXS] = {};
2015-11-04 01:02:05 +00:00
GLuint m_fbo = 0;
size_t m_width = 0;
size_t m_height = 0;
size_t m_samples = 0;
GLenum m_target;
GLTextureR(const ObjToken<BaseGraphicsData>& parent, GLCommandQueue* q, size_t width, size_t height, size_t samples,
2017-10-01 04:23:28 +00:00
TextureClampMode clampMode, size_t colorBindCount, size_t depthBindCount);
2015-11-04 01:02:05 +00:00
public:
~GLTextureR()
{
glDeleteTextures(2, m_texs);
glDeleteTextures(MAX_BIND_TEXS * 2, m_bindTexs[0]);
glDeleteFramebuffers(1, &m_fbo);
}
2015-10-30 00:00:56 +00:00
void bind(size_t idx, int bindIdx, bool depth) const
2015-10-30 00:00:56 +00:00
{
glActiveTexture(GL_TEXTURE0 + idx);
glBindTexture(m_target, m_bindTexs[depth][bindIdx]);
2015-10-30 00:00:56 +00:00
}
2016-02-16 19:41:16 +00:00
void resize(size_t width, size_t height)
{
m_width = width;
m_height = height;
2016-02-16 19:41:16 +00:00
if (m_samples > 1)
{
glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, m_texs[0]);
glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, m_samples, GL_RGBA, width, height, GL_FALSE);
glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, m_texs[1]);
glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, m_samples, GL_DEPTH_COMPONENT24, width, height, GL_FALSE);
}
else
{
glBindTexture(GL_TEXTURE_2D, m_texs[0]);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
glBindTexture(GL_TEXTURE_2D, m_texs[1]);
2016-04-02 04:40:31 +00:00
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24, width, height, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, nullptr);
2016-04-02 00:03:36 +00:00
2016-04-02 04:40:31 +00:00
glBindFramebuffer(GL_FRAMEBUFFER, m_fbo);
glDepthMask(GL_TRUE);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
2017-10-01 04:23:28 +00:00
}
2017-10-01 04:23:28 +00:00
for (int i=0 ; i<MAX_BIND_TEXS ; ++i)
{
if (m_bindTexs[0][i])
{
2017-10-01 04:23:28 +00:00
glBindTexture(GL_TEXTURE_2D, m_bindTexs[0][i]);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
}
2017-10-01 04:23:28 +00:00
}
2017-10-01 04:23:28 +00:00
for (int i=0 ; i<MAX_BIND_TEXS ; ++i)
{
if (m_bindTexs[1][i])
{
2017-10-01 04:23:28 +00:00
glBindTexture(GL_TEXTURE_2D, m_bindTexs[1][i]);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24, width, height, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, nullptr);
}
}
}
};
ObjToken<ITextureS>
GLDataFactory::Context::newStaticTexture(size_t width, size_t height, size_t mips, TextureFormat fmt,
2017-10-01 04:23:28 +00:00
TextureClampMode clampMode, const void* data, size_t sz)
{
return {new GLTextureS(m_data, width, height, mips, fmt, clampMode, data, sz)};
}
ObjToken<ITextureSA>
2017-01-29 03:56:17 +00:00
GLDataFactory::Context::newStaticArrayTexture(size_t width, size_t height, size_t layers, size_t mips,
TextureFormat fmt, TextureClampMode clampMode,
const void *data, size_t sz)
{
return {new GLTextureSA(m_data, width, height, layers, mips, fmt, clampMode, data, sz)};
}
class GLShaderPipeline : public GraphicsDataNode<IShaderPipeline>
{
friend class GLDataFactory;
2016-04-03 03:30:44 +00:00
friend struct GLCommandQueue;
2015-11-16 04:29:31 +00:00
friend struct GLShaderDataBinding;
mutable GLShareableShader::Token m_vert;
mutable GLShareableShader::Token m_frag;
mutable GLuint m_prog = 0;
GLenum m_sfactor = GL_ONE;
GLenum m_dfactor = GL_ZERO;
GLenum m_drawPrim = GL_TRIANGLES;
ZTest m_depthTest = ZTest::LEqual;
2015-10-30 00:00:56 +00:00
bool m_depthWrite = true;
bool m_colorWrite = true;
bool m_alphaWrite = true;
bool m_subtractBlend = false;
2017-03-10 20:38:00 +00:00
CullMode m_culling;
mutable std::vector<GLint> m_uniLocs;
mutable std::vector<std::string> m_texNames;
mutable std::vector<std::string> m_blockNames;
GLShaderPipeline(const ObjToken<BaseGraphicsData>& parent)
: GraphicsDataNode<IShaderPipeline>(parent) {}
public:
2017-03-05 23:34:24 +00:00
~GLShaderPipeline() { if (m_prog) glDeleteProgram(m_prog); }
2015-10-30 00:00:56 +00:00
GLuint bind() const
{
if (!m_prog)
{
m_prog = glCreateProgram();
if (!m_prog)
{
Log.report(logvisor::Error, "unable to create shader program");
return 0;
}
glAttachShader(m_prog, m_vert.get().m_shader);
glAttachShader(m_prog, m_frag.get().m_shader);
glLinkProgram(m_prog);
glDetachShader(m_prog, m_vert.get().m_shader);
glDetachShader(m_prog, m_frag.get().m_shader);
m_vert.reset();
m_frag.reset();
GLint status;
glGetProgramiv(m_prog, GL_LINK_STATUS, &status);
if (status != GL_TRUE)
{
GLint logLen;
glGetProgramiv(m_prog, GL_INFO_LOG_LENGTH, &logLen);
std::unique_ptr<char[]> log(new char[logLen]);
glGetProgramInfoLog(m_prog, logLen, nullptr, log.get());
Log.report(logvisor::Error, "unable to link shader program\n%s\n", log.get());
return 0;
}
glUseProgram(m_prog);
if (m_blockNames.size())
{
m_uniLocs.reserve(m_blockNames.size());
for (size_t i=0 ; i<m_blockNames.size() ; ++i)
{
GLint uniLoc = glGetUniformBlockIndex(m_prog, m_blockNames[i].c_str());
//if (uniLoc < 0)
// Log.report(logvisor::Warning, "unable to find uniform block '%s'", uniformBlockNames[i]);
m_uniLocs.push_back(uniLoc);
}
m_blockNames = std::vector<std::string>();
}
if (m_texNames.size())
{
for (int i=0 ; i<m_texNames.size() ; ++i)
{
GLint texLoc = glGetUniformLocation(m_prog, m_texNames[i].c_str());
if (texLoc < 0)
{ /* Log.report(logvisor::Warning, "unable to find sampler variable '%s'", texNames[i]); */ }
else
glUniform1i(texLoc, i);
}
m_texNames = std::vector<std::string>();
}
}
else
{
glUseProgram(m_prog);
}
2015-10-30 00:00:56 +00:00
if (m_dfactor != GL_ZERO)
{
glEnable(GL_BLEND);
glBlendFuncSeparate(m_sfactor, m_dfactor, GL_ONE, GL_ZERO);
if (m_subtractBlend)
glBlendEquation(GL_FUNC_SUBTRACT);
else
glBlendEquation(GL_FUNC_ADD);
2015-10-30 00:00:56 +00:00
}
else
glDisable(GL_BLEND);
if (m_depthTest != ZTest::None)
{
2015-10-30 00:00:56 +00:00
glEnable(GL_DEPTH_TEST);
switch (m_depthTest)
{
case ZTest::LEqual:
default:
glDepthFunc(GL_LEQUAL);
break;
case ZTest::Greater:
glDepthFunc(GL_GREATER);
break;
case ZTest::GEqual:
glDepthFunc(GL_GEQUAL);
break;
case ZTest::Equal:
glDepthFunc(GL_EQUAL);
break;
}
}
2015-10-30 00:00:56 +00:00
else
glDisable(GL_DEPTH_TEST);
glDepthMask(m_depthWrite);
glColorMask(m_colorWrite, m_colorWrite, m_colorWrite, m_alphaWrite);
2017-03-10 20:38:00 +00:00
if (m_culling != CullMode::None)
{
2015-11-03 22:15:37 +00:00
glEnable(GL_CULL_FACE);
2017-03-10 20:38:00 +00:00
glCullFace(m_culling == CullMode::Backface ? GL_BACK : GL_FRONT);
}
2015-11-03 22:15:37 +00:00
else
2015-10-30 00:00:56 +00:00
glDisable(GL_CULL_FACE);
2015-10-30 00:00:56 +00:00
return m_prog;
}
2015-10-30 00:00:56 +00:00
};
static const GLenum PRIMITIVE_TABLE[] =
{
GL_TRIANGLES,
GL_TRIANGLE_STRIP
};
static const GLenum BLEND_FACTOR_TABLE[] =
{
GL_ZERO,
GL_ONE,
GL_SRC_COLOR,
GL_ONE_MINUS_SRC_COLOR,
GL_DST_COLOR,
GL_ONE_MINUS_DST_COLOR,
GL_SRC_ALPHA,
GL_ONE_MINUS_SRC_ALPHA,
GL_DST_ALPHA,
GL_ONE_MINUS_DST_ALPHA,
GL_SRC1_COLOR,
GL_ONE_MINUS_SRC1_COLOR
};
ObjToken<IShaderPipeline> GLDataFactory::Context::newShaderPipeline
(const char* vertSource, const char* fragSource,
2016-07-08 00:05:22 +00:00
size_t texCount, const char** texNames,
2015-11-16 04:29:31 +00:00
size_t uniformBlockCount, const char** uniformBlockNames,
BlendFactor srcFac, BlendFactor dstFac, Primitive prim,
ZTest depthTest, bool depthWrite, bool colorWrite,
bool alphaWrite, CullMode culling)
{
GLDataFactoryImpl& factory = static_cast<GLDataFactoryImpl&>(m_parent);
ObjToken<IShaderPipeline> retval(new GLShaderPipeline(m_data));
GLShaderPipeline& shader = *retval.cast<GLShaderPipeline>();
XXH64_state_t hashState;
uint64_t hashes[2];
XXH64_reset(&hashState, 0);
XXH64_update(&hashState, vertSource, strlen(vertSource));
hashes[0] = XXH64_digest(&hashState);
XXH64_reset(&hashState, 0);
XXH64_update(&hashState, fragSource, strlen(fragSource));
hashes[1] = XXH64_digest(&hashState);
GLint status;
auto vertFind = factory.m_sharedShaders.find(hashes[0]);
if (vertFind != factory.m_sharedShaders.end())
{
shader.m_vert = vertFind->second->lock();
}
else
{
GLuint sobj = glCreateShader(GL_VERTEX_SHADER);
if (!sobj)
{
Log.report(logvisor::Error, "unable to create vert shader");
return {};
}
glShaderSource(sobj, 1, &vertSource, nullptr);
glCompileShader(sobj);
glGetShaderiv(sobj, GL_COMPILE_STATUS, &status);
if (status != GL_TRUE)
{
GLint logLen;
glGetShaderiv(sobj, GL_INFO_LOG_LENGTH, &logLen);
std::unique_ptr<char[]> log(new char[logLen]);
glGetShaderInfoLog(sobj, logLen, nullptr, log.get());
Log.report(logvisor::Error, "unable to compile vert source\n%s\n%s\n", log.get(), vertSource);
return {};
}
auto it =
factory.m_sharedShaders.emplace(std::make_pair(hashes[0],
std::make_unique<GLShareableShader>(factory, hashes[0], sobj))).first;
shader.m_vert = it->second->lock();
}
auto fragFind = factory.m_sharedShaders.find(hashes[1]);
if (fragFind != factory.m_sharedShaders.end())
{
shader.m_frag = fragFind->second->lock();
}
else
{
GLuint sobj = glCreateShader(GL_FRAGMENT_SHADER);
if (!sobj)
{
Log.report(logvisor::Error, "unable to create frag shader");
return {};
}
glShaderSource(sobj, 1, &fragSource, nullptr);
glCompileShader(sobj);
glGetShaderiv(sobj, GL_COMPILE_STATUS, &status);
if (status != GL_TRUE)
{
GLint logLen;
glGetShaderiv(sobj, GL_INFO_LOG_LENGTH, &logLen);
std::unique_ptr<char[]> log(new char[logLen]);
glGetShaderInfoLog(sobj, logLen, nullptr, log.get());
Log.report(logvisor::Error, "unable to compile frag source\n%s\n%s\n", log.get(), fragSource);
return {};
}
auto it =
factory.m_sharedShaders.emplace(std::make_pair(hashes[1],
std::make_unique<GLShareableShader>(factory, hashes[1], sobj))).first;
shader.m_frag = it->second->lock();
}
shader.m_texNames.reserve(texCount);
for (int i=0 ; i<texCount ; ++i)
shader.m_texNames.emplace_back(texNames[i]);
shader.m_blockNames.reserve(uniformBlockCount);
for (int i=0 ; i<uniformBlockCount ; ++i)
shader.m_blockNames.emplace_back(uniformBlockNames[i]);
if (srcFac == BlendFactor::Subtract || dstFac == BlendFactor::Subtract)
{
shader.m_sfactor = GL_DST_COLOR;
shader.m_dfactor = GL_SRC_COLOR;
shader.m_subtractBlend = true;
}
else
{
shader.m_sfactor = BLEND_FACTOR_TABLE[int(srcFac)];
shader.m_dfactor = BLEND_FACTOR_TABLE[int(dstFac)];
shader.m_subtractBlend = false;
}
shader.m_depthTest = depthTest;
shader.m_depthWrite = depthWrite;
shader.m_colorWrite = colorWrite;
shader.m_alphaWrite = alphaWrite;
2017-03-10 20:38:00 +00:00
shader.m_culling = culling;
shader.m_drawPrim = PRIMITIVE_TABLE[int(prim)];
return retval;
}
struct GLVertexFormat : GraphicsDataNode<IVertexFormat>
2015-10-30 00:00:56 +00:00
{
2015-11-30 02:37:46 +00:00
GLuint m_vao[3] = {};
2016-12-10 02:31:50 +00:00
GLuint m_baseVert, m_baseInst;
std::vector<VertexElementDescriptor> m_elements;
GLVertexFormat(const ObjToken<BaseGraphicsData>& parent, GLCommandQueue* q,
size_t elementCount, const VertexElementDescriptor* elements,
2016-12-10 02:31:50 +00:00
size_t baseVert, size_t baseInst);
~GLVertexFormat() { glDeleteVertexArrays(3, m_vao); }
void bind(int idx) const { glBindVertexArray(m_vao[idx]); }
2015-10-30 00:00:56 +00:00
};
struct GLShaderDataBinding : GraphicsDataNode<IShaderDataBinding>
{
ObjToken<IShaderPipeline> m_pipeline;
ObjToken<IVertexFormat> m_vtxFormat;
std::vector<ObjToken<IGraphicsBuffer>> m_ubufs;
std::vector<std::pair<size_t,size_t>> m_ubufOffs;
struct BoundTex
{
ObjToken<ITexture> tex;
int idx;
bool depth;
};
std::vector<BoundTex> m_texs;
2015-12-17 21:29:47 +00:00
GLShaderDataBinding(const ObjToken<BaseGraphicsData>& d,
const ObjToken<IShaderPipeline>& pipeline,
const ObjToken<IVertexFormat>& vtxFormat,
size_t ubufCount, const ObjToken<IGraphicsBuffer>* ubufs,
const size_t* ubufOffs, const size_t* ubufSizes,
size_t texCount, const ObjToken<ITexture>* texs,
const int* bindTexIdx,
const bool* depthBind)
: GraphicsDataNode<IShaderDataBinding>(d),
m_pipeline(pipeline),
m_vtxFormat(vtxFormat)
2015-10-30 00:00:56 +00:00
{
if (ubufOffs && ubufSizes)
{
m_ubufOffs.reserve(ubufCount);
for (size_t i=0 ; i<ubufCount ; ++i)
2016-03-30 20:42:36 +00:00
{
#ifndef NDEBUG
if (ubufOffs[i] % 256)
Log.report(logvisor::Fatal, "non-256-byte-aligned uniform-offset %d provided to newShaderDataBinding", int(i));
#endif
m_ubufOffs.emplace_back(ubufOffs[i], (ubufSizes[i] + 255) & ~255);
}
}
m_ubufs.reserve(ubufCount);
2015-10-30 00:00:56 +00:00
for (size_t i=0 ; i<ubufCount ; ++i)
{
#ifndef NDEBUG
if (!ubufs[i])
Log.report(logvisor::Fatal, "null uniform-buffer %d provided to newShaderDataBinding", int(i));
#endif
m_ubufs.push_back(ubufs[i]);
}
m_texs.reserve(texCount);
2015-10-30 00:00:56 +00:00
for (size_t i=0 ; i<texCount ; ++i)
{
m_texs.push_back({texs[i], bindTexIdx ? bindTexIdx[i] : 0, depthBind ? depthBind[i] : false});
}
2015-10-30 00:00:56 +00:00
}
2015-11-30 02:37:46 +00:00
void bind(int b) const
{
GLShaderPipeline& pipeline = *m_pipeline.cast<GLShaderPipeline>();
GLuint prog = pipeline.bind();
m_vtxFormat.cast<GLVertexFormat>()->bind(b);
if (m_ubufOffs.size())
2015-10-30 00:00:56 +00:00
{
for (size_t i=0 ; i<m_ubufs.size() && i<pipeline.m_uniLocs.size() ; ++i)
{
GLint loc = pipeline.m_uniLocs[i];
if (loc < 0)
continue;
IGraphicsBuffer* ubuf = m_ubufs[i].get();
const std::pair<size_t,size_t>& offset = m_ubufOffs[i];
if (ubuf->dynamic())
static_cast<GLGraphicsBufferD<BaseGraphicsData>*>(ubuf)->bindUniformRange(i, offset.first, offset.second, b);
else
static_cast<GLGraphicsBufferS*>(ubuf)->bindUniformRange(i, offset.first, offset.second);
glUniformBlockBinding(prog, loc, i);
}
}
else
{
for (size_t i=0 ; i<m_ubufs.size() && i<pipeline.m_uniLocs.size() ; ++i)
{
GLint loc = pipeline.m_uniLocs[i];
if (loc < 0)
continue;
IGraphicsBuffer* ubuf = m_ubufs[i].get();
if (ubuf->dynamic())
static_cast<GLGraphicsBufferD<BaseGraphicsData>*>(ubuf)->bindUniform(i, b);
else
static_cast<GLGraphicsBufferS*>(ubuf)->bindUniform(i);
glUniformBlockBinding(prog, loc, i);
}
2015-10-30 00:00:56 +00:00
}
for (size_t i=0 ; i<m_texs.size() ; ++i)
2015-11-02 09:31:06 +00:00
{
const BoundTex& tex = m_texs[i];
if (tex.tex)
2015-11-26 23:03:01 +00:00
{
switch (tex.tex->type())
{
case TextureType::Dynamic:
tex.tex.cast<GLTextureD>()->bind(i, b);
break;
case TextureType::Static:
tex.tex.cast<GLTextureS>()->bind(i);
break;
case TextureType::StaticArray:
tex.tex.cast<GLTextureSA>()->bind(i);
break;
case TextureType::Render:
tex.tex.cast<GLTextureR>()->bind(i, tex.idx, tex.depth);
break;
default: break;
}
2015-11-26 23:03:01 +00:00
}
2015-11-02 09:31:06 +00:00
}
}
};
ObjToken<IShaderDataBinding>
GLDataFactory::Context::newShaderDataBinding(const ObjToken<IShaderPipeline>& pipeline,
const ObjToken<IVertexFormat>& vtxFormat,
const ObjToken<IGraphicsBuffer>& vbo,
const ObjToken<IGraphicsBuffer>& instVbo,
const ObjToken<IGraphicsBuffer>& ibo,
size_t ubufCount, const ObjToken<IGraphicsBuffer>* ubufs,
const PipelineStage* ubufStages,
const size_t* ubufOffs, const size_t* ubufSizes,
size_t texCount, const ObjToken<ITexture>* texs,
const int* texBindIdx, const bool* depthBind,
size_t baseVert, size_t baseInst)
{
return {new GLShaderDataBinding(m_data, pipeline, vtxFormat, ubufCount, ubufs,
ubufOffs, ubufSizes, texCount, texs, texBindIdx, depthBind)};
}
GLDataFactory::Context::Context(GLDataFactory& parent)
: m_parent(parent), m_data(new BaseGraphicsData(static_cast<GLDataFactoryImpl&>(parent)))
{}
GLDataFactory::Context::~Context() {}
void GLDataFactoryImpl::commitTransaction(const FactoryCommitFunc& trans)
{
GLDataFactory::Context ctx(*this);
if (!trans(ctx))
return;
/* Let's go ahead and flush to ensure our data gets to the GPU
While this isn't strictly required, some drivers might behave
differently */
//glFlush();
2016-12-10 02:31:50 +00:00
}
ObjToken<IGraphicsBufferD> GLDataFactoryImpl::newPoolBuffer(BufferUse use, size_t stride, size_t count)
2015-11-01 00:06:56 +00:00
{
ObjToken<BaseGraphicsPool> pool(new BaseGraphicsPool(*this));
return {new GLGraphicsBufferD<BaseGraphicsPool>(pool, use, stride * count)};
2016-12-11 06:18:41 +00:00
}
2015-10-30 00:00:56 +00:00
static const GLint SEMANTIC_COUNT_TABLE[] =
{
0,
2015-10-30 00:00:56 +00:00
3,
2015-11-26 00:22:05 +00:00
4,
2015-10-30 00:00:56 +00:00
3,
4,
2015-11-26 00:22:05 +00:00
4,
4,
2015-10-30 00:00:56 +00:00
2,
4,
2015-11-26 00:22:05 +00:00
4,
2015-10-30 00:00:56 +00:00
4
};
static const size_t SEMANTIC_SIZE_TABLE[] =
{
0,
2015-10-30 00:00:56 +00:00
12,
2015-11-26 00:22:05 +00:00
16,
2015-10-30 00:00:56 +00:00
12,
2015-11-26 00:22:05 +00:00
16,
16,
2015-10-30 00:00:56 +00:00
4,
8,
16,
2015-11-26 00:22:05 +00:00
16,
2015-10-30 00:00:56 +00:00
16
};
static const GLenum SEMANTIC_TYPE_TABLE[] =
{
GL_INVALID_ENUM,
2015-10-30 00:00:56 +00:00
GL_FLOAT,
GL_FLOAT,
2015-11-26 00:22:05 +00:00
GL_FLOAT,
GL_FLOAT,
GL_FLOAT,
2015-10-30 00:00:56 +00:00
GL_UNSIGNED_BYTE,
GL_FLOAT,
GL_FLOAT,
2015-11-26 00:22:05 +00:00
GL_FLOAT,
2015-10-30 00:00:56 +00:00
GL_FLOAT
};
struct GLCommandQueue : IGraphicsCommandQueue
{
Platform platform() const { return IGraphicsDataFactory::Platform::OpenGL; }
const SystemChar* platformName() const { return _S("OpenGL"); }
2015-10-30 00:00:56 +00:00
IGraphicsContext* m_parent = nullptr;
std::mutex m_mt;
std::condition_variable m_cv;
std::mutex m_initmt;
std::condition_variable m_initcv;
std::unique_lock<std::mutex> m_initlk;
std::thread m_thr;
struct Command
{
2015-11-21 01:12:22 +00:00
enum class Op
{
2015-11-21 01:12:22 +00:00
SetShaderDataBinding,
SetRenderTarget,
SetViewport,
2015-11-26 00:22:05 +00:00
SetScissor,
2015-11-21 01:12:22 +00:00
SetClearColor,
ClearTarget,
Draw,
DrawIndexed,
DrawInstances,
DrawInstancesIndexed,
ResolveBindTexture,
2015-11-21 01:12:22 +00:00
Present
} m_op;
union
{
2016-04-05 01:49:08 +00:00
struct
{
SWindowRect rect;
float znear, zfar;
} viewport;
float rgba[4];
GLbitfield flags;
struct
{
size_t start;
size_t count;
size_t instCount;
};
};
ObjToken<IShaderDataBinding> binding;
ObjToken<ITextureR> target;
ObjToken<ITextureR> source;
ObjToken<ITextureR> resolveTex;
int bindIdx;
2016-02-25 05:06:13 +00:00
bool resolveColor : 1;
bool resolveDepth : 1;
Command(Op op) : m_op(op) {}
Command(const Command&) = delete;
Command& operator=(const Command&) = delete;
Command(Command&&) = default;
Command& operator=(Command&&) = default;
};
std::vector<Command> m_cmdBufs[3];
int m_fillBuf = 0;
int m_completeBuf = 0;
int m_drawBuf = 0;
bool m_running = true;
2015-11-17 06:41:32 +00:00
struct RenderTextureResize
{
ObjToken<ITextureR> tex;
2015-11-17 06:41:32 +00:00
size_t width;
size_t height;
};
2015-10-30 00:00:56 +00:00
/* These members are locked for multithreaded access */
2015-11-17 06:41:32 +00:00
std::vector<RenderTextureResize> m_pendingResizes;
2015-12-21 00:40:52 +00:00
std::vector<std::function<void(void)>> m_pendingPosts1;
std::vector<std::function<void(void)>> m_pendingPosts2;
std::vector<ObjToken<IVertexFormat>> m_pendingFmtAdds;
std::vector<ObjToken<ITextureR>> m_pendingFboAdds;
2015-10-30 00:00:56 +00:00
static void ConfigureVertexFormat(GLVertexFormat* fmt)
2015-10-30 00:00:56 +00:00
{
2015-11-30 02:37:46 +00:00
glGenVertexArrays(3, fmt->m_vao);
2015-10-30 00:00:56 +00:00
size_t stride = 0;
size_t instStride = 0;
for (size_t i=0 ; i<fmt->m_elements.size() ; ++i)
2015-10-30 00:00:56 +00:00
{
const VertexElementDescriptor& desc = fmt->m_elements[i];
if ((desc.semantic & VertexSemantic::Instanced) != VertexSemantic::None)
instStride += SEMANTIC_SIZE_TABLE[int(desc.semantic & VertexSemantic::SemanticMask)];
else
stride += SEMANTIC_SIZE_TABLE[int(desc.semantic & VertexSemantic::SemanticMask)];
2015-10-30 00:00:56 +00:00
}
2015-11-30 02:37:46 +00:00
for (int b=0 ; b<3 ; ++b)
2015-10-30 00:00:56 +00:00
{
2016-12-10 02:31:50 +00:00
size_t offset = fmt->m_baseVert * stride;
size_t instOffset = fmt->m_baseInst * instStride;
2015-11-30 02:37:46 +00:00
glBindVertexArray(fmt->m_vao[b]);
IGraphicsBuffer* lastVBO = nullptr;
IGraphicsBuffer* lastEBO = nullptr;
for (size_t i=0 ; i<fmt->m_elements.size() ; ++i)
2015-10-30 00:00:56 +00:00
{
const VertexElementDescriptor& desc = fmt->m_elements[i];
if (desc.vertBuffer.get() != lastVBO)
2015-11-30 02:37:46 +00:00
{
lastVBO = desc.vertBuffer.get();
2015-11-30 02:37:46 +00:00
if (lastVBO->dynamic())
static_cast<GLGraphicsBufferD<BaseGraphicsData>*>(lastVBO)->bindVertex(b);
2015-11-30 02:37:46 +00:00
else
static_cast<GLGraphicsBufferS*>(lastVBO)->bindVertex();
2015-11-30 02:37:46 +00:00
}
if (desc.indexBuffer.get() != lastEBO)
2015-11-30 02:37:46 +00:00
{
lastEBO = desc.indexBuffer.get();
2015-11-30 02:37:46 +00:00
if (lastEBO->dynamic())
static_cast<GLGraphicsBufferD<BaseGraphicsData>*>(lastEBO)->bindIndex(b);
2015-11-30 02:37:46 +00:00
else
static_cast<GLGraphicsBufferS*>(lastEBO)->bindIndex();
2015-11-30 02:37:46 +00:00
}
glEnableVertexAttribArray(i);
int maskedSem = int(desc.semantic & VertexSemantic::SemanticMask);
if ((desc.semantic & VertexSemantic::Instanced) != VertexSemantic::None)
2015-11-30 02:37:46 +00:00
{
glVertexAttribPointer(i, SEMANTIC_COUNT_TABLE[maskedSem],
SEMANTIC_TYPE_TABLE[maskedSem], GL_TRUE, instStride, (void*)instOffset);
glVertexAttribDivisor(i, 1);
instOffset += SEMANTIC_SIZE_TABLE[maskedSem];
}
2015-11-02 09:31:06 +00:00
else
2015-11-30 02:37:46 +00:00
{
glVertexAttribPointer(i, SEMANTIC_COUNT_TABLE[maskedSem],
SEMANTIC_TYPE_TABLE[maskedSem], GL_TRUE, stride, (void*)offset);
offset += SEMANTIC_SIZE_TABLE[maskedSem];
}
}
2015-10-30 00:00:56 +00:00
}
}
2015-11-04 01:02:05 +00:00
static void ConfigureFBO(GLTextureR* tex)
2015-10-30 00:00:56 +00:00
{
glGenFramebuffers(1, &tex->m_fbo);
2016-04-02 03:55:07 +00:00
glBindFramebuffer(GL_FRAMEBUFFER, tex->m_fbo);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tex->m_texs[0], 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, tex->m_texs[1], 0);
2015-10-30 00:00:56 +00:00
}
static void RenderingWorker(GLCommandQueue* self)
{
2017-11-09 08:10:43 +00:00
#if _WIN32
std::string thrName = WCSTMBS(APP->getFriendlyName().c_str()) + " GL Rendering Thread";
#else
2017-11-08 02:24:07 +00:00
std::string thrName = APP->getFriendlyName() + " GL Rendering Thread";
2017-11-09 08:10:43 +00:00
#endif
2017-11-08 02:24:07 +00:00
logvisor::RegisterThreadName(thrName.c_str());
{
2015-10-31 06:33:36 +00:00
std::unique_lock<std::mutex> lk(self->m_initmt);
self->m_parent->makeCurrent();
if (glewInit() != GLEW_OK)
2016-03-04 23:02:18 +00:00
Log.report(logvisor::Fatal, "unable to init glew");
const GLubyte* version = glGetString(GL_VERSION);
2016-03-04 23:02:18 +00:00
Log.report(logvisor::Info, "OpenGL Version: %s", version);
self->m_parent->postInit();
glClearColor(0.f, 0.f, 0.f, 0.f);
}
self->m_initcv.notify_one();
while (self->m_running)
{
2015-12-21 00:40:52 +00:00
std::vector<std::function<void(void)>> posts;
{
std::unique_lock<std::mutex> lk(self->m_mt);
self->m_cv.wait(lk);
if (!self->m_running)
break;
self->m_drawBuf = self->m_completeBuf;
2015-10-30 00:00:56 +00:00
2016-04-02 04:24:46 +00:00
glBindFramebuffer(GL_FRAMEBUFFER, 0);
2016-04-02 00:03:36 +00:00
if (self->m_pendingFboAdds.size())
{
for (ObjToken<ITextureR>& tex : self->m_pendingFboAdds)
ConfigureFBO(tex.cast<GLTextureR>());
2016-04-02 00:03:36 +00:00
self->m_pendingFboAdds.clear();
}
2015-11-17 06:41:32 +00:00
if (self->m_pendingResizes.size())
{
for (const RenderTextureResize& resize : self->m_pendingResizes)
resize.tex.cast<GLTextureR>()->resize(resize.width, resize.height);
2015-11-17 06:41:32 +00:00
self->m_pendingResizes.clear();
}
2015-10-30 00:00:56 +00:00
if (self->m_pendingFmtAdds.size())
2015-11-17 06:41:32 +00:00
{
for (ObjToken<IVertexFormat>& fmt : self->m_pendingFmtAdds)
if (fmt) ConfigureVertexFormat(fmt.cast<GLVertexFormat>());
2015-11-17 06:41:32 +00:00
self->m_pendingFmtAdds.clear();
}
2015-10-30 00:00:56 +00:00
2015-12-21 00:40:52 +00:00
if (self->m_pendingPosts2.size())
posts.swap(self->m_pendingPosts2);
}
std::vector<Command>& cmds = self->m_cmdBufs[self->m_drawBuf];
GLenum currentPrim = GL_TRIANGLES;
const GLShaderDataBinding* curBinding = nullptr;
for (const Command& cmd : cmds)
{
switch (cmd.m_op)
{
2015-11-21 01:12:22 +00:00
case Command::Op::SetShaderDataBinding:
{
const GLShaderDataBinding* binding = cmd.binding.cast<GLShaderDataBinding>();
binding->bind(self->m_drawBuf);
curBinding = binding;
currentPrim = binding->m_pipeline.cast<GLShaderPipeline>()->m_drawPrim;
break;
}
2015-11-21 01:12:22 +00:00
case Command::Op::SetRenderTarget:
{
const GLTextureR* tex = cmd.target.cast<GLTextureR>();
2015-11-04 01:02:05 +00:00
if (!tex)
2016-04-02 03:55:07 +00:00
glBindFramebuffer(GL_FRAMEBUFFER, 0);
2015-11-04 01:02:05 +00:00
else
2016-04-02 03:55:07 +00:00
glBindFramebuffer(GL_FRAMEBUFFER, tex->m_fbo);
break;
}
2015-11-21 01:12:22 +00:00
case Command::Op::SetViewport:
glViewport(cmd.viewport.rect.location[0], cmd.viewport.rect.location[1],
cmd.viewport.rect.size[0], cmd.viewport.rect.size[1]);
glDepthRange(cmd.viewport.znear, cmd.viewport.zfar);
2015-11-03 04:27:56 +00:00
break;
2015-11-26 00:22:05 +00:00
case Command::Op::SetScissor:
if (cmd.viewport.rect.size[0] == 0 && cmd.viewport.rect.size[1] == 0)
2015-11-26 00:22:05 +00:00
glDisable(GL_SCISSOR_TEST);
else
{
glEnable(GL_SCISSOR_TEST);
glScissor(cmd.viewport.rect.location[0], cmd.viewport.rect.location[1],
cmd.viewport.rect.size[0], cmd.viewport.rect.size[1]);
2015-11-26 00:22:05 +00:00
}
break;
2015-11-21 01:12:22 +00:00
case Command::Op::SetClearColor:
glClearColor(cmd.rgba[0], cmd.rgba[1], cmd.rgba[2], cmd.rgba[3]);
break;
2015-11-21 01:12:22 +00:00
case Command::Op::ClearTarget:
if (cmd.flags & GL_COLOR_BUFFER_BIT)
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
2016-04-02 03:55:07 +00:00
if (cmd.flags & GL_DEPTH_BUFFER_BIT)
2016-04-02 04:40:31 +00:00
glDepthMask(GL_TRUE);
glClear(cmd.flags);
break;
2015-11-21 01:12:22 +00:00
case Command::Op::Draw:
glDrawArrays(currentPrim, cmd.start, cmd.count);
break;
2015-11-21 01:12:22 +00:00
case Command::Op::DrawIndexed:
2016-04-02 03:32:39 +00:00
glDrawElements(currentPrim, cmd.count, GL_UNSIGNED_INT,
reinterpret_cast<void*>(cmd.start * 4));
break;
2015-11-21 01:12:22 +00:00
case Command::Op::DrawInstances:
glDrawArraysInstanced(currentPrim, cmd.start, cmd.count, cmd.instCount);
break;
2015-11-21 01:12:22 +00:00
case Command::Op::DrawInstancesIndexed:
2016-04-02 03:32:39 +00:00
glDrawElementsInstanced(currentPrim, cmd.count, GL_UNSIGNED_INT,
reinterpret_cast<void*>(cmd.start * 4), cmd.instCount);
break;
case Command::Op::ResolveBindTexture:
{
const GLTextureR* tex = cmd.resolveTex.cast<GLTextureR>();
GLenum target = (tex->m_samples > 1) ? GL_TEXTURE_2D_MULTISAMPLE : GL_TEXTURE_2D;
glBindFramebuffer(GL_READ_FRAMEBUFFER, tex->m_fbo);
glActiveTexture(GL_TEXTURE9);
if (cmd.resolveColor && tex->m_bindTexs[0][cmd.bindIdx])
{
glBindTexture(target, tex->m_bindTexs[0][cmd.bindIdx]);
glCopyTexSubImage2D(target, 0, cmd.viewport.rect.location[0], cmd.viewport.rect.location[1],
cmd.viewport.rect.location[0], cmd.viewport.rect.location[1],
cmd.viewport.rect.size[0], cmd.viewport.rect.size[1]);
}
if (cmd.resolveDepth && tex->m_bindTexs[1][cmd.bindIdx])
{
glBindTexture(target, tex->m_bindTexs[1][cmd.bindIdx]);
glCopyTexSubImage2D(target, 0, cmd.viewport.rect.location[0], cmd.viewport.rect.location[1],
cmd.viewport.rect.location[0], cmd.viewport.rect.location[1],
cmd.viewport.rect.size[0], cmd.viewport.rect.size[1]);
}
break;
}
2015-11-21 01:12:22 +00:00
case Command::Op::Present:
2015-11-04 01:02:05 +00:00
{
if (const GLTextureR* tex = cmd.source.cast<GLTextureR>())
2015-11-04 01:02:05 +00:00
{
glBindFramebuffer(GL_READ_FRAMEBUFFER, tex->m_fbo);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
glBlitFramebuffer(0, 0, tex->m_width, tex->m_height, 0, 0,
tex->m_width, tex->m_height, GL_COLOR_BUFFER_BIT, GL_NEAREST);
}
self->m_parent->present();
break;
2015-11-04 01:02:05 +00:00
}
default: break;
}
}
2015-12-21 00:40:52 +00:00
for (auto& p : posts)
p();
cmds.clear();
}
}
GLCommandQueue(IGraphicsContext* parent)
: m_parent(parent),
2015-10-31 06:33:36 +00:00
m_initlk(m_initmt),
m_thr(RenderingWorker, this)
{
m_initcv.wait(m_initlk);
m_initlk.unlock();
}
void stopRenderer()
{
if (m_running)
{
m_running = false;
m_cv.notify_one();
if (m_thr.joinable())
m_thr.join();
for (int i=0 ; i<3 ; ++i)
m_cmdBufs[i].clear();
}
}
~GLCommandQueue()
{
stopRenderer();
}
void setShaderDataBinding(const ObjToken<IShaderDataBinding>& binding)
{
std::vector<Command>& cmds = m_cmdBufs[m_fillBuf];
2015-11-21 01:12:22 +00:00
cmds.emplace_back(Command::Op::SetShaderDataBinding);
cmds.back().binding = binding;
}
2015-11-04 01:02:05 +00:00
void setRenderTarget(const ObjToken<ITextureR>& target)
{
std::vector<Command>& cmds = m_cmdBufs[m_fillBuf];
2015-11-21 01:12:22 +00:00
cmds.emplace_back(Command::Op::SetRenderTarget);
cmds.back().target = target;
}
2015-11-04 01:02:05 +00:00
2016-04-05 01:49:08 +00:00
void setViewport(const SWindowRect& rect, float znear, float zfar)
2015-11-03 04:27:56 +00:00
{
std::vector<Command>& cmds = m_cmdBufs[m_fillBuf];
2015-11-21 01:12:22 +00:00
cmds.emplace_back(Command::Op::SetViewport);
cmds.back().viewport.rect = rect;
cmds.back().viewport.znear = znear;
cmds.back().viewport.zfar = zfar;
2015-11-03 04:27:56 +00:00
}
2015-11-26 00:22:05 +00:00
void setScissor(const SWindowRect& rect)
{
std::vector<Command>& cmds = m_cmdBufs[m_fillBuf];
cmds.emplace_back(Command::Op::SetScissor);
cmds.back().viewport.rect = rect;
2015-11-26 00:22:05 +00:00
}
void resizeRenderTexture(const ObjToken<ITextureR>& tex, size_t width, size_t height)
{
2015-11-17 06:41:32 +00:00
std::unique_lock<std::mutex> lk(m_mt);
GLTextureR* texgl = tex.cast<GLTextureR>();
2015-11-17 06:41:32 +00:00
m_pendingResizes.push_back({texgl, width, height});
}
2015-12-21 00:40:52 +00:00
void schedulePostFrameHandler(std::function<void(void)>&& func)
{
m_pendingPosts1.push_back(std::move(func));
}
void setClearColor(const float rgba[4])
{
std::vector<Command>& cmds = m_cmdBufs[m_fillBuf];
2015-11-21 01:12:22 +00:00
cmds.emplace_back(Command::Op::SetClearColor);
cmds.back().rgba[0] = rgba[0];
cmds.back().rgba[1] = rgba[1];
cmds.back().rgba[2] = rgba[2];
cmds.back().rgba[3] = rgba[3];
}
2015-11-04 01:02:05 +00:00
void clearTarget(bool render=true, bool depth=true)
{
std::vector<Command>& cmds = m_cmdBufs[m_fillBuf];
2015-11-21 01:12:22 +00:00
cmds.emplace_back(Command::Op::ClearTarget);
cmds.back().flags = 0;
if (render)
cmds.back().flags |= GL_COLOR_BUFFER_BIT;
if (depth)
cmds.back().flags |= GL_DEPTH_BUFFER_BIT;
}
void draw(size_t start, size_t count)
{
std::vector<Command>& cmds = m_cmdBufs[m_fillBuf];
2015-11-21 01:12:22 +00:00
cmds.emplace_back(Command::Op::Draw);
cmds.back().start = start;
cmds.back().count = count;
}
2015-11-04 01:02:05 +00:00
void drawIndexed(size_t start, size_t count)
{
std::vector<Command>& cmds = m_cmdBufs[m_fillBuf];
2015-11-21 01:12:22 +00:00
cmds.emplace_back(Command::Op::DrawIndexed);
cmds.back().start = start;
cmds.back().count = count;
}
2015-11-04 01:02:05 +00:00
void drawInstances(size_t start, size_t count, size_t instCount)
{
std::vector<Command>& cmds = m_cmdBufs[m_fillBuf];
2015-11-21 01:12:22 +00:00
cmds.emplace_back(Command::Op::DrawInstances);
cmds.back().start = start;
cmds.back().count = count;
cmds.back().instCount = instCount;
}
2015-11-04 01:02:05 +00:00
void drawInstancesIndexed(size_t start, size_t count, size_t instCount)
{
std::vector<Command>& cmds = m_cmdBufs[m_fillBuf];
2015-11-21 01:12:22 +00:00
cmds.emplace_back(Command::Op::DrawInstancesIndexed);
cmds.back().start = start;
cmds.back().count = count;
cmds.back().instCount = instCount;
}
void resolveBindTexture(const ObjToken<ITextureR>& texture, const SWindowRect& rect, bool tlOrigin,
int bindIdx, bool color, bool depth)
{
GLTextureR* tex = texture.cast<GLTextureR>();
std::vector<Command>& cmds = m_cmdBufs[m_fillBuf];
cmds.emplace_back(Command::Op::ResolveBindTexture);
cmds.back().resolveTex = texture;
cmds.back().bindIdx = bindIdx;
2016-02-25 05:06:13 +00:00
cmds.back().resolveColor = color;
cmds.back().resolveDepth = depth;
SWindowRect intersectRect = rect.intersect(SWindowRect(0, 0, tex->m_width, tex->m_height));
SWindowRect& targetRect = cmds.back().viewport.rect;
targetRect.location[0] = intersectRect.location[0];
if (tlOrigin)
targetRect.location[1] = tex->m_height - intersectRect.location[1] - intersectRect.size[1];
2016-02-26 20:16:21 +00:00
else
targetRect.location[1] = intersectRect.location[1];
targetRect.size[0] = intersectRect.size[0];
targetRect.size[1] = intersectRect.size[1];
}
void resolveDisplay(const ObjToken<ITextureR>& source)
{
std::vector<Command>& cmds = m_cmdBufs[m_fillBuf];
2015-11-21 01:12:22 +00:00
cmds.emplace_back(Command::Op::Present);
2015-11-04 01:02:05 +00:00
cmds.back().source = source;
}
2016-02-16 19:41:16 +00:00
void addVertexFormat(const ObjToken<IVertexFormat>& fmt)
2015-10-30 00:00:56 +00:00
{
std::unique_lock<std::mutex> lk(m_mt);
m_pendingFmtAdds.push_back(fmt);
}
void addFBO(const ObjToken<ITextureR>& tex)
2015-10-30 00:00:56 +00:00
{
std::unique_lock<std::mutex> lk(m_mt);
m_pendingFboAdds.push_back(tex);
}
void execute()
{
std::unique_lock<std::mutex> lk(m_mt);
m_completeBuf = m_fillBuf;
for (int i=0 ; i<3 ; ++i)
{
if (i == m_completeBuf || i == m_drawBuf)
continue;
m_fillBuf = i;
break;
}
/* Update dynamic data here */
GLDataFactoryImpl* gfxF = static_cast<GLDataFactoryImpl*>(m_parent->getDataFactory());
2017-11-08 07:33:10 +00:00
std::unique_lock<std::recursive_mutex> datalk(gfxF->m_dataMutex);
if (gfxF->m_dataHead)
{
for (BaseGraphicsData& d : *gfxF->m_dataHead)
{
if (d.m_DBufs)
for (IGraphicsBufferD& b : *d.m_DBufs)
static_cast<GLGraphicsBufferD<BaseGraphicsData>&>(b).update(m_completeBuf);
if (d.m_DTexs)
for (ITextureD& t : *d.m_DTexs)
static_cast<GLTextureD&>(t).update(m_completeBuf);
}
}
if (gfxF->m_poolHead)
2016-12-10 02:31:50 +00:00
{
for (BaseGraphicsPool& p : *gfxF->m_poolHead)
{
if (p.m_DBufs)
for (IGraphicsBufferD& b : *p.m_DBufs)
static_cast<GLGraphicsBufferD<BaseGraphicsData>&>(b).update(m_completeBuf);
}
2016-12-10 02:31:50 +00:00
}
2015-12-14 04:54:57 +00:00
datalk.unlock();
2017-07-14 05:13:29 +00:00
glFlush();
2015-12-21 00:40:52 +00:00
for (auto& p : m_pendingPosts1)
m_pendingPosts2.push_back(std::move(p));
m_pendingPosts1.clear();
lk.unlock();
m_cv.notify_one();
m_cmdBufs[m_fillBuf].clear();
}
};
ObjToken<IGraphicsBufferD>
GLDataFactory::Context::newDynamicBuffer(BufferUse use, size_t stride, size_t count)
2015-10-30 00:00:56 +00:00
{
return {new GLGraphicsBufferD<BaseGraphicsData>(m_data, use, stride * count)};
2015-10-30 00:00:56 +00:00
}
ObjToken<ITextureD>
2017-10-01 04:23:28 +00:00
GLDataFactory::Context::newDynamicTexture(size_t width, size_t height, TextureFormat fmt, TextureClampMode clampMode)
2015-10-30 00:00:56 +00:00
{
return {new GLTextureD(m_data, width, height, fmt, clampMode)};
2015-10-30 00:00:56 +00:00
}
GLTextureR::GLTextureR(const ObjToken<BaseGraphicsData>& parent, GLCommandQueue* q, size_t width, size_t height,
size_t samples, TextureClampMode clampMode, size_t colorBindingCount, size_t depthBindingCount)
: GraphicsDataNode<ITextureR>(parent), m_q(q), m_width(width), m_height(height), m_samples(samples)
2015-11-04 01:02:05 +00:00
{
glGenTextures(2, m_texs);
if (colorBindingCount)
{
if (colorBindingCount > MAX_BIND_TEXS)
Log.report(logvisor::Fatal, "too many color bindings for render texture");
glGenTextures(colorBindingCount, m_bindTexs[0]);
}
if (depthBindingCount)
{
if (depthBindingCount > MAX_BIND_TEXS)
Log.report(logvisor::Fatal, "too many depth bindings for render texture");
glGenTextures(depthBindingCount, m_bindTexs[1]);
}
2017-10-01 04:23:28 +00:00
if (samples > 1)
{
m_target = GL_TEXTURE_2D_MULTISAMPLE;
glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, m_texs[0]);
glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, samples, GL_RGBA, width, height, GL_FALSE);
glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, m_texs[1]);
glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, samples, GL_DEPTH_COMPONENT24, width, height, GL_FALSE);
}
else
{
m_target = GL_TEXTURE_2D;
glBindTexture(GL_TEXTURE_2D, m_texs[0]);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
glBindTexture(GL_TEXTURE_2D, m_texs[1]);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24, width, height, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, nullptr);
2017-10-01 04:23:28 +00:00
}
2017-10-01 04:23:28 +00:00
for (int i=0 ; i<colorBindingCount ; ++i)
{
glBindTexture(GL_TEXTURE_2D, m_bindTexs[0][i]);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
SetClampMode(GL_TEXTURE_2D, clampMode);
}
2017-10-01 04:23:28 +00:00
for (int i=0 ; i<depthBindingCount ; ++i)
{
glBindTexture(GL_TEXTURE_2D, m_bindTexs[1][i]);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24, width, height, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, nullptr);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
SetClampMode(GL_TEXTURE_2D, clampMode);
}
2015-11-04 01:02:05 +00:00
m_q->addFBO(this);
}
2017-10-01 04:23:28 +00:00
ObjToken<ITextureR>
2017-10-01 04:23:28 +00:00
GLDataFactory::Context::newRenderTexture(size_t width, size_t height, TextureClampMode clampMode,
size_t colorBindingCount, size_t depthBindingCount)
2015-11-04 01:02:05 +00:00
{
GLDataFactoryImpl& factory = static_cast<GLDataFactoryImpl&>(m_parent);
GLCommandQueue* q = static_cast<GLCommandQueue*>(factory.m_parent->getCommandQueue());
ObjToken<ITextureR> retval(new GLTextureR(m_data, q, width, height, factory.m_drawSamples, clampMode,
colorBindingCount, depthBindingCount));
2015-11-17 06:41:32 +00:00
q->resizeRenderTexture(retval, width, height);
2015-11-04 01:02:05 +00:00
return retval;
}
GLVertexFormat::GLVertexFormat(const ObjToken<BaseGraphicsData>& parent, GLCommandQueue* q,
size_t elementCount, const VertexElementDescriptor* elements,
2016-12-10 02:31:50 +00:00
size_t baseVert, size_t baseInst)
: GraphicsDataNode<IVertexFormat>(parent),
2016-12-10 02:31:50 +00:00
m_baseVert(baseVert), m_baseInst(baseInst)
2015-10-30 00:00:56 +00:00
{
m_elements.reserve(elementCount);
2015-10-30 00:00:56 +00:00
for (size_t i=0 ; i<elementCount ; ++i)
m_elements.push_back(elements[i]);
q->addVertexFormat(this);
2015-10-30 00:00:56 +00:00
}
ObjToken<IVertexFormat> GLDataFactory::Context::newVertexFormat
2016-12-10 02:31:50 +00:00
(size_t elementCount, const VertexElementDescriptor* elements,
size_t baseVert, size_t baseInst)
2015-10-30 00:00:56 +00:00
{
GLDataFactoryImpl& factory = static_cast<GLDataFactoryImpl&>(m_parent);
GLCommandQueue* q = static_cast<GLCommandQueue*>(factory.m_parent->getCommandQueue());
return {new GLVertexFormat(m_data, q, elementCount, elements, baseVert, baseInst)};
2015-10-30 00:00:56 +00:00
}
IGraphicsCommandQueue* _NewGLCommandQueue(IGraphicsContext* parent)
{
return new struct GLCommandQueue(parent);
}
IGraphicsDataFactory* _NewGLDataFactory(IGraphicsContext* parent, uint32_t drawSamples)
{
return new class GLDataFactoryImpl(parent, drawSamples);
}
}