Add opengl header; Migrate relevant object implementations;

This commit is contained in:
bjorn 2018-07-09 02:05:30 -07:00
parent eb456f5b52
commit 8bf60b73ec
12 changed files with 158 additions and 158 deletions

View File

@ -12,7 +12,7 @@ static struct TempData tempData;
int l_lovrShaderHasUniform(lua_State* L) {
Shader* shader = luax_checktype(L, 1, Shader);
const char* name = luaL_checkstring(L, 2);
lua_pushboolean(L, lovrShaderGetUniform(shader, name) != NULL);
lua_pushboolean(L, lovrShaderHasUniform(shader, name));
return 1;
}
@ -21,23 +21,25 @@ int l_lovrShaderSend(lua_State* L) {
const char* name = luaL_checkstring(L, 2);
lua_settop(L, 3);
Uniform* uniform = lovrShaderGetUniform(shader, name);
int count;
int components;
size_t size;
UniformType type;
bool present = lovrShaderGetUniform(shader, name, &count, &components, &size, &type);
if (!uniform) {
if (!present) {
return luaL_error(L, "Unknown shader variable '%s'", name);
}
if (tempData.size < uniform->size) {
tempData.size = uniform->size;
if (tempData.size < size) {
tempData.size = size;
tempData.data = realloc(tempData.data, tempData.size);
}
int* ints = (int*) tempData.data;
float* floats = (float*) tempData.data;
Texture** textures = (Texture**) tempData.data;
int n = 1;
int components = uniform->components;
if (components > 1 && lua_istable(L, 3)) {
lua_rawgeti(L, 3, 1);
@ -50,18 +52,17 @@ int l_lovrShaderSend(lua_State* L) {
}
n = lua_objlen(L, -1);
if (n != uniform->count) {
const char* elements = uniform->count == 1 ? "element" : "elements";
return luaL_error(L, "Expected %d %s for array '%s', got %d", uniform->count, elements, uniform->name, n);
if (n != count) {
return luaL_error(L, "Expected %d element%s for array '%s', got %d", count, count == 1 ? "" : "s", name, n);
}
}
Blob** blob = luax_totype(L, 3, Blob);
switch (uniform->type) {
switch (type) {
case UNIFORM_FLOAT:
if (blob) {
n = uniform->count;
n = count;
floats = (float*) (*blob)->data;
size_t count = n * components;
size_t capacity = (*blob)->size / sizeof(float);
@ -74,7 +75,7 @@ int l_lovrShaderSend(lua_State* L) {
for (int i = 0; i < n; i++) {
lua_rawgeti(L, -1, i + 1);
if (lua_type(L, -1) != LUA_TTABLE || (int) lua_objlen(L, -1) != components) {
return luaL_error(L, "Expected %d components for uniform '%s' #%d, got %d", components, uniform->name, lua_objlen(L, -1));
return luaL_error(L, "Expected %d components for uniform '%s' #%d, got %d", components, name, lua_objlen(L, -1));
}
for (int j = 0; j < components; j++) {
lua_rawgeti(L, -1, j + 1);
@ -89,7 +90,7 @@ int l_lovrShaderSend(lua_State* L) {
case UNIFORM_INT:
if (blob) {
n = uniform->count;
n = count;
ints = (int*) (*blob)->data;
size_t count = n * components;
size_t capacity = (*blob)->size / sizeof(int);
@ -102,7 +103,7 @@ int l_lovrShaderSend(lua_State* L) {
for (int i = 0; i < n; i++) {
lua_rawgeti(L, -1, i + 1);
if (lua_type(L, -1) != LUA_TTABLE || (int) lua_objlen(L, -1) != components) {
return luaL_error(L, "Expected %d components for uniform '%s' #%d, got %d", components, uniform->name, lua_objlen(L, -1));
return luaL_error(L, "Expected %d components for uniform '%s' #%d, got %d", components, name, lua_objlen(L, -1));
}
for (int j = 0; j < components; j++) {
lua_rawgeti(L, -1, j + 1);
@ -117,7 +118,7 @@ int l_lovrShaderSend(lua_State* L) {
case UNIFORM_MATRIX:
if (blob) {
n = uniform->count;
n = count;
floats = (float*) (*blob)->data;
size_t count = n * components * components;
size_t capacity = (*blob)->size / sizeof(float);

View File

@ -13,15 +13,6 @@ typedef struct {
typedef struct Canvas Canvas;
struct Canvas {
Texture texture;
GLuint framebuffer;
GLuint resolveFramebuffer;
GLuint depthStencilBuffer;
GLuint msaaTexture;
CanvasFlags flags;
};
bool lovrCanvasSupportsFormat(TextureFormat format);
Canvas* lovrCanvasCreate(int width, int height, TextureFormat format, CanvasFlags flags);

View File

@ -1056,7 +1056,7 @@ void lovrGraphicsDraw(Mesh* mesh, mat4 transform, DefaultShader defaultShader, i
mat4_multiply(mat4_set(modelView, layer.view), model);
lovrShaderSetMatrix(shader, "lovrTransform", modelView, 16);
if (lovrShaderGetUniform(shader, "lovrNormalMatrix")) {
if (lovrShaderHasUniform(shader, "lovrNormalMatrix")) {
if (mat4_invert(modelView)) {
mat4_transpose(modelView);
} else {

View File

@ -1,9 +1,6 @@
#include "graphics/gpu.h"
#include "graphics/material.h"
#include "graphics/shader.h"
#include "data/vertexData.h"
#include "lib/map/map.h"
#include "util.h"
#include <stdbool.h>
#pragma once
@ -28,41 +25,6 @@ typedef enum {
typedef struct Mesh Mesh;
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;
};
Mesh* lovrMeshCreate(uint32_t count, VertexFormat format, MeshDrawMode drawMode, MeshUsage usage);
void lovrMeshDestroy(void* ref);
void lovrMeshAttachAttribute(Mesh* mesh, Mesh* other, const char* name, int divisor);

View File

@ -1,9 +1,6 @@
#include "graphics/canvas.h"
#include "graphics/gpu.h"
#include "graphics/opengl/opengl.h"
#include "graphics/graphics.h"
#include "data/blob.h"
#include "util.h"
#include <stdlib.h>
#include "graphics/gpu.h"
bool lovrCanvasSupportsFormat(TextureFormat format) {
switch (format) {
@ -110,7 +107,7 @@ void lovrCanvasResolve(Canvas* canvas) {
if (canvas->flags.mipmaps) {
lovrGraphicsBindTexture(&canvas->texture, canvas->texture.type, 0);
glGenerateMipmap(canvas->texture.type);
glGenerateMipmap(canvas->texture.glType);
}
}

View File

@ -1,11 +1,6 @@
#include "graphics/mesh.h"
#include "graphics/gpu.h"
#include "graphics/graphics.h"
#include "graphics/opengl/opengl.h"
#include "math/math.h"
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
static GLenum convertMeshUsage(MeshUsage usage) {
switch (usage) {
@ -240,15 +235,15 @@ void lovrMeshUnmapVertices(Mesh* mesh) {
}
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);
}
*size = mesh->indexSize;
*count = mesh->indexCount;
return mesh->indices;
}

View File

@ -0,0 +1,105 @@
#include "graphics/shader.h"
#include "graphics/texture.h"
#include "graphics/canvas.h"
#include "graphics/mesh.h"
#include "util.h"
#include "lib/map/map.h"
#include <stdint.h>
#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
#define LOVR_MAX_UNIFORM_LENGTH 256
#define LOVR_MAX_ATTRIBUTE_LENGTH 256
typedef struct {
GLchar name[LOVR_MAX_UNIFORM_LENGTH];
GLenum glType;
int index;
int location;
int count;
int components;
size_t size;
UniformType type;
union {
void* data;
int* ints;
float* floats;
Texture** textures;
} value;
int baseTextureSlot;
bool dirty;
} Uniform;
typedef map_t(Uniform) map_uniform_t;
struct Shader {
Ref ref;
uint32_t program;
map_uniform_t uniforms;
map_int_t attributes;
};
struct Texture {
Ref ref;
TextureType type;
GLenum glType;
TextureData** slices;
int width;
int height;
int depth;
GLuint id;
TextureFilter filter;
TextureWrap wrap;
bool srgb;
bool mipmaps;
bool allocated;
};
struct Canvas {
Texture texture;
GLuint framebuffer;
GLuint resolveFramebuffer;
GLuint depthStencilBuffer;
GLuint msaaTexture;
CanvasFlags flags;
};
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;
};

View File

@ -1,11 +1,10 @@
#include "graphics/shader.h"
#include "graphics/gpu.h"
#include "graphics/opengl/opengl.h"
#include "graphics/graphics.h"
#include "math/math.h"
#include "resources/shaders.h"
#include <stdio.h>
#include <stdlib.h>
#include "math/math.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
static UniformType getUniformType(GLenum type, const char* debug) {
switch (type) {
@ -179,22 +178,22 @@ Shader* lovrShaderCreate(const char* vertexSource, const char* fragmentSource) {
switch (uniform.type) {
case UNIFORM_FLOAT:
uniform.size = uniform.components * uniform.count * sizeof(float);
uniform.value.data = malloc(uniform.size);
uniform.value.data = calloc(1, uniform.size);
break;
case UNIFORM_INT:
uniform.size = uniform.components * uniform.count * sizeof(int);
uniform.value.data = malloc(uniform.size);
uniform.value.data = calloc(1, uniform.size);
break;
case UNIFORM_MATRIX:
uniform.size = uniform.components * uniform.components * uniform.count * sizeof(int);
uniform.value.data = malloc(uniform.size);
uniform.value.data = calloc(1, uniform.size);
break;
case UNIFORM_SAMPLER:
uniform.size = uniform.components * uniform.count * MAX(sizeof(Texture*), sizeof(int));
uniform.value.data = malloc(uniform.size);
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++) {
@ -204,8 +203,6 @@ Shader* lovrShaderCreate(const char* vertexSource, const char* fragmentSource) {
break;
}
memset(uniform.value.data, 0, uniform.size);
size_t offset = 0;
for (int j = 0; j < uniform.count; j++) {
int location = uniform.location;
@ -341,8 +338,21 @@ int lovrShaderGetAttributeId(Shader* shader, const char* name) {
return id ? *id : -1;
}
Uniform* lovrShaderGetUniform(Shader* shader, const char* name) {
return map_get(&shader->uniforms, name);
bool lovrShaderHasUniform(Shader* shader, const char* name) {
return map_get(&shader->uniforms, name) != NULL;
}
bool lovrShaderGetUniform(Shader* shader, const char* name, int* count, int* components, size_t* size, UniformType* type) {
Uniform* uniform = map_get(&shader->uniforms, name);
if (!uniform) {
return false;
}
*count = uniform->count;
*components = uniform->components;
*size = uniform->size;
*type = uniform->type;
return true;
}
static void lovrShaderSetUniform(Shader* shader, const char* name, UniformType type, void* data, int count, size_t size, const char* debug) {

View File

@ -1,11 +1,9 @@
#include "graphics/texture.h"
#include "graphics/opengl/opengl.h"
#include "graphics/graphics.h"
#include "data/blob.h"
#include "math/math.h"
#include "graphics/gpu.h"
#include "lib/vec/vec.h"
#include "math/math.h"
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
GLenum convertWrap(WrapMode mode) {
switch (mode) {

View File

@ -1,21 +1,8 @@
#include "graphics/texture.h"
#include "graphics/gpu.h"
#include "lib/map/map.h"
#include "util.h"
#include <stdbool.h>
#pragma once
#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
#define LOVR_MAX_UNIFORM_LENGTH 256
#define LOVR_MAX_ATTRIBUTE_LENGTH 256
typedef enum {
UNIFORM_FLOAT,
UNIFORM_MATRIX,
@ -23,13 +10,6 @@ typedef enum {
UNIFORM_SAMPLER
} UniformType;
typedef union {
void* data;
int* ints;
float* floats;
Texture** textures;
} UniformValue;
typedef enum {
SHADER_DEFAULT,
SHADER_CUBE,
@ -38,38 +18,16 @@ typedef enum {
SHADER_FILL
} DefaultShader;
typedef struct {
GLchar name[LOVR_MAX_UNIFORM_LENGTH];
GLenum glType;
int index;
int location;
int count;
int components;
size_t size;
UniformType type;
UniformValue value;
int baseTextureSlot;
bool dirty;
} Uniform;
typedef map_t(Uniform) map_uniform_t;
typedef struct Shader Shader;
struct Shader {
Ref ref;
uint32_t program;
map_uniform_t uniforms;
map_int_t attributes;
};
Shader* lovrShaderCreate(const char* vertexSource, const char* fragmentSource);
Shader* lovrShaderCreateDefault(DefaultShader type);
void lovrShaderDestroy(void* ref);
uint32_t lovrShaderGetProgram(Shader* shader);
void lovrShaderBind(Shader* shader);
int lovrShaderGetAttributeId(Shader* shader, const char* name);
Uniform* lovrShaderGetUniform(Shader* shader, const char* name);
bool lovrShaderHasUniform(Shader* shader, const char* name);
bool lovrShaderGetUniform(Shader* shader, const char* name, int* count, int* components, size_t* size, UniformType* type);
void lovrShaderSetFloat(Shader* shader, const char* name, float* data, int count);
void lovrShaderSetInt(Shader* shader, const char* name, int* data, int count);
void lovrShaderSetMatrix(Shader* shader, const char* name, float* data, int count);

View File

@ -1,6 +1,5 @@
#include "graphics/gpu.h"
#include "data/textureData.h"
#include "util.h"
#include <stdbool.h>
#pragma once
@ -38,22 +37,6 @@ typedef struct {
typedef struct Texture Texture;
struct Texture {
Ref ref;
TextureType type;
GLenum glType;
TextureData** slices;
int width;
int height;
int depth;
GLuint id;
TextureFilter filter;
TextureWrap wrap;
bool srgb;
bool mipmaps;
bool allocated;
};
GLenum lovrTextureFormatGetGLFormat(TextureFormat format);
GLenum lovrTextureFormatGetGLInternalFormat(TextureFormat format, bool srgb);
bool lovrTextureFormatIsCompressed(TextureFormat format);