2014-08-17 12:16:57 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <vector>
|
|
|
|
#include <string>
|
|
|
|
#include <map>
|
|
|
|
|
|
|
|
#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"
|
|
|
|
#include "gfx_es2/gpu_features.h"
|
2014-09-06 13:49:01 +02:00
|
|
|
#include "gfx/gl_lost_manager.h"
|
2014-08-17 12:16:57 +02: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
|
|
|
|
GL_REPEAT,
|
|
|
|
#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
|
|
|
};
|
|
|
|
|
|
|
|
static const char *glsl_fragment_prelude =
|
|
|
|
"#ifdef GL_ES\n"
|
|
|
|
"precision mediump float;\n"
|
|
|
|
"#endif\n";
|
|
|
|
|
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;
|
2015-01-04 17:57:52 +01:00
|
|
|
bool logicEnabled;
|
|
|
|
GLuint logicOp;
|
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
|
|
|
|
|
|
|
void Apply() {
|
2015-09-06 12:21:15 +02:00
|
|
|
if (enabled) {
|
|
|
|
glEnable(GL_BLEND);
|
|
|
|
glBlendEquationSeparate(eqCol, eqAlpha);
|
|
|
|
glBlendFuncSeparate(srcCol, dstCol, srcAlpha, dstAlpha);
|
|
|
|
} else {
|
|
|
|
glDisable(GL_BLEND);
|
|
|
|
}
|
2016-12-26 18:32:52 +01:00
|
|
|
glColorMask(colorMask & 1, (colorMask >> 1) & 1, (colorMask >> 2) & 1, (colorMask >> 3) & 1);
|
|
|
|
|
2014-08-17 12:16:57 +02:00
|
|
|
#if !defined(USING_GLES2)
|
2015-01-04 17:57:52 +01:00
|
|
|
if (logicEnabled) {
|
2015-12-22 23:51:50 -08:00
|
|
|
glEnable(GL_COLOR_LOGIC_OP);
|
2015-09-06 12:21:15 +02:00
|
|
|
glLogicOp(logicOp);
|
|
|
|
} else {
|
2015-12-22 23:51:50 -08:00
|
|
|
glDisable(GL_COLOR_LOGIC_OP);
|
2015-01-04 17:57:52 +01:00
|
|
|
}
|
2014-08-17 12:16:57 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-12-25 21:21:56 +01:00
|
|
|
class OpenGLSamplerState : public SamplerState {
|
2016-02-13 13:37:00 -08:00
|
|
|
public:
|
2016-12-25 21:21:56 +01:00
|
|
|
// Old school. Should also support using a sampler object.
|
|
|
|
|
2016-02-13 13:37:00 -08:00
|
|
|
GLint wrapS;
|
|
|
|
GLint wrapT;
|
|
|
|
GLint magFilt;
|
|
|
|
GLint minFilt;
|
|
|
|
GLint mipMinFilt;
|
|
|
|
|
|
|
|
void Apply(bool hasMips, bool canWrap) {
|
|
|
|
if (canWrap) {
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapS);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapT);
|
|
|
|
} else {
|
|
|
|
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_MAG_FILTER, magFilt);
|
|
|
|
if (hasMips) {
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, mipMinFilt);
|
|
|
|
} else {
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minFilt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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
|
|
|
|
|
|
|
void Apply() {
|
2015-09-06 12:21:15 +02:00
|
|
|
if (depthTestEnabled) {
|
|
|
|
glEnable(GL_DEPTH_TEST);
|
|
|
|
glDepthFunc(depthComp);
|
|
|
|
glDepthMask(depthWriteEnabled);
|
|
|
|
} else {
|
|
|
|
glDisable(GL_DEPTH_TEST);
|
|
|
|
}
|
2016-12-26 23:11:31 +01:00
|
|
|
if (stencilEnabled) {
|
|
|
|
glEnable(GL_STENCIL_TEST);
|
|
|
|
glStencilOpSeparate(GL_FRONT_AND_BACK, stencilFail, stencilZFail, stencilPass);
|
|
|
|
glStencilFuncSeparate(GL_FRONT_AND_BACK, stencilCompareOp, stencilReference, stencilCompareMask);
|
|
|
|
glStencilMaskSeparate(GL_FRONT_AND_BACK, stencilWriteMask);
|
|
|
|
} else {
|
|
|
|
glDisable(GL_STENCIL_TEST);
|
|
|
|
}
|
2015-09-06 12:21:15 +02:00
|
|
|
glDisable(GL_STENCIL_TEST);
|
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:
|
|
|
|
void Apply() {
|
2017-01-17 20:26:19 +07:00
|
|
|
glEnable(GL_SCISSOR_TEST);
|
2016-12-23 23:46:11 +01:00
|
|
|
if (!cullEnable) {
|
|
|
|
glDisable(GL_CULL_FACE);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
glEnable(GL_CULL_FACE);
|
|
|
|
glFrontFace(frontFace);
|
|
|
|
glCullFace(cullMode);
|
|
|
|
}
|
|
|
|
|
|
|
|
GLboolean cullEnable;
|
|
|
|
GLenum cullMode;
|
|
|
|
GLenum frontFace;
|
|
|
|
};
|
|
|
|
|
2016-12-25 21:21:56 +01:00
|
|
|
class OpenGLBuffer : public Buffer, GfxResourceHolder {
|
2014-08-17 12:16:57 +02:00
|
|
|
public:
|
2016-12-25 21:21:56 +01:00
|
|
|
OpenGLBuffer(size_t size, uint32_t flags) {
|
2014-08-17 12:16:57 +02:00
|
|
|
glGenBuffers(1, &buffer_);
|
2016-12-25 18:52:05 +01:00
|
|
|
target_ = (flags & BufferUsageFlag::INDEXDATA) ? GL_ELEMENT_ARRAY_BUFFER : GL_ARRAY_BUFFER;
|
2014-08-17 12:16:57 +02:00
|
|
|
usage_ = 0;
|
2016-12-25 18:52:05 +01:00
|
|
|
if (flags & BufferUsageFlag::DYNAMIC)
|
2014-08-17 12:16:57 +02:00
|
|
|
usage_ = GL_STREAM_DRAW;
|
|
|
|
else
|
|
|
|
usage_ = GL_STATIC_DRAW;
|
|
|
|
knownSize_ = 0;
|
2014-09-06 13:49:01 +02:00
|
|
|
register_gl_resource_holder(this);
|
2014-08-17 12:16:57 +02:00
|
|
|
}
|
2016-12-25 21:21:56 +01:00
|
|
|
~OpenGLBuffer() override {
|
2014-09-06 13:49:01 +02:00
|
|
|
unregister_gl_resource_holder(this);
|
2014-08-17 12:16:57 +02:00
|
|
|
glDeleteBuffers(1, &buffer_);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetData(const uint8_t *data, size_t size) override {
|
2015-01-11 16:52:37 -08:00
|
|
|
Bind();
|
2014-08-17 12:16:57 +02:00
|
|
|
glBufferData(target_, size, data, usage_);
|
|
|
|
knownSize_ = size;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SubData(const uint8_t *data, size_t offset, size_t size) override {
|
2015-01-11 16:52:37 -08:00
|
|
|
Bind();
|
2015-12-13 11:26:36 -08:00
|
|
|
if (size + offset > knownSize_) {
|
2014-08-17 12:16:57 +02:00
|
|
|
// Allocate the buffer.
|
|
|
|
glBufferData(target_, size + offset, NULL, usage_);
|
|
|
|
knownSize_ = size + offset;
|
|
|
|
}
|
|
|
|
glBufferSubData(target_, offset, size, data);
|
|
|
|
}
|
|
|
|
void Bind() {
|
2015-09-06 12:21:15 +02:00
|
|
|
glBindBuffer(target_, buffer_);
|
2014-08-17 12:16:57 +02:00
|
|
|
}
|
|
|
|
|
2016-09-10 20:29:58 -07:00
|
|
|
void GLLost() override {
|
|
|
|
buffer_ = 0;
|
|
|
|
}
|
|
|
|
|
2016-03-17 21:30:16 +01:00
|
|
|
void GLRestore() override {
|
2016-09-10 20:29:58 -07:00
|
|
|
ILOG("Recreating vertex buffer after gl_restore");
|
2014-09-06 13:49:01 +02:00
|
|
|
knownSize_ = 0; // Will cause a new glBufferData call. Should genBuffers again though?
|
|
|
|
glGenBuffers(1, &buffer_);
|
|
|
|
}
|
|
|
|
|
2014-08-17 12:16:57 +02:00
|
|
|
private:
|
|
|
|
GLuint buffer_;
|
|
|
|
GLuint target_;
|
|
|
|
GLuint usage_;
|
|
|
|
|
|
|
|
size_t knownSize_;
|
|
|
|
};
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-26 13:42:53 +01:00
|
|
|
// Not registering this as a resource holder, instead Pipeline is registered. It will
|
2014-09-06 13:49:01 +02:00
|
|
|
// invoke Compile again to recreate the shader then link them together.
|
2016-12-25 22:24:14 +01:00
|
|
|
class OpenGLShaderModule : public ShaderModule {
|
2014-08-17 12:16:57 +02:00
|
|
|
public:
|
2016-12-25 22:24:14 +01:00
|
|
|
OpenGLShaderModule(ShaderStage stage) : stage_(stage), shader_(0) {
|
|
|
|
glstage_ = ShaderStageToOpenGL(stage);
|
|
|
|
}
|
|
|
|
|
|
|
|
~OpenGLShaderModule() {
|
|
|
|
glDeleteShader(shader_);
|
2014-08-17 12:16:57 +02:00
|
|
|
}
|
|
|
|
|
2016-12-27 17:38:26 +01:00
|
|
|
bool Compile(ShaderLanguage language, const uint8_t *data, size_t dataSize);
|
2014-09-06 13:49:01 +02:00
|
|
|
GLuint GetShader() const {
|
|
|
|
return shader_;
|
|
|
|
}
|
|
|
|
const std::string &GetSource() const { return source_; }
|
2014-08-17 12:16:57 +02:00
|
|
|
|
2016-09-10 20:29:58 -07:00
|
|
|
void Unset() {
|
|
|
|
shader_ = 0;
|
|
|
|
}
|
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:
|
2016-12-25 22:24:14 +01:00
|
|
|
ShaderStage stage_;
|
2016-12-27 17:38:26 +01:00
|
|
|
ShaderLanguage language_;
|
2014-08-17 12:16:57 +02:00
|
|
|
GLuint shader_;
|
2016-12-25 22:24:14 +01:00
|
|
|
GLuint glstage_;
|
2014-08-17 12:16:57 +02:00
|
|
|
bool ok_;
|
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
|
|
|
};
|
|
|
|
|
2016-12-27 17:38:26 +01:00
|
|
|
bool OpenGLShaderModule::Compile(ShaderLanguage language, const uint8_t *data, size_t dataSize) {
|
|
|
|
source_ = std::string((const char *)data);
|
2016-12-25 22:24:14 +01:00
|
|
|
shader_ = glCreateShader(glstage_);
|
2016-12-27 17:38:26 +01:00
|
|
|
language_ = language;
|
2014-08-23 09:25:59 -07:00
|
|
|
|
|
|
|
std::string temp;
|
|
|
|
// Add the prelude on automatically for fragment shaders.
|
2016-12-25 22:24:14 +01:00
|
|
|
if (glstage_ == GL_FRAGMENT_SHADER) {
|
2016-12-27 17:38:26 +01:00
|
|
|
temp = std::string(glsl_fragment_prelude) + source_;
|
|
|
|
source_ = temp.c_str();
|
2014-08-23 09:25:59 -07:00
|
|
|
}
|
|
|
|
|
2016-12-27 17:38:26 +01:00
|
|
|
const char *code = source_.c_str();
|
|
|
|
glShaderSource(shader_, 1, &code, nullptr);
|
2014-08-17 12:16:57 +02:00
|
|
|
glCompileShader(shader_);
|
2014-11-23 11:01:51 -08:00
|
|
|
GLint success = 0;
|
2014-08-17 12:16:57 +02:00
|
|
|
glGetShaderiv(shader_, GL_COMPILE_STATUS, &success);
|
|
|
|
if (!success) {
|
|
|
|
#define MAX_INFO_LOG_SIZE 2048
|
|
|
|
GLchar infoLog[MAX_INFO_LOG_SIZE];
|
2014-11-23 11:01:51 -08:00
|
|
|
GLsizei len = 0;
|
2014-08-17 12:16:57 +02:00
|
|
|
glGetShaderInfoLog(shader_, MAX_INFO_LOG_SIZE, &len, infoLog);
|
|
|
|
infoLog[len] = '\0';
|
|
|
|
glDeleteShader(shader_);
|
|
|
|
shader_ = 0;
|
2016-12-25 22:24:14 +01:00
|
|
|
ILOG("%s Shader compile error:\n%s", glstage_ == GL_FRAGMENT_SHADER ? "Fragment" : "Vertex", infoLog);
|
2014-08-17 12:16:57 +02:00
|
|
|
}
|
|
|
|
ok_ = success != 0;
|
|
|
|
return ok_;
|
|
|
|
}
|
|
|
|
|
2016-12-26 13:22:48 +01:00
|
|
|
class OpenGLInputLayout : public InputLayout, GfxResourceHolder {
|
2014-08-17 12:16:57 +02:00
|
|
|
public:
|
2016-12-26 13:22:48 +01:00
|
|
|
~OpenGLInputLayout();
|
2015-12-13 09:25:22 -08:00
|
|
|
|
2015-01-29 10:35:34 +01:00
|
|
|
void Apply(const void *base = nullptr);
|
2014-08-17 12:16:57 +02:00
|
|
|
void Unapply();
|
|
|
|
void Compile();
|
2016-03-17 21:30:16 +01:00
|
|
|
void GLRestore() override;
|
2016-09-10 20:29:58 -07:00
|
|
|
void GLLost() override;
|
2016-12-26 17:03:01 +01:00
|
|
|
bool RequiresBuffer() {
|
2015-12-13 09:25:22 -08:00
|
|
|
return id_ != 0;
|
|
|
|
}
|
2014-08-17 12:16:57 +02:00
|
|
|
|
2016-12-26 13:22:48 +01:00
|
|
|
InputLayoutDesc desc;
|
|
|
|
|
2014-08-17 12:16:57 +02:00
|
|
|
int semanticsMask_; // Fast way to check what semantics to enable/disable.
|
|
|
|
int stride_;
|
2015-12-13 09:25:22 -08:00
|
|
|
GLuint id_;
|
|
|
|
bool needsEnable_;
|
|
|
|
intptr_t lastBase_;
|
2014-08-17 12:16:57 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct UniformInfo {
|
|
|
|
int loc_;
|
|
|
|
};
|
|
|
|
|
|
|
|
// TODO: Add Uniform Buffer support.
|
2016-12-26 11:06:17 +01:00
|
|
|
class OpenGLPipeline : public Pipeline, GfxResourceHolder {
|
2014-08-17 12:16:57 +02:00
|
|
|
public:
|
2016-12-26 11:06:17 +01:00
|
|
|
OpenGLPipeline() {
|
2014-09-06 13:49:01 +02:00
|
|
|
program_ = 0;
|
|
|
|
register_gl_resource_holder(this);
|
2014-08-17 12:16:57 +02:00
|
|
|
}
|
2016-12-26 11:06:17 +01:00
|
|
|
~OpenGLPipeline() {
|
2014-09-06 13:49:01 +02:00
|
|
|
unregister_gl_resource_holder(this);
|
2016-12-25 23:03:20 +01:00
|
|
|
for (auto iter : shaders) {
|
|
|
|
iter->Release();
|
|
|
|
}
|
2014-08-17 12:16:57 +02:00
|
|
|
glDeleteProgram(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 RequiresBuffer() override {
|
|
|
|
return inputLayout->RequiresBuffer();
|
2014-08-17 12:16:57 +02:00
|
|
|
}
|
2016-12-26 17:03:01 +01:00
|
|
|
|
|
|
|
bool LinkShaders();
|
2015-09-17 20:29:37 +02:00
|
|
|
|
2014-08-17 12:16:57 +02:00
|
|
|
void Apply();
|
|
|
|
void Unapply();
|
|
|
|
|
|
|
|
int GetUniformLoc(const char *name);
|
|
|
|
|
2015-09-17 20:29:37 +02:00
|
|
|
void SetVector(const char *name, float *value, int n) override;
|
2016-02-13 14:50:06 -08:00
|
|
|
void SetMatrix4x4(const char *name, const float value[16]) override;
|
2014-08-17 12:16:57 +02:00
|
|
|
|
2016-09-10 20:29:58 -07:00
|
|
|
void GLLost() override {
|
|
|
|
program_ = 0;
|
2016-12-25 23:03:20 +01:00
|
|
|
for (auto iter : shaders) {
|
|
|
|
iter->Unset();
|
|
|
|
}
|
2016-09-10 20:29:58 -07:00
|
|
|
}
|
|
|
|
|
2016-03-17 21:30:16 +01:00
|
|
|
void GLRestore() override {
|
2016-12-25 23:03:20 +01:00
|
|
|
for (auto iter : shaders) {
|
2016-12-27 17:38:26 +01:00
|
|
|
iter->Compile(iter->GetLanguage(), (const uint8_t *)iter->GetSource().c_str(), iter->GetSource().size());
|
2016-12-25 23:03:20 +01:00
|
|
|
}
|
2016-12-26 17:03:01 +01:00
|
|
|
LinkShaders();
|
2014-09-06 13:49:01 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
private:
|
|
|
|
GLuint program_;
|
|
|
|
std::map<std::string, UniformInfo> uniforms_;
|
|
|
|
};
|
|
|
|
|
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
|
|
|
|
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
|
|
|
|
}
|
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-25 20:54:37 +01:00
|
|
|
Buffer *CreateBuffer(size_t size, uint32_t usageFlags) 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
|
|
|
|
2016-12-25 20:54:37 +01:00
|
|
|
Texture *CreateTexture(TextureType type, DataFormat format, int width, int height, int depth, int mipLevels) override;
|
|
|
|
Texture *CreateTexture() 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 {
|
2016-02-13 13:37:00 -08:00
|
|
|
if (samplerStates_.size() < (size_t)(start + count)) {
|
|
|
|
samplerStates_.resize(start + count);
|
|
|
|
}
|
|
|
|
for (int i = 0; i < count; ++i) {
|
|
|
|
int index = i + start;
|
2016-12-25 21:21:56 +01:00
|
|
|
OpenGLSamplerState *s = static_cast<OpenGLSamplerState *>(states[index]);
|
2016-02-13 13:37:00 -08:00
|
|
|
|
|
|
|
if (samplerStates_[index]) {
|
|
|
|
samplerStates_[index]->Release();
|
|
|
|
}
|
|
|
|
samplerStates_[index] = s;
|
|
|
|
samplerStates_[index]->AddRef();
|
|
|
|
|
|
|
|
// TODO: Ideally, get these from the texture and apply on the right stage?
|
|
|
|
if (index == 0) {
|
|
|
|
s->Apply(false, true);
|
|
|
|
}
|
|
|
|
}
|
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 {
|
2015-09-06 12:21:15 +02:00
|
|
|
glScissor(left, targetHeight_ - (top + height), 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 {
|
2016-12-27 22:26:49 +01:00
|
|
|
// TODO: Use glViewportArrayv.
|
2015-09-06 12:21:15 +02:00
|
|
|
glViewport(viewports[0].TopLeftX, viewports[0].TopLeftY, viewports[0].Width, viewports[0].Height);
|
|
|
|
#if defined(USING_GLES2)
|
|
|
|
glDepthRangef(viewports[0].MinDepth, viewports[0].MaxDepth);
|
|
|
|
#else
|
|
|
|
glDepthRange(viewports[0].MinDepth, viewports[0].MaxDepth);
|
|
|
|
#endif
|
2014-08-17 12:16:57 +02:00
|
|
|
}
|
|
|
|
|
2016-12-27 15:52:03 +01:00
|
|
|
void SetBlendFactor(float color[4]) override {
|
|
|
|
glBlendColor(color[0], color[1], color[2], color[3]);
|
|
|
|
}
|
|
|
|
|
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;
|
2014-08-17 15:02:43 +02:00
|
|
|
|
2014-08-17 12:16:57 +02:00
|
|
|
// TODO: Add more sophisticated draws.
|
2016-12-26 17:03:01 +01:00
|
|
|
void Draw(Buffer *vdata, int vertexCount, int offset) override;
|
|
|
|
void DrawIndexed(Buffer *vdata, Buffer *idata, int vertexCount, int offset) override;
|
|
|
|
void DrawUP(const void *vdata, int vertexCount) override;
|
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:
|
|
|
|
switch (gl_extensions.gpuVendor) {
|
|
|
|
case GPU_VENDOR_AMD: return "VENDOR_AMD";
|
|
|
|
case GPU_VENDOR_POWERVR: return "VENDOR_POWERVR";
|
|
|
|
case GPU_VENDOR_NVIDIA: return "VENDOR_NVIDIA";
|
|
|
|
case GPU_VENDOR_INTEL: return "VENDOR_INTEL";
|
|
|
|
case GPU_VENDOR_ADRENO: return "VENDOR_ADRENO";
|
|
|
|
case GPU_VENDOR_ARM: return "VENDOR_ARM";
|
|
|
|
case GPU_VENDOR_BROADCOM: return "VENDOR_BROADCOM";
|
|
|
|
case GPU_VENDOR_UNKNOWN:
|
|
|
|
default:
|
|
|
|
return "VENDOR_UNKNOWN";
|
|
|
|
}
|
|
|
|
break;
|
2014-08-22 20:47:40 +02:00
|
|
|
case RENDERER: return (const char *)glGetString(GL_RENDERER);
|
|
|
|
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
|
|
|
|
2016-12-25 21:21:56 +01:00
|
|
|
std::vector<OpenGLSamplerState *> samplerStates_;
|
2016-12-26 11:16:59 +01:00
|
|
|
OpenGLPipeline *curPipeline_;
|
2016-12-26 17:31:20 +01:00
|
|
|
DeviceCaps caps_;
|
2014-08-17 12:16:57 +02:00
|
|
|
};
|
|
|
|
|
2016-12-25 21:21:56 +01:00
|
|
|
OpenGLContext::OpenGLContext() {
|
2014-08-17 12:16:57 +02:00
|
|
|
CreatePresets();
|
2016-12-26 17:31:20 +01:00
|
|
|
// TODO: Detect caps
|
2014-08-17 12:16:57 +02:00
|
|
|
}
|
|
|
|
|
2016-12-25 21:21:56 +01:00
|
|
|
OpenGLContext::~OpenGLContext() {
|
|
|
|
for (OpenGLSamplerState *s : samplerStates_) {
|
2016-02-13 13:37:00 -08:00
|
|
|
if (s) {
|
|
|
|
s->Release();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
samplerStates_.clear();
|
2014-08-17 12:16:57 +02:00
|
|
|
}
|
|
|
|
|
2016-12-26 13:22:48 +01:00
|
|
|
InputLayout *OpenGLContext::CreateInputLayout(const InputLayoutDesc &desc) {
|
|
|
|
OpenGLInputLayout *fmt = new OpenGLInputLayout();
|
|
|
|
fmt->desc = desc;
|
2014-08-17 12:16:57 +02:00
|
|
|
fmt->Compile();
|
|
|
|
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
|
2014-08-17 15:02:43 +02:00
|
|
|
case LINEAR1D: return GL_TEXTURE_1D;
|
2014-08-23 00:34:55 -07:00
|
|
|
#endif
|
2014-08-17 15:02:43 +02:00
|
|
|
case LINEAR2D: return GL_TEXTURE_2D;
|
|
|
|
case LINEAR3D: return GL_TEXTURE_3D;
|
|
|
|
case CUBE: return GL_TEXTURE_CUBE_MAP;
|
2014-08-23 00:34:55 -07:00
|
|
|
#ifndef USING_GLES2
|
2014-08-17 15:02:43 +02:00
|
|
|
case ARRAY1D: return GL_TEXTURE_1D_ARRAY;
|
2014-08-23 00:34:55 -07:00
|
|
|
#endif
|
2014-08-17 15:02:43 +02:00
|
|
|
case ARRAY2D: return GL_TEXTURE_2D_ARRAY;
|
|
|
|
default: return GL_NONE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-25 20:54:37 +01:00
|
|
|
class Thin3DGLTexture : public Texture, GfxResourceHolder {
|
2014-08-17 15:02:43 +02:00
|
|
|
public:
|
2014-09-06 13:49:01 +02:00
|
|
|
Thin3DGLTexture() : tex_(0), target_(0) {
|
2016-01-23 11:22:40 -08:00
|
|
|
generatedMips_ = false;
|
2016-02-13 13:37:00 -08:00
|
|
|
canWrap_ = true;
|
2014-09-06 13:49:01 +02:00
|
|
|
width_ = 0;
|
|
|
|
height_ = 0;
|
|
|
|
depth_ = 0;
|
|
|
|
glGenTextures(1, &tex_);
|
|
|
|
register_gl_resource_holder(this);
|
|
|
|
}
|
2016-12-25 18:52:05 +01:00
|
|
|
Thin3DGLTexture(TextureType type, DataFormat format, int width, int height, int depth, int mipLevels) : tex_(0), target_(TypeToTarget(type)), format_(format), mipLevels_(mipLevels) {
|
2016-01-23 11:22:40 -08:00
|
|
|
generatedMips_ = false;
|
2016-02-13 13:37:00 -08:00
|
|
|
canWrap_ = true;
|
2014-08-22 20:47:40 +02:00
|
|
|
width_ = width;
|
|
|
|
height_ = height;
|
|
|
|
depth_ = depth;
|
2014-08-17 15:02:43 +02:00
|
|
|
glGenTextures(1, &tex_);
|
2014-09-06 13:49:01 +02:00
|
|
|
register_gl_resource_holder(this);
|
2014-08-17 15:02:43 +02:00
|
|
|
}
|
|
|
|
~Thin3DGLTexture() {
|
2014-09-06 13:49:01 +02:00
|
|
|
unregister_gl_resource_holder(this);
|
|
|
|
Destroy();
|
|
|
|
}
|
|
|
|
|
2016-12-25 18:52:05 +01:00
|
|
|
bool Create(TextureType type, DataFormat format, int width, int height, int depth, int mipLevels) override {
|
2016-01-23 11:22:40 -08:00
|
|
|
generatedMips_ = false;
|
2016-02-13 13:37:00 -08:00
|
|
|
canWrap_ = true;
|
2014-09-06 13:49:01 +02:00
|
|
|
format_ = format;
|
|
|
|
target_ = TypeToTarget(type);
|
|
|
|
mipLevels_ = mipLevels;
|
|
|
|
width_ = width;
|
|
|
|
height_ = height;
|
|
|
|
depth_ = depth;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Destroy() {
|
|
|
|
if (tex_) {
|
|
|
|
glDeleteTextures(1, &tex_);
|
|
|
|
tex_ = 0;
|
2016-01-23 11:22:40 -08:00
|
|
|
generatedMips_ = false;
|
2014-09-06 13:49:01 +02:00
|
|
|
}
|
2014-08-17 15:02:43 +02:00
|
|
|
}
|
2014-08-22 20:47:40 +02:00
|
|
|
void SetImageData(int x, int y, int z, int width, int height, int depth, int level, int stride, const uint8_t *data) override;
|
2014-08-17 15:02:43 +02:00
|
|
|
void AutoGenMipmaps() override;
|
|
|
|
|
2016-02-13 13:37:00 -08:00
|
|
|
bool HasMips() {
|
|
|
|
return mipLevels_ > 1 || generatedMips_;
|
|
|
|
}
|
|
|
|
bool CanWrap() {
|
|
|
|
return canWrap_;
|
|
|
|
}
|
|
|
|
|
2014-08-17 21:28:34 +02:00
|
|
|
void Bind() {
|
|
|
|
glBindTexture(target_, tex_);
|
|
|
|
}
|
2014-09-06 13:49:01 +02:00
|
|
|
|
2016-09-10 20:29:58 -07:00
|
|
|
void GLLost() override {
|
2016-03-17 21:30:16 +01:00
|
|
|
// We can assume that the texture is gone.
|
2016-02-14 22:07:10 +01:00
|
|
|
tex_ = 0;
|
2016-01-23 11:22:40 -08:00
|
|
|
generatedMips_ = false;
|
2016-09-10 20:29:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void GLRestore() override {
|
2014-09-06 13:49:01 +02:00
|
|
|
}
|
2016-03-17 21:30:16 +01:00
|
|
|
|
2016-12-27 22:26:49 +01:00
|
|
|
void Finalize() override;
|
2014-08-17 15:02:43 +02:00
|
|
|
|
|
|
|
private:
|
2014-08-17 21:28:34 +02:00
|
|
|
GLuint tex_;
|
2014-08-17 15:02:43 +02:00
|
|
|
GLuint target_;
|
2014-08-17 21:28:34 +02:00
|
|
|
|
2016-12-25 18:52:05 +01:00
|
|
|
DataFormat format_;
|
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
|
|
|
};
|
|
|
|
|
2016-12-25 21:21:56 +01:00
|
|
|
Texture *OpenGLContext::CreateTexture() {
|
2014-09-06 13:49:01 +02:00
|
|
|
return new Thin3DGLTexture();
|
|
|
|
}
|
|
|
|
|
2016-12-25 21:21:56 +01:00
|
|
|
Texture *OpenGLContext::CreateTexture(TextureType type, DataFormat format, int width, int height, int depth, int mipLevels) {
|
2014-08-22 20:47:40 +02:00
|
|
|
return new Thin3DGLTexture(type, format, width, height, depth, mipLevels);
|
2014-08-17 15:02:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Thin3DGLTexture::AutoGenMipmaps() {
|
2016-01-23 11:22:40 -08:00
|
|
|
if (!generatedMips_) {
|
|
|
|
Bind();
|
|
|
|
glGenerateMipmap(target_);
|
2016-02-13 13:37:00 -08:00
|
|
|
// TODO: Really, this should follow the sampler state.
|
2016-01-23 11:22:40 -08:00
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
|
|
|
|
generatedMips_ = true;
|
|
|
|
}
|
2014-08-17 15:02:43 +02:00
|
|
|
}
|
|
|
|
|
2014-08-22 20:47:40 +02:00
|
|
|
void Thin3DGLTexture::SetImageData(int x, int y, int z, int width, int height, int depth, int level, int stride, const uint8_t *data) {
|
2014-08-17 15:02:43 +02:00
|
|
|
int internalFormat;
|
|
|
|
int format;
|
|
|
|
int type;
|
|
|
|
switch (format_) {
|
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;
|
|
|
|
break;
|
2016-12-25 18:52:05 +01:00
|
|
|
case DataFormat::R4G4B4A4_UNORM:
|
2014-08-22 20:47:40 +02:00
|
|
|
internalFormat = GL_RGBA;
|
|
|
|
format = GL_RGBA;
|
|
|
|
type = GL_UNSIGNED_SHORT_4_4_4_4;
|
|
|
|
break;
|
2014-08-17 15:02:43 +02:00
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
2016-02-13 12:20:40 -08:00
|
|
|
if (level == 0) {
|
|
|
|
width_ = width;
|
|
|
|
height_ = height;
|
|
|
|
depth_ = depth;
|
|
|
|
}
|
2014-08-17 15:02:43 +02:00
|
|
|
|
2014-08-23 10:31:23 +02:00
|
|
|
Bind();
|
2014-08-17 15:02:43 +02:00
|
|
|
switch (target_) {
|
|
|
|
case GL_TEXTURE_2D:
|
2014-08-22 20:47:40 +02:00
|
|
|
glTexImage2D(GL_TEXTURE_2D, level, internalFormat, width_, height_, 0, format, type, data);
|
2014-08-17 15:02:43 +02:00
|
|
|
break;
|
2014-08-23 10:31:23 +02:00
|
|
|
default:
|
|
|
|
ELOG("Thin3D GL: Targets other than GL_TEXTURE_2D not yet supported");
|
|
|
|
break;
|
2014-08-17 15:02:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-17 21:28:34 +02:00
|
|
|
bool isPowerOf2(int n) {
|
|
|
|
return n == 1 || (n & (n - 1)) == 0;
|
|
|
|
}
|
|
|
|
|
2016-12-27 22:26:49 +01:00
|
|
|
void Thin3DGLTexture::Finalize() {
|
|
|
|
canWrap_ = !isPowerOf2(width_) || !isPowerOf2(height_);
|
2014-08-17 21:28:34 +02:00
|
|
|
}
|
|
|
|
|
2016-12-26 13:22:48 +01:00
|
|
|
OpenGLInputLayout::~OpenGLInputLayout() {
|
2015-12-13 09:25:22 -08:00
|
|
|
if (id_) {
|
|
|
|
glDeleteVertexArrays(1, &id_);
|
|
|
|
}
|
|
|
|
}
|
2016-12-26 13:22:48 +01:00
|
|
|
|
|
|
|
void OpenGLInputLayout::Compile() {
|
|
|
|
int semMask = 0;
|
|
|
|
for (int i = 0; i < (int)desc.attributes.size(); i++) {
|
|
|
|
semMask |= 1 << desc.attributes[i].location;
|
2014-08-17 12:16:57 +02:00
|
|
|
}
|
2016-12-26 13:22:48 +01:00
|
|
|
semanticsMask_ = semMask;
|
2015-12-13 09:25:22 -08:00
|
|
|
|
2015-12-22 22:14:20 -08:00
|
|
|
if (gl_extensions.ARB_vertex_array_object && gl_extensions.IsCoreContext) {
|
2015-12-13 09:25:22 -08:00
|
|
|
glGenVertexArrays(1, &id_);
|
|
|
|
} else {
|
|
|
|
id_ = 0;
|
|
|
|
}
|
|
|
|
needsEnable_ = true;
|
|
|
|
lastBase_ = -1;
|
2014-08-17 12:16:57 +02:00
|
|
|
}
|
|
|
|
|
2016-12-26 13:22:48 +01:00
|
|
|
void OpenGLInputLayout::GLLost() {
|
2016-09-10 20:29:58 -07:00
|
|
|
id_ = 0;
|
|
|
|
}
|
|
|
|
|
2016-12-26 13:22:48 +01:00
|
|
|
void OpenGLInputLayout::GLRestore() {
|
2015-12-13 21:23:53 -08:00
|
|
|
Compile();
|
|
|
|
}
|
|
|
|
|
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];
|
2015-01-04 17:59:58 +01:00
|
|
|
#ifndef USING_GLES2
|
2015-01-04 17:57:52 +01:00
|
|
|
bs->logicEnabled = desc.logicEnabled;
|
2016-12-25 18:52:05 +01:00
|
|
|
bs->logicOp = logicOpToGL[(int)desc.logicOp];
|
2015-01-04 17:59:58 +01:00
|
|
|
#endif
|
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();
|
|
|
|
samps->wrapS = texWrapToGL[(int)desc.wrapU];
|
|
|
|
samps->wrapT = texWrapToGL[(int)desc.wrapV];
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
return rs;
|
|
|
|
}
|
|
|
|
|
2016-12-25 21:21:56 +01:00
|
|
|
Buffer *OpenGLContext::CreateBuffer(size_t size, uint32_t usageFlags) {
|
|
|
|
return new OpenGLBuffer(size, usageFlags);
|
2014-08-17 12:16:57 +02: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");
|
2014-08-23 10:36:42 -07:00
|
|
|
return NULL;
|
2014-08-17 12:16:57 +02:00
|
|
|
}
|
2016-12-26 13:42:53 +01:00
|
|
|
OpenGLPipeline *pipeline = new OpenGLPipeline();
|
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();
|
2016-12-26 13:42:53 +01:00
|
|
|
return pipeline;
|
2014-08-17 12:16:57 +02:00
|
|
|
} else {
|
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) {
|
2014-08-17 21:28:34 +02:00
|
|
|
for (int i = start; i < start + count; i++) {
|
2014-08-17 15:02:43 +02:00
|
|
|
Thin3DGLTexture *glTex = static_cast<Thin3DGLTexture *>(textures[i]);
|
|
|
|
glActiveTexture(GL_TEXTURE0 + i);
|
2014-08-17 21:28:34 +02:00
|
|
|
glTex->Bind();
|
2016-02-13 13:37:00 -08:00
|
|
|
|
2016-04-10 10:34:39 +02:00
|
|
|
if ((int)samplerStates_.size() > i && samplerStates_[i]) {
|
2016-02-13 13:37:00 -08:00
|
|
|
samplerStates_[i]->Apply(glTex->HasMips(), glTex->CanWrap());
|
|
|
|
}
|
2014-08-17 15:02:43 +02:00
|
|
|
}
|
|
|
|
glActiveTexture(GL_TEXTURE0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-12-27 17:38:26 +01:00
|
|
|
ShaderModule *OpenGLContext::CreateShaderModule(ShaderStage stage, ShaderLanguage language, const uint8_t *data, size_t dataSize) {
|
2016-12-25 22:24:14 +01:00
|
|
|
OpenGLShaderModule *shader = new OpenGLShaderModule(stage);
|
2016-12-27 17:38:26 +01:00
|
|
|
if (shader->Compile(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() {
|
2014-09-06 13:49:01 +02:00
|
|
|
program_ = glCreateProgram();
|
2016-12-25 23:03:20 +01:00
|
|
|
for (auto iter : shaders) {
|
|
|
|
glAttachShader(program_, iter->GetShader());
|
|
|
|
}
|
2014-08-17 12:16:57 +02:00
|
|
|
|
|
|
|
// Bind all the common vertex data points. Mismatching ones will be ignored.
|
|
|
|
glBindAttribLocation(program_, SEM_POSITION, "Position");
|
|
|
|
glBindAttribLocation(program_, SEM_COLOR0, "Color0");
|
|
|
|
glBindAttribLocation(program_, SEM_TEXCOORD0, "TexCoord0");
|
|
|
|
glBindAttribLocation(program_, SEM_NORMAL, "Normal");
|
|
|
|
glBindAttribLocation(program_, SEM_TANGENT, "Tangent");
|
|
|
|
glBindAttribLocation(program_, SEM_BINORMAL, "Binormal");
|
|
|
|
glLinkProgram(program_);
|
|
|
|
|
|
|
|
GLint linkStatus = GL_FALSE;
|
|
|
|
glGetProgramiv(program_, GL_LINK_STATUS, &linkStatus);
|
|
|
|
if (linkStatus != GL_TRUE) {
|
|
|
|
GLint bufLength = 0;
|
|
|
|
glGetProgramiv(program_, GL_INFO_LOG_LENGTH, &bufLength);
|
|
|
|
if (bufLength) {
|
|
|
|
char* buf = new char[bufLength];
|
|
|
|
glGetProgramInfoLog(program_, bufLength, NULL, buf);
|
2014-08-23 10:31:23 +02:00
|
|
|
ELOG("Could not link program:\n %s", buf);
|
|
|
|
// We've thrown out the source at this point. Might want to do something about that.
|
|
|
|
#ifdef _WIN32
|
2014-08-17 12:16:57 +02:00
|
|
|
OutputDebugStringUTF8(buf);
|
|
|
|
#endif
|
2014-08-23 10:31:23 +02:00
|
|
|
delete[] buf;
|
2014-08-17 12:16:57 +02:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Auto-initialize samplers.
|
2014-08-23 00:34:55 -07:00
|
|
|
glUseProgram(program_);
|
2014-08-17 12:16:57 +02:00
|
|
|
for (int i = 0; i < 4; i++) {
|
|
|
|
char temp[256];
|
|
|
|
sprintf(temp, "Sampler%i", i);
|
|
|
|
int samplerLoc = GetUniformLoc(temp);
|
|
|
|
if (samplerLoc != -1) {
|
2014-08-23 00:34:55 -07:00
|
|
|
glUniform1i(samplerLoc, i);
|
2014-08-17 12:16:57 +02:00
|
|
|
}
|
|
|
|
}
|
2014-08-23 10:31:23 +02:00
|
|
|
|
2014-08-17 12:16:57 +02:00
|
|
|
// Here we could (using glGetAttribLocation) save a bitmask about which pieces of vertex data are used in the shader
|
|
|
|
// and then AND it with the vertex format bitmask later...
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-12-26 11:06:17 +01:00
|
|
|
int OpenGLPipeline::GetUniformLoc(const char *name) {
|
2014-08-17 12:16:57 +02:00
|
|
|
auto iter = uniforms_.find(name);
|
|
|
|
int loc = -1;
|
|
|
|
if (iter != uniforms_.end()) {
|
|
|
|
loc = iter->second.loc_;
|
|
|
|
} else {
|
|
|
|
loc = glGetUniformLocation(program_, name);
|
|
|
|
UniformInfo info;
|
|
|
|
info.loc_ = loc;
|
|
|
|
uniforms_[name] = info;
|
|
|
|
}
|
|
|
|
return loc;
|
|
|
|
}
|
|
|
|
|
2016-12-26 11:06:17 +01:00
|
|
|
void OpenGLPipeline::SetVector(const char *name, float *value, int n) {
|
2014-08-23 00:34:55 -07:00
|
|
|
glUseProgram(program_);
|
2014-08-17 12:16:57 +02:00
|
|
|
int loc = GetUniformLoc(name);
|
|
|
|
if (loc != -1) {
|
|
|
|
switch (n) {
|
2014-08-23 00:34:55 -07:00
|
|
|
case 1: glUniform1fv(loc, 1, value); break;
|
|
|
|
case 2: glUniform1fv(loc, 2, value); break;
|
|
|
|
case 3: glUniform1fv(loc, 3, value); break;
|
|
|
|
case 4: glUniform1fv(loc, 4, value); break;
|
2014-08-17 12:16:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-26 11:06:17 +01:00
|
|
|
void OpenGLPipeline::SetMatrix4x4(const char *name, const float value[16]) {
|
2014-08-23 00:34:55 -07:00
|
|
|
glUseProgram(program_);
|
2014-08-17 12:16:57 +02:00
|
|
|
int loc = GetUniformLoc(name);
|
|
|
|
if (loc != -1) {
|
2016-02-13 14:50:06 -08:00
|
|
|
glUniformMatrix4fv(loc, 1, false, value);
|
2014-08-17 12:16:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-26 11:06:17 +01:00
|
|
|
void OpenGLPipeline::Apply() {
|
2014-08-17 12:16:57 +02:00
|
|
|
glUseProgram(program_);
|
|
|
|
}
|
|
|
|
|
2016-12-26 11:06:17 +01:00
|
|
|
void OpenGLPipeline::Unapply() {
|
2014-08-17 12:16:57 +02:00
|
|
|
glUseProgram(0);
|
|
|
|
}
|
|
|
|
|
2016-12-26 17:03:01 +01:00
|
|
|
void OpenGLContext::BindPipeline(Pipeline *pipeline) {
|
|
|
|
curPipeline_ = (OpenGLPipeline *)pipeline;
|
|
|
|
curPipeline_->blend->Apply();
|
|
|
|
curPipeline_->depthStencil->Apply();
|
|
|
|
curPipeline_->raster->Apply();
|
|
|
|
}
|
|
|
|
|
|
|
|
void OpenGLContext::Draw(Buffer *vdata, int vertexCount, int offset) {
|
2016-12-25 21:21:56 +01:00
|
|
|
OpenGLBuffer *vbuf = static_cast<OpenGLBuffer *>(vdata);
|
2014-08-17 12:16:57 +02:00
|
|
|
|
|
|
|
vbuf->Bind();
|
2016-12-26 17:03:01 +01:00
|
|
|
curPipeline_->inputLayout->Apply();
|
2016-12-26 11:16:59 +01:00
|
|
|
curPipeline_->Apply();
|
2014-08-17 12:16:57 +02:00
|
|
|
|
2016-12-26 17:03:01 +01:00
|
|
|
glDrawArrays(curPipeline_->prim, offset, vertexCount);
|
2014-08-17 12:16:57 +02:00
|
|
|
|
2016-12-26 11:16:59 +01:00
|
|
|
curPipeline_->Unapply();
|
2016-12-26 17:03:01 +01:00
|
|
|
curPipeline_->inputLayout->Unapply();
|
2014-08-17 12:16:57 +02:00
|
|
|
}
|
|
|
|
|
2016-12-26 17:03:01 +01:00
|
|
|
void OpenGLContext::DrawIndexed(Buffer *vdata, Buffer *idata, int vertexCount, int offset) {
|
2016-12-25 21:21:56 +01:00
|
|
|
OpenGLBuffer *vbuf = static_cast<OpenGLBuffer *>(vdata);
|
|
|
|
OpenGLBuffer *ibuf = static_cast<OpenGLBuffer *>(idata);
|
2014-08-17 12:16:57 +02:00
|
|
|
|
|
|
|
vbuf->Bind();
|
2016-12-26 17:03:01 +01:00
|
|
|
curPipeline_->inputLayout->Apply();
|
2016-12-26 11:16:59 +01:00
|
|
|
curPipeline_->Apply();
|
2015-12-13 09:25:22 -08:00
|
|
|
// Note: ibuf binding is stored in the VAO, so call this after binding the fmt.
|
|
|
|
ibuf->Bind();
|
2016-02-13 12:20:22 -08:00
|
|
|
|
2016-12-26 17:03:01 +01:00
|
|
|
glDrawElements(curPipeline_->prim, vertexCount, GL_UNSIGNED_INT, (const void *)(size_t)offset);
|
2014-08-17 12:16:57 +02:00
|
|
|
|
2016-12-26 11:16:59 +01:00
|
|
|
curPipeline_->Unapply();
|
2016-12-26 17:03:01 +01:00
|
|
|
curPipeline_->inputLayout->Unapply();
|
2015-01-29 10:35:34 +01:00
|
|
|
}
|
|
|
|
|
2016-12-26 17:03:01 +01:00
|
|
|
void OpenGLContext::DrawUP(const void *vdata, int vertexCount) {
|
|
|
|
curPipeline_->inputLayout->Apply(vdata);
|
2016-12-26 11:16:59 +01:00
|
|
|
curPipeline_->Apply();
|
2015-01-29 10:35:34 +01:00
|
|
|
|
2016-02-13 12:21:12 -08:00
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
|
2016-12-26 17:03:01 +01:00
|
|
|
glDrawArrays(curPipeline_->prim, 0, vertexCount);
|
2015-01-29 10:35:34 +01:00
|
|
|
|
2016-12-26 11:16:59 +01:00
|
|
|
curPipeline_->Unapply();
|
2016-12-26 17:03:01 +01:00
|
|
|
curPipeline_->inputLayout->Unapply();
|
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;
|
2016-12-25 18:52:05 +01:00
|
|
|
if (mask & ClearFlag::COLOR) {
|
2014-08-17 12:16:57 +02:00
|
|
|
glClearColor(col[0], col[1], col[2], col[3]);
|
|
|
|
glMask |= GL_COLOR_BUFFER_BIT;
|
|
|
|
}
|
2016-12-25 18:52:05 +01:00
|
|
|
if (mask & ClearFlag::DEPTH) {
|
2015-09-06 12:21:15 +02:00
|
|
|
#if defined(USING_GLES2)
|
2014-08-23 00:34:55 -07:00
|
|
|
glClearDepthf(depthVal);
|
|
|
|
#else
|
2014-08-17 12:16:57 +02:00
|
|
|
glClearDepth(depthVal);
|
2014-08-23 00:34:55 -07:00
|
|
|
#endif
|
2014-08-17 12:16:57 +02:00
|
|
|
glMask |= GL_DEPTH_BUFFER_BIT;
|
|
|
|
}
|
2016-12-25 18:52:05 +01:00
|
|
|
if (mask & ClearFlag::STENCIL) {
|
2014-08-17 12:16:57 +02:00
|
|
|
glClearStencil(stencilVal);
|
|
|
|
glMask |= GL_STENCIL_BUFFER_BIT;
|
|
|
|
}
|
|
|
|
glClear(glMask);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-12-26 13:22:48 +01:00
|
|
|
void OpenGLInputLayout::Apply(const void *base) {
|
2015-12-13 09:25:22 -08:00
|
|
|
if (id_ != 0) {
|
|
|
|
glBindVertexArray(id_);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (needsEnable_ || id_ == 0) {
|
|
|
|
for (int i = 0; i < SEM_MAX; i++) {
|
|
|
|
if (semanticsMask_ & (1 << i)) {
|
|
|
|
glEnableVertexAttribArray(i);
|
|
|
|
}
|
2014-08-17 12:16:57 +02:00
|
|
|
}
|
2015-12-22 22:13:50 -08:00
|
|
|
if (id_ != 0) {
|
|
|
|
needsEnable_ = false;
|
|
|
|
}
|
2014-08-17 12:16:57 +02:00
|
|
|
}
|
2015-12-13 09:25:22 -08:00
|
|
|
|
2015-01-29 10:35:34 +01:00
|
|
|
intptr_t b = (intptr_t)base;
|
2015-12-13 09:25:22 -08:00
|
|
|
if (b != lastBase_) {
|
2016-12-26 13:22:48 +01:00
|
|
|
for (size_t i = 0; i < desc.attributes.size(); i++) {
|
2016-12-26 13:42:53 +01:00
|
|
|
GLsizei stride = (GLsizei)desc.bindings[desc.attributes[i].binding].stride;
|
2016-12-26 13:22:48 +01:00
|
|
|
switch (desc.attributes[i].format) {
|
2016-12-25 21:21:56 +01:00
|
|
|
case DataFormat::R32G32_FLOAT:
|
2016-12-26 13:22:48 +01:00
|
|
|
glVertexAttribPointer(desc.attributes[i].location, 2, GL_FLOAT, GL_FALSE, stride, (void *)(b + (intptr_t)desc.attributes[i].offset));
|
2015-12-13 09:25:22 -08:00
|
|
|
break;
|
2016-12-25 21:21:56 +01:00
|
|
|
case DataFormat::R32G32B32_FLOAT:
|
2016-12-26 13:22:48 +01:00
|
|
|
glVertexAttribPointer(desc.attributes[i].location, 3, GL_FLOAT, GL_FALSE, stride, (void *)(b + (intptr_t)desc.attributes[i].offset));
|
2015-12-13 09:25:22 -08:00
|
|
|
break;
|
2016-12-25 21:21:56 +01:00
|
|
|
case DataFormat::R32G32B32A32_FLOAT:
|
2016-12-26 13:22:48 +01:00
|
|
|
glVertexAttribPointer(desc.attributes[i].location, 4, GL_FLOAT, GL_FALSE, stride, (void *)(b + (intptr_t)desc.attributes[i].offset));
|
2015-12-13 09:25:22 -08:00
|
|
|
break;
|
2016-12-25 21:21:56 +01:00
|
|
|
case DataFormat::R8G8B8A8_UNORM:
|
2016-12-26 13:22:48 +01:00
|
|
|
glVertexAttribPointer(desc.attributes[i].location, 4, GL_UNSIGNED_BYTE, GL_TRUE, stride, (void *)(b + (intptr_t)desc.attributes[i].offset));
|
2015-12-13 09:25:22 -08:00
|
|
|
break;
|
2016-12-25 22:11:14 +01:00
|
|
|
case DataFormat::UNDEFINED:
|
2016-12-23 09:58:15 +01:00
|
|
|
default:
|
|
|
|
ELOG("Thin3DGLVertexFormat: Invalid or unknown component type applied.");
|
|
|
|
break;
|
2015-12-13 09:25:22 -08:00
|
|
|
}
|
2014-08-17 12:16:57 +02:00
|
|
|
}
|
2015-12-22 22:13:50 -08:00
|
|
|
if (id_ != 0) {
|
|
|
|
lastBase_ = b;
|
|
|
|
}
|
2014-08-17 12:16:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-26 13:22:48 +01:00
|
|
|
void OpenGLInputLayout::Unapply() {
|
2015-12-13 09:25:22 -08:00
|
|
|
if (id_ == 0) {
|
2016-12-25 18:52:05 +01:00
|
|
|
for (int i = 0; i < (int)SEM_MAX; i++) {
|
2015-12-13 09:25:22 -08:00
|
|
|
if (semanticsMask_ & (1 << i)) {
|
|
|
|
glDisableVertexAttribArray(i);
|
|
|
|
}
|
2014-08-17 12:16:57 +02:00
|
|
|
}
|
2015-12-13 09:25:22 -08:00
|
|
|
} else {
|
|
|
|
glBindVertexArray(0);
|
2014-08-17 12:16:57 +02:00
|
|
|
}
|
2014-11-23 11:01:51 -08:00
|
|
|
}
|
2016-12-25 18:18:19 +01:00
|
|
|
|
|
|
|
} // namespace Draw
|