Commit b6740a7b authored by Richard Gobeille's avatar Richard Gobeille
Browse files

engine: improve GL state management, add support for GL sampler objects, add...

engine: improve GL state management, add support for GL sampler objects, add libtess2 and implement in polymer in place of the GLU tesselator, drop dependency on libGLU, improve GL debug output (in debug builds), fix various GL resource leaks

This should prevent a few more state changes, and address issues arising from rendermode changes wiping our state but not any of our accounting data for said state.

Because libtess2 seems to be abandoned and because there were a few issues I needed to fix anyway, I went ahead and made a custom single-file header-only version for improved performance.
parent 661883a5
......@@ -310,10 +310,10 @@ else
engine_objs += a-c.cpp
ifeq (1,$(USE_OPENGL))
engine_objs += glsurface.cpp voxmodel.cpp mdsprite.cpp tilepacker.cpp
engine_objs += glbuild.cpp glsurface.cpp voxmodel.cpp mdsprite.cpp tilepacker.cpp
engine_deps += glad
ifeq (1,$(POLYMER))
engine_objs += glbuild.cpp polymer.cpp
engine_objs += polymer.cpp
......@@ -458,6 +458,7 @@
<ClInclude Include="..\..\source\build\include\libasync_config.h" />
<ClInclude Include="..\..\source\build\include\libdivide.h" />
<ClInclude Include="..\..\source\build\include\libdivide_config.h" />
<ClInclude Include="..\..\source\build\include\libtess2.h" />
<ClInclude Include="..\..\source\build\include\linklist.h" />
<ClInclude Include="..\..\source\build\include\lru.h" />
<ClInclude Include="..\..\source\build\include\lz4.h" />
......@@ -552,4 +553,4 @@ echo )shader^"; &gt;&gt; $(IntermediateOutputPath)%(Filename)%(Extension).cpp</C
<ImportGroup Label="ExtensionTargets">
<Import Project="$(VCTargetsPath)\BuildCustomizations\masm.targets" />
\ No newline at end of file
......@@ -493,6 +493,9 @@
<ClInclude Include="..\..\source\build\include\minicoro.h">
<Filter>Header Files</Filter>
<ClInclude Include="..\..\source\build\include\libtess2.h">
<Filter>Header Files</Filter>
<CustomBuild Include="..\..\source\build\src\polymost1Frag.glsl">
......@@ -133,6 +133,7 @@ struct glinfo_t {
int multitex : 1;
int occlusionqueries : 1;
int rect : 1;
int reset_notification : 1;
int samplerobjects : 1;
int shadow : 1;
int sync : 1;
......@@ -1550,8 +1550,6 @@ int32_t polymost_drawtilescreen(int32_t tilex, int32_t tiley, int32_t wallnum, i
void polymost_glreset(void);
void polymost_precache(int32_t dapicnum, int32_t dapalnum, int32_t datype);
typedef uint16_t polytintflags_t;
enum cutsceneflags {
......@@ -1596,14 +1594,6 @@ extern int32_t mdtims, omdtims;
extern int32_t glrendmode;
void hicinit(void);
void hicsetpalettetint(int32_t palnum, char r, char g, char b, char sr, char sg, char sb, polytintflags_t effect);
// flags bitset: 1 = don't compress
int32_t hicsetsubsttex(int32_t picnum, int32_t palnum, const char *filen, float alphacut,
float xscale, float yscale, float specpower, float specfactor, char flags);
int32_t hicsetskybox(int32_t picnum, int32_t palnum, char *faces[6], int32_t flags);
int32_t hicclearsubst(int32_t picnum, int32_t palnum);
int32_t Ptile2tile(int32_t tile, int32_t palette) ATTRIBUTE((pure));
int32_t md_loadmodel(const char *fn);
int32_t md_setmisc(int32_t modelid, float scale, int32_t shadeoff, float zadd, float yoffset, int32_t flags);
......@@ -2,20 +2,20 @@
#include "baselayer.h"
#include "build.h"
#include "compat.h"
#include "glad/glad.h"
#include "hash.h"
#if !defined GEKKO && !defined EDUKE32_GLES
# define DYNAMIC_GL
# define DYNAMIC_GLU
# define USE_GLEXT
#if defined EDUKE32_OSX
# include <OpenGL/glu.h>
# include <GL/glu.h>
#if defined EDUKE32_GLES
# include "jwzgles.h"
......@@ -25,11 +25,86 @@
# else
# define PR_CALLBACK
# endif
// custom error checking
extern GLenum BuildGLError;
extern void BuildGLErrorCheck(void);
enum glsamplertype
enum glsamplerflags {
struct BuildGLState
GLuint currentShaderProgramID;
GLenum currentActiveTexture;
glsamplertype currentBoundSampler[MAXTEXUNIT - GL_TEXTURE0];
GLint x, y;
GLsizei width, height;
inthashtable_t state[MAXTEXUNIT - GL_TEXTURE0];
int fullReset;
extern BuildGLState gl;
extern GLuint samplerObjectIDs[NUM_SAMPLERS];
#define ACTIVETEX (gl.currentActiveTexture ? TEXUNIT_INDEX_FROM_NAME(gl.currentActiveTexture) : 0)
extern void buildgl_activeTexture(GLenum texture);
extern void buildgl_bindBuffer(GLenum target, uint32_t bufferID);
extern void buildgl_bindSamplerObject(int texunit, int32_t pth_method);
extern void buildgl_bindTexture(GLenum target, uint32_t textureID);
extern void buildgl_outputDebugMessage(uint8_t severity, const char *format, ...);
extern void buildgl_resetSamplerObjects(void);
extern void buildgl_resetStateAccounting(void);
extern void buildgl_setAlphaFunc(GLenum func, GLfloat ref);
extern void buildgl_setDepthFunc(GLenum func);
extern void buildgl_setDisabled(GLenum key);
extern void buildgl_setEnabled(GLenum key);
extern void buildgl_setPerspective(float fovy, float aspect, float zNear, float zFar);
extern void buildgl_setViewport(GLint x, GLint y, GLsizei width, GLsizei height);
extern void buildgl_useShaderProgram(uint32_t shaderID);
static FORCE_INLINE void buildgl_crossproduct(const GLfloat* in_a, const GLfloat* in_b, GLfloat* out)
out[0] = in_a[1] * in_b[2] - in_a[2] * in_b[1];
out[1] = in_a[2] * in_b[0] - in_a[0] * in_b[2];
out[2] = in_a[0] * in_b[1] - in_a[1] * in_b[0];
static FORCE_INLINE void buildgl_normalize(float* vec)
float norm = vec[0] * vec[0] + vec[1] * vec[1] + vec[2] * vec[2];
norm = 1.f/Bsqrtf(norm);
vec[0] *= norm;
vec[1] *= norm;
vec[2] *= norm;
extern int buildgl_unprojectMatrixToViewport(vec3f_t win, const float *modelMatrix, const float *projMatrix, const int *viewport, float *objx, float *objy,
float *objz);
extern int32_t r_usesamplerobjects; // FIXME: nasty circular include dependency issue
static FORCE_INLINE bool buildgl_samplerObjectsEnabled(void) { return glinfo.samplerobjects && r_usesamplerobjects; }
//////// dynamic/static API wrapping ////////
......@@ -61,74 +136,6 @@ extern bwglSetPixelFormatProcPtr bwglSetPixelFormat;
#define wglSetPixelFormat bwglSetPixelFormat
#if defined DYNAMIC_GLU
// GLU
#if defined __clang__ && defined __APPLE__
// XXX: OS X 10.9 deprecated GLUtesselator.
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
typedef void (APIENTRY * bgluTessBeginContourProcPtr)(GLUtesselator* tess);
extern bgluTessBeginContourProcPtr bgluTessBeginContour;
typedef void (APIENTRY * bgluTessBeginPolygonProcPtr)(GLUtesselator* tess, GLvoid* data);
extern bgluTessBeginPolygonProcPtr bgluTessBeginPolygon;
typedef void (APIENTRY * bgluTessCallbackProcPtr)(GLUtesselator* tess, GLenum which, void (PR_CALLBACK CallBackFuncProcPtr)());
extern bgluTessCallbackProcPtr bgluTessCallback;
typedef void (APIENTRY * bgluTessEndContourProcPtr)(GLUtesselator* tess);
extern bgluTessEndContourProcPtr bgluTessEndContour;
typedef void (APIENTRY * bgluTessEndPolygonProcPtr)(GLUtesselator* tess);
extern bgluTessEndPolygonProcPtr bgluTessEndPolygon;
typedef void (APIENTRY * bgluTessNormalProcPtr)(GLUtesselator* tess, GLdouble valueX, GLdouble valueY, GLdouble valueZ);
extern bgluTessNormalProcPtr bgluTessNormal;
typedef void (APIENTRY * bgluTessPropertyProcPtr)(GLUtesselator* tess, GLenum which, GLdouble data);
extern bgluTessPropertyProcPtr bgluTessProperty;
typedef void (APIENTRY * bgluTessVertexProcPtr)(GLUtesselator* tess, GLdouble *location, GLvoid* data);
extern bgluTessVertexProcPtr bgluTessVertex;
typedef GLUtesselator* (APIENTRY * bgluNewTessProcPtr)(void);
extern bgluNewTessProcPtr bgluNewTess;
typedef void (APIENTRY * bgluDeleteTessProcPtr)(GLUtesselator* tess);
extern bgluDeleteTessProcPtr bgluDeleteTess;
#if defined __clang__ && defined __APPLE__
#pragma clang diagnostic pop
typedef void (APIENTRY * bgluPerspectiveProcPtr)(GLdouble fovy, GLdouble aspect, GLdouble zNear, GLdouble zFar);
extern bgluPerspectiveProcPtr bgluPerspective;
typedef const GLubyte * (APIENTRY * bgluErrorStringProcPtr)(GLenum error);
extern bgluErrorStringProcPtr bgluErrorString;
typedef GLint (APIENTRY * bgluProjectProcPtr)(GLdouble objX, GLdouble objY, GLdouble objZ, const GLdouble *model, const GLdouble *proj, const GLint *view, GLdouble* winX, GLdouble* winY, GLdouble* winZ);
extern bgluProjectProcPtr bgluProject;
typedef GLint (APIENTRY * bgluUnProjectProcPtr)(GLdouble winX, GLdouble winY, GLdouble winZ, const GLdouble * model, const GLdouble * proj, const GLint * view, GLdouble* objX, GLdouble* objY, GLdouble* objZ);
extern bgluUnProjectProcPtr bgluUnProject;
#define bgluTessBeginContour gluTessBeginContour
#define bgluTessBeginPolygon gluTessBeginPolygon
#define bgluTessCallback gluTessCallback
#define bgluTessEndContour gluTessEndContour
#define bgluTessEndPolygon gluTessEndPolygon
#define bgluTessNormal gluTessNormal
#define bgluTessProperty gluTessProperty
#define bgluTessVertex gluTessVertex
#define bgluNewTess gluNewTess
#define bgluDeleteTess gluDeleteTess
#define bgluPerspective gluPerspective
#define bgluErrorString gluErrorString
#define bgluProject gluProject
#define bgluUnProject gluUnProject
//////// glGenTextures/glDeleteTextures debugging ////////
void texdbg_bglGenTextures(GLsizei n, GLuint *textures, const char *srcfn);
void texdbg_bglDeleteTextures(GLsizei n, const GLuint *textures, const char *srcfn);
......@@ -139,7 +146,6 @@ void texdbg_bglDeleteTextures(GLsizei n, const GLuint *textures, const char *src
# define glGenTextures(numtexs, texnamear) texdbg_bglGenTextures(numtexs, texnamear, __FILE__)
# define glDeleteTextures(numtexs, texnamear) texdbg_bglDeleteTextures(numtexs, texnamear, __FILE__)
#endif //USE_OPENGL
#if !defined RENDERTYPESDL && defined _WIN32 && defined DYNAMIC_GL
......@@ -149,9 +155,4 @@ int32_t loadwgl(const char *driver);
int32_t unloadwgl(void);
#ifdef POLYMER
int32_t loadglulibrary(const char *driver);
int32_t unloadglulibrary(void);
#include "build.h"
#include "palette.h"
#include "vec.h"
#ifdef __cplusplus
extern "C" {
......@@ -22,6 +26,8 @@ typedef struct hicreplc_t {
char palnum, flags;
} hicreplctyp;
typedef uint16_t polytintflags_t;
typedef struct {
polytintflags_t f;
uint8_t r, g, b;
......@@ -52,6 +58,14 @@ hicreplctyp * hicfindsubst(int picnum, int palnum, int nozero = 0);
hicreplctyp * hicfindskybox(int picnum, int palnum, int nozero = 0);
void hictinting_applypixcolor(coltype* tcol, uint8_t pal, bool no_rb_swap);
void hicinit(void);
void hicsetpalettetint(int32_t palnum, char r, char g, char b, char sr, char sg, char sb, polytintflags_t effect);
// flags bitset: 1 = don't compress
int32_t hicsetsubsttex(int32_t picnum, int32_t palnum, const char *filen, float alphacut,
float xscale, float yscale, float specpower, float specfactor, char flags);
int32_t hicsetskybox(int32_t picnum, int32_t palnum, char *faces[6], int32_t flags);
int32_t hicclearsubst(int32_t picnum, int32_t palnum);
static inline int have_basepal_tint(void)
polytint_t const & tint = hictinting[MAXPALOOKUPS-1];
This diff is collapsed.
#ifndef mdsprite_h_
# define mdsprite_h_
#ifdef __cplusplus
extern "C" {
#include "hightile.h"
#ifdef __cplusplus
extern "C" {
#if defined(_M_IX86) || defined(_M_AMD64) || defined(__i386) || defined(__x86_64)
#define SHIFTMOD32(a) (a)
......@@ -319,6 +319,7 @@ static FORCE_INLINE void OSD_FlushLog(void)
static FORCE_INLINE char const *OSD_GetErrorFmt(void) { return osd->draw.errorfmt; }
// registers a function
// name = name of the function
// help = a short help string
This diff is collapsed.
......@@ -29,7 +29,6 @@ extern struct glfiltermodes glfiltermodes[NUMGLFILTERMODES];
extern void Polymost_prepare_loadboard(void);
void polymost_outputGLDebugMessage(uint8_t severity, const char* format, ...);
//void phex(char v, char *s);
void uploadtexture(int32_t doalloc, vec2_t siz, int32_t texfmt, coltype *pic, vec2_t tsiz, int32_t dameth);
......@@ -50,23 +49,20 @@ int32_t polymost_maskWallHasTranslucency(uwalltype const * const wall);
int32_t polymost_spriteHasTranslucency(tspritetype const * const tspr);
int32_t polymost_spriteIsModelOrVoxel(tspritetype const * const tspr);
void polymost_resetVertexPointers(void);
void polymost_disableProgram(void);
char polymost_getClamp(void);
void polymost_resetProgram(void);
void polymost_setTexturePosSize(vec4f_t const &texturePosSize);
void polymost_setHalfTexelSize(vec2f_t const &halfTexelSize);
char polymost_getClamp();
void polymost_resetVertexPointers(void);
void polymost_setClamp(char clamp);
void polymost_setVisibility(float visibility);
void polymost_setFogEnabled(char fogEnabled);
void polymost_setHalfTexelSize(vec2f_t const &halfTexelSize);
void polymost_setTexturePosSize(vec4f_t const &texturePosSize);
void polymost_setVisibility(float visibility);
void polymost_updatePalette(void);
void polymost_useColorOnly(char useColorOnly);
void polymost_usePaletteIndexing(char usePaletteIndexing);
void polymost_useDetailMapping(char useDetailMapping);
void polymost_useGlowMapping(char useGlowMapping);
void polymost_activeTexture(GLenum texture);
void polymost_bindTexture(GLenum target, uint32_t textureID);
void polymost_updatePalette(void);
void polymost_useShaderProgram(uint32_t shaderID);
void polymost_usePaletteIndexing(char usePaletteIndexing);
float* multiplyMatrix4f(float m0[4*4], const float m1[4*4]);
......@@ -99,6 +95,8 @@ extern int32_t polymostcenterhoriz;
extern int16_t globalpicnum;
extern int32_t r_polymostDebug;
// Compare with polymer_eligible_for_artmap()
static FORCE_INLINE int32_t eligible_for_tileshades(int32_t const picnum, int32_t const pal)
......@@ -243,6 +241,10 @@ enum pthtyp_flags {
// temporary until I create separate flags for samplers
typedef struct pthtyp_t
......@@ -281,6 +283,7 @@ extern int32_t drawingskybox;
extern int32_t hicprecaching;
extern float fcosglobalang, fsinglobalang;
extern float fxdim, fydim, fydimen, fviewingrange;
extern float fsearchx, fsearchy;
extern char ptempbuf[MAXWALLSB<<1];
......@@ -317,7 +320,6 @@ extern void polymost_setupdetailtexture(int32_t texunits, int32_t tex);
#ifdef __cplusplus
......@@ -3,6 +3,7 @@
# define prlight_h_
#define PR_MAXLIGHTS 1024
......@@ -19,8 +20,8 @@ typedef struct s_prlight {
int8_t minshade, maxshade;
int16_t tilenum;
struct {
int32_t emitshadow : 1;
int32_t negative : 1;
int emitshadow : 1;
int negative : 1;
} publicflags;
// internal members
float proj[16];
......@@ -28,9 +29,9 @@ typedef struct s_prlight {
float frustum[5 * 4];
int32_t rtindex;
struct {
int32_t active : 1;
int32_t invalidate : 1;
int32_t isinview : 1;
int active : 1;
int invalidate : 1;
int isinview : 1;
} flags;
uint32_t lightmap;
_prplanelist* planelist;
This diff is collapsed.
......@@ -439,7 +439,7 @@ void animvpx_setup_glstate(int32_t animvpx_flags)
if (PHandle)
////////// GL STATE //////////
......@@ -496,7 +496,7 @@ void animvpx_restore_glstate(void)
#ifdef USE_GLEXT
if (glinfo.glsl)
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment