From 6d11126db69a63288f61bcb0911feaa69fb93066 Mon Sep 17 00:00:00 2001 From: Shinovon Date: Fri, 1 May 2026 12:36:04 +0500 Subject: [PATCH] Make gles1 stubs based on gl3 code --- group/librw.mmh | 2 +- vendor/librw/src/engine.cpp | 7 +- vendor/librw/src/gles1/gl1.cpp | 54 + vendor/librw/src/gles1/gl1device.cpp | 1610 ++++++++++++++++++++++++++ vendor/librw/src/gles1/gl1immed.cpp | 180 +++ vendor/librw/src/gles1/gl1pipe.cpp | 317 +++++ vendor/librw/src/gles1/gl1raster.cpp | 961 +++++++++++++++ vendor/librw/src/gles1/gl1render.cpp | 143 +++ vendor/librw/src/gles1/rwgles1.cpp | 58 - vendor/librw/src/gles1/rwgles1.h | 321 +++-- vendor/librw/src/gles1/rwgles1impl.h | 83 ++ vendor/librw/src/rwbase.h | 1 + vendor/librw/src/texture.cpp | 13 +- 13 files changed, 3595 insertions(+), 155 deletions(-) create mode 100644 vendor/librw/src/gles1/gl1.cpp create mode 100644 vendor/librw/src/gles1/gl1device.cpp create mode 100644 vendor/librw/src/gles1/gl1immed.cpp create mode 100644 vendor/librw/src/gles1/gl1pipe.cpp create mode 100644 vendor/librw/src/gles1/gl1raster.cpp create mode 100644 vendor/librw/src/gles1/gl1render.cpp delete mode 100644 vendor/librw/src/gles1/rwgles1.cpp create mode 100644 vendor/librw/src/gles1/rwgles1impl.h diff --git a/group/librw.mmh b/group/librw.mmh index 9360ae3..1eba534 100644 --- a/group/librw.mmh +++ b/group/librw.mmh @@ -14,7 +14,7 @@ SOURCE engine.cpp error.cpp frame.cpp geometry.cpp geoplg.cpp SOURCEPATH ../vendor/librw/src/gl SOURCE gl3.cpp gl3device.cpp gl3immed.cpp gl3matfx.cpp gl3pipe.cpp gl3raster.cpp gl3render.cpp gl3shader.cpp gl3skin.cpp wdgl.cpp SOURCEPATH ../vendor/librw/src/gles1 -SOURCE rwgles1.cpp +SOURCE gl1.cpp gl1device.cpp gl1immed.cpp gl1pipe.cpp gl1raster.cpp gl1render.cpp SOURCEPATH ../vendor/librw/src SOURCE hanim.cpp image.cpp light.cpp SOURCEPATH ../vendor/librw/src/lodepng diff --git a/vendor/librw/src/engine.cpp b/vendor/librw/src/engine.cpp index f21f011..d22dc1b 100644 --- a/vendor/librw/src/engine.cpp +++ b/vendor/librw/src/engine.cpp @@ -244,6 +244,9 @@ Engine::init(MemoryFunctions *memfuncs) d3d9::registerPlatformPlugins(); wdgl::registerPlatformPlugins(); gl3::registerPlatformPlugins(); +#ifdef RW_GLES1 + gles1::registerPlatformPlugins(); +#endif Engine::state = Initialized; return 1; @@ -269,8 +272,8 @@ Engine::open(EngineOpenParams *p) engine->device = ps2::renderdevice; #elif RW_GL3 engine->device = gl3::renderdevice; -//#elif RW_GLES1 // TODO -// engine->device = gles1::renderdevice; +#elif RW_GLES1 + engine->device = gles1::renderdevice; #elif RW_D3D9 engine->device = d3d::renderdevice; #else diff --git a/vendor/librw/src/gles1/gl1.cpp b/vendor/librw/src/gles1/gl1.cpp new file mode 100644 index 0000000..b185063 --- /dev/null +++ b/vendor/librw/src/gles1/gl1.cpp @@ -0,0 +1,54 @@ +#include +#include +#include +#include + +#include "../rwbase.h" +#include "../rwerror.h" +#include "../rwplg.h" +#include "../rwpipeline.h" +#include "../rwobjects.h" +#include "../rwengine.h" + +#include "rwgles1.h" +#include "rwgles1impl.h" + +#ifdef RW_GLES1 + +namespace rw { +namespace gles1 { + +static void* +driverOpen(void* object, int32 offset, int32 size) +{ + engine->driver[PLATFORM_GLES1]->defaultPipeline = makeDefaultPipeline(); + engine->driver[PLATFORM_GLES1]->rasterNativeOffset = nativeRasterOffset; + engine->driver[PLATFORM_GLES1]->rasterCreate = rasterCreate; + engine->driver[PLATFORM_GLES1]->rasterLock = rasterLock; + engine->driver[PLATFORM_GLES1]->rasterUnlock = rasterUnlock; + engine->driver[PLATFORM_GLES1]->rasterNumLevels = rasterNumLevels; + engine->driver[PLATFORM_GLES1]->imageFindRasterFormat = imageFindRasterFormat; + engine->driver[PLATFORM_GLES1]->rasterFromImage = rasterFromImage; + engine->driver[PLATFORM_GLES1]->rasterToImage = rasterToImage; + return object; +} + +static void* +driverClose(void* object, int32 offset, int32 size) +{ + // stub: no hace nada + return object; +} + +void +registerPlatformPlugins(void) +{ + Driver::registerPlugin(PLATFORM_GLES1, 0, PLATFORM_GLES1, + driverOpen, driverClose); + registerNativeRaster(); +} + +} +} + +#endif diff --git a/vendor/librw/src/gles1/gl1device.cpp b/vendor/librw/src/gles1/gl1device.cpp new file mode 100644 index 0000000..fd9190c --- /dev/null +++ b/vendor/librw/src/gles1/gl1device.cpp @@ -0,0 +1,1610 @@ +#include +#include +#include +#include + +#include "../rwbase.h" +#include "../rwerror.h" +#include "../rwplg.h" +#include "../rwrender.h" +#include "../rwengine.h" +#include "../rwpipeline.h" +#include "../rwobjects.h" +#ifdef RW_GLES1 + +#include "rwgles1.h" +#include "rwgles1impl.h" + +#define PLUGIN_ID 0 + +namespace rw { +namespace gles1 { + +GlGlobals glGlobals; + +// terrible hack for GLES +bool32 needToReadBackTextures; + +int32 alphaFunc; +float32 alphaRef; + +struct UniformState +{ + float32 alphaRefLow; + float32 alphaRefHigh; + int32 pad[2]; + + float32 fogStart; + float32 fogEnd; + float32 fogRange; + float32 fogDisable; + + RGBAf fogColor; +}; + +struct UniformScene +{ + float32 proj[16]; + float32 view[16]; +}; + +#define MAX_LIGHTS 4 + +struct UniformObject +{ + RawMatrix world; + RGBAf ambLight; + struct { + float type; + float radius; + float minusCosAngle; + float hardSpot; + } lightParams[MAX_LIGHTS]; + V4d lightPosition[MAX_LIGHTS]; + V4d lightDirection[MAX_LIGHTS]; + RGBAf lightColor[MAX_LIGHTS]; +}; + + + +static GLuint vao; +static GLuint whitetex; +static UniformState uniformState; +static UniformScene uniformScene; +static UniformObject uniformObject; + +#ifndef RW_GL_USE_UBOS +// State +int32 u_alphaRef; +int32 u_fogData; +int32 u_fogColor; + +// Scene +int32 u_proj; +int32 u_view; + +// Object +int32 u_world; +int32 u_ambLight; +int32 u_lightParams; +int32 u_lightPosition; +int32 u_lightDirection; +int32 u_lightColor; +#endif + +int32 u_matColor; +int32 u_surfProps; + +static bool32 stateDirty = 1; +static bool32 sceneDirty = 1; +static bool32 objectDirty = 1; + +struct RwRasterStateCache { + Raster *raster; + Texture::Addressing addressingU; + Texture::Addressing addressingV; + Texture::FilterMode filter; +}; + +#define MAXNUMSTAGES 8 + +// cached RW render states +struct RwStateCache { + bool32 vertexAlpha; + uint32 alphaTestEnable; + uint32 alphaFunc; + bool32 textureAlpha; + bool32 blendEnable; + uint32 srcblend, destblend; + uint32 zwrite; + uint32 ztest; + uint32 cullmode; + uint32 stencilenable; + uint32 stencilpass; + uint32 stencilfail; + uint32 stencilzfail; + uint32 stencilfunc; + uint32 stencilref; + uint32 stencilmask; + uint32 stencilwritemask; + uint32 fogEnable; + float32 fogStart; + float32 fogEnd; + + // emulation of PS2 GS + bool32 gsalpha; + uint32 gsalpharef; + + RwRasterStateCache texstage[MAXNUMSTAGES]; +}; +static RwStateCache rwStateCache; + +enum +{ + // actual gl states + RWGL_BLEND, + RWGL_SRCBLEND, + RWGL_DESTBLEND, + RWGL_DEPTHTEST, + RWGL_DEPTHFUNC, + RWGL_DEPTHMASK, + RWGL_CULL, + RWGL_CULLFACE, + RWGL_STENCIL, + RWGL_STENCILFUNC, + RWGL_STENCILFAIL, + RWGL_STENCILZFAIL, + RWGL_STENCILPASS, + RWGL_STENCILREF, + RWGL_STENCILMASK, + RWGL_STENCILWRITEMASK, + + // uniforms + RWGL_ALPHAFUNC, + RWGL_ALPHAREF, + RWGL_FOG, + RWGL_FOGSTART, + RWGL_FOGEND, + RWGL_FOGCOLOR, + + RWGL_NUM_STATES +}; +static bool uniformStateDirty[RWGL_NUM_STATES]; + +struct GlState { + bool32 blendEnable; + uint32 srcblend, destblend; + + bool32 depthTest; + uint32 depthFunc; + + uint32 depthMask; + + bool32 cullEnable; + uint32 cullFace; + + bool32 stencilEnable; + // glStencilFunc + uint32 stencilFunc; + uint32 stencilRef; + uint32 stencilMask; + // glStencilOp + uint32 stencilPass; + uint32 stencilFail; + uint32 stencilZFail; + // glStencilMask + uint32 stencilWriteMask; +}; +static GlState curGlState, oldGlState; + +static int32 activeTexture; +static uint32 boundTexture[MAXNUMSTAGES]; + +static uint32 currentFramebuffer; + +static uint32 blendMap[] = { + GL_ZERO, // actually invalid + GL_ZERO, + GL_ONE, + GL_SRC_COLOR, + GL_ONE_MINUS_SRC_COLOR, + GL_SRC_ALPHA, + GL_ONE_MINUS_SRC_ALPHA, + GL_DST_ALPHA, + GL_ONE_MINUS_DST_ALPHA, + GL_DST_COLOR, + GL_ONE_MINUS_DST_COLOR, + GL_SRC_ALPHA_SATURATE, +}; + +static uint32 stencilOpMap[] = { + GL_KEEP, // actually invalid + GL_KEEP, + GL_ZERO, + GL_REPLACE, + GL_INCR, + GL_DECR, + GL_INVERT, + GL_INCR,//_WRAP, + GL_DECR//_WRAP +}; + +static uint32 stencilFuncMap[] = { + GL_NEVER, // actually invalid + GL_NEVER, + GL_LESS, + GL_EQUAL, + GL_LEQUAL, + GL_GREATER, + GL_NOTEQUAL, + GL_GEQUAL, + GL_ALWAYS +}; + +static float maxAnisotropy; + +/* + * GL state cache + */ + +void +setGlRenderState(uint32 state, uint32 value) +{ + switch(state){ + case RWGL_BLEND: curGlState.blendEnable = value; break; + case RWGL_SRCBLEND: curGlState.srcblend = value; break; + case RWGL_DESTBLEND: curGlState.destblend = value; break; + case RWGL_DEPTHTEST: curGlState.depthTest = value; break; + case RWGL_DEPTHFUNC: curGlState.depthFunc = value; break; + case RWGL_DEPTHMASK: curGlState.depthMask = value; break; + case RWGL_CULL: curGlState.cullEnable = value; break; + case RWGL_CULLFACE: curGlState.cullFace = value; break; + case RWGL_STENCIL: curGlState.stencilEnable = value; break; + case RWGL_STENCILFUNC: curGlState.stencilFunc = value; break; + case RWGL_STENCILFAIL: curGlState.stencilFail = value; break; + case RWGL_STENCILZFAIL: curGlState.stencilZFail = value; break; + case RWGL_STENCILPASS: curGlState.stencilPass = value; break; + case RWGL_STENCILREF: curGlState.stencilRef = value; break; + case RWGL_STENCILMASK: curGlState.stencilMask = value; break; + case RWGL_STENCILWRITEMASK: curGlState.stencilWriteMask = value; break; + } +} + +void +flushGlRenderState(void) +{ + if(oldGlState.blendEnable != curGlState.blendEnable){ + oldGlState.blendEnable = curGlState.blendEnable; + (oldGlState.blendEnable ? glEnable : glDisable)(GL_BLEND); + } + + if(oldGlState.srcblend != curGlState.srcblend || + oldGlState.destblend != curGlState.destblend){ + oldGlState.srcblend = curGlState.srcblend; + oldGlState.destblend = curGlState.destblend; + glBlendFunc(oldGlState.srcblend, oldGlState.destblend); + } + + if(oldGlState.depthTest != curGlState.depthTest){ + oldGlState.depthTest = curGlState.depthTest; + (oldGlState.depthTest ? glEnable : glDisable)(GL_DEPTH_TEST); + } + if(oldGlState.depthFunc != curGlState.depthFunc){ + oldGlState.depthFunc = curGlState.depthFunc; + glDepthFunc(oldGlState.depthFunc); + } + if(oldGlState.depthMask != curGlState.depthMask){ + oldGlState.depthMask = curGlState.depthMask; + glDepthMask(oldGlState.depthMask); + } + + if(oldGlState.stencilEnable != curGlState.stencilEnable){ + oldGlState.stencilEnable = curGlState.stencilEnable; + (oldGlState.stencilEnable ? glEnable : glDisable)(GL_STENCIL_TEST); + } + if(oldGlState.stencilFunc != curGlState.stencilFunc || + oldGlState.stencilRef != curGlState.stencilRef || + oldGlState.stencilMask != curGlState.stencilMask){ + oldGlState.stencilFunc = curGlState.stencilFunc; + oldGlState.stencilRef = curGlState.stencilRef; + oldGlState.stencilMask = curGlState.stencilMask; + glStencilFunc(oldGlState.stencilFunc, oldGlState.stencilRef, oldGlState.stencilMask); + } + if(oldGlState.stencilPass != curGlState.stencilPass || + oldGlState.stencilFail != curGlState.stencilFail || + oldGlState.stencilZFail != curGlState.stencilZFail){ + oldGlState.stencilPass = curGlState.stencilPass; + oldGlState.stencilFail = curGlState.stencilFail; + oldGlState.stencilZFail = curGlState.stencilZFail; + glStencilOp(oldGlState.stencilFail, oldGlState.stencilZFail, oldGlState.stencilPass); + } + if(oldGlState.stencilWriteMask != curGlState.stencilWriteMask){ + oldGlState.stencilWriteMask = curGlState.stencilWriteMask; + glStencilMask(oldGlState.stencilWriteMask); + } + + if(oldGlState.cullEnable != curGlState.cullEnable){ + oldGlState.cullEnable = curGlState.cullEnable; + (oldGlState.cullEnable ? glEnable : glDisable)(GL_CULL_FACE); + } + if(oldGlState.cullFace != curGlState.cullFace){ + oldGlState.cullFace = curGlState.cullFace; + glCullFace(oldGlState.cullFace); + } +} + + + +void +setAlphaBlend(bool32 enable) +{ + if(rwStateCache.blendEnable != enable){ + rwStateCache.blendEnable = enable; + setGlRenderState(RWGL_BLEND, enable); + } +} + +bool32 +getAlphaBlend(void) +{ + return rwStateCache.blendEnable; +} + +bool32 getAlphaTest(void) { return rwStateCache.alphaTestEnable; } + +static void +setDepthTest(bool32 enable) +{ + if(rwStateCache.ztest != enable){ + rwStateCache.ztest = enable; + if(rwStateCache.zwrite && !enable){ + // If we still want to write, enable but set mode to always + setGlRenderState(RWGL_DEPTHTEST, true); + setGlRenderState(RWGL_DEPTHFUNC, GL_ALWAYS); + }else{ + setGlRenderState(RWGL_DEPTHTEST, rwStateCache.ztest); + setGlRenderState(RWGL_DEPTHFUNC, GL_LEQUAL); + } + } +} + +static void +setDepthWrite(bool32 enable) +{ + enable = enable ? GL_TRUE : GL_FALSE; + if(rwStateCache.zwrite != enable){ + rwStateCache.zwrite = enable; + if(enable && !rwStateCache.ztest){ + // Have to switch on ztest so writing can work + setGlRenderState(RWGL_DEPTHTEST, true); + setGlRenderState(RWGL_DEPTHFUNC, GL_ALWAYS); + } + setGlRenderState(RWGL_DEPTHMASK, rwStateCache.zwrite); + } +} + +static void +setAlphaTest(bool32 enable) +{ + uint32 shaderfunc; + if(rwStateCache.alphaTestEnable != enable){ + rwStateCache.alphaTestEnable = enable; + shaderfunc = rwStateCache.alphaTestEnable ? rwStateCache.alphaFunc : ALPHAALWAYS; + if(alphaFunc != shaderfunc){ + alphaFunc = shaderfunc; + uniformStateDirty[RWGL_ALPHAFUNC] = true; + stateDirty = 1; + } + } +} + +static void +setAlphaTestFunction(uint32 function) +{ + uint32 shaderfunc; + if(rwStateCache.alphaFunc != function){ + rwStateCache.alphaFunc = function; + shaderfunc = rwStateCache.alphaTestEnable ? rwStateCache.alphaFunc : ALPHAALWAYS; + if(alphaFunc != shaderfunc){ + alphaFunc = shaderfunc; + uniformStateDirty[RWGL_ALPHAFUNC] = true; + stateDirty = 1; + } + } +} + +static void +setVertexAlpha(bool32 enable) +{ + if(rwStateCache.vertexAlpha != enable){ + if(!rwStateCache.textureAlpha){ + setAlphaBlend(enable); + setAlphaTest(enable); + } + rwStateCache.vertexAlpha = enable; + } +} + +static void +setActiveTexture(int32 n) +{ + if(activeTexture != n){ + activeTexture = n; + glActiveTexture(GL_TEXTURE0+n); + } +} + +uint32 +bindTexture(uint32 texid) +{ + uint32 prev = boundTexture[activeTexture]; + if(prev != texid){ + boundTexture[activeTexture] = texid; + glBindTexture(GL_TEXTURE_2D, texid); + } + return prev; +} + +void +bindFramebuffer(uint32 fbo) +{ + if(currentFramebuffer != fbo){ +// glBindFramebuffer(GL_FRAMEBUFFER, fbo); + currentFramebuffer = fbo; + } +} + +static GLint filterConvMap_NoMIP[] = { + 0, GL_NEAREST, GL_LINEAR, + GL_NEAREST, GL_LINEAR, + GL_NEAREST, GL_LINEAR +}; +static GLint filterConvMap_MIP[] = { + 0, GL_NEAREST, GL_LINEAR, + GL_NEAREST_MIPMAP_NEAREST, GL_LINEAR_MIPMAP_NEAREST, + GL_NEAREST_MIPMAP_LINEAR, GL_LINEAR_MIPMAP_LINEAR +}; + +static GLint addressConvMap[] = { + 0, GL_REPEAT, GL_REPEAT, //GL_MIRRORED_REPEAT, +#ifdef __SYMBIAN32__ + // TODO + GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE +#else + GL_CLAMP_TO_EDGE, GL_CLAMP_TO_BORDER +#endif +}; + +static void +setFilterMode(uint32 stage, int32 filter, int32 maxAniso = 1) +{ + if(rwStateCache.texstage[stage].filter != (Texture::FilterMode)filter){ + rwStateCache.texstage[stage].filter = (Texture::FilterMode)filter; + Raster *raster = rwStateCache.texstage[stage].raster; + if(raster){ + Gl1Raster *natras = PLUGINOFFSET(Gl1Raster, rwStateCache.texstage[stage].raster, nativeRasterOffset); + if(natras->filterMode != filter){ + setActiveTexture(stage); +// if(natras->autogenMipmap || natras->numLevels > 1){ +// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filterConvMap_MIP[filter]); +// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filterConvMap_NoMIP[filter]); +// }else + { + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filterConvMap_NoMIP[filter]); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filterConvMap_NoMIP[filter]); + } + natras->filterMode = filter; + } + if(natras->maxAnisotropy != maxAniso){ + setActiveTexture(stage); + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, (float)maxAniso); + natras->maxAnisotropy = maxAniso; + } + } + } +} + +static void +setAddressU(uint32 stage, int32 addressing) +{ + if(rwStateCache.texstage[stage].addressingU != (Texture::Addressing)addressing){ + rwStateCache.texstage[stage].addressingU = (Texture::Addressing)addressing; + Raster *raster = rwStateCache.texstage[stage].raster; + if(raster){ + Gl1Raster *natras = PLUGINOFFSET(Gl1Raster, raster, nativeRasterOffset); + if(natras->addressU == addressing){ + setActiveTexture(stage); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, addressConvMap[addressing]); + natras->addressU = addressing; + } + } + } +} + +static void +setAddressV(uint32 stage, int32 addressing) +{ + if(rwStateCache.texstage[stage].addressingV != (Texture::Addressing)addressing){ + rwStateCache.texstage[stage].addressingV = (Texture::Addressing)addressing; + Raster *raster = rwStateCache.texstage[stage].raster; + if(raster){ + Gl1Raster *natras = PLUGINOFFSET(Gl1Raster, rwStateCache.texstage[stage].raster, nativeRasterOffset); + if(natras->addressV == addressing){ + setActiveTexture(stage); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, addressConvMap[addressing]); + natras->addressV = addressing; + } + } + } +} + +static void +setRasterStageOnly(uint32 stage, Raster *raster) +{ + bool32 alpha; + if(raster != rwStateCache.texstage[stage].raster){ + rwStateCache.texstage[stage].raster = raster; + setActiveTexture(stage); + if(raster){ + assert(raster->platform == PLATFORM_GLES1); + Gl1Raster *natras = PLUGINOFFSET(Gl1Raster, raster, nativeRasterOffset); + bindTexture(natras->texid); + + rwStateCache.texstage[stage].filter = (rw::Texture::FilterMode)natras->filterMode; + rwStateCache.texstage[stage].addressingU = (rw::Texture::Addressing)natras->addressU; + rwStateCache.texstage[stage].addressingV = (rw::Texture::Addressing)natras->addressV; + + alpha = natras->hasAlpha; + }else{ + bindTexture(whitetex); + alpha = 0; + } + + if(stage == 0){ + if(alpha != rwStateCache.textureAlpha){ + rwStateCache.textureAlpha = alpha; + if(!rwStateCache.vertexAlpha){ + setAlphaBlend(alpha); + setAlphaTest(alpha); + } + } + } + } +} + +static void +setRasterStage(uint32 stage, Raster *raster) +{ + bool32 alpha; + if(raster != rwStateCache.texstage[stage].raster){ + rwStateCache.texstage[stage].raster = raster; + setActiveTexture(stage); + if(raster){ + assert(raster->platform == PLATFORM_GLES1); + Gl1Raster *natras = PLUGINOFFSET(Gl1Raster, raster, nativeRasterOffset); + bindTexture(natras->texid); + uint32 filter = rwStateCache.texstage[stage].filter; + uint32 addrU = rwStateCache.texstage[stage].addressingU; + uint32 addrV = rwStateCache.texstage[stage].addressingV; + if(natras->filterMode != filter){ +// if(natras->autogenMipmap || natras->numLevels > 1){ +// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filterConvMap_MIP[filter]); +// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filterConvMap_NoMIP[filter]); +// }else + { + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filterConvMap_NoMIP[filter]); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filterConvMap_NoMIP[filter]); + } + natras->filterMode = filter; + } + if(natras->addressU != addrU){ + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, addressConvMap[addrU]); + natras->addressU = addrU; + } + if(natras->addressV != addrV){ + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, addressConvMap[addrV]); + natras->addressV = addrV; + } + alpha = natras->hasAlpha; + }else{ + bindTexture(whitetex); + alpha = 0; + } + + if(stage == 0){ + if(alpha != rwStateCache.textureAlpha){ + rwStateCache.textureAlpha = alpha; + if(!rwStateCache.vertexAlpha){ + setAlphaBlend(alpha); + setAlphaTest(alpha); + } + } + } + } +} + +void +evictRaster(Raster *raster) +{ + int i; + for(i = 0; i < MAXNUMSTAGES; i++){ + //assert(rwStateCache.texstage[i].raster != raster); + if(rwStateCache.texstage[i].raster != raster) + continue; + setRasterStage(i, nil); + } +} + +void +setTexture(int32 stage, Texture *tex) +{ + if(tex == nil || tex->raster == nil){ + setRasterStage(stage, nil); + return; + } + setRasterStageOnly(stage, tex->raster); + setFilterMode(stage, tex->getFilter(), tex->getMaxAnisotropy()); + setAddressU(stage, tex->getAddressU()); + setAddressV(stage, tex->getAddressV()); +} + +static void +setRenderState(int32 state, void *pvalue) +{ + uint32 value = (uint32)(uintptr)pvalue; + switch(state){ + case TEXTURERASTER: + setRasterStage(0, (Raster*)pvalue); + break; + case TEXTUREADDRESS: + setAddressU(0, value); + setAddressV(0, value); + break; + case TEXTUREADDRESSU: + setAddressU(0, value); + break; + case TEXTUREADDRESSV: + setAddressV(0, value); + break; + case TEXTUREFILTER: + setFilterMode(0, value); + break; + case VERTEXALPHA: + setVertexAlpha(value); + break; + case SRCBLEND: + if(rwStateCache.srcblend != value){ + rwStateCache.srcblend = value; + setGlRenderState(RWGL_SRCBLEND, blendMap[rwStateCache.srcblend]); + } + break; + case DESTBLEND: + if(rwStateCache.destblend != value){ + rwStateCache.destblend = value; + setGlRenderState(RWGL_DESTBLEND, blendMap[rwStateCache.destblend]); + } + break; + case ZTESTENABLE: + setDepthTest(value); + break; + case ZWRITEENABLE: + setDepthWrite(value); + break; + case FOGENABLE: + if(rwStateCache.fogEnable != value){ + rwStateCache.fogEnable = value; + uniformStateDirty[RWGL_FOG] = true; + stateDirty = 1; + } + break; + case FOGCOLOR: + // no cache check here...too lazy + RGBA c; + c.red = value; + c.green = value>>8; + c.blue = value>>16; + c.alpha = value>>24; + convColor(&uniformState.fogColor, &c); + uniformStateDirty[RWGL_FOGCOLOR] = true; + stateDirty = 1; + break; + case CULLMODE: + if(rwStateCache.cullmode != value){ + rwStateCache.cullmode = value; + if(rwStateCache.cullmode == CULLNONE) + setGlRenderState(RWGL_CULL, false); + else{ + setGlRenderState(RWGL_CULL, true); + setGlRenderState(RWGL_CULLFACE, rwStateCache.cullmode == CULLBACK ? GL_BACK : GL_FRONT); + } + } + break; + + case STENCILENABLE: + if(rwStateCache.stencilenable != value){ + rwStateCache.stencilenable = value; + setGlRenderState(RWGL_STENCIL, value); + } + break; + case STENCILFAIL: + if(rwStateCache.stencilfail != value){ + rwStateCache.stencilfail = value; + setGlRenderState(RWGL_STENCILFAIL, stencilOpMap[value]); + } + break; + case STENCILZFAIL: + if(rwStateCache.stencilzfail != value){ + rwStateCache.stencilzfail = value; + setGlRenderState(RWGL_STENCILZFAIL, stencilOpMap[value]); + } + break; + case STENCILPASS: + if(rwStateCache.stencilpass != value){ + rwStateCache.stencilpass = value; + setGlRenderState(RWGL_STENCILPASS, stencilOpMap[value]); + } + break; + case STENCILFUNCTION: + if(rwStateCache.stencilfunc != value){ + rwStateCache.stencilfunc = value; + setGlRenderState(RWGL_STENCILFUNC, stencilFuncMap[value]); + } + break; + case STENCILFUNCTIONREF: + if(rwStateCache.stencilref != value){ + rwStateCache.stencilref = value; + setGlRenderState(RWGL_STENCILREF, value); + } + break; + case STENCILFUNCTIONMASK: + if(rwStateCache.stencilmask != value){ + rwStateCache.stencilmask = value; + setGlRenderState(RWGL_STENCILMASK, value); + } + break; + case STENCILFUNCTIONWRITEMASK: + if(rwStateCache.stencilwritemask != value){ + rwStateCache.stencilwritemask = value; + setGlRenderState(RWGL_STENCILWRITEMASK, value); + } + break; + + case ALPHATESTFUNC: + setAlphaTestFunction(value); + break; + case ALPHATESTREF: + if(alphaRef != value/255.0f){ + alphaRef = value/255.0f; + uniformStateDirty[RWGL_ALPHAREF] = true; + stateDirty = 1; + } + break; + case GSALPHATEST: + rwStateCache.gsalpha = value; + break; + case GSALPHATESTREF: + rwStateCache.gsalpharef = value; + } +} + +static void* +getRenderState(int32 state) +{ + uint32 val; + RGBA rgba; + switch(state){ + case TEXTURERASTER: + return rwStateCache.texstage[0].raster; + case TEXTUREADDRESS: + if(rwStateCache.texstage[0].addressingU == rwStateCache.texstage[0].addressingV) + val = rwStateCache.texstage[0].addressingU; + else + val = 0; // invalid + break; + case TEXTUREADDRESSU: + val = rwStateCache.texstage[0].addressingU; + break; + case TEXTUREADDRESSV: + val = rwStateCache.texstage[0].addressingV; + break; + case TEXTUREFILTER: + val = rwStateCache.texstage[0].filter; + break; + + case VERTEXALPHA: + val = rwStateCache.vertexAlpha; + break; + case SRCBLEND: + val = rwStateCache.srcblend; + break; + case DESTBLEND: + val = rwStateCache.destblend; + break; + case ZTESTENABLE: + val = rwStateCache.ztest; + break; + case ZWRITEENABLE: + val = rwStateCache.zwrite; + break; + case FOGENABLE: + val = rwStateCache.fogEnable; + break; + case FOGCOLOR: + convColor(&rgba, &uniformState.fogColor); + val = RWRGBAINT(rgba.red, rgba.green, rgba.blue, rgba.alpha); + break; + case CULLMODE: + val = rwStateCache.cullmode; + break; + + case STENCILENABLE: + val = rwStateCache.stencilenable; + break; + case STENCILFAIL: + val = rwStateCache.stencilfail; + break; + case STENCILZFAIL: + val = rwStateCache.stencilzfail; + break; + case STENCILPASS: + val = rwStateCache.stencilpass; + break; + case STENCILFUNCTION: + val = rwStateCache.stencilfunc; + break; + case STENCILFUNCTIONREF: + val = rwStateCache.stencilref; + break; + case STENCILFUNCTIONMASK: + val = rwStateCache.stencilmask; + break; + case STENCILFUNCTIONWRITEMASK: + val = rwStateCache.stencilwritemask; + break; + + case ALPHATESTFUNC: + val = rwStateCache.alphaFunc; + break; + case ALPHATESTREF: + val = (uint32)(alphaRef*255.0f); + break; + case GSALPHATEST: + val = rwStateCache.gsalpha; + break; + case GSALPHATESTREF: + val = rwStateCache.gsalpharef; + break; + default: + val = 0; + } + return (void*)(uintptr)val; +} + +static void +resetRenderState(void) +{ + rwStateCache.alphaFunc = ALPHAGREATEREQUAL; + alphaFunc = 0; + alphaRef = 10.0f/255.0f; + uniformState.fogDisable = 1.0f; + uniformState.fogStart = 0.0f; + uniformState.fogEnd = 0.0f; + uniformState.fogRange = 0.0f; + uniformState.fogColor.red = 1.0f; + uniformState.fogColor.green = 1.0f; + uniformState.fogColor.blue = 1.0f; + uniformState.fogColor.alpha = 1.0f; + rwStateCache.gsalpha = 0; + rwStateCache.gsalpharef = 128; + stateDirty = 1; + + rwStateCache.vertexAlpha = 0; + rwStateCache.textureAlpha = 0; + rwStateCache.alphaTestEnable = 0; + + memset(&oldGlState, 0xFE, sizeof(oldGlState)); + + rwStateCache.blendEnable = 0; + setGlRenderState(RWGL_BLEND, false); + rwStateCache.srcblend = BLENDSRCALPHA; + rwStateCache.destblend = BLENDINVSRCALPHA; + setGlRenderState(RWGL_SRCBLEND, blendMap[rwStateCache.srcblend]); + setGlRenderState(RWGL_DESTBLEND, blendMap[rwStateCache.destblend]); + + rwStateCache.zwrite = GL_TRUE; + setGlRenderState(RWGL_DEPTHMASK, rwStateCache.zwrite); + + rwStateCache.ztest = 1; + setGlRenderState(RWGL_DEPTHTEST, true); + setGlRenderState(RWGL_DEPTHFUNC, GL_LEQUAL); + + rwStateCache.cullmode = CULLNONE; + setGlRenderState(RWGL_CULL, false); + setGlRenderState(RWGL_CULLFACE, GL_BACK); + + rwStateCache.stencilenable = 0; + setGlRenderState(RWGL_STENCIL, GL_FALSE); + rwStateCache.stencilfail = STENCILKEEP; + setGlRenderState(RWGL_STENCILFAIL, GL_KEEP); + rwStateCache.stencilzfail = STENCILKEEP; + setGlRenderState(RWGL_STENCILZFAIL, GL_KEEP); + rwStateCache.stencilpass = STENCILKEEP; + setGlRenderState(RWGL_STENCILPASS, GL_KEEP); + rwStateCache.stencilfunc = STENCILALWAYS; + setGlRenderState(RWGL_STENCILFUNC, GL_ALWAYS); + rwStateCache.stencilref = 0; + setGlRenderState(RWGL_STENCILREF, 0); + rwStateCache.stencilmask = 0xFFFFFFFF; + setGlRenderState(RWGL_STENCILMASK, 0xFFFFFFFF); + rwStateCache.stencilwritemask = 0xFFFFFFFF; + setGlRenderState(RWGL_STENCILWRITEMASK, 0xFFFFFFFF); + + activeTexture = -1; + for(int i = 0; i < MAXNUMSTAGES; i++){ + setActiveTexture(i); + bindTexture(whitetex); + } + setActiveTexture(0); +} + +void +setWorldMatrix(Matrix *mat) +{ + convMatrix(&uniformObject.world, mat); +// setUniform(u_world, &uniformObject.world); + objectDirty = 1; +} + +int32 +setLights(WorldLights *lightData) +{ + int i, n; + Light *l; + int32 bits; + + uniformObject.ambLight = lightData->ambient; + + bits = 0; + + if(lightData->numAmbients) + bits |= VSLIGHT_AMBIENT; + + n = 0; + for(i = 0; i < lightData->numDirectionals && i < 8; i++){ + l = lightData->directionals[i]; + uniformObject.lightParams[n].type = 1.0f; + uniformObject.lightColor[n] = l->color; + memcpy(&uniformObject.lightDirection[n], &l->getFrame()->getLTM()->at, sizeof(V3d)); + bits |= VSLIGHT_DIRECT; + n++; + if(n >= MAX_LIGHTS) + goto out; + } + + for(i = 0; i < lightData->numLocals; i++){ + Light *l = lightData->locals[i]; + + switch(l->getType()){ + case Light::POINT: + uniformObject.lightParams[n].type = 2.0f; + uniformObject.lightParams[n].radius = l->radius; + uniformObject.lightColor[n] = l->color; + memcpy(&uniformObject.lightPosition[n], &l->getFrame()->getLTM()->pos, sizeof(V3d)); + bits |= VSLIGHT_POINT; + n++; + if(n >= MAX_LIGHTS) + goto out; + break; + case Light::SPOT: + case Light::SOFTSPOT: + uniformObject.lightParams[n].type = 3.0f; + uniformObject.lightParams[n].minusCosAngle = l->minusCosAngle; + uniformObject.lightParams[n].radius = l->radius; + uniformObject.lightColor[n] = l->color; + memcpy(&uniformObject.lightPosition[n], &l->getFrame()->getLTM()->pos, sizeof(V3d)); + memcpy(&uniformObject.lightDirection[n], &l->getFrame()->getLTM()->at, sizeof(V3d)); + // lower bound of falloff + if(l->getType() == Light::SOFTSPOT) + uniformObject.lightParams[n].hardSpot = 0.0f; + else + uniformObject.lightParams[n].hardSpot = 1.0f; + bits |= VSLIGHT_SPOT; + n++; + if(n >= MAX_LIGHTS) + goto out; + break; + } + } + + uniformObject.lightParams[n].type = 0.0f; + +// setUniform(u_ambLight, &uniformObject.ambLight); +// setUniform(u_lightParams, uniformObject.lightParams); +// setUniform(u_lightPosition, uniformObject.lightPosition); +// setUniform(u_lightDirection, uniformObject.lightDirection); +// setUniform(u_lightColor, uniformObject.lightColor); +out: + objectDirty = 1; + return bits; +} + +void +setProjectionMatrix(float32 *mat) +{ + memcpy(&uniformScene.proj, mat, 64); +// setUniform(u_proj, uniformScene.proj); + sceneDirty = 1; +} + +void +setViewMatrix(float32 *mat) +{ + memcpy(&uniformScene.view, mat, 64); +// setUniform(u_view, uniformScene.view); + sceneDirty = 1; +} + + +void +setMaterial(const RGBA &color, const SurfaceProperties &surfaceprops, float extraSurfProp) +{ + rw::RGBAf col; + convColor(&col, &color); +// setUniform(u_matColor, &col); + + float surfProps[4]; + surfProps[0] = surfaceprops.ambient; + surfProps[1] = surfaceprops.specular; + surfProps[2] = surfaceprops.diffuse; + surfProps[3] = extraSurfProp; +// setUniform(u_surfProps, surfProps); +} + +void +flushCache(void) +{ + flushGlRenderState(); + +#ifndef RW_GL_USE_UBOS + + // what's this doing here?? + uniformState.fogDisable = rwStateCache.fogEnable ? 0.0f : 1.0f; + uniformState.fogStart = rwStateCache.fogStart; + uniformState.fogEnd = rwStateCache.fogEnd; + uniformState.fogRange = 1.0f/(rwStateCache.fogStart - rwStateCache.fogEnd); + + if(uniformStateDirty[RWGL_ALPHAFUNC] || uniformStateDirty[RWGL_ALPHAREF]){ + float alphaTest[4]; + switch(alphaFunc){ + case ALPHAALWAYS: + default: + alphaTest[0] = -1000.0f; + alphaTest[1] = 1000.0f; + break; + case ALPHAGREATEREQUAL: + alphaTest[0] = alphaRef; + alphaTest[1] = 1000.0f; + break; + case ALPHALESS: + alphaTest[0] = -1000.0f; + alphaTest[1] = alphaRef; + break; + } +// setUniform(u_alphaRef, alphaTest); + uniformStateDirty[RWGL_ALPHAFUNC] = false; + uniformStateDirty[RWGL_ALPHAREF] = false; + } + + if(uniformStateDirty[RWGL_FOG] || + uniformStateDirty[RWGL_FOGSTART] || + uniformStateDirty[RWGL_FOGEND]){ + float fogData[4] = { + uniformState.fogStart, + uniformState.fogEnd, + uniformState.fogRange, + uniformState.fogDisable + }; +// setUniform(u_fogData, fogData); + uniformStateDirty[RWGL_FOG] = false; + uniformStateDirty[RWGL_FOGSTART] = false; + uniformStateDirty[RWGL_FOGEND] = false; + } + + if(uniformStateDirty[RWGL_FOGCOLOR]){ +// setUniform(u_fogColor, &uniformState.fogColor); + uniformStateDirty[RWGL_FOGCOLOR] = false; + } + +#else + if(objectDirty){ + glBindBuffer(GL_UNIFORM_BUFFER, ubo_object); + glBufferData(GL_UNIFORM_BUFFER, sizeof(UniformObject), nil, GL_STREAM_DRAW); + glBufferData(GL_UNIFORM_BUFFER, sizeof(UniformObject), &uniformObject, GL_STREAM_DRAW); + objectDirty = 0; + } + if(sceneDirty){ + glBindBuffer(GL_UNIFORM_BUFFER, ubo_scene); + glBufferData(GL_UNIFORM_BUFFER, sizeof(UniformScene), nil, GL_STREAM_DRAW); + glBufferData(GL_UNIFORM_BUFFER, sizeof(UniformScene), &uniformScene, GL_STREAM_DRAW); + sceneDirty = 0; + } + if(stateDirty){ + switch(alphaFunc){ + case ALPHAALWAYS: + default: + uniformState.alphaRefLow = -1000.0f; + uniformState.alphaRefHigh = 1000.0f; + break; + case ALPHAGREATEREQUAL: + uniformState.alphaRefLow = alphaRef; + uniformState.alphaRefHigh = 1000.0f; + break; + case ALPHALESS: + uniformState.alphaRefLow = -1000.0f; + uniformState.alphaRefHigh = alphaRef; + break; + } + uniformState.fogDisable = rwStateCache.fogEnable ? 0.0f : 1.0f; + uniformState.fogStart = rwStateCache.fogStart; + uniformState.fogEnd = rwStateCache.fogEnd; + uniformState.fogRange = 1.0f/(rwStateCache.fogStart - rwStateCache.fogEnd); + glBindBuffer(GL_UNIFORM_BUFFER, ubo_state); + glBufferData(GL_UNIFORM_BUFFER, sizeof(UniformState), nil, GL_STREAM_DRAW); + glBufferData(GL_UNIFORM_BUFFER, sizeof(UniformState), &uniformState, GL_STREAM_DRAW); + stateDirty = 0; + } +#endif +// flushUniforms(); +} + +static void +setFrameBuffer(Camera *cam) +{ + Raster *fbuf = cam->frameBuffer->parent; + Raster *zbuf = cam->zBuffer->parent; + assert(fbuf); + + Gl1Raster *natfb = PLUGINOFFSET(Gl1Raster, fbuf, nativeRasterOffset); + Gl1Raster *natzb = PLUGINOFFSET(Gl1Raster, zbuf, nativeRasterOffset); + assert(fbuf->type == Raster::CAMERA || fbuf->type == Raster::CAMERATEXTURE); + + // Have to make sure depth buffer is attached to FB's fbo + bindFramebuffer(natfb->fbo); + if (zbuf) { + if(natfb->fboMate != zbuf){ +// glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, GETGL3RASTEREXT(zbuf)->texid); + natfb->fboMate = zbuf; + } + } else if(natfb->fboMate) { +// glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, 0); + natfb->fboMate = nil; + } +} + +static Rect +getFramebufferRect(Raster *frameBuffer) +{ + Rect r; + Raster *fb = frameBuffer->parent; + if(fb->type == Raster::CAMERA){ + r.w = glGlobals.modes[0].width; + r.h = glGlobals.modes[0].height; + }else{ + r.w = fb->width; + r.h = fb->height; + } + r.x = 0; + r.y = 0; + + // Got a subraster + if(frameBuffer != fb){ + r.x = frameBuffer->offsetX; + // GL y offset is from bottom + r.y = r.h - frameBuffer->height - frameBuffer->offsetY; + r.w = frameBuffer->width; + r.h = frameBuffer->height; + } + + return r; +} + +static void +setViewport(Raster *frameBuffer) +{ + Rect r = getFramebufferRect(frameBuffer); + if(r.w != glGlobals.presentWidth || r.h != glGlobals.presentHeight || + r.x != glGlobals.presentOffX || r.y != glGlobals.presentOffY){ + glViewport(r.x, r.y, r.w, r.h); + glGlobals.presentWidth = r.w; + glGlobals.presentHeight = r.h; + glGlobals.presentOffX = r.x; + glGlobals.presentOffY = r.y; + } +} + +static void +beginUpdate(Camera *cam) +{ + float view[16], proj[16]; + // View Matrix + Matrix inv; + Matrix::invert(&inv, cam->getFrame()->getLTM()); + // Since we're looking into positive Z, + // flip X to ge a left handed view space. + view[0] = -inv.right.x; + view[1] = inv.right.y; + view[2] = inv.right.z; + view[3] = 0.0f; + view[4] = -inv.up.x; + view[5] = inv.up.y; + view[6] = inv.up.z; + view[7] = 0.0f; + view[8] = -inv.at.x; + view[9] = inv.at.y; + view[10] = inv.at.z; + view[11] = 0.0f; + view[12] = -inv.pos.x; + view[13] = inv.pos.y; + view[14] = inv.pos.z; + view[15] = 1.0f; + memcpy(&cam->devView, &view, sizeof(RawMatrix)); + setViewMatrix(view); + + // Projection Matrix + float32 invwx = 1.0f/cam->viewWindow.x; + float32 invwy = 1.0f/cam->viewWindow.y; + float32 invz = 1.0f/(cam->farPlane-cam->nearPlane); + + proj[0] = invwx; + proj[1] = 0.0f; + proj[2] = 0.0f; + proj[3] = 0.0f; + + proj[4] = 0.0f; + proj[5] = invwy; + proj[6] = 0.0f; + proj[7] = 0.0f; + + proj[8] = cam->viewOffset.x*invwx; + proj[9] = cam->viewOffset.y*invwy; + proj[12] = -proj[8]; + proj[13] = -proj[9]; + if(cam->projection == Camera::PERSPECTIVE){ + proj[10] = (cam->farPlane+cam->nearPlane)*invz; + proj[11] = 1.0f; + + proj[14] = -2.0f*cam->nearPlane*cam->farPlane*invz; + proj[15] = 0.0f; + }else{ + proj[10] = 2.0f*invz; + proj[11] = 0.0f; + + proj[14] = -(cam->farPlane+cam->nearPlane)*invz; + proj[15] = 1.0f; + } + memcpy(&cam->devProj, &proj, sizeof(RawMatrix)); + setProjectionMatrix(proj); + + if(rwStateCache.fogStart != cam->fogPlane){ + rwStateCache.fogStart = cam->fogPlane; + uniformStateDirty[RWGL_FOGSTART] = true; + stateDirty = 1; + } + if(rwStateCache.fogEnd != cam->farPlane){ + rwStateCache.fogEnd = cam->farPlane; + uniformStateDirty[RWGL_FOGEND] = true; + stateDirty = 1; + } + + setFrameBuffer(cam); + + setViewport(cam->frameBuffer); +} + +static void +endUpdate(Camera *cam) +{ +} + +static void +clearCamera(Camera *cam, RGBA *col, uint32 mode) +{ + RGBAf colf; + GLbitfield mask; + + setFrameBuffer(cam); + + // make sure we're only clearing the part of the framebuffer + // that is subrastered + bool setScissor = cam->frameBuffer != cam->frameBuffer->parent; + if(setScissor){ + Rect r = getFramebufferRect(cam->frameBuffer); + glScissor(r.x, r.y, r.w, r.h); + glEnable(GL_SCISSOR_TEST); + } + + convColor(&colf, col); + glClearColor(colf.red, colf.green, colf.blue, colf.alpha); + mask = 0; + if(mode & Camera::CLEARIMAGE) + mask |= GL_COLOR_BUFFER_BIT; + if(mode & Camera::CLEARZ) + mask |= GL_DEPTH_BUFFER_BIT; + if(mode & Camera::CLEARSTENCIL) + mask |= GL_STENCIL_BUFFER_BIT; + glDepthMask(GL_TRUE); + glClear(mask); + glDepthMask(rwStateCache.zwrite); + + if(setScissor) + glDisable(GL_SCISSOR_TEST); +} + +static void +showRaster(Raster *raster, uint32 flags) +{ +// glViewport(raster->offsetX, raster->offsetY, +// raster->width, raster->height); + // TODO check if context is lost + eglSwapBuffers(eglGetCurrentDisplay(), eglGetCurrentSurface(EGL_DRAW)); +} + +static bool32 +rasterRenderFast(Raster *raster, int32 x, int32 y) +{ + Raster *src = raster; + Raster *dst = Raster::getCurrentContext(); + Gl1Raster *natdst = PLUGINOFFSET(Gl1Raster, dst, nativeRasterOffset); + Gl1Raster *natsrc = PLUGINOFFSET(Gl1Raster, src, nativeRasterOffset); + + switch(dst->type){ + case Raster::NORMAL: + case Raster::TEXTURE: + case Raster::CAMERATEXTURE: + switch(src->type){ + case Raster::CAMERA: + setActiveTexture(0); + glBindTexture(GL_TEXTURE_2D, natdst->texid); + glCopyTexSubImage2D(GL_TEXTURE_2D, 0, x, (dst->height-src->height)-y, + 0, 0, src->width, src->height); + glBindTexture(GL_TEXTURE_2D, boundTexture[0]); + return 1; + } + break; + } + return 0; +} + +static int +openSymbian(EngineOpenParams *openparams) +{ + glGlobals.winWidth = openparams->width; + glGlobals.winHeight = openparams->height; + glGlobals.winTitle = openparams->windowtitle; + + glGlobals.modes = (DisplayMode*)rwMalloc(sizeof(DisplayMode), ID_DRIVER | MEMDUR_EVENT); + glGlobals.modes[0].width = openparams->width; + glGlobals.modes[0].height = openparams->height; + glGlobals.modes[0].depth = 32; + glGlobals.modes[0].flags = VIDEOMODEEXCLUSIVE; + + glGlobals.numModes = 1; + glGlobals.currentMode = 0; + + glGlobals.numMonitors = 1; + glGlobals.currentMonitor = 0; + + return 1; +} + +static int +closeSymbian(void) +{ + if(glGlobals.modes){ + rwFree(glGlobals.modes); + glGlobals.modes = nil; + } + return 1; +} + +static int +startSymbian(void) +{ + glGlobals.presentWidth = glGlobals.winWidth; + glGlobals.presentHeight = glGlobals.winHeight; + glGlobals.presentOffX = 0; + glGlobals.presentOffY = 0; + + return 1; +} + +static int +stopSymbian(void) +{ + return 1; +} + +static int +initOpenGL(void) +{ + +#ifndef RW_GL_USE_UBOS +// u_alphaRef = registerUniform("u_alphaRef", UNIFORM_VEC4); +// u_fogData = registerUniform("u_fogData", UNIFORM_VEC4); +// u_fogColor = registerUniform("u_fogColor", UNIFORM_VEC4); +// u_proj = registerUniform("u_proj", UNIFORM_MAT4); +// u_view = registerUniform("u_view", UNIFORM_MAT4); +// u_world = registerUniform("u_world", UNIFORM_MAT4); +// u_ambLight = registerUniform("u_ambLight", UNIFORM_VEC4); +// u_lightParams = registerUniform("u_lightParams", UNIFORM_VEC4, MAX_LIGHTS); +// u_lightPosition = registerUniform("u_lightPosition", UNIFORM_VEC4, MAX_LIGHTS); +// u_lightDirection = registerUniform("u_lightDirection", UNIFORM_VEC4, MAX_LIGHTS); +// u_lightColor = registerUniform("u_lightColor", UNIFORM_VEC4, MAX_LIGHTS); +// lastShaderUploaded = nil; +#else + registerBlock("Scene"); + registerBlock("Object"); + registerBlock("State"); +#endif +// u_matColor = registerUniform("u_matColor", UNIFORM_VEC4); +// u_surfProps = registerUniform("u_surfProps", UNIFORM_VEC4); + + // for im2d +// registerUniform("u_xform", UNIFORM_VEC4); + + glClearColor(0.25, 0.25, 0.25, 1.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_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, + 0, GL_RGBA, GL_UNSIGNED_BYTE, &whitepixel); + + resetRenderState(); + +#ifndef __SYMBIAN32__ + glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &maxAnisotropy); + + if(gl3Caps.glversion >= 30){ + glGenVertexArrays(1, &vao); + glBindVertexArray(vao); + } +#endif + + openIm2D(); + openIm3D(); + + return 1; +} + +static int +termOpenGL(void) +{ + closeIm3D(); + closeIm2D(); + + glDeleteTextures(1, &whitetex); + whitetex = 0; + + return 1; +} + +static int +finalizeOpenGL(void) +{ + return 1; +} + +static int +deviceSystemSymbian(DeviceReq req, void *arg, int32 n) +{ + VideoMode *rwmode; + + switch(req){ + case DEVICEOPEN: + return openSymbian((EngineOpenParams*)arg); + + case DEVICECLOSE: + return closeSymbian(); + + case DEVICEINIT: + return startSymbian() && initOpenGL(); + + case DEVICETERM: + return termOpenGL() && stopSymbian(); + + case DEVICEFINALIZE: + return finalizeOpenGL(); + + case DEVICEGETNUMSUBSYSTEMS: + return glGlobals.numMonitors; + + case DEVICEGETCURRENTSUBSYSTEM: + return glGlobals.currentMonitor; + + case DEVICESETSUBSYSTEM: + if(n >= glGlobals.numMonitors) + return 0; + glGlobals.currentMonitor = n; + return 1; + + case DEVICEGETSUBSSYSTEMINFO: + if(n >= glGlobals.numMonitors) + return 0; + strncpy(((SubSystemInfo*)arg)->name, "Symbian Display", 80 /*sizeof(SubSystemInfo::name)*/); + return 1; + + case DEVICEGETNUMVIDEOMODES: + return glGlobals.numModes; + + case DEVICEGETCURRENTVIDEOMODE: + return glGlobals.currentMode; + + case DEVICESETVIDEOMODE: + if(n >= glGlobals.numModes) + return 0; + glGlobals.currentMode = n; + return 1; + + case DEVICEGETVIDEOMODEINFO: + rwmode = (VideoMode*)arg; + rwmode->width = glGlobals.modes[n].width; + rwmode->height = glGlobals.modes[n].height; + rwmode->depth = glGlobals.modes[n].depth; + rwmode->flags = glGlobals.modes[n].flags; + return 1; + + case DEVICEGETMAXMULTISAMPLINGLEVELS: + return 1; + + case DEVICEGETMULTISAMPLINGLEVELS: + return 1; + + case DEVICESETMULTISAMPLINGLEVELS: + glGlobals.numSamples = 1; + return 1; + + default: + assert(0 && "not implemented"); + return 0; + } + return 1; +} + +Device renderdevice = { + -1.0f, 1.0f, + gles1::beginUpdate, + gles1::endUpdate, + gles1::clearCamera, + gles1::showRaster, + gles1::rasterRenderFast, + gles1::setRenderState, + gles1::getRenderState, + gles1::im2DRenderLine, + gles1::im2DRenderTriangle, + gles1::im2DRenderPrimitive, + gles1::im2DRenderIndexedPrimitive, + gles1::im3DTransform, + gles1::im3DRenderPrimitive, + gles1::im3DRenderIndexedPrimitive, + gles1::im3DEnd, + gles1::deviceSystemSymbian +}; + +} +} + +#else +// urgh, probably should get rid of that eventually +#include "rwgles1.h" +namespace rw { +namespace gles1 { +Gl1Caps gles1Caps; +bool32 needToReadBackTextures; +} +} +#endif diff --git a/vendor/librw/src/gles1/gl1immed.cpp b/vendor/librw/src/gles1/gl1immed.cpp new file mode 100644 index 0000000..e394b5a --- /dev/null +++ b/vendor/librw/src/gles1/gl1immed.cpp @@ -0,0 +1,180 @@ +#include +#include +#include +#include + +#include "../rwbase.h" +#include "../rwerror.h" +#include "../rwplg.h" +#include "../rwrender.h" +#include "../rwpipeline.h" +#include "../rwobjects.h" +#include "../rwengine.h" +#ifdef RW_GLES1 +#include "rwgles1.h" +#include "rwgles1impl.h" + +namespace rw { +namespace gles1 { + +uint32 im2DVbo, im2DIbo; + +static AttribDesc im2dattribDesc[3] = { + { ATTRIB_POS, GL_FLOAT, GL_FALSE, 4, + sizeof(Im2DVertex), 0 }, + { ATTRIB_COLOR, GL_UNSIGNED_BYTE, GL_TRUE, 4, + sizeof(Im2DVertex), offsetof(Im2DVertex, r) }, + { ATTRIB_TEXCOORDS0, GL_FLOAT, GL_FALSE, 2, + sizeof(Im2DVertex), offsetof(Im2DVertex, u) }, +}; + +static int primTypeMap[] = { + GL_POINTS, // invalid + GL_LINES, + GL_LINE_STRIP, + GL_TRIANGLES, + GL_TRIANGLE_STRIP, + GL_TRIANGLE_FAN, + GL_POINTS +}; + +void +openIm2D(void) +{ +} + +void +closeIm2D(void) +{ +} + +static Im2DVertex tmpprimbuf[3]; + +void +im2DRenderLine(void *vertices, int32 numVertices, int32 vert1, int32 vert2) +{ + Im2DVertex *verts = (Im2DVertex*)vertices; + tmpprimbuf[0] = verts[vert1]; + tmpprimbuf[1] = verts[vert2]; + im2DRenderPrimitive(PRIMTYPELINELIST, tmpprimbuf, 2); +} + +void +im2DRenderTriangle(void *vertices, int32 numVertices, int32 vert1, int32 vert2, int32 vert3) +{ + Im2DVertex *verts = (Im2DVertex*)vertices; + tmpprimbuf[0] = verts[vert1]; + tmpprimbuf[1] = verts[vert2]; + tmpprimbuf[2] = verts[vert3]; + im2DRenderPrimitive(PRIMTYPETRILIST, tmpprimbuf, 3); +} + +void +im2DRenderPrimitive(PrimitiveType primType, void *vertices, int32 numVertices) +{ + Camera *cam; + cam = (Camera*)engine->currentCamera; + + glBindBuffer(GL_ARRAY_BUFFER, im2DVbo); + glBufferData(GL_ARRAY_BUFFER, numVertices*sizeof(Im2DVertex), vertices, GL_STATIC_DRAW); + + flushCache(); + glDrawArrays(primTypeMap[primType], 0, numVertices); +} + +void +im2DRenderIndexedPrimitive(PrimitiveType primType, + void *vertices, int32 numVertices, + void *indices, int32 numIndices) +{ + Camera *cam; + cam = (Camera*)engine->currentCamera; + + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, im2DIbo); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, numIndices*2, indices, GL_STATIC_DRAW); + + glBindBuffer(GL_ARRAY_BUFFER, im2DVbo); + glBufferData(GL_ARRAY_BUFFER, numVertices*sizeof(Im2DVertex), vertices, GL_STATIC_DRAW); + + flushCache(); + glDrawElements(primTypeMap[primType], numIndices, + GL_UNSIGNED_SHORT, nil); +} + + +// Im3D + + +static AttribDesc im3dattribDesc[3] = { + { ATTRIB_POS, GL_FLOAT, GL_FALSE, 3, + sizeof(Im3DVertex), 0 }, + { ATTRIB_COLOR, GL_UNSIGNED_BYTE, GL_TRUE, 4, + sizeof(Im3DVertex), offsetof(Im3DVertex, r) }, + { ATTRIB_TEXCOORDS0, GL_FLOAT, GL_FALSE, 2, + sizeof(Im3DVertex), offsetof(Im3DVertex, u) }, +}; +static uint32 im3DVbo, im3DIbo; +static int32 num3DVertices; // not actually needed here +static void* currentIm3dVertices; + +void +openIm3D(void) +{ + glGenBuffers(1, &im3DIbo); + glGenBuffers(1, &im3DVbo); +} + +void +closeIm3D(void) +{ + glDeleteBuffers(1, &im3DIbo); + glDeleteBuffers(1, &im3DVbo); +} + +void +im3DTransform(void *vertices, int32 numVertices, Matrix *world, uint32 flags) +{ + if(world == nil){ + static Matrix ident; + ident.setIdentity(); + world = &ident; + } + setWorldMatrix(world); + + if((flags & im3d::VERTEXUV) == 0) + SetRenderStatePtr(TEXTURERASTER, nil); + + glBindBuffer(GL_ARRAY_BUFFER, im3DVbo); + glBufferData(GL_ARRAY_BUFFER, numVertices*sizeof(Im3DVertex), vertices, GL_DYNAMIC_DRAW); + num3DVertices = numVertices; +} + +void +im3DRenderPrimitive(PrimitiveType primType) +{ + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, im3DIbo); + + flushCache(); + glDrawArrays(primTypeMap[primType], 0, num3DVertices); +} + +void +im3DRenderIndexedPrimitive(PrimitiveType primType, void *indices, int32 numIndices) +{ + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, im3DIbo); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, numIndices*2, indices, GL_STATIC_DRAW); + + flushCache(); + glDrawElements(primTypeMap[primType], numIndices, + GL_UNSIGNED_SHORT, nil); +} + +void +im3DEnd(void) +{ +} + +} +} + +#endif diff --git a/vendor/librw/src/gles1/gl1pipe.cpp b/vendor/librw/src/gles1/gl1pipe.cpp new file mode 100644 index 0000000..27b2b59 --- /dev/null +++ b/vendor/librw/src/gles1/gl1pipe.cpp @@ -0,0 +1,317 @@ +#include +#include +#include +#include + +#include "../rwbase.h" +#include "../rwerror.h" +#include "../rwplg.h" +#include "../rwpipeline.h" +#include "../rwobjects.h" +#include "../rwengine.h" + +#include "rwgles1.h" + +namespace rw { +namespace gles1 { + +// TODO: make some of these things platform-independent + +#ifdef RW_GLES1 + +void +freeInstanceData(Geometry *geometry) +{ + if(geometry->instData == nil || + geometry->instData->platform != PLATFORM_GLES1) + return; + InstanceDataHeader *header = (InstanceDataHeader*)geometry->instData; + geometry->instData = nil; + glDeleteBuffers(1, &header->ibo); + glDeleteBuffers(1, &header->vbo); + rwFree(header->indexBuffer); + rwFree(header->vertexBuffer); + rwFree(header->attribDesc); + rwFree(header->inst); + rwFree(header); +} + +void* +destroyNativeData(void *object, int32, int32) +{ + freeInstanceData((Geometry*)object); + return object; +} + +static InstanceDataHeader* +instanceMesh(rw::ObjPipeline *rwpipe, Geometry *geo) +{ + InstanceDataHeader *header = rwNewT(InstanceDataHeader, 1, MEMDUR_EVENT | ID_GEOMETRY); + MeshHeader *meshh = geo->meshHeader; + geo->instData = header; + header->platform = PLATFORM_GLES1; + + header->serialNumber = meshh->serialNum; + header->numMeshes = meshh->numMeshes; + header->primType = meshh->flags == 1 ? GL_TRIANGLE_STRIP : GL_TRIANGLES; + header->totalNumVertex = geo->numVertices; + header->totalNumIndex = meshh->totalIndices; + header->inst = rwNewT(InstanceData, header->numMeshes, MEMDUR_EVENT | ID_GEOMETRY); + + header->indexBuffer = rwNewT(uint16, header->totalNumIndex, MEMDUR_EVENT | ID_GEOMETRY); + InstanceData *inst = header->inst; + Mesh *mesh = meshh->getMeshes(); + uint32 offset = 0; + for(uint32 i = 0; i < header->numMeshes; i++){ + findMinVertAndNumVertices(mesh->indices, mesh->numIndices, + &inst->minVert, &inst->numVertices); + assert(inst->minVert != 0xFFFFFFFF); + inst->numIndex = mesh->numIndices; + inst->material = mesh->material; + inst->vertexAlpha = 0; + inst->program = 0; + inst->offset = offset; + memcpy((uint8*)header->indexBuffer + inst->offset, + mesh->indices, inst->numIndex*2); + offset += inst->numIndex*2; + mesh++; + inst++; + } + + header->vertexBuffer = nil; + header->numAttribs = 0; + header->attribDesc = nil; + header->ibo = 0; + header->vbo = 0; + + glGenBuffers(1, &header->ibo); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, header->ibo); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, header->totalNumIndex*2, + header->indexBuffer, GL_STATIC_DRAW); + + return header; +} + +static void +instance(rw::ObjPipeline *rwpipe, Atomic *atomic) +{ + ObjPipeline *pipe = (ObjPipeline*)rwpipe; + Geometry *geo = atomic->geometry; + // don't try to (re)instance native data + if(geo->flags & Geometry::NATIVE) + return; + + InstanceDataHeader *header = (InstanceDataHeader*)geo->instData; + if(geo->instData){ + // Already have instanced data, so check if we have to reinstance + assert(header->platform == PLATFORM_GLES1); + if(header->serialNumber != geo->meshHeader->serialNum){ + // Mesh changed, so reinstance everything + freeInstanceData(geo); + } + } + + // no instance or complete reinstance + if(geo->instData == nil){ + geo->instData = instanceMesh(rwpipe, geo); + pipe->instanceCB(geo, (InstanceDataHeader*)geo->instData, 0); + }else if(geo->lockedSinceInst) + pipe->instanceCB(geo, (InstanceDataHeader*)geo->instData, 1); + + geo->lockedSinceInst = 0; +} + +static void +uninstance(rw::ObjPipeline *rwpipe, Atomic *atomic) +{ + assert(0 && "can't uninstance"); +} + +static void +render(rw::ObjPipeline *rwpipe, Atomic *atomic) +{ + ObjPipeline *pipe = (ObjPipeline*)rwpipe; + Geometry *geo = atomic->geometry; + pipe->instance(atomic); + assert(geo->instData != nil); + assert(geo->instData->platform == PLATFORM_GLES1); + if(pipe->renderCB) + pipe->renderCB(atomic, (InstanceDataHeader*)geo->instData); +} + +void +ObjPipeline::init(void) +{ + this->rw::ObjPipeline::init(PLATFORM_GLES1); + this->impl.instance = gles1::instance; + this->impl.uninstance = gles1::uninstance; + this->impl.render = gles1::render; + this->instanceCB = nil; + this->uninstanceCB = nil; + this->renderCB = nil; +} + +ObjPipeline* +ObjPipeline::create(void) +{ + ObjPipeline *pipe = rwNewT(ObjPipeline, 1, MEMDUR_GLOBAL); + pipe->init(); + return pipe; +} + +void +defaultInstanceCB(Geometry *geo, InstanceDataHeader *header, bool32 reinstance) +{ + AttribDesc *attribs, *a; + + bool isPrelit = !!(geo->flags & Geometry::PRELIT); + bool hasNormals = !!(geo->flags & Geometry::NORMALS); + + if(!reinstance){ + AttribDesc tmpAttribs[12]; + uint32 stride; + + // + // Create attribute descriptions + // + a = tmpAttribs; + stride = 0; + + // Positions + a->index = ATTRIB_POS; + a->size = 3; + a->type = GL_FLOAT; + a->normalized = GL_FALSE; + a->offset = stride; + stride += 12; + a++; + + // Normals + // TODO: compress + if(hasNormals){ + a->index = ATTRIB_NORMAL; + a->size = 3; + a->type = GL_FLOAT; + a->normalized = GL_FALSE; + a->offset = stride; + stride += 12; + a++; + } + + // Prelighting + if(isPrelit){ + a->index = ATTRIB_COLOR; + a->size = 4; + a->type = GL_UNSIGNED_BYTE; + a->normalized = GL_TRUE; + a->offset = stride; + stride += 4; + a++; + } + + // Texture coordinates + for(int32 n = 0; n < geo->numTexCoordSets; n++){ + a->index = ATTRIB_TEXCOORDS0+n; + a->size = 2; + a->type = GL_FLOAT; + a->normalized = GL_FALSE; + a->offset = stride; + stride += 8; + a++; + } + + header->numAttribs = a - tmpAttribs; + for(a = tmpAttribs; a != &tmpAttribs[header->numAttribs]; a++) + a->stride = stride; + header->attribDesc = rwNewT(AttribDesc, header->numAttribs, MEMDUR_EVENT | ID_GEOMETRY); + memcpy(header->attribDesc, tmpAttribs, + header->numAttribs*sizeof(AttribDesc)); + + // + // Allocate vertex buffer + // + header->vertexBuffer = rwNewT(uint8, header->totalNumVertex*stride, MEMDUR_EVENT | ID_GEOMETRY); + assert(header->vbo == 0); + glGenBuffers(1, &header->vbo); + } + + attribs = header->attribDesc; + + // + // Fill vertex buffer + // + + uint8 *verts = header->vertexBuffer; + + // Positions + if(!reinstance || geo->lockedSinceInst&Geometry::LOCKVERTICES){ + for(a = attribs; a->index != ATTRIB_POS; a++) + ; + instV3d(VERT_FLOAT3, verts + a->offset, + geo->morphTargets[0].vertices, + header->totalNumVertex, a->stride); + } + + // Normals + if(hasNormals && (!reinstance || geo->lockedSinceInst&Geometry::LOCKNORMALS)){ + for(a = attribs; a->index != ATTRIB_NORMAL; a++) + ; + instV3d(VERT_FLOAT3, verts + a->offset, + geo->morphTargets[0].normals, + header->totalNumVertex, a->stride); + } + + // Prelighting + if(isPrelit && (!reinstance || geo->lockedSinceInst&Geometry::LOCKPRELIGHT)){ + for(a = attribs; a->index != ATTRIB_COLOR; a++) + ; + int n = header->numMeshes; + InstanceData *inst = header->inst; + while(n--){ + assert(inst->minVert != 0xFFFFFFFF); + inst->vertexAlpha = instColor(VERT_RGBA, + verts + a->offset + a->stride*inst->minVert, + geo->colors + inst->minVert, + inst->numVertices, a->stride); + inst++; + } + } + + // Texture coordinates + for(int32 n = 0; n < geo->numTexCoordSets; n++){ + if(!reinstance || geo->lockedSinceInst&(Geometry::LOCKTEXCOORDS<index != ATTRIB_TEXCOORDS0+n; a++) + ; + instTexCoords(VERT_FLOAT2, verts + a->offset, + geo->texCoords[n], + header->totalNumVertex, a->stride); + } + } + + glBindBuffer(GL_ARRAY_BUFFER, header->vbo); + glBufferData(GL_ARRAY_BUFFER, header->totalNumVertex*attribs[0].stride, + header->vertexBuffer, GL_STATIC_DRAW); +} + +void +defaultUninstanceCB(Geometry *geo, InstanceDataHeader *header) +{ + assert(0 && "can't uninstance"); +} + +ObjPipeline* +makeDefaultPipeline(void) +{ + ObjPipeline *pipe = ObjPipeline::create(); + pipe->instanceCB = defaultInstanceCB; + pipe->uninstanceCB = defaultUninstanceCB; + pipe->renderCB = defaultRenderCB; + return pipe; +} + +#else +void *destroyNativeData(void *object, int32, int32) { return object; } +#endif + +} +} diff --git a/vendor/librw/src/gles1/gl1raster.cpp b/vendor/librw/src/gles1/gl1raster.cpp new file mode 100644 index 0000000..be7118d --- /dev/null +++ b/vendor/librw/src/gles1/gl1raster.cpp @@ -0,0 +1,961 @@ +#include +#include +#include +#include + +#include "../rwbase.h" +#include "../rwerror.h" +#include "../rwplg.h" +#include "../rwpipeline.h" +#include "../rwobjects.h" +#include "../rwengine.h" + +#include "rwgles1.h" +#include "rwgles1impl.h" + +#define PLUGIN_ID ID_DRIVER + +namespace rw { +namespace gles1 { + +int32 nativeRasterOffset; + +static uint32 +getLevelSize(Raster *raster, int32 level) +{ + int i; + Gl1Raster *natras = GETGL1RASTEREXT(raster); + + int w = raster->originalWidth; + int h = raster->originalHeight; + int s = raster->originalStride; + int minDim = 1; + +//#ifdef RW_GLES1 +// switch(natras->internalFormat){ +// case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT: +// case GL_COMPRESSED_RGB_S3TC_DXT1_EXT: +// case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT: +// case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT: +// minDim = 4; +// break; +// } +//#endif + + for(i = 0; i < level; i++){ + if(w > minDim){ + w /= 2; + s /= 2; + } + if(h > minDim) + h /= 2; + } + + return s*h; +} + +#ifdef RW_GLES1 + +static Raster* +rasterCreateTexture(Raster *raster) +{ + if(raster->format & (Raster::PAL4 | Raster::PAL8)){ + RWERROR((ERR_NOTEXTURE)); + return nil; + } + + Gl1Raster *natras = GETGL1RASTEREXT(raster); + switch(raster->format & 0xF00){ + case Raster::C8888: + natras->internalFormat = GL_RGBA; + natras->format = GL_RGBA; + natras->type = GL_UNSIGNED_BYTE; + natras->hasAlpha = 1; + natras->bpp = 4; + raster->depth = 32; + break; + case Raster::C888: + natras->internalFormat = GL_RGB; + natras->format = GL_RGB; + natras->type = GL_UNSIGNED_BYTE; + natras->hasAlpha = 0; + natras->bpp = 3; + raster->depth = 24; + break; + case Raster::C565: + natras->internalFormat = GL_RGB; + natras->format = GL_RGB; + natras->type = GL_UNSIGNED_SHORT_5_6_5; + natras->hasAlpha = 0; + natras->bpp = 2; + raster->depth = 16; + break; + case Raster::C4444: + natras->internalFormat = GL_RGBA; + natras->format = GL_RGBA; + natras->type = GL_UNSIGNED_SHORT_4_4_4_4; + natras->hasAlpha = 1; + natras->bpp = 2; + raster->depth = 16; + break; + case Raster::C1555: +// natras->internalFormat = GL_RGB5_A1; + natras->internalFormat = GL_RGBA; + natras->format = GL_RGBA; + natras->type = GL_UNSIGNED_SHORT_5_5_5_1; + natras->hasAlpha = 1; + natras->bpp = 2; + raster->depth = 16; + break; + default: + RWERROR((ERR_INVRASTER)); + return nil; + } + + natras->internalFormat = natras->format; + + raster->stride = raster->width*natras->bpp; + + if(raster->format & Raster::MIPMAP){ + int w = raster->width; + int h = raster->height; + natras->numLevels = 1; + while(w != 1 || h != 1){ + natras->numLevels++; + if(w > 1) w /= 2; + if(h > 1) h /= 2; + } + } + natras->autogenMipmap = (raster->format & (Raster::MIPMAP|Raster::AUTOMIPMAP)) == (Raster::MIPMAP|Raster::AUTOMIPMAP); + if(natras->autogenMipmap) + natras->numLevels = 1; + + glGenTextures(1, &natras->texid); + uint32 prev = bindTexture(natras->texid); + glTexImage2D(GL_TEXTURE_2D, 0, natras->internalFormat, +// raster->width, raster->height, + 1,1, + 0, natras->format, natras->type, nil); + // TODO: allocate other levels...probably + natras->filterMode = 0; + natras->addressU = 0; + natras->addressV = 0; + natras->maxAnisotropy = 1; + + bindTexture(prev); + return raster; +} + +static Raster* +rasterCreateCameraTexture(Raster *raster) +{ + if(raster->format & (Raster::PAL4 | Raster::PAL8)){ + RWERROR((ERR_NOTEXTURE)); + return nil; + } + + // TODO: figure out what the backbuffer is and use that as a default + Gl1Raster *natras = GETGL1RASTEREXT(raster); + switch(raster->format & 0xF00){ + case Raster::C8888: + natras->internalFormat = GL_RGBA; + natras->format = GL_RGBA; + natras->type = GL_UNSIGNED_BYTE; + natras->hasAlpha = 1; + natras->bpp = 4; + break; + case Raster::C4444: + natras->internalFormat = GL_RGBA; + natras->format = GL_RGBA; + natras->type = GL_UNSIGNED_SHORT_4_4_4_4; + natras->hasAlpha = 1; + natras->bpp = 2; + break; + case Raster::C888: + default: + natras->internalFormat = GL_RGB; + natras->format = GL_RGB; + natras->type = GL_UNSIGNED_BYTE; + natras->hasAlpha = 0; + natras->bpp = 3; + break; + case Raster::C565: + natras->internalFormat = GL_RGB; + natras->format = GL_RGB; + natras->type = GL_UNSIGNED_SHORT_5_6_5; + natras->hasAlpha = 0; + natras->bpp = 2; + break; + case Raster::C1555: +// natras->internalFormat = GL_RGB5_A1; + natras->internalFormat = GL_RGBA; + natras->format = GL_RGBA; + natras->type = GL_UNSIGNED_SHORT_5_5_5_1; + natras->hasAlpha = 1; + natras->bpp = 2; + break; + } + + natras->internalFormat = natras->format; + + raster->stride = raster->width*natras->bpp; + + natras->autogenMipmap = (raster->format & (Raster::MIPMAP|Raster::AUTOMIPMAP)) == (Raster::MIPMAP|Raster::AUTOMIPMAP); + + glGenTextures(1, &natras->texid); + uint32 prev = bindTexture(natras->texid); + glTexImage2D(GL_TEXTURE_2D, 0, natras->internalFormat, +// raster->width, raster->height, + 1,1, + 0, natras->format, natras->type, nil); + natras->filterMode = 0; + natras->addressU = 0; + natras->addressV = 0; + natras->maxAnisotropy = 1; + + bindTexture(prev); + + +// glGenFramebuffers(1, &natras->fbo); +// bindFramebuffer(natras->fbo); +// glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, natras->texid, 0); +// bindFramebuffer(0); + natras->fboMate = nil; + + return raster; +} + +static Raster* +rasterCreateCamera(Raster *raster) +{ + Gl1Raster *natras = GETGL1RASTEREXT(raster); + + // TODO: set/check width, height, depth, format? + + // used for locking right now +// raster->format = Raster::C888; +// natras->internalFormat = GL_RGB; +// natras->format = GL_RGB; +// natras->type = GL_UNSIGNED_BYTE; +// natras->hasAlpha = 0; +// natras->bpp = 3; +// +// natras->autogenMipmap = 0; + + raster->originalWidth = raster->width; + raster->originalHeight = raster->height; + raster->stride = 0; + raster->pixels = nil; + + natras->texid = 0; + natras->fbo = 0; + natras->fboMate = nil; + + return raster; +} + +static Raster* +rasterCreateZbuffer(Raster *raster) +{ + Gl1Raster *natras = GETGL1RASTEREXT(raster); + + raster->originalWidth = raster->width; + raster->originalHeight = raster->height; + raster->stride = 0; + raster->pixels = nil; + +// if(gl1Caps.gles){ +// // have to use RBO on GLES!! +// glGenRenderbuffers(1, &natras->texid); +// glBindRenderbuffer(GL_RENDERBUFFER, natras->texid); +//#ifdef __SYMBIAN32__ +// glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, raster->width, raster->height); +//#else +// glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, raster->width, raster->height); +//#endif +//#ifndef __SYMBIAN32__ +// }else{ +// // TODO: set/check width, height, depth, format? +// natras->internalFormat = GL_DEPTH_STENCIL; +// natras->format = GL_DEPTH_STENCIL; +// natras->type = GL_UNSIGNED_INT_24_8; +// +// natras->autogenMipmap = 0; +// +// glGenTextures(1, &natras->texid); +// uint32 prev = bindTexture(natras->texid); +// glTexImage2D(GL_TEXTURE_2D, 0, natras->internalFormat, +// raster->width, raster->height, +// 0, natras->format, natras->type, nil); +// natras->filterMode = 0; +// natras->addressU = 0; +// natras->addressV = 0; +// natras->maxAnisotropy = 1; +// +// bindTexture(prev); +//#endif +// } +// natras->fbo = 0; +// natras->fboMate = nil; + + return raster; +} + + +#endif + +/* +{ 0, 0, 0 }, +{ 16, 4, GL_RGBA }, // 1555 +{ 16, 3, GL_RGB }, // 565 +{ 16, 4, GL_RGBA }, // 4444 +{ 0, 0, 0 }, // LUM8 +{ 32, 4, GL_RGBA }, // 8888 +{ 24, 3, GL_RGB }, // 888 +{ 16, 3, GL_RGB }, // D16 +{ 24, 3, GL_RGB }, // D24 +{ 32, 4, GL_RGBA }, // D32 +{ 16, 3, GL_RGB }, // 555 + +0, +GL_RGB5_A1, +GL_RGB5, +GL_RGBA4, +0, +GL_RGBA8, +GL_RGB8, +GL_RGB5, +GL_RGB8, +GL_RGBA8, +GL_RGB5 +*/ + +Raster* +rasterCreate(Raster *raster) +{ + Gl1Raster *natras = GETGL1RASTEREXT(raster); + + natras->isCompressed = 0; + natras->hasAlpha = 0; + natras->numLevels = 1; + + Raster *ret = raster; + + if(raster->width == 0 || raster->height == 0){ + raster->flags |= Raster::DONTALLOCATE; + raster->stride = 0; + goto ret; + } + if(raster->flags & Raster::DONTALLOCATE) + goto ret; + + switch(raster->type){ +#ifdef RW_GLES1 + case Raster::NORMAL: + case Raster::TEXTURE: + ret = rasterCreateTexture(raster); + break; + case Raster::CAMERATEXTURE: + ret = rasterCreateCameraTexture(raster); + break; + case Raster::ZBUFFER: + ret = rasterCreateZbuffer(raster); + break; + case Raster::CAMERA: + ret = rasterCreateCamera(raster); + break; +#endif + + default: + RWERROR((ERR_INVRASTER)); + return nil; + } + +ret: + raster->originalWidth = raster->width; + raster->originalHeight = raster->height; + raster->originalStride = raster->stride; + raster->originalPixels = raster->pixels; + return ret; +} + +uint8* +rasterLock(Raster *raster, int32 level, int32 lockMode) +{ +#ifdef RW_GLES1 + Gl1Raster *natras GETGL1RASTEREXT(raster); + uint8 *px; + uint32 allocSz; + int i; + + assert(raster->privateFlags == 0); + + switch(raster->type){ + case Raster::NORMAL: + case Raster::TEXTURE: + case Raster::CAMERATEXTURE: + for(i = 0; i < level; i++){ + if(raster->width > 1){ + raster->width /= 2; + raster->stride /= 2; + } + if(raster->height > 1) + raster->height /= 2; + } + + allocSz = getLevelSize(raster, level); + px = (uint8*)rwMalloc(allocSz, MEMDUR_EVENT | ID_DRIVER); + assert(raster->pixels == nil); + raster->pixels = px; + + if(lockMode & Raster::LOCKREAD || !(lockMode & Raster::LOCKNOFETCH)){ + if(natras->isCompressed){ + if(natras->backingStore){ + assert(level < natras->backingStore->numlevels); + assert(allocSz >= natras->backingStore->levels[level].size); + memcpy(px, natras->backingStore->levels[level].data, allocSz); + }else{ + // GLES is losing here + } + } else { + // TODO + } + } + + raster->privateFlags = lockMode; + break; + + case Raster::CAMERA: + if(lockMode & Raster::PRIVATELOCK_WRITE) + assert(0 && "can't lock framebuffer for writing"); + raster->width = glGlobals.presentWidth; + raster->height = glGlobals.presentHeight; + raster->stride = raster->width*natras->bpp; + assert(natras->bpp == 3); + allocSz = raster->height*raster->stride; + px = (uint8*)rwMalloc(allocSz, MEMDUR_EVENT | ID_DRIVER); + assert(raster->pixels == nil); + raster->pixels = px; +#ifdef __SYMBIAN32__ + memset(px, 0, allocSz); +#else + glReadBuffer(GL_BACK); +#endif + glReadPixels(0, 0, raster->width, raster->height, GL_RGB, GL_UNSIGNED_BYTE, px); + + raster->privateFlags = lockMode; + break; + + default: + assert(0 && "cannot lock this type of raster yet"); + return nil; + } + + return px; +#else + return nil; +#endif +} + +void +rasterUnlock(Raster *raster, int32 level) +{ +#ifdef RW_GLES1 + Gl1Raster *natras = GETGL1RASTEREXT(raster); + + assert(raster->pixels); + + switch(raster->type){ + case Raster::NORMAL: + case Raster::TEXTURE: + case Raster::CAMERATEXTURE: + if(raster->privateFlags & Raster::LOCKWRITE){ + if (level != 0) break; + uint32 prev = bindTexture(natras->texid); + if(natras->isCompressed){ + glCompressedTexImage2D(GL_TEXTURE_2D, level, natras->internalFormat, + raster->width, raster->height, 0, + getLevelSize(raster, level), + raster->pixels); + if(natras->backingStore){ + assert(level < natras->backingStore->numlevels); + memcpy(natras->backingStore->levels[level].data, raster->pixels, + natras->backingStore->levels[level].size); + } + }else{ + if (raster->pixels != nil && natras->format == GL_RGBA && natras->type == GL_UNSIGNED_BYTE) { + // convert to 16-bit + uint16_t* pixels16 = (uint16_t*)malloc(raster->width * raster->height * sizeof(uint16_t)); + uint8_t* pixels8 = (uint8_t*)raster->pixels; + + for (int i = 0; i < raster->width * raster->height; i++) { + uint8_t r = pixels8[i * 4 + 0]; + uint8_t g = pixels8[i * 4 + 1]; + uint8_t b = pixels8[i * 4 + 2]; + uint8_t a = pixels8[i * 4 + 3]; + + pixels16[i] = ((r >> 4) << 12) | ((g >> 4) << 8) | ((b >> 4) << 4) | (a >> 4); + } + glTexImage2D(GL_TEXTURE_2D, level, GL_RGBA, raster->width, raster->height, 0, GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4, pixels16); + free(pixels16); + } else + { +// glPixelStorei(GL_UNPACK_ALIGNMENT, 1); + glTexImage2D(GL_TEXTURE_2D, level, natras->internalFormat, + raster->width, raster->height, + 0, natras->format, natras->type, raster->pixels); + } + } +// if(level == 0 && natras->autogenMipmap) +// glGenerateMipmap(GL_TEXTURE_2D); + bindTexture(prev); + } + break; + + case Raster::CAMERA: + // TODO: write? + break; + } + + rwFree(raster->pixels); + raster->pixels = nil; +#endif + raster->width = raster->originalWidth; + raster->height = raster->originalHeight; + raster->stride = raster->originalStride; + raster->pixels = raster->originalPixels; + raster->privateFlags = 0; +} + +int32 +rasterNumLevels(Raster *raster) +{ + return GETGL1RASTEREXT(raster)->numLevels; +} + +// Almost the same as d3d9 and ps2 function +bool32 +imageFindRasterFormat(Image *img, int32 type, + int32 *pWidth, int32 *pHeight, int32 *pDepth, int32 *pFormat) +{ + int32 width, height, depth, format; + + assert((type&0xF) == Raster::TEXTURE); + +// for(width = 1; width < img->width; width <<= 1); +// for(height = 1; height < img->height; height <<= 1); + // Perhaps non-power-of-2 textures are acceptable? + width = img->width; + height = img->height; + + depth = img->depth; + + if(depth <= 8) + depth = 32; + + switch(depth){ + case 32: + if(img->hasAlpha()) + format = Raster::C8888; + else{ + format = Raster::C888; + depth = 24; + } + break; + case 24: + format = Raster::C888; + break; + case 16: + format = Raster::C1555; + break; + + case 8: + case 4: + default: + RWERROR((ERR_INVRASTER)); + return 0; + } + + format |= type; + + *pWidth = width; + *pHeight = height; + *pDepth = depth; + *pFormat = format; + + return 1; +} + +bool32 +rasterFromImage(Raster *raster, Image *image) +{ + if((raster->type&0xF) != Raster::TEXTURE) + return 0; + + void (*conv)(uint8 *out, uint8 *in) = nil; + + // Unpalettize image if necessary but don't change original + Image *truecolimg = nil; + if(image->depth <= 8){ + truecolimg = Image::create(image->width, image->height, image->depth); + truecolimg->pixels = image->pixels; + truecolimg->stride = image->stride; + truecolimg->palette = image->palette; + truecolimg->unpalettize(); + image = truecolimg; + } + + Gl1Raster *natras = GETGL1RASTEREXT(raster); + int32 format = raster->format&0xF00; + assert(!natras->isCompressed); + switch(image->depth){ + case 32: + if(format == Raster::C8888) + conv = conv_RGBA8888_from_RGBA8888; + else if(format == Raster::C888) + conv = conv_RGB888_from_RGB888; + else + goto err; + break; + case 24: + if(format == Raster::C8888) + conv = conv_RGBA8888_from_RGB888; + else if(format == Raster::C888) + conv = conv_RGB888_from_RGB888; + else + goto err; + break; + case 16: + if(format == Raster::C8888) + conv = conv_RGBA8888_from_ARGB1555; + else if(format == Raster::C1555) + conv = conv_RGBA5551_from_ARGB1555; + else + goto err; + break; + + case 8: + case 4: + default: + err: + RWERROR((ERR_INVRASTER)); + return 0; + } + + natras->hasAlpha = image->hasAlpha(); + + bool unlock = false; + if(raster->pixels == nil){ + raster->lock(0, Raster::LOCKWRITE|Raster::LOCKNOFETCH); + unlock = true; + } + + uint8 *pixels = raster->pixels; + assert(pixels); + uint8 *imgpixels = image->pixels + (image->height-1)*image->stride; + + int x, y; + assert(image->width == raster->width); + assert(image->height == raster->height); + for(y = 0; y < image->height; y++){ + uint8 *imgrow = imgpixels; + uint8 *rasrow = pixels; + for(x = 0; x < image->width; x++){ + conv(rasrow, imgrow); + imgrow += image->bpp; + rasrow += natras->bpp; + } + imgpixels -= image->stride; + pixels += raster->stride; + } + if(unlock) + raster->unlock(0); + + if(truecolimg) + truecolimg->destroy(); + + return 1; +} + +Image* +rasterToImage(Raster *raster) +{ + int32 depth; + Image *image; + + bool unlock = false; + if(raster->pixels == nil){ + raster->lock(0, Raster::LOCKREAD); + unlock = true; + } + + Gl1Raster *natras = GETGL1RASTEREXT(raster); + if(natras->isCompressed){ + // TODO + RWERROR((ERR_INVRASTER)); + return nil; + } + + void (*conv)(uint8 *out, uint8 *in) = nil; + switch(raster->format & 0xF00){ + case Raster::C1555: + depth = 16; + conv = conv_ARGB1555_from_RGBA5551; + break; + case Raster::C8888: + depth = 32; + conv = conv_RGBA8888_from_RGBA8888; + break; + case Raster::C888: + depth = 24; + conv = conv_RGB888_from_RGB888; + break; + + default: + case Raster::C555: + case Raster::C565: + case Raster::C4444: + case Raster::LUM8: + RWERROR((ERR_INVRASTER)); + return nil; + } + + if(raster->format & Raster::PAL4 || + raster->format & Raster::PAL8){ + RWERROR((ERR_INVRASTER)); + return nil; + } + + uint8 *in, *out; + image = Image::create(raster->width, raster->height, depth); + image->allocate(); + + uint8 *imgpixels = image->pixels + (image->height-1)*image->stride; + uint8 *pixels = raster->pixels; + + int x, y; + assert(image->width == raster->width); + assert(image->height == raster->height); + for(y = 0; y < image->height; y++){ + uint8 *imgrow = imgpixels; + uint8 *rasrow = pixels; + for(x = 0; x < image->width; x++){ + conv(imgrow, rasrow); + imgrow += image->bpp; + rasrow += natras->bpp; + } + imgpixels -= image->stride; + pixels += raster->stride; + } + + if(unlock) + raster->unlock(0); + + return image; +} + +static void* +createNativeRaster(void *object, int32 offset, int32) +{ + Gl1Raster *ras = PLUGINOFFSET(Gl1Raster, object, offset); + ras->texid = 0; + ras->fbo = 0; + ras->fboMate = nil; + ras->backingStore = nil; + return object; +} + +void evictRaster(Raster *raster); + +static void* +destroyNativeRaster(void *object, int32 offset, int32) +{ + Raster *raster = (Raster*)object; + Gl1Raster *natras = PLUGINOFFSET(Gl1Raster, object, offset); +#ifdef RW_GLES1 + evictRaster(raster); + switch(raster->type){ + case Raster::NORMAL: + case Raster::TEXTURE: + glDeleteTextures(1, &natras->texid); + break; + + case Raster::CAMERATEXTURE: + break; + + case Raster::ZBUFFER: + break; + + case Raster::CAMERA: + if(natras->fboMate){ + // Break apart from currently associated zbuffer + Gl1Raster *zras = GETGL1RASTEREXT(natras->fboMate); + zras->fboMate = nil; + natras->fboMate = nil; + } + break; + } + natras->texid = 0; + natras->fbo = 0; + + free(natras->backingStore); + +#endif + return object; +} + +static void* +copyNativeRaster(void *dst, void *, int32 offset, int32) +{ + Gl1Raster *d = PLUGINOFFSET(Gl1Raster, dst, offset); + d->texid = 0; + d->fbo = 0; + d->fboMate = nil; + d->backingStore = nil; + return dst; +} + +Texture* +readNativeTexture(Stream *stream) +{ + uint32 platform; + if(!findChunk(stream, ID_STRUCT, nil, nil)){ + RWERROR((ERR_CHUNK, "STRUCT")); + return nil; + } + platform = stream->readU32(); + if(platform != PLATFORM_GLES1){ + RWERROR((ERR_PLATFORM, platform)); + return nil; + } + Texture *tex = Texture::create(nil); + if(tex == nil) + return nil; + + // Texture + tex->filterAddressing = stream->readU32(); + stream->read8(tex->name, 32); + stream->read8(tex->mask, 32); + + // Raster + uint32 format = stream->readU32(); + int32 width = stream->readI32(); + int32 height = stream->readI32(); + int32 depth = stream->readI32(); + int32 numLevels = stream->readI32(); + + // Native raster + int32 subplatform = stream->readI32(); + int32 flags = stream->readI32(); + int32 compression = stream->readI32(); + + Raster *raster; + Gl1Raster *natras; + if(flags & 2){ + tex->destroy(); + RWERROR((ERR_FORMAT_UNSUPPORTED)); + return nil; + }else{ + raster = Raster::create(width, height, depth, format | Raster::TEXTURE, PLATFORM_GLES1); + } + assert(raster); + natras = GETGL1RASTEREXT(raster); + tex->raster = raster; + + uint32 size; + uint8 *data; + for(int32 i = 0; i < numLevels; i++){ + size = stream->readU32(); + data = raster->lock(i, Raster::LOCKWRITE|Raster::LOCKNOFETCH); + stream->read8(data, size); + raster->unlock(i); + } + return tex; +} + +void +writeNativeTexture(Texture *tex, Stream *stream) +{ + Raster *raster = tex->raster; + Gl1Raster *natras = GETGL1RASTEREXT(raster); + + int32 chunksize = getSizeNativeTexture(tex); + writeChunkHeader(stream, ID_STRUCT, chunksize-12); + stream->writeU32(PLATFORM_GLES1); + + // Texture + stream->writeU32(tex->filterAddressing); + stream->write8(tex->name, 32); + stream->write8(tex->mask, 32); + + // Raster + int32 numLevels = natras->numLevels; + stream->writeI32(raster->format); + stream->writeI32(raster->width); + stream->writeI32(raster->height); + stream->writeI32(raster->depth); + stream->writeI32(numLevels); + + // Native raster + int32 flags = 0; + int32 compression = 0; + if(natras->hasAlpha) + flags |= 1; + if(natras->isCompressed){ + flags |= 2; + switch(natras->internalFormat){ +#ifdef RW_OPENGL + case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT: + case GL_COMPRESSED_RGB_S3TC_DXT1_EXT: + compression = 1; + break; + case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT: + compression = 3; + break; + case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT: + compression = 5; + break; +#endif + default: + assert(0 && "unknown compression"); + } + } + stream->writeI32(1); + stream->writeI32(flags); + stream->writeI32(compression); + // TODO: auto mipmaps? + + uint32 size; + uint8 *data; + for(int32 i = 0; i < numLevels; i++){ + size = getLevelSize(raster, i); + stream->writeU32(size); + data = raster->lock(i, Raster::LOCKREAD); + stream->write8(data, size); + raster->unlock(i); + } +} + +uint32 +getSizeNativeTexture(Texture *tex) +{ + uint32 size = 12 + 72 + 32; + int32 levels = tex->raster->getNumLevels(); + for(int32 i = 0; i < levels; i++) + size += 4 + getLevelSize(tex->raster, i); + return size; +} + + + +void registerNativeRaster(void) +{ + nativeRasterOffset = Raster::registerPlugin(sizeof(Gl1Raster), + ID_RASTERGLES1, + createNativeRaster, + destroyNativeRaster, + copyNativeRaster); +} + +} +} diff --git a/vendor/librw/src/gles1/gl1render.cpp b/vendor/librw/src/gles1/gl1render.cpp new file mode 100644 index 0000000..e621257 --- /dev/null +++ b/vendor/librw/src/gles1/gl1render.cpp @@ -0,0 +1,143 @@ +#include +#include +#include + +#include "../rwbase.h" +#include "../rwerror.h" +#include "../rwplg.h" +#include "../rwrender.h" +#include "../rwengine.h" +#include "../rwpipeline.h" +#include "../rwobjects.h" +#ifdef RW_GLES1 +#include "rwgles1.h" +#include "rwgles1impl.h" + +namespace rw { +namespace gles1 { + +#define MAX_LIGHTS + +void +drawInst_simple(InstanceDataHeader *header, InstanceData *inst) +{ + flushCache(); + glDrawElements(header->primType, inst->numIndex, + GL_UNSIGNED_SHORT, (void*)(uintptr)inst->offset); +} + +// Emulate PS2 GS alpha test FB_ONLY case: failed alpha writes to frame- but not to depth buffer +void +drawInst_GSemu(InstanceDataHeader *header, InstanceData *inst) +{ + uint32 hasAlpha; + int alphafunc, alpharef, gsalpharef; + int zwrite; + hasAlpha = getAlphaBlend(); + if(hasAlpha){ + zwrite = rw::GetRenderState(rw::ZWRITEENABLE); + alphafunc = rw::GetRenderState(rw::ALPHATESTFUNC); + if(zwrite){ + alpharef = rw::GetRenderState(rw::ALPHATESTREF); + gsalpharef = rw::GetRenderState(rw::GSALPHATESTREF); + + SetRenderState(rw::ALPHATESTFUNC, rw::ALPHAGREATEREQUAL); + SetRenderState(rw::ALPHATESTREF, gsalpharef); + drawInst_simple(header, inst); + SetRenderState(rw::ALPHATESTFUNC, rw::ALPHALESS); + SetRenderState(rw::ZWRITEENABLE, 0); + drawInst_simple(header, inst); + SetRenderState(rw::ZWRITEENABLE, 1); + SetRenderState(rw::ALPHATESTFUNC, alphafunc); + SetRenderState(rw::ALPHATESTREF, alpharef); + }else{ + SetRenderState(rw::ALPHATESTFUNC, rw::ALPHAALWAYS); + drawInst_simple(header, inst); + SetRenderState(rw::ALPHATESTFUNC, alphafunc); + } + }else + drawInst_simple(header, inst); +} + +void +drawInst(InstanceDataHeader *header, InstanceData *inst) +{ +// if(rw::GetRenderState(rw::GSALPHATEST)) +// drawInst_GSemu(header, inst); +// else + drawInst_simple(header, inst); +} + + +void +setupVertexInput(InstanceDataHeader *header) +{ + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, header->ibo); + glBindBuffer(GL_ARRAY_BUFFER, header->vbo); +} + +void +teardownVertexInput(InstanceDataHeader *header) +{ +} + +int32 +lightingCB(Atomic *atomic) +{ + WorldLights lightData; + Light *directionals[8]; + Light *locals[8]; + lightData.directionals = directionals; + lightData.numDirectionals = 0; // 8; + lightData.locals = locals; + lightData.numLocals = 0; // 8; + + if(atomic->geometry->flags & rw::Geometry::LIGHT){ + ((World*)engine->currentWorld)->enumerateLights(atomic, &lightData); + if((atomic->geometry->flags & rw::Geometry::NORMALS) == 0){ + // Get rid of lights that need normals when we don't have any + lightData.numDirectionals = 0; + lightData.numLocals = 0; + } + return setLights(&lightData); + }else{ + memset(&lightData, 0, sizeof(lightData)); + return setLights(&lightData); + } +} + +void +defaultRenderCB(Atomic *atomic, InstanceDataHeader *header) +{ + Material *m; + + uint32 flags = atomic->geometry->flags; + setWorldMatrix(atomic->getFrame()->getLTM()); + int32 vsBits = lightingCB(atomic); + + setupVertexInput(header); + + InstanceData *inst = header->inst; + int32 n = header->numMeshes; + + while(n--){ + m = inst->material; + + setMaterial(flags, m->color, m->surfaceProps); + + setTexture(0, m->texture); + + rw::SetRenderState(VERTEXALPHA, inst->vertexAlpha || m->color.alpha != 0xFF); + + drawInst(header, inst); + inst++; + } + teardownVertexInput(header); +} + + +} +} + +#endif + diff --git a/vendor/librw/src/gles1/rwgles1.cpp b/vendor/librw/src/gles1/rwgles1.cpp deleted file mode 100644 index 262701f..0000000 --- a/vendor/librw/src/gles1/rwgles1.cpp +++ /dev/null @@ -1,58 +0,0 @@ -#include "rwgles1.h" - -#ifdef RW_GLES1 - -namespace rw { - -namespace gles1 { - -// --- Funciones dummy (para linkear después) --- - -inline void im2DRenderPrimitive(int, void*, int) {} -inline void im2DRenderIndexedPrimitive(int, void*, int, void*, int) {} -inline void im3DTransform(void*, int, void*, unsigned int) {} -inline void im3DRenderPrimitive(int) {} -inline void im3DRenderIndexedPrimitive(int, void*, int) {} -inline void im3DEnd(void) {} - -// --- Driver lifecycle --- -static void* -driverOpen(void* object, int32 offset, int32 size) -{ - // stub: no hace nada - return object; -} - -static void* -driverClose(void* object, int32 offset, int32 size) -{ - // stub: no hace nada - return object; -} - -// --- Raster --- -static void -registerNativeRaster(void) -{ - // stub por ahora -} - -void -registerPlatformPlugins(void) -{ - Driver::registerPlugin(PLATFORM_GLES1, 0, PLATFORM_GLES1, - driverOpen, driverClose); - registerNativeRaster(); -} - -void* -destroyNativeData(void *object, int32, int32) -{ - //freeInstanceData((Geometry*)object); - return object; -} - -} -} - -#endif diff --git a/vendor/librw/src/gles1/rwgles1.h b/vendor/librw/src/gles1/rwgles1.h index 3d9b354..14b8743 100644 --- a/vendor/librw/src/gles1/rwgles1.h +++ b/vendor/librw/src/gles1/rwgles1.h @@ -1,28 +1,7 @@ -#pragma once - -#include -#include -#include -#include - -#include "../rwbase.h" -#include "../rwerror.h" -#include "../rwplg.h" -#include "../rwpipeline.h" -#include "../rwobjects.h" -#include "../rwengine.h" - #ifdef RW_GLES1 -#include -#include -#ifdef __SYMBIAN32__ - -#define GL_COMPRESSED_RGB_S3TC_DXT1_EXT 0x83F0 -#define GL_COMPRESSED_RGBA_S3TC_DXT1_EXT 0x83F1 -#define GL_COMPRESSED_RGBA_S3TC_DXT3_EXT 0x83F2 -#define GL_COMPRESSED_RGBA_S3TC_DXT5_EXT 0x83F3 - -#endif +#include +#include +#include #endif namespace rw { @@ -30,79 +9,245 @@ namespace rw { #ifdef RW_GLES1 struct EngineOpenParams { - int width, height; + int width, height; const char *windowtitle; }; #endif namespace gles1 { -// --- Tipos básicos que charset.cpp y otros esperan --- - -struct Im2DVertex { - float x, y, z, w; - float camZ; - float recipCamZ; - unsigned char r, g, b, a; - float u, v; - - // --- setters esperados por librw --- - - void setScreenX(float v) { x = v; } - void setScreenY(float v) { y = v; } - void setScreenZ(float v) { z = v; } - - void setCameraZ(float v) { camZ = v; } - void setRecipCameraZ(float v) { recipCamZ = v; } - - void setColor(unsigned char _r, unsigned char _g, unsigned char _b, unsigned char _a) { - r = _r; g = _g; b = _b; a = _a; - } - - void setU(float val, float recipZ) { u = val; } - void setV(float val, float recipZ) { v = val; } -}; - -struct Im3DVertex { - float x, y, z; - float u, v; - uint32 color; - - void setX(float v) { x = v; } - void setY(float v) { y = v; } - void setZ(float v) { z = v; } - - void setU(float v) { u = v; } - void setV(float v) { this->v = v; } - - void setColor(uint8 r, uint8 g, uint8 b, uint8 a) { - color = (a<<24) | (r<<16) | (g<<8) | b; - } -}; - -// --- Funciones dummy (para linkear después) --- - -inline void im2DRenderPrimitive(int, void*, int); -inline void im2DRenderIndexedPrimitive(int, void*, int, void*, int); -inline void im3DTransform(void*, int, void*, unsigned int); -inline void im3DRenderPrimitive(int); -inline void im3DRenderIndexedPrimitive(int, void*, int); -inline void im3DEnd(void); - -// --- Driver lifecycle --- -static void* -driverOpen(void* object, int32 offset, int32 size); - -static void* -driverClose(void* object, int32 offset, int32 size); - -// --- Raster --- -static void -registerNativeRaster(void); - void registerPlatformPlugins(void); +extern Device renderdevice; + +// arguments to glVertexAttribPointer basically +struct AttribDesc +{ + uint32 index; + int32 type; + bool32 normalized; + int32 size; + uint32 stride; + uint32 offset; +}; + +enum AttribIndices +{ + ATTRIB_POS = 0, + ATTRIB_NORMAL, + ATTRIB_COLOR, + ATTRIB_WEIGHTS, + ATTRIB_INDICES, + ATTRIB_TEXCOORDS0, + ATTRIB_TEXCOORDS1, +#if 0 + ATTRIB_TEXCOORDS2, + ATTRIB_TEXCOORDS3, + ATTRIB_TEXCOORDS4, + ATTRIB_TEXCOORDS5, + ATTRIB_TEXCOORDS6, + ATTRIB_TEXCOORDS7, +#endif +}; + +// default uniform indices +extern int32 u_matColor; +extern int32 u_surfProps; + +struct InstanceData +{ + uint32 numIndex; + uint32 minVert; // not used for rendering + int32 numVertices; // + Material *material; + bool32 vertexAlpha; + uint32 program; + uint32 offset; +}; + +struct InstanceDataHeader : rw::InstanceDataHeader +{ + uint32 serialNumber; + uint32 numMeshes; + uint16 *indexBuffer; + uint32 primType; + uint8 *vertexBuffer; + int32 numAttribs; + AttribDesc *attribDesc; + uint32 totalNumIndex; + uint32 totalNumVertex; + + uint32 ibo; + uint32 vbo; // or 2? + + InstanceData *inst; +}; + +#ifdef RW_GLES1 + +struct Im3DVertex +{ + V3d position; + uint8 r, g, b, a; + float32 u, v; + + void setX(float32 x) { this->position.x = x; } + void setY(float32 y) { this->position.y = y; } + void setZ(float32 z) { this->position.z = z; } + void setColor(uint8 r, uint8 g, uint8 b, uint8 a) { + this->r = r; this->g = g; this->b = b; this->a = a; } + void setU(float32 u) { this->u = u; } + void setV(float32 v) { this->v = v; } + + float getX(void) { return this->position.x; } + float getY(void) { return this->position.y; } + float getZ(void) { return this->position.z; } + RGBA getColor(void) { return makeRGBA(this->r, this->g, this->b, this->a); } + float getU(void) { return this->u; } + float getV(void) { return this->v; } +}; + +struct Im2DVertex +{ + float32 x, y, z, w; + uint8 r, g, b, a; + float32 u, v; + + void setScreenX(float32 x) { this->x = x; } + void setScreenY(float32 y) { this->y = y; } + void setScreenZ(float32 z) { this->z = z; } + // This is a bit unefficient but we have to counteract GL's divide, so multiply + void setCameraZ(float32 z) { this->w = z; } + void setRecipCameraZ(float32 recipz) { this->w = 1.0f/recipz; } + void setColor(uint8 r, uint8 g, uint8 b, uint8 a) { + this->r = r; this->g = g; this->b = b; this->a = a; } + void setU(float32 u, float recipz) { this->u = u; } + void setV(float32 v, float recipz) { this->v = v; } + + float getScreenX(void) { return this->x; } + float getScreenY(void) { return this->y; } + float getScreenZ(void) { return this->z; } + float getCameraZ(void) { return this->w; } + float getRecipCameraZ(void) { return 1.0f/this->w; } + RGBA getColor(void) { return makeRGBA(this->r, this->g, this->b, this->a); } + float getU(void) { return this->u; } + float getV(void) { return this->v; } +}; + +void setupVertexInput(InstanceDataHeader *header); +void teardownVertexInput(InstanceDataHeader *header); + +// Render state + +// Vertex shader bits +enum +{ + // These should be low so they could be used as indices + VSLIGHT_DIRECT = 1, + VSLIGHT_POINT = 2, + VSLIGHT_SPOT = 4, + VSLIGHT_MASK = 7, // all the above + // less critical + VSLIGHT_AMBIENT = 8, +}; +// per Scene +void setProjectionMatrix(float32*); +void setViewMatrix(float32*); + +// per Object +void setWorldMatrix(Matrix*); +int32 setLights(WorldLights *lightData); + +// per Mesh +void setTexture(int32 n, Texture *tex); +void setMaterial(const RGBA &color, const SurfaceProperties &surfaceprops, float extraSurfProp = 0.0f); +inline void setMaterial(uint32 flags, const RGBA &color, const SurfaceProperties &surfaceprops, float extraSurfProp = 0.0f) +{ + static RGBA white = { 255, 255, 255, 255 }; + if(flags & Geometry::MODULATE) + setMaterial(color, surfaceprops, extraSurfProp); + else + setMaterial(white, surfaceprops, extraSurfProp); +} + +void setAlphaBlend(bool32 enable); +bool32 getAlphaBlend(void); + +bool32 getAlphaTest(void); + +void bindFramebuffer(uint32 fbo); +uint32 bindTexture(uint32 texid); + +void flushCache(void); + +#endif + +class ObjPipeline : public rw::ObjPipeline +{ +public: + void init(void); + static ObjPipeline *create(void); + + void (*instanceCB)(Geometry *geo, InstanceDataHeader *header, bool32 reinstance); + void (*uninstanceCB)(Geometry *geo, InstanceDataHeader *header); + void (*renderCB)(Atomic *atomic, InstanceDataHeader *header); +}; + +void defaultInstanceCB(Geometry *geo, InstanceDataHeader *header, bool32 reinstance); +void defaultUninstanceCB(Geometry *geo, InstanceDataHeader *header); +void defaultRenderCB(Atomic *atomic, InstanceDataHeader *header); +int32 lightingCB(Atomic *atomic); + +void drawInst_simple(InstanceDataHeader *header, InstanceData *inst); +// Emulate PS2 GS alpha test FB_ONLY case: failed alpha writes to frame- but not to depth buffer +void drawInst_GSemu(InstanceDataHeader *header, InstanceData *inst); +// This one switches between the above two depending on render state; +void drawInst(InstanceDataHeader *header, InstanceData *inst); + + void *destroyNativeData(void *object, int32, int32); +ObjPipeline *makeDefaultPipeline(void); + +// Native Texture and Raster + +struct Gl1Raster +{ + // arguments to glTexImage2D + int32 internalFormat; + int32 type; + int32 format; + int32 bpp; // bytes per pixel + // texture object + uint32 texid; + + bool isCompressed; + bool hasAlpha; + bool autogenMipmap; + int8 numLevels; + // cached filtermode and addressing + uint8 filterMode; + uint8 addressU; + uint8 addressV; + int32 maxAnisotropy; + + uint32 fbo; // used for camera texture only! + Raster *fboMate; // color or zbuffer raster mate of this one + RasterLevels *backingStore; // if we can't read back GPU memory but have to +}; + +// GLES can't read back textures very nicely. +// In most cases that's not an issue, but when it is, +// this has to be set before the texture is filled: +extern bool32 needToReadBackTextures; + +Texture *readNativeTexture(Stream *stream); +void writeNativeTexture(Texture *tex, Stream *stream); +uint32 getSizeNativeTexture(Texture *tex); + +extern int32 nativeRasterOffset; +void registerNativeRaster(void); +#define GETGL1RASTEREXT(raster) PLUGINOFFSET(Gl1Raster, raster, rw::gles1::nativeRasterOffset) + } } diff --git a/vendor/librw/src/gles1/rwgles1impl.h b/vendor/librw/src/gles1/rwgles1impl.h new file mode 100644 index 0000000..4164119 --- /dev/null +++ b/vendor/librw/src/gles1/rwgles1impl.h @@ -0,0 +1,83 @@ +namespace rw { +namespace gles1 { + +#ifdef RW_GLES1 + +extern uint32 im2DVbo, im2DIbo; +void openIm2D(void); +void closeIm2D(void); +void im2DRenderLine(void *vertices, int32 numVertices, + int32 vert1, int32 vert2); +void im2DRenderTriangle(void *vertices, int32 numVertices, + int32 vert1, int32 vert2, int32 vert3); +void im2DRenderPrimitive(PrimitiveType primType, + void *vertices, int32 numVertices); +void im2DRenderIndexedPrimitive(PrimitiveType primType, + void *vertices, int32 numVertices, void *indices, int32 numIndices); + +void openIm3D(void); +void closeIm3D(void); +void im3DTransform(void *vertices, int32 numVertices, Matrix *world, uint32 flags); +void im3DRenderPrimitive(PrimitiveType primType); +void im3DRenderIndexedPrimitive(PrimitiveType primType, void *indices, int32 numIndices); +void im3DEnd(void); + +struct DisplayMode +{ +#ifdef __SYMBIAN32__ + uint32 width; + uint32 height; +#else +#ifdef LIBRW_SDL2 + SDL_DisplayMode mode; +#else + GLFWvidmode mode; +#endif +#endif + int32 depth; + uint32 flags; +}; + +struct GlGlobals +{ +#ifdef LIBRW_SDL2 + SDL_Window **pWindow; + SDL_Window *window; + SDL_GLContext glcontext; +#else +#ifndef __SYMBIAN32__ + GLFWwindow **pWindow; + GLFWwindow *window; + + GLFWmonitor *monitor; +#endif + int numMonitors; + int currentMonitor; +#endif + + DisplayMode *modes; + int numModes; + int currentMode; + int presentWidth, presentHeight; + int presentOffX, presentOffY; + + // for opening the window + int winWidth, winHeight; + const char *winTitle; + uint32 numSamples; +}; + +extern GlGlobals glGlobals; +#endif + +Raster *rasterCreate(Raster *raster); +uint8 *rasterLock(Raster*, int32 level, int32 lockMode); +void rasterUnlock(Raster*, int32); +int32 rasterNumLevels(Raster*); +bool32 imageFindRasterFormat(Image *img, int32 type, + int32 *width, int32 *height, int32 *depth, int32 *format); +bool32 rasterFromImage(Raster *raster, Image *image); +Image *rasterToImage(Raster *raster); + +} +} diff --git a/vendor/librw/src/rwbase.h b/vendor/librw/src/rwbase.h index 01c1e61..3011b65 100644 --- a/vendor/librw/src/rwbase.h +++ b/vendor/librw/src/rwbase.h @@ -626,6 +626,7 @@ enum PluginID ID_RASTERD3D9 = MAKEPLUGINID(VEND_RASTER, PLATFORM_D3D9), ID_RASTERWDGL = MAKEPLUGINID(VEND_RASTER, PLATFORM_WDGL), ID_RASTERGL3 = MAKEPLUGINID(VEND_RASTER, PLATFORM_GL3), + ID_RASTERGLES1 = MAKEPLUGINID(VEND_RASTER, PLATFORM_GLES1), // anything driver/device related (only as allocation tag) ID_DRIVER = MAKEPLUGINID(VEND_DRIVER, 0) diff --git a/vendor/librw/src/texture.cpp b/vendor/librw/src/texture.cpp index 4692e72..219aa2a 100644 --- a/vendor/librw/src/texture.cpp +++ b/vendor/librw/src/texture.cpp @@ -17,6 +17,7 @@ #include "d3d/rwd3d9.h" #include "d3d/rwd3dimpl.h" #include "gl/rwgl3.h" +#include "gles1/rwgles1.h" #define PLUGIN_ID 0 @@ -482,8 +483,8 @@ Texture::streamReadNative(Stream *stream) if(platform == PLATFORM_GL3) return gl3::readNativeTexture(stream); #ifdef RW_GLES1 -// if(platform == PLATFORM_GLES1) // TODO -// return gles1::readNativeTexture(stream); + if(platform == PLATFORM_GLES1) + return gles1::readNativeTexture(stream); #endif assert(0 && "unsupported platform"); return nil; @@ -503,8 +504,8 @@ Texture::streamWriteNative(Stream *stream) else if(this->raster->platform == PLATFORM_GL3) gl3::writeNativeTexture(this, stream); #ifdef RW_GLES1 -// else if(this->raster->platform == PLATFORM_GLES1) -// gles1::writeNativeTexture(this, stream); + else if(this->raster->platform == PLATFORM_GLES1) + gles1::writeNativeTexture(this, stream); #endif else assert(0 && "unsupported platform"); @@ -524,8 +525,8 @@ Texture::streamGetSizeNative(void) if(this->raster->platform == PLATFORM_GL3) return gl3::getSizeNativeTexture(this); #ifdef RW_GLES1 -// if(this->raster->platform == PLATFORM_GLES1) -// return gles1::getSizeNativeTexture(this); + if(this->raster->platform == PLATFORM_GLES1) + return gles1::getSizeNativeTexture(this); #endif assert(0 && "unsupported platform"); return 0;