lovr/src/graphics/opengl.c

2421 lines
77 KiB
C
Raw Normal View History

2018-07-17 10:35:01 +00:00
#include "graphics/graphics.h"
#include "graphics/canvas.h"
#include "graphics/mesh.h"
#include "graphics/shader.h"
#include "graphics/texture.h"
2018-07-17 06:51:11 +00:00
#include "resources/shaders.h"
#include "data/modelData.h"
#include "math/mat4.h"
#include "lib/vec/vec.h"
#include <math.h>
#include <limits.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
2018-07-17 22:05:30 +00:00
#if EMSCRIPTEN
#include <GLES3/gl3.h>
#include <GLES2/gl2ext.h>
#include <GL/gl.h>
#include <GL/glext.h>
2018-07-17 22:05:30 +00:00
#else
#include "lib/glad/glad.h"
#endif
2018-07-17 10:35:01 +00:00
// Types
2018-07-17 06:51:11 +00:00
#define MAX_TEXTURES 16
2018-08-10 10:37:02 +00:00
#define MAX_IMAGES 8
#define MAX_BLOCK_BUFFERS 8
2018-07-17 06:51:11 +00:00
#define LOVR_SHADER_POSITION 0
#define LOVR_SHADER_NORMAL 1
#define LOVR_SHADER_TEX_COORD 2
#define LOVR_SHADER_VERTEX_COLOR 3
#define LOVR_SHADER_TANGENT 4
#define LOVR_SHADER_BONES 5
#define LOVR_SHADER_BONE_WEIGHTS 6
2018-08-31 13:03:35 +00:00
typedef enum {
BARRIER_BLOCK,
BARRIER_UNIFORM_TEXTURE,
BARRIER_UNIFORM_IMAGE,
BARRIER_TEXTURE,
BARRIER_CANVAS,
MAX_BARRIERS
} Barrier;
2018-07-17 10:35:01 +00:00
static struct {
Texture* defaultTexture;
BlendMode blendMode;
BlendAlphaMode blendAlphaMode;
bool culling;
bool depthEnabled;
CompareMode depthTest;
bool depthWrite;
float lineWidth;
bool stencilEnabled;
CompareMode stencilMode;
int stencilValue;
bool stencilWriting;
Winding winding;
bool wireframe;
uint32_t framebuffer;
uint32_t indexBuffer;
uint32_t program;
2018-08-31 02:42:01 +00:00
int activeTexture;
2018-07-17 10:35:01 +00:00
Texture* textures[MAX_TEXTURES];
Image images[MAX_IMAGES];
uint32_t blockBuffers[2][MAX_BLOCK_BUFFERS];
2018-07-17 10:35:01 +00:00
uint32_t vertexArray;
uint32_t vertexBuffer;
2018-08-30 10:25:16 +00:00
float viewports[2][4];
vec_void_t incoherents[MAX_BARRIERS];
2018-07-17 10:35:01 +00:00
bool srgb;
2018-08-31 13:03:35 +00:00
GpuFeatures features;
GpuLimits limits;
GpuStats stats;
2018-07-17 10:35:01 +00:00
} state;
2018-07-17 06:51:11 +00:00
2018-07-20 23:56:28 +00:00
struct ShaderBlock {
Ref ref;
BlockType type;
2018-08-03 18:19:40 +00:00
BufferUsage usage;
vec_uniform_t uniforms;
map_int_t uniformMap;
2018-07-20 23:56:28 +00:00
uint32_t buffer;
GLenum target;
2018-07-17 06:51:11 +00:00
size_t size;
void* data;
bool mapped;
uint8_t incoherent;
2018-07-20 23:56:28 +00:00
};
2018-07-17 06:51:11 +00:00
struct Shader {
Ref ref;
ShaderType type;
2018-07-17 06:51:11 +00:00
uint32_t program;
vec_uniform_t uniforms;
vec_block_t blocks[2];
2018-07-17 06:51:11 +00:00
map_int_t attributes;
map_int_t uniformMap;
map_int_t blockMap;
2018-07-17 06:51:11 +00:00
};
struct Texture {
Ref ref;
TextureType type;
TextureFormat format;
2018-07-17 06:51:11 +00:00
int width;
int height;
int depth;
int mipmapCount;
2018-07-17 06:51:11 +00:00
GLuint id;
GLuint msaaId;
GLenum target;
2018-07-17 06:51:11 +00:00
TextureFilter filter;
TextureWrap wrap;
int msaa;
2018-07-17 06:51:11 +00:00
bool srgb;
bool mipmaps;
bool allocated;
uint8_t incoherent;
2018-07-17 06:51:11 +00:00
};
struct Canvas {
2018-08-20 03:48:45 +00:00
Ref ref;
2018-08-24 21:43:39 +00:00
int width;
int height;
CanvasFlags flags;
2018-08-20 03:48:45 +00:00
uint32_t framebuffer;
2018-08-24 21:43:39 +00:00
uint32_t depthBuffer;
uint32_t resolveBuffer;
2018-08-24 01:28:37 +00:00
Attachment attachments[MAX_CANVAS_ATTACHMENTS];
int count;
2018-08-29 19:40:19 +00:00
bool needsAttach;
bool needsResolve;
2018-07-17 06:51:11 +00:00
};
typedef struct {
Mesh* mesh;
int attributeIndex;
int divisor;
bool enabled;
} MeshAttachment;
typedef map_t(MeshAttachment) map_attachment_t;
struct Mesh {
Ref ref;
uint32_t count;
VertexFormat format;
MeshDrawMode drawMode;
GLenum usage;
VertexPointer data;
IndexPointer indices;
uint32_t indexCount;
size_t indexSize;
size_t indexCapacity;
bool mappedIndices;
uint32_t dirtyStart;
uint32_t dirtyEnd;
uint32_t rangeStart;
uint32_t rangeCount;
GLuint vao;
GLuint vbo;
GLuint ibo;
Material* material;
float* pose;
map_attachment_t attachments;
MeshAttachment layout[MAX_ATTACHMENTS];
bool isAttachment;
};
2018-07-17 10:35:01 +00:00
// Helper functions
2018-07-17 06:51:11 +00:00
static void gammaCorrectColor(Color* color) {
if (state.srgb) {
color->r = lovrMathGammaToLinear(color->r);
color->g = lovrMathGammaToLinear(color->g);
color->b = lovrMathGammaToLinear(color->b);
}
}
static GLenum convertCompareMode(CompareMode mode) {
switch (mode) {
case COMPARE_NONE: return GL_ALWAYS;
case COMPARE_EQUAL: return GL_EQUAL;
case COMPARE_NEQUAL: return GL_NOTEQUAL;
case COMPARE_LESS: return GL_LESS;
case COMPARE_LEQUAL: return GL_LEQUAL;
case COMPARE_GREATER: return GL_GREATER;
case COMPARE_GEQUAL: return GL_GEQUAL;
}
}
2018-07-17 10:35:01 +00:00
static GLenum convertWrapMode(WrapMode mode) {
2018-07-17 06:51:11 +00:00
switch (mode) {
case WRAP_CLAMP: return GL_CLAMP_TO_EDGE;
case WRAP_REPEAT: return GL_REPEAT;
case WRAP_MIRRORED_REPEAT: return GL_MIRRORED_REPEAT;
}
}
2018-09-09 05:28:30 +00:00
static GLenum convertTextureTarget(TextureType type) {
switch (type) {
case TEXTURE_2D: return GL_TEXTURE_2D; break;
case TEXTURE_ARRAY: return GL_TEXTURE_2D_ARRAY; break;
case TEXTURE_CUBE: return GL_TEXTURE_CUBE_MAP; break;
case TEXTURE_VOLUME: return GL_TEXTURE_3D; break;
}
}
2018-07-17 10:35:01 +00:00
static GLenum convertTextureFormat(TextureFormat format) {
2018-07-17 06:51:11 +00:00
switch (format) {
case FORMAT_RGB: return GL_RGB;
case FORMAT_RGBA: return GL_RGBA;
2018-07-21 00:45:25 +00:00
case FORMAT_RGBA4: return GL_RGBA;
2018-07-17 06:51:11 +00:00
case FORMAT_RGBA16F: return GL_RGBA;
case FORMAT_RGBA32F: return GL_RGBA;
2018-07-21 00:45:25 +00:00
case FORMAT_R16F: return GL_RED;
case FORMAT_R32F: return GL_RED;
2018-08-16 21:54:37 +00:00
case FORMAT_RG16F: return GL_RG;
case FORMAT_RG32F: return GL_RG;
2018-07-21 00:45:25 +00:00
case FORMAT_RGB5A1: return GL_RGBA;
case FORMAT_RGB10A2: return GL_RGBA;
2018-07-17 06:51:11 +00:00
case FORMAT_RG11B10F: return GL_RGB;
case FORMAT_DXT1: return GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
case FORMAT_DXT3: return GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
case FORMAT_DXT5: return GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
}
}
2018-07-17 10:35:01 +00:00
static GLenum convertTextureFormatInternal(TextureFormat format, bool srgb) {
2018-07-17 06:51:11 +00:00
switch (format) {
case FORMAT_RGB: return srgb ? GL_SRGB8 : GL_RGB8;
case FORMAT_RGBA: return srgb ? GL_SRGB8_ALPHA8 : GL_RGBA8;
2018-07-21 00:45:25 +00:00
case FORMAT_RGBA4: return GL_RGBA4;
2018-07-17 06:51:11 +00:00
case FORMAT_RGBA16F: return GL_RGBA16F;
case FORMAT_RGBA32F: return GL_RGBA32F;
2018-07-21 00:45:25 +00:00
case FORMAT_R16F: return GL_R16F;
case FORMAT_R32F: return GL_R32F;
2018-08-16 21:54:37 +00:00
case FORMAT_RG16F: return GL_RG16F;
case FORMAT_RG32F: return GL_RG32F;
2018-07-21 00:45:25 +00:00
case FORMAT_RGB5A1: return GL_RGB5_A1;
case FORMAT_RGB10A2: return GL_RGB10_A2;
2018-07-17 06:51:11 +00:00
case FORMAT_RG11B10F: return GL_R11F_G11F_B10F;
case FORMAT_DXT1: return srgb ? GL_COMPRESSED_SRGB_S3TC_DXT1_EXT : GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
case FORMAT_DXT3: return srgb ? GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT : GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
case FORMAT_DXT5: return srgb ? GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT : GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
}
}
static GLenum convertTextureFormatType(TextureFormat format) {
switch (format) {
case FORMAT_RGB: return GL_UNSIGNED_BYTE;
case FORMAT_RGBA: return GL_UNSIGNED_BYTE;
case FORMAT_RGBA4: return GL_UNSIGNED_SHORT_4_4_4_4;
case FORMAT_RGBA16F: return GL_HALF_FLOAT;
case FORMAT_RGBA32F: return GL_FLOAT;
case FORMAT_R16F: return GL_HALF_FLOAT;
case FORMAT_R32F: return GL_FLOAT;
2018-08-16 21:54:37 +00:00
case FORMAT_RG16F: return GL_HALF_FLOAT;
case FORMAT_RG32F: return GL_FLOAT;
case FORMAT_RGB5A1: return GL_UNSIGNED_SHORT_5_5_5_1;
case FORMAT_RGB10A2: return GL_UNSIGNED_INT_2_10_10_10_REV;
case FORMAT_RG11B10F: return GL_UNSIGNED_INT_10F_11F_11F_REV;
case FORMAT_DXT1:
case FORMAT_DXT3:
case FORMAT_DXT5:
lovrThrow("Unreachable");
return GL_UNSIGNED_BYTE;
}
}
2018-08-24 21:43:39 +00:00
static GLenum convertDepthFormat(DepthFormat format) {
switch (format) {
case DEPTH_D16: return GL_DEPTH_COMPONENT16; break;
2018-08-31 03:56:08 +00:00
case DEPTH_D32F: return GL_DEPTH_COMPONENT32F; break;
2018-08-24 21:43:39 +00:00
case DEPTH_D24S8: return GL_DEPTH24_STENCIL8; break;
default: lovrThrow("Unreachable"); return GL_DEPTH_COMPONENT16;
}
}
2018-07-17 10:35:01 +00:00
static bool isTextureFormatCompressed(TextureFormat format) {
2018-07-17 06:51:11 +00:00
switch (format) {
2018-08-31 02:40:31 +00:00
case FORMAT_DXT1: case FORMAT_DXT3: case FORMAT_DXT5: return true;
default: return false;
2018-07-17 06:51:11 +00:00
}
}
static GLenum convertBufferUsage(BufferUsage usage) {
2018-07-17 10:35:01 +00:00
switch (usage) {
case USAGE_STATIC: return GL_STATIC_DRAW;
case USAGE_DYNAMIC: return GL_DYNAMIC_DRAW;
case USAGE_STREAM: return GL_STREAM_DRAW;
2018-07-17 06:51:11 +00:00
}
2018-07-17 10:35:01 +00:00
}
2018-07-17 06:51:11 +00:00
2018-08-31 03:56:08 +00:00
#ifndef EMSCRIPTEN
static GLenum convertAccess(UniformAccess access) {
switch (access) {
case ACCESS_READ: return GL_READ_ONLY;
case ACCESS_WRITE: return GL_WRITE_ONLY;
case ACCESS_READ_WRITE: return GL_READ_WRITE;
}
}
2018-08-31 03:56:08 +00:00
#endif
2018-07-17 10:35:01 +00:00
static GLenum convertMeshDrawMode(MeshDrawMode mode) {
switch (mode) {
case MESH_POINTS: return GL_POINTS;
case MESH_LINES: return GL_LINES;
case MESH_LINE_STRIP: return GL_LINE_STRIP;
case MESH_LINE_LOOP: return GL_LINE_LOOP;
case MESH_TRIANGLE_STRIP: return GL_TRIANGLE_STRIP;
case MESH_TRIANGLES: return GL_TRIANGLES;
case MESH_TRIANGLE_FAN: return GL_TRIANGLE_FAN;
2018-07-17 06:51:11 +00:00
}
2018-07-17 10:35:01 +00:00
}
2018-07-17 06:51:11 +00:00
2018-07-17 10:35:01 +00:00
static UniformType getUniformType(GLenum type, const char* debug) {
switch (type) {
case GL_FLOAT:
case GL_FLOAT_VEC2:
case GL_FLOAT_VEC3:
case GL_FLOAT_VEC4:
return UNIFORM_FLOAT;
case GL_INT:
case GL_INT_VEC2:
case GL_INT_VEC3:
case GL_INT_VEC4:
return UNIFORM_INT;
case GL_FLOAT_MAT2:
case GL_FLOAT_MAT3:
case GL_FLOAT_MAT4:
return UNIFORM_MATRIX;
case GL_SAMPLER_2D:
case GL_SAMPLER_3D:
case GL_SAMPLER_CUBE:
case GL_SAMPLER_2D_ARRAY:
return UNIFORM_SAMPLER;
2018-08-10 10:37:02 +00:00
#ifdef GL_ARB_shader_image_load_store
case GL_IMAGE_2D:
case GL_IMAGE_3D:
case GL_IMAGE_CUBE:
case GL_IMAGE_2D_ARRAY:
return UNIFORM_IMAGE;
2018-08-10 10:37:02 +00:00
#endif
2018-07-17 10:35:01 +00:00
default:
2018-08-03 18:19:40 +00:00
lovrThrow("Unsupported uniform type for uniform '%s'", debug);
2018-07-17 10:35:01 +00:00
return UNIFORM_FLOAT;
}
}
2018-07-17 06:51:11 +00:00
2018-07-17 10:35:01 +00:00
static int getUniformComponents(GLenum type) {
2018-07-17 06:51:11 +00:00
switch (type) {
2018-08-31 02:40:31 +00:00
case GL_FLOAT_VEC2: case GL_INT_VEC2: case GL_FLOAT_MAT2: return 2;
case GL_FLOAT_VEC3: case GL_INT_VEC3: case GL_FLOAT_MAT3: return 3;
case GL_FLOAT_VEC4: case GL_INT_VEC4: case GL_FLOAT_MAT4: return 4;
default: return 1;
2018-07-17 06:51:11 +00:00
}
2018-07-17 10:35:01 +00:00
}
2018-07-17 06:51:11 +00:00
static TextureType getUniformTextureType(GLenum type) {
switch (type) {
case GL_SAMPLER_2D: return TEXTURE_2D;
case GL_SAMPLER_3D: return TEXTURE_VOLUME;
case GL_SAMPLER_CUBE: return TEXTURE_CUBE;
case GL_SAMPLER_2D_ARRAY: return TEXTURE_ARRAY;
#ifdef GL_ARB_shader_image_load_store
case GL_IMAGE_2D: return TEXTURE_2D;
case GL_IMAGE_3D: return TEXTURE_VOLUME;
case GL_IMAGE_CUBE: return TEXTURE_CUBE;
case GL_IMAGE_2D_ARRAY: return TEXTURE_ARRAY;
#endif
default: return -1;
}
}
2018-08-06 18:46:42 +00:00
static size_t getUniformTypeLength(const Uniform* uniform) {
size_t size = 0;
if (uniform->count > 1) {
size += 2 + floor(log10(uniform->count)) + 1; // "[count]"
}
switch (uniform->type) {
case UNIFORM_MATRIX: size += 4; break;
2018-08-06 18:46:42 +00:00
case UNIFORM_FLOAT: size += uniform->components == 1 ? 5 : 4; break;
case UNIFORM_INT: size += uniform->components == 1 ? 3 : 5; break;
default: break;
}
return size;
}
static const char* getUniformTypeName(const Uniform* uniform) {
switch (uniform->type) {
case UNIFORM_FLOAT:
switch (uniform->components) {
case 1: return "float";
case 2: return "vec2";
case 3: return "vec3";
case 4: return "vec4";
}
break;
case UNIFORM_INT:
switch (uniform->components) {
case 1: return "int";
case 2: return "ivec2";
case 3: return "ivec3";
case 4: return "ivec4";
}
break;
case UNIFORM_MATRIX:
switch (uniform->components) {
case 2: return "mat2";
case 3: return "mat3";
case 4: return "mat4";
}
break;
default: break;
}
lovrThrow("Unreachable");
return "";
}
// TODO really ought to have TextureType-specific default textures
2018-08-10 10:37:02 +00:00
static Texture* lovrGpuGetDefaultTexture() {
if (!state.defaultTexture) {
2018-08-21 01:14:33 +00:00
TextureData* textureData = lovrTextureDataCreate(1, 1, 0xff, FORMAT_RGBA);
state.defaultTexture = lovrTextureCreate(TEXTURE_2D, &textureData, 1, true, false, 0);
2018-08-10 10:37:02 +00:00
lovrRelease(textureData);
}
return state.defaultTexture;
}
2018-08-31 13:03:35 +00:00
// Syncing resources is only relevant for compute shaders
#ifndef EMSCRIPTEN
static void lovrGpuSync(uint8_t flags) {
if (!flags) {
return;
}
GLbitfield bits = 0;
for (int i = 0; i < MAX_BARRIERS; i++) {
if (!((flags >> i) & 1)) {
continue;
}
if (state.incoherents[i].length == 0) {
flags &= ~(1 << i);
continue;
}
if (i == BARRIER_BLOCK) {
for (int j = 0; j < state.incoherents[i].length; j++) {
2018-08-31 13:03:35 +00:00
ShaderBlock* block = state.incoherents[i].data[j];
block->incoherent &= ~(1 << i);
}
} else {
for (int j = 0; j < state.incoherents[i].length; j++) {
2018-08-31 13:03:35 +00:00
Texture* texture = state.incoherents[i].data[j];
texture->incoherent &= ~(1 << i);
}
}
vec_clear(&state.incoherents[i]);
2018-08-31 13:03:35 +00:00
switch (i) {
case BARRIER_BLOCK: bits |= GL_SHADER_STORAGE_BARRIER_BIT; break;
case BARRIER_UNIFORM_IMAGE: bits |= GL_SHADER_IMAGE_ACCESS_BARRIER_BIT; break;
case BARRIER_UNIFORM_TEXTURE: bits |= GL_TEXTURE_FETCH_BARRIER_BIT; break;
case BARRIER_TEXTURE: bits |= GL_TEXTURE_UPDATE_BARRIER_BIT; break;
case BARRIER_CANVAS: bits |= GL_FRAMEBUFFER_BARRIER_BIT; break;
}
}
if (bits) {
glMemoryBarrier(bits);
}
}
#endif
static void lovrGpuDestroySyncResource(void* resource, uint8_t incoherent) {
if (!incoherent) {
return;
}
for (int i = 0; i < MAX_BARRIERS; i++) {
if (incoherent & (1 << i)) {
for (int j = 0; j < state.incoherents[i].length; j++) {
if (state.incoherents[i].data[j] == resource) {
vec_swapsplice(&state.incoherents[i], j, 1);
break;
}
}
}
}
}
2018-08-24 01:28:37 +00:00
static void lovrGpuBindFramebuffer(uint32_t framebuffer) {
if (state.framebuffer != framebuffer) {
state.framebuffer = framebuffer;
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
}
}
2018-07-17 10:35:01 +00:00
static void lovrGpuBindIndexBuffer(uint32_t indexBuffer) {
if (state.indexBuffer != indexBuffer) {
state.indexBuffer = indexBuffer;
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
}
}
2018-07-17 06:51:11 +00:00
2018-08-10 10:37:02 +00:00
static void lovrGpuBindTexture(Texture* texture, int slot) {
2018-07-17 10:35:01 +00:00
lovrAssert(slot >= 0 && slot < MAX_TEXTURES, "Invalid texture slot %d", slot);
2018-08-10 10:37:02 +00:00
texture = texture ? texture : lovrGpuGetDefaultTexture();
2018-07-17 06:51:11 +00:00
2018-07-17 10:35:01 +00:00
if (texture != state.textures[slot]) {
lovrRetain(texture);
lovrRelease(state.textures[slot]);
state.textures[slot] = texture;
2018-08-31 02:42:01 +00:00
if (state.activeTexture != slot) {
glActiveTexture(GL_TEXTURE0 + slot);
state.activeTexture = slot;
}
2018-08-10 10:37:02 +00:00
glBindTexture(texture->target, texture->id);
2018-07-17 06:51:11 +00:00
}
}
2018-08-31 13:03:35 +00:00
#ifndef EMSCRIPTEN
static void lovrGpuBindImage(Image* image, int slot) {
2018-08-10 10:37:02 +00:00
lovrAssert(slot >= 0 && slot < MAX_IMAGES, "Invalid image slot %d", slot);
// This is a risky way to compare the two structs
if (memcmp(state.images + slot, image, sizeof(Image))) {
Texture* texture = image->texture ? image->texture : lovrGpuGetDefaultTexture();
2018-08-11 07:02:30 +00:00
lovrAssert(!texture->srgb, "sRGB textures can not be used as image uniforms");
lovrAssert(!isTextureFormatCompressed(texture->format), "Compressed textures can not be used as image uniforms");
lovrAssert(texture->format != FORMAT_RGB && texture->format != FORMAT_RGBA4 && texture->format != FORMAT_RGB5A1, "Unsupported texture format for image uniform");
lovrAssert(image->mipmap >= 0 && image->mipmap < texture->mipmapCount, "Invalid mipmap level '%d' for image uniform", image->mipmap);
lovrAssert(image->slice < texture->depth, "Invalid texture slice '%d' for image uniform", image->slice);
GLenum glAccess = convertAccess(image->access);
GLenum glFormat = convertTextureFormatInternal(texture->format, false);
2018-09-04 13:58:54 +00:00
bool layered = image->slice == -1;
int slice = layered ? 0 : image->slice;
2018-08-10 10:37:02 +00:00
lovrRetain(texture);
lovrRelease(state.images[slot].texture);
2018-09-04 13:58:54 +00:00
glBindImageTexture(slot, texture->id, image->mipmap, layered, slice, glAccess, glFormat);
memcpy(state.images + slot, image, sizeof(Image));
2018-08-10 10:37:02 +00:00
}
}
2018-08-31 13:03:35 +00:00
#endif
2018-08-10 10:37:02 +00:00
static void lovrGpuBindBlockBuffer(BlockType type, uint32_t buffer, int slot) {
2018-08-31 03:56:08 +00:00
#ifdef EMSCRIPTEN
lovrAssert(type == BLOCK_UNIFORM, "Writable ShaderBlocks are not supported on this system");
GLenum target = GL_UNIFORM_BUFFER;
2018-08-31 03:56:08 +00:00
#else
GLenum target = type == BLOCK_UNIFORM ? GL_UNIFORM_BUFFER : GL_SHADER_STORAGE_BUFFER;
#endif
if (state.blockBuffers[type][slot] != buffer) {
state.blockBuffers[type][slot] = buffer;
2018-08-31 03:56:08 +00:00
glBindBufferBase(target, slot, buffer);
2018-07-20 23:56:28 +00:00
}
}
2018-07-17 10:35:01 +00:00
static void lovrGpuBindVertexArray(uint32_t vertexArray) {
if (state.vertexArray != vertexArray) {
state.vertexArray = vertexArray;
glBindVertexArray(vertexArray);
}
2018-07-17 06:51:11 +00:00
}
2018-07-17 10:35:01 +00:00
static void lovrGpuBindVertexBuffer(uint32_t vertexBuffer) {
if (state.vertexBuffer != vertexBuffer) {
state.vertexBuffer = vertexBuffer;
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
}
2018-07-17 06:51:11 +00:00
}
2018-07-17 10:35:01 +00:00
static void lovrGpuUseProgram(uint32_t program) {
if (state.program != program) {
state.program = program;
glUseProgram(program);
state.stats.shaderSwitches++;
}
2018-07-17 06:51:11 +00:00
}
2018-08-31 13:03:35 +00:00
// GPU
2018-08-29 04:16:19 +00:00
void lovrGpuInit(bool srgb, gpuProc (*getProcAddress)(const char*)) {
2018-07-17 10:35:01 +00:00
#ifndef EMSCRIPTEN
gladLoadGLLoader((GLADloadproc) getProcAddress);
2018-08-31 13:03:35 +00:00
state.features.computeShaders = GLAD_GL_ARB_compute_shader;
state.features.singlepass = GLAD_GL_ARB_viewport_array && GLAD_GL_AMD_vertex_shader_viewport_index && GLAD_GL_ARB_fragment_layer_viewport;
2018-07-17 10:35:01 +00:00
glEnable(GL_LINE_SMOOTH);
glEnable(GL_PROGRAM_POINT_SIZE);
if (srgb) {
glEnable(GL_FRAMEBUFFER_SRGB);
2018-07-17 06:51:11 +00:00
} else {
2018-07-17 10:35:01 +00:00
glDisable(GL_FRAMEBUFFER_SRGB);
2018-07-17 06:51:11 +00:00
}
2018-08-31 13:03:35 +00:00
glGetFloatv(GL_POINT_SIZE_RANGE, state.limits.pointSizes);
#else
glGetFloatv(GL_ALIASED_POINT_SIZE_RANGE, state.limits.pointSizes);
2018-07-17 10:35:01 +00:00
#endif
2018-08-31 13:03:35 +00:00
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &state.limits.textureSize);
glGetIntegerv(GL_MAX_SAMPLES, &state.limits.textureMSAA);
glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &state.limits.textureAnisotropy);
2018-07-17 10:35:01 +00:00
glEnable(GL_BLEND);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
state.srgb = srgb;
state.blendMode = BLEND_ALPHA;
state.blendAlphaMode = BLEND_ALPHA_MULTIPLY;
glBlendEquation(GL_FUNC_ADD);
glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
2018-07-17 21:53:21 +00:00
state.culling = false;
glDisable(GL_CULL_FACE);
state.depthEnabled = true;
state.depthTest = COMPARE_LEQUAL;
2018-07-17 21:53:21 +00:00
state.depthWrite = true;
glEnable(GL_DEPTH_TEST);
glDepthFunc(convertCompareMode(state.depthTest));
glDepthMask(state.depthWrite);
2018-07-17 21:53:21 +00:00
state.lineWidth = 1;
glLineWidth(state.lineWidth);
2018-07-17 21:53:21 +00:00
state.stencilEnabled = false;
state.stencilMode = COMPARE_NONE;
state.stencilValue = 0;
2018-07-17 10:35:01 +00:00
state.stencilWriting = false;
glDisable(GL_STENCIL_TEST);
2018-07-17 21:53:21 +00:00
state.winding = WINDING_COUNTERCLOCKWISE;
glFrontFace(GL_CCW);
2018-07-17 21:53:21 +00:00
state.wireframe = false;
#ifndef EMSCRIPTEN
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
#endif
for (int i = 0; i < MAX_BARRIERS; i++) {
vec_init(&state.incoherents[i]);
}
2018-07-17 10:35:01 +00:00
}
2018-07-17 06:51:11 +00:00
2018-07-17 10:35:01 +00:00
void lovrGpuDestroy() {
lovrRelease(state.defaultTexture);
for (int i = 0; i < MAX_TEXTURES; i++) {
lovrRelease(state.textures[i]);
2018-07-17 06:51:11 +00:00
}
for (int i = 0; i < MAX_IMAGES; i++) {
lovrRelease(state.images[i].texture);
}
for (int i = 0; i < MAX_BARRIERS; i++) {
vec_deinit(&state.incoherents[i]);
}
memset(&state, 0, sizeof(state));
2018-07-17 10:35:01 +00:00
}
2018-07-17 06:51:11 +00:00
2018-08-31 13:03:35 +00:00
void lovrGpuBindPipeline(Pipeline* pipeline) {
2018-07-17 10:35:01 +00:00
// Blend mode
if (state.blendMode != pipeline->blendMode || state.blendAlphaMode != pipeline->blendAlphaMode) {
state.blendMode = pipeline->blendMode;
state.blendAlphaMode = pipeline->blendAlphaMode;
2018-07-17 06:51:11 +00:00
2018-07-17 10:35:01 +00:00
GLenum srcRGB = state.blendMode == BLEND_MULTIPLY ? GL_DST_COLOR : GL_ONE;
if (srcRGB == GL_ONE && state.blendAlphaMode == BLEND_ALPHA_MULTIPLY) {
srcRGB = GL_SRC_ALPHA;
}
2018-07-17 06:51:11 +00:00
2018-07-17 10:35:01 +00:00
switch (state.blendMode) {
case BLEND_ALPHA:
glBlendEquation(GL_FUNC_ADD);
glBlendFuncSeparate(srcRGB, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
break;
2018-07-17 06:51:11 +00:00
2018-07-17 10:35:01 +00:00
case BLEND_ADD:
glBlendEquation(GL_FUNC_ADD);
glBlendFuncSeparate(srcRGB, GL_ONE, GL_ZERO, GL_ONE);
break;
2018-07-17 06:51:11 +00:00
2018-07-17 10:35:01 +00:00
case BLEND_SUBTRACT:
glBlendEquation(GL_FUNC_REVERSE_SUBTRACT);
glBlendFuncSeparate(srcRGB, GL_ONE, GL_ZERO, GL_ONE);
break;
case BLEND_MULTIPLY:
glBlendEquation(GL_FUNC_ADD);
glBlendFuncSeparate(srcRGB, GL_ZERO, GL_DST_COLOR, GL_ZERO);
break;
case BLEND_LIGHTEN:
glBlendEquation(GL_MAX);
glBlendFuncSeparate(srcRGB, GL_ZERO, GL_ONE, GL_ZERO);
break;
case BLEND_DARKEN:
glBlendEquation(GL_MIN);
glBlendFuncSeparate(srcRGB, GL_ZERO, GL_ONE, GL_ZERO);
break;
case BLEND_SCREEN:
glBlendEquation(GL_FUNC_ADD);
glBlendFuncSeparate(srcRGB, GL_ONE_MINUS_SRC_COLOR, GL_ONE, GL_ONE_MINUS_SRC_COLOR);
break;
case BLEND_REPLACE:
glBlendEquation(GL_FUNC_ADD);
glBlendFuncSeparate(srcRGB, GL_ZERO, GL_ONE, GL_ZERO);
break;
}
2018-07-17 06:51:11 +00:00
}
2018-07-17 10:35:01 +00:00
// Culling
if (state.culling != pipeline->culling) {
state.culling = pipeline->culling;
if (state.culling) {
glEnable(GL_CULL_FACE);
2018-07-17 06:51:11 +00:00
} else {
2018-07-17 10:35:01 +00:00
glDisable(GL_CULL_FACE);
2018-07-17 06:51:11 +00:00
}
2018-07-17 10:35:01 +00:00
}
2018-07-17 06:51:11 +00:00
2018-07-17 10:35:01 +00:00
// Depth test
if (state.depthTest != pipeline->depthTest) {
state.depthTest = pipeline->depthTest;
if (state.depthTest != COMPARE_NONE) {
if (!state.depthEnabled) {
state.depthEnabled = true;
glEnable(GL_DEPTH_TEST);
}
glDepthFunc(convertCompareMode(state.depthTest));
} else if (state.depthEnabled) {
state.depthEnabled = false;
glDisable(GL_DEPTH_TEST);
2018-07-17 06:51:11 +00:00
}
2018-07-17 10:35:01 +00:00
}
2018-07-17 06:51:11 +00:00
2018-07-17 10:35:01 +00:00
// Depth write
if (state.depthWrite != pipeline->depthWrite) {
state.depthWrite = pipeline->depthWrite;
glDepthMask(state.depthWrite);
2018-07-17 06:51:11 +00:00
}
2018-07-17 10:35:01 +00:00
// Line width
if (state.lineWidth != pipeline->lineWidth) {
2018-07-17 21:53:21 +00:00
state.lineWidth = pipeline->lineWidth;
2018-07-17 10:35:01 +00:00
glLineWidth(state.lineWidth);
2018-07-17 06:51:11 +00:00
}
2018-07-17 10:35:01 +00:00
// Stencil mode
if (!state.stencilWriting && (state.stencilMode != pipeline->stencilMode || state.stencilValue != pipeline->stencilValue)) {
state.stencilMode = pipeline->stencilMode;
state.stencilValue = pipeline->stencilValue;
if (state.stencilMode != COMPARE_NONE) {
if (!state.stencilEnabled) {
state.stencilEnabled = true;
glEnable(GL_STENCIL_TEST);
}
2018-07-17 06:51:11 +00:00
2018-07-17 10:35:01 +00:00
GLenum glMode = GL_ALWAYS;
switch (state.stencilMode) {
case COMPARE_EQUAL: glMode = GL_EQUAL; break;
case COMPARE_NEQUAL: glMode = GL_NOTEQUAL; break;
case COMPARE_LESS: glMode = GL_GREATER; break;
case COMPARE_LEQUAL: glMode = GL_GEQUAL; break;
case COMPARE_GREATER: glMode = GL_LESS; break;
case COMPARE_GEQUAL: glMode = GL_LEQUAL; break;
default: break;
}
2018-07-17 06:51:11 +00:00
2018-07-17 10:35:01 +00:00
glStencilFunc(glMode, state.stencilValue, 0xff);
glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
} else if (state.stencilEnabled) {
state.stencilEnabled = false;
glDisable(GL_STENCIL_TEST);
}
2018-07-17 06:51:11 +00:00
}
2018-07-17 10:35:01 +00:00
// Winding
if (state.winding != pipeline->winding) {
state.winding = pipeline->winding;
glFrontFace(state.winding == WINDING_CLOCKWISE ? GL_CW : GL_CCW);
}
2018-07-17 06:51:11 +00:00
2018-07-17 10:35:01 +00:00
// Wireframe
2018-08-31 02:40:31 +00:00
#ifndef EMSCRIPTEN
2018-07-17 10:35:01 +00:00
if (state.wireframe != pipeline->wireframe) {
state.wireframe = pipeline->wireframe;
glPolygonMode(GL_FRONT_AND_BACK, state.wireframe ? GL_LINE : GL_FILL);
2018-07-17 06:51:11 +00:00
}
2018-08-31 02:40:31 +00:00
#endif
2018-08-31 13:03:35 +00:00
}
2018-07-17 06:51:11 +00:00
2018-08-31 13:03:35 +00:00
void lovrGpuSetViewports(float* viewport, int count) {
#ifndef EMSCRIPTEN
if (count > 1) {
if (memcmp(state.viewports, viewport, count * 4 * sizeof(float))) {
memcpy(state.viewports, viewport, count * 4 * sizeof(float));
glViewportArrayv(0, count, viewport);
2018-07-17 06:51:11 +00:00
}
2018-07-17 10:35:01 +00:00
} else {
2018-08-31 13:03:35 +00:00
#endif
if (memcmp(state.viewports, viewport, 4 * sizeof(float))) {
memcpy(state.viewports, viewport, 4 * sizeof(float));
glViewport(viewport[0], viewport[1], viewport[2], viewport[3]);
}
#ifndef EMSCRIPTEN
2018-07-17 06:51:11 +00:00
}
2018-08-31 13:03:35 +00:00
#endif
}
2018-07-17 06:51:11 +00:00
2018-08-31 13:03:35 +00:00
void lovrGpuClear(Canvas* canvas, Color* color, float* depth, int* stencil) {
lovrCanvasBind(canvas, true);
2018-07-17 06:51:11 +00:00
2018-08-31 13:03:35 +00:00
if (color) {
gammaCorrectColor(color);
int count = canvas ? canvas->count : 1;
for (int i = 0; i < count; i++) {
glClearBufferfv(GL_COLOR, i, (float[]) { color->r, color->g, color->b, color->a });
}
2018-07-17 10:35:01 +00:00
}
2018-07-17 06:51:11 +00:00
2018-08-31 13:03:35 +00:00
if (depth && !state.depthWrite) {
state.depthWrite = true;
glDepthMask(state.depthWrite);
2018-07-17 10:35:01 +00:00
}
2018-07-17 06:51:11 +00:00
2018-08-31 13:03:35 +00:00
if (depth && stencil) {
glClearBufferfi(GL_DEPTH_STENCIL, 0, *depth, *stencil);
} else if (depth) {
glClearBufferfv(GL_DEPTH, 0, depth);
} else if (stencil) {
glClearBufferiv(GL_STENCIL, 0, stencil);
2018-07-17 10:35:01 +00:00
}
2018-08-31 13:03:35 +00:00
}
2018-07-17 06:51:11 +00:00
2018-08-31 13:03:35 +00:00
void lovrGpuStencil(StencilAction action, int replaceValue, StencilCallback callback, void* userdata) {
state.depthWrite = false;
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
2018-07-17 06:51:11 +00:00
2018-08-31 13:03:35 +00:00
if (!state.stencilEnabled) {
state.stencilEnabled = true;
glEnable(GL_STENCIL_TEST);
2018-08-29 06:05:09 +00:00
}
2018-08-31 13:03:35 +00:00
GLenum glAction;
switch (action) {
case STENCIL_REPLACE: glAction = GL_REPLACE; break;
case STENCIL_INCREMENT: glAction = GL_INCR; break;
case STENCIL_DECREMENT: glAction = GL_DECR; break;
case STENCIL_INCREMENT_WRAP: glAction = GL_INCR_WRAP; break;
case STENCIL_DECREMENT_WRAP: glAction = GL_DECR_WRAP; break;
case STENCIL_INVERT: glAction = GL_INVERT; break;
}
2018-08-30 10:38:31 +00:00
2018-08-31 13:03:35 +00:00
glStencilFunc(GL_ALWAYS, replaceValue, 0xff);
glStencilOp(GL_KEEP, GL_KEEP, glAction);
2018-08-31 13:03:35 +00:00
state.stencilWriting = true;
callback(userdata);
state.stencilWriting = false;
2018-07-17 06:51:11 +00:00
2018-08-31 13:03:35 +00:00
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
state.stencilMode = ~0; // Dirty
2018-07-17 10:35:01 +00:00
}
2018-07-17 06:51:11 +00:00
2018-08-09 00:53:59 +00:00
void lovrGpuCompute(Shader* shader, int x, int y, int z) {
#ifdef EMSCRIPTEN
lovrThrow("Compute shaders are not supported on this system");
#else
lovrAssert(GLAD_GL_ARB_compute_shader, "Compute shaders are not supported on this system");
2018-08-09 00:53:59 +00:00
lovrAssert(shader->type == SHADER_COMPUTE, "Attempt to use a non-compute shader for a compute operation");
2018-08-10 02:03:21 +00:00
lovrShaderBind(shader);
2018-08-09 00:53:59 +00:00
glDispatchCompute(x, y, z);
#endif
2018-08-09 00:53:59 +00:00
}
2018-07-17 10:35:01 +00:00
void lovrGpuPresent() {
memset(&state.stats, 0, sizeof(state.stats));
2018-08-01 22:40:43 +00:00
#ifdef __APPLE__
// For some reason instancing doesn't work on macOS unless you reset the shader every frame
lovrGpuUseProgram(0);
#endif
2018-07-17 10:35:01 +00:00
}
2018-07-17 06:51:11 +00:00
2018-09-01 06:24:59 +00:00
void lovrGpuDirtyTexture() {
state.textures[state.activeTexture] = NULL;
2018-07-17 10:35:01 +00:00
}
2018-07-17 06:51:11 +00:00
2018-08-31 13:03:35 +00:00
const GpuFeatures* lovrGpuGetSupported() {
return &state.features;
}
2018-07-17 06:51:11 +00:00
2018-08-31 13:03:35 +00:00
const GpuLimits* lovrGpuGetLimits() {
return &state.limits;
2018-07-17 10:35:01 +00:00
}
2018-07-17 06:51:11 +00:00
2018-08-31 13:03:35 +00:00
const GpuStats* lovrGpuGetStats() {
return &state.stats;
2018-07-17 06:51:11 +00:00
}
2018-07-17 10:35:01 +00:00
// Texture
2018-07-17 06:51:11 +00:00
Texture* lovrTextureCreate(TextureType type, TextureData** slices, int sliceCount, bool srgb, bool mipmaps, int msaa) {
Texture* texture = lovrAlloc(Texture, lovrTextureDestroy);
if (!texture) return NULL;
texture->type = type;
texture->srgb = srgb;
texture->mipmaps = mipmaps;
2018-09-09 05:28:30 +00:00
texture->target = convertTextureTarget(type);
WrapMode wrap = type == TEXTURE_CUBE ? WRAP_CLAMP : WRAP_REPEAT;
glGenTextures(1, &texture->id);
lovrGpuBindTexture(texture, 0);
lovrTextureSetFilter(texture, lovrGraphicsGetDefaultFilter());
lovrTextureSetWrap(texture, (TextureWrap) { .s = wrap, .t = wrap, .r = wrap });
if (msaa > 0) {
texture->msaa = msaa;
glGenRenderbuffers(1, &texture->msaaId);
}
if (sliceCount > 0) {
lovrTextureAllocate(texture, slices[0]->width, slices[0]->height, sliceCount, slices[0]->format);
for (int i = 0; i < sliceCount; i++) {
lovrTextureReplacePixels(texture, slices[i], 0, 0, i, 0);
}
}
return texture;
}
2018-09-09 05:28:30 +00:00
Texture* lovrTextureCreateFromHandle(uint32_t handle, TextureType type) {
lovrAssert(glIsTexture(handle), "Invalid texture handle");
Texture* texture = lovrAlloc(Texture, lovrTextureDestroy);
if (!texture) return NULL;
texture->type = type;
texture->id = handle;
texture->target = convertTextureTarget(type);
return texture;
}
void lovrTextureDestroy(void* ref) {
Texture* texture = ref;
glDeleteTextures(1, &texture->id);
glDeleteRenderbuffers(1, &texture->msaaId);
2018-08-31 13:03:35 +00:00
lovrGpuDestroySyncResource(texture, texture->incoherent);
free(texture);
}
void lovrTextureAllocate(Texture* texture, int width, int height, int depth, TextureFormat format) {
2018-08-31 13:03:35 +00:00
int maxSize = state.limits.textureSize;
lovrAssert(!texture->allocated, "Texture is already allocated");
lovrAssert(texture->type != TEXTURE_CUBE || width == height, "Cubemap images must be square");
lovrAssert(texture->type != TEXTURE_CUBE || depth == 6, "6 images are required for a cube texture\n");
lovrAssert(texture->type != TEXTURE_2D || depth == 1, "2D textures can only contain a single image");
lovrAssert(width < maxSize, "Texture width %d exceeds max of %d", width, maxSize);
lovrAssert(height < maxSize, "Texture height %d exceeds max of %d", height, maxSize);
lovrAssert(!texture->msaa || texture->type == TEXTURE_2D, "Only 2D textures can be created with MSAA");
2018-07-17 10:35:01 +00:00
texture->allocated = true;
texture->width = width;
texture->height = height;
texture->depth = depth;
texture->format = format;
if (texture->mipmaps) {
int dimension = texture->type == TEXTURE_VOLUME ? (MAX(MAX(width, height), depth)) : MAX(width, height);
texture->mipmapCount = texture->mipmaps ? (log2(dimension) + 1) : 1;
} else {
texture->mipmapCount = 1;
}
2018-07-17 06:51:11 +00:00
if (isTextureFormatCompressed(format)) {
2018-07-17 10:35:01 +00:00
return;
}
2018-07-17 06:51:11 +00:00
2018-08-31 13:03:35 +00:00
bool srgb = state.srgb && texture->srgb;
GLenum glFormat = convertTextureFormat(format);
GLenum internalFormat = convertTextureFormatInternal(format, srgb);
2018-07-17 10:35:01 +00:00
#ifndef EMSCRIPTEN
if (GLAD_GL_ARB_texture_storage) {
#endif
if (texture->type == TEXTURE_ARRAY) {
glTexStorage3D(texture->target, texture->mipmapCount, internalFormat, width, height, depth);
2018-07-17 10:35:01 +00:00
} else {
glTexStorage2D(texture->target, texture->mipmapCount, internalFormat, width, height);
2018-07-17 06:51:11 +00:00
}
2018-07-17 10:35:01 +00:00
#ifndef EMSCRIPTEN
} else {
for (int i = 0; i < texture->mipmapCount; i++) {
2018-07-17 10:35:01 +00:00
switch (texture->type) {
case TEXTURE_2D:
glTexImage2D(texture->target, i, internalFormat, width, height, 0, glFormat, GL_UNSIGNED_BYTE, NULL);
2018-07-17 10:35:01 +00:00
break;
2018-07-17 06:51:11 +00:00
2018-07-17 10:35:01 +00:00
case TEXTURE_CUBE:
for (int face = 0; face < 6; face++) {
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, i, internalFormat, width, height, 0, glFormat, GL_UNSIGNED_BYTE, NULL);
2018-07-17 10:35:01 +00:00
}
break;
case TEXTURE_ARRAY:
case TEXTURE_VOLUME:
glTexImage3D(texture->target, i, internalFormat, width, height, depth, 0, glFormat, GL_UNSIGNED_BYTE, NULL);
2018-07-17 10:35:01 +00:00
break;
}
width = MAX(width >> 1, 1);
height = MAX(height >> 1, 1);
depth = texture->type == TEXTURE_VOLUME ? MAX(depth >> 1, 1) : depth;
2018-07-17 10:35:01 +00:00
}
}
#endif
if (texture->msaaId) {
glBindRenderbuffer(GL_RENDERBUFFER, texture->msaaId);
glRenderbufferStorageMultisample(GL_RENDERBUFFER, texture->msaa, internalFormat, width, height);
}
2018-07-17 06:51:11 +00:00
}
2018-07-23 02:42:39 +00:00
void lovrTextureReplacePixels(Texture* texture, TextureData* textureData, int x, int y, int slice, int mipmap) {
lovrAssert(texture->allocated, "Texture is not allocated");
2018-07-17 06:51:11 +00:00
2018-08-31 13:03:35 +00:00
#ifndef EMSCRIPTEN
if ((texture->incoherent >> BARRIER_TEXTURE) & 1) {
2018-08-31 13:03:35 +00:00
lovrGpuSync(1 << BARRIER_TEXTURE);
}
2018-08-31 13:03:35 +00:00
#endif
int maxWidth = lovrTextureGetWidth(texture, mipmap);
int maxHeight = lovrTextureGetHeight(texture, mipmap);
int width = textureData->width;
int height = textureData->height;
bool overflow = (x + width > maxWidth) || (y + height > maxHeight);
2018-07-23 02:42:39 +00:00
lovrAssert(!overflow, "Trying to replace pixels outside the texture's bounds");
lovrAssert(mipmap >= 0 && mipmap < texture->mipmapCount, "Invalid mipmap level %d", mipmap);
2018-07-17 10:35:01 +00:00
GLenum glFormat = convertTextureFormat(textureData->format);
GLenum glInternalFormat = convertTextureFormatInternal(textureData->format, texture->srgb);
GLenum binding = (texture->type == TEXTURE_CUBE) ? GL_TEXTURE_CUBE_MAP_POSITIVE_X + slice : texture->target;
2018-07-17 06:51:11 +00:00
lovrGpuBindTexture(texture, 0);
2018-07-17 10:35:01 +00:00
if (isTextureFormatCompressed(textureData->format)) {
lovrAssert(width == maxWidth && height == maxHeight, "Compressed texture pixels must be fully replaced");
2018-07-23 02:42:39 +00:00
lovrAssert(mipmap == 0, "Unable to replace a specific mipmap of a compressed texture");
2018-07-17 10:35:01 +00:00
Mipmap m; int i;
vec_foreach(&textureData->mipmaps, m, i) {
switch (texture->type) {
case TEXTURE_2D:
case TEXTURE_CUBE:
glCompressedTexImage2D(binding, i, glInternalFormat, m.width, m.height, 0, m.size, m.data);
break;
case TEXTURE_ARRAY:
case TEXTURE_VOLUME:
2018-07-17 21:53:21 +00:00
glCompressedTexSubImage3D(binding, i, x, y, slice, m.width, m.height, 1, glInternalFormat, m.size, m.data);
2018-07-17 10:35:01 +00:00
break;
}
2018-07-17 06:51:11 +00:00
}
2018-07-17 10:35:01 +00:00
} else {
2018-09-01 12:40:45 +00:00
lovrAssert(textureData->blob.data, "Trying to replace Texture pixels with empty pixel data");
GLenum glType = convertTextureFormatType(textureData->format);
2018-07-17 10:35:01 +00:00
switch (texture->type) {
case TEXTURE_2D:
case TEXTURE_CUBE:
glTexSubImage2D(binding, mipmap, x, y, width, height, glFormat, glType, textureData->blob.data);
2018-07-17 06:51:11 +00:00
break;
2018-07-17 10:35:01 +00:00
case TEXTURE_ARRAY:
case TEXTURE_VOLUME:
glTexSubImage3D(binding, mipmap, x, y, slice, width, height, 1, glFormat, glType, textureData->blob.data);
2018-07-17 06:51:11 +00:00
break;
2018-07-17 10:35:01 +00:00
}
2018-07-17 06:51:11 +00:00
2018-07-17 10:35:01 +00:00
if (texture->mipmaps) {
2018-09-05 10:48:44 +00:00
#if defined(__APPLE__) || defined(EMSCRIPTEN) // glGenerateMipmap doesn't work on big cubemap textures on macOS
if (texture->type != TEXTURE_CUBE || width < 2048) {
glGenerateMipmap(texture->target);
} else {
glTexParameteri(texture->target, GL_TEXTURE_MAX_LEVEL, 0);
}
#else
glGenerateMipmap(texture->target);
2018-09-05 10:48:44 +00:00
#endif
2018-07-17 10:35:01 +00:00
}
}
}
2018-07-17 06:51:11 +00:00
GLuint lovrTextureGetId(Texture* texture) {
return texture->id;
}
int lovrTextureGetWidth(Texture* texture, int mipmap) {
return MAX(texture->width >> mipmap, 1);
}
int lovrTextureGetHeight(Texture* texture, int mipmap) {
return MAX(texture->height >> mipmap, 1);
}
int lovrTextureGetDepth(Texture* texture, int mipmap) {
return texture->type == TEXTURE_VOLUME ? MAX(texture->depth >> mipmap, 1) : texture->depth;
}
int lovrTextureGetMipmapCount(Texture* texture) {
return texture->mipmapCount;
}
TextureType lovrTextureGetType(Texture* texture) {
return texture->type;
}
2018-08-17 00:54:48 +00:00
TextureFormat lovrTextureGetFormat(Texture* texture) {
2018-08-16 21:27:45 +00:00
return texture->format;
}
2018-07-17 10:35:01 +00:00
TextureFilter lovrTextureGetFilter(Texture* texture) {
return texture->filter;
}
2018-07-17 06:51:11 +00:00
2018-07-17 10:35:01 +00:00
void lovrTextureSetFilter(Texture* texture, TextureFilter filter) {
float anisotropy = filter.mode == FILTER_ANISOTROPIC ? MAX(filter.anisotropy, 1.) : 1.;
lovrGpuBindTexture(texture, 0);
texture->filter = filter;
2018-07-17 06:51:11 +00:00
2018-07-17 10:35:01 +00:00
switch (filter.mode) {
case FILTER_NEAREST:
glTexParameteri(texture->target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(texture->target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
2018-07-17 10:35:01 +00:00
break;
2018-07-17 06:51:11 +00:00
2018-07-17 10:35:01 +00:00
case FILTER_BILINEAR:
if (texture->mipmaps) {
glTexParameteri(texture->target, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
glTexParameteri(texture->target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2018-07-17 10:35:01 +00:00
} else {
glTexParameteri(texture->target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(texture->target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2018-07-17 06:51:11 +00:00
}
2018-07-17 10:35:01 +00:00
break;
2018-07-17 06:51:11 +00:00
2018-07-17 10:35:01 +00:00
case FILTER_TRILINEAR:
case FILTER_ANISOTROPIC:
if (texture->mipmaps) {
glTexParameteri(texture->target, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(texture->target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2018-07-17 10:35:01 +00:00
} else {
glTexParameteri(texture->target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(texture->target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2018-07-17 06:51:11 +00:00
}
2018-07-17 10:35:01 +00:00
break;
2018-07-17 06:51:11 +00:00
}
glTexParameteri(texture->target, GL_TEXTURE_MAX_ANISOTROPY_EXT, anisotropy);
2018-07-17 10:35:01 +00:00
}
2018-07-17 06:51:11 +00:00
2018-07-17 10:35:01 +00:00
TextureWrap lovrTextureGetWrap(Texture* texture) {
return texture->wrap;
2018-07-17 06:51:11 +00:00
}
2018-07-17 10:35:01 +00:00
void lovrTextureSetWrap(Texture* texture, TextureWrap wrap) {
texture->wrap = wrap;
lovrGpuBindTexture(texture, 0);
glTexParameteri(texture->target, GL_TEXTURE_WRAP_S, convertWrapMode(wrap.s));
glTexParameteri(texture->target, GL_TEXTURE_WRAP_T, convertWrapMode(wrap.t));
2018-07-17 10:35:01 +00:00
if (texture->type == TEXTURE_CUBE || texture->type == TEXTURE_VOLUME) {
glTexParameteri(texture->target, GL_TEXTURE_WRAP_R, convertWrapMode(wrap.r));
2018-07-17 06:51:11 +00:00
}
}
2018-07-17 10:35:01 +00:00
// Canvas
2018-07-17 06:51:11 +00:00
2018-08-24 21:43:39 +00:00
Canvas* lovrCanvasCreate(int width, int height, CanvasFlags flags) {
Canvas* canvas = lovrAlloc(Canvas, lovrCanvasDestroy);
2018-08-20 03:48:45 +00:00
if (!canvas) return NULL;
2018-07-17 10:35:01 +00:00
2018-08-24 21:43:39 +00:00
canvas->width = width;
canvas->height = height;
canvas->flags = flags;
glGenFramebuffers(1, &canvas->framebuffer);
2018-08-24 21:43:39 +00:00
lovrGpuBindFramebuffer(canvas->framebuffer);
if (flags.depth != DEPTH_NONE) {
GLenum attachment = flags.depth == DEPTH_D24S8 ? GL_DEPTH_STENCIL_ATTACHMENT : GL_DEPTH_ATTACHMENT;
GLenum format = convertDepthFormat(flags.depth);
glGenRenderbuffers(1, &canvas->depthBuffer);
glBindRenderbuffer(GL_RENDERBUFFER, canvas->depthBuffer);
glRenderbufferStorageMultisample(GL_RENDERBUFFER, canvas->flags.msaa, format, width, height);
2018-08-24 21:43:39 +00:00
glFramebufferRenderbuffer(GL_FRAMEBUFFER, attachment, GL_RENDERBUFFER, canvas->depthBuffer);
}
if (flags.msaa) {
glGenFramebuffers(1, &canvas->resolveBuffer);
}
2018-07-17 10:35:01 +00:00
return canvas;
2018-07-17 06:51:11 +00:00
}
2018-07-17 10:35:01 +00:00
void lovrCanvasDestroy(void* ref) {
Canvas* canvas = ref;
glDeleteFramebuffers(1, &canvas->framebuffer);
glDeleteRenderbuffers(1, &canvas->depthBuffer);
glDeleteFramebuffers(1, &canvas->resolveBuffer);
for (int i = 0; i < canvas->count; i++) {
2018-08-24 01:28:37 +00:00
lovrRelease(canvas->attachments[i].texture);
}
2018-08-20 03:48:45 +00:00
free(ref);
2018-07-17 06:51:11 +00:00
}
2018-08-24 01:28:37 +00:00
const Attachment* lovrCanvasGetAttachments(Canvas* canvas, int* count) {
2018-08-27 18:01:05 +00:00
if (count) *count = canvas->count;
2018-08-24 01:28:37 +00:00
return canvas->attachments;
}
void lovrCanvasSetAttachments(Canvas* canvas, Attachment* attachments, int count) {
lovrAssert(count > 0, "A Canvas must have at least one attached Texture");
lovrAssert(count <= MAX_CANVAS_ATTACHMENTS, "Only %d textures can be attached to a Canvas, got %d\n", MAX_CANVAS_ATTACHMENTS, count);
2018-08-29 19:40:19 +00:00
if (!canvas->needsAttach && count == canvas->count && !memcmp(canvas->attachments, attachments, count * sizeof(Attachment))) {
return;
}
2018-08-29 19:40:19 +00:00
for (int i = 0; i < count; i++) {
Texture* texture = attachments[i].texture;
int width = lovrTextureGetWidth(texture, attachments[i].level);
int height = lovrTextureGetHeight(texture, attachments[i].level);
2018-08-30 00:06:20 +00:00
lovrAssert(!canvas->depthBuffer || width == canvas->width, "Texture width of %d does not match Canvas width", width);
lovrAssert(!canvas->depthBuffer || height == canvas->height, "Texture height of %d does not match Canvas height", height);
2018-08-29 19:40:19 +00:00
lovrAssert(texture->msaa == canvas->flags.msaa, "Texture MSAA does not match Canvas MSAA");
lovrRetain(texture);
}
2018-08-29 19:40:19 +00:00
for (int i = 0; i < canvas->count; i++) {
lovrRelease(canvas->attachments[i].texture);
2018-08-24 01:28:37 +00:00
}
2018-08-29 19:40:19 +00:00
memcpy(canvas->attachments, attachments, count * sizeof(Attachment));
canvas->count = count;
canvas->needsAttach = true;
2018-08-24 01:28:37 +00:00
}
2018-08-31 13:03:35 +00:00
void lovrCanvasBind(Canvas* canvas, bool willDraw) {
if (canvas) {
lovrGpuBindFramebuffer(canvas->framebuffer);
canvas->needsResolve = willDraw;
} else {
lovrGpuBindFramebuffer(0);
return;
}
2018-08-24 01:28:37 +00:00
2018-08-29 19:40:19 +00:00
if (!canvas->needsAttach) {
2018-08-24 01:28:37 +00:00
return;
}
// We need to synchronize if any of the Canvas attachments have pending writes on them
2018-08-31 13:03:35 +00:00
#ifndef EMSCRIPTEN
2018-08-24 01:28:37 +00:00
for (int i = 0; i < canvas->count; i++) {
Texture* texture = canvas->attachments[i].texture;
if (texture->incoherent && (texture->incoherent >> BARRIER_CANVAS) & 1) {
2018-08-31 13:03:35 +00:00
lovrGpuSync(1 << BARRIER_CANVAS);
2018-08-24 01:28:37 +00:00
break;
}
}
2018-08-31 13:03:35 +00:00
#endif
2018-08-24 01:28:37 +00:00
// Use the read framebuffer as a binding point to bind resolve textures
if (canvas->flags.msaa) {
glBindFramebuffer(GL_READ_FRAMEBUFFER, canvas->resolveBuffer);
}
2018-08-24 01:28:37 +00:00
GLenum buffers[MAX_CANVAS_ATTACHMENTS] = { GL_NONE };
for (int i = 0; i < canvas->count; i++) {
GLenum buffer = buffers[i] = GL_COLOR_ATTACHMENT0 + i;
Attachment* attachment = &canvas->attachments[i];
Texture* texture = attachment->texture;
int slice = attachment->slice;
int level = attachment->level;
2018-09-01 06:24:59 +00:00
if (canvas->flags.msaa) {
glFramebufferRenderbuffer(GL_FRAMEBUFFER, buffer, GL_RENDERBUFFER, texture->msaaId);
}
2018-08-24 01:28:37 +00:00
switch (texture->type) {
2018-08-30 03:53:39 +00:00
case TEXTURE_2D: glFramebufferTexture2D(GL_READ_FRAMEBUFFER, buffer, GL_TEXTURE_2D, texture->id, level); break;
case TEXTURE_CUBE: glFramebufferTexture2D(GL_READ_FRAMEBUFFER, buffer, GL_TEXTURE_CUBE_MAP_POSITIVE_X + slice, texture->id, level); break;
case TEXTURE_ARRAY: glFramebufferTextureLayer(GL_READ_FRAMEBUFFER, buffer, texture->id, level, slice); break;
2018-08-31 03:56:08 +00:00
case TEXTURE_VOLUME: glFramebufferTextureLayer(GL_READ_FRAMEBUFFER, buffer, texture->id, level, slice); break;
2018-08-24 01:28:37 +00:00
}
}
glDrawBuffers(canvas->count, buffers);
GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
2018-08-24 22:11:37 +00:00
switch (status) {
case GL_FRAMEBUFFER_COMPLETE: break;
case GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE: lovrThrow("Unable to set Canvas (MSAA settings)"); break;
case GL_FRAMEBUFFER_UNSUPPORTED: lovrThrow("Unable to set Canvas (Texture formats)"); break;
default: lovrThrow("Unable to set Canvas (reason unknown)"); break;
}
2018-08-24 01:28:37 +00:00
2018-08-29 19:40:19 +00:00
canvas->needsAttach = false;
2018-08-24 01:28:37 +00:00
}
2018-08-29 19:40:19 +00:00
void lovrCanvasResolve(Canvas* canvas) {
if (!canvas->needsResolve) {
2018-08-29 19:40:19 +00:00
return;
}
if (canvas->flags.msaa) {
int w = canvas->width;
int h = canvas->height;
glBindFramebuffer(GL_READ_FRAMEBUFFER, canvas->framebuffer);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, canvas->resolveBuffer);
state.framebuffer = canvas->resolveBuffer;
if (canvas->count == 1) {
glBlitFramebuffer(0, 0, w, h, 0, 0, w, h, GL_COLOR_BUFFER_BIT, GL_NEAREST);
} else {
GLenum buffers[MAX_CANVAS_ATTACHMENTS] = { GL_NONE };
for (int i = 0; i < canvas->count; i++) {
buffers[i] = GL_COLOR_ATTACHMENT0 + i;
glReadBuffer(i);
glDrawBuffers(1, &buffers[i]);
glBlitFramebuffer(0, 0, w, h, 0, 0, w, h, GL_COLOR_BUFFER_BIT, GL_NEAREST);
}
glReadBuffer(0);
glDrawBuffers(canvas->count, buffers);
}
}
2018-08-29 20:34:54 +00:00
if (canvas->flags.mipmaps) {
for (int i = 0; i < canvas->count; i++) {
Texture* texture = canvas->attachments[i].texture;
if (texture->mipmapCount > 1) {
lovrGpuBindTexture(texture, 0);
glGenerateMipmap(texture->target);
}
}
}
canvas->needsResolve = false;
}
2018-08-29 04:16:19 +00:00
bool lovrCanvasIsStereo(Canvas* canvas) {
return canvas->flags.stereo;
}
2018-08-30 00:06:20 +00:00
int lovrCanvasGetWidth(Canvas* canvas) {
2018-08-27 19:42:18 +00:00
return canvas->width;
}
2018-08-30 00:06:20 +00:00
int lovrCanvasGetHeight(Canvas* canvas) {
2018-08-27 19:42:18 +00:00
return canvas->height;
}
2018-08-30 00:06:20 +00:00
int lovrCanvasGetMSAA(Canvas* canvas) {
return canvas->flags.msaa;
}
DepthFormat lovrCanvasGetDepthFormat(Canvas* canvas) {
return canvas->flags.depth;
}
2018-08-29 21:03:14 +00:00
TextureData* lovrCanvasNewTextureData(Canvas* canvas, int index) {
2018-08-31 13:03:35 +00:00
lovrCanvasBind(canvas, false);
2018-08-29 21:03:14 +00:00
2018-08-31 13:03:35 +00:00
#ifndef EMSCRIPTEN
2018-08-29 21:03:14 +00:00
Texture* texture = canvas->attachments[index].texture;
if ((texture->incoherent >> BARRIER_TEXTURE) & 1) {
2018-08-31 13:03:35 +00:00
lovrGpuSync(1 << BARRIER_TEXTURE);
2018-08-29 21:03:14 +00:00
}
2018-08-31 13:03:35 +00:00
#endif
2018-08-29 21:03:14 +00:00
if (index != 0) {
glReadBuffer(index);
}
TextureData* textureData = lovrTextureDataCreate(canvas->width, canvas->height, 0x0, FORMAT_RGBA);
glReadPixels(0, 0, canvas->width, canvas->height, GL_RGBA, GL_UNSIGNED_BYTE, textureData->blob.data);
if (index != 0) {
glReadBuffer(0);
}
return textureData;
}
2018-07-17 10:35:01 +00:00
// Shader
2018-07-17 06:51:11 +00:00
2018-07-19 08:22:47 +00:00
static GLuint compileShader(GLenum type, const char** sources, int count) {
2018-07-17 10:35:01 +00:00
GLuint shader = glCreateShader(type);
2018-07-19 08:22:47 +00:00
glShaderSource(shader, count, sources, NULL);
2018-07-17 10:35:01 +00:00
glCompileShader(shader);
2018-07-17 06:51:11 +00:00
2018-07-17 10:35:01 +00:00
int isShaderCompiled;
glGetShaderiv(shader, GL_COMPILE_STATUS, &isShaderCompiled);
if (!isShaderCompiled) {
int logLength;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &logLength);
char* log = malloc(logLength);
glGetShaderInfoLog(shader, logLength, &logLength, log);
lovrThrow("Could not compile shader:\n%s", log);
2018-07-17 10:35:01 +00:00
}
2018-07-17 06:51:11 +00:00
2018-07-17 10:35:01 +00:00
return shader;
2018-07-17 06:51:11 +00:00
}
2018-08-07 23:58:14 +00:00
static GLuint linkProgram(GLuint program) {
2018-07-17 10:35:01 +00:00
glLinkProgram(program);
int isLinked;
glGetProgramiv(program, GL_LINK_STATUS, &isLinked);
if (!isLinked) {
int logLength;
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &logLength);
char* log = malloc(logLength);
glGetProgramInfoLog(program, logLength, &logLength, log);
lovrThrow("Could not link shader:\n%s", log);
2018-07-17 06:51:11 +00:00
}
2018-07-17 10:35:01 +00:00
return program;
2018-07-17 06:51:11 +00:00
}
2018-08-07 23:58:14 +00:00
static void lovrShaderSetupUniforms(Shader* shader) {
uint32_t program = shader->program;
lovrGpuUseProgram(program); // TODO necessary?
2018-07-17 10:35:01 +00:00
// Uniform blocks
int32_t blockCount;
glGetProgramiv(program, GL_ACTIVE_UNIFORM_BLOCKS, &blockCount);
lovrAssert(blockCount <= MAX_BLOCK_BUFFERS, "Shader has too many read-only blocks (%d) the max is %d", blockCount, MAX_BLOCK_BUFFERS);
map_init(&shader->blockMap);
vec_block_t* uniformBlocks = &shader->blocks[BLOCK_UNIFORM];
vec_init(uniformBlocks);
vec_reserve(uniformBlocks, blockCount);
for (int i = 0; i < blockCount; i++) {
UniformBlock block = { .slot = i, .source = NULL };
glUniformBlockBinding(program, i, block.slot);
vec_init(&block.uniforms);
char name[LOVR_MAX_UNIFORM_LENGTH];
glGetActiveUniformBlockName(program, i, LOVR_MAX_UNIFORM_LENGTH, NULL, name);
int blockId = (i << 1) + BLOCK_UNIFORM;
map_set(&shader->blockMap, name, blockId);
vec_push(uniformBlocks, block);
}
// Shader storage buffers and their buffer variables
vec_block_t* storageBlocks = &shader->blocks[BLOCK_STORAGE];
vec_init(storageBlocks);
2018-08-06 15:51:06 +00:00
#ifndef EMSCRIPTEN
if (GLAD_GL_ARB_shader_storage_buffer_object && GLAD_GL_ARB_program_interface_query) {
// Iterate over storage blocks, setting their binding and pushing them onto the block vector
int storageCount;
glGetProgramInterfaceiv(program, GL_SHADER_STORAGE_BLOCK, GL_ACTIVE_RESOURCES, &storageCount);
lovrAssert(storageCount <= MAX_BLOCK_BUFFERS, "Shader has too many writable blocks (%d) the max is %d", storageCount, MAX_BLOCK_BUFFERS);
vec_reserve(storageBlocks, storageCount);
for (int i = 0; i < storageCount; i++) {
UniformBlock block = { .slot = i, .source = NULL };
glShaderStorageBlockBinding(program, i, block.slot);
vec_init(&block.uniforms);
char name[LOVR_MAX_UNIFORM_LENGTH];
glGetProgramResourceName(program, GL_SHADER_STORAGE_BLOCK, i, LOVR_MAX_UNIFORM_LENGTH, NULL, name);
int blockId = (i << 1) + BLOCK_STORAGE;
map_set(&shader->blockMap, name, blockId);
vec_push(storageBlocks, block);
}
2018-08-03 18:19:40 +00:00
// Iterate over buffer variables, pushing them onto the uniform list of the correct block
int bufferVariableCount;
glGetProgramInterfaceiv(program, GL_BUFFER_VARIABLE, GL_ACTIVE_RESOURCES, &bufferVariableCount);
for (int i = 0; i < bufferVariableCount; i++) {
Uniform uniform;
enum { blockIndex, offset, glType, count, arrayStride, matrixStride, propCount };
int values[propCount];
GLenum properties[propCount] = { GL_BLOCK_INDEX, GL_OFFSET, GL_TYPE, GL_ARRAY_SIZE, GL_ARRAY_STRIDE, GL_MATRIX_STRIDE };
glGetProgramResourceiv(program, GL_BUFFER_VARIABLE, i, propCount, properties, sizeof(values), NULL, values);
glGetProgramResourceName(program, GL_BUFFER_VARIABLE, i, LOVR_MAX_UNIFORM_LENGTH, NULL, uniform.name);
uniform.type = getUniformType(values[glType], uniform.name);
uniform.components = getUniformComponents(uniform.type);
uniform.count = values[count];
uniform.offset = values[offset];
if (uniform.count > 1) {
uniform.size = uniform.count * values[arrayStride];
} else if (uniform.type == UNIFORM_MATRIX) {
uniform.size = values[matrixStride] * uniform.components;
} else {
2018-08-03 19:32:32 +00:00
uniform.size = 4 * (uniform.components == 3 ? 4 : uniform.components);
2018-08-03 18:19:40 +00:00
}
vec_push(&storageBlocks->data[values[blockIndex]].uniforms, uniform);
}
}
2018-08-06 15:51:06 +00:00
#endif
2018-07-17 10:35:01 +00:00
// Uniform introspection
int32_t uniformCount;
int textureSlot = 0;
int imageSlot = 0;
2018-08-01 06:54:49 +00:00
map_init(&shader->uniformMap);
vec_init(&shader->uniforms);
2018-07-17 10:35:01 +00:00
glGetProgramiv(program, GL_ACTIVE_UNIFORMS, &uniformCount);
for (uint32_t i = 0; i < (uint32_t) uniformCount; i++) {
2018-07-17 10:35:01 +00:00
Uniform uniform;
2018-07-19 23:58:59 +00:00
GLenum glType;
glGetActiveUniform(program, i, LOVR_MAX_UNIFORM_LENGTH, NULL, &uniform.count, &glType, uniform.name);
2018-07-17 10:35:01 +00:00
char* subscript = strchr(uniform.name, '[');
if (subscript) {
*subscript = '\0';
2018-07-17 06:51:11 +00:00
}
2018-07-17 10:35:01 +00:00
uniform.location = glGetUniformLocation(program, uniform.name);
2018-07-19 23:58:59 +00:00
uniform.type = getUniformType(glType, uniform.name);
uniform.components = getUniformComponents(glType);
2018-08-10 11:21:03 +00:00
#ifdef EMSCRIPTEN
uniform.image = false;
#else
2018-08-10 10:37:02 +00:00
uniform.image = glType == GL_IMAGE_2D || glType == GL_IMAGE_3D || glType == GL_IMAGE_CUBE || glType == GL_IMAGE_2D_ARRAY;
2018-08-10 11:21:03 +00:00
#endif
uniform.textureType = getUniformTextureType(glType);
uniform.baseSlot = uniform.type == UNIFORM_SAMPLER ? textureSlot : (uniform.type == UNIFORM_IMAGE ? imageSlot : -1);
2018-07-17 06:51:11 +00:00
int blockIndex;
glGetActiveUniformsiv(program, 1, &i, GL_UNIFORM_BLOCK_INDEX, &blockIndex);
if (blockIndex != -1) {
UniformBlock* block = &shader->blocks[BLOCK_UNIFORM].data[blockIndex];
glGetActiveUniformsiv(program, 1, &i, GL_UNIFORM_OFFSET, &uniform.offset);
2018-08-01 23:02:20 +00:00
glGetActiveUniformsiv(program, 1, &i, GL_UNIFORM_SIZE, &uniform.count);
if (uniform.count > 1) {
int stride;
glGetActiveUniformsiv(program, 1, &i, GL_UNIFORM_ARRAY_STRIDE, &stride);
uniform.size = stride * uniform.count;
} else if (uniform.type == UNIFORM_MATRIX) {
int matrixStride;
glGetActiveUniformsiv(program, 1, &i, GL_UNIFORM_MATRIX_STRIDE, &matrixStride);
uniform.size = uniform.components * matrixStride;
} else {
2018-08-03 19:32:32 +00:00
uniform.size = 4 * (uniform.components == 3 ? 4 : uniform.components);
2018-08-01 23:02:20 +00:00
}
vec_push(&block->uniforms, uniform);
continue;
} else if (uniform.location == -1) {
2018-07-17 06:51:11 +00:00
continue;
}
2018-07-17 10:35:01 +00:00
switch (uniform.type) {
case UNIFORM_FLOAT:
uniform.size = uniform.components * uniform.count * sizeof(float);
uniform.value.data = calloc(1, uniform.size);
break;
2018-07-17 06:51:11 +00:00
2018-07-17 10:35:01 +00:00
case UNIFORM_INT:
uniform.size = uniform.components * uniform.count * sizeof(int);
uniform.value.data = calloc(1, uniform.size);
break;
case UNIFORM_MATRIX:
uniform.size = uniform.components * uniform.components * uniform.count * sizeof(int);
uniform.value.data = calloc(1, uniform.size);
break;
case UNIFORM_SAMPLER:
case UNIFORM_IMAGE:
uniform.size = uniform.count * (uniform.type == UNIFORM_SAMPLER ? sizeof(Texture*) : sizeof(Image));
2018-07-17 10:35:01 +00:00
uniform.value.data = calloc(1, uniform.size);
// Use the value for ints to bind texture slots, but use the value for textures afterwards.
for (int i = 0; i < uniform.count; i++) {
uniform.value.ints[i] = uniform.baseSlot + i;
2018-07-17 10:35:01 +00:00
}
glUniform1iv(uniform.location, uniform.count, uniform.value.ints);
memset(uniform.value.data, 0, uniform.size);
2018-07-17 10:35:01 +00:00
break;
2018-07-17 06:51:11 +00:00
}
2018-07-17 10:35:01 +00:00
size_t offset = 0;
for (int j = 0; j < uniform.count; j++) {
int location = uniform.location;
if (uniform.count > 1) {
char name[LOVR_MAX_UNIFORM_LENGTH];
snprintf(name, LOVR_MAX_UNIFORM_LENGTH, "%s[%d]", uniform.name, j);
location = glGetUniformLocation(program, name);
}
switch (uniform.type) {
case UNIFORM_FLOAT:
glGetUniformfv(program, location, &uniform.value.floats[offset]);
offset += uniform.components;
2018-07-17 06:51:11 +00:00
break;
2018-07-17 10:35:01 +00:00
case UNIFORM_INT:
glGetUniformiv(program, location, &uniform.value.ints[offset]);
offset += uniform.components;
2018-07-17 06:51:11 +00:00
break;
2018-07-17 10:35:01 +00:00
case UNIFORM_MATRIX:
glGetUniformfv(program, location, &uniform.value.floats[offset]);
offset += uniform.components * uniform.components;
break;
default: break;
2018-07-17 06:51:11 +00:00
}
}
map_set(&shader->uniformMap, uniform.name, shader->uniforms.length);
vec_push(&shader->uniforms, uniform);
textureSlot += uniform.type == UNIFORM_SAMPLER ? uniform.count : 0;
imageSlot += uniform.type == UNIFORM_IMAGE ? uniform.count : 0;
2018-07-17 06:51:11 +00:00
}
2018-08-07 23:58:14 +00:00
}
Shader* lovrShaderCreateGraphics(const char* vertexSource, const char* fragmentSource) {
Shader* shader = lovrAlloc(Shader, lovrShaderDestroy);
if (!shader) return NULL;
2018-09-01 08:57:38 +00:00
#ifdef EMSCRIPTEN
const char* vertexHeader = "#version 300 es\nprecision mediump float;\nprecision mediump int;\n";
const char* fragmentHeader = vertexHeader;
#else
const char* vertexHeader = state.features.computeShaders ? "#version 430\n" : "#version 150\n";
const char* fragmentHeader = "#version 150\nin vec4 gl_FragCoord;\n";
#endif
const char* vertexSinglepass = state.features.singlepass ?
"#extension GL_AMD_vertex_shader_viewport_index : require\n" "#define SINGLEPASS 1\n" :
"#define SINGLEPASS 0\n";
const char* fragmentSinglepass = state.features.singlepass ?
"#extension GL_ARB_fragment_layer_viewport : require\n" "#define SINGLEPASS 1\n" :
"#define SINGLEPASS 0\n";
2018-08-07 23:58:14 +00:00
// Vertex
vertexSource = vertexSource == NULL ? lovrDefaultVertexShader : vertexSource;
2018-09-01 08:57:38 +00:00
const char* vertexSources[] = { vertexHeader, vertexSinglepass, lovrShaderVertexPrefix, vertexSource, lovrShaderVertexSuffix };
2018-08-07 23:58:14 +00:00
GLuint vertexShader = compileShader(GL_VERTEX_SHADER, vertexSources, sizeof(vertexSources) / sizeof(vertexSources[0]));
// Fragment
fragmentSource = fragmentSource == NULL ? lovrDefaultFragmentShader : fragmentSource;
2018-09-01 08:57:38 +00:00
const char* fragmentSources[] = { fragmentHeader, fragmentSinglepass, lovrShaderFragmentPrefix, fragmentSource, lovrShaderFragmentSuffix };
2018-08-07 23:58:14 +00:00
GLuint fragmentShader = compileShader(GL_FRAGMENT_SHADER, fragmentSources, sizeof(fragmentSources) / sizeof(fragmentSources[0]));
// Link
uint32_t program = glCreateProgram();
glAttachShader(program, vertexShader);
glAttachShader(program, fragmentShader);
glBindAttribLocation(program, LOVR_SHADER_POSITION, "lovrPosition");
glBindAttribLocation(program, LOVR_SHADER_NORMAL, "lovrNormal");
glBindAttribLocation(program, LOVR_SHADER_TEX_COORD, "lovrTexCoord");
glBindAttribLocation(program, LOVR_SHADER_VERTEX_COLOR, "lovrVertexColor");
glBindAttribLocation(program, LOVR_SHADER_TANGENT, "lovrTangent");
glBindAttribLocation(program, LOVR_SHADER_BONES, "lovrBones");
glBindAttribLocation(program, LOVR_SHADER_BONE_WEIGHTS, "lovrBoneWeights");
linkProgram(program);
glDetachShader(program, vertexShader);
glDeleteShader(vertexShader);
glDetachShader(program, fragmentShader);
glDeleteShader(fragmentShader);
shader->program = program;
shader->type = SHADER_GRAPHICS;
lovrGpuUseProgram(program);
glVertexAttrib4fv(LOVR_SHADER_VERTEX_COLOR, (float[4]) { 1., 1., 1., 1. });
glVertexAttribI4iv(LOVR_SHADER_BONES, (int[4]) { 0., 0., 0., 0. });
glVertexAttrib4fv(LOVR_SHADER_BONE_WEIGHTS, (float[4]) { 1., 0., 0., 0. });
lovrShaderSetupUniforms(shader);
2018-07-17 06:51:11 +00:00
2018-07-17 10:35:01 +00:00
// Attribute cache
int32_t attributeCount;
glGetProgramiv(program, GL_ACTIVE_ATTRIBUTES, &attributeCount);
map_init(&shader->attributes);
for (int i = 0; i < attributeCount; i++) {
char name[LOVR_MAX_ATTRIBUTE_LENGTH];
GLint size;
GLenum type;
glGetActiveAttrib(program, i, LOVR_MAX_ATTRIBUTE_LENGTH, NULL, &size, &type, name);
map_set(&shader->attributes, name, glGetAttribLocation(program, name));
}
2018-07-17 06:51:11 +00:00
2018-07-17 10:35:01 +00:00
return shader;
2018-07-17 06:51:11 +00:00
}
2018-08-07 23:58:14 +00:00
Shader* lovrShaderCreateCompute(const char* source) {
Shader* shader = lovrAlloc(Shader, lovrShaderDestroy);
if (!shader) return NULL;
#ifdef EMSCRIPTEN
lovrThrow("Compute shaders are not supported on this system");
#else
lovrAssert(GLAD_GL_ARB_compute_shader, "Compute shaders are not supported on this system");
2018-08-09 01:05:29 +00:00
const char* sources[] = { lovrShaderComputePrefix, source, lovrShaderComputeSuffix };
2018-08-07 23:58:14 +00:00
GLuint computeShader = compileShader(GL_COMPUTE_SHADER, sources, sizeof(sources) / sizeof(sources[0]));
GLuint program = glCreateProgram();
glAttachShader(program, computeShader);
linkProgram(program);
glDetachShader(program, computeShader);
glDeleteShader(computeShader);
shader->program = program;
shader->type = SHADER_COMPUTE;
lovrShaderSetupUniforms(shader);
#endif
2018-08-07 23:58:14 +00:00
return shader;
}
2018-07-17 10:35:01 +00:00
Shader* lovrShaderCreateDefault(DefaultShader type) {
switch (type) {
case SHADER_DEFAULT: return lovrShaderCreateGraphics(NULL, NULL);
case SHADER_CUBE: return lovrShaderCreateGraphics(lovrCubeVertexShader, lovrCubeFragmentShader); break;
case SHADER_PANO: return lovrShaderCreateGraphics(lovrCubeVertexShader, lovrPanoFragmentShader); break;
case SHADER_FONT: return lovrShaderCreateGraphics(NULL, lovrFontFragmentShader);
case SHADER_FILL: return lovrShaderCreateGraphics(lovrFillVertexShader, NULL);
2018-07-17 10:35:01 +00:00
default: lovrThrow("Unknown default shader type"); return NULL;
}
2018-07-17 06:51:11 +00:00
}
2018-07-17 10:35:01 +00:00
void lovrShaderDestroy(void* ref) {
Shader* shader = ref;
glDeleteProgram(shader->program);
for (int i = 0; i < shader->uniforms.length; i++) {
free(shader->uniforms.data[i].value.data);
2018-08-02 10:26:03 +00:00
}
2018-08-03 19:28:25 +00:00
for (BlockType type = BLOCK_UNIFORM; type <= BLOCK_STORAGE; type++) {
UniformBlock* block; int i;
vec_foreach_ptr(&shader->blocks[type], block, i) {
lovrRelease(block->source);
}
2018-08-02 10:26:03 +00:00
}
vec_deinit(&shader->uniforms);
vec_deinit(&shader->blocks[BLOCK_UNIFORM]);
vec_deinit(&shader->blocks[BLOCK_STORAGE]);
2018-07-17 10:35:01 +00:00
map_deinit(&shader->attributes);
map_deinit(&shader->uniformMap);
map_deinit(&shader->blockMap);
2018-07-17 10:35:01 +00:00
free(shader);
2018-07-17 06:51:11 +00:00
}
2018-08-07 23:58:14 +00:00
ShaderType lovrShaderGetType(Shader* shader) {
return shader->type;
}
2018-07-17 10:35:01 +00:00
void lovrShaderBind(Shader* shader) {
UniformBlock* block;
Uniform* uniform;
int i;
2018-08-31 13:03:35 +00:00
lovrGpuUseProgram(shader->program);
// Figure out if we need to wait for pending writes on resources to complete
2018-08-31 13:03:35 +00:00
#ifndef EMSCRIPTEN
uint8_t flags = 0;
vec_foreach_ptr(&shader->blocks[BLOCK_STORAGE], block, i) {
if (block->source && (block->source->incoherent >> BARRIER_BLOCK) & 1) {
flags |= 1 << BARRIER_BLOCK;
break;
}
}
vec_foreach_ptr(&shader->uniforms, uniform, i) {
if (uniform->type == UNIFORM_SAMPLER) {
for (int i = 0; i < uniform->count; i++) {
Texture* texture = uniform->value.textures[i];
if (texture && texture->incoherent && (texture->incoherent >> BARRIER_UNIFORM_TEXTURE) & 1) {
flags |= 1 << BARRIER_UNIFORM_TEXTURE;
if (flags & (1 << BARRIER_UNIFORM_IMAGE)) {
break;
}
}
}
} else if (uniform->type == UNIFORM_IMAGE) {
for (int i = 0; i < uniform->count; i++) {
Texture* texture = uniform->value.images[i].texture;
if (texture && texture->incoherent && (texture->incoherent >> BARRIER_UNIFORM_IMAGE) & 1) {
flags |= 1 << BARRIER_UNIFORM_IMAGE;
if (flags & (1 << BARRIER_UNIFORM_TEXTURE)) {
break;
}
}
}
}
}
2018-08-31 13:03:35 +00:00
lovrGpuSync(flags);
#endif
// Bind uniforms
vec_foreach_ptr(&shader->uniforms, uniform, i) {
if (uniform->type != UNIFORM_SAMPLER && uniform->type != UNIFORM_IMAGE && !uniform->dirty) {
2018-07-17 10:35:01 +00:00
continue;
}
2018-07-17 06:51:11 +00:00
2018-07-17 10:35:01 +00:00
uniform->dirty = false;
int count = uniform->count;
void* data = uniform->value.data;
2018-07-17 06:51:11 +00:00
2018-07-17 10:35:01 +00:00
switch (uniform->type) {
case UNIFORM_FLOAT:
switch (uniform->components) {
case 1: glUniform1fv(uniform->location, count, data); break;
case 2: glUniform2fv(uniform->location, count, data); break;
case 3: glUniform3fv(uniform->location, count, data); break;
case 4: glUniform4fv(uniform->location, count, data); break;
}
break;
2018-07-17 06:51:11 +00:00
2018-07-17 10:35:01 +00:00
case UNIFORM_INT:
switch (uniform->components) {
case 1: glUniform1iv(uniform->location, count, data); break;
case 2: glUniform2iv(uniform->location, count, data); break;
case 3: glUniform3iv(uniform->location, count, data); break;
case 4: glUniform4iv(uniform->location, count, data); break;
}
break;
2018-07-17 06:51:11 +00:00
2018-07-17 10:35:01 +00:00
case UNIFORM_MATRIX:
switch (uniform->components) {
case 2: glUniformMatrix2fv(uniform->location, count, GL_FALSE, data); break;
case 3: glUniformMatrix3fv(uniform->location, count, GL_FALSE, data); break;
case 4: glUniformMatrix4fv(uniform->location, count, GL_FALSE, data); break;
}
break;
2018-07-17 06:51:11 +00:00
case UNIFORM_IMAGE:
2018-09-01 01:10:51 +00:00
#ifndef EMSCRIPTEN
2018-07-17 10:35:01 +00:00
for (int i = 0; i < count; i++) {
Image* image = &uniform->value.images[i];
Texture* texture = image->texture;
lovrAssert(!texture || texture->type == uniform->textureType, "Uniform texture type mismatch for uniform %s", uniform->name);
// If the Shader can write to the texture, mark it as incoherent
if (texture && image->access != ACCESS_READ) {
for (Barrier barrier = BARRIER_BLOCK + 1; barrier < MAX_BARRIERS; barrier++) {
texture->incoherent |= 1 << barrier;
vec_push(&state.incoherents[barrier], texture);
}
}
2018-08-17 22:09:13 +00:00
lovrGpuBindImage(image, uniform->baseSlot + i);
}
2018-08-31 13:03:35 +00:00
#endif
2018-09-01 01:10:51 +00:00
break;
case UNIFORM_SAMPLER:
for (int i = 0; i < count; i++) {
Texture* texture = uniform->value.textures[i];
lovrAssert(!texture || texture->type == uniform->textureType, "Uniform texture type mismatch for uniform %s", uniform->name);
lovrGpuBindTexture(texture, uniform->baseSlot + i);
2018-07-17 10:35:01 +00:00
}
break;
}
2018-07-17 06:51:11 +00:00
}
2018-07-23 02:33:55 +00:00
2018-08-10 02:03:21 +00:00
// Bind uniform blocks
for (BlockType type = BLOCK_UNIFORM; type <= BLOCK_STORAGE; type++) {
vec_foreach_ptr(&shader->blocks[type], block, i) {
if (block->source) {
2018-08-17 22:09:13 +00:00
// If the Shader can write to the block, mark it as incoherent
bool writable = type == BLOCK_STORAGE && block->access != ACCESS_READ;
block->source->incoherent |= writable ? (1 << BARRIER_BLOCK) : 0;
vec_push(&state.incoherents[BARRIER_BLOCK], block->source);
lovrShaderBlockUnmap(block->source);
lovrGpuBindBlockBuffer(type, block->source->buffer, block->slot);
} else {
lovrGpuBindBlockBuffer(type, 0, block->slot);
}
}
}
2018-07-17 06:51:11 +00:00
}
2018-07-17 10:35:01 +00:00
int lovrShaderGetAttributeId(Shader* shader, const char* name) {
int* id = map_get(&shader->attributes, name);
return id ? *id : -1;
2018-07-17 06:51:11 +00:00
}
2018-07-17 10:35:01 +00:00
bool lovrShaderHasUniform(Shader* shader, const char* name) {
return map_get(&shader->uniformMap, name) != NULL;
2018-07-17 06:51:11 +00:00
}
const Uniform* lovrShaderGetUniform(Shader* shader, const char* name) {
int* index = map_get(&shader->uniformMap, name);
if (!index) {
2018-07-17 10:35:01 +00:00
return false;
2018-07-17 06:51:11 +00:00
}
return &shader->uniforms.data[*index];
2018-07-17 06:51:11 +00:00
}
static void lovrShaderSetUniform(Shader* shader, const char* name, UniformType type, void* data, int start, int count, int size, const char* debug) {
int* index = map_get(&shader->uniformMap, name);
if (!index) {
2018-07-17 06:51:11 +00:00
return;
}
Uniform* uniform = &shader->uniforms.data[*index];
2018-07-17 10:35:01 +00:00
const char* plural = (uniform->size / size) > 1 ? "s" : "";
lovrAssert(uniform->type == type, "Unable to send %ss to uniform %s", debug, name);
lovrAssert((start + count) * size <= uniform->size, "Too many %s%s for uniform %s, maximum is %d", debug, plural, name, uniform->size / size);
2018-07-17 06:51:11 +00:00
void* dest = uniform->value.bytes + start * size;
if (!uniform->dirty && !memcmp(dest, data, count * size)) {
2018-07-17 10:35:01 +00:00
return;
2018-07-17 06:51:11 +00:00
}
memcpy(dest, data, count * size);
2018-07-17 10:35:01 +00:00
uniform->dirty = true;
2018-07-17 06:51:11 +00:00
}
void lovrShaderSetFloats(Shader* shader, const char* name, float* data, int start, int count) {
lovrShaderSetUniform(shader, name, UNIFORM_FLOAT, data, start, count, sizeof(float), "float");
2018-07-17 06:51:11 +00:00
}
void lovrShaderSetInts(Shader* shader, const char* name, int* data, int start, int count) {
lovrShaderSetUniform(shader, name, UNIFORM_INT, data, start, count, sizeof(int), "int");
2018-07-17 06:51:11 +00:00
}
void lovrShaderSetMatrices(Shader* shader, const char* name, float* data, int start, int count) {
lovrShaderSetUniform(shader, name, UNIFORM_MATRIX, data, start, count, sizeof(float), "float");
2018-07-17 06:51:11 +00:00
}
void lovrShaderSetTextures(Shader* shader, const char* name, Texture** data, int start, int count) {
lovrShaderSetUniform(shader, name, UNIFORM_SAMPLER, data, start, count, sizeof(Texture*), "texture");
2018-07-17 10:35:01 +00:00
}
2018-07-17 06:51:11 +00:00
void lovrShaderSetImages(Shader* shader, const char* name, Image* data, int start, int count) {
lovrShaderSetUniform(shader, name, UNIFORM_IMAGE, data, start, count, sizeof(Image), "image");
2018-07-28 22:46:46 +00:00
}
2018-08-31 13:03:35 +00:00
void lovrShaderSetColor(Shader* shader, const char* name, Color color) {
gammaCorrectColor(&color);
lovrShaderSetUniform(shader, name, UNIFORM_FLOAT, (float*) &color, 0, 4, sizeof(float), "float");
}
2018-08-17 22:09:13 +00:00
void lovrShaderSetBlock(Shader* shader, const char* name, ShaderBlock* source, UniformAccess access) {
int* id = map_get(&shader->blockMap, name);
2018-08-04 02:21:15 +00:00
lovrAssert(id, "No shader block named '%s'", name);
2018-07-28 22:46:46 +00:00
int type = *id & 1;
int index = *id >> 1;
UniformBlock* block = &shader->blocks[type].data[index];
2018-08-17 22:09:13 +00:00
block->access = access;
2018-08-01 06:16:26 +00:00
2018-08-03 19:28:25 +00:00
if (source != block->source) {
if (source) {
lovrAssert(block->uniforms.length == source->uniforms.length, "ShaderBlock must have same number of uniforms as block definition in Shader");
for (int i = 0; i < block->uniforms.length; i++) {
const Uniform* u = &block->uniforms.data[i];
const Uniform* v = &source->uniforms.data[i];
2018-08-03 20:19:28 +00:00
lovrAssert(u->type == v->type, "Shader is not compatible with ShaderBlock, check type of variable '%s'", v->name);
lovrAssert(u->offset == v->offset, "Shader is not compatible with ShaderBlock, check order of variable '%s'", v->name);
2018-08-17 22:09:13 +00:00
// This check is disabled due to observed driver bugs with std140 layouts
// lovrAssert(u->size == v->size, "Shader is not compatible with ShaderBlock, check count of variable '%s'", v->name);
2018-08-03 19:28:25 +00:00
}
2018-08-01 06:16:26 +00:00
}
2018-08-03 19:28:25 +00:00
lovrRetain(source);
lovrRelease(block->source);
block->source = source;
}
2018-07-28 22:46:46 +00:00
}
2018-07-20 23:56:28 +00:00
// ShaderBlock
ShaderBlock* lovrShaderBlockCreate(vec_uniform_t* uniforms, BlockType type, BufferUsage usage) {
2018-07-28 22:46:09 +00:00
ShaderBlock* block = lovrAlloc(ShaderBlock, lovrShaderBlockDestroy);
2018-07-20 23:56:28 +00:00
if (!block) return NULL;
2018-08-31 13:03:35 +00:00
lovrAssert(type != BLOCK_STORAGE || state.features.computeShaders, "Writable ShaderBlocks are not supported on this system");
vec_init(&block->uniforms);
vec_extend(&block->uniforms, uniforms);
map_init(&block->uniformMap);
2018-07-20 23:56:28 +00:00
int i;
Uniform* uniform;
size_t size = 0;
vec_foreach_ptr(&block->uniforms, uniform, i) {
// Calculate size and offset
2018-08-07 20:26:28 +00:00
size_t align;
if (uniform->count > 1 || uniform->type == UNIFORM_MATRIX) {
align = 16 * (uniform->type == UNIFORM_MATRIX ? uniform->components : 1);
uniform->size = align * uniform->count;
2018-07-20 23:56:28 +00:00
} else {
2018-08-07 20:26:28 +00:00
align = (uniform->components + (uniform->components == 3)) * 4;
uniform->size = uniform->components * 4;
2018-07-20 23:56:28 +00:00
}
2018-08-07 20:26:28 +00:00
uniform->offset = (size + (align - 1)) & -align;
size = uniform->offset + uniform->size;
// Write index to uniform lookup
map_set(&block->uniformMap, uniform->name, i);
2018-07-20 23:56:28 +00:00
}
2018-08-06 15:51:06 +00:00
#ifdef EMSCRIPTEN
block->target = GL_UNIFORM_BUFFER;
#else
block->target = block->type == BLOCK_UNIFORM ? GL_UNIFORM_BUFFER : GL_SHADER_STORAGE_BUFFER;
2018-08-06 15:51:06 +00:00
#endif
block->type = type;
2018-08-06 16:12:59 +00:00
block->usage = convertBufferUsage(usage);
block->size = size;
block->data = calloc(1, size);
glGenBuffers(1, &block->buffer);
2018-08-29 02:10:23 +00:00
glBindBuffer(block->target, block->buffer);
2018-09-05 11:10:12 +00:00
glBufferData(block->target, size, NULL, block->usage);
2018-07-20 23:56:28 +00:00
return block;
}
void lovrShaderBlockDestroy(void* ref) {
ShaderBlock* block = ref;
2018-08-31 13:03:35 +00:00
lovrGpuDestroySyncResource(block, block->incoherent);
2018-08-02 06:42:37 +00:00
glDeleteBuffers(1, &block->buffer);
vec_deinit(&block->uniforms);
map_deinit(&block->uniformMap);
free(block->data);
free(block);
2018-07-20 23:56:28 +00:00
}
size_t lovrShaderBlockGetSize(ShaderBlock* block) {
return block->size;
}
2018-08-06 17:41:57 +00:00
BlockType lovrShaderBlockGetType(ShaderBlock* block) {
return block->type;
}
2018-08-06 18:46:42 +00:00
char* lovrShaderBlockGetShaderCode(ShaderBlock* block, const char* blockName, size_t* length) {
// Calculate
size_t size = 0;
size_t tab = 2;
2018-08-07 20:26:28 +00:00
size += 15; // "layout(std140) "
2018-08-06 18:46:42 +00:00
size += block->type == BLOCK_UNIFORM ? 7 : 6; // "uniform" || "buffer"
size += 1; // " "
size += strlen(blockName);
size += 3; // " {\n"
for (int i = 0; i < block->uniforms.length; i++) {
size += tab;
size += getUniformTypeLength(&block->uniforms.data[i]);
size += 1; // " "
size += strlen(block->uniforms.data[i].name);
size += 2; // ";\n"
}
size += 3; // "};\n"
// Allocate
char* code = malloc(size + 1);
// Concatenate
char* s = code;
2018-08-07 20:26:28 +00:00
s += sprintf(s, "layout(std140) %s %s {\n", block->type == BLOCK_UNIFORM ? "uniform" : "buffer", blockName);
2018-08-06 18:46:42 +00:00
for (int i = 0; i < block->uniforms.length; i++) {
const Uniform* uniform = &block->uniforms.data[i];
if (uniform->count > 1) {
s += sprintf(s, " %s %s[%d];\n", getUniformTypeName(uniform), uniform->name, uniform->count);
} else {
s += sprintf(s, " %s %s;\n", getUniformTypeName(uniform), uniform->name);
}
}
s += sprintf(s, "};\n");
*s = '\0';
*length = size;
return code;
}
const Uniform* lovrShaderBlockGetUniform(ShaderBlock* block, const char* name) {
int* index = map_get(&block->uniformMap, name);
if (!index) return NULL;
return &block->uniforms.data[*index];
}
void* lovrShaderBlockMap(ShaderBlock* block) {
block->mapped = true;
return block->data;
}
void lovrShaderBlockUnmap(ShaderBlock* block) {
if (!block->mapped) {
return;
}
2018-08-29 02:10:23 +00:00
glBindBuffer(block->target, block->buffer);
glBufferData(block->target, block->size, NULL, block->usage);
glBufferSubData(block->target, 0, block->size, block->data);
block->mapped = false;
}
2018-07-17 10:35:01 +00:00
// Mesh
2018-07-17 06:51:11 +00:00
Mesh* lovrMeshCreate(uint32_t count, VertexFormat format, MeshDrawMode drawMode, BufferUsage usage) {
2018-07-25 02:14:29 +00:00
Mesh* mesh = lovrAlloc(Mesh, lovrMeshDestroy);
2018-07-17 10:35:01 +00:00
if (!mesh) return NULL;
2018-07-17 06:51:11 +00:00
2018-07-17 10:35:01 +00:00
mesh->count = count;
mesh->format = format;
mesh->drawMode = drawMode;
mesh->usage = convertBufferUsage(usage);
2018-07-17 06:51:11 +00:00
2018-07-17 10:35:01 +00:00
glGenBuffers(1, &mesh->vbo);
glGenBuffers(1, &mesh->ibo);
lovrGpuBindVertexBuffer(mesh->vbo);
glBufferData(GL_ARRAY_BUFFER, count * format.stride, NULL, mesh->usage);
glGenVertexArrays(1, &mesh->vao);
2018-07-17 06:51:11 +00:00
2018-07-17 10:35:01 +00:00
map_init(&mesh->attachments);
for (int i = 0; i < format.count; i++) {
map_set(&mesh->attachments, format.attributes[i].name, ((MeshAttachment) { mesh, i, 0, true }));
2018-07-17 06:51:11 +00:00
}
2018-07-17 10:35:01 +00:00
mesh->data.raw = calloc(count, format.stride);
2018-07-17 06:51:11 +00:00
2018-07-17 10:35:01 +00:00
return mesh;
}
2018-07-17 06:51:11 +00:00
2018-07-17 10:35:01 +00:00
void lovrMeshDestroy(void* ref) {
Mesh* mesh = ref;
lovrRelease(mesh->material);
free(mesh->data.raw);
free(mesh->indices.raw);
glDeleteBuffers(1, &mesh->vbo);
glDeleteBuffers(1, &mesh->ibo);
glDeleteVertexArrays(1, &mesh->vao);
const char* key;
map_iter_t iter = map_iter(&mesh->attachments);
while ((key = map_next(&mesh->attachments, &iter)) != NULL) {
MeshAttachment* attachment = map_get(&mesh->attachments, key);
if (attachment->mesh != mesh) {
lovrRelease(attachment->mesh);
2018-07-17 06:51:11 +00:00
}
}
2018-07-17 10:35:01 +00:00
map_deinit(&mesh->attachments);
free(mesh);
}
2018-07-17 06:51:11 +00:00
2018-07-17 10:35:01 +00:00
void lovrMeshAttachAttribute(Mesh* mesh, Mesh* other, const char* name, int divisor) {
MeshAttachment* otherAttachment = map_get(&other->attachments, name);
lovrAssert(!mesh->isAttachment, "Attempted to attach to a mesh which is an attachment itself");
lovrAssert(otherAttachment, "No attribute named '%s' exists", name);
lovrAssert(!map_get(&mesh->attachments, name), "Mesh already has an attribute named '%s'", name);
lovrAssert(divisor >= 0, "Divisor can't be negative");
2018-07-17 06:51:11 +00:00
2018-07-17 10:35:01 +00:00
MeshAttachment attachment = { other, otherAttachment->attributeIndex, divisor, true };
map_set(&mesh->attachments, name, attachment);
other->isAttachment = true;
lovrRetain(other);
}
2018-07-17 06:51:11 +00:00
2018-07-17 10:35:01 +00:00
void lovrMeshDetachAttribute(Mesh* mesh, const char* name) {
MeshAttachment* attachment = map_get(&mesh->attachments, name);
lovrAssert(attachment, "No attached attribute '%s' was found", name);
lovrAssert(attachment->mesh != mesh, "Attribute '%s' was not attached from another Mesh", name);
lovrRelease(attachment->mesh);
map_remove(&mesh->attachments, name);
}
2018-07-17 06:51:11 +00:00
void lovrMeshBind(Mesh* mesh, Shader* shader, int divisorMultiplier) {
2018-07-17 10:35:01 +00:00
const char* key;
map_iter_t iter = map_iter(&mesh->attachments);
2018-07-17 06:51:11 +00:00
2018-07-17 10:35:01 +00:00
MeshAttachment layout[MAX_ATTACHMENTS];
memset(layout, 0, MAX_ATTACHMENTS * sizeof(MeshAttachment));
2018-07-17 06:51:11 +00:00
2018-07-17 10:35:01 +00:00
lovrGpuBindVertexArray(mesh->vao);
lovrMeshUnmapVertices(mesh);
lovrMeshUnmapIndices(mesh);
if (mesh->indexCount > 0) {
lovrGpuBindIndexBuffer(mesh->ibo);
2018-07-17 06:51:11 +00:00
}
2018-07-17 10:35:01 +00:00
while ((key = map_next(&mesh->attachments, &iter)) != NULL) {
int location = lovrShaderGetAttributeId(shader, key);
2018-07-17 06:51:11 +00:00
2018-07-17 10:35:01 +00:00
if (location >= 0) {
MeshAttachment* attachment = map_get(&mesh->attachments, key);
layout[location] = *attachment;
lovrMeshUnmapVertices(attachment->mesh);
lovrMeshUnmapIndices(attachment->mesh);
}
2018-07-17 06:51:11 +00:00
}
2018-07-17 10:35:01 +00:00
for (int i = 0; i < MAX_ATTACHMENTS; i++) {
MeshAttachment previous = mesh->layout[i];
MeshAttachment current = layout[i];
2018-07-17 06:51:11 +00:00
2018-07-17 10:35:01 +00:00
if (!memcmp(&previous, &current, sizeof(MeshAttachment))) {
continue;
}
2018-07-17 06:51:11 +00:00
2018-07-17 10:35:01 +00:00
if (previous.enabled != current.enabled) {
if (current.enabled) {
glEnableVertexAttribArray(i);
} else {
glDisableVertexAttribArray(i);
mesh->layout[i] = current;
continue;
}
2018-07-17 06:51:11 +00:00
}
2018-07-17 10:35:01 +00:00
if (previous.divisor != current.divisor) {
glVertexAttribDivisor(i, current.divisor * divisorMultiplier);
2018-07-17 10:35:01 +00:00
}
if (previous.mesh != current.mesh || previous.attributeIndex != current.attributeIndex) {
lovrGpuBindVertexBuffer(current.mesh->vbo);
VertexFormat* format = &current.mesh->format;
Attribute attribute = format->attributes[current.attributeIndex];
switch (attribute.type) {
case ATTR_FLOAT:
glVertexAttribPointer(i, attribute.count, GL_FLOAT, GL_TRUE, format->stride, (void*) attribute.offset);
break;
case ATTR_BYTE:
glVertexAttribPointer(i, attribute.count, GL_UNSIGNED_BYTE, GL_TRUE, format->stride, (void*) attribute.offset);
break;
case ATTR_INT:
2018-09-05 10:25:13 +00:00
glVertexAttribIPointer(i, attribute.count, GL_INT, format->stride, (void*) attribute.offset);
2018-07-17 10:35:01 +00:00
break;
}
2018-07-17 06:51:11 +00:00
}
2018-07-17 10:35:01 +00:00
mesh->layout[i] = current;
2018-07-17 06:51:11 +00:00
}
2018-07-17 10:35:01 +00:00
}
2018-07-17 06:51:11 +00:00
2018-08-31 13:03:35 +00:00
void lovrMeshDraw(Mesh* mesh, int instances) {
GLenum glDrawMode = convertMeshDrawMode(lovrMeshGetDrawMode(mesh));
if (mesh->indexCount > 0) {
size_t count = mesh->rangeCount ? mesh->rangeCount : mesh->indexCount;
GLenum indexType = mesh->indexSize == sizeof(uint16_t) ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT;
size_t offset = mesh->rangeStart * mesh->indexSize;
if (instances > 1) {
glDrawElementsInstanced(glDrawMode, count, indexType, (GLvoid*) offset, instances);
} else {
glDrawElements(glDrawMode, count, indexType, (GLvoid*) offset);
}
} else {
size_t count = mesh->rangeCount ? mesh->rangeCount : lovrMeshGetVertexCount(mesh);
if (instances > 1) {
glDrawArraysInstanced(glDrawMode, mesh->rangeStart, count, instances);
} else {
glDrawArrays(glDrawMode, mesh->rangeStart, count);
}
}
state.stats.drawCalls++;
}
2018-07-17 10:35:01 +00:00
VertexFormat* lovrMeshGetVertexFormat(Mesh* mesh) {
return &mesh->format;
2018-07-17 06:51:11 +00:00
}
2018-07-17 10:35:01 +00:00
MeshDrawMode lovrMeshGetDrawMode(Mesh* mesh) {
return mesh->drawMode;
2018-07-17 06:51:11 +00:00
}
2018-07-17 10:35:01 +00:00
void lovrMeshSetDrawMode(Mesh* mesh, MeshDrawMode drawMode) {
mesh->drawMode = drawMode;
}
2018-07-17 06:51:11 +00:00
2018-07-17 10:35:01 +00:00
int lovrMeshGetVertexCount(Mesh* mesh) {
return mesh->count;
2018-07-17 06:51:11 +00:00
}
2018-07-17 10:35:01 +00:00
bool lovrMeshIsAttributeEnabled(Mesh* mesh, const char* name) {
MeshAttachment* attachment = map_get(&mesh->attachments, name);
lovrAssert(attachment, "Mesh does not have an attribute named '%s'", name);
return attachment->enabled;
}
void lovrMeshSetAttributeEnabled(Mesh* mesh, const char* name, bool enable) {
MeshAttachment* attachment = map_get(&mesh->attachments, name);
lovrAssert(attachment, "Mesh does not have an attribute named '%s'", name);
attachment->enabled = enable;
2018-07-17 06:51:11 +00:00
}
2018-07-17 10:35:01 +00:00
void lovrMeshGetDrawRange(Mesh* mesh, uint32_t* start, uint32_t* count) {
*start = mesh->rangeStart;
*count = mesh->rangeCount;
}
2018-07-17 06:51:11 +00:00
2018-07-17 10:35:01 +00:00
void lovrMeshSetDrawRange(Mesh* mesh, uint32_t start, uint32_t count) {
uint32_t limit = mesh->indexCount > 0 ? mesh->indexCount : mesh->count;
lovrAssert(start + count <= limit, "Invalid mesh draw range [%d, %d]", start + 1, start + count + 1);
mesh->rangeStart = start;
mesh->rangeCount = count;
2018-07-17 06:51:11 +00:00
}
2018-07-17 10:35:01 +00:00
Material* lovrMeshGetMaterial(Mesh* mesh) {
return mesh->material;
}
void lovrMeshSetMaterial(Mesh* mesh, Material* material) {
if (mesh->material != material) {
lovrRetain(material);
lovrRelease(mesh->material);
mesh->material = material;
2018-07-17 06:51:11 +00:00
}
}
2018-07-17 10:35:01 +00:00
float* lovrMeshGetPose(Mesh* mesh) {
return mesh->pose;
}
2018-07-17 06:51:11 +00:00
2018-07-17 10:35:01 +00:00
void lovrMeshSetPose(Mesh* mesh, float* pose) {
mesh->pose = pose;
}
2018-07-17 06:51:11 +00:00
2018-07-17 10:35:01 +00:00
VertexPointer lovrMeshMapVertices(Mesh* mesh, uint32_t start, uint32_t count, bool read, bool write) {
if (write) {
mesh->dirtyStart = MIN(mesh->dirtyStart, start);
mesh->dirtyEnd = MAX(mesh->dirtyEnd, start + count);
2018-07-17 06:51:11 +00:00
}
2018-07-17 10:35:01 +00:00
return (VertexPointer) { .bytes = mesh->data.bytes + start * mesh->format.stride };
2018-07-17 06:51:11 +00:00
}
2018-07-17 10:35:01 +00:00
void lovrMeshUnmapVertices(Mesh* mesh) {
if (mesh->dirtyEnd == 0) {
return;
}
size_t stride = mesh->format.stride;
lovrGpuBindVertexBuffer(mesh->vbo);
if (mesh->usage == USAGE_STREAM) {
2018-07-17 10:35:01 +00:00
glBufferData(GL_ARRAY_BUFFER, mesh->count * stride, mesh->data.bytes, mesh->usage);
} else {
size_t offset = mesh->dirtyStart * stride;
size_t count = (mesh->dirtyEnd - mesh->dirtyStart) * stride;
glBufferSubData(GL_ARRAY_BUFFER, offset, count, mesh->data.bytes + offset);
2018-07-17 06:51:11 +00:00
}
2018-07-17 10:35:01 +00:00
mesh->dirtyStart = INT_MAX;
mesh->dirtyEnd = 0;
2018-07-17 06:51:11 +00:00
}
2018-07-17 10:35:01 +00:00
IndexPointer lovrMeshReadIndices(Mesh* mesh, uint32_t* count, size_t* size) {
*size = mesh->indexSize;
*count = mesh->indexCount;
if (mesh->indexCount == 0) {
return (IndexPointer) { .raw = NULL };
} else if (mesh->mappedIndices) {
lovrMeshUnmapIndices(mesh);
2018-07-17 06:51:11 +00:00
}
2018-07-17 10:35:01 +00:00
return mesh->indices;
2018-07-17 06:51:11 +00:00
}
2018-07-17 10:35:01 +00:00
IndexPointer lovrMeshWriteIndices(Mesh* mesh, uint32_t count, size_t size) {
if (mesh->mappedIndices) {
lovrMeshUnmapIndices(mesh);
}
mesh->indexSize = size;
mesh->indexCount = count;
if (count == 0) {
return (IndexPointer) { .raw = NULL };
}
lovrGpuBindVertexArray(mesh->vao);
lovrGpuBindIndexBuffer(mesh->ibo);
mesh->mappedIndices = true;
if (mesh->indexCapacity < size * count) {
mesh->indexCapacity = nextPo2(size * count);
mesh->indices.raw = realloc(mesh->indices.raw, mesh->indexCapacity);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, mesh->indexCapacity, NULL, mesh->usage);
}
return mesh->indices;
2018-07-17 06:51:11 +00:00
}
2018-07-17 10:35:01 +00:00
void lovrMeshUnmapIndices(Mesh* mesh) {
if (!mesh->mappedIndices) {
return;
2018-07-17 06:51:11 +00:00
}
2018-07-17 10:35:01 +00:00
mesh->mappedIndices = false;
lovrGpuBindIndexBuffer(mesh->ibo);
glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, mesh->indexCount * mesh->indexSize, mesh->indices.raw);
2018-07-17 06:51:11 +00:00
}
2018-07-17 10:35:01 +00:00
void lovrMeshResize(Mesh* mesh, uint32_t count) {
if (mesh->count < count) {
mesh->count = nextPo2(count);
lovrGpuBindVertexBuffer(mesh->vbo);
mesh->data.raw = realloc(mesh->data.raw, count * mesh->format.stride);
glBufferData(GL_ARRAY_BUFFER, count * mesh->format.stride, mesh->data.raw, mesh->usage);
2018-07-17 06:51:11 +00:00
}
}