2015-10-31 04:28:21 +00:00
|
|
|
#include "boo/graphicsdev/GL.hpp"
|
2015-11-08 00:36:38 +00:00
|
|
|
#include "boo/graphicsdev/glew.h"
|
2017-11-08 02:24:07 +00:00
|
|
|
#include "boo/IApplication.hpp"
|
2017-01-20 03:52:40 +00:00
|
|
|
#include "Common.hpp"
|
2015-10-29 04:44:38 +00:00
|
|
|
#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>
|
2017-03-05 07:54:58 +00:00
|
|
|
#include <unordered_set>
|
2018-10-07 02:49:22 +00:00
|
|
|
#include "xxhash/xxhash.h"
|
|
|
|
#include "glslang/Public/ShaderLang.h"
|
|
|
|
#include "glslang/Include/Types.h"
|
|
|
|
#include "StandAlone/ResourceLimits.h"
|
2015-10-29 04:44:38 +00:00
|
|
|
|
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
|
|
|
|
|
2018-01-20 03:02:29 +00:00
|
|
|
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"
|
2018-01-21 22:01:52 +00:00
|
|
|
" ivec4 tex = ivec4(texture(screenTex, vtf.uv) * 65535.0);\n"
|
2018-01-20 05:50:01 +00:00
|
|
|
" for (int i=0 ; i<3 ; ++i)\n"
|
2018-01-21 22:01:52 +00:00
|
|
|
" colorOut[i] = texelFetch(gammaLUT, ivec2(tex[i] % 256, tex[i] / 256), 0).r;\n"
|
2018-01-20 03:02:29 +00:00
|
|
|
"}\n";
|
|
|
|
|
2015-10-29 04:44:38 +00:00
|
|
|
namespace boo
|
|
|
|
{
|
2016-03-04 23:02:18 +00:00
|
|
|
static logvisor::Module Log("boo::GL");
|
2017-03-05 07:54:58 +00:00
|
|
|
class GLDataFactoryImpl;
|
2015-10-29 04:44:38 +00:00
|
|
|
|
2017-11-03 09:39:26 +00:00
|
|
|
class GLDataFactoryImpl : public GLDataFactory, public GraphicsDataFactoryHead
|
2017-03-05 07:54:58 +00:00
|
|
|
{
|
|
|
|
friend struct GLCommandQueue;
|
|
|
|
friend class GLDataFactory::Context;
|
|
|
|
IGraphicsContext* m_parent;
|
2018-01-07 05:17:14 +00:00
|
|
|
GLContext* m_glCtx;
|
2018-01-20 03:02:29 +00:00
|
|
|
|
2018-06-07 04:36:17 +00:00
|
|
|
bool m_hasTessellation = false;
|
|
|
|
uint32_t m_maxPatchSize = 0;
|
|
|
|
|
2018-01-21 22:01:52 +00:00
|
|
|
float m_gamma = 1.f;
|
2018-01-20 03:02:29 +00:00
|
|
|
ObjToken<IShaderPipeline> m_gammaShader;
|
|
|
|
ObjToken<ITextureD> m_gammaLUT;
|
|
|
|
ObjToken<IGraphicsBufferS> m_gammaVBO;
|
2018-10-07 02:49:22 +00:00
|
|
|
ObjToken<IShaderDataBinding> m_gammaBinding;
|
2018-01-20 03:02:29 +00:00
|
|
|
void SetupGammaResources()
|
|
|
|
{
|
2018-06-07 04:36:17 +00:00
|
|
|
/* Good enough place for this */
|
2018-10-07 02:49:22 +00:00
|
|
|
if (!glslang::InitializeProcess())
|
|
|
|
Log.report(logvisor::Error, "unable to initialize glslang");
|
|
|
|
|
2018-06-07 04:36:17 +00:00
|
|
|
if (GLEW_ARB_tessellation_shader)
|
|
|
|
{
|
|
|
|
m_hasTessellation = true;
|
|
|
|
GLint maxPVerts;
|
|
|
|
glGetIntegerv(GL_MAX_PATCH_VERTICES, &maxPVerts);
|
|
|
|
m_maxPatchSize = uint32_t(maxPVerts);
|
|
|
|
}
|
|
|
|
|
2018-05-25 06:30:42 +00:00
|
|
|
commitTransaction([this](IGraphicsDataFactory::Context& ctx)
|
2018-01-20 03:02:29 +00:00
|
|
|
{
|
2018-10-07 02:49:22 +00:00
|
|
|
auto vertex = ctx.newShaderStage((uint8_t*)GammaVS, 0, PipelineStage::Vertex);
|
|
|
|
auto fragment = ctx.newShaderStage((uint8_t*)GammaFS, 0, PipelineStage::Fragment);
|
|
|
|
AdditionalPipelineInfo info =
|
|
|
|
{
|
|
|
|
BlendFactor::One, BlendFactor::Zero,
|
|
|
|
Primitive::TriStrips, ZTest::None, false, true, false, CullMode::None
|
|
|
|
};
|
|
|
|
const VertexElementDescriptor vfmt[] = {
|
|
|
|
{VertexSemantic::Position4},
|
|
|
|
{VertexSemantic::UV4}
|
|
|
|
};
|
|
|
|
m_gammaShader = ctx.newShaderPipeline(vertex, fragment, vfmt, info);
|
2018-01-20 03:02:29 +00:00
|
|
|
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);
|
2018-10-07 02:49:22 +00:00
|
|
|
ObjToken<ITexture> texs[] = {{}, m_gammaLUT.get()};
|
|
|
|
m_gammaBinding = ctx.newShaderDataBinding(m_gammaShader, m_gammaVBO.get(), {}, {},
|
|
|
|
0, nullptr, nullptr, 2, texs, nullptr, nullptr);
|
2018-01-20 03:02:29 +00:00
|
|
|
return true;
|
2018-05-25 06:30:42 +00:00
|
|
|
} BooTrace);
|
2018-01-20 03:02:29 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 07:54:58 +00:00
|
|
|
public:
|
2018-01-07 05:17:14 +00:00
|
|
|
GLDataFactoryImpl(IGraphicsContext* parent, GLContext* glCtx)
|
|
|
|
: m_parent(parent), m_glCtx(glCtx) {}
|
2017-03-05 07:54:58 +00:00
|
|
|
|
2017-11-03 09:39:26 +00:00
|
|
|
Platform platform() const { return Platform::OpenGL; }
|
2018-10-07 02:49:22 +00:00
|
|
|
const SystemChar* platformName() const { return _SYS_STR("OpenGL"); }
|
2018-05-20 06:11:49 +00:00
|
|
|
void commitTransaction(const FactoryCommitFunc& trans __BooTraceArgs);
|
|
|
|
ObjToken<IGraphicsBufferD> newPoolBuffer(BufferUse use, size_t stride, size_t count __BooTraceArgs);
|
2018-01-20 05:50:01 +00:00
|
|
|
|
2018-01-21 22:01:52 +00:00
|
|
|
void setDisplayGamma(float gamma)
|
|
|
|
{
|
|
|
|
m_gamma = gamma;
|
|
|
|
if (gamma != 1.f)
|
|
|
|
UpdateGammaLUT(m_gammaLUT.get(), gamma);
|
|
|
|
}
|
2018-06-07 04:36:17 +00:00
|
|
|
|
|
|
|
bool isTessellationSupported(uint32_t& maxPatchSizeOut)
|
|
|
|
{
|
|
|
|
maxPatchSizeOut = m_maxPatchSize;
|
|
|
|
return m_hasTessellation;
|
|
|
|
}
|
2016-12-10 02:31:50 +00:00
|
|
|
};
|
|
|
|
|
2015-10-29 04:44:38 +00:00
|
|
|
static const GLenum USE_TABLE[] =
|
|
|
|
{
|
|
|
|
GL_INVALID_ENUM,
|
|
|
|
GL_ARRAY_BUFFER,
|
|
|
|
GL_ELEMENT_ARRAY_BUFFER,
|
|
|
|
GL_UNIFORM_BUFFER
|
|
|
|
};
|
|
|
|
|
2017-11-03 09:39:26 +00:00
|
|
|
class GLGraphicsBufferS : public GraphicsDataNode<IGraphicsBufferS>
|
2015-10-29 04:44:38 +00:00
|
|
|
{
|
2015-11-02 10:07:15 +00:00
|
|
|
friend class GLDataFactory;
|
|
|
|
friend struct GLCommandQueue;
|
2015-10-29 04:44:38 +00:00
|
|
|
GLuint m_buf;
|
|
|
|
GLenum m_target;
|
2017-11-03 09:39:26 +00:00
|
|
|
GLGraphicsBufferS(const ObjToken<BaseGraphicsData>& parent, BufferUse use, const void* data, size_t sz)
|
|
|
|
: GraphicsDataNode<IGraphicsBufferS>(parent)
|
2015-10-29 04:44:38 +00:00
|
|
|
{
|
2015-11-21 01:12:22 +00:00
|
|
|
m_target = USE_TABLE[int(use)];
|
2015-10-29 04:44:38 +00:00
|
|
|
glGenBuffers(1, &m_buf);
|
|
|
|
glBindBuffer(m_target, m_buf);
|
|
|
|
glBufferData(m_target, sz, data, GL_STATIC_DRAW);
|
|
|
|
}
|
|
|
|
public:
|
2017-11-05 06:12:49 +00:00
|
|
|
~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);}
|
2016-03-30 19:14:17 +00:00
|
|
|
void bindUniformRange(size_t idx, GLintptr off, GLsizeiptr size) const
|
|
|
|
{glBindBufferRange(GL_UNIFORM_BUFFER, idx, m_buf, off, size);}
|
2015-10-29 04:44:38 +00:00
|
|
|
};
|
|
|
|
|
2017-11-03 09:39:26 +00:00
|
|
|
template<class DataCls>
|
|
|
|
class GLGraphicsBufferD : public GraphicsDataNode<IGraphicsBufferD, DataCls>
|
2015-10-29 04:44:38 +00:00
|
|
|
{
|
2015-11-02 10:07:15 +00:00
|
|
|
friend class GLDataFactory;
|
2017-03-05 07:54:58 +00:00
|
|
|
friend class GLDataFactoryImpl;
|
2015-11-02 10:07:15 +00:00
|
|
|
friend struct GLCommandQueue;
|
2015-10-30 00:00:56 +00:00
|
|
|
GLuint m_bufs[3];
|
2015-10-29 04:44:38 +00:00
|
|
|
GLenum m_target;
|
2015-12-02 21:09:49 +00:00
|
|
|
std::unique_ptr<uint8_t[]> m_cpuBuf;
|
|
|
|
size_t m_cpuSz = 0;
|
|
|
|
int m_validMask = 0;
|
2017-11-03 09:39:26 +00:00
|
|
|
GLGraphicsBufferD(const ObjToken<DataCls>& parent, BufferUse use, size_t sz)
|
|
|
|
: GraphicsDataNode<IGraphicsBufferD, DataCls>(parent),
|
2017-03-14 07:02:53 +00:00
|
|
|
m_target(USE_TABLE[int(use)]), m_cpuBuf(new uint8_t[sz]), m_cpuSz(sz)
|
2015-10-29 04:44:38 +00:00
|
|
|
{
|
2015-10-30 00:00:56 +00:00
|
|
|
glGenBuffers(3, m_bufs);
|
2016-01-08 00:04:37 +00:00
|
|
|
for (int i=0 ; i<3 ; ++i)
|
|
|
|
{
|
|
|
|
glBindBuffer(m_target, m_bufs[i]);
|
|
|
|
glBufferData(m_target, m_cpuSz, nullptr, GL_STREAM_DRAW);
|
|
|
|
}
|
2015-10-29 04:44:38 +00:00
|
|
|
}
|
|
|
|
public:
|
2017-11-05 06:12:49 +00:00
|
|
|
~GLGraphicsBufferD() { glDeleteBuffers(3, m_bufs); }
|
2015-10-29 04:44:38 +00:00
|
|
|
|
2017-11-05 06:12:49 +00:00
|
|
|
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
|
|
|
|
2017-11-05 06:12:49 +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)
|
|
|
|
{
|
2017-12-20 06:04:11 +00:00
|
|
|
if (sz > m_cpuSz)
|
2017-11-05 06:12:49 +00:00
|
|
|
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);}
|
2015-10-29 04:44:38 +00:00
|
|
|
};
|
|
|
|
|
2017-11-03 09:39:26 +00:00
|
|
|
ObjToken<IGraphicsBufferS>
|
2016-03-30 19:14:17 +00:00
|
|
|
GLDataFactory::Context::newStaticBuffer(BufferUse use, const void* data, size_t stride, size_t count)
|
2015-10-29 04:44:38 +00:00
|
|
|
{
|
2017-11-03 09:39:26 +00:00
|
|
|
return {new GLGraphicsBufferS(m_data, use, data, stride * count)};
|
2015-10-29 04:44:38 +00:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
2018-06-07 04:36:17 +00:00
|
|
|
case TextureClampMode::ClampToBlack:
|
|
|
|
{
|
|
|
|
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[] = {0.f, 0.f, 0.f, 1.f};
|
|
|
|
glTexParameterfv(target, GL_TEXTURE_BORDER_COLOR, color);
|
|
|
|
break;
|
|
|
|
}
|
2017-11-17 08:55:51 +00:00
|
|
|
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;
|
|
|
|
}
|
2018-02-01 23:12:42 +00:00
|
|
|
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;
|
|
|
|
}
|
2018-11-08 00:52:25 +00:00
|
|
|
default:
|
|
|
|
break;
|
2017-10-01 04:23:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-03 09:39:26 +00:00
|
|
|
class GLTextureS : public GraphicsDataNode<ITextureS>
|
2015-10-29 04:44:38 +00:00
|
|
|
{
|
2015-11-02 10:07:15 +00:00
|
|
|
friend class GLDataFactory;
|
2015-10-29 04:44:38 +00:00
|
|
|
GLuint m_tex;
|
2018-11-08 00:52:25 +00:00
|
|
|
TextureClampMode m_clampMode = TextureClampMode::Invalid;
|
2017-11-03 09:39:26 +00:00
|
|
|
GLTextureS(const ObjToken<BaseGraphicsData>& parent, size_t width, size_t height, size_t mips,
|
2018-01-07 05:17:14 +00:00
|
|
|
TextureFormat fmt, TextureClampMode clampMode, GLint aniso, const void* data, size_t sz)
|
2017-11-03 09:39:26 +00:00
|
|
|
: GraphicsDataNode<ITextureS>(parent)
|
2015-10-29 04:44:38 +00:00
|
|
|
{
|
|
|
|
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
|
|
|
{
|
2018-02-01 23:12:42 +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);
|
2015-12-02 21:09:49 +00:00
|
|
|
|
2018-01-07 05:17:14 +00:00
|
|
|
if (GLEW_EXT_texture_filter_anisotropic)
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso);
|
|
|
|
|
2017-10-01 04:23:28 +00:00
|
|
|
SetClampMode(GL_TEXTURE_2D, clampMode);
|
|
|
|
|
2015-12-02 21:09:49 +00:00
|
|
|
GLenum intFormat, format;
|
|
|
|
int pxPitch;
|
2016-02-16 05:47:09 +00:00
|
|
|
bool compressed = false;
|
2015-12-02 21:09:49 +00:00
|
|
|
switch (fmt)
|
2015-10-29 04:44:38 +00:00
|
|
|
{
|
2015-12-02 21:09:49 +00:00
|
|
|
case TextureFormat::RGBA8:
|
2017-01-29 03:56:17 +00:00
|
|
|
intFormat = GL_RGBA8;
|
2015-12-02 21:09:49 +00:00
|
|
|
format = GL_RGBA;
|
|
|
|
pxPitch = 4;
|
|
|
|
break;
|
|
|
|
case TextureFormat::I8:
|
|
|
|
intFormat = GL_R8;
|
|
|
|
format = GL_RED;
|
|
|
|
pxPitch = 1;
|
|
|
|
break;
|
2018-01-20 03:02:29 +00:00
|
|
|
case TextureFormat::I16:
|
|
|
|
intFormat = GL_R16;
|
|
|
|
format = GL_RED;
|
|
|
|
pxPitch = 2;
|
|
|
|
break;
|
2016-02-16 05:47:09 +00:00
|
|
|
case TextureFormat::DXT1:
|
2016-02-16 19:41:16 +00:00
|
|
|
intFormat = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
|
2016-02-16 05:47:09 +00:00
|
|
|
compressed = true;
|
|
|
|
break;
|
2015-12-02 21:09:49 +00:00
|
|
|
default:
|
2016-03-04 23:02:18 +00:00
|
|
|
Log.report(logvisor::Fatal, "unsupported tex format");
|
2015-12-02 21:09:49 +00:00
|
|
|
}
|
|
|
|
|
2016-02-16 05:47:09 +00:00
|
|
|
if (compressed)
|
2015-12-02 21:09:49 +00:00
|
|
|
{
|
2016-02-16 05:47:09 +00:00
|
|
|
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;
|
2016-02-16 05:47:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-01-20 05:50:01 +00:00
|
|
|
GLenum compType = intFormat == GL_R16 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_BYTE;
|
2016-02-16 05:47:09 +00:00
|
|
|
for (size_t i=0 ; i<mips ; ++i)
|
|
|
|
{
|
2018-01-20 05:50:01 +00:00
|
|
|
glTexImage2D(GL_TEXTURE_2D, i, intFormat, width, height, 0, format, compType, dataIt);
|
2016-02-16 05:47:09 +00:00
|
|
|
dataIt += width * height * pxPitch;
|
2017-01-29 03:56:17 +00:00
|
|
|
if (width > 1)
|
|
|
|
width /= 2;
|
|
|
|
if (height > 1)
|
|
|
|
height /= 2;
|
2016-02-16 05:47:09 +00:00
|
|
|
}
|
2015-10-29 04:44:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
public:
|
2017-11-05 06:12:49 +00:00
|
|
|
~GLTextureS() { glDeleteTextures(1, &m_tex); }
|
2015-10-30 00:00:56 +00:00
|
|
|
|
2017-11-17 08:55:51 +00:00
|
|
|
void setClampMode(TextureClampMode mode)
|
|
|
|
{
|
2018-11-08 00:52:25 +00:00
|
|
|
if (m_clampMode == mode)
|
|
|
|
return;
|
|
|
|
m_clampMode = mode;
|
2017-11-17 08:55:51 +00:00
|
|
|
glBindTexture(GL_TEXTURE_2D, m_tex);
|
|
|
|
SetClampMode(GL_TEXTURE_2D, mode);
|
|
|
|
}
|
|
|
|
|
2015-10-30 00:00:56 +00:00
|
|
|
void bind(size_t idx) const
|
|
|
|
{
|
|
|
|
glActiveTexture(GL_TEXTURE0 + idx);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, m_tex);
|
|
|
|
}
|
2015-10-29 04:44:38 +00:00
|
|
|
};
|
|
|
|
|
2017-11-03 09:39:26 +00:00
|
|
|
class GLTextureSA : public GraphicsDataNode<ITextureSA>
|
2015-11-23 08:49:53 +00:00
|
|
|
{
|
|
|
|
friend class GLDataFactory;
|
|
|
|
GLuint m_tex;
|
2018-11-08 00:52:25 +00:00
|
|
|
TextureClampMode m_clampMode = TextureClampMode::Invalid;
|
2017-11-03 09:39:26 +00:00
|
|
|
GLTextureSA(const ObjToken<BaseGraphicsData>& parent, size_t width, size_t height, size_t layers, size_t mips,
|
2018-01-07 05:17:14 +00:00
|
|
|
TextureFormat fmt, TextureClampMode clampMode, GLint aniso, const void* data, size_t sz)
|
2017-11-03 09:39:26 +00:00
|
|
|
: GraphicsDataNode<ITextureSA>(parent)
|
2015-11-23 08:49:53 +00:00
|
|
|
{
|
2017-01-29 03:56:17 +00:00
|
|
|
const uint8_t* dataIt = static_cast<const uint8_t*>(data);
|
2015-11-23 08:49:53 +00:00
|
|
|
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);
|
|
|
|
|
2018-01-07 05:17:14 +00:00
|
|
|
if (GLEW_EXT_texture_filter_anisotropic)
|
|
|
|
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso);
|
|
|
|
|
2017-10-01 04:23:28 +00:00
|
|
|
SetClampMode(GL_TEXTURE_2D_ARRAY, clampMode);
|
|
|
|
|
2018-05-25 06:30:42 +00:00
|
|
|
GLenum intFormat = 0, format = 0;
|
|
|
|
int pxPitch = 0;
|
2018-01-20 03:02:29 +00:00
|
|
|
switch (fmt)
|
2017-01-29 03:56:17 +00:00
|
|
|
{
|
2018-01-20 03:02:29 +00:00
|
|
|
case TextureFormat::RGBA8:
|
2017-01-29 03:56:17 +00:00
|
|
|
intFormat = GL_RGBA8;
|
|
|
|
format = GL_RGBA;
|
|
|
|
pxPitch = 4;
|
2018-01-20 03:02:29 +00:00
|
|
|
break;
|
|
|
|
case TextureFormat::I8:
|
2017-01-29 03:56:17 +00:00
|
|
|
intFormat = GL_R8;
|
|
|
|
format = GL_RED;
|
|
|
|
pxPitch = 1;
|
2018-01-20 03:02:29 +00:00
|
|
|
break;
|
|
|
|
case TextureFormat::I16:
|
|
|
|
intFormat = GL_R16;
|
|
|
|
format = GL_RED;
|
|
|
|
pxPitch = 2;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
Log.report(logvisor::Fatal, "unsupported tex format");
|
2017-01-29 03:56:17 +00:00
|
|
|
}
|
|
|
|
|
2018-01-20 05:50:01 +00:00
|
|
|
GLenum compType = intFormat == GL_R16 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_BYTE;
|
2017-01-29 03:56:17 +00:00
|
|
|
for (size_t i=0 ; i<mips ; ++i)
|
|
|
|
{
|
2018-01-20 05:50:01 +00:00
|
|
|
glTexImage3D(GL_TEXTURE_2D_ARRAY, i, intFormat, width, height, layers, 0, format, compType, dataIt);
|
2017-01-29 03:56:17 +00:00
|
|
|
dataIt += width * height * layers * pxPitch;
|
|
|
|
if (width > 1)
|
|
|
|
width /= 2;
|
|
|
|
if (height > 1)
|
|
|
|
height /= 2;
|
|
|
|
}
|
2015-11-23 08:49:53 +00:00
|
|
|
}
|
|
|
|
public:
|
2017-11-05 06:12:49 +00:00
|
|
|
~GLTextureSA() { glDeleteTextures(1, &m_tex); }
|
2015-11-23 08:49:53 +00:00
|
|
|
|
2017-11-17 08:55:51 +00:00
|
|
|
void setClampMode(TextureClampMode mode)
|
|
|
|
{
|
2018-11-08 00:52:25 +00:00
|
|
|
if (m_clampMode == mode)
|
|
|
|
return;
|
|
|
|
m_clampMode = mode;
|
2017-11-17 08:55:51 +00:00
|
|
|
glBindTexture(GL_TEXTURE_2D_ARRAY, m_tex);
|
|
|
|
SetClampMode(GL_TEXTURE_2D_ARRAY, mode);
|
|
|
|
}
|
|
|
|
|
2015-11-23 08:49:53 +00:00
|
|
|
void bind(size_t idx) const
|
|
|
|
{
|
|
|
|
glActiveTexture(GL_TEXTURE0 + idx);
|
|
|
|
glBindTexture(GL_TEXTURE_2D_ARRAY, m_tex);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-11-03 09:39:26 +00:00
|
|
|
class GLTextureD : public GraphicsDataNode<ITextureD>
|
2015-10-29 04:44:38 +00:00
|
|
|
{
|
2015-11-02 10:07:15 +00:00
|
|
|
friend class GLDataFactory;
|
|
|
|
friend struct GLCommandQueue;
|
2015-11-04 01:02:05 +00:00
|
|
|
GLuint m_texs[3];
|
2015-12-02 21:09:49 +00:00
|
|
|
std::unique_ptr<uint8_t[]> m_cpuBuf;
|
|
|
|
size_t m_cpuSz = 0;
|
|
|
|
GLenum m_intFormat, m_format;
|
2015-10-29 04:44:38 +00:00
|
|
|
size_t m_width = 0;
|
|
|
|
size_t m_height = 0;
|
2015-12-02 21:09:49 +00:00
|
|
|
int m_validMask = 0;
|
2018-11-08 00:52:25 +00:00
|
|
|
TextureClampMode m_clampMode = TextureClampMode::Invalid;
|
2017-11-05 06:12:49 +00:00
|
|
|
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;
|
2018-01-20 03:02:29 +00:00
|
|
|
case TextureFormat::I16:
|
|
|
|
m_intFormat = GL_R16;
|
|
|
|
m_format = GL_RED;
|
|
|
|
pxPitch = 2;
|
|
|
|
break;
|
2017-11-05 06:12:49 +00:00
|
|
|
default:
|
|
|
|
Log.report(logvisor::Fatal, "unsupported tex format");
|
|
|
|
}
|
|
|
|
m_cpuSz = width * height * pxPitch;
|
|
|
|
m_cpuBuf.reset(new uint8_t[m_cpuSz]);
|
|
|
|
|
2018-01-20 05:50:01 +00:00
|
|
|
GLenum compType = m_intFormat == GL_R16 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_BYTE;
|
2017-11-05 06:12:49 +00:00
|
|
|
glGenTextures(3, m_texs);
|
|
|
|
for (int i=0 ; i<3 ; ++i)
|
|
|
|
{
|
|
|
|
glBindTexture(GL_TEXTURE_2D, m_texs[i]);
|
2018-01-20 05:50:01 +00:00
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, m_intFormat, width, height, 0, m_format, compType, nullptr);
|
2017-11-05 06:12:49 +00:00
|
|
|
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-10-29 04:44:38 +00:00
|
|
|
public:
|
2017-11-05 06:12:49 +00:00
|
|
|
~GLTextureD() { glDeleteTextures(3, m_texs); }
|
2015-10-29 04:44:38 +00:00
|
|
|
|
2017-11-05 06:12:49 +00:00
|
|
|
void update(int b)
|
|
|
|
{
|
|
|
|
int slot = 1 << b;
|
|
|
|
if ((slot & m_validMask) == 0)
|
|
|
|
{
|
|
|
|
glBindTexture(GL_TEXTURE_2D, m_texs[b]);
|
2018-01-20 05:50:01 +00:00
|
|
|
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());
|
2017-11-05 06:12:49 +00:00
|
|
|
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
|
|
|
|
2017-11-17 08:55:51 +00:00
|
|
|
void setClampMode(TextureClampMode mode)
|
|
|
|
{
|
2018-11-08 00:52:25 +00:00
|
|
|
if (m_clampMode == mode)
|
|
|
|
return;
|
|
|
|
m_clampMode = mode;
|
2017-11-17 08:55:51 +00:00
|
|
|
for (int i=0 ; i<3 ; ++i)
|
|
|
|
{
|
|
|
|
glBindTexture(GL_TEXTURE_2D, m_texs[i]);
|
|
|
|
SetClampMode(GL_TEXTURE_2D, mode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-05 06:12:49 +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
|
|
|
};
|
|
|
|
|
2017-03-14 07:02:53 +00:00
|
|
|
#define MAX_BIND_TEXS 4
|
|
|
|
|
2017-11-03 09:39:26 +00:00
|
|
|
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] = {};
|
2017-03-14 07:02:53 +00:00
|
|
|
GLuint m_bindTexs[2][MAX_BIND_TEXS] = {};
|
2018-01-07 05:17:14 +00:00
|
|
|
GLuint m_bindFBOs[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;
|
2018-01-16 06:29:43 +00:00
|
|
|
GLenum m_colorFormat;
|
2017-11-17 08:55:51 +00:00
|
|
|
size_t m_colorBindCount;
|
|
|
|
size_t m_depthBindCount;
|
2018-01-16 06:29:43 +00:00
|
|
|
GLTextureR(const ObjToken<BaseGraphicsData>& parent, GLCommandQueue* q, size_t width, size_t height,
|
|
|
|
size_t samples, GLenum colorFormat, TextureClampMode clampMode,
|
|
|
|
size_t colorBindCount, size_t depthBindCount);
|
2015-11-04 01:02:05 +00:00
|
|
|
public:
|
2017-11-05 06:12:49 +00:00
|
|
|
~GLTextureR()
|
|
|
|
{
|
|
|
|
glDeleteTextures(2, m_texs);
|
|
|
|
glDeleteTextures(MAX_BIND_TEXS * 2, m_bindTexs[0]);
|
2018-01-07 05:17:14 +00:00
|
|
|
if (m_samples > 1)
|
|
|
|
glDeleteFramebuffers(MAX_BIND_TEXS * 2, m_bindFBOs[0]);
|
2017-11-05 06:12:49 +00:00
|
|
|
glDeleteFramebuffers(1, &m_fbo);
|
|
|
|
}
|
2015-10-30 00:00:56 +00:00
|
|
|
|
2017-11-17 08:55:51 +00:00
|
|
|
void setClampMode(TextureClampMode mode)
|
|
|
|
{
|
|
|
|
for (int i=0 ; i<m_colorBindCount ; ++i)
|
|
|
|
{
|
|
|
|
glBindTexture(GL_TEXTURE_2D, m_bindTexs[0][i]);
|
|
|
|
SetClampMode(GL_TEXTURE_2D, mode);
|
|
|
|
}
|
|
|
|
for (int i=0 ; i<m_depthBindCount ; ++i)
|
|
|
|
{
|
|
|
|
glBindTexture(GL_TEXTURE_2D, m_bindTexs[1][i]);
|
|
|
|
SetClampMode(GL_TEXTURE_2D, mode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-14 07:02:53 +00:00
|
|
|
void bind(size_t idx, int bindIdx, bool depth) const
|
2015-10-30 00:00:56 +00:00
|
|
|
{
|
|
|
|
glActiveTexture(GL_TEXTURE0 + idx);
|
2018-01-07 05:17:14 +00:00
|
|
|
glBindTexture(GL_TEXTURE_2D, m_bindTexs[depth][bindIdx]);
|
2015-10-30 00:00:56 +00:00
|
|
|
}
|
2016-02-16 19:41:16 +00:00
|
|
|
|
2015-11-04 01:58:36 +00:00
|
|
|
void resize(size_t width, size_t height)
|
|
|
|
{
|
|
|
|
m_width = width;
|
|
|
|
m_height = height;
|
2016-02-16 19:41:16 +00:00
|
|
|
|
2018-01-20 05:50:01 +00:00
|
|
|
GLenum compType = m_colorFormat == GL_RGBA16 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_BYTE;
|
2016-01-08 00:04:37 +00:00
|
|
|
if (m_samples > 1)
|
|
|
|
{
|
|
|
|
glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, m_texs[0]);
|
2018-01-16 06:29:43 +00:00
|
|
|
glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, m_samples, m_colorFormat, width, height, GL_FALSE);
|
2016-01-08 00:04:37 +00:00
|
|
|
glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, m_texs[1]);
|
2018-05-10 07:24:42 +00:00
|
|
|
glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, m_samples, GL_DEPTH_COMPONENT32F, width, height, GL_FALSE);
|
2016-01-08 00:04:37 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
glBindTexture(GL_TEXTURE_2D, m_texs[0]);
|
2018-01-20 05:50:01 +00:00
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, m_colorFormat, width, height, 0, GL_RGBA, compType, nullptr);
|
2016-01-08 00:04:37 +00:00
|
|
|
glBindTexture(GL_TEXTURE_2D, m_texs[1]);
|
2018-05-10 07:24:42 +00:00
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32F, 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
|
|
|
}
|
2016-02-25 02:53:23 +00:00
|
|
|
|
2017-10-01 04:23:28 +00:00
|
|
|
for (int i=0 ; i<MAX_BIND_TEXS ; ++i)
|
|
|
|
{
|
|
|
|
if (m_bindTexs[0][i])
|
2016-02-25 02:53:23 +00:00
|
|
|
{
|
2017-10-01 04:23:28 +00:00
|
|
|
glBindTexture(GL_TEXTURE_2D, m_bindTexs[0][i]);
|
2018-01-20 05:50:01 +00:00
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, m_colorFormat, width, height, 0, GL_RGBA, compType, nullptr);
|
2016-02-25 02:53:23 +00:00
|
|
|
}
|
2017-10-01 04:23:28 +00:00
|
|
|
}
|
2017-03-14 07:02:53 +00:00
|
|
|
|
2017-10-01 04:23:28 +00:00
|
|
|
for (int i=0 ; i<MAX_BIND_TEXS ; ++i)
|
|
|
|
{
|
|
|
|
if (m_bindTexs[1][i])
|
2016-02-25 02:53:23 +00:00
|
|
|
{
|
2017-10-01 04:23:28 +00:00
|
|
|
glBindTexture(GL_TEXTURE_2D, m_bindTexs[1][i]);
|
2018-05-10 07:24:42 +00:00
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32F, width, height, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, nullptr);
|
2016-02-25 02:53:23 +00:00
|
|
|
}
|
2016-01-08 00:04:37 +00:00
|
|
|
}
|
2015-11-04 01:58:36 +00:00
|
|
|
}
|
2015-10-29 04:44:38 +00:00
|
|
|
};
|
|
|
|
|
2017-11-03 09:39:26 +00:00
|
|
|
ObjToken<ITextureS>
|
2016-03-30 19:14:17 +00:00
|
|
|
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)
|
2015-10-29 04:44:38 +00:00
|
|
|
{
|
2018-01-07 05:17:14 +00:00
|
|
|
GLDataFactoryImpl& factory = static_cast<GLDataFactoryImpl&>(m_parent);
|
|
|
|
return {new GLTextureS(m_data, width, height, mips, fmt, clampMode,
|
|
|
|
factory.m_glCtx->m_anisotropy, data, sz)};
|
2015-10-29 04:44:38 +00:00
|
|
|
}
|
|
|
|
|
2017-11-03 09:39:26 +00:00
|
|
|
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,
|
2017-11-03 09:39:26 +00:00
|
|
|
TextureFormat fmt, TextureClampMode clampMode,
|
|
|
|
const void *data, size_t sz)
|
2015-11-23 08:49:53 +00:00
|
|
|
{
|
2018-01-07 05:17:14 +00:00
|
|
|
GLDataFactoryImpl& factory = static_cast<GLDataFactoryImpl&>(m_parent);
|
|
|
|
return {new GLTextureSA(m_data, width, height, layers, mips, fmt, clampMode,
|
|
|
|
factory.m_glCtx->m_anisotropy, data, sz)};
|
2015-11-23 08:49:53 +00:00
|
|
|
}
|
|
|
|
|
2018-06-07 04:36:17 +00:00
|
|
|
static const GLenum PRIMITIVE_TABLE[] =
|
|
|
|
{
|
|
|
|
GL_TRIANGLES,
|
|
|
|
GL_TRIANGLE_STRIP,
|
|
|
|
GL_PATCHES
|
|
|
|
};
|
|
|
|
|
|
|
|
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
|
|
|
|
};
|
|
|
|
|
2018-10-07 02:49:22 +00:00
|
|
|
static const GLenum SHADER_STAGE_TABLE[] =
|
|
|
|
{
|
|
|
|
0,
|
|
|
|
GL_VERTEX_SHADER,
|
|
|
|
GL_FRAGMENT_SHADER,
|
|
|
|
GL_GEOMETRY_SHADER,
|
|
|
|
GL_TESS_CONTROL_SHADER,
|
|
|
|
GL_TESS_EVALUATION_SHADER
|
|
|
|
};
|
|
|
|
|
|
|
|
class GLShaderStage : public GraphicsDataNode<IShaderStage>
|
|
|
|
{
|
|
|
|
friend class GLDataFactory;
|
|
|
|
GLuint m_shad = 0;
|
|
|
|
std::vector<std::pair<std::string, int>> m_texNames;
|
2018-10-11 20:47:37 +00:00
|
|
|
std::vector<std::pair<std::string, int>> m_blockNames;
|
2018-10-07 02:49:22 +00:00
|
|
|
|
|
|
|
static constexpr EShLanguage ShaderTypes[] =
|
|
|
|
{
|
|
|
|
EShLangVertex,
|
|
|
|
EShLangVertex,
|
|
|
|
EShLangFragment,
|
|
|
|
EShLangGeometry,
|
|
|
|
EShLangTessControl,
|
|
|
|
EShLangTessEvaluation
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Use glslang's reflection API to pull out uniform indices from Vulkan
|
|
|
|
* version of shader. Aids in glGetUniformBlockIndex and glGetUniformLocation calls */
|
|
|
|
void BuildNameLists(const char* source, PipelineStage stage)
|
|
|
|
{
|
|
|
|
EShLanguage lang = ShaderTypes[int(stage)];
|
|
|
|
const EShMessages messages = EShMessages(EShMsgSpvRules | EShMsgVulkanRules);
|
|
|
|
glslang::TShader shader(lang);
|
|
|
|
shader.setStrings(&source, 1);
|
|
|
|
if (!shader.parse(&glslang::DefaultTBuiltInResource, 110, false, messages))
|
|
|
|
{
|
|
|
|
printf("%s\n", source);
|
|
|
|
Log.report(logvisor::Fatal, "unable to compile shader\n%s", shader.getInfoLog());
|
|
|
|
}
|
|
|
|
|
|
|
|
glslang::TProgram prog;
|
|
|
|
prog.addShader(&shader);
|
|
|
|
if (!prog.link(messages))
|
|
|
|
{
|
2018-10-11 20:47:37 +00:00
|
|
|
printf("%s\n", source);
|
2018-10-07 02:49:22 +00:00
|
|
|
Log.report(logvisor::Fatal, "unable to link shader program\n%s", prog.getInfoLog());
|
|
|
|
}
|
|
|
|
|
|
|
|
prog.buildReflection();
|
|
|
|
int count = prog.getNumLiveUniformVariables();
|
|
|
|
for (int i = 0; i < count; ++i)
|
|
|
|
{
|
|
|
|
const glslang::TType* tp = prog.getUniformTType(i);
|
|
|
|
if (tp->getBasicType() != glslang::TBasicType::EbtSampler)
|
|
|
|
continue;
|
|
|
|
const auto& qual = tp->getQualifier();
|
|
|
|
if (!qual.hasBinding())
|
|
|
|
Log.report(logvisor::Fatal, "shader uniform %s does not have layout binding", prog.getUniformName(i));
|
|
|
|
m_texNames.emplace_back(std::make_pair(prog.getUniformName(i),
|
|
|
|
qual.layoutBinding - BOO_GLSL_MAX_UNIFORM_COUNT));
|
|
|
|
}
|
|
|
|
count = prog.getNumLiveUniformBlocks();
|
|
|
|
m_blockNames.reserve(count);
|
|
|
|
for (int i = 0; i < count; ++i)
|
2018-10-11 20:47:37 +00:00
|
|
|
{
|
|
|
|
const glslang::TType* tp = prog.getUniformBlockTType(i);
|
|
|
|
const auto& qual = tp->getQualifier();
|
|
|
|
if (!qual.hasBinding())
|
|
|
|
Log.report(logvisor::Fatal, "shader uniform %s does not have layout binding", prog.getUniformBlockName(i));
|
|
|
|
m_blockNames.emplace_back(std::make_pair(prog.getUniformBlockName(i), qual.layoutBinding));
|
|
|
|
}
|
2018-10-07 02:49:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GLShaderStage(const ObjToken<BaseGraphicsData>& parent, const char* source, PipelineStage stage)
|
|
|
|
: GraphicsDataNode<IShaderStage>(parent)
|
|
|
|
{
|
|
|
|
BuildNameLists(source, stage);
|
|
|
|
|
|
|
|
m_shad = glCreateShader(SHADER_STAGE_TABLE[int(stage)]);
|
|
|
|
if (!m_shad)
|
|
|
|
{
|
|
|
|
Log.report(logvisor::Fatal, "unable to create shader");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
glShaderSource(m_shad, 1, &source, nullptr);
|
|
|
|
glCompileShader(m_shad);
|
|
|
|
GLint status;
|
|
|
|
glGetShaderiv(m_shad, GL_COMPILE_STATUS, &status);
|
|
|
|
if (status != GL_TRUE)
|
|
|
|
{
|
|
|
|
GLint logLen;
|
|
|
|
glGetShaderiv(m_shad, GL_INFO_LOG_LENGTH, &logLen);
|
|
|
|
std::unique_ptr<char[]> log(new char[logLen]);
|
|
|
|
glGetShaderInfoLog(m_shad, logLen, nullptr, log.get());
|
|
|
|
Log.report(logvisor::Fatal, "unable to compile source\n%s\n%s\n", log.get(), source);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public:
|
|
|
|
~GLShaderStage() { if (m_shad) glDeleteShader(m_shad); }
|
|
|
|
GLuint getShader() const { return m_shad; }
|
|
|
|
const std::vector<std::pair<std::string, int>>& getTexNames() const { return m_texNames; }
|
2018-10-11 20:47:37 +00:00
|
|
|
const std::vector<std::pair<std::string, int>>& getBlockNames() const { return m_blockNames; }
|
2018-10-07 02:49:22 +00:00
|
|
|
};
|
|
|
|
|
2017-11-03 09:39:26 +00:00
|
|
|
class GLShaderPipeline : public GraphicsDataNode<IShaderPipeline>
|
2015-10-29 04:44:38 +00:00
|
|
|
{
|
2018-06-07 04:36:17 +00:00
|
|
|
protected:
|
2015-11-02 10:07:15 +00:00
|
|
|
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;
|
2018-10-07 02:49:22 +00:00
|
|
|
mutable ObjToken<IShaderStage> m_vertex;
|
|
|
|
mutable ObjToken<IShaderStage> m_fragment;
|
|
|
|
mutable ObjToken<IShaderStage> m_geometry;
|
|
|
|
mutable ObjToken<IShaderStage> m_control;
|
|
|
|
mutable ObjToken<IShaderStage> m_evaluation;
|
|
|
|
std::vector<VertexElementDescriptor> m_elements;
|
|
|
|
size_t baseVert = 0;
|
|
|
|
size_t baseInst = 0;
|
2017-11-02 09:24:50 +00:00
|
|
|
mutable GLuint m_prog = 0;
|
2015-10-29 04:44:38 +00:00
|
|
|
GLenum m_sfactor = GL_ONE;
|
|
|
|
GLenum m_dfactor = GL_ZERO;
|
2016-03-24 08:05:19 +00:00
|
|
|
GLenum m_drawPrim = GL_TRIANGLES;
|
2017-03-14 07:02:53 +00:00
|
|
|
ZTest m_depthTest = ZTest::LEqual;
|
2015-10-30 00:00:56 +00:00
|
|
|
bool m_depthWrite = true;
|
2017-03-14 07:02:53 +00:00
|
|
|
bool m_colorWrite = true;
|
|
|
|
bool m_alphaWrite = true;
|
2017-09-05 02:59:41 +00:00
|
|
|
bool m_subtractBlend = false;
|
2018-02-05 06:52:54 +00:00
|
|
|
bool m_overwriteAlpha = false;
|
2017-03-10 20:38:00 +00:00
|
|
|
CullMode m_culling;
|
2018-10-07 02:49:22 +00:00
|
|
|
uint32_t m_patchSize = 0;
|
2018-10-11 20:47:37 +00:00
|
|
|
mutable GLint m_uniLocs[BOO_GLSL_MAX_UNIFORM_COUNT];
|
2018-10-17 03:26:07 +00:00
|
|
|
GLShaderPipeline(const ObjToken<BaseGraphicsData>& parent,
|
|
|
|
ObjToken<IShaderStage> vertex, ObjToken<IShaderStage> fragment,
|
|
|
|
ObjToken<IShaderStage> geometry, ObjToken<IShaderStage> control,
|
|
|
|
ObjToken<IShaderStage> evaluation, const VertexFormatInfo& vtxFmt,
|
|
|
|
const AdditionalPipelineInfo& info)
|
2018-06-07 04:36:17 +00:00
|
|
|
: GraphicsDataNode<IShaderPipeline>(parent)
|
|
|
|
{
|
2018-10-11 20:47:37 +00:00
|
|
|
std::fill(std::begin(m_uniLocs), std::end(m_uniLocs), -1);
|
|
|
|
|
2018-10-07 02:49:22 +00:00
|
|
|
if (info.srcFac == BlendFactor::Subtract || info.dstFac == BlendFactor::Subtract)
|
2018-06-07 04:36:17 +00:00
|
|
|
{
|
|
|
|
m_sfactor = GL_SRC_ALPHA;
|
|
|
|
m_dfactor = GL_ONE;
|
|
|
|
m_subtractBlend = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-10-07 02:49:22 +00:00
|
|
|
m_sfactor = BLEND_FACTOR_TABLE[int(info.srcFac)];
|
|
|
|
m_dfactor = BLEND_FACTOR_TABLE[int(info.dstFac)];
|
2018-06-07 04:36:17 +00:00
|
|
|
m_subtractBlend = false;
|
|
|
|
}
|
|
|
|
|
2018-10-07 02:49:22 +00:00
|
|
|
m_depthTest = info.depthTest;
|
|
|
|
m_depthWrite = info.depthWrite;
|
|
|
|
m_colorWrite = info.colorWrite;
|
|
|
|
m_alphaWrite = info.alphaWrite;
|
|
|
|
m_overwriteAlpha = info.overwriteAlpha;
|
|
|
|
m_culling = info.culling;
|
|
|
|
m_drawPrim = PRIMITIVE_TABLE[int(info.prim)];
|
|
|
|
m_patchSize = info.patchSize;
|
2018-10-17 03:26:07 +00:00
|
|
|
|
|
|
|
m_vertex = vertex;
|
|
|
|
m_fragment = fragment;
|
|
|
|
m_geometry = geometry;
|
|
|
|
m_control = control;
|
|
|
|
m_evaluation = evaluation;
|
|
|
|
|
|
|
|
if (control && evaluation)
|
|
|
|
m_drawPrim = GL_PATCHES;
|
|
|
|
|
|
|
|
m_elements.reserve(vtxFmt.elementCount);
|
|
|
|
for (size_t i=0 ; i<vtxFmt.elementCount ; ++i)
|
|
|
|
m_elements.push_back(vtxFmt.elements[i]);
|
2018-06-07 04:36:17 +00:00
|
|
|
}
|
2015-10-29 04:44:38 +00:00
|
|
|
public:
|
2017-03-05 23:34:24 +00:00
|
|
|
~GLShaderPipeline() { if (m_prog) glDeleteProgram(m_prog); }
|
2015-10-29 04:44:38 +00:00
|
|
|
|
2015-10-30 00:00:56 +00:00
|
|
|
GLuint bind() const
|
|
|
|
{
|
2017-11-02 09:24:50 +00:00
|
|
|
if (!m_prog)
|
|
|
|
{
|
|
|
|
m_prog = glCreateProgram();
|
|
|
|
if (!m_prog)
|
|
|
|
{
|
|
|
|
Log.report(logvisor::Error, "unable to create shader program");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-10-07 02:49:22 +00:00
|
|
|
if (m_vertex)
|
|
|
|
glAttachShader(m_prog, m_vertex.cast<GLShaderStage>()->getShader());
|
|
|
|
if (m_fragment)
|
|
|
|
glAttachShader(m_prog, m_fragment.cast<GLShaderStage>()->getShader());
|
|
|
|
if (m_geometry)
|
|
|
|
glAttachShader(m_prog, m_geometry.cast<GLShaderStage>()->getShader());
|
|
|
|
if (m_control)
|
|
|
|
glAttachShader(m_prog, m_control.cast<GLShaderStage>()->getShader());
|
|
|
|
if (m_evaluation)
|
|
|
|
glAttachShader(m_prog, m_evaluation.cast<GLShaderStage>()->getShader());
|
2017-11-02 09:24:50 +00:00
|
|
|
|
|
|
|
glLinkProgram(m_prog);
|
|
|
|
|
2018-10-07 02:49:22 +00:00
|
|
|
if (m_vertex)
|
|
|
|
glDetachShader(m_prog, m_vertex.cast<GLShaderStage>()->getShader());
|
|
|
|
if (m_fragment)
|
|
|
|
glDetachShader(m_prog, m_fragment.cast<GLShaderStage>()->getShader());
|
|
|
|
if (m_geometry)
|
|
|
|
glDetachShader(m_prog, m_geometry.cast<GLShaderStage>()->getShader());
|
|
|
|
if (m_control)
|
|
|
|
glDetachShader(m_prog, m_control.cast<GLShaderStage>()->getShader());
|
|
|
|
if (m_evaluation)
|
|
|
|
glDetachShader(m_prog, m_evaluation.cast<GLShaderStage>()->getShader());
|
2017-11-02 09:24:50 +00:00
|
|
|
|
|
|
|
GLint status;
|
|
|
|
glGetProgramiv(m_prog, GL_LINK_STATUS, &status);
|
|
|
|
if (status != GL_TRUE)
|
|
|
|
{
|
|
|
|
GLint logLen;
|
|
|
|
glGetProgramiv(m_prog, GL_INFO_LOG_LENGTH, &logLen);
|
2017-11-03 09:39:26 +00:00
|
|
|
std::unique_ptr<char[]> log(new char[logLen]);
|
|
|
|
glGetProgramInfoLog(m_prog, logLen, nullptr, log.get());
|
2017-11-24 22:07:23 +00:00
|
|
|
Log.report(logvisor::Fatal, "unable to link shader program\n%s\n", log.get());
|
2017-11-02 09:24:50 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
glUseProgram(m_prog);
|
|
|
|
|
2018-10-07 02:49:22 +00:00
|
|
|
for (const auto& shader : {m_vertex, m_fragment, m_geometry, m_control, m_evaluation})
|
2017-11-02 09:24:50 +00:00
|
|
|
{
|
2018-10-07 02:49:22 +00:00
|
|
|
if (const GLShaderStage* stage = shader.cast<GLShaderStage>())
|
2017-11-02 09:24:50 +00:00
|
|
|
{
|
2018-10-07 02:49:22 +00:00
|
|
|
for (const auto& name : stage->getBlockNames())
|
|
|
|
{
|
2018-10-11 20:47:37 +00:00
|
|
|
GLint uniLoc = glGetUniformBlockIndex(m_prog, name.first.c_str());
|
2018-10-07 02:49:22 +00:00
|
|
|
//if (uniLoc < 0)
|
|
|
|
// Log.report(logvisor::Warning, "unable to find uniform block '%s'", uniformBlockNames[i]);
|
2018-10-11 20:47:37 +00:00
|
|
|
m_uniLocs[name.second] = uniLoc;
|
2018-10-07 02:49:22 +00:00
|
|
|
}
|
|
|
|
for (const auto& name : stage->getTexNames())
|
|
|
|
{
|
|
|
|
GLint texLoc = glGetUniformLocation(m_prog, name.first.c_str());
|
|
|
|
if (texLoc < 0)
|
|
|
|
{ /* Log.report(logvisor::Warning, "unable to find sampler variable '%s'", texNames[i]); */ }
|
|
|
|
else
|
|
|
|
glUniform1i(texLoc, name.second);
|
|
|
|
}
|
2017-11-02 09:24:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-07 02:49:22 +00:00
|
|
|
m_vertex.reset();
|
|
|
|
m_fragment.reset();
|
|
|
|
m_geometry.reset();
|
|
|
|
m_control.reset();
|
|
|
|
m_evaluation.reset();
|
2017-11-02 09:24:50 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
glUseProgram(m_prog);
|
|
|
|
}
|
2015-10-29 04:44:38 +00:00
|
|
|
|
2015-10-30 00:00:56 +00:00
|
|
|
if (m_dfactor != GL_ZERO)
|
|
|
|
{
|
|
|
|
glEnable(GL_BLEND);
|
2018-02-05 06:52:54 +00:00
|
|
|
if (m_overwriteAlpha)
|
|
|
|
glBlendFuncSeparate(m_sfactor, m_dfactor, GL_ONE, GL_ZERO);
|
|
|
|
else
|
|
|
|
glBlendFuncSeparate(m_sfactor, m_dfactor, m_sfactor, m_dfactor);
|
2017-09-05 02:59:41 +00:00
|
|
|
if (m_subtractBlend)
|
2018-02-05 06:52:54 +00:00
|
|
|
glBlendEquationSeparate(GL_FUNC_REVERSE_SUBTRACT,
|
|
|
|
m_overwriteAlpha ? GL_FUNC_ADD : GL_FUNC_REVERSE_SUBTRACT);
|
2017-09-05 02:59:41 +00:00
|
|
|
else
|
|
|
|
glBlendEquation(GL_FUNC_ADD);
|
2015-10-30 00:00:56 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
glDisable(GL_BLEND);
|
2015-10-29 04:44:38 +00:00
|
|
|
|
2017-03-14 07:02:53 +00:00
|
|
|
if (m_depthTest != ZTest::None)
|
|
|
|
{
|
2015-10-30 00:00:56 +00:00
|
|
|
glEnable(GL_DEPTH_TEST);
|
2017-03-14 07:02:53 +00:00
|
|
|
switch (m_depthTest)
|
|
|
|
{
|
|
|
|
case ZTest::LEqual:
|
|
|
|
default:
|
|
|
|
glDepthFunc(GL_LEQUAL);
|
|
|
|
break;
|
|
|
|
case ZTest::Greater:
|
|
|
|
glDepthFunc(GL_GREATER);
|
|
|
|
break;
|
2017-09-03 05:52:53 +00:00
|
|
|
case ZTest::GEqual:
|
|
|
|
glDepthFunc(GL_GEQUAL);
|
|
|
|
break;
|
2017-03-14 07:02:53 +00:00
|
|
|
case ZTest::Equal:
|
|
|
|
glDepthFunc(GL_EQUAL);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-10-30 00:00:56 +00:00
|
|
|
else
|
|
|
|
glDisable(GL_DEPTH_TEST);
|
|
|
|
glDepthMask(m_depthWrite);
|
2017-03-14 07:02:53 +00:00
|
|
|
glColorMask(m_colorWrite, m_colorWrite, m_colorWrite, m_alphaWrite);
|
2015-10-29 04:44:38 +00:00
|
|
|
|
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-29 04:44:38 +00:00
|
|
|
|
2018-10-07 02:49:22 +00:00
|
|
|
glPatchParameteri(GL_PATCH_VERTICES, m_patchSize);
|
2018-06-07 04:36:17 +00:00
|
|
|
|
2015-10-30 00:00:56 +00:00
|
|
|
return m_prog;
|
2015-10-29 04:44:38 +00:00
|
|
|
}
|
2015-10-30 00:00:56 +00:00
|
|
|
};
|
2015-10-29 04:44:38 +00:00
|
|
|
|
2018-10-07 02:49:22 +00:00
|
|
|
ObjToken<IShaderStage>
|
|
|
|
GLDataFactory::Context::newShaderStage(const uint8_t* data, size_t size, PipelineStage stage)
|
2016-03-24 08:05:19 +00:00
|
|
|
{
|
2018-10-07 02:49:22 +00:00
|
|
|
GLDataFactoryImpl& factory = static_cast<GLDataFactoryImpl&>(m_parent);
|
2018-06-07 04:36:17 +00:00
|
|
|
|
2018-10-07 02:49:22 +00:00
|
|
|
if (stage == PipelineStage::Control || stage == PipelineStage::Evaluation)
|
2018-06-07 04:36:17 +00:00
|
|
|
{
|
2018-10-07 02:49:22 +00:00
|
|
|
if (!factory.m_hasTessellation)
|
|
|
|
Log.report(logvisor::Fatal, "Device does not support tessellation shaders");
|
2018-06-07 04:36:17 +00:00
|
|
|
}
|
|
|
|
|
2018-10-07 02:49:22 +00:00
|
|
|
return {new GLShaderStage(m_data, (char*)data, stage)};
|
|
|
|
}
|
2015-10-29 04:44:38 +00:00
|
|
|
|
2018-10-07 02:49:22 +00:00
|
|
|
ObjToken<IShaderPipeline>
|
|
|
|
GLDataFactory::Context::newShaderPipeline(ObjToken<IShaderStage> vertex, ObjToken<IShaderStage> fragment,
|
|
|
|
ObjToken<IShaderStage> geometry, ObjToken<IShaderStage> control,
|
|
|
|
ObjToken<IShaderStage> evaluation, const VertexFormatInfo& vtxFmt,
|
|
|
|
const AdditionalPipelineInfo& additionalInfo)
|
2015-10-29 04:44:38 +00:00
|
|
|
{
|
2018-10-07 02:49:22 +00:00
|
|
|
GLDataFactoryImpl& factory = static_cast<GLDataFactoryImpl&>(m_parent);
|
2017-03-05 07:54:58 +00:00
|
|
|
|
2018-10-07 02:49:22 +00:00
|
|
|
if (control || evaluation)
|
2015-10-29 04:44:38 +00:00
|
|
|
{
|
2018-10-07 02:49:22 +00:00
|
|
|
if (!factory.m_hasTessellation)
|
|
|
|
Log.report(logvisor::Fatal, "Device does not support tessellation shaders");
|
|
|
|
if (additionalInfo.patchSize > factory.m_maxPatchSize)
|
|
|
|
Log.report(logvisor::Fatal, "Device supports %d patch vertices, %d requested",
|
|
|
|
int(factory.m_maxPatchSize), int(additionalInfo.patchSize));
|
2015-10-29 04:44:38 +00:00
|
|
|
}
|
|
|
|
|
2018-10-17 03:26:07 +00:00
|
|
|
return {new GLShaderPipeline(m_data, vertex, fragment, geometry, control,
|
|
|
|
evaluation, vtxFmt, additionalInfo)};
|
2015-10-29 04:44:38 +00:00
|
|
|
}
|
|
|
|
|
2017-11-03 09:39:26 +00:00
|
|
|
struct GLShaderDataBinding : GraphicsDataNode<IShaderDataBinding>
|
2015-10-29 04:44:38 +00:00
|
|
|
{
|
2017-11-03 09:39:26 +00:00
|
|
|
ObjToken<IShaderPipeline> m_pipeline;
|
2018-10-07 02:49:22 +00:00
|
|
|
ObjToken<IGraphicsBuffer> m_vbo;
|
|
|
|
ObjToken<IGraphicsBuffer> m_instVbo;
|
|
|
|
ObjToken<IGraphicsBuffer> m_ibo;
|
2017-11-03 09:39:26 +00:00
|
|
|
std::vector<ObjToken<IGraphicsBuffer>> m_ubufs;
|
2016-03-30 19:14:17 +00:00
|
|
|
std::vector<std::pair<size_t,size_t>> m_ubufOffs;
|
2017-03-14 07:02:53 +00:00
|
|
|
struct BoundTex
|
|
|
|
{
|
2017-11-03 09:39:26 +00:00
|
|
|
ObjToken<ITexture> tex;
|
2017-03-14 07:02:53 +00:00
|
|
|
int idx;
|
|
|
|
bool depth;
|
|
|
|
};
|
2017-11-03 09:39:26 +00:00
|
|
|
std::vector<BoundTex> m_texs;
|
2018-10-07 02:49:22 +00:00
|
|
|
size_t m_baseVert;
|
|
|
|
size_t m_baseInst;
|
|
|
|
GLuint m_vao[3] = {};
|
2015-12-17 21:29:47 +00:00
|
|
|
|
2017-11-03 09:39:26 +00:00
|
|
|
GLShaderDataBinding(const ObjToken<BaseGraphicsData>& d,
|
|
|
|
const ObjToken<IShaderPipeline>& pipeline,
|
2018-10-07 02:49:22 +00:00
|
|
|
const ObjToken<IGraphicsBuffer>& vbo,
|
|
|
|
const ObjToken<IGraphicsBuffer>& instVbo,
|
|
|
|
const ObjToken<IGraphicsBuffer>& ibo,
|
2017-11-03 09:39:26 +00:00
|
|
|
size_t ubufCount, const ObjToken<IGraphicsBuffer>* ubufs,
|
2016-03-30 19:14:17 +00:00
|
|
|
const size_t* ubufOffs, const size_t* ubufSizes,
|
2017-11-03 09:39:26 +00:00
|
|
|
size_t texCount, const ObjToken<ITexture>* texs,
|
2018-10-07 02:49:22 +00:00
|
|
|
const int* bindTexIdx, const bool* depthBind,
|
|
|
|
size_t baseVert, size_t baseInst)
|
2017-11-03 09:39:26 +00:00
|
|
|
: GraphicsDataNode<IShaderDataBinding>(d),
|
2018-10-07 02:49:22 +00:00
|
|
|
m_pipeline(pipeline), m_vbo(vbo), m_instVbo(instVbo), m_ibo(ibo), m_baseVert(baseVert), m_baseInst(baseInst)
|
2015-10-30 00:00:56 +00:00
|
|
|
{
|
2016-03-30 19:14:17 +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);
|
|
|
|
}
|
2016-03-30 19:14:17 +00:00
|
|
|
}
|
2017-11-03 09:39:26 +00:00
|
|
|
m_ubufs.reserve(ubufCount);
|
2015-10-30 00:00:56 +00:00
|
|
|
for (size_t i=0 ; i<ubufCount ; ++i)
|
2016-03-30 19:14:17 +00:00
|
|
|
{
|
|
|
|
#ifndef NDEBUG
|
|
|
|
if (!ubufs[i])
|
|
|
|
Log.report(logvisor::Fatal, "null uniform-buffer %d provided to newShaderDataBinding", int(i));
|
|
|
|
#endif
|
2017-11-03 09:39:26 +00:00
|
|
|
m_ubufs.push_back(ubufs[i]);
|
2016-03-30 19:14:17 +00:00
|
|
|
}
|
2017-11-03 09:39:26 +00:00
|
|
|
m_texs.reserve(texCount);
|
2015-10-30 00:00:56 +00:00
|
|
|
for (size_t i=0 ; i<texCount ; ++i)
|
2017-03-14 07:02:53 +00:00
|
|
|
{
|
2017-11-05 06:12:49 +00:00
|
|
|
m_texs.push_back({texs[i], bindTexIdx ? bindTexIdx[i] : 0, depthBind ? depthBind[i] : false});
|
2017-03-14 07:02:53 +00:00
|
|
|
}
|
2015-10-30 00:00:56 +00:00
|
|
|
}
|
2018-10-07 02:49:22 +00:00
|
|
|
|
|
|
|
~GLShaderDataBinding()
|
|
|
|
{
|
|
|
|
glDeleteVertexArrays(3, m_vao);
|
|
|
|
}
|
|
|
|
|
2015-11-30 02:37:46 +00:00
|
|
|
void bind(int b) const
|
2015-10-29 04:44:38 +00:00
|
|
|
{
|
2017-11-03 09:39:26 +00:00
|
|
|
GLShaderPipeline& pipeline = *m_pipeline.cast<GLShaderPipeline>();
|
|
|
|
GLuint prog = pipeline.bind();
|
2018-10-07 02:49:22 +00:00
|
|
|
glBindVertexArray(m_vao[b]);
|
2016-03-30 19:14:17 +00:00
|
|
|
if (m_ubufOffs.size())
|
2015-10-30 00:00:56 +00:00
|
|
|
{
|
2018-10-11 20:47:37 +00:00
|
|
|
for (size_t i=0 ; i<m_ubufs.size() ; ++i)
|
2016-03-30 19:14:17 +00:00
|
|
|
{
|
2017-11-03 09:39:26 +00:00
|
|
|
GLint loc = pipeline.m_uniLocs[i];
|
2016-04-04 05:00:13 +00:00
|
|
|
if (loc < 0)
|
|
|
|
continue;
|
2017-11-03 09:39:26 +00:00
|
|
|
IGraphicsBuffer* ubuf = m_ubufs[i].get();
|
2016-03-30 19:14:17 +00:00
|
|
|
const std::pair<size_t,size_t>& offset = m_ubufOffs[i];
|
|
|
|
if (ubuf->dynamic())
|
2017-11-03 09:39:26 +00:00
|
|
|
static_cast<GLGraphicsBufferD<BaseGraphicsData>*>(ubuf)->bindUniformRange(i, offset.first, offset.second, b);
|
2016-03-30 19:14:17 +00:00
|
|
|
else
|
|
|
|
static_cast<GLGraphicsBufferS*>(ubuf)->bindUniformRange(i, offset.first, offset.second);
|
2016-04-04 05:00:13 +00:00
|
|
|
glUniformBlockBinding(prog, loc, i);
|
2016-03-30 19:14:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-10-11 20:47:37 +00:00
|
|
|
for (size_t i=0 ; i<m_ubufs.size() ; ++i)
|
2016-03-30 19:14:17 +00:00
|
|
|
{
|
2017-11-03 09:39:26 +00:00
|
|
|
GLint loc = pipeline.m_uniLocs[i];
|
2016-04-04 05:00:13 +00:00
|
|
|
if (loc < 0)
|
|
|
|
continue;
|
2017-11-03 09:39:26 +00:00
|
|
|
IGraphicsBuffer* ubuf = m_ubufs[i].get();
|
2016-03-30 19:14:17 +00:00
|
|
|
if (ubuf->dynamic())
|
2017-11-03 09:39:26 +00:00
|
|
|
static_cast<GLGraphicsBufferD<BaseGraphicsData>*>(ubuf)->bindUniform(i, b);
|
2016-03-30 19:14:17 +00:00
|
|
|
else
|
|
|
|
static_cast<GLGraphicsBufferS*>(ubuf)->bindUniform(i);
|
2016-04-04 05:00:13 +00:00
|
|
|
glUniformBlockBinding(prog, loc, i);
|
2016-03-30 19:14:17 +00:00
|
|
|
}
|
2015-10-30 00:00:56 +00:00
|
|
|
}
|
2017-11-03 09:39:26 +00:00
|
|
|
for (size_t i=0 ; i<m_texs.size() ; ++i)
|
2015-11-02 09:31:06 +00:00
|
|
|
{
|
2017-11-03 09:39:26 +00:00
|
|
|
const BoundTex& tex = m_texs[i];
|
2017-03-14 07:02:53 +00:00
|
|
|
if (tex.tex)
|
2015-11-26 23:03:01 +00:00
|
|
|
{
|
2017-03-14 07:02:53 +00:00
|
|
|
switch (tex.tex->type())
|
2016-07-31 02:05:20 +00:00
|
|
|
{
|
|
|
|
case TextureType::Dynamic:
|
2017-11-03 09:39:26 +00:00
|
|
|
tex.tex.cast<GLTextureD>()->bind(i, b);
|
2016-07-31 02:05:20 +00:00
|
|
|
break;
|
|
|
|
case TextureType::Static:
|
2017-11-03 09:39:26 +00:00
|
|
|
tex.tex.cast<GLTextureS>()->bind(i);
|
2016-07-31 02:05:20 +00:00
|
|
|
break;
|
|
|
|
case TextureType::StaticArray:
|
2017-11-03 09:39:26 +00:00
|
|
|
tex.tex.cast<GLTextureSA>()->bind(i);
|
2016-07-31 02:05:20 +00:00
|
|
|
break;
|
|
|
|
case TextureType::Render:
|
2017-11-03 09:39:26 +00:00
|
|
|
tex.tex.cast<GLTextureR>()->bind(i, tex.idx, tex.depth);
|
2016-07-31 02:05:20 +00:00
|
|
|
break;
|
|
|
|
default: break;
|
|
|
|
}
|
2015-11-26 23:03:01 +00:00
|
|
|
}
|
2015-11-02 09:31:06 +00:00
|
|
|
}
|
2015-10-29 04:44:38 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-05-20 06:11:49 +00:00
|
|
|
GLDataFactory::Context::Context(GLDataFactory& parent __BooTraceArgs)
|
|
|
|
: m_parent(parent), m_data(new BaseGraphicsData(static_cast<GLDataFactoryImpl&>(parent) __BooTraceArgsUse))
|
2017-11-03 09:39:26 +00:00
|
|
|
{}
|
2015-10-29 04:44:38 +00:00
|
|
|
|
2017-11-03 09:39:26 +00:00
|
|
|
GLDataFactory::Context::~Context() {}
|
2016-03-30 19:14:17 +00:00
|
|
|
|
2018-05-20 06:11:49 +00:00
|
|
|
void GLDataFactoryImpl::commitTransaction(const FactoryCommitFunc& trans __BooTraceArgs)
|
2017-11-03 09:39:26 +00:00
|
|
|
{
|
2018-05-20 06:11:49 +00:00
|
|
|
GLDataFactory::Context ctx(*this __BooTraceArgsUse);
|
2016-03-30 19:14:17 +00:00
|
|
|
if (!trans(ctx))
|
2017-11-03 09:39:26 +00:00
|
|
|
return;
|
2017-01-20 03:52:40 +00:00
|
|
|
|
2015-11-03 22:13:15 +00:00
|
|
|
/* 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 */
|
2017-03-14 07:02:53 +00:00
|
|
|
//glFlush();
|
2016-12-10 02:31:50 +00:00
|
|
|
}
|
|
|
|
|
2018-05-20 06:11:49 +00:00
|
|
|
ObjToken<IGraphicsBufferD> GLDataFactoryImpl::newPoolBuffer(BufferUse use, size_t stride, size_t count __BooTraceArgs)
|
2015-11-01 00:06:56 +00:00
|
|
|
{
|
2018-05-20 06:11:49 +00:00
|
|
|
ObjToken<BaseGraphicsPool> pool(new BaseGraphicsPool(*this __BooTraceArgsUse));
|
2017-11-03 09:39:26 +00:00
|
|
|
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[] =
|
|
|
|
{
|
2015-11-25 02:14:30 +00:00
|
|
|
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,
|
2015-11-25 02:14:30 +00:00
|
|
|
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[] =
|
|
|
|
{
|
2015-11-25 02:14:30 +00:00
|
|
|
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,
|
2015-11-25 02:14:30 +00:00
|
|
|
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[] =
|
|
|
|
{
|
2015-11-25 02:14:30 +00:00
|
|
|
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,
|
2015-11-25 02:14:30 +00:00
|
|
|
GL_FLOAT,
|
2015-11-26 00:22:05 +00:00
|
|
|
GL_FLOAT,
|
2015-10-30 00:00:56 +00:00
|
|
|
GL_FLOAT
|
|
|
|
};
|
|
|
|
|
2015-11-02 10:07:15 +00:00
|
|
|
struct GLCommandQueue : IGraphicsCommandQueue
|
2015-10-29 04:44:38 +00:00
|
|
|
{
|
2017-11-03 09:39:26 +00:00
|
|
|
Platform platform() const { return IGraphicsDataFactory::Platform::OpenGL; }
|
2018-10-07 02:49:22 +00:00
|
|
|
const SystemChar* platformName() const { return _SYS_STR("OpenGL"); }
|
2015-10-30 00:00:56 +00:00
|
|
|
IGraphicsContext* m_parent = nullptr;
|
2018-01-07 05:17:14 +00:00
|
|
|
GLContext* m_glCtx = nullptr;
|
2015-10-29 04:44:38 +00:00
|
|
|
|
2017-03-14 07:02:53 +00:00
|
|
|
std::mutex m_mt;
|
|
|
|
std::condition_variable m_cv;
|
|
|
|
std::mutex m_initmt;
|
|
|
|
std::condition_variable m_initcv;
|
|
|
|
std::thread m_thr;
|
|
|
|
|
2015-10-29 04:44:38 +00:00
|
|
|
struct Command
|
|
|
|
{
|
2015-11-21 01:12:22 +00:00
|
|
|
enum class Op
|
2015-10-29 04:44:38 +00:00
|
|
|
{
|
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,
|
2016-02-25 02:53:23 +00:00
|
|
|
ResolveBindTexture,
|
2015-11-21 01:12:22 +00:00
|
|
|
Present
|
2015-10-29 04:44:38 +00:00
|
|
|
} m_op;
|
|
|
|
union
|
|
|
|
{
|
2016-04-05 01:49:08 +00:00
|
|
|
struct
|
|
|
|
{
|
|
|
|
SWindowRect rect;
|
|
|
|
float znear, zfar;
|
2016-04-15 20:41:24 +00:00
|
|
|
} viewport;
|
2015-10-29 04:44:38 +00:00
|
|
|
float rgba[4];
|
|
|
|
GLbitfield flags;
|
|
|
|
struct
|
|
|
|
{
|
|
|
|
size_t start;
|
|
|
|
size_t count;
|
|
|
|
size_t instCount;
|
2018-11-05 07:24:38 +00:00
|
|
|
size_t startInst;
|
2015-10-29 04:44:38 +00:00
|
|
|
};
|
|
|
|
};
|
2017-11-03 09:39:26 +00:00
|
|
|
ObjToken<IShaderDataBinding> binding;
|
|
|
|
ObjToken<ITextureR> target;
|
|
|
|
ObjToken<ITextureR> source;
|
|
|
|
ObjToken<ITextureR> resolveTex;
|
2017-03-14 07:02:53 +00:00
|
|
|
int bindIdx;
|
2016-02-25 05:06:13 +00:00
|
|
|
bool resolveColor : 1;
|
|
|
|
bool resolveDepth : 1;
|
2018-01-06 06:49:54 +00:00
|
|
|
bool clearDepth : 1;
|
2015-10-29 04:44:38 +00:00
|
|
|
Command(Op op) : m_op(op) {}
|
2017-01-20 03:52:40 +00:00
|
|
|
Command(const Command&) = delete;
|
|
|
|
Command& operator=(const Command&) = delete;
|
|
|
|
Command(Command&&) = default;
|
|
|
|
Command& operator=(Command&&) = default;
|
2015-10-29 04:44:38 +00:00
|
|
|
};
|
|
|
|
std::vector<Command> m_cmdBufs[3];
|
2017-11-03 09:39:26 +00:00
|
|
|
int m_fillBuf = 0;
|
|
|
|
int m_completeBuf = 0;
|
|
|
|
int m_drawBuf = 0;
|
2015-10-29 04:44:38 +00:00
|
|
|
bool m_running = true;
|
2015-10-31 04:28:21 +00:00
|
|
|
|
2015-11-17 06:41:32 +00:00
|
|
|
struct RenderTextureResize
|
|
|
|
{
|
2017-11-03 09:39:26 +00:00
|
|
|
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;
|
2018-10-07 02:49:22 +00:00
|
|
|
std::vector<ObjToken<IShaderDataBinding>> m_pendingFmtAdds;
|
2017-11-03 09:39:26 +00:00
|
|
|
std::vector<ObjToken<ITextureR>> m_pendingFboAdds;
|
2015-10-30 00:00:56 +00:00
|
|
|
|
2018-10-07 02:49:22 +00:00
|
|
|
static void ConfigureVertexFormat(GLShaderDataBinding* 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;
|
2015-11-25 02:14:30 +00:00
|
|
|
size_t instStride = 0;
|
2018-10-07 02:49:22 +00:00
|
|
|
auto pipeline = fmt->m_pipeline.cast<GLShaderPipeline>();
|
|
|
|
for (size_t i=0 ; i<pipeline->m_elements.size() ; ++i)
|
2015-10-30 00:00:56 +00:00
|
|
|
{
|
2018-10-07 02:49:22 +00:00
|
|
|
const VertexElementDescriptor& desc = pipeline->m_elements[i];
|
2017-11-05 06:12:49 +00:00
|
|
|
if ((desc.semantic & VertexSemantic::Instanced) != VertexSemantic::None)
|
|
|
|
instStride += SEMANTIC_SIZE_TABLE[int(desc.semantic & VertexSemantic::SemanticMask)];
|
2015-11-25 02:14:30 +00:00
|
|
|
else
|
2017-11-05 06:12:49 +00:00
|
|
|
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]);
|
2015-12-02 21:09:49 +00:00
|
|
|
IGraphicsBuffer* lastVBO = nullptr;
|
|
|
|
IGraphicsBuffer* lastEBO = nullptr;
|
2018-10-07 02:49:22 +00:00
|
|
|
for (size_t i=0 ; i<pipeline->m_elements.size() ; ++i)
|
2015-10-30 00:00:56 +00:00
|
|
|
{
|
2018-10-07 02:49:22 +00:00
|
|
|
const VertexElementDescriptor& desc = pipeline->m_elements[i];
|
|
|
|
IGraphicsBuffer* vbo = (desc.semantic & VertexSemantic::Instanced) != VertexSemantic::None
|
|
|
|
? fmt->m_instVbo.get() : fmt->m_vbo.get();
|
|
|
|
IGraphicsBuffer* ebo = fmt->m_ibo.get();
|
|
|
|
if (vbo != lastVBO)
|
2015-11-30 02:37:46 +00:00
|
|
|
{
|
2018-10-07 02:49:22 +00:00
|
|
|
lastVBO = vbo;
|
2015-11-30 02:37:46 +00:00
|
|
|
if (lastVBO->dynamic())
|
2017-11-03 09:39:26 +00:00
|
|
|
static_cast<GLGraphicsBufferD<BaseGraphicsData>*>(lastVBO)->bindVertex(b);
|
2015-11-30 02:37:46 +00:00
|
|
|
else
|
2015-12-02 21:09:49 +00:00
|
|
|
static_cast<GLGraphicsBufferS*>(lastVBO)->bindVertex();
|
2015-11-30 02:37:46 +00:00
|
|
|
}
|
2018-10-07 02:49:22 +00:00
|
|
|
if (ebo != lastEBO)
|
2015-11-30 02:37:46 +00:00
|
|
|
{
|
2018-10-07 02:49:22 +00:00
|
|
|
lastEBO = ebo;
|
2015-11-30 02:37:46 +00:00
|
|
|
if (lastEBO->dynamic())
|
2017-11-03 09:39:26 +00:00
|
|
|
static_cast<GLGraphicsBufferD<BaseGraphicsData>*>(lastEBO)->bindIndex(b);
|
2015-11-30 02:37:46 +00:00
|
|
|
else
|
2015-12-02 21:09:49 +00:00
|
|
|
static_cast<GLGraphicsBufferS*>(lastEBO)->bindIndex();
|
2015-11-30 02:37:46 +00:00
|
|
|
}
|
|
|
|
glEnableVertexAttribArray(i);
|
2017-11-05 06:12:49 +00:00
|
|
|
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-11-25 02:14:30 +00:00
|
|
|
}
|
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);
|
2018-01-07 05:17:14 +00:00
|
|
|
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 ; i<tex->m_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 ; i<tex->m_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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-10-30 00:00:56 +00:00
|
|
|
}
|
|
|
|
|
2015-11-02 10:07:15 +00:00
|
|
|
static void RenderingWorker(GLCommandQueue* self)
|
2015-10-29 04:44:38 +00:00
|
|
|
{
|
2017-11-09 08:10:43 +00:00
|
|
|
#if _WIN32
|
2018-11-05 07:24:38 +00:00
|
|
|
std::string thrName = WCSTMBS(APP->getFriendlyName().data()) + " Render";
|
2017-11-09 08:10:43 +00:00
|
|
|
#else
|
2018-06-02 00:01:47 +00:00
|
|
|
std::string thrName = std::string(APP->getFriendlyName()) + " Render";
|
2017-11-09 08:10:43 +00:00
|
|
|
#endif
|
2017-11-08 02:24:07 +00:00
|
|
|
logvisor::RegisterThreadName(thrName.c_str());
|
2018-01-20 03:02:29 +00:00
|
|
|
GLDataFactoryImpl* dataFactory = static_cast<GLDataFactoryImpl*>(self->m_parent->getDataFactory());
|
2015-10-31 04:28:21 +00:00
|
|
|
{
|
2015-10-31 06:33:36 +00:00
|
|
|
std::unique_lock<std::mutex> lk(self->m_initmt);
|
2015-10-31 04:28:21 +00:00
|
|
|
self->m_parent->makeCurrent();
|
2016-02-15 17:53:54 +00:00
|
|
|
const GLubyte* version = glGetString(GL_VERSION);
|
2016-03-04 23:02:18 +00:00
|
|
|
Log.report(logvisor::Info, "OpenGL Version: %s", version);
|
2015-10-31 04:28:21 +00:00
|
|
|
self->m_parent->postInit();
|
2017-03-14 07:02:53 +00:00
|
|
|
glClearColor(0.f, 0.f, 0.f, 0.f);
|
2018-01-07 05:17:14 +00:00
|
|
|
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);
|
2018-01-20 03:02:29 +00:00
|
|
|
|
2018-05-05 20:55:37 +00:00
|
|
|
glEnable(GL_PRIMITIVE_RESTART);
|
|
|
|
glPrimitiveRestartIndex(0xffffffff);
|
|
|
|
|
2018-01-20 05:50:01 +00:00
|
|
|
dataFactory->SetupGammaResources();
|
2015-10-31 04:28:21 +00:00
|
|
|
}
|
|
|
|
self->m_initcv.notify_one();
|
2015-10-29 04:44:38 +00:00
|
|
|
while (self->m_running)
|
|
|
|
{
|
2015-12-21 00:40:52 +00:00
|
|
|
std::vector<std::function<void(void)>> posts;
|
2015-10-29 04:44:38 +00:00
|
|
|
{
|
|
|
|
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())
|
|
|
|
{
|
2017-11-03 09:39:26 +00:00
|
|
|
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)
|
2017-11-03 09:39:26 +00:00
|
|
|
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
|
|
|
{
|
2018-10-07 02:49:22 +00:00
|
|
|
for (ObjToken<IShaderDataBinding>& fmt : self->m_pendingFmtAdds)
|
|
|
|
if (fmt) ConfigureVertexFormat(fmt.cast<GLShaderDataBinding>());
|
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);
|
2015-10-29 04:44:38 +00:00
|
|
|
}
|
|
|
|
std::vector<Command>& cmds = self->m_cmdBufs[self->m_drawBuf];
|
2016-03-24 08:05:19 +00:00
|
|
|
GLenum currentPrim = GL_TRIANGLES;
|
2017-11-02 09:24:50 +00:00
|
|
|
const GLShaderDataBinding* curBinding = nullptr;
|
2018-01-07 05:17:14 +00:00
|
|
|
GLuint curFBO = 0;
|
2015-10-29 04:44:38 +00:00
|
|
|
for (const Command& cmd : cmds)
|
|
|
|
{
|
|
|
|
switch (cmd.m_op)
|
|
|
|
{
|
2015-11-21 01:12:22 +00:00
|
|
|
case Command::Op::SetShaderDataBinding:
|
2016-03-24 08:05:19 +00:00
|
|
|
{
|
2017-11-03 09:39:26 +00:00
|
|
|
const GLShaderDataBinding* binding = cmd.binding.cast<GLShaderDataBinding>();
|
2016-03-24 08:05:19 +00:00
|
|
|
binding->bind(self->m_drawBuf);
|
2017-11-02 09:24:50 +00:00
|
|
|
curBinding = binding;
|
2017-11-03 09:39:26 +00:00
|
|
|
currentPrim = binding->m_pipeline.cast<GLShaderPipeline>()->m_drawPrim;
|
2015-10-29 04:44:38 +00:00
|
|
|
break;
|
2016-03-24 08:05:19 +00:00
|
|
|
}
|
2015-11-21 01:12:22 +00:00
|
|
|
case Command::Op::SetRenderTarget:
|
2015-10-29 04:44:38 +00:00
|
|
|
{
|
2017-11-03 09:39:26 +00:00
|
|
|
const GLTextureR* tex = cmd.target.cast<GLTextureR>();
|
2018-01-07 05:17:14 +00:00
|
|
|
curFBO = (!tex) ? 0 : tex->m_fbo;
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, curFBO);
|
2015-10-29 04:44:38 +00:00
|
|
|
break;
|
|
|
|
}
|
2015-11-21 01:12:22 +00:00
|
|
|
case Command::Op::SetViewport:
|
2016-04-15 20:41:24 +00:00
|
|
|
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:
|
2016-04-15 20:41:24 +00:00
|
|
|
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);
|
2016-04-15 20:41:24 +00:00
|
|
|
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:
|
2015-10-29 04:44:38 +00:00
|
|
|
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:
|
2017-03-14 07:02:53 +00:00
|
|
|
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);
|
2015-10-29 04:44:38 +00:00
|
|
|
break;
|
2015-11-21 01:12:22 +00:00
|
|
|
case Command::Op::Draw:
|
2016-03-24 08:05:19 +00:00
|
|
|
glDrawArrays(currentPrim, cmd.start, cmd.count);
|
2015-10-29 04:44:38 +00:00
|
|
|
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));
|
2015-10-29 04:44:38 +00:00
|
|
|
break;
|
2015-11-21 01:12:22 +00:00
|
|
|
case Command::Op::DrawInstances:
|
2018-11-05 07:24:38 +00:00
|
|
|
if (cmd.startInst)
|
|
|
|
glDrawArraysInstancedBaseInstance(currentPrim, cmd.start, cmd.count, cmd.instCount, cmd.startInst);
|
|
|
|
else
|
|
|
|
glDrawArraysInstanced(currentPrim, cmd.start, cmd.count, cmd.instCount);
|
2015-10-29 04:44:38 +00:00
|
|
|
break;
|
2015-11-21 01:12:22 +00:00
|
|
|
case Command::Op::DrawInstancesIndexed:
|
2018-11-05 07:24:38 +00:00
|
|
|
if (cmd.startInst)
|
|
|
|
glDrawElementsInstancedBaseInstance(currentPrim, cmd.count, GL_UNSIGNED_INT,
|
|
|
|
reinterpret_cast<void*>(cmd.start * 4), cmd.instCount, cmd.startInst);
|
|
|
|
else
|
|
|
|
glDrawElementsInstanced(currentPrim, cmd.count, GL_UNSIGNED_INT,
|
|
|
|
reinterpret_cast<void*>(cmd.start * 4), cmd.instCount);
|
2015-10-29 04:44:38 +00:00
|
|
|
break;
|
2016-02-25 02:53:23 +00:00
|
|
|
case Command::Op::ResolveBindTexture:
|
|
|
|
{
|
2018-01-07 05:17:14 +00:00
|
|
|
const SWindowRect& rect = cmd.viewport.rect;
|
2017-11-03 09:39:26 +00:00
|
|
|
const GLTextureR* tex = cmd.resolveTex.cast<GLTextureR>();
|
2016-02-25 02:53:23 +00:00
|
|
|
glBindFramebuffer(GL_READ_FRAMEBUFFER, tex->m_fbo);
|
2018-01-07 05:17:14 +00:00
|
|
|
if (tex->m_samples <= 1)
|
2016-02-25 02:53:23 +00:00
|
|
|
{
|
2018-01-07 05:17:14 +00:00
|
|
|
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]);
|
|
|
|
}
|
2016-02-25 02:53:23 +00:00
|
|
|
}
|
2018-01-07 05:17:14 +00:00
|
|
|
else
|
2016-02-25 02:53:23 +00:00
|
|
|
{
|
2018-01-07 05:17:14 +00:00
|
|
|
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);
|
|
|
|
}
|
2016-02-25 02:53:23 +00:00
|
|
|
}
|
2018-01-06 06:49:54 +00:00
|
|
|
if (cmd.clearDepth)
|
|
|
|
{
|
|
|
|
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, tex->m_fbo);
|
|
|
|
glDepthMask(GL_TRUE);
|
|
|
|
glClear(GL_DEPTH_BUFFER_BIT);
|
|
|
|
}
|
2018-01-07 05:17:14 +00:00
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, curFBO);
|
2016-02-25 02:53:23 +00:00
|
|
|
break;
|
|
|
|
}
|
2015-11-21 01:12:22 +00:00
|
|
|
case Command::Op::Present:
|
2015-11-04 01:02:05 +00:00
|
|
|
{
|
2017-11-03 09:39:26 +00:00
|
|
|
if (const GLTextureR* tex = cmd.source.cast<GLTextureR>())
|
2015-11-04 01:02:05 +00:00
|
|
|
{
|
2018-01-20 03:02:29 +00:00
|
|
|
#ifndef NDEBUG
|
|
|
|
if (!tex->m_colorBindCount)
|
|
|
|
Log.report(logvisor::Fatal,
|
|
|
|
"texture provided to resolveDisplay() must have at least 1 color binding");
|
|
|
|
#endif
|
2018-01-21 22:01:52 +00:00
|
|
|
if (dataFactory->m_gamma != 1.f)
|
2018-01-20 03:02:29 +00:00
|
|
|
{
|
2018-01-21 22:01:52 +00:00
|
|
|
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);
|
2018-10-07 02:49:22 +00:00
|
|
|
dataFactory->m_gammaBinding.cast<GLShaderDataBinding>()->bind(self->m_drawBuf);
|
2018-01-21 22:01:52 +00:00
|
|
|
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
|
2018-01-20 03:02:29 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-01-21 22:01:52 +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);
|
2018-01-20 03:02:29 +00:00
|
|
|
}
|
2015-11-04 01:02:05 +00:00
|
|
|
}
|
2015-10-31 04:28:21 +00:00
|
|
|
self->m_parent->present();
|
|
|
|
break;
|
2015-11-04 01:02:05 +00:00
|
|
|
}
|
2015-10-29 04:44:38 +00:00
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
}
|
2015-12-21 00:40:52 +00:00
|
|
|
for (auto& p : posts)
|
|
|
|
p();
|
2017-11-03 09:39:26 +00:00
|
|
|
cmds.clear();
|
2015-10-29 04:44:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-07 05:17:14 +00:00
|
|
|
GLCommandQueue(IGraphicsContext* parent, GLContext* glCtx)
|
2015-10-29 04:44:38 +00:00
|
|
|
: m_parent(parent),
|
2018-01-20 03:02:29 +00:00
|
|
|
m_glCtx(glCtx)
|
|
|
|
{}
|
|
|
|
|
|
|
|
void startRenderer()
|
2015-10-31 04:28:21 +00:00
|
|
|
{
|
2018-01-20 03:02:29 +00:00
|
|
|
std::unique_lock<std::mutex> lk(m_initmt);
|
|
|
|
m_thr = std::thread(RenderingWorker, this);
|
|
|
|
m_initcv.wait(lk);
|
2015-10-31 04:28:21 +00:00
|
|
|
}
|
2015-10-29 04:44:38 +00:00
|
|
|
|
2015-12-05 00:41:30 +00:00
|
|
|
void stopRenderer()
|
2015-10-29 04:44:38 +00:00
|
|
|
{
|
2017-03-14 07:02:53 +00:00
|
|
|
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();
|
|
|
|
}
|
2015-10-29 04:44:38 +00:00
|
|
|
}
|
|
|
|
|
2015-12-05 00:41:30 +00:00
|
|
|
~GLCommandQueue()
|
|
|
|
{
|
2017-03-14 07:02:53 +00:00
|
|
|
stopRenderer();
|
2015-12-05 00:41:30 +00:00
|
|
|
}
|
|
|
|
|
2017-11-03 09:39:26 +00:00
|
|
|
void setShaderDataBinding(const ObjToken<IShaderDataBinding>& binding)
|
2015-10-29 04:44:38 +00:00
|
|
|
{
|
|
|
|
std::vector<Command>& cmds = m_cmdBufs[m_fillBuf];
|
2015-11-21 01:12:22 +00:00
|
|
|
cmds.emplace_back(Command::Op::SetShaderDataBinding);
|
2015-10-29 04:44:38 +00:00
|
|
|
cmds.back().binding = binding;
|
|
|
|
}
|
2015-11-04 01:02:05 +00:00
|
|
|
|
2017-11-03 09:39:26 +00:00
|
|
|
void setRenderTarget(const ObjToken<ITextureR>& target)
|
2015-10-29 04:44:38 +00:00
|
|
|
{
|
|
|
|
std::vector<Command>& cmds = m_cmdBufs[m_fillBuf];
|
2015-11-21 01:12:22 +00:00
|
|
|
cmds.emplace_back(Command::Op::SetRenderTarget);
|
2015-10-29 04:44:38 +00:00
|
|
|
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);
|
2016-04-15 20:41:24 +00:00
|
|
|
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);
|
2016-04-15 20:41:24 +00:00
|
|
|
cmds.back().viewport.rect = rect;
|
2015-11-26 00:22:05 +00:00
|
|
|
}
|
2015-11-25 02:14:30 +00:00
|
|
|
|
2017-11-03 09:39:26 +00:00
|
|
|
void resizeRenderTexture(const ObjToken<ITextureR>& tex, size_t width, size_t height)
|
2015-11-04 01:58:36 +00:00
|
|
|
{
|
2015-11-17 06:41:32 +00:00
|
|
|
std::unique_lock<std::mutex> lk(m_mt);
|
2017-11-03 09:39:26 +00:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2015-10-29 04:44:38 +00:00
|
|
|
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);
|
2015-10-29 04:44:38 +00:00
|
|
|
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
|
|
|
|
2015-10-29 04:44:38 +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);
|
2015-10-29 04:44:38 +00:00
|
|
|
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);
|
2015-10-29 04:44:38 +00:00
|
|
|
cmds.back().start = start;
|
|
|
|
cmds.back().count = count;
|
|
|
|
}
|
2015-11-04 01:02:05 +00:00
|
|
|
|
2015-10-29 04:44:38 +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);
|
2015-10-29 04:44:38 +00:00
|
|
|
cmds.back().start = start;
|
|
|
|
cmds.back().count = count;
|
|
|
|
}
|
2015-11-04 01:02:05 +00:00
|
|
|
|
2018-11-05 07:24:38 +00:00
|
|
|
void drawInstances(size_t start, size_t count, size_t instCount, size_t startInst)
|
2015-10-29 04:44:38 +00:00
|
|
|
{
|
|
|
|
std::vector<Command>& cmds = m_cmdBufs[m_fillBuf];
|
2015-11-21 01:12:22 +00:00
|
|
|
cmds.emplace_back(Command::Op::DrawInstances);
|
2015-10-29 04:44:38 +00:00
|
|
|
cmds.back().start = start;
|
|
|
|
cmds.back().count = count;
|
|
|
|
cmds.back().instCount = instCount;
|
2018-11-05 07:24:38 +00:00
|
|
|
cmds.back().startInst = startInst;
|
2015-10-29 04:44:38 +00:00
|
|
|
}
|
2015-11-04 01:02:05 +00:00
|
|
|
|
2018-11-05 07:24:38 +00:00
|
|
|
void drawInstancesIndexed(size_t start, size_t count, size_t instCount, size_t startInst)
|
2015-10-29 04:44:38 +00:00
|
|
|
{
|
|
|
|
std::vector<Command>& cmds = m_cmdBufs[m_fillBuf];
|
2015-11-21 01:12:22 +00:00
|
|
|
cmds.emplace_back(Command::Op::DrawInstancesIndexed);
|
2015-10-29 04:44:38 +00:00
|
|
|
cmds.back().start = start;
|
|
|
|
cmds.back().count = count;
|
|
|
|
cmds.back().instCount = instCount;
|
2018-11-05 07:24:38 +00:00
|
|
|
cmds.back().startInst = startInst;
|
2015-10-29 04:44:38 +00:00
|
|
|
}
|
|
|
|
|
2017-11-03 09:39:26 +00:00
|
|
|
void resolveBindTexture(const ObjToken<ITextureR>& texture, const SWindowRect& rect, bool tlOrigin,
|
2018-01-06 06:49:54 +00:00
|
|
|
int bindIdx, bool color, bool depth, bool clearDepth)
|
2016-02-25 02:53:23 +00:00
|
|
|
{
|
2017-11-03 09:39:26 +00:00
|
|
|
GLTextureR* tex = texture.cast<GLTextureR>();
|
2016-02-25 02:53:23 +00:00
|
|
|
std::vector<Command>& cmds = m_cmdBufs[m_fillBuf];
|
|
|
|
cmds.emplace_back(Command::Op::ResolveBindTexture);
|
|
|
|
cmds.back().resolveTex = texture;
|
2017-03-14 07:02:53 +00:00
|
|
|
cmds.back().bindIdx = bindIdx;
|
2016-02-25 05:06:13 +00:00
|
|
|
cmds.back().resolveColor = color;
|
|
|
|
cmds.back().resolveDepth = depth;
|
2018-01-06 06:49:54 +00:00
|
|
|
cmds.back().clearDepth = clearDepth;
|
2016-02-27 01:38:13 +00:00
|
|
|
SWindowRect intersectRect = rect.intersect(SWindowRect(0, 0, tex->m_width, tex->m_height));
|
2016-04-15 20:41:24 +00:00
|
|
|
SWindowRect& targetRect = cmds.back().viewport.rect;
|
2016-02-27 01:38:13 +00:00
|
|
|
targetRect.location[0] = intersectRect.location[0];
|
2016-02-25 02:53:23 +00:00
|
|
|
if (tlOrigin)
|
2016-02-27 01:38:13 +00:00
|
|
|
targetRect.location[1] = tex->m_height - intersectRect.location[1] - intersectRect.size[1];
|
2016-02-26 20:16:21 +00:00
|
|
|
else
|
2016-02-27 01:38:13 +00:00
|
|
|
targetRect.location[1] = intersectRect.location[1];
|
|
|
|
targetRect.size[0] = intersectRect.size[0];
|
|
|
|
targetRect.size[1] = intersectRect.size[1];
|
2016-02-25 02:53:23 +00:00
|
|
|
}
|
|
|
|
|
2017-11-03 09:39:26 +00:00
|
|
|
void resolveDisplay(const ObjToken<ITextureR>& source)
|
2015-10-31 04:28:21 +00:00
|
|
|
{
|
|
|
|
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;
|
2015-10-31 04:28:21 +00:00
|
|
|
}
|
2016-02-16 19:41:16 +00:00
|
|
|
|
2018-10-07 02:49:22 +00:00
|
|
|
void addVertexFormat(const ObjToken<IShaderDataBinding>& fmt)
|
2015-10-30 00:00:56 +00:00
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lk(m_mt);
|
|
|
|
m_pendingFmtAdds.push_back(fmt);
|
|
|
|
}
|
|
|
|
|
2017-11-03 09:39:26 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2015-10-29 04:44:38 +00:00
|
|
|
void execute()
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lk(m_mt);
|
|
|
|
m_completeBuf = m_fillBuf;
|
2017-11-03 09:39:26 +00:00
|
|
|
for (int i=0 ; i<3 ; ++i)
|
2015-10-29 04:44:38 +00:00
|
|
|
{
|
|
|
|
if (i == m_completeBuf || i == m_drawBuf)
|
|
|
|
continue;
|
|
|
|
m_fillBuf = i;
|
|
|
|
break;
|
|
|
|
}
|
2015-12-02 21:09:49 +00:00
|
|
|
|
|
|
|
/* Update dynamic data here */
|
2017-03-05 07:54:58 +00:00
|
|
|
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);
|
2017-11-03 09:39:26 +00:00
|
|
|
if (gfxF->m_dataHead)
|
2015-12-02 21:09:49 +00:00
|
|
|
{
|
2017-11-03 09:39:26 +00:00
|
|
|
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);
|
|
|
|
}
|
2015-12-02 21:09:49 +00:00
|
|
|
}
|
2017-11-03 09:39:26 +00:00
|
|
|
if (gfxF->m_poolHead)
|
2016-12-10 02:31:50 +00:00
|
|
|
{
|
2017-11-03 09:39:26 +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-02 21:09:49 +00:00
|
|
|
|
2015-12-21 00:40:52 +00:00
|
|
|
for (auto& p : m_pendingPosts1)
|
|
|
|
m_pendingPosts2.push_back(std::move(p));
|
|
|
|
m_pendingPosts1.clear();
|
|
|
|
|
2015-10-29 04:44:38 +00:00
|
|
|
lk.unlock();
|
|
|
|
m_cv.notify_one();
|
|
|
|
m_cmdBufs[m_fillBuf].clear();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-11-03 09:39:26 +00:00
|
|
|
ObjToken<IGraphicsBufferD>
|
2016-03-30 19:14:17 +00:00
|
|
|
GLDataFactory::Context::newDynamicBuffer(BufferUse use, size_t stride, size_t count)
|
2015-10-30 00:00:56 +00:00
|
|
|
{
|
2017-11-03 09:39:26 +00:00
|
|
|
return {new GLGraphicsBufferD<BaseGraphicsData>(m_data, use, stride * count)};
|
2015-10-30 00:00:56 +00:00
|
|
|
}
|
|
|
|
|
2017-11-03 09:39:26 +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
|
|
|
{
|
2017-11-03 09:39:26 +00:00
|
|
|
return {new GLTextureD(m_data, width, height, fmt, clampMode)};
|
2015-10-30 00:00:56 +00:00
|
|
|
}
|
|
|
|
|
2017-11-03 09:39:26 +00:00
|
|
|
GLTextureR::GLTextureR(const ObjToken<BaseGraphicsData>& parent, GLCommandQueue* q, size_t width, size_t height,
|
2018-01-16 06:29:43 +00:00
|
|
|
size_t samples, GLenum colorFormat, TextureClampMode clampMode,
|
|
|
|
size_t colorBindingCount, size_t depthBindingCount)
|
2017-11-17 08:55:51 +00:00
|
|
|
: GraphicsDataNode<ITextureR>(parent), m_q(q), m_width(width), m_height(height), m_samples(samples),
|
2018-01-16 06:29:43 +00:00
|
|
|
m_colorFormat(colorFormat), m_colorBindCount(colorBindingCount), m_depthBindCount(depthBindingCount)
|
2015-11-04 01:02:05 +00:00
|
|
|
{
|
|
|
|
glGenTextures(2, m_texs);
|
2017-03-14 07:02:53 +00:00
|
|
|
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
|
|
|
|
2018-01-20 05:50:01 +00:00
|
|
|
GLenum compType = colorFormat == GL_RGBA16 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_BYTE;
|
2016-01-08 00:04:37 +00:00
|
|
|
if (samples > 1)
|
|
|
|
{
|
|
|
|
glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, m_texs[0]);
|
2018-01-16 06:29:43 +00:00
|
|
|
glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, samples, colorFormat, width, height, GL_FALSE);
|
2016-01-08 00:04:37 +00:00
|
|
|
glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, m_texs[1]);
|
2018-05-10 07:24:42 +00:00
|
|
|
glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, samples, GL_DEPTH_COMPONENT32F, width, height, GL_FALSE);
|
2016-01-08 00:04:37 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
glBindTexture(GL_TEXTURE_2D, m_texs[0]);
|
2018-01-20 05:50:01 +00:00
|
|
|
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);
|
2016-01-08 00:04:37 +00:00
|
|
|
glBindTexture(GL_TEXTURE_2D, m_texs[1]);
|
2018-05-10 07:24:42 +00:00
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32F, width, height, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, nullptr);
|
2017-10-01 04:23:28 +00:00
|
|
|
}
|
2016-02-25 02:53:23 +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]);
|
2018-01-20 05:50:01 +00:00
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, colorFormat, width, height, 0, GL_RGBA, compType, nullptr);
|
2017-10-01 04:23:28 +00:00
|
|
|
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);
|
2016-01-08 00:04:37 +00:00
|
|
|
}
|
2017-10-01 04:23:28 +00:00
|
|
|
for (int i=0 ; i<depthBindingCount ; ++i)
|
|
|
|
{
|
|
|
|
glBindTexture(GL_TEXTURE_2D, m_bindTexs[1][i]);
|
2018-05-10 07:24:42 +00:00
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32F, width, height, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, nullptr);
|
2017-10-01 04:23:28 +00:00
|
|
|
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
|
|
|
|
2017-11-03 09:39:26 +00:00
|
|
|
ObjToken<ITextureR>
|
2017-10-01 04:23:28 +00:00
|
|
|
GLDataFactory::Context::newRenderTexture(size_t width, size_t height, TextureClampMode clampMode,
|
2017-03-14 07:02:53 +00:00
|
|
|
size_t colorBindingCount, size_t depthBindingCount)
|
2015-11-04 01:02:05 +00:00
|
|
|
{
|
2017-03-05 07:54:58 +00:00
|
|
|
GLDataFactoryImpl& factory = static_cast<GLDataFactoryImpl&>(m_parent);
|
|
|
|
GLCommandQueue* q = static_cast<GLCommandQueue*>(factory.m_parent->getCommandQueue());
|
2018-01-16 06:29:43 +00:00
|
|
|
ObjToken<ITextureR> retval(new GLTextureR(m_data, q, width, height, factory.m_glCtx->m_sampleCount,
|
|
|
|
factory.m_glCtx->m_deepColor ? GL_RGBA16 : GL_RGBA8, clampMode,
|
2017-11-03 09:39:26 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-10-07 02:49:22 +00:00
|
|
|
ObjToken<IShaderDataBinding>
|
|
|
|
GLDataFactory::Context::newShaderDataBinding(const ObjToken<IShaderPipeline>& pipeline,
|
|
|
|
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)
|
2015-10-30 00:00:56 +00:00
|
|
|
{
|
2017-03-05 07:54:58 +00:00
|
|
|
GLDataFactoryImpl& factory = static_cast<GLDataFactoryImpl&>(m_parent);
|
|
|
|
GLCommandQueue* q = static_cast<GLCommandQueue*>(factory.m_parent->getCommandQueue());
|
2018-10-07 02:49:22 +00:00
|
|
|
ObjToken<GLShaderDataBinding> ret = {new GLShaderDataBinding(m_data, pipeline, vbo, instVbo, ibo, ubufCount, ubufs,
|
|
|
|
ubufOffs, ubufSizes, texCount, texs, texBindIdx, depthBind, baseVert, baseInst)};
|
|
|
|
q->addVertexFormat(ret.get());
|
|
|
|
return ret.get();
|
2015-10-30 00:00:56 +00:00
|
|
|
}
|
|
|
|
|
2018-05-20 06:11:49 +00:00
|
|
|
std::unique_ptr<IGraphicsCommandQueue> _NewGLCommandQueue(IGraphicsContext* parent, GLContext* glCtx)
|
2015-10-29 04:44:38 +00:00
|
|
|
{
|
2018-05-20 06:11:49 +00:00
|
|
|
return std::make_unique<GLCommandQueue>(parent, glCtx);
|
2015-10-29 04:44:38 +00:00
|
|
|
}
|
|
|
|
|
2018-05-20 06:11:49 +00:00
|
|
|
std::unique_ptr<IGraphicsDataFactory> _NewGLDataFactory(IGraphicsContext* parent, GLContext* glCtx)
|
2017-03-05 07:54:58 +00:00
|
|
|
{
|
2018-05-20 06:11:49 +00:00
|
|
|
return std::make_unique<GLDataFactoryImpl>(parent, glCtx);
|
2017-03-05 07:54:58 +00:00
|
|
|
}
|
|
|
|
|
2015-10-29 04:44:38 +00:00
|
|
|
}
|