+typedef struct rsurfacestate_s
+{
+ // current model array pointers
+ // these may point to processing buffers if model is animated,
+ // otherwise they point to static data.
+ // these are not directly used for rendering, they are just another level
+ // of processing
+ //
+ // these either point at array_model* buffers (if the model is animated)
+ // or the model->surfmesh.data_* buffers (if the model is not animated)
+ //
+ // these are only set when an entity render begins, they do not change on
+ // a per surface basis.
+ //
+ // this indicates the model* arrays are pointed at array_model* buffers
+ // (in other words, the model has been animated in software)
+ qbool forcecurrenttextureupdate; // set for RSurf_ActiveCustomEntity to force R_GetCurrentTexture to recalculate the texture parameters (such as entity alpha)
+ qbool modelgeneratedvertex;
+ // skeletal animation can be done by entity (animcache) or per batch,
+ // batch may be non-skeletal even if entity is skeletal, indicating that
+ // the dynamicvertex code path had to apply skeletal manually for a case
+ // where gpu-skinning is not possible, for this reason batch has its own
+ // variables
+ int entityskeletalnumtransforms; // how many transforms are used for this mesh
+ float *entityskeletaltransform3x4; // use gpu-skinning shader on this mesh
+ const r_meshbuffer_t *entityskeletaltransform3x4buffer; // uniform buffer
+ int entityskeletaltransform3x4offset;
+ int entityskeletaltransform3x4size;
+ float *modelvertex3f;
+ const r_meshbuffer_t *modelvertex3f_vertexbuffer;
+ int modelvertex3f_bufferoffset;
+ float *modelsvector3f;
+ const r_meshbuffer_t *modelsvector3f_vertexbuffer;
+ int modelsvector3f_bufferoffset;
+ float *modeltvector3f;
+ const r_meshbuffer_t *modeltvector3f_vertexbuffer;
+ int modeltvector3f_bufferoffset;
+ float *modelnormal3f;
+ const r_meshbuffer_t *modelnormal3f_vertexbuffer;
+ int modelnormal3f_bufferoffset;
+ float *modellightmapcolor4f;
+ const r_meshbuffer_t *modellightmapcolor4f_vertexbuffer;
+ int modellightmapcolor4f_bufferoffset;
+ float *modeltexcoordtexture2f;
+ const r_meshbuffer_t *modeltexcoordtexture2f_vertexbuffer;
+ int modeltexcoordtexture2f_bufferoffset;
+ float *modeltexcoordlightmap2f;
+ const r_meshbuffer_t *modeltexcoordlightmap2f_vertexbuffer;
+ int modeltexcoordlightmap2f_bufferoffset;
+ unsigned char *modelskeletalindex4ub;
+ const r_meshbuffer_t *modelskeletalindex4ub_vertexbuffer;
+ int modelskeletalindex4ub_bufferoffset;
+ unsigned char *modelskeletalweight4ub;
+ const r_meshbuffer_t *modelskeletalweight4ub_vertexbuffer;
+ int modelskeletalweight4ub_bufferoffset;
+ int *modelelement3i;
+ const r_meshbuffer_t *modelelement3i_indexbuffer;
+ int modelelement3i_bufferoffset;
+ unsigned short *modelelement3s;
+ const r_meshbuffer_t *modelelement3s_indexbuffer;
+ int modelelement3s_bufferoffset;
+ int *modellightmapoffsets;
+ int modelnumvertices;
+ int modelnumtriangles;
+ const msurface_t *modelsurfaces;
+ // current rendering array pointers
+ // these may point to any of several different buffers depending on how
+ // much processing was needed to prepare this model for rendering
+ // these usually equal the model* pointers, they only differ if
+ // deformvertexes is used in a q3 shader, and consequently these can
+ // change on a per-surface basis (according to rsurface.texture)
+ qbool batchgeneratedvertex;
+ qbool batchmultidraw;
+ int batchmultidrawnumsurfaces;
+ const msurface_t **batchmultidrawsurfacelist;
+ int batchfirstvertex;
+ int batchnumvertices;
+ int batchfirsttriangle;
+ int batchnumtriangles;
+ float *batchvertex3f;
+ const r_meshbuffer_t *batchvertex3f_vertexbuffer;
+ int batchvertex3f_bufferoffset;
+ float *batchsvector3f;
+ const r_meshbuffer_t *batchsvector3f_vertexbuffer;
+ int batchsvector3f_bufferoffset;
+ float *batchtvector3f;
+ const r_meshbuffer_t *batchtvector3f_vertexbuffer;
+ int batchtvector3f_bufferoffset;
+ float *batchnormal3f;
+ const r_meshbuffer_t *batchnormal3f_vertexbuffer;
+ int batchnormal3f_bufferoffset;
+ float *batchlightmapcolor4f;
+ const r_meshbuffer_t *batchlightmapcolor4f_vertexbuffer;
+ int batchlightmapcolor4f_bufferoffset;
+ float *batchtexcoordtexture2f;
+ const r_meshbuffer_t *batchtexcoordtexture2f_vertexbuffer;
+ int batchtexcoordtexture2f_bufferoffset;
+ float *batchtexcoordlightmap2f;
+ const r_meshbuffer_t *batchtexcoordlightmap2f_vertexbuffer;
+ int batchtexcoordlightmap2f_bufferoffset;
+ unsigned char *batchskeletalindex4ub;
+ const r_meshbuffer_t *batchskeletalindex4ub_vertexbuffer;
+ int batchskeletalindex4ub_bufferoffset;
+ unsigned char *batchskeletalweight4ub;
+ const r_meshbuffer_t *batchskeletalweight4ub_vertexbuffer;
+ int batchskeletalweight4ub_bufferoffset;
+ int *batchelement3i;
+ const r_meshbuffer_t *batchelement3i_indexbuffer;
+ int batchelement3i_bufferoffset;
+ unsigned short *batchelement3s;
+ const r_meshbuffer_t *batchelement3s_indexbuffer;
+ int batchelement3s_bufferoffset;
+ int batchskeletalnumtransforms;
+ float *batchskeletaltransform3x4;
+ const r_meshbuffer_t *batchskeletaltransform3x4buffer; // uniform buffer
+ int batchskeletaltransform3x4offset;
+ int batchskeletaltransform3x4size;
+
+ // some important fields from the entity
+ int ent_skinnum;
+ int ent_qwskin;
+ int ent_flags;
+ int ent_alttextures; // used by q1bsp animated textures (pressed buttons)
+ double shadertime; // r_refdef.scene.time - ent->shadertime
+ // transform matrices to render this entity and effects on this entity
+ matrix4x4_t matrix;
+ matrix4x4_t inversematrix;
+ // scale factors for transforming lengths into/out of entity space
+ float matrixscale;
+ float inversematrixscale;
+ // animation blending state from entity
+ frameblend_t frameblend[MAX_FRAMEBLENDS];
+ skeleton_t *skeleton;
+ // view location in model space
+ vec3_t localvieworigin;
+ // polygon offset data for submodels
+ float basepolygonfactor;
+ float basepolygonoffset;
+ // current textures in batching code
+ texture_t *texture;
+ rtexture_t *lightmaptexture;
+ rtexture_t *deluxemaptexture;
+ // whether lightmapping is active on this batch
+ // (otherwise vertex colored)
+ qbool uselightmaptexture;
+ // fog plane in model space for direct application to vertices
+ float fograngerecip;
+ float fogmasktabledistmultiplier;
+ float fogplane[4];
+ float fogheightfade;
+ float fogplaneviewdist;
+
+ // rtlight rendering
+ // light currently being rendered
+ const rtlight_t *rtlight;
+
+ // this is the location of the light in entity space
+ vec3_t entitylightorigin;
+ // this transforms entity coordinates to light filter cubemap coordinates
+ // (also often used for other purposes)
+ matrix4x4_t entitytolight;
+ // based on entitytolight this transforms -1 to +1 to 0 to 1 for purposes
+ // of attenuation texturing in full 3D (Z result often ignored)
+ matrix4x4_t entitytoattenuationxyz;
+ // this transforms only the Z to S, and T is always 0.5
+ matrix4x4_t entitytoattenuationz;
+
+ // user wavefunc parameters (from csqc)
+ float userwavefunc_param[Q3WAVEFUNC_USER_COUNT];
+
+ // pointer to an entity_render_t used only by R_GetCurrentTexture and
+ // RSurf_ActiveModelEntity as a unique id within each frame (see r_frame
+ // also)
+ entity_render_t *entity;
+}
+rsurfacestate_t;
+
+extern rsurfacestate_t rsurface;
+
+void R_HDR_UpdateIrisAdaptation(const vec3_t point);
+
+void RSurf_ActiveModelEntity(const entity_render_t *ent, qbool wantnormals, qbool wanttangents, qbool prepass);
+void RSurf_ActiveCustomEntity(const matrix4x4_t *matrix, const matrix4x4_t *inversematrix, int entflags, double shadertime, float r, float g, float b, float a, int numvertices, const float *vertex3f, const float *texcoord2f, const float *normal3f, const float *svector3f, const float *tvector3f, const float *color4f, int numtriangles, const int *element3i, const unsigned short *element3s, qbool wantnormals, qbool wanttangents);
+void RSurf_SetupDepthAndCulling(void);
+
+extern int r_textureframe; ///< used only by R_GetCurrentTexture, incremented per view and per UI render
+texture_t *R_GetCurrentTexture(texture_t *t);
+void R_DrawModelSurfaces(entity_render_t *ent, qbool skysurfaces, qbool writedepth, qbool depthonly, qbool debug, qbool prepass, qbool ui);
+void R_DrawCustomSurface(skinframe_t *skinframe, const matrix4x4_t *texmatrix, int materialflags, int firstvertex, int numvertices, int firsttriangle, int numtriangles, qbool writedepth, qbool prepass, qbool ui);
+void R_DrawCustomSurface_Texture(texture_t *texture, const matrix4x4_t *texmatrix, int materialflags, int firstvertex, int numvertices, int firsttriangle, int numtriangles, qbool writedepth, qbool prepass, qbool ui);
+
+#define BATCHNEED_ARRAY_VERTEX (1<< 0) // set up rsurface.batchvertex3f
+#define BATCHNEED_ARRAY_NORMAL (1<< 1) // set up rsurface.batchnormal3f
+#define BATCHNEED_ARRAY_VECTOR (1<< 2) // set up rsurface.batchsvector3f and rsurface.batchtvector3f
+#define BATCHNEED_ARRAY_VERTEXTINTCOLOR (1<< 3) // set up rsurface.batchvertexcolor4f
+#define BATCHNEED_ARRAY_VERTEXCOLOR (1<< 4) // set up rsurface.batchlightmapcolor4f
+#define BATCHNEED_ARRAY_TEXCOORD (1<< 5) // set up rsurface.batchtexcoordtexture2f
+#define BATCHNEED_ARRAY_LIGHTMAP (1<< 6) // set up rsurface.batchtexcoordlightmap2f
+#define BATCHNEED_ARRAY_SKELETAL (1<< 7) // set up skeletal index and weight data for vertex shader
+#define BATCHNEED_NOGAPS (1<< 8) // force vertex copying if firstvertex is not zero or there are gaps
+#define BATCHNEED_ALWAYSCOPY (1<< 9) // force vertex copying unconditionally - useful if you want to modify colors
+#define BATCHNEED_ALLOWMULTIDRAW (1<<10) // allow multiple draws
+void RSurf_PrepareVerticesForBatch(int batchneed, int texturenumsurfaces, const msurface_t **texturesurfacelist);
+void RSurf_UploadBuffersForBatch(void);
+void RSurf_DrawBatch(void);
+
+void R_DecalSystem_SplatEntities(const vec3_t org, const vec3_t normal, float r, float g, float b, float a, float s1, float t1, float s2, float t2, float size);
+
+typedef enum rsurfacepass_e