improved engine init and plugin registration

This commit is contained in:
aap
2017-08-26 20:08:23 +02:00
parent 45b48b8f4e
commit ea48c140c1
30 changed files with 246 additions and 124 deletions

View File

@@ -22,7 +22,7 @@ namespace gl3 {
// TODO: make some of these things platform-independent
void*
static void*
driverOpen(void *o, int32, int32)
{
#ifdef RW_OPENGL
@@ -38,28 +38,18 @@ driverOpen(void *o, int32, int32)
return o;
}
void*
static void*
driverClose(void *o, int32, int32)
{
return o;
}
void
initializePlatform(void)
registerPlatformPlugins(void)
{
Driver::registerPlugin(PLATFORM_GL3, 0, PLATFORM_GL3,
driverOpen, driverClose);
registerNativeRaster();
#ifdef RW_OPENGL
// uniforms need to be registered before any shaders are created
registerBlock("Scene");
registerBlock("Object");
registerBlock("State");
registerUniform("u_matColor");
registerUniform("u_surfaceProps");
#endif
}
}

View File

@@ -60,12 +60,16 @@ struct UniformObject
UniformLight lights[MAX_LIGHTS];
};
GLuint vao;
GLuint ubo_state, ubo_scene, ubo_object;
GLuint whitetex;
UniformState uniformState;
UniformScene uniformScene;
UniformObject uniformObject;
static GLuint vao;
static GLuint ubo_state, ubo_scene, ubo_object;
static GLuint whitetex;
static UniformState uniformState;
static UniformScene uniformScene;
static UniformObject uniformObject;
int32 u_matColor;
int32 u_surfaceProps;
Shader *simpleShader;
@@ -501,8 +505,11 @@ stopGLFW(void)
static int
initOpenGL(void)
{
#include "shaders/simple_gl3.inc"
simpleShader = Shader::fromStrings(simple_vert_src, simple_frag_src);
registerBlock("Scene");
registerBlock("Object");
registerBlock("State");
u_matColor = registerUniform("u_matColor");
u_surfaceProps = registerUniform("u_surfaceProps");
glClearColor(0.25, 0.25, 0.25, 1.0);
@@ -511,6 +518,25 @@ initOpenGL(void)
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
byte whitepixel[4] = {0xFF, 0xFF, 0xFF, 0xFF};
glGenTextures(1, &whitetex);
glBindTexture(GL_TEXTURE_2D, whitetex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1,
0, GL_RGBA, GL_UNSIGNED_BYTE, &whitepixel);
im2DInit();
return 1;
}
static int
finalizeOpenGL(void)
{
#include "shaders/simple_gl3.inc"
simpleShader = Shader::fromStrings(simple_vert_src, simple_frag_src);
glGenBuffers(1, &ubo_state);
glBindBuffer(GL_UNIFORM_BUFFER, ubo_state);
glBindBufferBase(GL_UNIFORM_BUFFER, gl3::findBlock("State"), ubo_state);
@@ -532,15 +558,6 @@ initOpenGL(void)
GL_DYNAMIC_DRAW);
glBindBuffer(GL_UNIFORM_BUFFER, 0);
byte whitepixel[4] = {0xFF, 0xFF, 0xFF, 0xFF};
glGenTextures(1, &whitetex);
glBindTexture(GL_TEXTURE_2D, whitetex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1,
0, GL_RGBA, GL_UNSIGNED_BYTE, &whitepixel);
im2DInit();
return 1;
}
@@ -552,6 +569,8 @@ deviceSystem(DeviceReq req, void *arg0)
return startGLFW((EngineStartParams*)arg0);
case DEVICEINIT:
return initOpenGL();
case DEVICEFINALIZE:
return finalizeOpenGL();
case DEVICESTOP:
return stopGLFW();
}

View File

