2014-08-17 12:16:57 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <vector>
|
|
|
|
#include <string>
|
2017-02-19 11:22:23 +01:00
|
|
|
#include <algorithm>
|
2014-08-17 12:16:57 +02:00
|
|
|
#include <map>
|
2017-10-10 14:26:35 +02:00
|
|
|
#include <cassert>
|
2014-08-17 12:16:57 +02:00
|
|
|
|
|
|
|
#include "base/logging.h"
|
2015-10-10 16:41:19 +02:00
|
|
|
#include "math/dataconv.h"
|
2014-08-17 12:16:57 +02:00
|
|
|
#include "math/lin/matrix4x4.h"
|
|
|
|
#include "thin3d/thin3d.h"
|
2015-09-06 12:21:15 +02:00
|
|
|
#include "gfx/gl_common.h"
|
2017-03-03 14:15:27 +01:00
|
|
|
#include "gfx/gl_debug_log.h"
|
2015-09-06 12:21:15 +02:00
|
|
|
#include "gfx_es2/gpu_features.h"
|
2014-08-17 12:16:57 +02:00
|
|
|
|
2017-11-18 15:42:39 +01:00
|
|
|
#include "thin3d/GLRenderManager.h"
|
|
|
|
|
2017-02-06 11:55:54 +01:00
|
|
|
#ifdef IOS
|
|
|
|
extern void bindDefaultFBO();
|
2017-08-29 14:51:10 +02:00
|
|
|
#endif
|
2017-08-28 21:16:17 +02:00
|
|
|
|
2017-10-16 14:32:26 +02:00
|
|
|
// #define DEBUG_READ_PIXELS 1
|
|
|
|
|
2017-08-28 21:16:17 +02:00
|
|
|
// Workaround for Retroarch. Simply declare
|
|
|
|
// extern GLuint g_defaultFBO;
|
|
|
|
// and set is as appropriate. Can adjust the variables in ext/native/base/display.h as
|
|
|
|
// appropriate.
|
|
|
|
GLuint g_defaultFBO = 0;
|
2017-02-06 11:55:54 +01:00
|
|
|
|
2016-12-25 18:18:19 +01:00
|
|
|
namespace Draw {
|
|
|
|
|
2014-08-17 12:16:57 +02:00
|
|
|
static const unsigned short compToGL[] = {
|
|
|
|
GL_NEVER,
|
|
|
|
GL_LESS,
|
|
|
|
GL_EQUAL,
|
|
|
|
GL_LEQUAL,
|
|
|
|
GL_GREATER,
|
|
|
|
GL_NOTEQUAL,
|
|
|
|
GL_GEQUAL,
|
|
|
|
GL_ALWAYS
|
|
|
|
};
|
|
|
|
|
|
|
|
static const unsigned short blendEqToGL[] = {
|
|
|
|
GL_FUNC_ADD,
|
|
|
|
GL_FUNC_SUBTRACT,
|
|
|
|
GL_FUNC_REVERSE_SUBTRACT,
|
2016-12-26 23:51:17 +01:00
|
|
|
GL_MIN,
|
|
|
|
GL_MAX,
|
2014-08-17 12:16:57 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static const unsigned short blendFactorToGL[] = {
|
|
|
|
GL_ZERO,
|
|
|
|
GL_ONE,
|
|
|
|
GL_SRC_COLOR,
|
|
|
|
GL_ONE_MINUS_SRC_COLOR,
|
|
|
|
GL_DST_COLOR,
|
|
|
|
GL_ONE_MINUS_DST_COLOR,
|
2016-12-26 23:51:17 +01:00
|
|
|
GL_SRC_ALPHA,
|
|
|
|
GL_ONE_MINUS_SRC_ALPHA,
|
|
|
|
GL_DST_ALPHA,
|
2014-08-17 12:16:57 +02:00
|
|
|
GL_ONE_MINUS_DST_ALPHA,
|
|
|
|
GL_CONSTANT_COLOR,
|
2016-12-26 23:51:17 +01:00
|
|
|
GL_ONE_MINUS_CONSTANT_COLOR,
|
|
|
|
GL_CONSTANT_ALPHA,
|
|
|
|
GL_ONE_MINUS_CONSTANT_ALPHA,
|
2016-12-26 23:59:42 +01:00
|
|
|
#if !defined(USING_GLES2) // TODO: Remove when we have better headers
|
2016-12-26 23:51:17 +01:00
|
|
|
GL_SRC1_COLOR,
|
|
|
|
GL_ONE_MINUS_SRC1_COLOR,
|
|
|
|
GL_SRC1_ALPHA,
|
|
|
|
GL_ONE_MINUS_SRC1_ALPHA,
|
2016-12-26 23:59:42 +01:00
|
|
|
#elif !defined(IOS)
|
|
|
|
GL_SRC1_COLOR_EXT,
|
|
|
|
GL_ONE_MINUS_SRC1_COLOR_EXT,
|
|
|
|
GL_SRC1_ALPHA_EXT,
|
|
|
|
GL_ONE_MINUS_SRC1_ALPHA_EXT,
|
|
|
|
#else
|
|
|
|
GL_INVALID_ENUM,
|
|
|
|
GL_INVALID_ENUM,
|
|
|
|
GL_INVALID_ENUM,
|
|
|
|
GL_INVALID_ENUM,
|
|
|
|
#endif
|
2014-08-17 12:16:57 +02:00
|
|
|
};
|
|
|
|
|
2016-02-13 13:37:00 -08:00
|
|
|
static const unsigned short texWrapToGL[] = {
|
|
|
|
GL_REPEAT,
|
2016-12-26 23:51:17 +01:00
|
|
|
GL_MIRRORED_REPEAT,
|
2016-02-13 13:37:00 -08:00
|
|
|
GL_CLAMP_TO_EDGE,
|
2016-12-26 23:59:42 +01:00
|
|
|
#if !defined(USING_GLES2)
|
2016-12-26 23:51:17 +01:00
|
|
|
GL_CLAMP_TO_BORDER,
|
2016-12-26 23:59:42 +01:00
|
|
|
#else
|
2017-02-19 11:22:23 +01:00
|
|
|
GL_CLAMP_TO_EDGE,
|
2016-12-26 23:59:42 +01:00
|
|
|
#endif
|
2016-02-13 13:37:00 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
static const unsigned short texFilterToGL[] = {
|
|
|
|
GL_NEAREST,
|
|
|
|
GL_LINEAR,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const unsigned short texMipFilterToGL[2][2] = {
|
|
|
|
// Min nearest:
|
|
|
|
{ GL_NEAREST_MIPMAP_NEAREST, GL_NEAREST_MIPMAP_LINEAR },
|
|
|
|
// Min linear:
|
|
|
|
{ GL_LINEAR_MIPMAP_NEAREST, GL_LINEAR_MIPMAP_LINEAR },
|
|
|
|
};
|
|
|
|
|
2015-01-04 17:59:58 +01:00
|
|
|
#ifndef USING_GLES2
|
2015-01-04 17:57:52 +01:00
|
|
|
static const unsigned short logicOpToGL[] = {
|
|
|
|
GL_CLEAR,
|
|
|
|
GL_SET,
|
|
|
|
GL_COPY,
|
|
|
|
GL_COPY_INVERTED,
|
|
|
|
GL_NOOP,
|
|
|
|
GL_INVERT,
|
|
|
|
GL_AND,
|
|
|
|
GL_NAND,
|
|
|
|
GL_OR,
|
|
|
|
GL_NOR,
|
|
|
|
GL_XOR,
|
|
|
|
GL_EQUIV,
|
|
|
|
GL_AND_REVERSE,
|
|
|
|
GL_AND_INVERTED,
|
|
|
|
GL_OR_REVERSE,
|
|
|
|
GL_OR_INVERTED,
|
|
|
|
};
|
2015-01-04 17:59:58 +01:00
|
|
|
#endif
|
2015-01-04 17:57:52 +01:00
|
|
|
|
2016-12-26 23:11:31 +01:00
|
|
|
static const GLuint stencilOpToGL[8] = {
|
|
|
|
GL_KEEP,
|
|
|
|
GL_ZERO,
|
|
|
|
GL_REPLACE,
|
|
|
|
GL_INCR,
|
|
|
|
GL_DECR,
|
|
|
|
GL_INVERT,
|
|
|
|
GL_INCR_WRAP,
|
|
|
|
GL_DECR_WRAP,
|
|
|
|
};
|
|
|
|
|
2014-08-17 12:16:57 +02:00
|
|
|
static const unsigned short primToGL[] = {
|
|
|
|
GL_POINTS,
|
|
|
|
GL_LINES,
|
2016-12-25 21:01:57 +01:00
|
|
|
GL_LINE_STRIP,
|
2014-08-17 12:16:57 +02:00
|
|
|
GL_TRIANGLES,
|
2016-12-25 21:01:57 +01:00
|
|
|
GL_TRIANGLE_STRIP,
|
|
|
|
GL_TRIANGLE_FAN,
|
2016-12-26 23:59:42 +01:00
|
|
|
#if !defined(USING_GLES2) // TODO: Remove when we have better headers
|
2016-12-25 21:01:57 +01:00
|
|
|
GL_PATCHES,
|
|
|
|
GL_LINES_ADJACENCY,
|
|
|
|
GL_LINE_STRIP_ADJACENCY,
|
|
|
|
GL_TRIANGLES_ADJACENCY,
|
|
|
|
GL_TRIANGLE_STRIP_ADJACENCY,
|
2016-12-26 23:59:42 +01:00
|
|
|
#elif !defined(IOS)
|
|
|
|
GL_POINTS,
|
|
|
|
GL_POINTS,
|
|
|
|
GL_POINTS,
|
|
|
|
GL_POINTS,
|
|
|
|
GL_POINTS,
|
|
|
|
#else
|
|
|
|
GL_POINTS,
|
|
|
|
GL_POINTS,
|
|
|
|
GL_POINTS,
|
|
|
|
GL_POINTS,
|
|
|
|
GL_POINTS,
|
|
|
|
#endif
|
2014-08-17 12:16:57 +02:00
|
|
|
};
|
|
|
|
|
2017-02-07 19:04:44 +01:00
|
|
|
class OpenGLBuffer;
|
|
|
|
|
2016-12-25 21:21:56 +01:00
|
|
|
class OpenGLBlendState : public BlendState {
|
2014-08-17 12:16:57 +02:00
|
|
|
public:
|
|
|
|
bool enabled;
|
|
|
|
GLuint eqCol, eqAlpha;
|
|
|
|
GLuint srcCol, srcAlpha, dstCol, dstAlpha;
|
2016-12-26 18:32:52 +01:00
|
|
|
int colorMask;
|
2014-08-23 10:31:23 +02:00
|
|
|
// uint32_t fixedColor;
|
2014-08-17 12:16:57 +02:00
|
|
|
|
2017-11-18 16:50:49 +01:00
|
|
|
void Apply(GLRenderManager *render) {
|
2017-11-19 00:23:52 +01:00
|
|
|
render->SetBlendAndMask(colorMask, enabled, srcCol, dstCol, srcAlpha, dstAlpha, eqCol, eqAlpha);
|
2014-08-17 12:16:57 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-12-25 21:21:56 +01:00
|
|
|
class OpenGLSamplerState : public SamplerState {
|
2016-02-13 13:37:00 -08:00
|
|
|
public:
|
2017-02-19 11:22:23 +01:00
|
|
|
GLint wrapU;
|
|
|
|
GLint wrapV;
|
|
|
|
GLint wrapW;
|
2016-02-13 13:37:00 -08:00
|
|
|
GLint magFilt;
|
|
|
|
GLint minFilt;
|
|
|
|
GLint mipMinFilt;
|
|
|
|
};
|
|
|
|
|
2016-12-25 21:21:56 +01:00
|
|
|
class OpenGLDepthStencilState : public DepthStencilState {
|
2014-08-17 12:16:57 +02:00
|
|
|
public:
|
|
|
|
bool depthTestEnabled;
|
|
|
|
bool depthWriteEnabled;
|
|
|
|
GLuint depthComp;
|
2016-12-26 23:11:31 +01:00
|
|
|
// TODO: Two-sided
|
|
|
|
GLboolean stencilEnabled;
|
|
|
|
GLuint stencilFail;
|
|
|
|
GLuint stencilZFail;
|
|
|
|
GLuint stencilPass;
|
|
|
|
GLuint stencilCompareOp;
|
|
|
|
uint8_t stencilReference;
|
|
|
|
uint8_t stencilCompareMask;
|
|
|
|
uint8_t stencilWriteMask;
|
2014-08-17 12:16:57 +02:00
|
|
|
|
2017-11-18 16:50:49 +01:00
|
|
|
void Apply(GLRenderManager *render) {
|
|
|
|
render->SetDepth(depthTestEnabled, depthWriteEnabled, depthComp);
|
2017-12-12 15:04:46 +01:00
|
|
|
render->SetStencilFunc(stencilEnabled, stencilCompareOp, stencilReference, stencilCompareMask);
|
|
|
|
render->SetStencilOp(stencilWriteMask, stencilFail, stencilZFail, stencilPass);
|
2014-08-17 12:16:57 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-12-25 21:21:56 +01:00
|
|
|
class OpenGLRasterState : public RasterState {
|
2016-12-23 23:46:11 +01:00
|
|
|
public:
|
2017-11-18 16:50:49 +01:00
|
|
|
void Apply(GLRenderManager *render) {
|
2017-11-19 00:43:58 +01:00
|
|
|
render->SetRaster(cullEnable, frontFace, cullMode, false);
|
2016-12-23 23:46:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
GLboolean cullEnable;
|
|
|
|
GLenum cullMode;
|
|
|
|
GLenum frontFace;
|
|
|
|
};
|
|
|
|
|
2016-12-25 22:24:14 +01:00
|
|
|
GLuint ShaderStageToOpenGL(ShaderStage stage) {
|
|
|
|
switch (stage) {
|
|
|
|
case ShaderStage::VERTEX: return GL_VERTEX_SHADER;
|
2016-12-26 23:59:42 +01:00
|
|
|
#ifndef USING_GLES2
|
2016-12-25 22:24:14 +01:00
|
|
|
case ShaderStage::COMPUTE: return GL_COMPUTE_SHADER;
|
|
|
|
case ShaderStage::EVALUATION: return GL_TESS_EVALUATION_SHADER;
|
|
|
|
case ShaderStage::CONTROL: return GL_TESS_CONTROL_SHADER;
|
|
|
|
case ShaderStage::GEOMETRY: return GL_GEOMETRY_SHADER;
|
2016-12-26 23:59:42 +01:00
|
|
|
#endif
|
2016-12-25 22:24:14 +01:00
|
|
|
case ShaderStage::FRAGMENT:
|
|
|
|
default:
|
|
|
|
return GL_FRAGMENT_SHADER;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-07 14:56:19 +01:00
|
|
|
class OpenGLShaderModule : public ShaderModule {
|
2014-08-17 12:16:57 +02:00
|
|
|
public:
|
2017-11-18 16:50:49 +01:00
|
|
|
OpenGLShaderModule(GLRenderManager *render, ShaderStage stage) : render_(render), stage_(stage) {
|
|
|
|
ILOG("Shader module created (%p)", this);
|
2016-12-25 22:24:14 +01:00
|
|
|
glstage_ = ShaderStageToOpenGL(stage);
|
|
|
|
}
|
|
|
|
|
|
|
|
~OpenGLShaderModule() {
|
2017-03-18 15:20:36 +01:00
|
|
|
if (shader_)
|
2017-11-18 16:50:49 +01:00
|
|
|
render_->DeleteShader(shader_);
|
2014-08-17 12:16:57 +02:00
|
|
|
}
|
|
|
|
|
2017-11-18 16:50:49 +01:00
|
|
|
bool Compile(GLRenderManager *render, ShaderLanguage language, const uint8_t *data, size_t dataSize);
|
|
|
|
GLRShader *GetShader() const {
|
2014-09-06 13:49:01 +02:00
|
|
|
return shader_;
|
|
|
|
}
|
|
|
|
const std::string &GetSource() const { return source_; }
|
2014-08-17 12:16:57 +02:00
|
|
|
|
2016-12-27 17:38:26 +01:00
|
|
|
ShaderLanguage GetLanguage() {
|
|
|
|
return language_;
|
|
|
|
}
|
2016-12-25 22:24:14 +01:00
|
|
|
ShaderStage GetStage() const override {
|
|
|
|
return stage_;
|
2014-08-17 12:16:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2017-11-18 16:50:49 +01:00
|
|
|
GLRenderManager *render_;
|
2016-12-25 22:24:14 +01:00
|
|
|
ShaderStage stage_;
|
2016-12-27 17:38:26 +01:00
|
|
|
ShaderLanguage language_;
|
2017-11-18 16:50:49 +01:00
|
|
|
GLRShader *shader_ = nullptr;
|
2017-03-18 15:20:36 +01:00
|
|
|
GLuint glstage_ = 0;
|
|
|
|
bool ok_ = false;
|
2014-09-06 13:49:01 +02:00
|
|
|
std::string source_; // So we can recompile in case of context loss.
|
2014-08-17 12:16:57 +02:00
|
|
|
};
|
|
|
|
|
2017-11-18 16:50:49 +01:00
|
|
|
bool OpenGLShaderModule::Compile(GLRenderManager *render, ShaderLanguage language, const uint8_t *data, size_t dataSize) {
|
2016-12-27 17:38:26 +01:00
|
|
|
source_ = std::string((const char *)data);
|
2014-08-23 09:25:59 -07:00
|
|
|
std::string temp;
|
2017-09-20 12:22:01 -07:00
|
|
|
// Add the prelude on automatically.
|
|
|
|
if (glstage_ == GL_FRAGMENT_SHADER || glstage_ == GL_VERTEX_SHADER) {
|
|
|
|
temp = ApplyGLSLPrelude(source_, glstage_);
|
2016-12-27 17:38:26 +01:00
|
|
|
source_ = temp.c_str();
|
2014-08-23 09:25:59 -07:00
|
|
|
}
|
|
|
|
|
2017-11-18 16:50:49 +01:00
|
|
|
shader_ = render->CreateShader(glstage_, source_);
|
|
|
|
return true;
|
2014-08-17 12:16:57 +02:00
|
|
|
}
|
|
|
|
|
2017-12-07 14:56:19 +01:00
|
|
|
class OpenGLInputLayout : public InputLayout {
|
2014-08-17 12:16:57 +02:00
|
|
|
public:
|
2017-11-18 21:12:11 +01:00
|
|
|
OpenGLInputLayout(GLRenderManager *render) : render_(render) {}
|
2016-12-26 13:22:48 +01:00
|
|
|
~OpenGLInputLayout();
|
2015-12-13 09:25:22 -08:00
|
|
|
|
2017-11-18 21:12:11 +01:00
|
|
|
void Compile(const InputLayoutDesc &desc);
|
2016-12-26 17:03:01 +01:00
|
|
|
bool RequiresBuffer() {
|
2017-11-18 21:12:11 +01:00
|
|
|
return false;
|
2015-12-13 09:25:22 -08:00
|
|
|
}
|
2014-08-17 12:16:57 +02:00
|
|
|
|
2017-11-18 21:12:11 +01:00
|
|
|
GLRInputLayout *inputLayout_;
|
2017-11-19 00:23:52 +01:00
|
|
|
int stride;
|
2017-11-18 21:12:11 +01:00
|
|
|
private:
|
|
|
|
GLRenderManager *render_;
|
2014-08-17 12:16:57 +02:00
|
|
|
};
|
|
|
|
|
2017-12-07 14:56:19 +01:00
|
|
|
class OpenGLPipeline : public Pipeline {
|
2014-08-17 12:16:57 +02:00
|
|
|
public:
|
2017-11-18 16:50:49 +01:00
|
|
|
OpenGLPipeline(GLRenderManager *render) : render_(render) {
|
2014-08-17 12:16:57 +02:00
|
|
|
}
|
2016-12-26 11:06:17 +01:00
|
|
|
~OpenGLPipeline() {
|
2017-03-18 15:20:36 +01:00
|
|
|
for (auto &iter : shaders) {
|
2016-12-25 23:03:20 +01:00
|
|
|
iter->Release();
|
|
|
|
}
|
2017-11-18 16:50:49 +01:00
|
|
|
render_->DeleteProgram(program_);
|
2016-12-26 17:03:01 +01:00
|
|
|
if (depthStencil) depthStencil->Release();
|
|
|
|
if (blend) blend->Release();
|
|
|
|
if (raster) raster->Release();
|
|
|
|
if (inputLayout) inputLayout->Release();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LinkShaders();
|
2015-09-17 20:29:37 +02:00
|
|
|
|
2017-02-08 13:07:38 +01:00
|
|
|
bool RequiresBuffer() override {
|
|
|
|
return inputLayout->RequiresBuffer();
|
|
|
|
}
|
|
|
|
|
2016-12-26 17:03:01 +01:00
|
|
|
GLuint prim;
|
2016-12-25 23:03:20 +01:00
|
|
|
std::vector<OpenGLShaderModule *> shaders;
|
2016-12-26 17:03:01 +01:00
|
|
|
OpenGLInputLayout *inputLayout = nullptr;
|
|
|
|
OpenGLDepthStencilState *depthStencil = nullptr;
|
|
|
|
OpenGLBlendState *blend = nullptr;
|
|
|
|
OpenGLRasterState *raster = nullptr;
|
2014-08-17 12:16:57 +02:00
|
|
|
|
2017-02-08 12:26:48 +01:00
|
|
|
// TODO: Optimize by getting the locations first and putting in a custom struct
|
|
|
|
UniformBufferDesc dynamicUniforms;
|
|
|
|
|
2017-11-18 16:50:49 +01:00
|
|
|
GLRProgram *program_ = nullptr;
|
2017-02-08 12:55:58 +01:00
|
|
|
private:
|
2017-11-18 16:50:49 +01:00
|
|
|
GLRenderManager *render_;
|
2014-08-17 12:16:57 +02:00
|
|
|
};
|
|
|
|
|
2017-02-04 18:46:12 +01:00
|
|
|
class OpenGLFramebuffer;
|
2017-02-19 11:22:23 +01:00
|
|
|
class OpenGLTexture;
|
2017-02-04 18:46:12 +01:00
|
|
|
|
2016-12-25 21:21:56 +01:00
|
|
|
class OpenGLContext : public DrawContext {
|
2014-08-17 12:16:57 +02:00
|
|
|
public:
|
2016-12-25 21:21:56 +01:00
|
|
|
OpenGLContext();
|
|
|
|
virtual ~OpenGLContext();
|
2014-08-17 12:16:57 +02:00
|
|
|
|
2017-12-12 15:04:46 +01:00
|
|
|
void SetTargetSize(int w, int h) override {
|
|
|
|
DrawContext::SetTargetSize(w, h);
|
|
|
|
renderManager_.Resize(w, h);
|
|
|
|
}
|
|
|
|
|
2016-12-26 17:31:20 +01:00
|
|
|
const DeviceCaps &GetDeviceCaps() const override {
|
|
|
|
return caps_;
|
|
|
|
}
|
2016-12-27 16:33:54 +01:00
|
|
|
uint32_t GetSupportedShaderLanguages() const override {
|
|
|
|
#if defined(USING_GLES2)
|
|
|
|
return (uint32_t)ShaderLanguage::GLSL_ES_200 | (uint32_t)ShaderLanguage::GLSL_ES_300;
|
|
|
|
#else
|
2016-12-27 17:38:26 +01:00
|
|
|
return (uint32_t)ShaderLanguage::GLSL_ES_200 | (uint32_t)ShaderLanguage::GLSL_410;
|
2016-12-27 16:33:54 +01:00
|
|
|
#endif
|
|
|
|
}
|
2017-01-19 12:16:36 +07:00
|
|
|
uint32_t GetDataFormatSupport(DataFormat fmt) const override;
|
2016-12-26 17:31:20 +01:00
|
|
|
|
2016-12-25 21:10:46 +01:00
|
|
|
DepthStencilState *CreateDepthStencilState(const DepthStencilStateDesc &desc) override;
|
2016-12-25 18:52:05 +01:00
|
|
|
BlendState *CreateBlendState(const BlendStateDesc &desc) override;
|
2016-12-25 20:54:37 +01:00
|
|
|
SamplerState *CreateSamplerState(const SamplerStateDesc &desc) override;
|
2016-12-25 22:24:14 +01:00
|
|
|
RasterState *CreateRasterState(const RasterStateDesc &desc) override;
|
2016-12-26 13:42:53 +01:00
|
|
|
Pipeline *CreateGraphicsPipeline(const PipelineDesc &desc) override;
|
2016-12-26 13:22:48 +01:00
|
|
|
InputLayout *CreateInputLayout(const InputLayoutDesc &desc) override;
|
2016-12-27 17:38:26 +01:00
|
|
|
ShaderModule *CreateShaderModule(ShaderStage stage, ShaderLanguage language, const uint8_t *data, size_t dataSize) override;
|
2016-12-27 16:33:54 +01:00
|
|
|
|
2017-01-16 23:43:07 +07:00
|
|
|
Texture *CreateTexture(const TextureDesc &desc) override;
|
2017-02-04 18:46:12 +01:00
|
|
|
Buffer *CreateBuffer(size_t size, uint32_t usageFlags) override;
|
2017-02-06 11:26:24 +01:00
|
|
|
Framebuffer *CreateFramebuffer(const FramebufferDesc &desc) override;
|
2017-02-04 18:46:12 +01:00
|
|
|
|
2017-11-18 21:12:11 +01:00
|
|
|
void BeginFrame() override;
|
|
|
|
void EndFrame() override;
|
|
|
|
|
2017-02-07 19:41:58 +01:00
|
|
|
void UpdateBuffer(Buffer *buffer, const uint8_t *data, size_t offset, size_t size, UpdateBufferFlags flags) override;
|
2017-02-07 18:16:52 +01:00
|
|
|
|
2017-02-12 11:20:55 +01:00
|
|
|
void CopyFramebufferImage(Framebuffer *src, int level, int x, int y, int z, Framebuffer *dst, int dstLevel, int dstX, int dstY, int dstZ, int width, int height, int depth, int channelBits) override;
|
2017-02-06 11:26:24 +01:00
|
|
|
bool BlitFramebuffer(Framebuffer *src, int srcX1, int srcY1, int srcX2, int srcY2, Framebuffer *dst, int dstX1, int dstY1, int dstX2, int dstY2, int channelBits, FBBlitFilter filter) override;
|
2017-10-10 16:51:44 +02:00
|
|
|
bool CopyFramebufferToMemorySync(Framebuffer *src, int channelBits, int x, int y, int w, int h, Draw::DataFormat format, void *pixels, int pixelStride) override;
|
2017-02-04 18:46:12 +01:00
|
|
|
|
|
|
|
// These functions should be self explanatory.
|
2017-05-16 16:00:34 +02:00
|
|
|
void BindFramebufferAsRenderTarget(Framebuffer *fbo, const RenderPassInfo &rp) override;
|
2017-02-04 18:46:12 +01:00
|
|
|
// color must be 0, for now.
|
2017-02-06 11:26:24 +01:00
|
|
|
void BindFramebufferAsTexture(Framebuffer *fbo, int binding, FBChannel channelBit, int attachment) override;
|
2017-02-04 18:46:12 +01:00
|
|
|
|
2017-02-06 11:26:24 +01:00
|
|
|
void GetFramebufferDimensions(Framebuffer *fbo, int *w, int *h) override;
|
2014-08-17 12:16:57 +02:00
|
|
|
|
2016-12-26 11:16:59 +01:00
|
|
|
void BindSamplerStates(int start, int count, SamplerState **states) override {
|
2017-02-19 11:22:23 +01:00
|
|
|
if (boundSamplers_.size() < (size_t)(start + count)) {
|
|
|
|
boundSamplers_.resize(start + count);
|
2016-02-13 13:37:00 -08:00
|
|
|
}
|
2017-02-19 11:22:23 +01:00
|
|
|
for (int i = 0; i < count; i++) {
|
2016-02-13 13:37:00 -08:00
|
|
|
int index = i + start;
|
2017-02-19 11:22:23 +01:00
|
|
|
boundSamplers_[index] = static_cast<OpenGLSamplerState *>(states[index]);
|
2016-02-13 13:37:00 -08:00
|
|
|
}
|
2016-12-23 23:46:11 +01:00
|
|
|
}
|
|
|
|
|
2014-08-17 12:16:57 +02:00
|
|
|
void SetScissorRect(int left, int top, int width, int height) override {
|
2017-11-21 16:45:29 +01:00
|
|
|
int y = top;
|
|
|
|
if (!curFB_) {
|
|
|
|
// We render "upside down" to the backbuffer since GL is silly.
|
|
|
|
y = targetHeight_ - (top + height);
|
|
|
|
}
|
2017-11-18 21:12:11 +01:00
|
|
|
renderManager_.SetScissor({ left, y, width, height });
|
2014-08-17 12:16:57 +02:00
|
|
|
}
|
|
|
|
|
2016-12-25 18:52:05 +01:00
|
|
|
void SetViewports(int count, Viewport *viewports) override {
|
2017-11-18 20:17:17 +01:00
|
|
|
// Same structure, different name.
|
|
|
|
renderManager_.SetViewport((GLRViewport &)*viewports);
|
2014-08-17 12:16:57 +02:00
|
|
|
}
|
|
|
|
|
2016-12-27 15:52:03 +01:00
|
|
|
void SetBlendFactor(float color[4]) override {
|
2017-11-18 20:17:17 +01:00
|
|
|
renderManager_.SetBlendFactor(color);
|
2016-12-27 15:52:03 +01:00
|
|
|
}
|
|
|
|
|
2016-12-25 20:54:37 +01:00
|
|
|
void BindTextures(int start, int count, Texture **textures) override;
|
2016-12-26 17:03:01 +01:00
|
|
|
void BindPipeline(Pipeline *pipeline) override;
|
2017-01-17 23:14:47 +07:00
|
|
|
void BindVertexBuffers(int start, int count, Buffer **buffers, int *offsets) override {
|
|
|
|
for (int i = 0; i < count; i++) {
|
|
|
|
curVBuffers_[i + start] = (OpenGLBuffer *)buffers[i];
|
|
|
|
curVBufferOffsets_[i + start] = offsets ? offsets[i] : 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void BindIndexBuffer(Buffer *indexBuffer, int offset) override {
|
|
|
|
curIBuffer_ = (OpenGLBuffer *)indexBuffer;
|
|
|
|
curIBufferOffset_ = offset;
|
|
|
|
}
|
2014-08-17 15:02:43 +02:00
|
|
|
|
2017-02-19 11:22:23 +01:00
|
|
|
void UpdateDynamicUniformBuffer(const void *ub, size_t size) override;
|
2017-02-08 12:26:48 +01:00
|
|
|
|
2014-08-17 12:16:57 +02:00
|
|
|
// TODO: Add more sophisticated draws.
|
2017-01-17 23:14:47 +07:00
|
|
|
void Draw(int vertexCount, int offset) override;
|
|
|
|
void DrawIndexed(int vertexCount, int offset) override;
|
2016-12-26 17:03:01 +01:00
|
|
|
void DrawUP(const void *vdata, int vertexCount) override;
|
2017-01-17 23:14:47 +07:00
|
|
|
|
2014-08-17 12:16:57 +02:00
|
|
|
void Clear(int mask, uint32_t colorval, float depthVal, int stencilVal) override;
|
2014-08-22 19:55:39 +02:00
|
|
|
|
2016-12-25 18:52:05 +01:00
|
|
|
std::string GetInfoString(InfoField info) const override {
|
2014-08-22 20:47:40 +02:00
|
|
|
// TODO: Make these actually query the right information
|
|
|
|
switch (info) {
|
|
|
|
case APINAME:
|
2015-08-23 21:34:11 -07:00
|
|
|
if (gl_extensions.IsGLES) {
|
|
|
|
return "OpenGL ES";
|
|
|
|
} else {
|
|
|
|
return "OpenGL";
|
|
|
|
}
|
2015-07-21 20:46:31 +02:00
|
|
|
case VENDORSTRING: return (const char *)glGetString(GL_VENDOR);
|
|
|
|
case VENDOR:
|
2017-11-21 15:42:01 +01:00
|
|
|
switch (caps_.vendor) {
|
2017-11-22 11:01:42 +01:00
|
|
|
case GPUVendor::VENDOR_AMD: return "VENDOR_AMD";
|
|
|
|
case GPUVendor::VENDOR_IMGTEC: return "VENDOR_POWERVR";
|
|
|
|
case GPUVendor::VENDOR_NVIDIA: return "VENDOR_NVIDIA";
|
|
|
|
case GPUVendor::VENDOR_INTEL: return "VENDOR_INTEL";
|
|
|
|
case GPUVendor::VENDOR_QUALCOMM: return "VENDOR_ADRENO";
|
|
|
|
case GPUVendor::VENDOR_ARM: return "VENDOR_ARM";
|
|
|
|
case GPUVendor::VENDOR_BROADCOM: return "VENDOR_BROADCOM";
|
|
|
|
case GPUVendor::VENDOR_UNKNOWN:
|
2015-07-21 20:46:31 +02:00
|
|
|
default:
|
|
|
|
return "VENDOR_UNKNOWN";
|
|
|
|
}
|
|
|
|
break;
|
2017-03-16 09:48:10 +01:00
|
|
|
case DRIVER: return (const char *)glGetString(GL_RENDERER);
|
2014-08-22 20:47:40 +02:00
|
|
|
case SHADELANGVERSION: return (const char *)glGetString(GL_SHADING_LANGUAGE_VERSION);
|
|
|
|
case APIVERSION: return (const char *)glGetString(GL_VERSION);
|
|
|
|
default: return "?";
|
|
|
|
}
|
2014-08-22 19:55:39 +02:00
|
|
|
}
|
2016-02-13 13:37:00 -08:00
|
|
|
|
2017-08-19 17:32:10 +02:00
|
|
|
uintptr_t GetNativeObject(NativeObject obj) override {
|
2017-11-18 22:15:12 +01:00
|
|
|
switch (obj) {
|
|
|
|
case NativeObject::RENDER_MANAGER:
|
|
|
|
return (uintptr_t)&renderManager_;
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
2017-02-05 20:05:03 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 20:30:39 +01:00
|
|
|
void HandleEvent(Event ev, int width, int height, void *param1, void *param2) override {}
|
2017-02-06 11:20:27 +01:00
|
|
|
|
2017-02-19 11:22:23 +01:00
|
|
|
private:
|
2017-02-04 18:46:12 +01:00
|
|
|
OpenGLFramebuffer *fbo_ext_create(const FramebufferDesc &desc);
|
|
|
|
void fbo_bind_fb_target(bool read, GLuint name);
|
|
|
|
GLenum fbo_get_fb_target(bool read, GLuint **cached);
|
|
|
|
void fbo_unbind();
|
2017-02-19 11:22:23 +01:00
|
|
|
void ApplySamplers();
|
2017-02-04 18:46:12 +01:00
|
|
|
|
2017-11-18 15:42:39 +01:00
|
|
|
GLRenderManager renderManager_;
|
|
|
|
|
2017-02-19 11:22:23 +01:00
|
|
|
std::vector<OpenGLSamplerState *> boundSamplers_;
|
|
|
|
OpenGLTexture *boundTextures_[8]{};
|
|
|
|
int maxTextures_ = 0;
|
2017-02-20 11:21:07 +01:00
|
|
|
DeviceCaps caps_{};
|
2017-01-17 23:14:47 +07:00
|
|
|
|
|
|
|
// Bound state
|
2017-02-20 11:21:07 +01:00
|
|
|
OpenGLPipeline *curPipeline_ = nullptr;
|
|
|
|
OpenGLBuffer *curVBuffers_[4]{};
|
|
|
|
int curVBufferOffsets_[4]{};
|
|
|
|
OpenGLBuffer *curIBuffer_ = nullptr;
|
|
|
|
int curIBufferOffset_ = 0;
|
2017-11-21 16:45:29 +01:00
|
|
|
OpenGLFramebuffer *curFB_;
|
2017-02-04 18:46:12 +01:00
|
|
|
|
|
|
|
// Framebuffer state
|
|
|
|
GLuint currentDrawHandle_ = 0;
|
|
|
|
GLuint currentReadHandle_ = 0;
|
2017-11-19 00:23:52 +01:00
|
|
|
|
|
|
|
// Frames in flight is not such a strict concept as with Vulkan until we start using glBufferStorage and fences.
|
|
|
|
// But might as well have the structure ready, and can't hurt to rotate buffers.
|
|
|
|
struct FrameData {
|
|
|
|
GLPushBuffer *push;
|
|
|
|
};
|
|
|
|
FrameData frameData_[GLRenderManager::MAX_INFLIGHT_FRAMES];
|
2014-08-17 12:16:57 +02:00
|
|
|
};
|
|
|
|
|
2016-12-25 21:21:56 +01:00
|
|
|
OpenGLContext::OpenGLContext() {
|
2017-02-06 11:35:55 +01:00
|
|
|
// TODO: Detect more caps
|
|
|
|
if (gl_extensions.IsGLES) {
|
|
|
|
if (gl_extensions.OES_packed_depth_stencil || gl_extensions.OES_depth24) {
|
|
|
|
caps_.preferredDepthBufferFormat = DataFormat::D24_S8;
|
|
|
|
} else {
|
|
|
|
caps_.preferredDepthBufferFormat = DataFormat::D16;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
caps_.preferredDepthBufferFormat = DataFormat::D24_S8;
|
|
|
|
}
|
2017-02-12 18:29:58 +01:00
|
|
|
caps_.framebufferBlitSupported = gl_extensions.NV_framebuffer_blit || gl_extensions.ARB_framebuffer_object;
|
2017-10-25 21:56:39 +02:00
|
|
|
caps_.framebufferDepthBlitSupported = caps_.framebufferBlitSupported;
|
2017-11-21 15:42:01 +01:00
|
|
|
|
|
|
|
switch (gl_extensions.gpuVendor) {
|
2017-11-22 11:01:42 +01:00
|
|
|
case GPU_VENDOR_AMD: caps_.vendor = GPUVendor::VENDOR_AMD; break;
|
|
|
|
case GPU_VENDOR_NVIDIA: caps_.vendor = GPUVendor::VENDOR_NVIDIA; break;
|
|
|
|
case GPU_VENDOR_ARM: caps_.vendor = GPUVendor::VENDOR_ARM; break;
|
|
|
|
case GPU_VENDOR_QUALCOMM: caps_.vendor = GPUVendor::VENDOR_QUALCOMM; break;
|
|
|
|
case GPU_VENDOR_BROADCOM: caps_.vendor = GPUVendor::VENDOR_BROADCOM; break;
|
|
|
|
case GPU_VENDOR_INTEL: caps_.vendor = GPUVendor::VENDOR_INTEL; break;
|
|
|
|
case GPU_VENDOR_IMGTEC: caps_.vendor = GPUVendor::VENDOR_IMGTEC; break;
|
2017-11-21 15:42:01 +01:00
|
|
|
case GPU_VENDOR_UNKNOWN:
|
|
|
|
default:
|
2017-11-22 11:01:42 +01:00
|
|
|
caps_.vendor = GPUVendor::VENDOR_UNKNOWN;
|
2017-11-21 15:42:01 +01:00
|
|
|
break;
|
2017-11-19 00:23:52 +01:00
|
|
|
}
|
|
|
|
for (int i = 0; i < GLRenderManager::MAX_INFLIGHT_FRAMES; i++) {
|
2017-11-19 18:22:46 +01:00
|
|
|
frameData_[i].push = new GLPushBuffer(&renderManager_, GL_ARRAY_BUFFER, 64 * 1024);
|
2017-11-21 15:42:01 +01:00
|
|
|
}
|
2014-08-17 12:16:57 +02:00
|
|
|
}
|
|
|
|
|
2016-12-25 21:21:56 +01:00
|
|
|
OpenGLContext::~OpenGLContext() {
|
2017-11-19 00:23:52 +01:00
|
|
|
for (int i = 0; i < GLRenderManager::MAX_INFLIGHT_FRAMES; i++) {
|
|
|
|
frameData_[i].push->Destroy();
|
|
|
|
delete frameData_[i].push;
|
|
|
|
}
|
2017-02-19 11:22:23 +01:00
|
|
|
boundSamplers_.clear();
|
2014-08-17 12:16:57 +02:00
|
|
|
}
|
|
|
|
|
2017-11-18 21:12:11 +01:00
|
|
|
void OpenGLContext::BeginFrame() {
|
|
|
|
renderManager_.BeginFrame();
|
2017-11-19 00:23:52 +01:00
|
|
|
FrameData &frameData = frameData_[renderManager_.GetCurFrame()];
|
|
|
|
frameData.push->Begin();
|
2017-11-18 21:12:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void OpenGLContext::EndFrame() {
|
2017-11-19 00:23:52 +01:00
|
|
|
FrameData &frameData = frameData_[renderManager_.GetCurFrame()];
|
|
|
|
frameData.push->End(); // upload the data!
|
2017-11-18 21:12:11 +01:00
|
|
|
renderManager_.Finish();
|
|
|
|
}
|
|
|
|
|
2016-12-26 13:22:48 +01:00
|
|
|
InputLayout *OpenGLContext::CreateInputLayout(const InputLayoutDesc &desc) {
|
2017-11-18 21:12:11 +01:00
|
|
|
OpenGLInputLayout *fmt = new OpenGLInputLayout(&renderManager_);
|
|
|
|
fmt->Compile(desc);
|
2014-08-17 12:16:57 +02:00
|
|
|
return fmt;
|
|
|
|
}
|
|
|
|
|
2016-12-25 18:52:05 +01:00
|
|
|
GLuint TypeToTarget(TextureType type) {
|
2014-08-17 15:02:43 +02:00
|
|
|
switch (type) {
|
2014-08-23 00:34:55 -07:00
|
|
|
#ifndef USING_GLES2
|
2017-01-16 23:43:07 +07:00
|
|
|
case TextureType::LINEAR1D: return GL_TEXTURE_1D;
|
2014-08-23 00:34:55 -07:00
|
|
|
#endif
|
2017-01-16 23:43:07 +07:00
|
|
|
case TextureType::LINEAR2D: return GL_TEXTURE_2D;
|
|
|
|
case TextureType::LINEAR3D: return GL_TEXTURE_3D;
|
|
|
|
case TextureType::CUBE: return GL_TEXTURE_CUBE_MAP;
|
2014-08-23 00:34:55 -07:00
|
|
|
#ifndef USING_GLES2
|
2017-01-16 23:43:07 +07:00
|
|
|
case TextureType::ARRAY1D: return GL_TEXTURE_1D_ARRAY;
|
2014-08-23 00:34:55 -07:00
|
|
|
#endif
|
2017-01-16 23:43:07 +07:00
|
|
|
case TextureType::ARRAY2D: return GL_TEXTURE_2D_ARRAY;
|
2017-02-19 11:22:23 +01:00
|
|
|
default:
|
|
|
|
ELOG("Bad texture type %d", (int)type);
|
|
|
|
return GL_NONE;
|
2014-08-17 15:02:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-18 00:31:44 +07:00
|
|
|
inline bool isPowerOf2(int n) {
|
|
|
|
return n == 1 || (n & (n - 1)) == 0;
|
|
|
|
}
|
|
|
|
|
2017-01-26 11:57:48 +01:00
|
|
|
class OpenGLTexture : public Texture {
|
2014-08-17 15:02:43 +02:00
|
|
|
public:
|
2017-11-18 20:17:17 +01:00
|
|
|
OpenGLTexture(GLRenderManager *render, const TextureDesc &desc);
|
2017-02-19 11:02:24 +01:00
|
|
|
~OpenGLTexture();
|
2017-01-16 23:43:07 +07:00
|
|
|
|
2017-02-19 11:22:23 +01:00
|
|
|
bool HasMips() const {
|
2016-02-13 13:37:00 -08:00
|
|
|
return mipLevels_ > 1 || generatedMips_;
|
|
|
|
}
|
2017-02-19 11:22:23 +01:00
|
|
|
bool CanWrap() const {
|
2016-02-13 13:37:00 -08:00
|
|
|
return canWrap_;
|
|
|
|
}
|
2017-02-19 11:22:23 +01:00
|
|
|
TextureType GetType() const { return type_; }
|
2017-11-18 20:17:17 +01:00
|
|
|
void Bind(int stage) {
|
|
|
|
render_->BindTexture(stage, tex_);
|
2014-08-17 21:28:34 +02:00
|
|
|
}
|
2014-09-06 13:49:01 +02:00
|
|
|
|
2017-02-19 11:22:23 +01:00
|
|
|
void AutoGenMipmaps();
|
|
|
|
|
2014-08-17 15:02:43 +02:00
|
|
|
private:
|
2017-02-19 11:02:24 +01:00
|
|
|
void SetImageData(int x, int y, int z, int width, int height, int depth, int level, int stride, const uint8_t *data);
|
|
|
|
|
2017-11-18 20:17:17 +01:00
|
|
|
GLRenderManager *render_;
|
|
|
|
GLRTexture *tex_;
|
2014-08-17 21:28:34 +02:00
|
|
|
|
2016-12-25 18:52:05 +01:00
|
|
|
DataFormat format_;
|
2017-02-19 11:22:23 +01:00
|
|
|
TextureType type_;
|
2014-08-17 21:28:34 +02:00
|
|
|
int mipLevels_;
|
2016-01-23 11:22:40 -08:00
|
|
|
bool generatedMips_;
|
2016-02-13 13:37:00 -08:00
|
|
|
bool canWrap_;
|
2014-08-17 15:02:43 +02:00
|
|
|
};
|
|
|
|
|
2017-11-18 20:17:17 +01:00
|
|
|
OpenGLTexture::OpenGLTexture(GLRenderManager *render, const TextureDesc &desc) : render_(render) {
|
2017-02-19 11:02:24 +01:00
|
|
|
generatedMips_ = false;
|
|
|
|
canWrap_ = true;
|
|
|
|
width_ = desc.width;
|
|
|
|
height_ = desc.height;
|
|
|
|
depth_ = desc.depth;
|
2017-02-19 11:22:23 +01:00
|
|
|
format_ = desc.format;
|
|
|
|
type_ = desc.type;
|
2017-11-18 20:17:17 +01:00
|
|
|
GLenum target = TypeToTarget(desc.type);
|
2017-11-19 17:37:56 +01:00
|
|
|
tex_ = render->CreateTexture(target);
|
2017-11-18 20:17:17 +01:00
|
|
|
|
2017-02-19 11:22:23 +01:00
|
|
|
canWrap_ = isPowerOf2(width_) && isPowerOf2(height_);
|
2017-06-05 17:32:49 +02:00
|
|
|
mipLevels_ = desc.mipLevels;
|
2017-02-19 11:02:24 +01:00
|
|
|
if (!desc.initData.size())
|
|
|
|
return;
|
|
|
|
|
|
|
|
int level = 0;
|
|
|
|
for (auto data : desc.initData) {
|
|
|
|
SetImageData(0, 0, 0, width_, height_, depth_, level, 0, data);
|
|
|
|
width_ = (width_ + 1) / 2;
|
|
|
|
height_ = (height_ + 1) / 2;
|
|
|
|
level++;
|
|
|
|
}
|
2017-03-11 14:43:42 +01:00
|
|
|
mipLevels_ = desc.generateMips ? desc.mipLevels : level;
|
2017-02-19 11:02:24 +01:00
|
|
|
|
2017-11-18 20:17:17 +01:00
|
|
|
/*
|
2017-02-19 11:09:42 +01:00
|
|
|
#ifdef USING_GLES2
|
|
|
|
if (gl_extensions.GLES3) {
|
2017-03-11 14:43:42 +01:00
|
|
|
glTexParameteri(target_, GL_TEXTURE_MAX_LEVEL, mipLevels_ - 1);
|
2017-02-19 11:09:42 +01:00
|
|
|
}
|
|
|
|
#else
|
2017-03-11 14:43:42 +01:00
|
|
|
glTexParameteri(target_, GL_TEXTURE_MAX_LEVEL, mipLevels_ - 1);
|
2017-02-19 11:09:42 +01:00
|
|
|
#endif
|
2017-11-18 20:17:17 +01:00
|
|
|
*/
|
2017-02-19 11:02:24 +01:00
|
|
|
|
2017-03-11 14:43:42 +01:00
|
|
|
if ((int)desc.initData.size() < desc.mipLevels && desc.generateMips) {
|
2017-02-19 11:22:23 +01:00
|
|
|
ILOG("Generating mipmaps");
|
2017-02-19 11:02:24 +01:00
|
|
|
AutoGenMipmaps();
|
2017-02-19 11:22:23 +01:00
|
|
|
}
|
|
|
|
|
2017-02-19 11:02:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
OpenGLTexture::~OpenGLTexture() {
|
|
|
|
if (tex_) {
|
2017-11-18 20:17:17 +01:00
|
|
|
render_->DeleteTexture(tex_);
|
2017-02-19 11:02:24 +01:00
|
|
|
tex_ = 0;
|
|
|
|
generatedMips_ = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-16 23:34:53 +07:00
|
|
|
void OpenGLTexture::AutoGenMipmaps() {
|
2016-01-23 11:22:40 -08:00
|
|
|
if (!generatedMips_) {
|
2017-11-18 21:51:17 +01:00
|
|
|
// Assumes the texture is bound for editing
|
2017-11-18 20:17:17 +01:00
|
|
|
render_->GenerateMipmap();
|
2016-01-23 11:22:40 -08:00
|
|
|
generatedMips_ = true;
|
|
|
|
}
|
2014-08-17 15:02:43 +02:00
|
|
|
}
|
|
|
|
|
2017-12-07 14:56:19 +01:00
|
|
|
class OpenGLFramebuffer : public Framebuffer {
|
2017-10-10 14:46:47 +02:00
|
|
|
public:
|
2017-12-07 14:56:19 +01:00
|
|
|
OpenGLFramebuffer() {}
|
2017-10-10 14:46:47 +02:00
|
|
|
~OpenGLFramebuffer();
|
|
|
|
|
2017-11-18 20:17:17 +01:00
|
|
|
GLRFramebuffer *framebuffer;
|
|
|
|
|
2017-10-10 14:46:47 +02:00
|
|
|
GLuint handle = 0;
|
|
|
|
GLuint color_texture = 0;
|
|
|
|
GLuint z_stencil_buffer = 0; // Either this is set, or the two below.
|
|
|
|
GLuint z_buffer = 0;
|
|
|
|
GLuint stencil_buffer = 0;
|
|
|
|
|
|
|
|
int width;
|
|
|
|
int height;
|
|
|
|
FBColorDepth colorDepth;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2017-10-10 14:26:35 +02:00
|
|
|
// TODO: Also output storage format (GL_RGBA8 etc) for modern GL usage.
|
|
|
|
static bool Thin3DFormatToFormatAndType(DataFormat fmt, GLuint &internalFormat, GLuint &format, GLuint &type, int &alignment) {
|
|
|
|
alignment = 4;
|
2017-10-09 15:51:43 +02:00
|
|
|
switch (fmt) {
|
2016-12-25 21:21:56 +01:00
|
|
|
case DataFormat::R8G8B8A8_UNORM:
|
2014-08-17 15:02:43 +02:00
|
|
|
internalFormat = GL_RGBA;
|
|
|
|
format = GL_RGBA;
|
|
|
|
type = GL_UNSIGNED_BYTE;
|
2017-10-10 14:26:35 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DataFormat::D32F:
|
|
|
|
internalFormat = GL_DEPTH_COMPONENT;
|
|
|
|
format = GL_DEPTH_COMPONENT;
|
|
|
|
type = GL_FLOAT;
|
|
|
|
break;
|
|
|
|
|
2017-10-16 14:34:11 +02:00
|
|
|
#ifndef USING_GLES2
|
2017-10-10 14:26:35 +02:00
|
|
|
case DataFormat::S8:
|
|
|
|
internalFormat = GL_STENCIL_INDEX;
|
|
|
|
format = GL_STENCIL_INDEX;
|
|
|
|
type = GL_UNSIGNED_BYTE;
|
|
|
|
alignment = 1;
|
|
|
|
break;
|
2017-10-16 14:34:11 +02:00
|
|
|
#endif
|
2017-10-09 15:58:03 +02:00
|
|
|
|
|
|
|
case DataFormat::R8G8B8_UNORM:
|
|
|
|
internalFormat = GL_RGB;
|
|
|
|
format = GL_RGB;
|
|
|
|
type = GL_UNSIGNED_BYTE;
|
2017-10-10 14:26:35 +02:00
|
|
|
alignment = 1;
|
|
|
|
break;
|
2017-10-09 15:58:03 +02:00
|
|
|
|
2017-02-19 11:09:42 +01:00
|
|
|
case DataFormat::B4G4R4A4_UNORM_PACK16:
|
2014-08-22 20:47:40 +02:00
|
|
|
internalFormat = GL_RGBA;
|
|
|
|
format = GL_RGBA;
|
|
|
|
type = GL_UNSIGNED_SHORT_4_4_4_4;
|
2017-10-10 14:26:35 +02:00
|
|
|
alignment = 2;
|
|
|
|
break;
|
2017-10-09 15:58:03 +02:00
|
|
|
|
|
|
|
case DataFormat::B5G6R5_UNORM_PACK16:
|
|
|
|
internalFormat = GL_RGB;
|
|
|
|
format = GL_RGB;
|
|
|
|
type = GL_UNSIGNED_SHORT_5_6_5;
|
2017-10-10 14:26:35 +02:00
|
|
|
alignment = 2;
|
|
|
|
break;
|
2017-10-09 15:58:03 +02:00
|
|
|
|
|
|
|
case DataFormat::B5G5R5A1_UNORM_PACK16:
|
|
|
|
internalFormat = GL_RGBA;
|
|
|
|
format = GL_RGBA;
|
|
|
|
type = GL_UNSIGNED_SHORT_5_5_5_1;
|
2017-10-10 14:26:35 +02:00
|
|
|
alignment = 2;
|
|
|
|
break;
|
2017-10-09 15:58:03 +02:00
|
|
|
|
2017-01-29 14:19:58 +01:00
|
|
|
#ifndef USING_GLES2
|
2017-02-22 16:23:04 +01:00
|
|
|
case DataFormat::A4R4G4B4_UNORM_PACK16:
|
2017-01-21 13:11:03 +01:00
|
|
|
internalFormat = GL_RGBA;
|
|
|
|
format = GL_RGBA;
|
|
|
|
type = GL_UNSIGNED_SHORT_4_4_4_4_REV;
|
2017-10-10 14:26:35 +02:00
|
|
|
alignment = 2;
|
|
|
|
break;
|
2017-10-09 15:58:03 +02:00
|
|
|
|
|
|
|
case DataFormat::R5G6B5_UNORM_PACK16:
|
|
|
|
internalFormat = GL_RGB;
|
|
|
|
format = GL_RGB;
|
|
|
|
type = GL_UNSIGNED_SHORT_5_6_5_REV;
|
2017-10-10 14:26:35 +02:00
|
|
|
alignment = 2;
|
|
|
|
break;
|
2017-10-09 15:58:03 +02:00
|
|
|
|
|
|
|
case DataFormat::A1R5G5B5_UNORM_PACK16:
|
|
|
|
internalFormat = GL_RGBA;
|
|
|
|
format = GL_RGBA;
|
|
|
|
type = GL_UNSIGNED_SHORT_1_5_5_5_REV;
|
2017-10-10 14:26:35 +02:00
|
|
|
alignment = 2;
|
|
|
|
break;
|
2017-01-21 13:11:03 +01:00
|
|
|
#endif
|
2017-10-09 15:58:03 +02:00
|
|
|
|
2014-08-17 15:02:43 +02:00
|
|
|
default:
|
2017-10-09 15:51:43 +02:00
|
|
|
ELOG("Thin3d GL: Unsupported texture format %d", (int)fmt);
|
|
|
|
return false;
|
|
|
|
}
|
2017-10-10 14:26:35 +02:00
|
|
|
return true;
|
2017-10-09 15:51:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void OpenGLTexture::SetImageData(int x, int y, int z, int width, int height, int depth, int level, int stride, const uint8_t *data) {
|
|
|
|
if (width != width_ || height != height_ || depth != depth_) {
|
|
|
|
// When switching to texStorage we need to handle this correctly.
|
|
|
|
width_ = width;
|
|
|
|
height_ = height;
|
|
|
|
depth_ = depth;
|
|
|
|
}
|
|
|
|
|
|
|
|
GLuint internalFormat;
|
|
|
|
GLuint format;
|
|
|
|
GLuint type;
|
2017-10-10 14:26:35 +02:00
|
|
|
int alignment;
|
|
|
|
if (!Thin3DFormatToFormatAndType(format_, internalFormat, format, type, alignment)) {
|
2014-08-17 15:02:43 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-18 22:15:12 +01:00
|
|
|
if (stride == 0)
|
|
|
|
stride = width;
|
|
|
|
|
2017-11-18 20:17:17 +01:00
|
|
|
// Make a copy of data with stride eliminated.
|
|
|
|
uint8_t *texData = new uint8_t[width * height * alignment];
|
|
|
|
for (int y = 0; y < height; y++) {
|
|
|
|
memcpy(texData + y * width * alignment, data + y * stride * alignment, width * alignment);
|
2014-08-17 15:02:43 +02:00
|
|
|
}
|
2017-11-18 20:17:17 +01:00
|
|
|
render_->TextureImage(tex_, level, width, height, internalFormat, format, type, texData);
|
2017-01-29 14:19:58 +01:00
|
|
|
}
|
2014-08-17 21:28:34 +02:00
|
|
|
|
2017-10-16 14:32:26 +02:00
|
|
|
#ifdef DEBUG_READ_PIXELS
|
|
|
|
// TODO: Make more generic.
|
|
|
|
static void LogReadPixelsError(GLenum error) {
|
|
|
|
switch (error) {
|
|
|
|
case GL_NO_ERROR:
|
|
|
|
break;
|
|
|
|
case GL_INVALID_ENUM:
|
|
|
|
ERROR_LOG(FRAMEBUF, "glReadPixels: GL_INVALID_ENUM");
|
|
|
|
break;
|
|
|
|
case GL_INVALID_VALUE:
|
|
|
|
ERROR_LOG(FRAMEBUF, "glReadPixels: GL_INVALID_VALUE");
|
|
|
|
break;
|
|
|
|
case GL_INVALID_OPERATION:
|
|
|
|
ERROR_LOG(FRAMEBUF, "glReadPixels: GL_INVALID_OPERATION");
|
|
|
|
break;
|
|
|
|
case GL_INVALID_FRAMEBUFFER_OPERATION:
|
|
|
|
ERROR_LOG(FRAMEBUF, "glReadPixels: GL_INVALID_FRAMEBUFFER_OPERATION");
|
|
|
|
break;
|
|
|
|
case GL_OUT_OF_MEMORY:
|
|
|
|
ERROR_LOG(FRAMEBUF, "glReadPixels: GL_OUT_OF_MEMORY");
|
|
|
|
break;
|
|
|
|
#ifndef USING_GLES2
|
|
|
|
case GL_STACK_UNDERFLOW:
|
|
|
|
ERROR_LOG(FRAMEBUF, "glReadPixels: GL_STACK_UNDERFLOW");
|
|
|
|
break;
|
|
|
|
case GL_STACK_OVERFLOW:
|
|
|
|
ERROR_LOG(FRAMEBUF, "glReadPixels: GL_STACK_OVERFLOW");
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
ERROR_LOG(FRAMEBUF, "glReadPixels: %08x", error);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-10-10 16:51:44 +02:00
|
|
|
bool OpenGLContext::CopyFramebufferToMemorySync(Framebuffer *src, int channelBits, int x, int y, int w, int h, Draw::DataFormat dataFormat, void *pixels, int pixelStride) {
|
2017-10-10 14:46:47 +02:00
|
|
|
OpenGLFramebuffer *fb = (OpenGLFramebuffer *)src;
|
|
|
|
fbo_bind_fb_target(true, fb ? fb->handle : 0);
|
|
|
|
|
2017-10-10 14:26:35 +02:00
|
|
|
// Reads from the "bound for read" framebuffer.
|
|
|
|
if (gl_extensions.GLES3 || !gl_extensions.IsGLES)
|
|
|
|
glReadBuffer(GL_COLOR_ATTACHMENT0);
|
|
|
|
|
|
|
|
CHECK_GL_ERROR_IF_DEBUG();
|
|
|
|
|
|
|
|
GLuint internalFormat;
|
|
|
|
GLuint format;
|
|
|
|
GLuint type;
|
|
|
|
int alignment;
|
|
|
|
if (!Thin3DFormatToFormatAndType(dataFormat, internalFormat, format, type, alignment)) {
|
|
|
|
assert(false);
|
|
|
|
}
|
|
|
|
// Apply the correct alignment.
|
|
|
|
glPixelStorei(GL_PACK_ALIGNMENT, alignment);
|
2017-12-21 11:41:53 -08:00
|
|
|
if (!gl_extensions.IsGLES || gl_extensions.GLES3) {
|
|
|
|
// Even if not required, some drivers seem to require we specify this. See #8254.
|
2017-10-10 16:51:44 +02:00
|
|
|
glPixelStorei(GL_PACK_ROW_LENGTH, pixelStride);
|
2017-10-10 14:26:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
glReadPixels(x, y, w, h, format, type, pixels);
|
|
|
|
#ifdef DEBUG_READ_PIXELS
|
|
|
|
LogReadPixelsError(glGetError());
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (!gl_extensions.IsGLES || gl_extensions.GLES3) {
|
|
|
|
glPixelStorei(GL_PACK_ROW_LENGTH, 0);
|
|
|
|
}
|
|
|
|
CHECK_GL_ERROR_IF_DEBUG();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-02-19 11:22:23 +01:00
|
|
|
Texture *OpenGLContext::CreateTexture(const TextureDesc &desc) {
|
2017-11-18 20:17:17 +01:00
|
|
|
return new OpenGLTexture(&renderManager_, desc);
|
2017-02-19 11:22:23 +01:00
|
|
|
}
|
|
|
|
|
2016-12-25 21:21:56 +01:00
|
|
|
DepthStencilState *OpenGLContext::CreateDepthStencilState(const DepthStencilStateDesc &desc) {
|
|
|
|
OpenGLDepthStencilState *ds = new OpenGLDepthStencilState();
|
2016-12-25 21:10:46 +01:00
|
|
|
ds->depthTestEnabled = desc.depthTestEnabled;
|
|
|
|
ds->depthWriteEnabled = desc.depthWriteEnabled;
|
|
|
|
ds->depthComp = compToGL[(int)desc.depthCompare];
|
2016-12-26 23:11:31 +01:00
|
|
|
ds->stencilEnabled = desc.stencilEnabled;
|
|
|
|
ds->stencilCompareOp = compToGL[(int)desc.front.compareOp];
|
|
|
|
ds->stencilPass = stencilOpToGL[(int)desc.front.passOp];
|
|
|
|
ds->stencilFail = stencilOpToGL[(int)desc.front.failOp];
|
|
|
|
ds->stencilZFail = stencilOpToGL[(int)desc.front.depthFailOp];
|
|
|
|
ds->stencilWriteMask = desc.front.writeMask;
|
|
|
|
ds->stencilReference = desc.front.reference;
|
|
|
|
ds->stencilCompareMask = desc.front.compareMask;
|
2014-08-17 12:16:57 +02:00
|
|
|
return ds;
|
|
|
|
}
|
|
|
|
|
2016-12-25 21:21:56 +01:00
|
|
|
BlendState *OpenGLContext::CreateBlendState(const BlendStateDesc &desc) {
|
|
|
|
OpenGLBlendState *bs = new OpenGLBlendState();
|
2014-08-17 12:16:57 +02:00
|
|
|
bs->enabled = desc.enabled;
|
2016-12-25 21:10:46 +01:00
|
|
|
bs->eqCol = blendEqToGL[(int)desc.eqCol];
|
|
|
|
bs->srcCol = blendFactorToGL[(int)desc.srcCol];
|
|
|
|
bs->dstCol = blendFactorToGL[(int)desc.dstCol];
|
|
|
|
bs->eqAlpha = blendEqToGL[(int)desc.eqAlpha];
|
|
|
|
bs->srcAlpha = blendFactorToGL[(int)desc.srcAlpha];
|
|
|
|
bs->dstAlpha = blendFactorToGL[(int)desc.dstAlpha];
|
2016-12-26 18:32:52 +01:00
|
|
|
bs->colorMask = desc.colorMask;
|
2014-08-17 12:16:57 +02:00
|
|
|
return bs;
|
|
|
|
}
|
|
|
|
|
2016-12-25 21:21:56 +01:00
|
|
|
SamplerState *OpenGLContext::CreateSamplerState(const SamplerStateDesc &desc) {
|
|
|
|
OpenGLSamplerState *samps = new OpenGLSamplerState();
|
2017-02-19 11:22:23 +01:00
|
|
|
samps->wrapU = texWrapToGL[(int)desc.wrapU];
|
|
|
|
samps->wrapV = texWrapToGL[(int)desc.wrapV];
|
|
|
|
samps->wrapW = texWrapToGL[(int)desc.wrapW];
|
2016-12-25 21:21:56 +01:00
|
|
|
samps->magFilt = texFilterToGL[(int)desc.magFilter];
|
|
|
|
samps->minFilt = texFilterToGL[(int)desc.minFilter];
|
|
|
|
samps->mipMinFilt = texMipFilterToGL[(int)desc.minFilter][(int)desc.mipFilter];
|
2016-02-13 13:37:00 -08:00
|
|
|
return samps;
|
|
|
|
}
|
|
|
|
|
2016-12-25 22:24:14 +01:00
|
|
|
RasterState *OpenGLContext::CreateRasterState(const RasterStateDesc &desc) {
|
2016-12-25 21:21:56 +01:00
|
|
|
OpenGLRasterState *rs = new OpenGLRasterState();
|
2016-12-25 18:52:05 +01:00
|
|
|
if (desc.cull == CullMode::NONE) {
|
2016-12-23 23:46:11 +01:00
|
|
|
rs->cullEnable = GL_FALSE;
|
|
|
|
return rs;
|
|
|
|
}
|
|
|
|
rs->cullEnable = GL_TRUE;
|
2016-12-27 15:52:03 +01:00
|
|
|
switch (desc.frontFace) {
|
2016-12-25 18:52:05 +01:00
|
|
|
case Facing::CW:
|
2016-12-23 23:46:11 +01:00
|
|
|
rs->frontFace = GL_CW;
|
|
|
|
break;
|
2016-12-25 18:52:05 +01:00
|
|
|
case Facing::CCW:
|
2016-12-23 23:46:11 +01:00
|
|
|
rs->frontFace = GL_CCW;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
switch (desc.cull) {
|
2016-12-25 18:52:05 +01:00
|
|
|
case CullMode::FRONT:
|
2016-12-23 23:46:11 +01:00
|
|
|
rs->cullMode = GL_FRONT;
|
|
|
|
break;
|
2016-12-25 18:52:05 +01:00
|
|
|
case CullMode::BACK:
|
2016-12-23 23:46:11 +01:00
|
|
|
rs->cullMode = GL_BACK;
|
|
|
|
break;
|
2016-12-25 18:52:05 +01:00
|
|
|
case CullMode::FRONT_AND_BACK:
|
2016-12-23 23:46:11 +01:00
|
|
|
rs->cullMode = GL_FRONT_AND_BACK;
|
|
|
|
break;
|
2017-01-26 11:57:48 +01:00
|
|
|
case CullMode::NONE:
|
|
|
|
// Unsupported
|
|
|
|
break;
|
2016-12-23 23:46:11 +01:00
|
|
|
}
|
|
|
|
return rs;
|
|
|
|
}
|
|
|
|
|
2017-12-07 14:56:19 +01:00
|
|
|
class OpenGLBuffer : public Buffer {
|
2017-02-07 19:04:44 +01:00
|
|
|
public:
|
2017-11-18 20:17:17 +01:00
|
|
|
OpenGLBuffer(GLRenderManager *render, size_t size, uint32_t flags) : render_(render) {
|
2017-02-07 19:04:44 +01:00
|
|
|
target_ = (flags & BufferUsageFlag::INDEXDATA) ? GL_ELEMENT_ARRAY_BUFFER : GL_ARRAY_BUFFER;
|
|
|
|
usage_ = 0;
|
|
|
|
if (flags & BufferUsageFlag::DYNAMIC)
|
|
|
|
usage_ = GL_STREAM_DRAW;
|
|
|
|
else
|
|
|
|
usage_ = GL_STATIC_DRAW;
|
2017-11-18 20:17:17 +01:00
|
|
|
buffer_ = render->CreateBuffer(target_, size, usage_);
|
2017-02-07 19:04:44 +01:00
|
|
|
totalSize_ = size;
|
|
|
|
}
|
|
|
|
~OpenGLBuffer() override {
|
2017-11-18 20:17:17 +01:00
|
|
|
render_->DeleteBuffer(buffer_);
|
2017-02-07 19:04:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Bind(int offset) {
|
2017-11-18 20:17:17 +01:00
|
|
|
Crash();
|
|
|
|
// render_->BindBuffer(buffer_);
|
2017-02-07 19:04:44 +01:00
|
|
|
// TODO: Can't support offset using ES 2.0
|
2017-11-18 20:17:17 +01:00
|
|
|
// glBindBuffer(target_, buffer_);
|
2017-02-07 19:04:44 +01:00
|
|
|
}
|
|
|
|
|
2017-11-18 20:17:17 +01:00
|
|
|
GLRenderManager *render_;
|
|
|
|
GLRBuffer *buffer_;
|
2017-02-07 19:04:44 +01:00
|
|
|
GLuint target_;
|
|
|
|
GLuint usage_;
|
|
|
|
|
|
|
|
size_t totalSize_;
|
|
|
|
};
|
|
|
|
|
2016-12-25 21:21:56 +01:00
|
|
|
Buffer *OpenGLContext::CreateBuffer(size_t size, uint32_t usageFlags) {
|
2017-11-18 20:17:17 +01:00
|
|
|
return new OpenGLBuffer(&renderManager_, size, usageFlags);
|
2014-08-17 12:16:57 +02:00
|
|
|
}
|
|
|
|
|
2017-02-07 19:41:58 +01:00
|
|
|
void OpenGLContext::UpdateBuffer(Buffer *buffer, const uint8_t *data, size_t offset, size_t size, UpdateBufferFlags flags) {
|
2017-02-07 19:04:44 +01:00
|
|
|
OpenGLBuffer *buf = (OpenGLBuffer *)buffer;
|
|
|
|
|
|
|
|
buf->Bind(0);
|
|
|
|
if (size + offset > buf->totalSize_) {
|
|
|
|
Crash();
|
|
|
|
}
|
2017-11-18 20:17:17 +01:00
|
|
|
|
|
|
|
uint8_t *dataCopy = new uint8_t[size];
|
|
|
|
memcpy(dataCopy, data, size);
|
2017-02-07 19:41:58 +01:00
|
|
|
// if (flags & UPDATE_DISCARD) we could try to orphan the buffer using glBufferData.
|
2017-11-18 20:17:17 +01:00
|
|
|
renderManager_.BufferSubdata(buf->buffer_, offset, size, dataCopy);
|
2017-02-07 18:16:52 +01:00
|
|
|
}
|
|
|
|
|
2016-12-26 13:42:53 +01:00
|
|
|
Pipeline *OpenGLContext::CreateGraphicsPipeline(const PipelineDesc &desc) {
|
2016-12-25 23:03:20 +01:00
|
|
|
if (!desc.shaders.size()) {
|
2016-12-26 13:42:53 +01:00
|
|
|
ELOG("Pipeline requires at least one shader");
|
2017-11-18 20:17:17 +01:00
|
|
|
return nullptr;
|
2014-08-17 12:16:57 +02:00
|
|
|
}
|
2017-11-18 16:50:49 +01:00
|
|
|
OpenGLPipeline *pipeline = new OpenGLPipeline(&renderManager_);
|
2016-12-25 23:03:20 +01:00
|
|
|
for (auto iter : desc.shaders) {
|
|
|
|
iter->AddRef();
|
2016-12-26 13:42:53 +01:00
|
|
|
pipeline->shaders.push_back(static_cast<OpenGLShaderModule *>(iter));
|
2016-12-25 23:03:20 +01:00
|
|
|
}
|
2016-12-26 17:03:01 +01:00
|
|
|
if (pipeline->LinkShaders()) {
|
|
|
|
// Build the rest of the virtual pipeline object.
|
|
|
|
pipeline->prim = primToGL[(int)desc.prim];
|
|
|
|
pipeline->depthStencil = (OpenGLDepthStencilState *)desc.depthStencil;
|
|
|
|
pipeline->blend = (OpenGLBlendState *)desc.blend;
|
|
|
|
pipeline->raster = (OpenGLRasterState *)desc.raster;
|
|
|
|
pipeline->inputLayout = (OpenGLInputLayout *)desc.inputLayout;
|
|
|
|
pipeline->depthStencil->AddRef();
|
|
|
|
pipeline->blend->AddRef();
|
|
|
|
pipeline->raster->AddRef();
|
|
|
|
pipeline->inputLayout->AddRef();
|
2017-02-08 12:26:48 +01:00
|
|
|
if (desc.uniformDesc)
|
|
|
|
pipeline->dynamicUniforms = *desc.uniformDesc;
|
2016-12-26 13:42:53 +01:00
|
|
|
return pipeline;
|
2014-08-17 12:16:57 +02:00
|
|
|
} else {
|
2017-03-18 15:20:36 +01:00
|
|
|
ELOG("Failed to create pipeline - shaders failed to link");
|
2016-12-26 13:42:53 +01:00
|
|
|
delete pipeline;
|
2014-08-17 12:16:57 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-25 21:21:56 +01:00
|
|
|
void OpenGLContext::BindTextures(int start, int count, Texture **textures) {
|
2017-02-19 11:22:23 +01:00
|
|
|
maxTextures_ = std::max(maxTextures_, start + count);
|
2014-08-17 21:28:34 +02:00
|
|
|
for (int i = start; i < start + count; i++) {
|
2017-01-16 23:34:53 +07:00
|
|
|
OpenGLTexture *glTex = static_cast<OpenGLTexture *>(textures[i]);
|
2017-02-08 15:37:40 +01:00
|
|
|
if (!glTex) {
|
2017-02-19 11:22:23 +01:00
|
|
|
boundTextures_[i] = 0;
|
2017-11-18 20:17:17 +01:00
|
|
|
renderManager_.BindTexture(i, nullptr);
|
2017-02-08 15:37:40 +01:00
|
|
|
continue;
|
|
|
|
}
|
2017-11-18 20:17:17 +01:00
|
|
|
glTex->Bind(i);
|
2017-02-19 11:22:23 +01:00
|
|
|
boundTextures_[i] = glTex;
|
2014-08-17 15:02:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-19 11:22:23 +01:00
|
|
|
void OpenGLContext::ApplySamplers() {
|
|
|
|
for (int i = 0; i < maxTextures_; i++) {
|
|
|
|
if ((int)boundSamplers_.size() > i && boundSamplers_[i]) {
|
|
|
|
const OpenGLSamplerState *samp = boundSamplers_[i];
|
|
|
|
const OpenGLTexture *tex = boundTextures_[i];
|
|
|
|
if (!tex)
|
|
|
|
continue;
|
2017-11-18 20:17:17 +01:00
|
|
|
GLenum wrapS;
|
|
|
|
GLenum wrapT;
|
2017-02-19 11:22:23 +01:00
|
|
|
if (tex->CanWrap()) {
|
2017-11-18 20:17:17 +01:00
|
|
|
wrapS = samp->wrapU;
|
|
|
|
wrapT = samp->wrapV;
|
2017-02-19 11:22:23 +01:00
|
|
|
} else {
|
2017-11-18 20:17:17 +01:00
|
|
|
wrapS = GL_CLAMP_TO_EDGE;
|
|
|
|
wrapT = GL_CLAMP_TO_EDGE;
|
2017-02-19 11:22:23 +01:00
|
|
|
}
|
2017-11-18 20:17:17 +01:00
|
|
|
GLenum magFilt = samp->magFilt;
|
|
|
|
GLenum minFilt = tex->HasMips() ? samp->mipMinFilt : samp->minFilt;
|
2017-11-19 17:37:56 +01:00
|
|
|
renderManager_.SetTextureSampler(wrapS, wrapT, magFilt, minFilt, 0.0f);
|
2017-02-19 11:22:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-08-17 15:02:43 +02:00
|
|
|
|
2016-12-27 17:38:26 +01:00
|
|
|
ShaderModule *OpenGLContext::CreateShaderModule(ShaderStage stage, ShaderLanguage language, const uint8_t *data, size_t dataSize) {
|
2017-11-18 16:50:49 +01:00
|
|
|
OpenGLShaderModule *shader = new OpenGLShaderModule(&renderManager_, stage);
|
|
|
|
if (shader->Compile(&renderManager_, language, data, dataSize)) {
|
2014-08-17 12:16:57 +02:00
|
|
|
return shader;
|
|
|
|
} else {
|
|
|
|
shader->Release();
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-26 17:03:01 +01:00
|
|
|
bool OpenGLPipeline::LinkShaders() {
|
2017-11-18 16:50:49 +01:00
|
|
|
std::vector<GLRShader *> linkShaders;
|
2016-12-25 23:03:20 +01:00
|
|
|
for (auto iter : shaders) {
|
2017-11-18 16:50:49 +01:00
|
|
|
linkShaders.push_back(iter->GetShader());
|
2016-12-25 23:03:20 +01:00
|
|
|
}
|
2017-11-18 16:50:49 +01:00
|
|
|
std::vector<GLRProgram::Semantic> semantics;
|
2014-08-17 12:16:57 +02:00
|
|
|
// Bind all the common vertex data points. Mismatching ones will be ignored.
|
2017-11-18 16:50:49 +01:00
|
|
|
semantics.push_back({ SEM_POSITION, "Position" });
|
|
|
|
semantics.push_back({ SEM_COLOR0, "Color0" });
|
|
|
|
semantics.push_back({ SEM_TEXCOORD0, "TexCoord0" });
|
|
|
|
semantics.push_back({ SEM_NORMAL, "Normal" });
|
|
|
|
semantics.push_back({ SEM_TANGENT, "Tangent" });
|
|
|
|
semantics.push_back({ SEM_BINORMAL, "Binormal" });
|
2017-11-19 00:43:58 +01:00
|
|
|
std::vector<GLRProgram::UniformLocQuery> queries;
|
2017-11-19 12:25:57 +01:00
|
|
|
std::vector<GLRProgram::Initializer> initialize;
|
|
|
|
program_ = render_->CreateProgram(linkShaders, semantics, queries, initialize, false);
|
2014-08-17 12:16:57 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-12-26 17:03:01 +01:00
|
|
|
void OpenGLContext::BindPipeline(Pipeline *pipeline) {
|
|
|
|
curPipeline_ = (OpenGLPipeline *)pipeline;
|
2017-11-18 16:50:49 +01:00
|
|
|
curPipeline_->blend->Apply(&renderManager_);
|
|
|
|
curPipeline_->depthStencil->Apply(&renderManager_);
|
|
|
|
curPipeline_->raster->Apply(&renderManager_);
|
|
|
|
renderManager_.BindProgram(curPipeline_->program_);
|
2016-12-26 17:03:01 +01:00
|
|
|
}
|
|
|
|
|
2017-02-08 12:26:48 +01:00
|
|
|
void OpenGLContext::UpdateDynamicUniformBuffer(const void *ub, size_t size) {
|
|
|
|
if (curPipeline_->dynamicUniforms.uniformBufferSize != size) {
|
|
|
|
Crash();
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto &uniform : curPipeline_->dynamicUniforms.uniforms) {
|
|
|
|
const float *data = (const float *)((uint8_t *)ub + uniform.offset);
|
|
|
|
switch (uniform.type) {
|
|
|
|
case UniformType::FLOAT4:
|
2017-11-18 20:17:17 +01:00
|
|
|
renderManager_.SetUniformF(uniform.name, 4, data);
|
2017-02-08 12:26:48 +01:00
|
|
|
break;
|
|
|
|
case UniformType::MATRIX4X4:
|
2017-11-18 20:17:17 +01:00
|
|
|
renderManager_.SetUniformM4x4(uniform.name, data);
|
2017-02-08 12:26:48 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-17 23:14:47 +07:00
|
|
|
void OpenGLContext::Draw(int vertexCount, int offset) {
|
|
|
|
curVBuffers_[0]->Bind(curVBufferOffsets_[0]);
|
2017-11-19 00:23:52 +01:00
|
|
|
renderManager_.BindInputLayout(curPipeline_->inputLayout->inputLayout_, 0);
|
2017-02-19 11:22:23 +01:00
|
|
|
ApplySamplers();
|
2014-08-17 12:16:57 +02:00
|
|
|
|
2017-11-18 21:12:11 +01:00
|
|
|
renderManager_.Draw(curPipeline_->prim, offset, vertexCount);
|
2014-08-17 12:16:57 +02:00
|
|
|
}
|
|
|
|
|
2017-01-17 23:14:47 +07:00
|
|
|
void OpenGLContext::DrawIndexed(int vertexCount, int offset) {
|
|
|
|
curVBuffers_[0]->Bind(curVBufferOffsets_[0]);
|
2017-11-19 00:23:52 +01:00
|
|
|
renderManager_.BindInputLayout(curPipeline_->inputLayout->inputLayout_, 0);
|
2017-02-19 11:22:23 +01:00
|
|
|
ApplySamplers();
|
2015-12-13 09:25:22 -08:00
|
|
|
// Note: ibuf binding is stored in the VAO, so call this after binding the fmt.
|
2017-01-17 23:14:47 +07:00
|
|
|
curIBuffer_->Bind(curIBufferOffset_);
|
2016-02-13 12:20:22 -08:00
|
|
|
|
2017-11-18 21:51:17 +01:00
|
|
|
renderManager_.DrawIndexed(curPipeline_->prim, vertexCount, GL_UNSIGNED_INT, (void *)(intptr_t)offset);
|
2015-01-29 10:35:34 +01:00
|
|
|
}
|
|
|
|
|
2016-12-26 17:03:01 +01:00
|
|
|
void OpenGLContext::DrawUP(const void *vdata, int vertexCount) {
|
2017-11-19 00:23:52 +01:00
|
|
|
#if 1
|
|
|
|
int stride = curPipeline_->inputLayout->stride;
|
|
|
|
size_t dataSize = stride * vertexCount;
|
|
|
|
|
|
|
|
FrameData &frameData = frameData_[renderManager_.GetCurFrame()];
|
|
|
|
|
|
|
|
GLRBuffer *buf;
|
|
|
|
size_t offset = frameData.push->Push(vdata, dataSize, &buf);
|
|
|
|
|
2017-02-19 11:22:23 +01:00
|
|
|
ApplySamplers();
|
2015-01-29 10:35:34 +01:00
|
|
|
|
2017-11-19 00:23:52 +01:00
|
|
|
renderManager_.BindVertexBuffer(buf);
|
|
|
|
renderManager_.BindInputLayout(curPipeline_->inputLayout->inputLayout_, (void *)offset);
|
|
|
|
|
2017-11-18 21:51:17 +01:00
|
|
|
renderManager_.Draw(curPipeline_->prim, 0, vertexCount);
|
2017-11-19 00:23:52 +01:00
|
|
|
#else
|
|
|
|
ApplySamplers();
|
|
|
|
renderManager_.BindInputLayout(curPipeline_->inputLayout->inputLayout_, (void *)vdata);
|
|
|
|
renderManager_.Draw(curPipeline_->prim, 0, vertexCount);
|
|
|
|
renderManager_.UnbindInputLayout(curPipeline_->inputLayout->inputLayout_);
|
2015-01-29 10:35:34 +01:00
|
|
|
|
2017-11-19 00:23:52 +01:00
|
|
|
#endif
|
2014-08-17 12:16:57 +02:00
|
|
|
}
|
|
|
|
|
2016-12-25 21:21:56 +01:00
|
|
|
void OpenGLContext::Clear(int mask, uint32_t colorval, float depthVal, int stencilVal) {
|
2014-08-17 12:16:57 +02:00
|
|
|
float col[4];
|
2015-10-10 16:41:19 +02:00
|
|
|
Uint8x4ToFloat4(col, colorval);
|
2014-08-17 12:16:57 +02:00
|
|
|
GLuint glMask = 0;
|
2017-02-15 11:06:59 +01:00
|
|
|
if (mask & FBChannel::FB_COLOR_BIT) {
|
2014-08-17 12:16:57 +02:00
|
|
|
glMask |= GL_COLOR_BUFFER_BIT;
|
|
|
|
}
|
2017-02-15 11:06:59 +01:00
|
|
|
if (mask & FBChannel::FB_DEPTH_BIT) {
|
2014-08-17 12:16:57 +02:00
|
|
|
glMask |= GL_DEPTH_BUFFER_BIT;
|
|
|
|
}
|
2017-02-15 11:06:59 +01:00
|
|
|
if (mask & FBChannel::FB_STENCIL_BIT) {
|
2014-08-17 12:16:57 +02:00
|
|
|
glMask |= GL_STENCIL_BUFFER_BIT;
|
|
|
|
}
|
2017-11-18 21:12:11 +01:00
|
|
|
renderManager_.Clear(colorval, depthVal, stencilVal, glMask);
|
2014-08-17 12:16:57 +02:00
|
|
|
}
|
|
|
|
|
2016-12-25 21:01:57 +01:00
|
|
|
DrawContext *T3DCreateGLContext() {
|
2016-12-25 21:21:56 +01:00
|
|
|
return new OpenGLContext();
|
2014-08-17 12:16:57 +02:00
|
|
|
}
|
|
|
|
|
2017-11-18 21:12:11 +01:00
|
|
|
OpenGLInputLayout::~OpenGLInputLayout() {
|
|
|
|
}
|
2015-12-13 09:25:22 -08:00
|
|
|
|
2017-11-18 21:12:11 +01:00
|
|
|
void OpenGLInputLayout::Compile(const InputLayoutDesc &desc) {
|
|
|
|
int semMask = 0;
|
2017-11-19 00:23:52 +01:00
|
|
|
|
|
|
|
// This is only accurate if there's only one stream. But whatever, for now.
|
|
|
|
stride = (GLsizei)desc.bindings[0].stride;
|
|
|
|
|
2017-11-18 21:12:11 +01:00
|
|
|
std::vector<GLRInputLayout::Entry> entries;
|
|
|
|
for (auto &attr : desc.attributes) {
|
|
|
|
GLRInputLayout::Entry entry;
|
|
|
|
entry.location = attr.location;
|
|
|
|
entry.stride = (GLsizei)desc.bindings[attr.binding].stride;
|
|
|
|
entry.offset = attr.offset;
|
|
|
|
switch (attr.format) {
|
|
|
|
case DataFormat::R32G32_FLOAT:
|
|
|
|
entry.count = 2;
|
|
|
|
entry.type = GL_FLOAT;
|
|
|
|
entry.normalized = GL_FALSE;
|
|
|
|
break;
|
|
|
|
case DataFormat::R32G32B32_FLOAT:
|
|
|
|
entry.count = 3;
|
|
|
|
entry.type = GL_FLOAT;
|
|
|
|
entry.normalized = GL_FALSE;
|
|
|
|
break;
|
|
|
|
case DataFormat::R32G32B32A32_FLOAT:
|
|
|
|
entry.count = 4;
|
|
|
|
entry.type = GL_FLOAT;
|
|
|
|
entry.normalized = GL_FALSE;
|
|
|
|
break;
|
|
|
|
case DataFormat::R8G8B8A8_UNORM:
|
|
|
|
entry.count = 4;
|
|
|
|
entry.type = GL_UNSIGNED_BYTE;
|
|
|
|
entry.normalized = GL_TRUE;
|
|
|
|
break;
|
|
|
|
case DataFormat::UNDEFINED:
|
|
|
|
default:
|
|
|
|
ELOG("Thin3DGLVertexFormat: Invalid or unknown component type applied.");
|
|
|
|
break;
|
2015-12-22 22:13:50 -08:00
|
|
|
}
|
2015-12-13 09:25:22 -08:00
|
|
|
|
2017-11-18 21:12:11 +01:00
|
|
|
entries.push_back(entry);
|
2014-08-17 12:16:57 +02:00
|
|
|
}
|
2017-11-18 21:12:11 +01:00
|
|
|
inputLayout_ = render_->CreateInputLayout(entries);
|
|
|
|
}
|
|
|
|
|
2017-02-04 18:46:12 +01:00
|
|
|
// On PC, we always use GL_DEPTH24_STENCIL8.
|
|
|
|
// On Android, we try to use what's available.
|
|
|
|
|
|
|
|
#ifndef USING_GLES2
|
|
|
|
OpenGLFramebuffer *OpenGLContext::fbo_ext_create(const FramebufferDesc &desc) {
|
|
|
|
OpenGLFramebuffer *fbo = new OpenGLFramebuffer();
|
|
|
|
fbo->width = desc.width;
|
|
|
|
fbo->height = desc.height;
|
|
|
|
fbo->colorDepth = desc.colorDepth;
|
|
|
|
|
|
|
|
// Color texture is same everywhere
|
|
|
|
glGenFramebuffersEXT(1, &fbo->handle);
|
|
|
|
glGenTextures(1, &fbo->color_texture);
|
|
|
|
|
|
|
|
// Create the surfaces.
|
|
|
|
glBindTexture(GL_TEXTURE_2D, fbo->color_texture);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
|
|
|
|
|
|
// TODO: We could opt to only create 16-bit render targets on slow devices. For later.
|
|
|
|
switch (fbo->colorDepth) {
|
|
|
|
case FBO_8888:
|
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, fbo->width, fbo->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
|
|
|
|
break;
|
|
|
|
case FBO_4444:
|
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, fbo->width, fbo->height, 0, GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4, NULL);
|
|
|
|
break;
|
|
|
|
case FBO_5551:
|
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, fbo->width, fbo->height, 0, GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1, NULL);
|
|
|
|
break;
|
|
|
|
case FBO_565:
|
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, fbo->width, fbo->height, 0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, NULL);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
|
|
|
|
|
|
fbo->stencil_buffer = 0;
|
|
|
|
fbo->z_buffer = 0;
|
|
|
|
// 24-bit Z, 8-bit stencil
|
|
|
|
glGenRenderbuffersEXT(1, &fbo->z_stencil_buffer);
|
|
|
|
glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, fbo->z_stencil_buffer);
|
|
|
|
glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_STENCIL_EXT, fbo->width, fbo->height);
|
|
|
|
//glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH24_STENCIL8, width, height);
|
|
|
|
|
|
|
|
// Bind it all together
|
|
|
|
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo->handle);
|
|
|
|
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, fbo->color_texture, 0);
|
|
|
|
glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, fbo->z_stencil_buffer);
|
|
|
|
glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, fbo->z_stencil_buffer);
|
|
|
|
|
|
|
|
GLenum status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
|
|
|
|
switch (status) {
|
|
|
|
case GL_FRAMEBUFFER_COMPLETE_EXT:
|
|
|
|
// ILOG("Framebuffer verified complete.");
|
|
|
|
break;
|
|
|
|
case GL_FRAMEBUFFER_UNSUPPORTED_EXT:
|
|
|
|
ELOG("GL_FRAMEBUFFER_UNSUPPORTED");
|
|
|
|
break;
|
|
|
|
case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT:
|
|
|
|
ELOG("GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT ");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
FLOG("Other framebuffer error: %i", status);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// Unbind state we don't need
|
|
|
|
glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, 0);
|
|
|
|
|
|
|
|
currentDrawHandle_ = fbo->handle;
|
|
|
|
currentReadHandle_ = fbo->handle;
|
|
|
|
return fbo;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-02-06 11:26:24 +01:00
|
|
|
Framebuffer *OpenGLContext::CreateFramebuffer(const FramebufferDesc &desc) {
|
2017-02-04 18:46:12 +01:00
|
|
|
CheckGLExtensions();
|
|
|
|
|
2017-11-19 17:37:56 +01:00
|
|
|
OpenGLFramebuffer *fbo = new OpenGLFramebuffer();
|
|
|
|
// fbo->framebuffer = renderManager_.CreateFramebuffer()
|
|
|
|
|
2017-02-04 18:46:12 +01:00
|
|
|
#ifndef USING_GLES2
|
|
|
|
if (!gl_extensions.ARB_framebuffer_object && gl_extensions.EXT_framebuffer_object) {
|
|
|
|
return fbo_ext_create(desc);
|
|
|
|
} else if (!gl_extensions.ARB_framebuffer_object) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
// If GLES2, we have basic FBO support and can just proceed.
|
|
|
|
#endif
|
2017-03-03 14:15:27 +01:00
|
|
|
CHECK_GL_ERROR_IF_DEBUG();
|
2017-02-04 18:46:12 +01:00
|
|
|
|
|
|
|
fbo->width = desc.width;
|
|
|
|
fbo->height = desc.height;
|
|
|
|
fbo->colorDepth = desc.colorDepth;
|
|
|
|
|
|
|
|
// Color texture is same everywhere
|
|
|
|
glGenFramebuffers(1, &fbo->handle);
|
|
|
|
glGenTextures(1, &fbo->color_texture);
|
|
|
|
|
|
|
|
// Create the surfaces.
|
|
|
|
glBindTexture(GL_TEXTURE_2D, fbo->color_texture);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
|
|
|
|
|
|
// TODO: We could opt to only create 16-bit render targets on slow devices. For later.
|
|
|
|
switch (fbo->colorDepth) {
|
|
|
|
case FBO_8888:
|
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, fbo->width, fbo->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
|
|
|
|
break;
|
|
|
|
case FBO_4444:
|
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, fbo->width, fbo->height, 0, GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4, NULL);
|
|
|
|
break;
|
|
|
|
case FBO_5551:
|
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, fbo->width, fbo->height, 0, GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1, NULL);
|
|
|
|
break;
|
|
|
|
case FBO_565:
|
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, fbo->width, fbo->height, 0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, NULL);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
|
|
|
|
|
|
if (gl_extensions.IsGLES) {
|
|
|
|
if (gl_extensions.OES_packed_depth_stencil) {
|
|
|
|
ILOG("Creating %i x %i FBO using DEPTH24_STENCIL8", fbo->width, fbo->height);
|
|
|
|
// Standard method
|
|
|
|
fbo->stencil_buffer = 0;
|
|
|
|
fbo->z_buffer = 0;
|
|
|
|
// 24-bit Z, 8-bit stencil combined
|
|
|
|
glGenRenderbuffers(1, &fbo->z_stencil_buffer);
|
|
|
|
glBindRenderbuffer(GL_RENDERBUFFER, fbo->z_stencil_buffer);
|
|
|
|
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8_OES, fbo->width, fbo->height);
|
|
|
|
|
|
|
|
// Bind it all together
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, fbo->handle);
|
|
|
|
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, fbo->color_texture, 0);
|
|
|
|
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, fbo->z_stencil_buffer);
|
|
|
|
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, fbo->z_stencil_buffer);
|
|
|
|
} else {
|
|
|
|
ILOG("Creating %i x %i FBO using separate stencil", fbo->width, fbo->height);
|
|
|
|
// TEGRA
|
|
|
|
fbo->z_stencil_buffer = 0;
|
|
|
|
// 16/24-bit Z, separate 8-bit stencil
|
|
|
|
glGenRenderbuffers(1, &fbo->z_buffer);
|
|
|
|
glBindRenderbuffer(GL_RENDERBUFFER, fbo->z_buffer);
|
|
|
|
// Don't forget to make sure fbo_standard_z_depth() matches.
|
|
|
|
glRenderbufferStorage(GL_RENDERBUFFER, gl_extensions.OES_depth24 ? GL_DEPTH_COMPONENT24 : GL_DEPTH_COMPONENT16, fbo->width, fbo->height);
|
|
|
|
|
|
|
|
// 8-bit stencil buffer
|
|
|
|
glGenRenderbuffers(1, &fbo->stencil_buffer);
|
|
|
|
glBindRenderbuffer(GL_RENDERBUFFER, fbo->stencil_buffer);
|
|
|
|
glRenderbufferStorage(GL_RENDERBUFFER, GL_STENCIL_INDEX8, fbo->width, fbo->height);
|
|
|
|
|
|
|
|
// Bind it all together
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, fbo->handle);
|
|
|
|
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, fbo->color_texture, 0);
|
|
|
|
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, fbo->z_buffer);
|
|
|
|
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, fbo->stencil_buffer);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fbo->stencil_buffer = 0;
|
|
|
|
fbo->z_buffer = 0;
|
|
|
|
// 24-bit Z, 8-bit stencil
|
|
|
|
glGenRenderbuffers(1, &fbo->z_stencil_buffer);
|
|
|
|
glBindRenderbuffer(GL_RENDERBUFFER, fbo->z_stencil_buffer);
|
|
|
|
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, fbo->width, fbo->height);
|
|
|
|
|
|
|
|
// Bind it all together
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, fbo->handle);
|
|
|
|
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, fbo->color_texture, 0);
|
|
|
|
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, fbo->z_stencil_buffer);
|
|
|
|
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, fbo->z_stencil_buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
|
|
|
|
switch (status) {
|
|
|
|
case GL_FRAMEBUFFER_COMPLETE:
|
|
|
|
// ILOG("Framebuffer verified complete.");
|
|
|
|
break;
|
|
|
|
case GL_FRAMEBUFFER_UNSUPPORTED:
|
|
|
|
ELOG("GL_FRAMEBUFFER_UNSUPPORTED");
|
|
|
|
break;
|
|
|
|
case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT:
|
|
|
|
ELOG("GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT ");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
FLOG("Other framebuffer error: %i", status);
|
|
|
|
break;
|
|
|
|
}
|
2017-05-16 16:00:34 +02:00
|
|
|
|
2017-02-04 18:46:12 +01:00
|
|
|
// Unbind state we don't need
|
|
|
|
glBindRenderbuffer(GL_RENDERBUFFER, 0);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, 0);
|
2017-03-03 14:15:27 +01:00
|
|
|
CHECK_GL_ERROR_IF_DEBUG();
|
2017-02-04 18:46:12 +01:00
|
|
|
|
|
|
|
currentDrawHandle_ = fbo->handle;
|
|
|
|
currentReadHandle_ = fbo->handle;
|
|
|
|
return fbo;
|
|
|
|
}
|
|
|
|
|
|
|
|
GLenum OpenGLContext::fbo_get_fb_target(bool read, GLuint **cached) {
|
|
|
|
bool supportsBlit = gl_extensions.ARB_framebuffer_object;
|
|
|
|
if (gl_extensions.IsGLES) {
|
|
|
|
supportsBlit = (gl_extensions.GLES3 || gl_extensions.NV_framebuffer_blit);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note: GL_FRAMEBUFFER_EXT and GL_FRAMEBUFFER have the same value, same with _NV.
|
|
|
|
if (supportsBlit) {
|
|
|
|
if (read) {
|
|
|
|
*cached = ¤tReadHandle_;
|
|
|
|
return GL_READ_FRAMEBUFFER;
|
|
|
|
} else {
|
|
|
|
*cached = ¤tDrawHandle_;
|
|
|
|
return GL_DRAW_FRAMEBUFFER;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
*cached = ¤tDrawHandle_;
|
|
|
|
return GL_FRAMEBUFFER;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void OpenGLContext::fbo_bind_fb_target(bool read, GLuint name) {
|
|
|
|
GLuint *cached;
|
|
|
|
GLenum target = fbo_get_fb_target(read, &cached);
|
|
|
|
if (*cached != name) {
|
|
|
|
if (gl_extensions.ARB_framebuffer_object || gl_extensions.IsGLES) {
|
|
|
|
glBindFramebuffer(target, name);
|
|
|
|
} else {
|
|
|
|
#ifndef USING_GLES2
|
|
|
|
glBindFramebufferEXT(target, name);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
*cached = name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void OpenGLContext::fbo_unbind() {
|
|
|
|
#ifndef USING_GLES2
|
|
|
|
if (gl_extensions.ARB_framebuffer_object || gl_extensions.IsGLES) {
|
2017-08-28 21:16:17 +02:00
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, g_defaultFBO);
|
2017-02-04 18:46:12 +01:00
|
|
|
} else if (gl_extensions.EXT_framebuffer_object) {
|
2017-08-28 21:16:17 +02:00
|
|
|
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, g_defaultFBO);
|
2017-02-04 18:46:12 +01:00
|
|
|
}
|
|
|
|
#else
|
2017-08-28 21:16:17 +02:00
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, g_defaultFBO);
|
2017-02-04 18:46:12 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef IOS
|
|
|
|
bindDefaultFBO();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
currentDrawHandle_ = 0;
|
|
|
|
currentReadHandle_ = 0;
|
|
|
|
}
|
|
|
|
|
2017-05-16 16:00:34 +02:00
|
|
|
void OpenGLContext::BindFramebufferAsRenderTarget(Framebuffer *fbo, const RenderPassInfo &rp) {
|
2017-11-18 21:51:17 +01:00
|
|
|
OpenGLFramebuffer *fb = (OpenGLFramebuffer *)fbo;
|
|
|
|
GLRRenderPassAction color = (GLRRenderPassAction)rp.color;
|
|
|
|
GLRRenderPassAction depth = (GLRRenderPassAction)rp.depth;
|
|
|
|
|
|
|
|
renderManager_.BindFramebufferAsRenderTarget(fb ? fb->framebuffer : nullptr, color, depth, rp.clearColor, rp.clearDepth, rp.clearStencil);
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
|
2017-03-03 14:15:27 +01:00
|
|
|
CHECK_GL_ERROR_IF_DEBUG();
|
2017-11-21 16:45:29 +01:00
|
|
|
curFB_ = (OpenGLFramebuffer *)fbo;
|
2017-05-16 13:53:57 +02:00
|
|
|
if (fbo) {
|
|
|
|
OpenGLFramebuffer *fb = (OpenGLFramebuffer *)fbo;
|
|
|
|
// Without FBO_ARB / GLES3, this will collide with bind_for_read, but there's nothing
|
|
|
|
// in ES 2.0 that actually separate them anyway of course, so doesn't matter.
|
|
|
|
fbo_bind_fb_target(false, fb->handle);
|
|
|
|
// Always restore viewport after render target binding. Works around driver bugs.
|
|
|
|
glstate.viewport.restore();
|
|
|
|
} else {
|
|
|
|
fbo_unbind();
|
|
|
|
}
|
2017-05-16 16:00:34 +02:00
|
|
|
int clearFlags = 0;
|
|
|
|
if (rp.color == RPAction::CLEAR) {
|
|
|
|
float fc[4]{};
|
|
|
|
if (rp.clearColor) {
|
|
|
|
Uint8x4ToFloat4(fc, rp.clearColor);
|
|
|
|
}
|
|
|
|
glClearColor(fc[0], fc[1], fc[2], fc[3]);
|
|
|
|
clearFlags |= GL_COLOR_BUFFER_BIT;
|
|
|
|
glstate.colorMask.force(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
|
|
|
|
}
|
|
|
|
if (rp.depth == RPAction::CLEAR) {
|
2017-06-01 08:59:45 +02:00
|
|
|
#ifdef USING_GLES2
|
2017-05-22 14:48:20 +02:00
|
|
|
glClearDepthf(rp.clearDepth);
|
2017-06-01 08:59:45 +02:00
|
|
|
#else
|
|
|
|
glClearDepth(rp.clearDepth);
|
|
|
|
#endif
|
2017-12-30 22:52:22 +01:00
|
|
|
clearFlags |= GL_DEPTH_BUFFER_BIT;
|
2017-05-16 16:00:34 +02:00
|
|
|
glstate.depthWrite.force(GL_TRUE);
|
2017-12-30 22:52:22 +01:00
|
|
|
}
|
|
|
|
if (rp.stencil == RPAction::CLEAR) {
|
|
|
|
glClearStencil(rp.clearStencil);
|
|
|
|
clearFlags |= GL_STENCIL_BUFFER_BIT;
|
2017-05-16 16:00:34 +02:00
|
|
|
glstate.stencilFunc.force(GL_ALWAYS, 0, 0);
|
|
|
|
glstate.stencilMask.force(0xFF);
|
|
|
|
}
|
|
|
|
if (clearFlags) {
|
|
|
|
glstate.scissorTest.force(false);
|
|
|
|
glClear(clearFlags);
|
|
|
|
glstate.scissorTest.restore();
|
|
|
|
}
|
|
|
|
if (rp.color == RPAction::CLEAR) {
|
|
|
|
glstate.colorMask.restore();
|
|
|
|
}
|
|
|
|
if (rp.depth == RPAction::CLEAR) {
|
|
|
|
glstate.depthWrite.restore();
|
2017-12-30 22:52:22 +01:00
|
|
|
}
|
|
|
|
if (rp.stencil == RPAction::CLEAR) {
|
2017-05-16 16:00:34 +02:00
|
|
|
glstate.stencilFunc.restore();
|
|
|
|
glstate.stencilMask.restore();
|
|
|
|
}
|
2017-03-03 14:15:27 +01:00
|
|
|
CHECK_GL_ERROR_IF_DEBUG();
|
2017-11-18 21:51:17 +01:00
|
|
|
#endif
|
2017-02-04 18:46:12 +01:00
|
|
|
}
|
|
|
|
|
2017-02-12 11:20:55 +01:00
|
|
|
void OpenGLContext::CopyFramebufferImage(Framebuffer *fbsrc, int srcLevel, int srcX, int srcY, int srcZ, Framebuffer *fbdst, int dstLevel, int dstX, int dstY, int dstZ, int width, int height, int depth, int channelBits) {
|
2017-02-04 18:46:12 +01:00
|
|
|
OpenGLFramebuffer *src = (OpenGLFramebuffer *)fbsrc;
|
|
|
|
OpenGLFramebuffer *dst = (OpenGLFramebuffer *)fbdst;
|
2017-11-18 20:17:17 +01:00
|
|
|
|
|
|
|
int aspect = 0;
|
|
|
|
if (channelBits & FB_COLOR_BIT) {
|
2017-11-18 21:12:11 +01:00
|
|
|
aspect |= GL_COLOR_BUFFER_BIT;
|
2017-11-18 20:17:17 +01:00
|
|
|
} else if (channelBits & (FB_STENCIL_BIT | FB_DEPTH_BIT)) {
|
|
|
|
if (channelBits & FB_DEPTH_BIT)
|
2017-11-18 21:12:11 +01:00
|
|
|
aspect |= GL_DEPTH_BUFFER_BIT;
|
2017-11-19 17:37:56 +01:00
|
|
|
if (channelBits & FB_STENCIL_BIT)
|
|
|
|
aspect |= GL_STENCIL_BUFFER_BIT;
|
2017-02-04 18:46:12 +01:00
|
|
|
}
|
2017-11-18 20:17:17 +01:00
|
|
|
renderManager_.CopyFramebuffer(src->framebuffer, GLRect2D{ srcX, srcY, width, height }, dst->framebuffer, GLOffset2D{ dstX, dstY }, aspect);
|
2017-02-04 18:46:12 +01:00
|
|
|
}
|
|
|
|
|
2017-02-06 11:26:24 +01:00
|
|
|
bool OpenGLContext::BlitFramebuffer(Framebuffer *fbsrc, int srcX1, int srcY1, int srcX2, int srcY2, Framebuffer *fbdst, int dstX1, int dstY1, int dstX2, int dstY2, int channels, FBBlitFilter linearFilter) {
|
2017-02-04 18:46:12 +01:00
|
|
|
OpenGLFramebuffer *src = (OpenGLFramebuffer *)fbsrc;
|
|
|
|
OpenGLFramebuffer *dst = (OpenGLFramebuffer *)fbdst;
|
2017-11-19 17:37:56 +01:00
|
|
|
GLuint aspect = 0;
|
2017-02-04 18:46:12 +01:00
|
|
|
if (channels & FB_COLOR_BIT)
|
2017-11-19 17:37:56 +01:00
|
|
|
aspect |= GL_COLOR_BUFFER_BIT;
|
2017-02-04 18:46:12 +01:00
|
|
|
if (channels & FB_DEPTH_BIT)
|
2017-11-19 17:37:56 +01:00
|
|
|
aspect |= GL_DEPTH_BUFFER_BIT;
|
2017-02-04 18:46:12 +01:00
|
|
|
if (channels & FB_STENCIL_BIT)
|
2017-11-19 17:37:56 +01:00
|
|
|
aspect |= GL_STENCIL_BUFFER_BIT;
|
|
|
|
|
|
|
|
renderManager_.BlitFramebuffer(src->framebuffer, GLRect2D{ srcX1, srcY1, srcX2 - srcX1, srcY2 - srcY1 }, dst->framebuffer, GLRect2D{ dstX1, dstY1, dstX2 - dstX1, dstY2 - dstY1 }, aspect, linearFilter == FB_BLIT_LINEAR);
|
|
|
|
/*
|
2017-05-16 14:27:34 +02:00
|
|
|
// Without FBO_ARB / GLES3, this will collide with bind_for_read, but there's nothing
|
|
|
|
// in ES 2.0 that actually separate them anyway of course, so doesn't matter.
|
|
|
|
fbo_bind_fb_target(false, dst->handle);
|
|
|
|
fbo_bind_fb_target(true, src->handle);
|
2017-02-04 18:46:12 +01:00
|
|
|
if (gl_extensions.GLES3 || gl_extensions.ARB_framebuffer_object) {
|
2017-11-19 17:37:56 +01:00
|
|
|
glBlitFramebuffer(srcX1, srcY1, srcX2, srcY2, dstX1, dstY1, dstX2, dstY2, aspect, linearFilter == FB_BLIT_LINEAR ? GL_LINEAR : GL_NEAREST);
|
2017-03-03 14:15:27 +01:00
|
|
|
CHECK_GL_ERROR_IF_DEBUG();
|
2017-02-04 18:46:12 +01:00
|
|
|
#if defined(USING_GLES2) && defined(__ANDROID__) // We only support this extension on Android, it's not even available on PC.
|
|
|
|
return true;
|
|
|
|
} else if (gl_extensions.NV_framebuffer_blit) {
|
2017-11-19 17:37:56 +01:00
|
|
|
glBlitFramebufferNV(srcX1, srcY1, srcX2, srcY2, dstX1, dstY1, dstX2, dstY2, aspect, linearFilter == FB_BLIT_LINEAR ? GL_LINEAR : GL_NEAREST);
|
2017-03-03 14:15:27 +01:00
|
|
|
CHECK_GL_ERROR_IF_DEBUG();
|
2017-02-04 18:46:12 +01:00
|
|
|
#endif // defined(USING_GLES2) && defined(__ANDROID__)
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
2017-11-19 17:37:56 +01:00
|
|
|
}*/
|
|
|
|
return true;
|
2017-02-04 18:46:12 +01:00
|
|
|
}
|
|
|
|
|
2017-02-06 11:26:24 +01:00
|
|
|
void OpenGLContext::BindFramebufferAsTexture(Framebuffer *fbo, int binding, FBChannel channelBit, int color) {
|
2017-02-04 18:46:12 +01:00
|
|
|
OpenGLFramebuffer *fb = (OpenGLFramebuffer *)fbo;
|
2017-11-18 22:15:12 +01:00
|
|
|
|
|
|
|
GLuint aspect = 0;
|
|
|
|
renderManager_.BindFramebufferAsTexture(fb->framebuffer, binding, (int)channelBit, color);
|
2017-02-04 18:46:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
OpenGLFramebuffer::~OpenGLFramebuffer() {
|
2017-03-03 14:15:27 +01:00
|
|
|
CHECK_GL_ERROR_IF_DEBUG();
|
2017-02-04 18:46:12 +01:00
|
|
|
if (gl_extensions.ARB_framebuffer_object || gl_extensions.IsGLES) {
|
2017-03-18 13:26:18 +01:00
|
|
|
if (handle) {
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, handle);
|
|
|
|
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
|
|
|
|
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, 0);
|
2017-08-28 21:16:17 +02:00
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, g_defaultFBO);
|
2017-02-15 23:56:38 +01:00
|
|
|
glDeleteFramebuffers(1, &handle);
|
2017-03-18 13:26:18 +01:00
|
|
|
}
|
2017-02-15 23:56:38 +01:00
|
|
|
if (z_stencil_buffer)
|
|
|
|
glDeleteRenderbuffers(1, &z_stencil_buffer);
|
|
|
|
if (z_buffer)
|
|
|
|
glDeleteRenderbuffers(1, &z_buffer);
|
|
|
|
if (stencil_buffer)
|
|
|
|
glDeleteRenderbuffers(1, &stencil_buffer);
|
2017-02-04 18:46:12 +01:00
|
|
|
} else if (gl_extensions.EXT_framebuffer_object) {
|
|
|
|
#ifndef USING_GLES2
|
2017-03-18 13:26:18 +01:00
|
|
|
if (handle) {
|
|
|
|
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, handle);
|
|
|
|
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
|
|
|
|
glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER_EXT, 0);
|
2017-08-28 21:16:17 +02:00
|
|
|
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, g_defaultFBO);
|
2017-02-15 23:56:38 +01:00
|
|
|
glDeleteFramebuffersEXT(1, &handle);
|
2017-03-18 13:26:18 +01:00
|
|
|
}
|
2017-02-15 23:56:38 +01:00
|
|
|
if (z_stencil_buffer)
|
|
|
|
glDeleteRenderbuffers(1, &z_stencil_buffer);
|
|
|
|
if (z_buffer)
|
|
|
|
glDeleteRenderbuffers(1, &z_buffer);
|
|
|
|
if (stencil_buffer)
|
|
|
|
glDeleteRenderbuffers(1, &stencil_buffer);
|
2017-02-04 18:46:12 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
glDeleteTextures(1, &color_texture);
|
|
|
|
}
|
|
|
|
|
2017-02-06 11:26:24 +01:00
|
|
|
void OpenGLContext::GetFramebufferDimensions(Framebuffer *fbo, int *w, int *h) {
|
2017-02-04 18:46:12 +01:00
|
|
|
OpenGLFramebuffer *fb = (OpenGLFramebuffer *)fbo;
|
2017-10-16 16:27:16 +02:00
|
|
|
if (fb) {
|
|
|
|
*w = fb->width;
|
|
|
|
*h = fb->height;
|
|
|
|
} else {
|
|
|
|
*w = targetWidth_;
|
|
|
|
*h = targetHeight_;
|
|
|
|
}
|
2017-02-04 18:46:12 +01:00
|
|
|
}
|
|
|
|
|
2017-01-19 12:16:36 +07:00
|
|
|
uint32_t OpenGLContext::GetDataFormatSupport(DataFormat fmt) const {
|
|
|
|
switch (fmt) {
|
|
|
|
case DataFormat::B8G8R8A8_UNORM:
|
2017-03-11 14:43:42 +01:00
|
|
|
return FMT_RENDERTARGET | FMT_TEXTURE | FMT_AUTOGEN_MIPS;
|
2017-01-21 13:11:03 +01:00
|
|
|
case DataFormat::B4G4R4A4_UNORM_PACK16:
|
2017-03-11 14:43:42 +01:00
|
|
|
return FMT_RENDERTARGET | FMT_TEXTURE | FMT_AUTOGEN_MIPS; // native support
|
2017-02-22 16:23:04 +01:00
|
|
|
case DataFormat::A4R4G4B4_UNORM_PACK16:
|
2017-02-19 11:09:42 +01:00
|
|
|
#ifndef USING_GLES2
|
|
|
|
// Can support this if _REV formats are supported.
|
|
|
|
return FMT_TEXTURE;
|
|
|
|
#endif
|
|
|
|
return 0;
|
2017-01-19 12:16:36 +07:00
|
|
|
|
|
|
|
case DataFormat::R8G8B8A8_UNORM:
|
2017-03-11 14:43:42 +01:00
|
|
|
return FMT_RENDERTARGET | FMT_TEXTURE | FMT_INPUTLAYOUT | FMT_AUTOGEN_MIPS;
|
2017-01-19 12:16:36 +07:00
|
|
|
|
|
|
|
case DataFormat::R32_FLOAT:
|
|
|
|
case DataFormat::R32G32_FLOAT:
|
|
|
|
case DataFormat::R32G32B32_FLOAT:
|
|
|
|
case DataFormat::R32G32B32A32_FLOAT:
|
|
|
|
return FMT_INPUTLAYOUT;
|
|
|
|
|
|
|
|
case DataFormat::R8_UNORM:
|
|
|
|
return 0;
|
|
|
|
case DataFormat::BC1_RGBA_UNORM_BLOCK:
|
|
|
|
case DataFormat::BC2_UNORM_BLOCK:
|
|
|
|
case DataFormat::BC3_UNORM_BLOCK:
|
|
|
|
return FMT_TEXTURE;
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-26 11:57:48 +01:00
|
|
|
} // namespace Draw
|