Move over texture binding;

This commit is contained in:
bjorn 2018-07-11 16:43:31 -07:00
parent 3c623bdc2e
commit c35415ba66
9 changed files with 59 additions and 44 deletions

View File

@ -1,4 +1,5 @@
#include "graphics/font.h"
#include "graphics/gpu.h"
#include "graphics/graphics.h"
#include "graphics/texture.h"
#include "data/rasterizer.h"
@ -275,7 +276,7 @@ void lovrFontAddGlyph(Font* font, Glyph* glyph) {
glyph->y = atlas->y;
// Paste glyph into texture
lovrGraphicsBindTexture(font->texture, GL_TEXTURE_2D, 0);
gpuBindTexture(font->texture, 0);
glTexSubImage2D(GL_TEXTURE_2D, 0, atlas->x, atlas->y, glyph->tw, glyph->th, GL_RGB, GL_UNSIGNED_BYTE, glyph->data);
// Advance atlas cursor

View File

@ -1,3 +1,4 @@
#include "graphics/texture.h"
#include <stdint.h>
#include <stdbool.h>
@ -10,10 +11,13 @@ typedef struct {
typedef void (*gpuProc)(void);
void gpuInit(bool srgb, gpuProc (*getProcAddress)(const char*));
void gpuDestroy();
void gpuPresent();
void gpuBindFramebuffer(uint32_t framebuffer);
void gpuBindIndexBuffer(uint32_t indexBuffer);
void gpuBindTexture(Texture* texture, int slot);
void gpuBindVertexArray(uint32_t vertexArray);
void gpuBindVertexBuffer(uint32_t vertexBuffer);
Texture* gpuGetTexture(int slot);
void gpuSetViewport(uint32_t viewport[4]);
void gpuUseProgram(uint32_t program);

View File

@ -68,16 +68,13 @@ void lovrGraphicsDestroy() {
lovrGraphicsSetShader(NULL);
lovrGraphicsSetFont(NULL);
lovrGraphicsSetCanvas(NULL, 0);
for (int i = 0; i < MAX_TEXTURES; i++) {
lovrRelease(state.textures[i]);
}
for (int i = 0; i < DEFAULT_SHADER_COUNT; i++) {
lovrRelease(state.defaultShaders[i]);
}
lovrRelease(state.defaultMaterial);
lovrRelease(state.defaultFont);
lovrRelease(state.defaultTexture);
lovrRelease(state.mesh);
gpuDestroy();
memset(&state, 0, sizeof(GraphicsState));
}
@ -1179,30 +1176,6 @@ void lovrGraphicsSetViewport(uint32_t x, uint32_t y, uint32_t width, uint32_t he
state.layers[state.layer].viewport[3] = height;
}
Texture* lovrGraphicsGetTexture(int slot) {
return state.textures[slot];
}
void lovrGraphicsBindTexture(Texture* texture, GLenum type, int slot) {
if (!texture) {
if (!state.defaultTexture) {
TextureData* textureData = lovrTextureDataGetBlank(1, 1, 0xff, FORMAT_RGBA);
state.defaultTexture = lovrTextureCreate(TEXTURE_2D, &textureData, 1, true, false);
lovrRelease(textureData);
}
texture = state.defaultTexture;
}
if (texture != state.textures[slot]) {
lovrRetain(texture);
lovrRelease(state.textures[slot]);
state.textures[slot] = texture;
glActiveTexture(GL_TEXTURE0 + slot);
glBindTexture(type, lovrTextureGetId(texture));
}
}
Material* lovrGraphicsGetDefaultMaterial() {
if (!state.defaultMaterial) {
state.defaultMaterial = lovrMaterialCreate(true);

View File

@ -98,7 +98,6 @@ typedef struct {
Shader* defaultShaders[DEFAULT_SHADER_COUNT];
Material* defaultMaterial;
Font* defaultFont;
Texture* defaultTexture;
float transforms[MAX_TRANSFORMS + INTERNAL_TRANSFORMS][16];
int transform;
Layer layers[MAX_LAYERS];
@ -124,7 +123,6 @@ typedef struct {
Winding winding;
bool wireframe;
Mesh* mesh;
Texture* textures[MAX_TEXTURES];
bool stencilEnabled;
bool stencilWriting;
GraphicsStats stats;
@ -205,6 +203,4 @@ void lovrGraphicsPushLayer(Canvas* canvas);
void lovrGraphicsPopLayer();
void lovrGraphicsSetCamera(mat4 projection, mat4 view);
void lovrGraphicsSetViewport(uint32_t x, uint32_t y, uint32_t width, uint32_t height);
Texture* lovrGraphicsGetTexture(int slot);
void lovrGraphicsBindTexture(Texture* texture, GLenum type, int slot);
Material* lovrGraphicsGetDefaultMaterial();

View File

@ -106,7 +106,7 @@ void lovrCanvasResolve(Canvas* canvas) {
}
if (canvas->flags.mipmaps) {
lovrGraphicsBindTexture(&canvas->texture, canvas->texture.type, 0);
gpuBindTexture(&canvas->texture, 0);
glGenerateMipmap(canvas->texture.glType);
}
}

View File

@ -1,11 +1,16 @@
#include "graphics/gpu.h"
#include "graphics/opengl/opengl.h"
#include "lib/glfw.h"
#include <string.h>
#define MAX_TEXTURES 16
static struct {
Texture* defaultTexture;
uint32_t framebuffer;
uint32_t indexBuffer;
uint32_t program;
Texture* textures[MAX_TEXTURES];
uint32_t vertexArray;
uint32_t vertexBuffer;
uint32_t viewport[4];
@ -28,6 +33,13 @@ void gpuInit(bool srgb, gpuProc (*getProcAddress)(const char*)) {
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
}
void gpuDestroy() {
lovrRelease(state.defaultTexture);
for (int i = 0; i < MAX_TEXTURES; i++) {
lovrRelease(state.textures[i]);
}
}
void gpuPresent() {
memset(&state.stats, 0, sizeof(state.stats));
}
@ -46,6 +58,28 @@ void gpuBindIndexBuffer(uint32_t indexBuffer) {
}
}
void gpuBindTexture(Texture* texture, int slot) {
lovrAssert(slot >= 0 && slot < MAX_TEXTURES, "Invalid texture slot %d", slot);
if (!texture) {
if (!state.defaultTexture) {
TextureData* textureData = lovrTextureDataGetBlank(1, 1, 0xff, FORMAT_RGBA);
state.defaultTexture = lovrTextureCreate(TEXTURE_2D, &textureData, 1, true, false);
lovrRelease(textureData);
}
texture = state.defaultTexture;
}
if (texture != state.textures[slot]) {
lovrRetain(texture);
lovrRelease(state.textures[slot]);
state.textures[slot] = texture;
glActiveTexture(GL_TEXTURE0 + slot);
glBindTexture(texture->glType, lovrTextureGetId(texture));
}
}
void gpuBindVertexArray(uint32_t vertexArray) {
if (state.vertexArray != vertexArray) {
state.vertexArray = vertexArray;
@ -60,6 +94,11 @@ void gpuBindVertexBuffer(uint32_t vertexBuffer) {
}
}
Texture* gpuGetTexture(int slot) {
lovrAssert(slot >= 0 && slot < MAX_TEXTURES, "Invalid texture slot %d", slot);
return state.textures[slot];
}
void gpuSetViewport(uint32_t viewport[4]) {
if (memcmp(state.viewport, viewport, 4 * sizeof(uint32_t))) {
memcpy(state.viewport, viewport, 4 * sizeof(uint32_t));

View File

@ -319,14 +319,15 @@ void lovrShaderBind(Shader* shader) {
case UNIFORM_SAMPLER:
for (int i = 0; i < count; i++) {
TextureType type;
GLenum uniformTextureType;
switch (uniform->glType) {
case GL_SAMPLER_2D: type = GL_TEXTURE_2D; break;
case GL_SAMPLER_3D: type = GL_TEXTURE_3D; break;
case GL_SAMPLER_CUBE: type = GL_TEXTURE_CUBE_MAP; break;
case GL_SAMPLER_2D_ARRAY: type = GL_TEXTURE_2D_ARRAY; break;
case GL_SAMPLER_2D: uniformTextureType = GL_TEXTURE_2D; break;
case GL_SAMPLER_3D: uniformTextureType = GL_TEXTURE_3D; break;
case GL_SAMPLER_CUBE: uniformTextureType = GL_TEXTURE_CUBE_MAP; break;
case GL_SAMPLER_2D_ARRAY: uniformTextureType = GL_TEXTURE_2D_ARRAY; break;
}
lovrGraphicsBindTexture(uniform->value.textures[i], type, uniform->baseTextureSlot + i);
lovrAssert(uniform->value.textures[i]->glType == uniformTextureType, "Texture uniform types do not match");
gpuBindTexture(uniform->value.textures[i], uniform->baseTextureSlot + i);
}
break;
}

View File

@ -117,7 +117,7 @@ Texture* lovrTextureCreate(TextureType type, TextureData** slices, int depth, bo
WrapMode wrap = type == TEXTURE_CUBE ? WRAP_CLAMP : WRAP_REPEAT;
glGenTextures(1, &texture->id);
lovrGraphicsBindTexture(texture, texture->glType, 0);
gpuBindTexture(texture, 0);
lovrTextureSetFilter(texture, lovrGraphicsGetDefaultFilter());
lovrTextureSetWrap(texture, (TextureWrap) { .s = wrap, .t = wrap, .r = wrap });
@ -221,7 +221,7 @@ TextureFilter lovrTextureGetFilter(Texture* texture) {
void lovrTextureSetFilter(Texture* texture, TextureFilter filter) {
float anisotropy = filter.mode == FILTER_ANISOTROPIC ? MAX(filter.anisotropy, 1.) : 1.;
lovrGraphicsBindTexture(texture, texture->glType, 0);
gpuBindTexture(texture, 0);
texture->filter = filter;
switch (filter.mode) {
@ -261,7 +261,7 @@ TextureWrap lovrTextureGetWrap(Texture* texture) {
void lovrTextureSetWrap(Texture* texture, TextureWrap wrap) {
texture->wrap = wrap;
lovrGraphicsBindTexture(texture, texture->glType, 0);
gpuBindTexture(texture, 0);
glTexParameteri(texture->glType, GL_TEXTURE_WRAP_S, lovrConvertWrapMode(wrap.s));
glTexParameteri(texture->glType, GL_TEXTURE_WRAP_T, lovrConvertWrapMode(wrap.t));
if (texture->type == TEXTURE_CUBE || texture->type == TEXTURE_VOLUME) {

View File

@ -1,5 +1,6 @@
#include "event/event.h"
#include "graphics/graphics.h"
#include "graphics/gpu.h"
#include "graphics/canvas.h"
#include "math/mat4.h"
#include "math/quat.h"
@ -693,7 +694,7 @@ static void openvrRenderTo(void (*callback)(void*), void* userdata) {
// Submit
glActiveTexture(GL_TEXTURE0);
Texture* oldTexture = lovrGraphicsGetTexture(0);
Texture* oldTexture = gpuGetTexture(0);
uintptr_t texture = (uintptr_t) lovrTextureGetId((Texture*) state.canvas);
EColorSpace colorSpace = lovrGraphicsIsGammaCorrect() ? EColorSpace_ColorSpace_Linear : EColorSpace_ColorSpace_Gamma;
Texture_t eyeTexture = { (void*) texture, ETextureType_TextureType_OpenGL, colorSpace };