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"
|
2015-10-29 04:44:38 +00:00
|
|
|
#include "boo/IGraphicsContext.hpp"
|
2017-01-20 03:52:40 +00:00
|
|
|
#include "Common.hpp"
|
2015-10-29 04:44:38 +00:00
|
|
|
#include <vector>
|
|
|
|
#include <thread>
|
|
|
|
#include <mutex>
|
|
|
|
#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>
|
2017-01-20 03:52:40 +00:00
|
|
|
#include <atomic>
|
2017-03-05 07:54:58 +00:00
|
|
|
#include <functional>
|
|
|
|
#include "xxhash.h"
|
2015-10-29 04:44:38 +00:00
|
|
|
|
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
|
|
|
|
|
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-03-05 07:54:58 +00:00
|
|
|
struct GLShareableShader : IShareableShader<GLDataFactoryImpl, GLShareableShader>
|
|
|
|
{
|
|
|
|
GLuint m_shader = 0;
|
2017-03-05 22:59:58 +00:00
|
|
|
GLShareableShader(GLDataFactoryImpl& fac, uint64_t srcKey, GLuint s)
|
|
|
|
: IShareableShader(fac, srcKey, 0), m_shader(s) {}
|
2017-03-05 07:54:58 +00:00
|
|
|
~GLShareableShader() { glDeleteShader(m_shader); }
|
|
|
|
};
|
|
|
|
|
|
|
|
class GLDataFactoryImpl : public GLDataFactory
|
|
|
|
{
|
|
|
|
friend struct GLCommandQueue;
|
|
|
|
friend class GLDataFactory::Context;
|
|
|
|
IGraphicsContext* m_parent;
|
|
|
|
uint32_t m_drawSamples;
|
|
|
|
static ThreadLocalPtr<struct GLData> m_deferredData;
|
|
|
|
std::unordered_set<struct GLData*> m_committedData;
|
|
|
|
std::unordered_set<struct GLPool*> m_committedPools;
|
|
|
|
std::mutex m_committedMutex;
|
|
|
|
std::unordered_map<uint64_t, std::unique_ptr<GLShareableShader>> m_sharedShaders;
|
|
|
|
void destroyData(IGraphicsData*);
|
|
|
|
void destroyAllData();
|
|
|
|
void destroyPool(IGraphicsBufferPool*);
|
|
|
|
IGraphicsBufferD* newPoolBuffer(IGraphicsBufferPool* pool, BufferUse use,
|
|
|
|
size_t stride, size_t count);
|
|
|
|
void deletePoolBuffer(IGraphicsBufferPool* p, IGraphicsBufferD* buf);
|
|
|
|
public:
|
|
|
|
GLDataFactoryImpl(IGraphicsContext* parent, uint32_t drawSamples);
|
|
|
|
~GLDataFactoryImpl() {destroyAllData();}
|
|
|
|
|
|
|
|
Platform platform() const {return Platform::OpenGL;}
|
|
|
|
const SystemChar* platformName() const {return _S("OpenGL");}
|
|
|
|
|
|
|
|
GraphicsDataToken commitTransaction(const FactoryCommitFunc&);
|
|
|
|
GraphicsBufferPoolToken newBufferPool();
|
|
|
|
|
2017-03-05 22:59:58 +00:00
|
|
|
void _unregisterShareableShader(uint64_t srcKey, uint64_t binKey)
|
|
|
|
{
|
|
|
|
m_sharedShaders.erase(srcKey);
|
|
|
|
}
|
2017-03-05 07:54:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
ThreadLocalPtr<struct GLData> GLDataFactoryImpl::m_deferredData;
|
2017-03-14 07:02:53 +00:00
|
|
|
struct GLData : IGraphicsDataPriv
|
2015-10-29 04:44:38 +00:00
|
|
|
{
|
2015-11-02 10:07:15 +00:00
|
|
|
std::vector<std::unique_ptr<class GLShaderPipeline>> m_SPs;
|
2015-11-03 22:13:15 +00:00
|
|
|
std::vector<std::unique_ptr<struct GLShaderDataBinding>> m_SBinds;
|
2015-11-02 10:07:15 +00:00
|
|
|
std::vector<std::unique_ptr<class GLGraphicsBufferS>> m_SBufs;
|
|
|
|
std::vector<std::unique_ptr<class GLGraphicsBufferD>> m_DBufs;
|
|
|
|
std::vector<std::unique_ptr<class GLTextureS>> m_STexs;
|
2015-11-26 23:03:01 +00:00
|
|
|
std::vector<std::unique_ptr<class GLTextureSA>> m_SATexs;
|
2015-11-02 10:07:15 +00:00
|
|
|
std::vector<std::unique_ptr<class GLTextureD>> m_DTexs;
|
2015-11-04 01:02:05 +00:00
|
|
|
std::vector<std::unique_ptr<class GLTextureR>> m_RTexs;
|
2015-11-03 22:13:15 +00:00
|
|
|
std::vector<std::unique_ptr<struct GLVertexFormat>> m_VFmts;
|
2015-10-29 04:44:38 +00:00
|
|
|
};
|
|
|
|
|
2017-03-14 07:02:53 +00:00
|
|
|
struct GLPoolItem : IGraphicsDataPriv
|
|
|
|
{
|
|
|
|
std::unique_ptr<class GLGraphicsBufferD> m_buf;
|
|
|
|
};
|
|
|
|
|
2016-12-10 02:31:50 +00:00
|
|
|
struct GLPool : IGraphicsBufferPool
|
|
|
|
{
|
2017-03-14 07:02:53 +00:00
|
|
|
std::unordered_set<GLPoolItem*> m_items;
|
|
|
|
~GLPool()
|
|
|
|
{
|
|
|
|
for (auto& item : m_items)
|
|
|
|
item->decrement();
|
|
|
|
}
|
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
|
|
|
|
};
|
|
|
|
|
2015-11-02 10:07:15 +00:00
|
|
|
class GLGraphicsBufferS : public 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-03-14 07:02:53 +00:00
|
|
|
GLGraphicsBufferS(IGraphicsData* parent, BufferUse use, const void* data, size_t sz)
|
|
|
|
: 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:
|
2015-11-02 10:07:15 +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
|
|
|
};
|
|
|
|
|
2015-11-02 10:07:15 +00:00
|
|
|
class GLGraphicsBufferD : public IGraphicsBufferD
|
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-03-14 07:02:53 +00:00
|
|
|
GLGraphicsBufferD(IGraphicsData* parent, BufferUse use, size_t sz)
|
|
|
|
: boo::IGraphicsBufferD(parent),
|
|
|
|
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
|
|
|
}
|
2015-12-02 21:09:49 +00:00
|
|
|
void update(int b);
|
2015-10-29 04:44:38 +00:00
|
|
|
public:
|
2015-11-02 10:07:15 +00:00
|
|
|
~GLGraphicsBufferD() {glDeleteBuffers(3, m_bufs);}
|
2015-10-29 04:44:38 +00:00
|
|
|
|
2015-10-30 00:00:56 +00:00
|
|
|
void load(const void* data, size_t sz);
|
|
|
|
void* map(size_t sz);
|
|
|
|
void unmap();
|
|
|
|
|
2015-12-02 21:09:49 +00:00
|
|
|
void bindVertex(int b);
|
|
|
|
void bindIndex(int b);
|
|
|
|
void bindUniform(size_t idx, int b);
|
2016-03-30 19:14:17 +00:00
|
|
|
void bindUniformRange(size_t idx, GLintptr off, GLsizeiptr size, int b);
|
2015-10-29 04:44:38 +00:00
|
|
|
};
|
|
|
|
|
2015-11-02 09:31:06 +00:00
|
|
|
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-03-14 07:02:53 +00:00
|
|
|
GLData* d = GLDataFactoryImpl::m_deferredData.get();
|
|
|
|
GLGraphicsBufferS* retval = new GLGraphicsBufferS(d, use, data, stride * count);
|
|
|
|
d->m_SBufs.emplace_back(retval);
|
2015-10-29 04:44:38 +00:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2015-11-02 10:07:15 +00:00
|
|
|
class GLTextureS : public 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;
|
2017-03-14 07:02:53 +00:00
|
|
|
GLTextureS(GLData* parent, size_t width, size_t height, size_t mips,
|
2015-11-21 01:12:22 +00:00
|
|
|
TextureFormat fmt, const void* data, size_t sz)
|
2017-03-14 07:02:53 +00:00
|
|
|
: 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
|
|
|
{
|
2015-11-01 00:06:56 +00:00
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
|
2016-02-16 19:41:16 +00:00
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, mips-1);
|
|
|
|
}
|
2015-11-01 00:06:56 +00:00
|
|
|
else
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
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;
|
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
|
|
|
|
{
|
|
|
|
for (size_t i=0 ; i<mips ; ++i)
|
|
|
|
{
|
|
|
|
glTexImage2D(GL_TEXTURE_2D, i, intFormat, width, height, 0, format, GL_UNSIGNED_BYTE, dataIt);
|
|
|
|
dataIt += width * height * pxPitch;
|
2017-01-29 03:56:17 +00:00
|
|
|
if (width > 1)
|
|
|
|
width /= 2;
|
|
|
|
if (height > 1)
|
|
|
|
height /= 2;
|
2016-02-16 05:47:09 +00:00
|
|
|
}
|
2015-10-29 04:44:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
public:
|
2015-11-02 10:07:15 +00:00
|
|
|
~GLTextureS() {glDeleteTextures(1, &m_tex);}
|
2015-10-30 00:00:56 +00:00
|
|
|
|
|
|
|
void bind(size_t idx) const
|
|
|
|
{
|
|
|
|
glActiveTexture(GL_TEXTURE0 + idx);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, m_tex);
|
|
|
|
}
|
2015-10-29 04:44:38 +00:00
|
|
|
};
|
|
|
|
|
2015-11-26 23:03:01 +00:00
|
|
|
class GLTextureSA : public ITextureSA
|
2015-11-23 08:49:53 +00:00
|
|
|
{
|
|
|
|
friend class GLDataFactory;
|
|
|
|
GLuint m_tex;
|
2017-03-14 07:02:53 +00:00
|
|
|
GLTextureSA(GLData* parent, size_t width, size_t height, size_t layers, size_t mips,
|
2015-11-26 23:03:01 +00:00
|
|
|
TextureFormat fmt, const void* data, size_t sz)
|
2017-03-14 07:02:53 +00:00
|
|
|
: 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);
|
|
|
|
|
|
|
|
GLenum intFormat, format;
|
|
|
|
int pxPitch;
|
2015-11-23 08:49:53 +00:00
|
|
|
if (fmt == TextureFormat::RGBA8)
|
2017-01-29 03:56:17 +00:00
|
|
|
{
|
|
|
|
intFormat = GL_RGBA8;
|
|
|
|
format = GL_RGBA;
|
|
|
|
pxPitch = 4;
|
|
|
|
}
|
2015-11-23 08:49:53 +00:00
|
|
|
else if (fmt == TextureFormat::I8)
|
2017-01-29 03:56:17 +00:00
|
|
|
{
|
|
|
|
intFormat = GL_R8;
|
|
|
|
format = GL_RED;
|
|
|
|
pxPitch = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i=0 ; i<mips ; ++i)
|
|
|
|
{
|
|
|
|
glTexImage3D(GL_TEXTURE_2D_ARRAY, i, intFormat, width, height, layers, 0, format, GL_UNSIGNED_BYTE, dataIt);
|
|
|
|
dataIt += width * height * layers * pxPitch;
|
|
|
|
if (width > 1)
|
|
|
|
width /= 2;
|
|
|
|
if (height > 1)
|
|
|
|
height /= 2;
|
|
|
|
}
|
2015-11-23 08:49:53 +00:00
|
|
|
}
|
|
|
|
public:
|
2015-11-26 23:03:01 +00:00
|
|
|
~GLTextureSA() {glDeleteTextures(1, &m_tex);}
|
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);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-11-02 10:07:15 +00:00
|
|
|
class GLTextureD : public 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;
|
2017-03-14 07:02:53 +00:00
|
|
|
GLTextureD(IGraphicsData* parent, size_t width, size_t height, TextureFormat fmt);
|
2015-12-02 21:09:49 +00:00
|
|
|
void update(int b);
|
2015-10-29 04:44:38 +00:00
|
|
|
public:
|
2015-11-02 10:07:15 +00:00
|
|
|
~GLTextureD();
|
2015-10-29 04:44:38 +00:00
|
|
|
|
2015-11-04 01:02:05 +00:00
|
|
|
void load(const void* data, size_t sz);
|
|
|
|
void* map(size_t sz);
|
|
|
|
void unmap();
|
|
|
|
|
2015-12-02 21:09:49 +00:00
|
|
|
void bind(size_t idx, int b);
|
2015-11-04 01:02:05 +00:00
|
|
|
};
|
|
|
|
|
2017-03-14 07:02:53 +00:00
|
|
|
#define MAX_BIND_TEXS 4
|
|
|
|
|
2015-11-04 01:02:05 +00:00
|
|
|
class GLTextureR : public ITextureR
|
|
|
|
{
|
|
|
|
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] = {};
|
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;
|
2016-01-08 00:04:37 +00:00
|
|
|
GLenum m_target;
|
2017-03-14 07:02:53 +00:00
|
|
|
GLTextureR(IGraphicsData* parent, GLCommandQueue* q, size_t width, size_t height, size_t samples,
|
|
|
|
size_t colorBindCount, size_t depthBindCount);
|
2015-11-04 01:02:05 +00:00
|
|
|
public:
|
|
|
|
~GLTextureR();
|
2015-10-30 00:00:56 +00:00
|
|
|
|
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);
|
2017-03-14 07:02:53 +00:00
|
|
|
glBindTexture(m_target, 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
|
|
|
|
2016-01-08 00:04:37 +00:00
|
|
|
if (m_samples > 1)
|
|
|
|
{
|
|
|
|
glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, m_texs[0]);
|
|
|
|
glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, m_samples, GL_RGBA, width, height, GL_FALSE);
|
|
|
|
glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, m_texs[1]);
|
|
|
|
glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, m_samples, GL_DEPTH_COMPONENT24, width, height, GL_FALSE);
|
2016-02-25 02:53:23 +00:00
|
|
|
|
2017-03-14 07:02:53 +00:00
|
|
|
for (int i=0 ; i<MAX_BIND_TEXS ; ++i)
|
2016-02-25 02:53:23 +00:00
|
|
|
{
|
2017-03-14 07:02:53 +00:00
|
|
|
if (m_bindTexs[0][i])
|
|
|
|
{
|
|
|
|
glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, m_bindTexs[0][i]);
|
|
|
|
glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, m_samples, GL_RGBA, width, height, GL_FALSE);
|
|
|
|
}
|
2016-02-25 02:53:23 +00:00
|
|
|
}
|
2017-03-14 07:02:53 +00:00
|
|
|
|
|
|
|
for (int i=0 ; i<MAX_BIND_TEXS ; ++i)
|
2016-02-25 02:53:23 +00:00
|
|
|
{
|
2017-03-14 07:02:53 +00:00
|
|
|
if (m_bindTexs[1][i])
|
|
|
|
{
|
|
|
|
glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, m_bindTexs[1][i]);
|
|
|
|
glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, m_samples, GL_DEPTH_COMPONENT24, width, height, GL_FALSE);
|
|
|
|
}
|
2016-02-25 02:53:23 +00:00
|
|
|
}
|
2016-01-08 00:04:37 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
glBindTexture(GL_TEXTURE_2D, m_texs[0]);
|
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, m_texs[1]);
|
2016-04-02 04:40:31 +00:00
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24, width, height, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, nullptr);
|
2016-04-02 00:03:36 +00:00
|
|
|
|
2016-04-02 04:40:31 +00:00
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, m_fbo);
|
|
|
|
glDepthMask(GL_TRUE);
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
2016-02-25 02:53:23 +00:00
|
|
|
|
2017-03-14 07:02:53 +00:00
|
|
|
for (int i=0 ; i<MAX_BIND_TEXS ; ++i)
|
2016-02-25 02:53:23 +00:00
|
|
|
{
|
2017-03-14 07:02:53 +00:00
|
|
|
if (m_bindTexs[0][i])
|
|
|
|
{
|
|
|
|
glBindTexture(GL_TEXTURE_2D, m_bindTexs[0][i]);
|
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
|
|
|
|
}
|
2016-02-25 02:53:23 +00:00
|
|
|
}
|
2017-03-14 07:02:53 +00:00
|
|
|
|
|
|
|
for (int i=0 ; i<MAX_BIND_TEXS ; ++i)
|
2016-02-25 02:53:23 +00:00
|
|
|
{
|
2017-03-14 07:02:53 +00:00
|
|
|
if (m_bindTexs[1][i])
|
|
|
|
{
|
|
|
|
glBindTexture(GL_TEXTURE_2D, m_bindTexs[1][i]);
|
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24, width, height, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, nullptr);
|
|
|
|
}
|
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
|
|
|
};
|
|
|
|
|
2015-11-02 09:31:06 +00:00
|
|
|
ITextureS*
|
2016-03-30 19:14:17 +00:00
|
|
|
GLDataFactory::Context::newStaticTexture(size_t width, size_t height, size_t mips, TextureFormat fmt,
|
|
|
|
const void* data, size_t sz)
|
2015-10-29 04:44:38 +00:00
|
|
|
{
|
2017-03-14 07:02:53 +00:00
|
|
|
GLData* d = GLDataFactoryImpl::m_deferredData.get();
|
|
|
|
GLTextureS* retval = new GLTextureS(d, width, height, mips, fmt, data, sz);
|
|
|
|
d->m_STexs.emplace_back(retval);
|
2015-10-29 04:44:38 +00:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2015-11-26 23:03:01 +00:00
|
|
|
ITextureSA*
|
2017-01-29 03:56:17 +00:00
|
|
|
GLDataFactory::Context::newStaticArrayTexture(size_t width, size_t height, size_t layers, size_t mips,
|
|
|
|
TextureFormat fmt, const void *data, size_t sz)
|
2015-11-23 08:49:53 +00:00
|
|
|
{
|
2017-03-14 07:02:53 +00:00
|
|
|
GLData* d = GLDataFactoryImpl::m_deferredData.get();
|
|
|
|
GLTextureSA* retval = new GLTextureSA(d, width, height, layers, mips, fmt, data, sz);
|
|
|
|
d->m_SATexs.emplace_back(retval);
|
2015-11-23 08:49:53 +00:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2015-11-02 10:07:15 +00:00
|
|
|
class GLShaderPipeline : public IShaderPipeline
|
2015-10-29 04:44:38 +00:00
|
|
|
{
|
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;
|
2017-03-05 07:54:58 +00:00
|
|
|
GLShareableShader::Token m_vert;
|
|
|
|
GLShareableShader::Token m_frag;
|
2015-10-29 04:44:38 +00:00
|
|
|
GLuint m_prog = 0;
|
|
|
|
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-03-10 20:38:00 +00:00
|
|
|
CullMode m_culling;
|
2015-11-16 04:29:31 +00:00
|
|
|
std::vector<GLint> m_uniLocs;
|
2017-03-14 07:02:53 +00:00
|
|
|
GLShaderPipeline(GLData* parent) : IShaderPipeline(parent) {}
|
2015-10-29 04:44:38 +00:00
|
|
|
public:
|
|
|
|
operator bool() const {return m_prog != 0;}
|
2017-03-05 23:34:24 +00:00
|
|
|
~GLShaderPipeline() { if (m_prog) glDeleteProgram(m_prog); }
|
2015-11-02 10:07:15 +00:00
|
|
|
GLShaderPipeline& operator=(const GLShaderPipeline&) = delete;
|
|
|
|
GLShaderPipeline(const GLShaderPipeline&) = delete;
|
2017-03-05 23:34:24 +00:00
|
|
|
GLShaderPipeline& operator=(GLShaderPipeline&& other)
|
|
|
|
{
|
|
|
|
m_vert = std::move(other.m_vert);
|
|
|
|
m_frag = std::move(other.m_frag);
|
|
|
|
m_prog = other.m_prog;
|
|
|
|
other.m_prog = 0;
|
|
|
|
m_sfactor = other.m_sfactor;
|
|
|
|
m_dfactor = other.m_dfactor;
|
|
|
|
m_drawPrim = other.m_drawPrim;
|
|
|
|
m_depthTest = other.m_depthTest;
|
|
|
|
m_depthWrite = other.m_depthWrite;
|
2017-03-14 07:02:53 +00:00
|
|
|
m_colorWrite = other.m_colorWrite;
|
|
|
|
m_alphaWrite = other.m_alphaWrite;
|
2017-03-10 20:38:00 +00:00
|
|
|
m_culling = other.m_culling;
|
2017-03-05 23:34:24 +00:00
|
|
|
m_uniLocs = std::move(other.m_uniLocs);
|
|
|
|
return *this;
|
|
|
|
}
|
2017-03-14 07:02:53 +00:00
|
|
|
GLShaderPipeline(GLShaderPipeline&& other)
|
|
|
|
: IShaderPipeline(other.m_parentData) { *this = std::move(other); }
|
2015-10-29 04:44:38 +00:00
|
|
|
|
2015-10-30 00:00:56 +00:00
|
|
|
GLuint bind() const
|
|
|
|
{
|
|
|
|
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);
|
2017-03-14 07:02:53 +00:00
|
|
|
glBlendFuncSeparate(m_sfactor, m_dfactor, GL_ONE, GL_ZERO);
|
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;
|
|
|
|
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
|
|
|
|
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
|
|
|
|
2016-03-24 08:05:19 +00:00
|
|
|
static const GLenum PRIMITIVE_TABLE[] =
|
|
|
|
{
|
|
|
|
GL_TRIANGLES,
|
|
|
|
GL_TRIANGLE_STRIP
|
|
|
|
};
|
|
|
|
|
2015-10-29 04:44:38 +00:00
|
|
|
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,
|
2015-11-25 02:14:30 +00:00
|
|
|
GL_ONE_MINUS_DST_ALPHA,
|
|
|
|
GL_SRC1_COLOR,
|
|
|
|
GL_ONE_MINUS_SRC1_COLOR
|
2015-10-29 04:44:38 +00:00
|
|
|
};
|
|
|
|
|
2016-03-30 19:14:17 +00:00
|
|
|
IShaderPipeline* GLDataFactory::Context::newShaderPipeline
|
2015-10-29 04:44:38 +00:00
|
|
|
(const char* vertSource, const char* fragSource,
|
2016-07-08 00:05:22 +00:00
|
|
|
size_t texCount, const char** texNames,
|
2015-11-16 04:29:31 +00:00
|
|
|
size_t uniformBlockCount, const char** uniformBlockNames,
|
2016-03-24 08:05:19 +00:00
|
|
|
BlendFactor srcFac, BlendFactor dstFac, Primitive prim,
|
2017-03-14 07:02:53 +00:00
|
|
|
ZTest depthTest, bool depthWrite, bool colorWrite,
|
|
|
|
bool alphaWrite, CullMode culling)
|
2015-10-29 04:44:38 +00:00
|
|
|
{
|
2017-03-14 07:02:53 +00:00
|
|
|
GLData* d = GLDataFactoryImpl::m_deferredData.get();
|
2017-03-05 07:54:58 +00:00
|
|
|
GLDataFactoryImpl& factory = static_cast<GLDataFactoryImpl&>(m_parent);
|
2017-03-14 07:02:53 +00:00
|
|
|
GLShaderPipeline shader(d);
|
2017-03-05 07:54:58 +00:00
|
|
|
|
|
|
|
XXH64_state_t hashState;
|
|
|
|
uint64_t hashes[2];
|
|
|
|
XXH64_reset(&hashState, 0);
|
|
|
|
XXH64_update(&hashState, vertSource, strlen(vertSource));
|
|
|
|
hashes[0] = XXH64_digest(&hashState);
|
|
|
|
XXH64_reset(&hashState, 0);
|
|
|
|
XXH64_update(&hashState, fragSource, strlen(fragSource));
|
|
|
|
hashes[1] = XXH64_digest(&hashState);
|
|
|
|
|
|
|
|
GLint status;
|
|
|
|
auto vertFind = factory.m_sharedShaders.find(hashes[0]);
|
|
|
|
if (vertFind != factory.m_sharedShaders.end())
|
2015-10-29 04:44:38 +00:00
|
|
|
{
|
2017-03-05 07:54:58 +00:00
|
|
|
shader.m_vert = vertFind->second->lock();
|
2015-10-29 04:44:38 +00:00
|
|
|
}
|
2017-03-05 07:54:58 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
GLuint sobj = glCreateShader(GL_VERTEX_SHADER);
|
|
|
|
if (!sobj)
|
|
|
|
{
|
|
|
|
Log.report(logvisor::Error, "unable to create vert shader");
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-10-29 04:44:38 +00:00
|
|
|
|
2017-03-05 07:54:58 +00:00
|
|
|
glShaderSource(sobj, 1, &vertSource, nullptr);
|
|
|
|
glCompileShader(sobj);
|
|
|
|
glGetShaderiv(sobj, GL_COMPILE_STATUS, &status);
|
|
|
|
if (status != GL_TRUE)
|
|
|
|
{
|
|
|
|
GLint logLen;
|
|
|
|
glGetShaderiv(sobj, GL_INFO_LOG_LENGTH, &logLen);
|
|
|
|
char* log = (char*)malloc(logLen);
|
|
|
|
glGetShaderInfoLog(sobj, logLen, nullptr, log);
|
|
|
|
Log.report(logvisor::Error, "unable to compile vert source\n%s\n%s\n", log, vertSource);
|
|
|
|
free(log);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto it =
|
|
|
|
factory.m_sharedShaders.emplace(std::make_pair(hashes[0],
|
|
|
|
std::make_unique<GLShareableShader>(factory, hashes[0], sobj))).first;
|
|
|
|
shader.m_vert = it->second->lock();
|
|
|
|
}
|
|
|
|
auto fragFind = factory.m_sharedShaders.find(hashes[1]);
|
|
|
|
if (fragFind != factory.m_sharedShaders.end())
|
2015-10-29 04:44:38 +00:00
|
|
|
{
|
2017-03-05 07:54:58 +00:00
|
|
|
shader.m_frag = fragFind->second->lock();
|
2015-10-29 04:44:38 +00:00
|
|
|
}
|
2017-03-05 07:54:58 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
GLuint sobj = glCreateShader(GL_FRAGMENT_SHADER);
|
|
|
|
if (!sobj)
|
|
|
|
{
|
|
|
|
Log.report(logvisor::Error, "unable to create frag shader");
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-10-29 04:44:38 +00:00
|
|
|
|
2017-03-05 07:54:58 +00:00
|
|
|
glShaderSource(sobj, 1, &fragSource, nullptr);
|
|
|
|
glCompileShader(sobj);
|
|
|
|
glGetShaderiv(sobj, GL_COMPILE_STATUS, &status);
|
|
|
|
if (status != GL_TRUE)
|
|
|
|
{
|
|
|
|
GLint logLen;
|
|
|
|
glGetShaderiv(sobj, GL_INFO_LOG_LENGTH, &logLen);
|
|
|
|
char* log = (char*)malloc(logLen);
|
|
|
|
glGetShaderInfoLog(sobj, logLen, nullptr, log);
|
|
|
|
Log.report(logvisor::Error, "unable to compile frag source\n%s\n%s\n", log, fragSource);
|
|
|
|
free(log);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto it =
|
|
|
|
factory.m_sharedShaders.emplace(std::make_pair(hashes[1],
|
|
|
|
std::make_unique<GLShareableShader>(factory, hashes[1], sobj))).first;
|
|
|
|
shader.m_frag = it->second->lock();
|
|
|
|
}
|
|
|
|
|
|
|
|
shader.m_prog = glCreateProgram();
|
|
|
|
if (!shader.m_prog)
|
2015-10-29 04:44:38 +00:00
|
|
|
{
|
2017-03-05 07:54:58 +00:00
|
|
|
Log.report(logvisor::Error, "unable to create shader program");
|
2015-10-29 04:44:38 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2017-03-05 07:54:58 +00:00
|
|
|
glAttachShader(shader.m_prog, shader.m_vert.get().m_shader);
|
|
|
|
glAttachShader(shader.m_prog, shader.m_frag.get().m_shader);
|
|
|
|
|
2015-10-29 04:44:38 +00:00
|
|
|
glLinkProgram(shader.m_prog);
|
|
|
|
glGetProgramiv(shader.m_prog, GL_LINK_STATUS, &status);
|
|
|
|
if (status != GL_TRUE)
|
|
|
|
{
|
|
|
|
GLint logLen;
|
|
|
|
glGetProgramiv(shader.m_prog, GL_INFO_LOG_LENGTH, &logLen);
|
|
|
|
char* log = (char*)malloc(logLen);
|
|
|
|
glGetProgramInfoLog(shader.m_prog, logLen, nullptr, log);
|
2016-03-04 23:02:18 +00:00
|
|
|
Log.report(logvisor::Error, "unable to link shader program\n%s\n", log);
|
2015-10-29 04:44:38 +00:00
|
|
|
free(log);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-10-31 04:28:21 +00:00
|
|
|
glUseProgram(shader.m_prog);
|
2015-11-16 04:29:31 +00:00
|
|
|
|
|
|
|
if (uniformBlockCount)
|
2015-10-31 04:28:21 +00:00
|
|
|
{
|
2015-11-16 04:29:31 +00:00
|
|
|
shader.m_uniLocs.reserve(uniformBlockCount);
|
|
|
|
for (size_t i=0 ; i<uniformBlockCount ; ++i)
|
|
|
|
{
|
|
|
|
GLint uniLoc = glGetUniformBlockIndex(shader.m_prog, uniformBlockNames[i]);
|
2016-07-31 04:45:03 +00:00
|
|
|
//if (uniLoc < 0)
|
|
|
|
// Log.report(logvisor::Warning, "unable to find uniform block '%s'", uniformBlockNames[i]);
|
2015-11-16 04:29:31 +00:00
|
|
|
shader.m_uniLocs.push_back(uniLoc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-08 00:05:22 +00:00
|
|
|
if (texCount && texNames)
|
2015-11-16 04:29:31 +00:00
|
|
|
{
|
2016-07-08 00:05:22 +00:00
|
|
|
for (int i=0 ; i<texCount ; ++i)
|
2016-02-16 05:47:09 +00:00
|
|
|
{
|
2016-07-08 00:05:22 +00:00
|
|
|
GLint texLoc = glGetUniformLocation(shader.m_prog, texNames[i]);
|
|
|
|
if (texLoc < 0)
|
2016-07-31 04:45:03 +00:00
|
|
|
{ /* Log.report(logvisor::Warning, "unable to find sampler variable '%s'", texNames[i]); */ }
|
2016-07-08 00:05:22 +00:00
|
|
|
else
|
|
|
|
glUniform1i(texLoc, i);
|
2016-02-16 05:47:09 +00:00
|
|
|
}
|
2015-10-31 04:28:21 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 07:54:58 +00:00
|
|
|
shader.m_sfactor = BLEND_FACTOR_TABLE[int(srcFac)];
|
|
|
|
shader.m_dfactor = BLEND_FACTOR_TABLE[int(dstFac)];
|
|
|
|
shader.m_depthTest = depthTest;
|
|
|
|
shader.m_depthWrite = depthWrite;
|
2017-03-14 07:02:53 +00:00
|
|
|
shader.m_colorWrite = colorWrite;
|
|
|
|
shader.m_alphaWrite = alphaWrite;
|
2017-03-10 20:38:00 +00:00
|
|
|
shader.m_culling = culling;
|
2017-03-05 07:54:58 +00:00
|
|
|
shader.m_drawPrim = PRIMITIVE_TABLE[int(prim)];
|
|
|
|
|
2015-11-02 10:07:15 +00:00
|
|
|
GLShaderPipeline* retval = new GLShaderPipeline(std::move(shader));
|
2017-03-14 07:02:53 +00:00
|
|
|
d->m_SPs.emplace_back(retval);
|
2015-10-29 04:44:38 +00:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2015-11-03 22:13:15 +00:00
|
|
|
struct GLVertexFormat : IVertexFormat
|
2015-10-30 00:00:56 +00:00
|
|
|
{
|
2015-11-02 10:07:15 +00:00
|
|
|
GLCommandQueue* m_q;
|
2015-11-30 02:37:46 +00:00
|
|
|
GLuint m_vao[3] = {};
|
2015-10-30 00:00:56 +00:00
|
|
|
size_t m_elementCount;
|
2016-12-10 02:31:50 +00:00
|
|
|
GLuint m_baseVert, m_baseInst;
|
2015-10-30 06:26:02 +00:00
|
|
|
std::unique_ptr<VertexElementDescriptor[]> m_elements;
|
2017-03-14 07:02:53 +00:00
|
|
|
GLVertexFormat(IGraphicsData* parent, GLCommandQueue* q, size_t elementCount,
|
2016-12-10 02:31:50 +00:00
|
|
|
const VertexElementDescriptor* elements,
|
|
|
|
size_t baseVert, size_t baseInst);
|
2015-11-03 22:13:15 +00:00
|
|
|
~GLVertexFormat();
|
2015-11-30 02:37:46 +00:00
|
|
|
void bind(int idx) const {glBindVertexArray(m_vao[idx]);}
|
2015-10-30 00:00:56 +00:00
|
|
|
};
|
|
|
|
|
2017-03-14 07:02:53 +00:00
|
|
|
struct GLShaderDataBinding : IShaderDataBindingPriv
|
2015-10-29 04:44:38 +00:00
|
|
|
{
|
2015-11-02 10:07:15 +00:00
|
|
|
const GLShaderPipeline* m_pipeline;
|
2015-11-03 22:13:15 +00:00
|
|
|
const GLVertexFormat* m_vtxFormat;
|
2015-10-30 00:00:56 +00:00
|
|
|
size_t m_ubufCount;
|
2015-11-02 09:31:06 +00:00
|
|
|
std::unique_ptr<IGraphicsBuffer*[]> m_ubufs;
|
2016-03-30 19:14:17 +00:00
|
|
|
std::vector<std::pair<size_t,size_t>> m_ubufOffs;
|
2015-10-30 00:00:56 +00:00
|
|
|
size_t m_texCount;
|
2017-03-14 07:02:53 +00:00
|
|
|
struct BoundTex
|
|
|
|
{
|
|
|
|
ITexture* tex;
|
|
|
|
int idx;
|
|
|
|
bool depth;
|
|
|
|
};
|
|
|
|
std::unique_ptr<BoundTex[]> m_texs;
|
2015-12-17 21:29:47 +00:00
|
|
|
|
2017-01-20 03:52:40 +00:00
|
|
|
GLShaderDataBinding(GLData* d,
|
|
|
|
IShaderPipeline* pipeline,
|
2015-11-16 04:29:31 +00:00
|
|
|
IVertexFormat* vtxFormat,
|
|
|
|
size_t ubufCount, IGraphicsBuffer** ubufs,
|
2016-03-30 19:14:17 +00:00
|
|
|
const size_t* ubufOffs, const size_t* ubufSizes,
|
2017-03-14 07:02:53 +00:00
|
|
|
size_t texCount, ITexture** texs,
|
|
|
|
const int* bindTexIdx,
|
|
|
|
const bool* depthBind)
|
2017-01-20 03:52:40 +00:00
|
|
|
: IShaderDataBindingPriv(d),
|
|
|
|
m_pipeline(static_cast<GLShaderPipeline*>(pipeline)),
|
2015-11-03 22:13:15 +00:00
|
|
|
m_vtxFormat(static_cast<GLVertexFormat*>(vtxFormat)),
|
2015-10-30 00:00:56 +00:00
|
|
|
m_ubufCount(ubufCount),
|
2015-11-02 09:31:06 +00:00
|
|
|
m_ubufs(new IGraphicsBuffer*[ubufCount]),
|
2015-10-30 00:00:56 +00:00
|
|
|
m_texCount(texCount),
|
2017-03-14 07:02:53 +00:00
|
|
|
m_texs(new BoundTex[texCount])
|
2015-10-30 00:00:56 +00:00
|
|
|
{
|
2017-03-14 07:02:53 +00:00
|
|
|
addDepData(m_pipeline->m_parentData);
|
|
|
|
addDepData(m_vtxFormat->m_parentData);
|
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
|
|
|
}
|
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
|
2015-10-30 00:00:56 +00:00
|
|
|
m_ubufs[i] = ubufs[i];
|
2017-03-14 07:02:53 +00:00
|
|
|
if (ubufs[i])
|
|
|
|
addDepData(ubufs[i]->m_parentData);
|
2016-03-30 19:14:17 +00:00
|
|
|
}
|
2015-10-30 00:00:56 +00:00
|
|
|
for (size_t i=0 ; i<texCount ; ++i)
|
2017-03-14 07:02:53 +00:00
|
|
|
{
|
|
|
|
m_texs[i] = {texs[i], bindTexIdx ? bindTexIdx[i] : 0, depthBind ? depthBind[i] : false};
|
|
|
|
if (texs[i])
|
|
|
|
addDepData(texs[i]->m_parentData);
|
|
|
|
}
|
2015-10-30 00:00:56 +00:00
|
|
|
}
|
2015-11-30 02:37:46 +00:00
|
|
|
void bind(int b) const
|
2015-10-29 04:44:38 +00:00
|
|
|
{
|
2015-10-30 00:00:56 +00:00
|
|
|
GLuint prog = m_pipeline->bind();
|
2015-11-30 02:37:46 +00:00
|
|
|
m_vtxFormat->bind(b);
|
2016-03-30 19:14:17 +00:00
|
|
|
if (m_ubufOffs.size())
|
2015-10-30 00:00:56 +00:00
|
|
|
{
|
2016-04-04 05:00:13 +00:00
|
|
|
for (size_t i=0 ; i<m_ubufCount && i<m_pipeline->m_uniLocs.size() ; ++i)
|
2016-03-30 19:14:17 +00:00
|
|
|
{
|
2016-04-04 05:00:13 +00:00
|
|
|
GLint loc = m_pipeline->m_uniLocs[i];
|
|
|
|
if (loc < 0)
|
|
|
|
continue;
|
2016-03-30 19:14:17 +00:00
|
|
|
IGraphicsBuffer* ubuf = m_ubufs[i];
|
|
|
|
const std::pair<size_t,size_t>& offset = m_ubufOffs[i];
|
|
|
|
if (ubuf->dynamic())
|
|
|
|
static_cast<GLGraphicsBufferD*>(ubuf)->bindUniformRange(i, offset.first, offset.second, b);
|
|
|
|
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
|
|
|
|
{
|
2016-04-04 05:00:13 +00:00
|
|
|
for (size_t i=0 ; i<m_ubufCount && i<m_pipeline->m_uniLocs.size() ; ++i)
|
2016-03-30 19:14:17 +00:00
|
|
|
{
|
2016-04-04 05:00:13 +00:00
|
|
|
GLint loc = m_pipeline->m_uniLocs[i];
|
|
|
|
if (loc < 0)
|
|
|
|
continue;
|
2016-03-30 19:14:17 +00:00
|
|
|
IGraphicsBuffer* ubuf = m_ubufs[i];
|
|
|
|
if (ubuf->dynamic())
|
|
|
|
static_cast<GLGraphicsBufferD*>(ubuf)->bindUniform(i, b);
|
|
|
|
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
|
|
|
}
|
|
|
|
for (size_t i=0 ; i<m_texCount ; ++i)
|
2015-11-02 09:31:06 +00:00
|
|
|
{
|
2017-03-14 07:02:53 +00:00
|
|
|
BoundTex& tex = m_texs[i];
|
|
|
|
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-03-14 07:02:53 +00:00
|
|
|
static_cast<GLTextureD*>(tex.tex)->bind(i, b);
|
2016-07-31 02:05:20 +00:00
|
|
|
break;
|
|
|
|
case TextureType::Static:
|
2017-03-14 07:02:53 +00:00
|
|
|
static_cast<GLTextureS*>(tex.tex)->bind(i);
|
2016-07-31 02:05:20 +00:00
|
|
|
break;
|
|
|
|
case TextureType::StaticArray:
|
2017-03-14 07:02:53 +00:00
|
|
|
static_cast<GLTextureSA*>(tex.tex)->bind(i);
|
2016-07-31 02:05:20 +00:00
|
|
|
break;
|
|
|
|
case TextureType::Render:
|
2017-03-14 07:02:53 +00:00
|
|
|
static_cast<GLTextureR*>(tex.tex)->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
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-11-02 09:31:06 +00:00
|
|
|
IShaderDataBinding*
|
2016-03-30 19:14:17 +00:00
|
|
|
GLDataFactory::Context::newShaderDataBinding(IShaderPipeline* pipeline,
|
|
|
|
IVertexFormat* vtxFormat,
|
|
|
|
IGraphicsBuffer*, IGraphicsBuffer*, IGraphicsBuffer*,
|
2016-04-04 06:13:11 +00:00
|
|
|
size_t ubufCount, IGraphicsBuffer** ubufs, const PipelineStage* ubufStages,
|
2016-03-30 19:14:17 +00:00
|
|
|
const size_t* ubufOffs, const size_t* ubufSizes,
|
2017-03-14 07:02:53 +00:00
|
|
|
size_t texCount, ITexture** texs,
|
|
|
|
const int* texBindIdx, const bool* depthBind,
|
|
|
|
size_t baseVert, size_t baseInst)
|
2015-10-29 04:44:38 +00:00
|
|
|
{
|
2015-11-03 22:13:15 +00:00
|
|
|
GLShaderDataBinding* retval =
|
2017-03-05 07:54:58 +00:00
|
|
|
new GLShaderDataBinding(GLDataFactoryImpl::m_deferredData.get(), pipeline, vtxFormat, ubufCount, ubufs,
|
2017-03-14 07:02:53 +00:00
|
|
|
ubufOffs, ubufSizes, texCount, texs, texBindIdx, depthBind);
|
2017-03-05 07:54:58 +00:00
|
|
|
GLDataFactoryImpl::m_deferredData->m_SBinds.emplace_back(retval);
|
2015-10-30 00:00:56 +00:00
|
|
|
return retval;
|
2015-10-29 04:44:38 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 07:54:58 +00:00
|
|
|
GLDataFactoryImpl::GLDataFactoryImpl(IGraphicsContext* parent, uint32_t drawSamples)
|
2016-02-24 03:11:58 +00:00
|
|
|
: m_parent(parent), m_drawSamples(drawSamples) {}
|
2015-10-29 04:44:38 +00:00
|
|
|
|
|
|
|
|
2017-03-05 07:54:58 +00:00
|
|
|
GraphicsDataToken GLDataFactoryImpl::commitTransaction(const FactoryCommitFunc& trans)
|
2015-10-29 04:44:38 +00:00
|
|
|
{
|
2016-03-30 19:14:17 +00:00
|
|
|
if (m_deferredData.get())
|
|
|
|
Log.report(logvisor::Fatal, "nested commitTransaction usage detected");
|
|
|
|
m_deferredData.reset(new GLData());
|
|
|
|
|
|
|
|
GLDataFactory::Context ctx(*this);
|
|
|
|
if (!trans(ctx))
|
|
|
|
{
|
|
|
|
delete m_deferredData.get();
|
|
|
|
m_deferredData.reset();
|
2015-12-18 03:25:23 +00:00
|
|
|
return GraphicsDataToken(this, nullptr);
|
2016-03-30 19:14:17 +00:00
|
|
|
}
|
|
|
|
|
2015-12-14 04:54:57 +00:00
|
|
|
std::unique_lock<std::mutex> lk(m_committedMutex);
|
2015-12-18 21:33:53 +00:00
|
|
|
GLData* retval = m_deferredData.get();
|
|
|
|
m_deferredData.reset();
|
2015-11-01 00:06:56 +00:00
|
|
|
m_committedData.insert(retval);
|
2017-01-20 03:52:40 +00:00
|
|
|
|
2016-02-16 05:47:09 +00:00
|
|
|
lk.unlock();
|
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();
|
2015-12-18 03:25:23 +00:00
|
|
|
return GraphicsDataToken(this, retval);
|
2015-10-29 04:44:38 +00:00
|
|
|
}
|
2015-12-14 04:54:57 +00:00
|
|
|
|
2017-03-05 07:54:58 +00:00
|
|
|
GraphicsBufferPoolToken GLDataFactoryImpl::newBufferPool()
|
2016-12-10 02:31:50 +00:00
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lk(m_committedMutex);
|
|
|
|
GLPool* retval = new GLPool;
|
|
|
|
m_committedPools.insert(retval);
|
|
|
|
return GraphicsBufferPoolToken(this, retval);
|
|
|
|
}
|
|
|
|
|
2017-03-05 07:54:58 +00:00
|
|
|
void GLDataFactoryImpl::destroyData(IGraphicsData* d)
|
2015-11-01 00:06:56 +00:00
|
|
|
{
|
2015-12-14 04:54:57 +00:00
|
|
|
std::unique_lock<std::mutex> lk(m_committedMutex);
|
2015-11-02 10:07:15 +00:00
|
|
|
GLData* data = static_cast<GLData*>(d);
|
2015-11-01 00:06:56 +00:00
|
|
|
m_committedData.erase(data);
|
2017-01-20 03:52:40 +00:00
|
|
|
data->decrement();
|
2015-11-01 00:06:56 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 07:54:58 +00:00
|
|
|
void GLDataFactoryImpl::destroyAllData()
|
2015-11-01 00:06:56 +00:00
|
|
|
{
|
2015-12-14 04:54:57 +00:00
|
|
|
std::unique_lock<std::mutex> lk(m_committedMutex);
|
2017-01-20 03:52:40 +00:00
|
|
|
for (GLData* data : m_committedData)
|
|
|
|
data->decrement();
|
2017-03-14 07:02:53 +00:00
|
|
|
for (GLPool* pool : m_committedPools)
|
|
|
|
delete pool;
|
2015-11-01 00:06:56 +00:00
|
|
|
m_committedData.clear();
|
2016-12-10 02:31:50 +00:00
|
|
|
m_committedPools.clear();
|
|
|
|
}
|
|
|
|
|
2017-03-05 07:54:58 +00:00
|
|
|
void GLDataFactoryImpl::destroyPool(IGraphicsBufferPool* p)
|
2016-12-10 02:31:50 +00:00
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lk(m_committedMutex);
|
|
|
|
GLPool* pool = static_cast<GLPool*>(p);
|
|
|
|
m_committedPools.erase(pool);
|
|
|
|
delete pool;
|
|
|
|
}
|
|
|
|
|
2017-03-05 07:54:58 +00:00
|
|
|
IGraphicsBufferD* GLDataFactoryImpl::newPoolBuffer(IGraphicsBufferPool* p, BufferUse use,
|
|
|
|
size_t stride, size_t count)
|
2016-12-10 02:31:50 +00:00
|
|
|
{
|
|
|
|
GLPool* pool = static_cast<GLPool*>(p);
|
2017-03-14 07:02:53 +00:00
|
|
|
GLPoolItem* item = new GLPoolItem;
|
|
|
|
GLGraphicsBufferD* retval = new GLGraphicsBufferD(item, use, stride * count);
|
|
|
|
item->m_buf.reset(retval);
|
|
|
|
pool->m_items.emplace(item);
|
2016-12-10 02:31:50 +00:00
|
|
|
return retval;
|
2015-11-01 00:06:56 +00:00
|
|
|
}
|
2015-10-29 04:44:38 +00:00
|
|
|
|
2017-03-14 07:02:53 +00:00
|
|
|
void GLDataFactoryImpl::deletePoolBuffer(IGraphicsBufferPool* p, IGraphicsBufferD* buf)
|
2016-12-11 06:18:41 +00:00
|
|
|
{
|
|
|
|
GLPool* pool = static_cast<GLPool*>(p);
|
2017-03-14 07:02:53 +00:00
|
|
|
auto search = pool->m_items.find(static_cast<GLPoolItem*>(buf->m_parentData));
|
|
|
|
if (search != pool->m_items.end())
|
|
|
|
{
|
|
|
|
(*search)->decrement();
|
|
|
|
pool->m_items.erase(search);
|
|
|
|
}
|
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
|
|
|
{
|
2016-08-24 04:33:20 +00:00
|
|
|
Platform platform() const {return IGraphicsDataFactory::Platform::OpenGL;}
|
|
|
|
const SystemChar* platformName() const {return _S("OpenGL");}
|
2015-10-30 00:00:56 +00:00
|
|
|
IGraphicsContext* m_parent = nullptr;
|
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::unique_lock<std::mutex> m_initlk;
|
|
|
|
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
|
|
|
|
{
|
|
|
|
const IShaderDataBinding* binding;
|
2015-11-04 01:02:05 +00:00
|
|
|
const ITextureR* target;
|
|
|
|
const ITextureR* source;
|
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;
|
|
|
|
};
|
|
|
|
};
|
2017-03-14 07:02:53 +00:00
|
|
|
IShaderDataBindingPriv::Token resToken;
|
2016-02-25 02:53:23 +00:00
|
|
|
const 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;
|
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];
|
|
|
|
size_t m_fillBuf = 0;
|
|
|
|
size_t m_completeBuf = 0;
|
|
|
|
size_t m_drawBuf = 0;
|
|
|
|
bool m_running = true;
|
2015-10-31 04:28:21 +00:00
|
|
|
|
2015-11-17 06:41:32 +00:00
|
|
|
struct RenderTextureResize
|
|
|
|
{
|
|
|
|
GLTextureR* tex;
|
|
|
|
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;
|
2015-11-03 22:13:15 +00:00
|
|
|
std::vector<GLVertexFormat*> m_pendingFmtAdds;
|
2015-11-30 02:37:46 +00:00
|
|
|
std::vector<std::array<GLuint, 3>> m_pendingFmtDels;
|
2015-11-04 01:02:05 +00:00
|
|
|
std::vector<GLTextureR*> m_pendingFboAdds;
|
2015-10-30 00:00:56 +00:00
|
|
|
std::vector<GLuint> m_pendingFboDels;
|
|
|
|
|
2015-11-03 22:13:15 +00:00
|
|
|
static void ConfigureVertexFormat(GLVertexFormat* fmt)
|
2015-10-30 00:00:56 +00:00
|
|
|
{
|
2015-11-30 02:37:46 +00:00
|
|
|
glGenVertexArrays(3, fmt->m_vao);
|
2015-10-30 00:00:56 +00:00
|
|
|
|
|
|
|
size_t stride = 0;
|
2015-11-25 02:14:30 +00:00
|
|
|
size_t instStride = 0;
|
2015-10-30 00:00:56 +00:00
|
|
|
for (size_t i=0 ; i<fmt->m_elementCount ; ++i)
|
|
|
|
{
|
2015-10-30 06:26:02 +00:00
|
|
|
const VertexElementDescriptor* desc = &fmt->m_elements[i];
|
2015-11-25 02:14:30 +00:00
|
|
|
if ((desc->semantic & VertexSemantic::Instanced) != VertexSemantic::None)
|
|
|
|
instStride += SEMANTIC_SIZE_TABLE[int(desc->semantic & VertexSemantic::SemanticMask)];
|
|
|
|
else
|
|
|
|
stride += SEMANTIC_SIZE_TABLE[int(desc->semantic & VertexSemantic::SemanticMask)];
|
2015-10-30 00:00:56 +00:00
|
|
|
}
|
|
|
|
|
2015-11-30 02:37:46 +00:00
|
|
|
for (int b=0 ; b<3 ; ++b)
|
2015-10-30 00:00:56 +00:00
|
|
|
{
|
2016-12-10 02:31:50 +00:00
|
|
|
size_t offset = fmt->m_baseVert * stride;
|
|
|
|
size_t instOffset = fmt->m_baseInst * instStride;
|
2015-11-30 02:37:46 +00:00
|
|
|
glBindVertexArray(fmt->m_vao[b]);
|
2015-12-02 21:09:49 +00:00
|
|
|
IGraphicsBuffer* lastVBO = nullptr;
|
|
|
|
IGraphicsBuffer* lastEBO = nullptr;
|
2015-11-30 02:37:46 +00:00
|
|
|
for (size_t i=0 ; i<fmt->m_elementCount ; ++i)
|
2015-10-30 00:00:56 +00:00
|
|
|
{
|
2015-11-30 02:37:46 +00:00
|
|
|
const VertexElementDescriptor* desc = &fmt->m_elements[i];
|
|
|
|
if (desc->vertBuffer != lastVBO)
|
|
|
|
{
|
|
|
|
lastVBO = desc->vertBuffer;
|
|
|
|
if (lastVBO->dynamic())
|
2015-12-02 21:09:49 +00:00
|
|
|
static_cast<GLGraphicsBufferD*>(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
|
|
|
}
|
|
|
|
if (desc->indexBuffer != lastEBO)
|
|
|
|
{
|
|
|
|
lastEBO = desc->indexBuffer;
|
|
|
|
if (lastEBO->dynamic())
|
2015-12-02 21:09:49 +00:00
|
|
|
static_cast<GLGraphicsBufferD*>(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);
|
|
|
|
int maskedSem = int(desc->semantic & VertexSemantic::SemanticMask);
|
|
|
|
if ((desc->semantic & VertexSemantic::Instanced) != VertexSemantic::None)
|
|
|
|
{
|
|
|
|
glVertexAttribPointer(i, SEMANTIC_COUNT_TABLE[maskedSem],
|
|
|
|
SEMANTIC_TYPE_TABLE[maskedSem], GL_TRUE, instStride, (void*)instOffset);
|
|
|
|
glVertexAttribDivisor(i, 1);
|
|
|
|
instOffset += SEMANTIC_SIZE_TABLE[maskedSem];
|
|
|
|
}
|
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);
|
|
|
|
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tex->m_texs[0], 0);
|
|
|
|
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, tex->m_texs[1], 0);
|
2015-10-30 00:00:56 +00:00
|
|
|
}
|
|
|
|
|
2015-11-02 10:07:15 +00:00
|
|
|
static void RenderingWorker(GLCommandQueue* self)
|
2015-10-29 04:44:38 +00:00
|
|
|
{
|
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();
|
|
|
|
if (glewInit() != GLEW_OK)
|
2016-03-04 23:02:18 +00:00
|
|
|
Log.report(logvisor::Fatal, "unable to init glew");
|
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);
|
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())
|
|
|
|
{
|
|
|
|
for (GLTextureR* tex : self->m_pendingFboAdds)
|
|
|
|
ConfigureFBO(tex);
|
|
|
|
self->m_pendingFboAdds.clear();
|
|
|
|
}
|
|
|
|
|
2015-11-17 06:41:32 +00:00
|
|
|
if (self->m_pendingResizes.size())
|
|
|
|
{
|
|
|
|
for (const RenderTextureResize& resize : self->m_pendingResizes)
|
|
|
|
resize.tex->resize(resize.width, resize.height);
|
|
|
|
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
|
|
|
{
|
2015-11-03 22:13:15 +00:00
|
|
|
for (GLVertexFormat* fmt : self->m_pendingFmtAdds)
|
2015-12-08 01:49:17 +00:00
|
|
|
if (fmt) ConfigureVertexFormat(fmt);
|
2015-11-17 06:41:32 +00:00
|
|
|
self->m_pendingFmtAdds.clear();
|
|
|
|
}
|
2015-10-30 00:00:56 +00:00
|
|
|
|
|
|
|
if (self->m_pendingFmtDels.size())
|
2015-11-17 06:41:32 +00:00
|
|
|
{
|
2015-11-30 02:37:46 +00:00
|
|
|
for (const auto& fmt : self->m_pendingFmtDels)
|
|
|
|
glDeleteVertexArrays(3, fmt.data());
|
2015-11-17 06:41:32 +00:00
|
|
|
self->m_pendingFmtDels.clear();
|
|
|
|
}
|
2015-10-30 00:00:56 +00:00
|
|
|
|
|
|
|
if (self->m_pendingFboDels.size())
|
2015-11-17 06:41:32 +00:00
|
|
|
{
|
2015-10-30 00:00:56 +00:00
|
|
|
for (GLuint fbo : self->m_pendingFboDels)
|
|
|
|
glDeleteFramebuffers(1, &fbo);
|
2015-11-17 06:41:32 +00:00
|
|
|
self->m_pendingFboDels.clear();
|
|
|
|
}
|
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;
|
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
|
|
|
{
|
|
|
|
const GLShaderDataBinding* binding = static_cast<const GLShaderDataBinding*>(cmd.binding);
|
|
|
|
binding->bind(self->m_drawBuf);
|
|
|
|
currentPrim = binding->m_pipeline->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
|
|
|
{
|
2015-11-04 01:02:05 +00:00
|
|
|
const GLTextureR* tex = static_cast<const GLTextureR*>(cmd.target);
|
|
|
|
if (!tex)
|
2016-04-02 03:55:07 +00:00
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
2015-11-04 01:02:05 +00:00
|
|
|
else
|
2016-04-02 03:55:07 +00:00
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, tex->m_fbo);
|
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:
|
2016-03-24 08:05:19 +00:00
|
|
|
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:
|
2016-04-02 03:32:39 +00:00
|
|
|
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:
|
|
|
|
{
|
|
|
|
const GLTextureR* tex = static_cast<const GLTextureR*>(cmd.resolveTex);
|
|
|
|
GLenum target = (tex->m_samples > 1) ? GL_TEXTURE_2D_MULTISAMPLE : GL_TEXTURE_2D;
|
|
|
|
glBindFramebuffer(GL_READ_FRAMEBUFFER, tex->m_fbo);
|
|
|
|
glActiveTexture(GL_TEXTURE9);
|
2017-03-14 07:02:53 +00:00
|
|
|
if (cmd.resolveColor && tex->m_bindTexs[0][cmd.bindIdx])
|
2016-02-25 02:53:23 +00:00
|
|
|
{
|
2017-03-14 07:02:53 +00:00
|
|
|
glBindTexture(target, tex->m_bindTexs[0][cmd.bindIdx]);
|
2016-04-15 20:41:24 +00:00
|
|
|
glCopyTexSubImage2D(target, 0, cmd.viewport.rect.location[0], cmd.viewport.rect.location[1],
|
|
|
|
cmd.viewport.rect.location[0], cmd.viewport.rect.location[1],
|
|
|
|
cmd.viewport.rect.size[0], cmd.viewport.rect.size[1]);
|
2016-02-25 02:53:23 +00:00
|
|
|
}
|
2017-03-14 07:02:53 +00:00
|
|
|
if (cmd.resolveDepth && tex->m_bindTexs[1][cmd.bindIdx])
|
2016-02-25 02:53:23 +00:00
|
|
|
{
|
2017-03-14 07:02:53 +00:00
|
|
|
glBindTexture(target, tex->m_bindTexs[1][cmd.bindIdx]);
|
2016-04-15 20:41:24 +00:00
|
|
|
glCopyTexSubImage2D(target, 0, cmd.viewport.rect.location[0], cmd.viewport.rect.location[1],
|
|
|
|
cmd.viewport.rect.location[0], cmd.viewport.rect.location[1],
|
|
|
|
cmd.viewport.rect.size[0], cmd.viewport.rect.size[1]);
|
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
|
|
|
{
|
|
|
|
const GLTextureR* tex = static_cast<const GLTextureR*>(cmd.source);
|
|
|
|
if (tex)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cmds.clear();
|
2015-12-21 00:40:52 +00:00
|
|
|
for (auto& p : posts)
|
|
|
|
p();
|
2015-10-29 04:44:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-02 10:07:15 +00:00
|
|
|
GLCommandQueue(IGraphicsContext* parent)
|
2015-10-29 04:44:38 +00:00
|
|
|
: m_parent(parent),
|
2015-10-31 06:33:36 +00:00
|
|
|
m_initlk(m_initmt),
|
2015-10-31 04:28:21 +00:00
|
|
|
m_thr(RenderingWorker, this)
|
|
|
|
{
|
|
|
|
m_initcv.wait(m_initlk);
|
|
|
|
m_initlk.unlock();
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2015-11-03 04:27:56 +00:00
|
|
|
void setShaderDataBinding(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;
|
2017-03-14 07:02:53 +00:00
|
|
|
cmds.back().resToken = static_cast<IShaderDataBindingPriv*>(binding)->lock();
|
2015-10-29 04:44:38 +00:00
|
|
|
}
|
2015-11-04 01:02:05 +00:00
|
|
|
|
|
|
|
void setRenderTarget(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
|
|
|
|
2015-11-04 01:58:36 +00:00
|
|
|
void resizeRenderTexture(ITextureR* tex, size_t width, size_t height)
|
|
|
|
{
|
2015-11-17 06:41:32 +00:00
|
|
|
std::unique_lock<std::mutex> lk(m_mt);
|
|
|
|
GLTextureR* texgl = static_cast<GLTextureR*>(tex);
|
|
|
|
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
|
|
|
|
2015-10-29 04:44:38 +00:00
|
|
|
void drawInstances(size_t start, size_t count, size_t instCount)
|
|
|
|
{
|
|
|
|
std::vector<Command>& cmds = m_cmdBufs[m_fillBuf];
|
2015-11-21 01:12:22 +00:00
|
|
|
cmds.emplace_back(Command::Op::DrawInstances);
|
2015-10-29 04:44:38 +00:00
|
|
|
cmds.back().start = start;
|
|
|
|
cmds.back().count = count;
|
|
|
|
cmds.back().instCount = instCount;
|
|
|
|
}
|
2015-11-04 01:02:05 +00:00
|
|
|
|
2015-10-29 04:44:38 +00:00
|
|
|
void drawInstancesIndexed(size_t start, size_t count, size_t instCount)
|
|
|
|
{
|
|
|
|
std::vector<Command>& cmds = m_cmdBufs[m_fillBuf];
|
2015-11-21 01:12:22 +00:00
|
|
|
cmds.emplace_back(Command::Op::DrawInstancesIndexed);
|
2015-10-29 04:44:38 +00:00
|
|
|
cmds.back().start = start;
|
|
|
|
cmds.back().count = count;
|
|
|
|
cmds.back().instCount = instCount;
|
|
|
|
}
|
|
|
|
|
2017-03-14 07:02:53 +00:00
|
|
|
void resolveBindTexture(ITextureR* texture, const SWindowRect& rect, bool tlOrigin,
|
|
|
|
int bindIdx, bool color, bool depth)
|
2016-02-25 02:53:23 +00:00
|
|
|
{
|
2016-02-27 01:38:13 +00:00
|
|
|
GLTextureR* tex = static_cast<GLTextureR*>(texture);
|
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;
|
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
|
|
|
}
|
|
|
|
|
2015-11-04 01:02:05 +00:00
|
|
|
void resolveDisplay(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
|
|
|
|
2015-11-03 22:13:15 +00:00
|
|
|
void addVertexFormat(GLVertexFormat* fmt)
|
2015-10-30 00:00:56 +00:00
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lk(m_mt);
|
|
|
|
m_pendingFmtAdds.push_back(fmt);
|
|
|
|
}
|
|
|
|
|
2015-11-03 22:13:15 +00:00
|
|
|
void delVertexFormat(GLVertexFormat* fmt)
|
2015-10-30 00:00:56 +00:00
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lk(m_mt);
|
2015-12-08 01:49:17 +00:00
|
|
|
bool foundAdd = false;
|
|
|
|
for (GLVertexFormat*& afmt : m_pendingFmtAdds)
|
|
|
|
if (afmt == fmt)
|
|
|
|
{
|
|
|
|
foundAdd = true;
|
|
|
|
afmt = nullptr;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!foundAdd)
|
|
|
|
m_pendingFmtDels.push_back({fmt->m_vao[0], fmt->m_vao[1], fmt->m_vao[2]});
|
2015-10-30 00:00:56 +00:00
|
|
|
}
|
|
|
|
|
2015-11-04 01:02:05 +00:00
|
|
|
void addFBO(GLTextureR* tex)
|
2015-10-30 00:00:56 +00:00
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lk(m_mt);
|
|
|
|
m_pendingFboAdds.push_back(tex);
|
|
|
|
}
|
|
|
|
|
2015-11-04 01:02:05 +00:00
|
|
|
void delFBO(GLTextureR* tex)
|
2015-10-30 00:00:56 +00:00
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lk(m_mt);
|
|
|
|
m_pendingFboDels.push_back(tex->m_fbo);
|
|
|
|
}
|
|
|
|
|
2015-10-29 04:44:38 +00:00
|
|
|
void execute()
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lk(m_mt);
|
|
|
|
m_completeBuf = m_fillBuf;
|
|
|
|
for (size_t i=0 ; i<3 ; ++i)
|
|
|
|
{
|
|
|
|
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());
|
2015-12-14 04:54:57 +00:00
|
|
|
std::unique_lock<std::mutex> datalk(gfxF->m_committedMutex);
|
2015-12-02 21:09:49 +00:00
|
|
|
for (GLData* d : gfxF->m_committedData)
|
|
|
|
{
|
|
|
|
for (std::unique_ptr<GLGraphicsBufferD>& b : d->m_DBufs)
|
|
|
|
b->update(m_completeBuf);
|
|
|
|
for (std::unique_ptr<GLTextureD>& t : d->m_DTexs)
|
|
|
|
t->update(m_completeBuf);
|
|
|
|
}
|
2016-12-10 02:31:50 +00:00
|
|
|
for (GLPool* p : gfxF->m_committedPools)
|
|
|
|
{
|
2017-03-14 07:02:53 +00:00
|
|
|
for (auto& b : p->m_items)
|
|
|
|
b->m_buf->update(m_completeBuf);
|
2016-12-10 02:31:50 +00:00
|
|
|
}
|
2015-12-14 04:54:57 +00:00
|
|
|
datalk.unlock();
|
2017-03-14 07:02:53 +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();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-12-02 21:09:49 +00:00
|
|
|
void GLGraphicsBufferD::update(int b)
|
|
|
|
{
|
|
|
|
int slot = 1 << b;
|
|
|
|
if ((slot & m_validMask) == 0)
|
|
|
|
{
|
|
|
|
glBindBuffer(m_target, m_bufs[b]);
|
2016-01-08 00:04:37 +00:00
|
|
|
glBufferSubData(m_target, 0, m_cpuSz, m_cpuBuf.get());
|
2015-12-02 21:09:49 +00:00
|
|
|
m_validMask |= slot;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-02 10:07:15 +00:00
|
|
|
void GLGraphicsBufferD::load(const void* data, size_t sz)
|
2015-10-30 00:00:56 +00:00
|
|
|
{
|
2015-12-02 21:09:49 +00:00
|
|
|
size_t bufSz = std::min(sz, m_cpuSz);
|
|
|
|
memcpy(m_cpuBuf.get(), data, bufSz);
|
|
|
|
m_validMask = 0;
|
2015-10-30 00:00:56 +00:00
|
|
|
}
|
2015-11-02 10:07:15 +00:00
|
|
|
void* GLGraphicsBufferD::map(size_t sz)
|
2015-10-30 00:00:56 +00:00
|
|
|
{
|
2015-12-02 21:09:49 +00:00
|
|
|
if (sz < m_cpuSz)
|
|
|
|
return nullptr;
|
|
|
|
return m_cpuBuf.get();
|
2015-10-30 00:00:56 +00:00
|
|
|
}
|
2015-11-02 10:07:15 +00:00
|
|
|
void GLGraphicsBufferD::unmap()
|
2015-10-30 00:00:56 +00:00
|
|
|
{
|
2015-12-02 21:09:49 +00:00
|
|
|
m_validMask = 0;
|
2015-10-30 00:00:56 +00:00
|
|
|
}
|
2015-12-02 21:09:49 +00:00
|
|
|
void GLGraphicsBufferD::bindVertex(int b)
|
2015-11-30 02:37:46 +00:00
|
|
|
{glBindBuffer(GL_ARRAY_BUFFER, m_bufs[b]);}
|
2015-12-02 21:09:49 +00:00
|
|
|
void GLGraphicsBufferD::bindIndex(int b)
|
2015-11-30 02:37:46 +00:00
|
|
|
{glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_bufs[b]);}
|
2015-12-02 21:09:49 +00:00
|
|
|
void GLGraphicsBufferD::bindUniform(size_t idx, int b)
|
2015-11-30 02:37:46 +00:00
|
|
|
{glBindBufferBase(GL_UNIFORM_BUFFER, idx, m_bufs[b]);}
|
2016-03-30 19:14:17 +00:00
|
|
|
void GLGraphicsBufferD::bindUniformRange(size_t idx, GLintptr off, GLsizeiptr size, int b)
|
|
|
|
{glBindBufferRange(GL_UNIFORM_BUFFER, idx, m_bufs[b], off, size);}
|
2015-10-30 00:00:56 +00:00
|
|
|
|
|
|
|
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-03-14 07:02:53 +00:00
|
|
|
GLData* d = GLDataFactoryImpl::m_deferredData.get();
|
|
|
|
GLGraphicsBufferD* retval = new GLGraphicsBufferD(d, use, stride * count);
|
|
|
|
d->m_DBufs.emplace_back(retval);
|
2015-10-30 00:00:56 +00:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2017-03-14 07:02:53 +00:00
|
|
|
GLTextureD::GLTextureD(IGraphicsData* parent, size_t width, size_t height, TextureFormat fmt)
|
|
|
|
: boo::ITextureD(parent), m_width(width), m_height(height)
|
2015-10-30 00:00:56 +00:00
|
|
|
{
|
2016-01-08 00:04:37 +00:00
|
|
|
int pxPitch = 4;
|
2015-12-02 21:09:49 +00:00
|
|
|
switch (fmt)
|
|
|
|
{
|
|
|
|
case TextureFormat::RGBA8:
|
2017-01-29 03:56:17 +00:00
|
|
|
m_intFormat = GL_RGBA8;
|
2015-12-02 21:09:49 +00:00
|
|
|
m_format = GL_RGBA;
|
|
|
|
pxPitch = 4;
|
|
|
|
break;
|
|
|
|
case TextureFormat::I8:
|
|
|
|
m_intFormat = GL_R8;
|
|
|
|
m_format = GL_RED;
|
|
|
|
pxPitch = 1;
|
|
|
|
break;
|
|
|
|
default:
|
2016-03-04 23:02:18 +00:00
|
|
|
Log.report(logvisor::Fatal, "unsupported tex format");
|
2015-12-02 21:09:49 +00:00
|
|
|
}
|
|
|
|
m_cpuSz = width * height * pxPitch;
|
|
|
|
m_cpuBuf.reset(new uint8_t[m_cpuSz]);
|
|
|
|
|
2015-11-04 01:02:05 +00:00
|
|
|
glGenTextures(3, m_texs);
|
2016-03-07 22:47:45 +00:00
|
|
|
for (int i=0 ; i<3 ; ++i)
|
|
|
|
{
|
|
|
|
glBindTexture(GL_TEXTURE_2D, m_texs[i]);
|
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, m_intFormat, width, height, 0, m_format, GL_UNSIGNED_BYTE, nullptr);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
|
|
|
}
|
2015-11-04 01:02:05 +00:00
|
|
|
}
|
|
|
|
GLTextureD::~GLTextureD() {glDeleteTextures(3, m_texs);}
|
|
|
|
|
2015-12-02 21:09:49 +00:00
|
|
|
void GLTextureD::update(int b)
|
|
|
|
{
|
|
|
|
int slot = 1 << b;
|
|
|
|
if ((slot & m_validMask) == 0)
|
|
|
|
{
|
|
|
|
glBindTexture(GL_TEXTURE_2D, m_texs[b]);
|
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, m_intFormat, m_width, m_height, 0, m_format, GL_UNSIGNED_BYTE, m_cpuBuf.get());
|
|
|
|
m_validMask |= slot;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-04 01:02:05 +00:00
|
|
|
void GLTextureD::load(const void* data, size_t sz)
|
|
|
|
{
|
2015-12-02 21:09:49 +00:00
|
|
|
size_t bufSz = std::min(sz, m_cpuSz);
|
|
|
|
memcpy(m_cpuBuf.get(), data, bufSz);
|
|
|
|
m_validMask = 0;
|
2015-11-04 01:02:05 +00:00
|
|
|
}
|
|
|
|
void* GLTextureD::map(size_t sz)
|
|
|
|
{
|
2015-12-02 21:09:49 +00:00
|
|
|
if (sz > m_cpuSz)
|
|
|
|
return nullptr;
|
|
|
|
return m_cpuBuf.get();
|
2015-11-04 01:02:05 +00:00
|
|
|
}
|
|
|
|
void GLTextureD::unmap()
|
|
|
|
{
|
2015-12-02 21:09:49 +00:00
|
|
|
m_validMask = 0;
|
2015-11-04 01:02:05 +00:00
|
|
|
}
|
|
|
|
|
2015-12-02 21:09:49 +00:00
|
|
|
void GLTextureD::bind(size_t idx, int b)
|
2015-11-04 01:02:05 +00:00
|
|
|
{
|
|
|
|
glActiveTexture(GL_TEXTURE0 + idx);
|
2015-12-02 21:09:49 +00:00
|
|
|
glBindTexture(GL_TEXTURE_2D, m_texs[b]);
|
2015-10-30 00:00:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ITextureD*
|
2016-03-30 19:14:17 +00:00
|
|
|
GLDataFactory::Context::newDynamicTexture(size_t width, size_t height, TextureFormat fmt)
|
2015-10-30 00:00:56 +00:00
|
|
|
{
|
2017-03-14 07:02:53 +00:00
|
|
|
GLData* d = GLDataFactoryImpl::m_deferredData.get();
|
|
|
|
GLTextureD* retval = new GLTextureD(d, width, height, fmt);
|
|
|
|
d->m_DTexs.emplace_back(retval);
|
2015-10-30 00:00:56 +00:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2017-03-14 07:02:53 +00:00
|
|
|
GLTextureR::GLTextureR(IGraphicsData* parent, GLCommandQueue* q, size_t width, size_t height, size_t samples,
|
|
|
|
size_t colorBindingCount, size_t depthBindingCount)
|
|
|
|
: boo::ITextureR(parent), m_q(q), m_width(width), m_height(height), m_samples(samples)
|
2015-11-04 01:02:05 +00:00
|
|
|
{
|
|
|
|
glGenTextures(2, m_texs);
|
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]);
|
|
|
|
}
|
2016-01-08 00:04:37 +00:00
|
|
|
if (samples > 1)
|
|
|
|
{
|
|
|
|
m_target = GL_TEXTURE_2D_MULTISAMPLE;
|
|
|
|
glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, m_texs[0]);
|
|
|
|
glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, samples, GL_RGBA, width, height, GL_FALSE);
|
|
|
|
glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, m_texs[1]);
|
|
|
|
glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, samples, GL_DEPTH_COMPONENT24, width, height, GL_FALSE);
|
2016-02-25 02:53:23 +00:00
|
|
|
|
2017-03-14 07:02:53 +00:00
|
|
|
for (int i=0 ; i<colorBindingCount ; ++i)
|
2016-02-25 02:53:23 +00:00
|
|
|
{
|
2017-03-14 07:02:53 +00:00
|
|
|
glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, m_bindTexs[0][i]);
|
2016-02-25 02:53:23 +00:00
|
|
|
glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, samples, GL_RGBA, width, height, GL_FALSE);
|
|
|
|
}
|
2017-03-14 07:02:53 +00:00
|
|
|
|
|
|
|
for (int i=0 ; i<depthBindingCount ; ++i)
|
2016-02-25 02:53:23 +00:00
|
|
|
{
|
2017-03-14 07:02:53 +00:00
|
|
|
glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, m_bindTexs[1][i]);
|
2016-02-25 02:53:23 +00:00
|
|
|
glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, samples, GL_DEPTH_COMPONENT24, width, height, GL_FALSE);
|
|
|
|
}
|
2016-01-08 00:04:37 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_target = GL_TEXTURE_2D;
|
|
|
|
glBindTexture(GL_TEXTURE_2D, m_texs[0]);
|
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, m_texs[1]);
|
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24, width, height, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, nullptr);
|
2016-02-25 02:53:23 +00:00
|
|
|
|
2017-03-14 07:02:53 +00:00
|
|
|
for (int i=0 ; i<colorBindingCount ; ++i)
|
2016-02-25 02:53:23 +00:00
|
|
|
{
|
2017-03-14 07:02:53 +00:00
|
|
|
glBindTexture(GL_TEXTURE_2D, m_bindTexs[0][i]);
|
2016-02-25 02:53:23 +00:00
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
|
|
|
}
|
2017-03-14 07:02:53 +00:00
|
|
|
for (int i=0 ; i<depthBindingCount ; ++i)
|
2016-02-25 02:53:23 +00:00
|
|
|
{
|
2017-03-14 07:02:53 +00:00
|
|
|
glBindTexture(GL_TEXTURE_2D, m_bindTexs[1][i]);
|
2016-02-25 02:53:23 +00:00
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24, width, height, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, nullptr);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
|
|
|
}
|
2016-01-08 00:04:37 +00:00
|
|
|
}
|
2015-11-04 01:02:05 +00:00
|
|
|
m_q->addFBO(this);
|
|
|
|
}
|
2016-02-25 02:53:23 +00:00
|
|
|
GLTextureR::~GLTextureR()
|
|
|
|
{
|
|
|
|
glDeleteTextures(2, m_texs);
|
2017-03-14 07:02:53 +00:00
|
|
|
glDeleteTextures(MAX_BIND_TEXS * 2, m_bindTexs[0]);
|
2016-02-25 02:53:23 +00:00
|
|
|
m_q->delFBO(this);
|
|
|
|
}
|
2015-11-04 01:02:05 +00:00
|
|
|
|
|
|
|
ITextureR*
|
2016-03-30 19:14:17 +00:00
|
|
|
GLDataFactory::Context::newRenderTexture(size_t width, size_t height,
|
2017-03-14 07:02:53 +00:00
|
|
|
size_t colorBindingCount, size_t depthBindingCount)
|
2015-11-04 01:02:05 +00:00
|
|
|
{
|
2017-03-14 07:02:53 +00:00
|
|
|
GLData* d = GLDataFactoryImpl::m_deferredData.get();
|
2017-03-05 07:54:58 +00:00
|
|
|
GLDataFactoryImpl& factory = static_cast<GLDataFactoryImpl&>(m_parent);
|
|
|
|
GLCommandQueue* q = static_cast<GLCommandQueue*>(factory.m_parent->getCommandQueue());
|
2017-03-14 07:02:53 +00:00
|
|
|
GLTextureR* retval = new GLTextureR(d, q, width, height, factory.m_drawSamples,
|
|
|
|
colorBindingCount, depthBindingCount);
|
2015-11-17 06:41:32 +00:00
|
|
|
q->resizeRenderTexture(retval, width, height);
|
2017-03-05 07:54:58 +00:00
|
|
|
GLDataFactoryImpl::m_deferredData->m_RTexs.emplace_back(retval);
|
2015-11-04 01:02:05 +00:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2017-03-14 07:02:53 +00:00
|
|
|
GLVertexFormat::GLVertexFormat(IGraphicsData* parent, GLCommandQueue* q, size_t elementCount,
|
2016-12-10 02:31:50 +00:00
|
|
|
const VertexElementDescriptor* elements,
|
|
|
|
size_t baseVert, size_t baseInst)
|
2017-03-14 07:02:53 +00:00
|
|
|
: boo::IVertexFormat(parent),
|
|
|
|
m_q(q),
|
2015-10-30 00:00:56 +00:00
|
|
|
m_elementCount(elementCount),
|
2016-12-10 02:31:50 +00:00
|
|
|
m_elements(new VertexElementDescriptor[elementCount]),
|
|
|
|
m_baseVert(baseVert), m_baseInst(baseInst)
|
2015-10-30 00:00:56 +00:00
|
|
|
{
|
|
|
|
for (size_t i=0 ; i<elementCount ; ++i)
|
|
|
|
m_elements[i] = elements[i];
|
|
|
|
m_q->addVertexFormat(this);
|
|
|
|
}
|
2015-11-03 22:13:15 +00:00
|
|
|
GLVertexFormat::~GLVertexFormat() {m_q->delVertexFormat(this);}
|
2015-10-30 00:00:56 +00:00
|
|
|
|
2016-03-30 19:14:17 +00:00
|
|
|
IVertexFormat* GLDataFactory::Context::newVertexFormat
|
2016-12-10 02:31:50 +00:00
|
|
|
(size_t elementCount, const VertexElementDescriptor* elements,
|
|
|
|
size_t baseVert, size_t baseInst)
|
2015-10-30 00:00:56 +00:00
|
|
|
{
|
2017-03-14 07:02:53 +00:00
|
|
|
GLData* d = GLDataFactoryImpl::m_deferredData.get();
|
2017-03-05 07:54:58 +00:00
|
|
|
GLDataFactoryImpl& factory = static_cast<GLDataFactoryImpl&>(m_parent);
|
|
|
|
GLCommandQueue* q = static_cast<GLCommandQueue*>(factory.m_parent->getCommandQueue());
|
2017-03-14 07:02:53 +00:00
|
|
|
GLVertexFormat* retval = new struct GLVertexFormat(d, q, elementCount, elements, baseVert, baseInst);
|
|
|
|
d->m_VFmts.emplace_back(retval);
|
2015-10-30 00:00:56 +00:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2015-11-07 01:43:12 +00:00
|
|
|
IGraphicsCommandQueue* _NewGLCommandQueue(IGraphicsContext* parent)
|
2015-10-29 04:44:38 +00:00
|
|
|
{
|
2015-11-02 10:07:15 +00:00
|
|
|
return new struct GLCommandQueue(parent);
|
2015-10-29 04:44:38 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 07:54:58 +00:00
|
|
|
IGraphicsDataFactory* _NewGLDataFactory(IGraphicsContext* parent, uint32_t drawSamples)
|
|
|
|
{
|
|
|
|
return new class GLDataFactoryImpl(parent, drawSamples);
|
|
|
|
}
|
|
|
|
|
2015-10-29 04:44:38 +00:00
|
|
|
}
|