@@ -25,13 +25,19 @@ namespace gl3 {
#ifdef RW_OPENGL
#define U(i) currentShader->uniformLocations[i]
// MatFX
Shader *envShader;
static Shader *envShader;
static int32 u_texMatrix;
static int32 u_coefficient;
static void*
matfxOpen(void *o, int32, int32)
{
u_texMatrix = registerUniform("u_texMatrix");
u_coefficient = registerUniform("u_coefficient");
#include "shaders/matfx_gl3.inc"
matFXGlobals.pipelines[PLATFORM_GL3] = makeMatFXPipeline();
envShader = Shader::fromStrings(matfx_env_vert_src, matfx_env_frag_src);
@@ -49,11 +55,8 @@ initMatFX(void)
{
Driver::registerPlugin(PLATFORM_GL3, 0, ID_MATFX,
matfxOpen, matfxClose);
registerUniform("u_texMatrix");
registerUniform("u_coefficient");
}
#define U(s) currentShader->uniformLocations[findUniform(s)]
void
matfxDefaultRender(InstanceDataHeader *header, InstanceData *inst)
@@ -66,13 +69,13 @@ matfxDefaultRender(InstanceDataHeader *header, InstanceData *inst)
simpleShader->use();
convColor(&col, &m->color);
glUniform4fv(U("u_matColor"), 1, (GLfloat*)&col);
glUniform4fv(U(u_matColor), 1, (GLfloat*)&col);
surfProps[0] = m->surfaceProps.ambient;
surfProps[1] = m->surfaceProps.specular;
surfProps[2] = m->surfaceProps.diffuse;
surfProps[3] = 0.0f;
glUniform4fv(U("u_surfaceProps"), 1, surfProps);
glUniform4fv(U(u_surfaceProps), 1, surfProps);
setTexture(0, m->texture);
@@ -160,18 +163,18 @@ matfxEnvRender(InstanceDataHeader *header, InstanceData *inst)
envShader->use();
convColor(&col, &m->color);
glUniform4fv(U("u_matColor"), 1, (GLfloat*)&col);
glUniform4fv(U(u_matColor), 1, (GLfloat*)&col);
surfProps[0] = m->surfaceProps.ambient;
surfProps[1] = m->surfaceProps.specular;
surfProps[2] = m->surfaceProps.diffuse;
surfProps[3] = 0.0f;
glUniform4fv(U("u_surfaceProps"), 1, surfProps);
glUniform4fv(U(u_surfaceProps), 1, surfProps);
glUniform1fv(U("u_coefficient"), 1, &env->coefficient);
glUniform1fv(U(u_coefficient), 1, &env->coefficient);
calcEnvTexMatrix(env->frame, texMat);
glUniformMatrix4fv(U("u_texMatrix"), 1, GL_FALSE, texMat);
glUniformMatrix4fv(U(u_texMatrix), 1, GL_FALSE, texMat);
setTexture(0, env->tex);
@@ -232,11 +235,13 @@ makeMatFXPipeline(void)
// Skin
Shader *skinShader;
static Shader *skinShader;
static int32 u_boneMatrices;
static void*
skinOpen(void *o, int32, int32)
{
u_boneMatrices = registerUniform("u_boneMatrices");
#include "shaders/simple_gl3.inc"
#include "shaders/skin_gl3.inc"
skinGlobals.pipelines[PLATFORM_GL3] = makeSkinPipeline();
@@ -255,7 +260,6 @@ initSkin(void)
{
Driver::registerPlugin(PLATFORM_GL3, 0, ID_SKIN,
skinOpen, skinClose);
registerUniform("u_boneMatrices");
}
enum
@@ -416,8 +420,6 @@ skinUninstanceCB(Geometry *geo, InstanceDataHeader *header)
assert(0 && "can't uninstance");
}
#define U(s) currentShader->uniformLocations[findUniform(s)]
static float skinMatrices[64*16];
void
@@ -464,20 +466,20 @@ skinRenderCB(Atomic *atomic, InstanceDataHeader *header)
skinShader->use();
updateSkinMatrices(atomic);
glUniformMatrix4fv(U("u_boneMatrices"), 64, GL_FALSE,
glUniformMatrix4fv(U(u_boneMatrices), 64, GL_FALSE,
(GLfloat*)skinMatrices);
while(n--){
m = inst->material;
convColor(&col, &m->color);
glUniform4fv(U("u_matColor"), 1, (GLfloat*)&col);
glUniform4fv(U(u_matColor), 1, (GLfloat*)&col);
surfProps[0] = m->surfaceProps.ambient;
surfProps[1] = m->surfaceProps.specular;
surfProps[2] = m->surfaceProps.diffuse;
surfProps[3] = 0.0f;
glUniform4fv(U("u_surfaceProps"), 1, surfProps);
glUniform4fv(U(u_surfaceProps), 1, surfProps);
setTexture(0, m->texture);

View File

@@ -64,7 +64,7 @@ lightingCB(void)
setAmbientLight(&ambLight);
}
#define U(s) currentShader->uniformLocations[findUniform(s)]
#define U(i) currentShader->uniformLocations[i]
void
defaultRenderCB(Atomic *atomic, InstanceDataHeader *header)
@@ -90,13 +90,13 @@ defaultRenderCB(Atomic *atomic, InstanceDataHeader *header)
m = inst->material;
convColor(&col, &m->color);
glUniform4fv(U("u_matColor"), 1, (GLfloat*)&col);
glUniform4fv(U(u_matColor), 1, (GLfloat*)&col);
surfProps[0] = m->surfaceProps.ambient;
surfProps[1] = m->surfaceProps.specular;
surfProps[2] = m->surfaceProps.diffuse;
surfProps[3] = 0.0f;
glUniform4fv(U("u_surfaceProps"), 1, surfProps);
glUniform4fv(U(u_surfaceProps), 1, surfProps);
setTexture(0, m->texture);

View File

@@ -18,17 +18,20 @@ namespace gl3 {
UniformRegistry uniformRegistry;
int
int32
registerUniform(const char *name)
{
int i;
i = findUniform(name);
if(i >= 0) return i;
// TODO: print error
if(uniformRegistry.numUniforms+1 >= MAX_UNIFORMS)
return -1;
uniformRegistry.uniformNames[uniformRegistry.numUniforms] = strdup(name);
return uniformRegistry.numUniforms++;
}
int
int32
findUniform(const char *name)
{
int i;
@@ -38,17 +41,20 @@ findUniform(const char *name)
return -1;
}
int
int32
registerBlock(const char *name)
{
int i;
i = findBlock(name);
if(i >= 0) return i;
// TODO: print error
if(uniformRegistry.numBlocks+1 >= MAX_BLOCKS)
return -1;
uniformRegistry.blockNames[uniformRegistry.numBlocks] = strdup(name);
return uniformRegistry.numBlocks++;
}
int
int32
findBlock(const char *name)
{
int i;

View File

@@ -16,7 +16,7 @@ struct EngineStartParams
namespace gl3 {
void initializePlatform(void);
void registerPlatformPlugins(void);
extern Device renderdevice;
@@ -46,6 +46,10 @@ enum AttribIndices
ATTRIB_TEXCOORDS7,
};
// default uniform indices
extern int32 u_matColor;
extern int32 u_surfaceProps;
struct InstanceData
{
uint32 numIndex;

View File

@@ -11,17 +11,17 @@ enum {
struct UniformRegistry
{
int numUniforms;
int32 numUniforms;
char *uniformNames[MAX_UNIFORMS];
int numBlocks;
int32 numBlocks;
char *blockNames[MAX_BLOCKS];
};
int registerUniform(const char *name);
int findUniform(const char *name);
int registerBlock(const char *name);
int findBlock(const char *name);
int32 registerUniform(const char *name);
int32 findUniform(const char *name);
int32 registerBlock(const char *name);
int32 findBlock(const char *name);
extern UniformRegistry uniformRegistry;

View File

@@ -4,7 +4,7 @@ namespace wdgl {
// NOTE: This is not really RW OpenGL! It's specific to WarDrum's GTA ports
void initializePlatform(void);
void registerPlatformPlugins(void);
struct AttribDesc
{

View File

@@ -22,21 +22,21 @@
namespace rw {
namespace wdgl {
void*
static void*
driverOpen(void *o, int32, int32)
{
engine->driver[PLATFORM_WDGL]->defaultPipeline = makeDefaultPipeline();
return o;
}
void*
static void*
driverClose(void *o, int32, int32)
{
return o;
}
void
initializePlatform(void)
registerPlatformPlugins(void)
{
Driver::registerPlugin(PLATFORM_WDGL, 0, PLATFORM_WDGL,
driverOpen, driverClose);