mirror of
https://github.com/gnif/LookingGlass.git
synced 2025-08-05 18:24:08 +00:00
[client] added renderer abstratction
This moves the bulk of the rendering code into seperate rendering modules cleaning up much of intertwined SDL & OpenGL mess.
This commit is contained in:
146
client/renderers/basic.c
Normal file
146
client/renderers/basic.c
Normal file
@@ -0,0 +1,146 @@
|
||||
#include "lg-renderer.h"
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <SDL2/SDL.h>
|
||||
|
||||
#include "debug.h"
|
||||
#include "memcpySSE.h"
|
||||
|
||||
struct LGR_Basic
|
||||
{
|
||||
bool initialized;
|
||||
LG_RendererFormat format;
|
||||
size_t texSize;
|
||||
size_t dataWidth;
|
||||
SDL_Renderer * renderer;
|
||||
SDL_Texture * texture;
|
||||
};
|
||||
|
||||
const char * lgr_basic_get_name()
|
||||
{
|
||||
return "Basic";
|
||||
}
|
||||
|
||||
bool lgr_basic_initialize(void ** opaque, const LG_RendererParams params, const LG_RendererFormat format)
|
||||
{
|
||||
// create our local storage
|
||||
*opaque = malloc(sizeof(struct LGR_Basic));
|
||||
if (!*opaque)
|
||||
{
|
||||
DEBUG_INFO("Failed to allocate %lu bytes", sizeof(struct LGR_Basic));
|
||||
return false;
|
||||
}
|
||||
memset(*opaque, 0, sizeof(struct LGR_Basic));
|
||||
struct LGR_Basic * this = (struct LGR_Basic *)*opaque;
|
||||
|
||||
Uint32 sdlFormat;
|
||||
switch(format.bpp)
|
||||
{
|
||||
case 24:
|
||||
sdlFormat = SDL_PIXELFORMAT_RGB24;
|
||||
break;
|
||||
|
||||
case 32:
|
||||
sdlFormat = SDL_PIXELFORMAT_ARGB8888;
|
||||
break;
|
||||
|
||||
default:
|
||||
DEBUG_ERROR("Unsupported bpp");
|
||||
return false;
|
||||
}
|
||||
|
||||
// calculate the texture size in bytes
|
||||
this->texSize = format.height * format.pitch;
|
||||
|
||||
// create the target texture
|
||||
this->texture = SDL_CreateTexture(
|
||||
params.renderer,
|
||||
sdlFormat,
|
||||
SDL_TEXTUREACCESS_STREAMING,
|
||||
format.width,
|
||||
format.height
|
||||
);
|
||||
|
||||
if (!this->texture)
|
||||
{
|
||||
DEBUG_ERROR("SDL_CreateTexture failed");
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
memcpy(&this->format, &format, sizeof(LG_RendererFormat));
|
||||
this->renderer = params.renderer;
|
||||
this->dataWidth = this->format.width * (this->format.bpp / 8);
|
||||
this->initialized = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
void lgr_basic_deinitialize(void * opaque)
|
||||
{
|
||||
struct LGR_Basic * this = (struct LGR_Basic *)opaque;
|
||||
if (!this)
|
||||
return;
|
||||
|
||||
if (this->texture)
|
||||
SDL_DestroyTexture(this->texture);
|
||||
|
||||
free(this);
|
||||
}
|
||||
|
||||
bool lgr_basic_is_compatible(void * opaque, const LG_RendererFormat format)
|
||||
{
|
||||
const struct LGR_Basic * this = (struct LGR_Basic *)opaque;
|
||||
if (!this || !this->initialized)
|
||||
return false;
|
||||
|
||||
return (memcmp(&this->format, &format, sizeof(LG_RendererFormat)) == 0);
|
||||
}
|
||||
|
||||
bool lgr_basic_render(void * opaque, const LG_RendererRect destRect, const uint8_t * data, bool resample)
|
||||
{
|
||||
struct LGR_Basic * this = (struct LGR_Basic *)opaque;
|
||||
if (!this || !this->initialized)
|
||||
return false;
|
||||
|
||||
int pitch;
|
||||
uint8_t * dest;
|
||||
|
||||
if (SDL_LockTexture(this->texture, NULL, (void**)&dest, &pitch) != 0)
|
||||
{
|
||||
DEBUG_ERROR("Failed to lock the texture for update");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (pitch == this->format.pitch)
|
||||
memcpySSE(dest, data, this->texSize);
|
||||
else
|
||||
{
|
||||
for(unsigned int y = 0; y < this->format.height; ++y)
|
||||
{
|
||||
memcpySSE(dest, data, this->dataWidth);
|
||||
dest += pitch;
|
||||
data += this->format.pitch;
|
||||
}
|
||||
}
|
||||
|
||||
SDL_Rect rect;
|
||||
rect.x = destRect.x;
|
||||
rect.y = destRect.y;
|
||||
rect.w = destRect.w;
|
||||
rect.h = destRect.h;
|
||||
|
||||
SDL_UnlockTexture(this->texture);
|
||||
SDL_RenderCopy(this->renderer, this->texture, NULL, &rect);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
const LG_Renderer LGR_Basic =
|
||||
{
|
||||
.get_name = lgr_basic_get_name,
|
||||
.initialize = lgr_basic_initialize,
|
||||
.deinitialize = lgr_basic_deinitialize,
|
||||
.is_compatible = lgr_basic_is_compatible,
|
||||
.render = lgr_basic_render
|
||||
};
|
255
client/renderers/opengl.c
Normal file
255
client/renderers/opengl.c
Normal file
@@ -0,0 +1,255 @@
|
||||
#include "lg-renderer.h"
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#define GL_GLEXT_PROTOTYPES
|
||||
#include <GL/gl.h>
|
||||
#include <GL/glu.h>
|
||||
|
||||
#include "debug.h"
|
||||
#include "memcpySSE.h"
|
||||
|
||||
#define VBO_BUFFERS 2
|
||||
|
||||
struct LGR_OpenGL
|
||||
{
|
||||
bool initialized;
|
||||
LG_RendererFormat format;
|
||||
GLuint intFormat;
|
||||
GLuint vboFormat;
|
||||
size_t texSize;
|
||||
|
||||
bool hasBuffers;
|
||||
GLuint vboID[VBO_BUFFERS];
|
||||
uint8_t * texPixels[VBO_BUFFERS];
|
||||
int texIndex;
|
||||
|
||||
bool hasTextures;
|
||||
GLuint vboTex[VBO_BUFFERS];
|
||||
};
|
||||
|
||||
bool lgr_opengl_check_error(const char * name)
|
||||
{
|
||||
GLenum error = glGetError();
|
||||
if (error == GL_NO_ERROR)
|
||||
return false;
|
||||
|
||||
const GLubyte * errStr = gluErrorString(error);
|
||||
DEBUG_ERROR("%s = %d (%s)", name, error, errStr);
|
||||
return true;
|
||||
}
|
||||
|
||||
const char * lgr_opengl_get_name()
|
||||
{
|
||||
return "OpenGL";
|
||||
}
|
||||
|
||||
bool lgr_opengl_initialize(void ** opaque, const LG_RendererParams params, const LG_RendererFormat format)
|
||||
{
|
||||
// check if the GPU supports GL_ARB_buffer_storage first
|
||||
// there is no advantage to this renderer if it is not present.
|
||||
const GLubyte * extensions = glGetString(GL_EXTENSIONS);
|
||||
if (!gluCheckExtension((const GLubyte *)"GL_ARB_buffer_storage", extensions))
|
||||
{
|
||||
DEBUG_INFO("The GPU doesn't support GL_ARB_buffer_storage");
|
||||
return false;
|
||||
}
|
||||
|
||||
// create our local storage
|
||||
*opaque = malloc(sizeof(struct LGR_OpenGL));
|
||||
if (!*opaque)
|
||||
{
|
||||
DEBUG_INFO("Failed to allocate %lu bytes", sizeof(struct LGR_OpenGL));
|
||||
return false;
|
||||
}
|
||||
memset(*opaque, 0, sizeof(struct LGR_OpenGL));
|
||||
struct LGR_OpenGL * this = (struct LGR_OpenGL *)*opaque;
|
||||
|
||||
// assume 24 and 32 bit formats are RGB and RGBA
|
||||
switch(format.bpp)
|
||||
{
|
||||
case 24:
|
||||
this->intFormat = GL_RGB8;
|
||||
this->vboFormat = GL_BGR;
|
||||
break;
|
||||
|
||||
case 32:
|
||||
this->intFormat = GL_RGBA8;
|
||||
this->vboFormat = GL_BGRA;
|
||||
break;
|
||||
|
||||
default:
|
||||
DEBUG_INFO("%d bpp not supported", format.bpp);
|
||||
return false;
|
||||
}
|
||||
|
||||
// calculate the texture size in bytes
|
||||
this->texSize = format.height * format.pitch;
|
||||
|
||||
// generate the pixel unpack buffers
|
||||
glGenBuffers(VBO_BUFFERS, this->vboID);
|
||||
if (lgr_opengl_check_error("glGenBuffers"))
|
||||
return false;
|
||||
this->hasBuffers = true;
|
||||
|
||||
// persistant bind the buffers
|
||||
for (int i = 0; i < VBO_BUFFERS; ++i)
|
||||
{
|
||||
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, this->vboID[i]);
|
||||
if (lgr_opengl_check_error("glBindBuffer"))
|
||||
return false;
|
||||
|
||||
glBufferStorage(GL_PIXEL_UNPACK_BUFFER, this->texSize, 0, GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT);
|
||||
if (lgr_opengl_check_error("glBufferStorage"))
|
||||
return false;
|
||||
|
||||
this->texPixels[i] = glMapBufferRange(GL_PIXEL_UNPACK_BUFFER, 0, this->texSize,
|
||||
GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT | GL_MAP_FLUSH_EXPLICIT_BIT);
|
||||
if (lgr_opengl_check_error("glMapBufferRange"))
|
||||
return false;
|
||||
|
||||
if (!this->texPixels[i])
|
||||
{
|
||||
DEBUG_ERROR("Failed to map the buffer range");
|
||||
return false;
|
||||
}
|
||||
|
||||
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
|
||||
}
|
||||
|
||||
// create the textures
|
||||
glGenTextures(VBO_BUFFERS, this->vboTex);
|
||||
if (lgr_opengl_check_error("glGenTextures"))
|
||||
return false;
|
||||
this->hasTextures = true;
|
||||
|
||||
// bind the textures to the unpack buffers
|
||||
for (int i = 0; i < VBO_BUFFERS; ++i)
|
||||
{
|
||||
glBindTexture(GL_TEXTURE_2D, this->vboTex[i]);
|
||||
if (lgr_opengl_check_error("glBindTexture"))
|
||||
return false;
|
||||
|
||||
glTexImage2D(
|
||||
GL_TEXTURE_2D,
|
||||
0,
|
||||
this->intFormat,
|
||||
format.width, format.height,
|
||||
0,
|
||||
this->vboFormat,
|
||||
GL_UNSIGNED_BYTE,
|
||||
(void*)0
|
||||
);
|
||||
|
||||
if (lgr_opengl_check_error("glTexImage2D"))
|
||||
return false;
|
||||
}
|
||||
|
||||
glEnable(GL_TEXTURE_2D);
|
||||
|
||||
// copy the format into the local storage
|
||||
memcpy(&this->format, &format, sizeof(LG_RendererFormat));
|
||||
this->initialized = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
void lgr_opengl_deinitialize(void * opaque)
|
||||
{
|
||||
struct LGR_OpenGL * this = (struct LGR_OpenGL *)opaque;
|
||||
if (!this)
|
||||
return;
|
||||
|
||||
if (this->hasTextures)
|
||||
glDeleteTextures(VBO_BUFFERS, this->vboTex);
|
||||
|
||||
if (this->hasBuffers)
|
||||
glDeleteBuffers(VBO_BUFFERS, this->vboID);
|
||||
|
||||
free(this);
|
||||
}
|
||||
|
||||
bool lgr_opengl_is_compatible(void * opaque, const LG_RendererFormat format)
|
||||
{
|
||||
const struct LGR_OpenGL * this = (struct LGR_OpenGL *)opaque;
|
||||
if (!this || !this->initialized)
|
||||
return false;
|
||||
|
||||
return (memcmp(&this->format, &format, sizeof(LG_RendererFormat)) == 0);
|
||||
}
|
||||
|
||||
bool lgr_opengl_render(void * opaque, const LG_RendererRect destRect, const uint8_t * data, bool resample)
|
||||
{
|
||||
struct LGR_OpenGL * this = (struct LGR_OpenGL *)opaque;
|
||||
if (!this || !this->initialized)
|
||||
return false;
|
||||
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
|
||||
// copy the buffer to the texture
|
||||
memcpySSE(this->texPixels[this->texIndex], data, this->texSize);
|
||||
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, this->vboID[this->texIndex]);
|
||||
glFlushMappedBufferRange(GL_PIXEL_UNPACK_BUFFER, 0, this->texSize);
|
||||
|
||||
// bind the texture and update it
|
||||
glBindTexture(GL_TEXTURE_2D , this->vboTex[this->texIndex]);
|
||||
glPixelStorei(GL_UNPACK_ALIGNMENT , 4 );
|
||||
glPixelStorei(GL_UNPACK_ROW_LENGTH , this->format.width );
|
||||
glTexSubImage2D(
|
||||
GL_TEXTURE_2D,
|
||||
0,
|
||||
0, 0,
|
||||
this->format.width,
|
||||
this->format.height,
|
||||
this->vboFormat,
|
||||
GL_UNSIGNED_BYTE,
|
||||
(void*)0
|
||||
);
|
||||
|
||||
const bool mipmap = resample && (
|
||||
(this->format.width != destRect.w) ||
|
||||
(this->format.height != destRect.h));
|
||||
|
||||
// unbind the buffer
|
||||
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
|
||||
|
||||
// configure the texture
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
||||
if (mipmap)
|
||||
{
|
||||
glGenerateMipmap(GL_TEXTURE_2D);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
|
||||
}
|
||||
else
|
||||
{
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
||||
}
|
||||
|
||||
// draw the screen
|
||||
glEnable(GL_TEXTURE_2D);
|
||||
glBegin(GL_TRIANGLE_STRIP);
|
||||
glTexCoord2f(0.0f, 0.0f); glVertex2i(destRect.x , destRect.y );
|
||||
glTexCoord2f(1.0f, 0.0f); glVertex2i(destRect.x + destRect.w, destRect.y );
|
||||
glTexCoord2f(0.0f, 1.0f); glVertex2i(destRect.x , destRect.y + destRect.h);
|
||||
glTexCoord2f(1.0f, 1.0f); glVertex2i(destRect.x + destRect.w, destRect.y + destRect.h);
|
||||
glEnd();
|
||||
glDisable(GL_TEXTURE_2D);
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
|
||||
if (++this->texIndex == VBO_BUFFERS)
|
||||
this->texIndex = 0;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
const LG_Renderer LGR_OpenGL =
|
||||
{
|
||||
.get_name = lgr_opengl_get_name,
|
||||
.initialize = lgr_opengl_initialize,
|
||||
.deinitialize = lgr_opengl_deinitialize,
|
||||
.is_compatible = lgr_opengl_is_compatible,
|
||||
.render = lgr_opengl_render
|
||||
};
|
Reference in New Issue
Block a user