#include "boo/graphicsdev/GL.hpp" #include "boo/graphicsdev/glew.h" #include "boo/IApplication.hpp" #include "Common.hpp" #include #include #include #include #include #include "xxhash.h" #if _WIN32 #include "../win/WinCommon.hpp" #endif #include "logvisor/logvisor.hpp" #undef min #undef max static const char* GammaVS = "#version 330\n" BOO_GLSL_BINDING_HEAD "layout(location=0) in vec4 posIn;\n" "layout(location=1) in vec4 uvIn;\n" "\n" "struct VertToFrag\n" "{\n" " vec2 uv;\n" "};\n" "\n" "SBINDING(0) out VertToFrag vtf;\n" "void main()\n" "{\n" " vtf.uv = uvIn.xy;\n" " gl_Position = posIn;\n" "}\n"; static const char* GammaFS = "#version 330\n" BOO_GLSL_BINDING_HEAD "struct VertToFrag\n" "{\n" " vec2 uv;\n" "};\n" "\n" "SBINDING(0) in VertToFrag vtf;\n" "layout(location=0) out vec4 colorOut;\n" "TBINDING0 uniform sampler2D screenTex;\n" "TBINDING1 uniform sampler2D gammaLUT;\n" "void main()\n" "{\n" " ivec4 tex = ivec4(texture(screenTex, vtf.uv) * 65535.0);\n" " for (int i=0 ; i<3 ; ++i)\n" " colorOut[i] = texelFetch(gammaLUT, ivec2(tex[i] % 256, tex[i] / 256), 0).r;\n" "}\n"; namespace boo { static logvisor::Module Log("boo::GL"); class GLDataFactoryImpl; struct GLShareableShader : IShareableShader { GLuint m_shader = 0; 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; GLContext* m_glCtx; std::unordered_map> m_sharedShaders; float m_gamma = 1.f; ObjToken m_gammaShader; ObjToken m_gammaLUT; ObjToken m_gammaVBO; ObjToken m_gammaVFMT; void SetupGammaResources() { BooCommitTransaction([this](IGraphicsDataFactory::Context& ctx) { const char* texNames[] = {"screenTex", "gammaLUT"}; m_gammaShader = static_cast(ctx).newShaderPipeline(GammaVS, GammaFS, 2, texNames, 0, nullptr, BlendFactor::One, BlendFactor::Zero, Primitive::TriStrips, ZTest::None, false, true, false, CullMode::None); m_gammaLUT = ctx.newDynamicTexture(256, 256, TextureFormat::I16, TextureClampMode::ClampToEdge); const struct Vert { float pos[4]; float uv[4]; } verts[4] = { {{-1.f, -1.f, 0.f, 1.f}, {0.f, 0.f, 0.f, 0.f}}, {{ 1.f, -1.f, 0.f, 1.f}, {1.f, 0.f, 0.f, 0.f}}, {{-1.f, 1.f, 0.f, 1.f}, {0.f, 1.f, 0.f, 0.f}}, {{ 1.f, 1.f, 0.f, 1.f}, {1.f, 1.f, 0.f, 0.f}} }; m_gammaVBO = ctx.newStaticBuffer(BufferUse::Vertex, verts, 32, 4); const VertexElementDescriptor vfmt[] = { {m_gammaVBO.get(), nullptr, VertexSemantic::Position4}, {m_gammaVBO.get(), nullptr, VertexSemantic::UV4} }; m_gammaVFMT = ctx.newVertexFormat(2, vfmt); return true; }); } public: GLDataFactoryImpl(IGraphicsContext* parent, GLContext* glCtx) : m_parent(parent), m_glCtx(glCtx) {} Platform platform() const { return Platform::OpenGL; } const SystemChar* platformName() const { return _S("OpenGL"); } void commitTransaction(const FactoryCommitFunc& trans __BooTraceArgs); ObjToken newPoolBuffer(BufferUse use, size_t stride, size_t count __BooTraceArgs); void _unregisterShareableShader(uint64_t srcKey, uint64_t binKey) { m_sharedShaders.erase(srcKey); } void setDisplayGamma(float gamma) { m_gamma = gamma; if (gamma != 1.f) UpdateGammaLUT(m_gammaLUT.get(), gamma); } }; static const GLenum USE_TABLE[] = { GL_INVALID_ENUM, GL_ARRAY_BUFFER, GL_ELEMENT_ARRAY_BUFFER, GL_UNIFORM_BUFFER }; class GLGraphicsBufferS : public GraphicsDataNode { friend class GLDataFactory; friend struct GLCommandQueue; GLuint m_buf; GLenum m_target; GLGraphicsBufferS(const ObjToken& parent, BufferUse use, const void* data, size_t sz) : GraphicsDataNode(parent) { 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); } 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 GLGraphicsBufferD : public GraphicsDataNode { friend class GLDataFactory; friend class GLDataFactoryImpl; friend struct GLCommandQueue; GLuint m_bufs[3]; GLenum m_target; std::unique_ptr m_cpuBuf; size_t m_cpuSz = 0; int m_validMask = 0; GLGraphicsBufferD(const ObjToken& parent, BufferUse use, size_t sz) : GraphicsDataNode(parent), m_target(USE_TABLE[int(use)]), m_cpuBuf(new uint8_t[sz]), m_cpuSz(sz) { 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; } } 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 GLDataFactory::Context::newStaticBuffer(BufferUse use, const void* data, size_t stride, size_t count) { return {new GLGraphicsBufferS(m_data, use, data, stride * count)}; } 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; } case TextureClampMode::ClampToEdge: { glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameteri(target, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE); break; } case TextureClampMode::ClampToEdgeNearest: { glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameteri(target, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE); glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST); break; } } } class GLTextureS : public GraphicsDataNode { friend class GLDataFactory; GLuint m_tex; GLTextureS(const ObjToken& parent, size_t width, size_t height, size_t mips, TextureFormat fmt, TextureClampMode clampMode, GLint aniso, const void* data, size_t sz) : GraphicsDataNode(parent) { const uint8_t* dataIt = static_cast(data); glGenTextures(1, &m_tex); glBindTexture(GL_TEXTURE_2D, m_tex); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); if (mips > 1) { glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, mips-1); } else glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); if (GLEW_EXT_texture_filter_anisotropic) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso); SetClampMode(GL_TEXTURE_2D, clampMode); GLenum intFormat, format; int pxPitch; bool compressed = false; switch (fmt) { case TextureFormat::RGBA8: intFormat = GL_RGBA8; format = GL_RGBA; pxPitch = 4; break; case TextureFormat::I8: intFormat = GL_R8; format = GL_RED; pxPitch = 1; break; case TextureFormat::I16: intFormat = GL_R16; format = GL_RED; pxPitch = 2; break; case TextureFormat::DXT1: intFormat = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT; compressed = true; break; default: Log.report(logvisor::Fatal, "unsupported tex format"); } if (compressed) { for (size_t i=0 ; i 1) width /= 2; if (height > 1) height /= 2; } } else { GLenum compType = intFormat == GL_R16 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_BYTE; for (size_t i=0 ; i 1) width /= 2; if (height > 1) height /= 2; } } } public: ~GLTextureS() { glDeleteTextures(1, &m_tex); } void setClampMode(TextureClampMode mode) { glBindTexture(GL_TEXTURE_2D, m_tex); SetClampMode(GL_TEXTURE_2D, mode); } void bind(size_t idx) const { glActiveTexture(GL_TEXTURE0 + idx); glBindTexture(GL_TEXTURE_2D, m_tex); } }; class GLTextureSA : public GraphicsDataNode { friend class GLDataFactory; GLuint m_tex; GLTextureSA(const ObjToken& parent, size_t width, size_t height, size_t layers, size_t mips, TextureFormat fmt, TextureClampMode clampMode, GLint aniso, const void* data, size_t sz) : GraphicsDataNode(parent) { const uint8_t* dataIt = static_cast(data); glGenTextures(1, &m_tex); glBindTexture(GL_TEXTURE_2D_ARRAY, m_tex); glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 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); if (GLEW_EXT_texture_filter_anisotropic) glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso); SetClampMode(GL_TEXTURE_2D_ARRAY, clampMode); GLenum intFormat, format; int pxPitch; switch (fmt) { case TextureFormat::RGBA8: intFormat = GL_RGBA8; format = GL_RGBA; pxPitch = 4; break; case TextureFormat::I8: intFormat = GL_R8; format = GL_RED; pxPitch = 1; break; case TextureFormat::I16: intFormat = GL_R16; format = GL_RED; pxPitch = 2; break; default: Log.report(logvisor::Fatal, "unsupported tex format"); } GLenum compType = intFormat == GL_R16 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_BYTE; for (size_t i=0 ; i 1) width /= 2; if (height > 1) height /= 2; } } public: ~GLTextureSA() { glDeleteTextures(1, &m_tex); } void setClampMode(TextureClampMode mode) { glBindTexture(GL_TEXTURE_2D_ARRAY, m_tex); SetClampMode(GL_TEXTURE_2D_ARRAY, mode); } void bind(size_t idx) const { glActiveTexture(GL_TEXTURE0 + idx); glBindTexture(GL_TEXTURE_2D_ARRAY, m_tex); } }; class GLTextureD : public GraphicsDataNode { friend class GLDataFactory; friend struct GLCommandQueue; GLuint m_texs[3]; std::unique_ptr 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& parent, size_t width, size_t height, TextureFormat fmt, TextureClampMode clampMode) : GraphicsDataNode(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; case TextureFormat::I16: m_intFormat = GL_R16; m_format = GL_RED; pxPitch = 2; break; default: Log.report(logvisor::Fatal, "unsupported tex format"); } m_cpuSz = width * height * pxPitch; m_cpuBuf.reset(new uint8_t[m_cpuSz]); GLenum compType = m_intFormat == GL_R16 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_BYTE; 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, compType, 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]); GLenum compType = m_intFormat == GL_R16 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_BYTE; glTexImage2D(GL_TEXTURE_2D, 0, m_intFormat, m_width, m_height, 0, m_format, compType, 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; } void setClampMode(TextureClampMode mode) { for (int i=0 ; i<3 ; ++i) { glBindTexture(GL_TEXTURE_2D, m_texs[i]); SetClampMode(GL_TEXTURE_2D, mode); } } void bind(size_t idx, int b) { glActiveTexture(GL_TEXTURE0 + idx); glBindTexture(GL_TEXTURE_2D, m_texs[b]); } }; #define MAX_BIND_TEXS 4 class GLTextureR : public GraphicsDataNode { friend class GLDataFactory; friend struct GLCommandQueue; struct GLCommandQueue* m_q; GLuint m_texs[2] = {}; GLuint m_bindTexs[2][MAX_BIND_TEXS] = {}; GLuint m_bindFBOs[2][MAX_BIND_TEXS] = {}; GLuint m_fbo = 0; size_t m_width = 0; size_t m_height = 0; size_t m_samples = 0; GLenum m_colorFormat; size_t m_colorBindCount; size_t m_depthBindCount; GLTextureR(const ObjToken& parent, GLCommandQueue* q, size_t width, size_t height, size_t samples, GLenum colorFormat, TextureClampMode clampMode, size_t colorBindCount, size_t depthBindCount); public: ~GLTextureR() { glDeleteTextures(2, m_texs); glDeleteTextures(MAX_BIND_TEXS * 2, m_bindTexs[0]); if (m_samples > 1) glDeleteFramebuffers(MAX_BIND_TEXS * 2, m_bindFBOs[0]); glDeleteFramebuffers(1, &m_fbo); } void setClampMode(TextureClampMode mode) { for (int i=0 ; i 1) { glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, m_texs[0]); glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, m_samples, m_colorFormat, width, height, GL_FALSE); glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, m_texs[1]); glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, m_samples, GL_DEPTH_COMPONENT32F, width, height, GL_FALSE); } else { glBindTexture(GL_TEXTURE_2D, m_texs[0]); glTexImage2D(GL_TEXTURE_2D, 0, m_colorFormat, width, height, 0, GL_RGBA, compType, nullptr); glBindTexture(GL_TEXTURE_2D, m_texs[1]); glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32F, width, height, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, nullptr); glBindFramebuffer(GL_FRAMEBUFFER, m_fbo); glDepthMask(GL_TRUE); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); } for (int i=0 ; i GLDataFactory::Context::newStaticTexture(size_t width, size_t height, size_t mips, TextureFormat fmt, TextureClampMode clampMode, const void* data, size_t sz) { GLDataFactoryImpl& factory = static_cast(m_parent); return {new GLTextureS(m_data, width, height, mips, fmt, clampMode, factory.m_glCtx->m_anisotropy, data, sz)}; } ObjToken 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) { GLDataFactoryImpl& factory = static_cast(m_parent); return {new GLTextureSA(m_data, width, height, layers, mips, fmt, clampMode, factory.m_glCtx->m_anisotropy, data, sz)}; } class GLShaderPipeline : public GraphicsDataNode { friend class GLDataFactory; friend struct GLCommandQueue; 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; bool m_depthWrite = true; bool m_colorWrite = true; bool m_alphaWrite = true; bool m_subtractBlend = false; bool m_overwriteAlpha = false; CullMode m_culling; mutable std::vector m_uniLocs; mutable std::vector m_texNames; mutable std::vector m_blockNames; GLShaderPipeline(const ObjToken& parent) : GraphicsDataNode(parent) {} public: ~GLShaderPipeline() { if (m_prog) glDeleteProgram(m_prog); } 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 log(new char[logLen]); glGetProgramInfoLog(m_prog, logLen, nullptr, log.get()); Log.report(logvisor::Fatal, "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(); } if (m_texNames.size()) { for (int i=0 ; i(); } } else { glUseProgram(m_prog); } if (m_dfactor != GL_ZERO) { glEnable(GL_BLEND); if (m_overwriteAlpha) glBlendFuncSeparate(m_sfactor, m_dfactor, GL_ONE, GL_ZERO); else glBlendFuncSeparate(m_sfactor, m_dfactor, m_sfactor, m_dfactor); if (m_subtractBlend) glBlendEquationSeparate(GL_FUNC_REVERSE_SUBTRACT, m_overwriteAlpha ? GL_FUNC_ADD : GL_FUNC_REVERSE_SUBTRACT); else glBlendEquation(GL_FUNC_ADD); } else glDisable(GL_BLEND); if (m_depthTest != ZTest::None) { 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; } } else glDisable(GL_DEPTH_TEST); glDepthMask(m_depthWrite); glColorMask(m_colorWrite, m_colorWrite, m_colorWrite, m_alphaWrite); if (m_culling != CullMode::None) { glEnable(GL_CULL_FACE); glCullFace(m_culling == CullMode::Backface ? GL_BACK : GL_FRONT); } else glDisable(GL_CULL_FACE); return m_prog; } }; 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 GLDataFactory::Context::newShaderPipeline (const char* vertSource, const char* fragSource, size_t texCount, const char** texNames, size_t uniformBlockCount, const char** uniformBlockNames, BlendFactor srcFac, BlendFactor dstFac, Primitive prim, ZTest depthTest, bool depthWrite, bool colorWrite, bool alphaWrite, CullMode culling, bool overwriteAlpha) { GLDataFactoryImpl& factory = static_cast(m_parent); ObjToken retval(new GLShaderPipeline(m_data)); GLShaderPipeline& shader = *retval.cast(); 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::Fatal, "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 log(new char[logLen]); glGetShaderInfoLog(sobj, logLen, nullptr, log.get()); Log.report(logvisor::Fatal, "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(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::Fatal, "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 log(new char[logLen]); glGetShaderInfoLog(sobj, logLen, nullptr, log.get()); Log.report(logvisor::Fatal, "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(factory, hashes[1], sobj))).first; shader.m_frag = it->second->lock(); } shader.m_texNames.reserve(texCount); for (int i=0 ; i { GLuint m_vao[3] = {}; GLuint m_baseVert, m_baseInst; std::vector m_elements; GLVertexFormat(const ObjToken& parent, GLCommandQueue* q, size_t elementCount, const VertexElementDescriptor* elements, size_t baseVert, size_t baseInst); ~GLVertexFormat() { glDeleteVertexArrays(3, m_vao); } void bind(int idx) const { glBindVertexArray(m_vao[idx]); } }; struct GLShaderDataBinding : GraphicsDataNode { ObjToken m_pipeline; ObjToken m_vtxFormat; std::vector> m_ubufs; std::vector> m_ubufOffs; struct BoundTex { ObjToken tex; int idx; bool depth; }; std::vector m_texs; GLShaderDataBinding(const ObjToken& d, const ObjToken& pipeline, const ObjToken& vtxFormat, size_t ubufCount, const ObjToken* ubufs, const size_t* ubufOffs, const size_t* ubufSizes, size_t texCount, const ObjToken* texs, const int* bindTexIdx, const bool* depthBind) : GraphicsDataNode(d), m_pipeline(pipeline), m_vtxFormat(vtxFormat) { if (ubufOffs && ubufSizes) { m_ubufOffs.reserve(ubufCount); for (size_t i=0 ; i(); GLuint prog = pipeline.bind(); m_vtxFormat.cast()->bind(b); if (m_ubufOffs.size()) { for (size_t i=0 ; i& offset = m_ubufOffs[i]; if (ubuf->dynamic()) static_cast*>(ubuf)->bindUniformRange(i, offset.first, offset.second, b); else static_cast(ubuf)->bindUniformRange(i, offset.first, offset.second); glUniformBlockBinding(prog, loc, i); } } else { for (size_t i=0 ; idynamic()) static_cast*>(ubuf)->bindUniform(i, b); else static_cast(ubuf)->bindUniform(i); glUniformBlockBinding(prog, loc, i); } } for (size_t i=0 ; itype()) { case TextureType::Dynamic: tex.tex.cast()->bind(i, b); break; case TextureType::Static: tex.tex.cast()->bind(i); break; case TextureType::StaticArray: tex.tex.cast()->bind(i); break; case TextureType::Render: tex.tex.cast()->bind(i, tex.idx, tex.depth); break; default: break; } } } } }; ObjToken GLDataFactory::Context::newShaderDataBinding(const ObjToken& pipeline, const ObjToken& vtxFormat, const ObjToken& vbo, const ObjToken& instVbo, const ObjToken& ibo, size_t ubufCount, const ObjToken* ubufs, const PipelineStage* ubufStages, const size_t* ubufOffs, const size_t* ubufSizes, size_t texCount, const ObjToken* 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 __BooTraceArgs) : m_parent(parent), m_data(new BaseGraphicsData(static_cast(parent) __BooTraceArgsUse)) {} GLDataFactory::Context::~Context() {} void GLDataFactoryImpl::commitTransaction(const FactoryCommitFunc& trans __BooTraceArgs) { GLDataFactory::Context ctx(*this __BooTraceArgsUse); 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(); } ObjToken GLDataFactoryImpl::newPoolBuffer(BufferUse use, size_t stride, size_t count __BooTraceArgs) { ObjToken pool(new BaseGraphicsPool(*this __BooTraceArgsUse)); return {new GLGraphicsBufferD(pool, use, stride * count)}; } static const GLint SEMANTIC_COUNT_TABLE[] = { 0, 3, 4, 3, 4, 4, 4, 2, 4, 4, 4 }; static const size_t SEMANTIC_SIZE_TABLE[] = { 0, 12, 16, 12, 16, 16, 4, 8, 16, 16, 16 }; static const GLenum SEMANTIC_TYPE_TABLE[] = { GL_INVALID_ENUM, GL_FLOAT, GL_FLOAT, GL_FLOAT, GL_FLOAT, GL_FLOAT, GL_UNSIGNED_BYTE, GL_FLOAT, GL_FLOAT, GL_FLOAT, GL_FLOAT }; struct GLCommandQueue : IGraphicsCommandQueue { Platform platform() const { return IGraphicsDataFactory::Platform::OpenGL; } const SystemChar* platformName() const { return _S("OpenGL"); } IGraphicsContext* m_parent = nullptr; GLContext* m_glCtx = nullptr; std::mutex m_mt; std::condition_variable m_cv; std::mutex m_initmt; std::condition_variable m_initcv; std::thread m_thr; struct Command { enum class Op { SetShaderDataBinding, SetRenderTarget, SetViewport, SetScissor, SetClearColor, ClearTarget, Draw, DrawIndexed, DrawInstances, DrawInstancesIndexed, ResolveBindTexture, Present } m_op; union { struct { SWindowRect rect; float znear, zfar; } viewport; float rgba[4]; GLbitfield flags; struct { size_t start; size_t count; size_t instCount; }; }; ObjToken binding; ObjToken target; ObjToken source; ObjToken resolveTex; int bindIdx; bool resolveColor : 1; bool resolveDepth : 1; bool clearDepth : 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 m_cmdBufs[3]; int m_fillBuf = 0; int m_completeBuf = 0; int m_drawBuf = 0; bool m_running = true; struct RenderTextureResize { ObjToken tex; size_t width; size_t height; }; /* These members are locked for multithreaded access */ std::vector m_pendingResizes; std::vector> m_pendingPosts1; std::vector> m_pendingPosts2; std::vector> m_pendingFmtAdds; std::vector> m_pendingFboAdds; static void ConfigureVertexFormat(GLVertexFormat* fmt) { glGenVertexArrays(3, fmt->m_vao); size_t stride = 0; size_t instStride = 0; for (size_t i=0 ; im_elements.size() ; ++i) { 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)]; } for (int b=0 ; b<3 ; ++b) { size_t offset = fmt->m_baseVert * stride; size_t instOffset = fmt->m_baseInst * instStride; glBindVertexArray(fmt->m_vao[b]); IGraphicsBuffer* lastVBO = nullptr; IGraphicsBuffer* lastEBO = nullptr; for (size_t i=0 ; im_elements.size() ; ++i) { const VertexElementDescriptor& desc = fmt->m_elements[i]; if (desc.vertBuffer.get() != lastVBO) { lastVBO = desc.vertBuffer.get(); if (lastVBO->dynamic()) static_cast*>(lastVBO)->bindVertex(b); else static_cast(lastVBO)->bindVertex(); } if (desc.indexBuffer.get() != lastEBO) { lastEBO = desc.indexBuffer.get(); if (lastEBO->dynamic()) static_cast*>(lastEBO)->bindIndex(b); else static_cast(lastEBO)->bindIndex(); } glEnableVertexAttribArray(i); int maskedSem = int(desc.semantic & VertexSemantic::SemanticMask); if ((desc.semantic & VertexSemantic::Instanced) != VertexSemantic::None) { glVertexAttribPointer(i, SEMANTIC_COUNT_TABLE[maskedSem], SEMANTIC_TYPE_TABLE[maskedSem], GL_TRUE, instStride, (void*)instOffset); glVertexAttribDivisor(i, 1); instOffset += SEMANTIC_SIZE_TABLE[maskedSem]; } else { glVertexAttribPointer(i, SEMANTIC_COUNT_TABLE[maskedSem], SEMANTIC_TYPE_TABLE[maskedSem], GL_TRUE, stride, (void*)offset); offset += SEMANTIC_SIZE_TABLE[maskedSem]; } } } } static void ConfigureFBO(GLTextureR* tex) { glGenFramebuffers(1, &tex->m_fbo); glBindFramebuffer(GL_FRAMEBUFFER, tex->m_fbo); GLenum target = tex->m_samples > 1 ? GL_TEXTURE_2D_MULTISAMPLE : GL_TEXTURE_2D; glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, target, tex->m_texs[0], 0); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, target, tex->m_texs[1], 0); if (tex->m_samples > 1) { if (tex->m_colorBindCount) { glGenFramebuffers(tex->m_colorBindCount, tex->m_bindFBOs[0]); for (int i=0 ; im_colorBindCount ; ++i) { glBindFramebuffer(GL_FRAMEBUFFER, tex->m_bindFBOs[0][i]); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tex->m_bindTexs[0][i], 0); } } if (tex->m_depthBindCount) { glGenFramebuffers(tex->m_depthBindCount, tex->m_bindFBOs[1]); for (int i=0 ; im_depthBindCount ; ++i) { glBindFramebuffer(GL_FRAMEBUFFER, tex->m_bindFBOs[1][i]); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, tex->m_bindTexs[1][i], 0); } } } } static void RenderingWorker(GLCommandQueue* self) { #if _WIN32 std::string thrName = WCSTMBS(APP->getFriendlyName().data()) + " GL Rendering Thread"; #else std::string thrName = std::string(APP->getFriendlyName()) + " GL Rendering Thread"; #endif logvisor::RegisterThreadName(thrName.c_str()); GLDataFactoryImpl* dataFactory = static_cast(self->m_parent->getDataFactory()); { std::unique_lock lk(self->m_initmt); self->m_parent->makeCurrent(); const GLubyte* version = glGetString(GL_VERSION); Log.report(logvisor::Info, "OpenGL Version: %s", version); self->m_parent->postInit(); glClearColor(0.f, 0.f, 0.f, 0.f); if (GLEW_EXT_texture_filter_anisotropic) { GLint maxAniso; glGetIntegerv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &maxAniso); self->m_glCtx->m_anisotropy = std::min(uint32_t(maxAniso), self->m_glCtx->m_anisotropy); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, maxAniso); } GLint maxSamples; glGetIntegerv(GL_MAX_SAMPLES, &maxSamples); self->m_glCtx->m_sampleCount = flp2(std::min(uint32_t(maxSamples), std::max(uint32_t(1), self->m_glCtx->m_sampleCount)) - 1); glEnable(GL_PRIMITIVE_RESTART); glPrimitiveRestartIndex(0xffffffff); dataFactory->SetupGammaResources(); } self->m_initcv.notify_one(); while (self->m_running) { std::vector> posts; { std::unique_lock lk(self->m_mt); self->m_cv.wait(lk); if (!self->m_running) break; self->m_drawBuf = self->m_completeBuf; glBindFramebuffer(GL_FRAMEBUFFER, 0); if (self->m_pendingFboAdds.size()) { for (ObjToken& tex : self->m_pendingFboAdds) ConfigureFBO(tex.cast()); self->m_pendingFboAdds.clear(); } if (self->m_pendingResizes.size()) { for (const RenderTextureResize& resize : self->m_pendingResizes) resize.tex.cast()->resize(resize.width, resize.height); self->m_pendingResizes.clear(); } if (self->m_pendingFmtAdds.size()) { for (ObjToken& fmt : self->m_pendingFmtAdds) if (fmt) ConfigureVertexFormat(fmt.cast()); self->m_pendingFmtAdds.clear(); } if (self->m_pendingPosts2.size()) posts.swap(self->m_pendingPosts2); } std::vector& cmds = self->m_cmdBufs[self->m_drawBuf]; GLenum currentPrim = GL_TRIANGLES; const GLShaderDataBinding* curBinding = nullptr; GLuint curFBO = 0; for (const Command& cmd : cmds) { switch (cmd.m_op) { case Command::Op::SetShaderDataBinding: { const GLShaderDataBinding* binding = cmd.binding.cast(); binding->bind(self->m_drawBuf); curBinding = binding; currentPrim = binding->m_pipeline.cast()->m_drawPrim; break; } case Command::Op::SetRenderTarget: { const GLTextureR* tex = cmd.target.cast(); curFBO = (!tex) ? 0 : tex->m_fbo; glBindFramebuffer(GL_FRAMEBUFFER, curFBO); break; } 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); break; case Command::Op::SetScissor: if (cmd.viewport.rect.size[0] == 0 && cmd.viewport.rect.size[1] == 0) 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]); } break; case Command::Op::SetClearColor: glClearColor(cmd.rgba[0], cmd.rgba[1], cmd.rgba[2], cmd.rgba[3]); break; case Command::Op::ClearTarget: if (cmd.flags & GL_COLOR_BUFFER_BIT) glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); if (cmd.flags & GL_DEPTH_BUFFER_BIT) glDepthMask(GL_TRUE); glClear(cmd.flags); break; case Command::Op::Draw: glDrawArrays(currentPrim, cmd.start, cmd.count); break; case Command::Op::DrawIndexed: glDrawElements(currentPrim, cmd.count, GL_UNSIGNED_INT, reinterpret_cast(cmd.start * 4)); break; case Command::Op::DrawInstances: glDrawArraysInstanced(currentPrim, cmd.start, cmd.count, cmd.instCount); break; case Command::Op::DrawInstancesIndexed: glDrawElementsInstanced(currentPrim, cmd.count, GL_UNSIGNED_INT, reinterpret_cast(cmd.start * 4), cmd.instCount); break; case Command::Op::ResolveBindTexture: { const SWindowRect& rect = cmd.viewport.rect; const GLTextureR* tex = cmd.resolveTex.cast(); glBindFramebuffer(GL_READ_FRAMEBUFFER, tex->m_fbo); if (tex->m_samples <= 1) { glActiveTexture(GL_TEXTURE9); if (cmd.resolveColor && tex->m_bindTexs[0][cmd.bindIdx]) { glBindTexture(GL_TEXTURE_2D, tex->m_bindTexs[0][cmd.bindIdx]); glCopyTexSubImage2D(GL_TEXTURE_2D, 0, rect.location[0], rect.location[1], rect.location[0], rect.location[1], rect.size[0], rect.size[1]); } if (cmd.resolveDepth && tex->m_bindTexs[1][cmd.bindIdx]) { glBindTexture(GL_TEXTURE_2D, tex->m_bindTexs[1][cmd.bindIdx]); glCopyTexSubImage2D(GL_TEXTURE_2D, 0, rect.location[0], rect.location[1], rect.location[0], rect.location[1], rect.size[0], rect.size[1]); } } else { if (cmd.resolveColor && tex->m_bindTexs[0][cmd.bindIdx]) { glBindFramebuffer(GL_DRAW_FRAMEBUFFER, tex->m_bindFBOs[0][cmd.bindIdx]); glBlitFramebuffer(rect.location[0], rect.location[1], rect.location[0] + rect.size[0], rect.location[1] + rect.size[1], rect.location[0], rect.location[1], rect.location[0] + rect.size[0], rect.location[1] + rect.size[1], GL_COLOR_BUFFER_BIT, GL_NEAREST); } if (cmd.resolveDepth && tex->m_bindTexs[1][cmd.bindIdx]) { glBindFramebuffer(GL_DRAW_FRAMEBUFFER, tex->m_bindFBOs[1][cmd.bindIdx]); glBlitFramebuffer(rect.location[0], rect.location[1], rect.location[0] + rect.size[0], rect.location[1] + rect.size[1], rect.location[0], rect.location[1], rect.location[0] + rect.size[0], rect.location[1] + rect.size[1], GL_DEPTH_BUFFER_BIT, GL_NEAREST); } } if (cmd.clearDepth) { glBindFramebuffer(GL_DRAW_FRAMEBUFFER, tex->m_fbo); glDepthMask(GL_TRUE); glClear(GL_DEPTH_BUFFER_BIT); } glBindFramebuffer(GL_FRAMEBUFFER, curFBO); break; } case Command::Op::Present: { if (const GLTextureR* tex = cmd.source.cast()) { #ifndef NDEBUG if (!tex->m_colorBindCount) Log.report(logvisor::Fatal, "texture provided to resolveDisplay() must have at least 1 color binding"); #endif if (dataFactory->m_gamma != 1.f) { glBindFramebuffer(GL_READ_FRAMEBUFFER, tex->m_fbo); if (tex->m_samples <= 1) { glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, tex->m_texs[0]); } else { glBindFramebuffer(GL_DRAW_FRAMEBUFFER, tex->m_bindFBOs[0][0]); glBlitFramebuffer(0, 0, tex->m_width, tex->m_height, 0, 0, tex->m_width, tex->m_height, GL_COLOR_BUFFER_BIT, GL_NEAREST); tex->bind(0, 0, false); } glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0); dataFactory->m_gammaShader.cast()->bind(); dataFactory->m_gammaVFMT.cast()->bind(self->m_drawBuf); dataFactory->m_gammaLUT.cast()->bind(1, self->m_drawBuf); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); } else { 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; } default: break; } } for (auto& p : posts) p(); cmds.clear(); } } GLCommandQueue(IGraphicsContext* parent, GLContext* glCtx) : m_parent(parent), m_glCtx(glCtx) {} void startRenderer() { std::unique_lock lk(m_initmt); m_thr = std::thread(RenderingWorker, this); m_initcv.wait(lk); } 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& binding) { std::vector& cmds = m_cmdBufs[m_fillBuf]; cmds.emplace_back(Command::Op::SetShaderDataBinding); cmds.back().binding = binding; } void setRenderTarget(const ObjToken& target) { std::vector& cmds = m_cmdBufs[m_fillBuf]; cmds.emplace_back(Command::Op::SetRenderTarget); cmds.back().target = target; } void setViewport(const SWindowRect& rect, float znear, float zfar) { std::vector& cmds = m_cmdBufs[m_fillBuf]; cmds.emplace_back(Command::Op::SetViewport); cmds.back().viewport.rect = rect; cmds.back().viewport.znear = znear; cmds.back().viewport.zfar = zfar; } void setScissor(const SWindowRect& rect) { std::vector& cmds = m_cmdBufs[m_fillBuf]; cmds.emplace_back(Command::Op::SetScissor); cmds.back().viewport.rect = rect; } void resizeRenderTexture(const ObjToken& tex, size_t width, size_t height) { std::unique_lock lk(m_mt); GLTextureR* texgl = tex.cast(); m_pendingResizes.push_back({texgl, width, height}); } void schedulePostFrameHandler(std::function&& func) { m_pendingPosts1.push_back(std::move(func)); } void setClearColor(const float rgba[4]) { std::vector& cmds = m_cmdBufs[m_fillBuf]; 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]; } void clearTarget(bool render=true, bool depth=true) { std::vector& cmds = m_cmdBufs[m_fillBuf]; 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& cmds = m_cmdBufs[m_fillBuf]; cmds.emplace_back(Command::Op::Draw); cmds.back().start = start; cmds.back().count = count; } void drawIndexed(size_t start, size_t count) { std::vector& cmds = m_cmdBufs[m_fillBuf]; cmds.emplace_back(Command::Op::DrawIndexed); cmds.back().start = start; cmds.back().count = count; } void drawInstances(size_t start, size_t count, size_t instCount) { std::vector& cmds = m_cmdBufs[m_fillBuf]; cmds.emplace_back(Command::Op::DrawInstances); cmds.back().start = start; cmds.back().count = count; cmds.back().instCount = instCount; } void drawInstancesIndexed(size_t start, size_t count, size_t instCount) { std::vector& cmds = m_cmdBufs[m_fillBuf]; cmds.emplace_back(Command::Op::DrawInstancesIndexed); cmds.back().start = start; cmds.back().count = count; cmds.back().instCount = instCount; } void resolveBindTexture(const ObjToken& texture, const SWindowRect& rect, bool tlOrigin, int bindIdx, bool color, bool depth, bool clearDepth) { GLTextureR* tex = texture.cast(); std::vector& cmds = m_cmdBufs[m_fillBuf]; cmds.emplace_back(Command::Op::ResolveBindTexture); cmds.back().resolveTex = texture; cmds.back().bindIdx = bindIdx; cmds.back().resolveColor = color; cmds.back().resolveDepth = depth; cmds.back().clearDepth = clearDepth; 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]; else targetRect.location[1] = intersectRect.location[1]; targetRect.size[0] = intersectRect.size[0]; targetRect.size[1] = intersectRect.size[1]; } void resolveDisplay(const ObjToken& source) { std::vector& cmds = m_cmdBufs[m_fillBuf]; cmds.emplace_back(Command::Op::Present); cmds.back().source = source; } void addVertexFormat(const ObjToken& fmt) { std::unique_lock lk(m_mt); m_pendingFmtAdds.push_back(fmt); } void addFBO(const ObjToken& tex) { std::unique_lock lk(m_mt); m_pendingFboAdds.push_back(tex); } void execute() { std::unique_lock 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(m_parent->getDataFactory()); std::unique_lock 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&>(b).update(m_completeBuf); if (d.m_DTexs) for (ITextureD& t : *d.m_DTexs) static_cast(t).update(m_completeBuf); } } if (gfxF->m_poolHead) { for (BaseGraphicsPool& p : *gfxF->m_poolHead) { if (p.m_DBufs) for (IGraphicsBufferD& b : *p.m_DBufs) static_cast&>(b).update(m_completeBuf); } } datalk.unlock(); glFlush(); 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 GLDataFactory::Context::newDynamicBuffer(BufferUse use, size_t stride, size_t count) { return {new GLGraphicsBufferD(m_data, use, stride * count)}; } ObjToken GLDataFactory::Context::newDynamicTexture(size_t width, size_t height, TextureFormat fmt, TextureClampMode clampMode) { return {new GLTextureD(m_data, width, height, fmt, clampMode)}; } GLTextureR::GLTextureR(const ObjToken& parent, GLCommandQueue* q, size_t width, size_t height, size_t samples, GLenum colorFormat, TextureClampMode clampMode, size_t colorBindingCount, size_t depthBindingCount) : GraphicsDataNode(parent), m_q(q), m_width(width), m_height(height), m_samples(samples), m_colorFormat(colorFormat), m_colorBindCount(colorBindingCount), m_depthBindCount(depthBindingCount) { 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]); } GLenum compType = colorFormat == GL_RGBA16 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_BYTE; if (samples > 1) { glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, m_texs[0]); glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, samples, colorFormat, width, height, GL_FALSE); glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, m_texs[1]); glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, samples, GL_DEPTH_COMPONENT32F, width, height, GL_FALSE); } else { glBindTexture(GL_TEXTURE_2D, m_texs[0]); glTexImage2D(GL_TEXTURE_2D, 0, colorFormat, width, height, 0, GL_RGBA, compType, nullptr); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glBindTexture(GL_TEXTURE_2D, m_texs[1]); glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32F, width, height, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, nullptr); } for (int i=0 ; iaddFBO(this); } ObjToken GLDataFactory::Context::newRenderTexture(size_t width, size_t height, TextureClampMode clampMode, size_t colorBindingCount, size_t depthBindingCount) { GLDataFactoryImpl& factory = static_cast(m_parent); GLCommandQueue* q = static_cast(factory.m_parent->getCommandQueue()); ObjToken retval(new GLTextureR(m_data, q, width, height, factory.m_glCtx->m_sampleCount, factory.m_glCtx->m_deepColor ? GL_RGBA16 : GL_RGBA8, clampMode, colorBindingCount, depthBindingCount)); q->resizeRenderTexture(retval, width, height); return retval; } GLVertexFormat::GLVertexFormat(const ObjToken& parent, GLCommandQueue* q, size_t elementCount, const VertexElementDescriptor* elements, size_t baseVert, size_t baseInst) : GraphicsDataNode(parent), m_baseVert(baseVert), m_baseInst(baseInst) { m_elements.reserve(elementCount); for (size_t i=0 ; iaddVertexFormat(this); } ObjToken GLDataFactory::Context::newVertexFormat (size_t elementCount, const VertexElementDescriptor* elements, size_t baseVert, size_t baseInst) { GLDataFactoryImpl& factory = static_cast(m_parent); GLCommandQueue* q = static_cast(factory.m_parent->getCommandQueue()); return {new GLVertexFormat(m_data, q, elementCount, elements, baseVert, baseInst)}; } std::unique_ptr _NewGLCommandQueue(IGraphicsContext* parent, GLContext* glCtx) { return std::make_unique(parent, glCtx); } std::unique_ptr _NewGLDataFactory(IGraphicsContext* parent, GLContext* glCtx) { return std::make_unique(parent, glCtx); } }