]> git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - model_shared.h
model_shared: Avoid using R_FrameData_Alloc when building sorted surfaces list
[xonotic/darkplaces.git] / model_shared.h
index 463af5f12725ebd3b0809f77aa1dcdaca43a3d25..cf5e02dc1ec4450210fceb52f9767a01ca738caf 100644 (file)
@@ -29,6 +29,8 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 struct rtexture_s;
 struct mempool_s;
 struct skeleton_s;
+struct skinframe_s;
+
 typedef enum synctype_e {ST_SYNC=0, ST_RAND } synctype_t;
 
 /*
@@ -38,7 +40,7 @@ m*_t structures are in-memory
 
 */
 
-typedef enum modtype_e {mod_invalid, mod_brushq1, mod_sprite, mod_alias, mod_brushq2, mod_brushq3, mod_obj, mod_null} modtype_t;
+typedef enum modtype_e {mod_invalid, mod_brushq1, mod_sprite, mod_alias, mod_brushq2, mod_brushq3, mod_brushhl2, mod_obj, mod_null} modtype_t;
 
 typedef struct animscene_s
 {
@@ -50,49 +52,6 @@ typedef struct animscene_s
 }
 animscene_t;
 
-typedef struct skinframe_s
-{
-       struct rtexture_s *stain; // inverse modulate with background (used for decals and such)
-       struct rtexture_s *merged; // original texture without glow
-       struct rtexture_s *base; // original texture without pants/shirt/glow
-       struct rtexture_s *pants; // pants only (in greyscale)
-       struct rtexture_s *shirt; // shirt only (in greyscale)
-       struct rtexture_s *nmap; // normalmap (bumpmap for dot3)
-       struct rtexture_s *gloss; // glossmap (for dot3)
-       struct rtexture_s *glow; // glow only (fullbrights)
-       struct rtexture_s *fog; // alpha of the base texture (if not opaque)
-       struct rtexture_s *reflect; // colored mask for cubemap reflections
-       // accounting data for hash searches:
-       // the compare variables are used to identify internal skins from certain
-       // model formats
-       // (so that two q1bsp maps with the same texture name for different
-       //  textures do not have any conflicts)
-       struct skinframe_s *next; // next on hash chain
-       char basename[MAX_QPATH]; // name of this
-       int textureflags; // texture flags to use
-       int comparewidth;
-       int compareheight;
-       int comparecrc;
-       // mark and sweep garbage collection, this value is updated to a new value
-       // on each level change for the used skinframes, if some are not used they
-       // are freed
-       unsigned int loadsequence;
-       // indicates whether this texture has transparent pixels
-       qbool hasalpha;
-       // average texture color, if applicable
-       float avgcolor[4];
-       // for mdl skins, we actually only upload on first use (many are never used, and they are almost never used in both base+pants+shirt and merged modes)
-       unsigned char *qpixels;
-       int qwidth;
-       int qheight;
-       qbool qhascolormapping;
-       qbool qgeneratebase;
-       qbool qgeneratemerged;
-       qbool qgeneratenmap;
-       qbool qgenerateglow;
-}
-skinframe_t;
-
 struct md3vertex_s;
 struct trivertx_s;
 typedef struct texvecvertex_s
@@ -249,9 +208,9 @@ typedef struct texture_s
        float biaspolygonoffset;
 
        // textures to use when rendering this material (derived from materialshaderpass)
-       skinframe_t *currentskinframe;
+       struct skinframe_s *currentskinframe;
        // textures to use for terrain texture blending (derived from backgroundshaderpass)
-       skinframe_t *backgroundcurrentskinframe;
+       struct skinframe_s *backgroundcurrentskinframe;
 
        // total frames in sequence and alternate sequence
        int anim_total[2];
@@ -411,33 +370,40 @@ typedef struct msurface_lightmapinfo_s
 msurface_lightmapinfo_t;
 
 struct q3deffect_s;
+
+/// <summary>
+///  describes the textures to use on a range of triangles in the model, and mins/maxs (AABB) for culling.
+/// </summary>
 typedef struct msurface_s
 {
-       // bounding box for onscreen checks
-       vec3_t mins;
-       vec3_t maxs;
-       // the texture to use on the surface
+       /// range of triangles and vertices in model->surfmesh
+       int num_triangles; // triangles
+       int num_firsttriangle; // first element is this *3
+       int num_vertices; // length of the range referenced by elements
+       int num_firstvertex; // min vertex referenced by elements
+
+       /// the texture to use on the surface
        texture_t *texture;
-       // the lightmap texture fragment to use on the rendering mesh
+       /// the lightmap texture fragment to use on the rendering mesh
        struct rtexture_s *lightmaptexture;
-       // the lighting direction texture fragment to use on the rendering mesh
+       /// the lighting direction texture fragment to use on the rendering mesh
        struct rtexture_s *deluxemaptexture;
-       // lightmaptexture rebuild information not used in q3bsp
-       msurface_lightmapinfo_t *lightmapinfo; // q1bsp
-       // fog volume info in q3bsp
-       struct q3deffect_s *effect; // q3bsp
-       // mesh information for collisions (only used by q3bsp curves)
-       int num_firstcollisiontriangle;
-
-       // surfaces own ranges of vertices and triangles in the model->surfmesh
-       int num_triangles; // number of triangles
-       int num_firsttriangle; // first triangle
-       int num_vertices; // number of vertices
-       int num_firstvertex; // first vertex
-
-       // mesh information for collisions (only used by q3bsp curves)
-       int num_collisiontriangles; // q3bsp
-       int num_collisionvertices; // q3bsp
+
+       // the following fields are used situationally and are not part of rendering in typical usage
+
+       /// bounding box for onscreen checks
+       vec3_t mins;
+       vec3_t maxs;
+
+       /// lightmaptexture rebuild information not used in q3bsp
+       msurface_lightmapinfo_t* lightmapinfo; // q1bsp
+       /// fog volume info in q3bsp
+       struct q3deffect_s* effect; // q3bsp
+
+       /// mesh information for collisions (only used by q3bsp curves)
+       int num_firstcollisiontriangle; // q3bsp only
+       int num_collisiontriangles; // number of triangles (if surface has collisions enabled)
+       int num_collisionvertices; // number of vertices referenced by collision triangles (if surface has collisions enabled)
 
        // used by Mod_Mesh_Finalize when building sortedmodelsurfaces
        qbool included;
@@ -450,6 +416,7 @@ msurface_t;
 #include "model_q1bsp.h"
 #include "model_q2bsp.h"
 #include "model_q3bsp.h"
+#include "model_vbsp.h"
 #include "model_sprite.h"
 #include "model_alias.h"
 
@@ -496,10 +463,11 @@ typedef struct model_s
        animscene_t             *skinscenes; // [numskins]
        // skin animation info
        animscene_t             *animscenes; // [numframes]
-       // range of surface numbers in this (sub)model
-       int                             firstmodelsurface;
-       int                             nummodelsurfaces;
-       int                             *sortedmodelsurfaces;
+       // range of surface numbers in this model
+       int                             submodelsurfaces_start;
+       int                             submodelsurfaces_end;
+       /// surface indices of model in an optimal draw order (submodelindex -> texture -> lightmap -> index)
+       int                             *modelsurfaces_sorted; // same size as num_surfaces
        // range of collision brush numbers in this (sub)model
        int                             firstmodelbrush;
        int                             nummodelbrushes;
@@ -537,9 +505,9 @@ typedef struct model_s
        const char              *modeldatatypestring;
        // generates vertex data for a given frameblend
        void(*AnimateVertices)(const struct model_s * RESTRICT model, const struct frameblend_s * RESTRICT frameblend, const struct skeleton_s *skeleton, float * RESTRICT vertex3f, float * RESTRICT normal3f, float * RESTRICT svector3f, float * RESTRICT tvector3f);
-       // draw the model's sky polygons (only used by brush models)
+       // draw the model's sky polygons
        void(*DrawSky)(struct entity_render_s *ent);
-       // draw refraction/reflection textures for the model's water polygons (only used by brush models)
+       // draw refraction/reflection textures for the model's water polygons
        void(*DrawAddWaterPlanes)(struct entity_render_s *ent);
        // draw the model using lightmap/dlight shading
        void(*Draw)(struct entity_render_s *ent);
@@ -582,12 +550,12 @@ typedef struct model_s
 
        qbool nolerp;
 }
-dp_model_t;
+model_t;
 
 //============================================================================
 
 // model loading
-extern dp_model_t *loadmodel;
+extern model_t *loadmodel;
 extern unsigned char *mod_base;
 
 typedef struct modloader_s
@@ -595,7 +563,7 @@ typedef struct modloader_s
        const char *extension;
        const char *header;
        size_t headersize; // The header might not be NULL terminated
-       void (*Load)(dp_model_t *, void *, void *);
+       void (*Load)(model_t *, void *, void *);
 } modloader_t;
 
 // sky/water subdivision
@@ -618,16 +586,16 @@ extern struct cvar_s mod_q3bsp_lightgrid_bsp_surfaces;
 
 void Mod_Init (void);
 void Mod_Reload (void);
-dp_model_t *Mod_LoadModel(dp_model_t *mod, qbool crash, qbool checkdisk);
-dp_model_t *Mod_FindName (const char *name, const char *parentname);
-dp_model_t *Mod_ForName (const char *name, qbool crash, qbool checkdisk, const char *parentname);
-void Mod_UnloadModel (dp_model_t *mod);
+model_t *Mod_LoadModel(model_t *mod, qbool crash, qbool checkdisk);
+model_t *Mod_FindName (const char *name, const char *parentname);
+model_t *Mod_ForName (const char *name, qbool crash, qbool checkdisk, const char *parentname);
+void Mod_UnloadModel (model_t *mod);
 
 void Mod_ClearUsed(void);
 void Mod_PurgeUnused(void);
-void Mod_RemoveStaleWorldModels(dp_model_t *skip); // only used during loading!
+void Mod_RemoveStaleWorldModels(model_t *skip); // only used during loading!
 
-extern dp_model_t *loadmodel;
+extern model_t *loadmodel;
 extern char loadname[32];      // for hunk tags
 
 int Mod_BuildVertexRemapTableFromElements(int numelements, const int *elements, int numvertices, int *remapvertices);
@@ -636,12 +604,17 @@ void Mod_BuildTextureVectorsFromNormals(int firstvertex, int numvertices, int nu
 
 qbool Mod_ValidateElements(int *element3i, unsigned short *element3s, int numtriangles, int firstvertex, int numvertices, const char *filename, int fileline);
 void Mod_AllocSurfMesh(struct mempool_s *mempool, int numvertices, int numtriangles, qbool lightmapoffsets, qbool vertexcolors);
-void Mod_MakeSortedSurfaces(dp_model_t *mod);
+void Mod_MakeSortedSurfaces(model_t *mod);
 
 // called specially by brush model loaders before generating submodels
 // automatically called after model loader returns
 void Mod_BuildVBOs(void);
 
+/// Sets the mod->DrawSky and mod->DrawAddWaterPlanes pointers conditionally based on whether surfaces in this submodel use these features
+/// called specifically by brush model loaders when generating submodels
+/// automatically called after model loader returns
+void Mod_SetDrawSkyAndWater(model_t* mod);
+
 shadowmesh_t *Mod_ShadowMesh_Alloc(struct mempool_s *mempool, int maxverts, int maxtriangles);
 int Mod_ShadowMesh_AddVertex(shadowmesh_t *mesh, const float *vertex3f);
 void Mod_ShadowMesh_AddMesh(shadowmesh_t *mesh, const float *vertex3f, int numtris, const int *element3i);
@@ -650,16 +623,16 @@ shadowmesh_t *Mod_ShadowMesh_Finish(shadowmesh_t *firstmesh, qbool createvbo);
 void Mod_ShadowMesh_CalcBBox(shadowmesh_t *firstmesh, vec3_t mins, vec3_t maxs, vec3_t center, float *radius);
 void Mod_ShadowMesh_Free(shadowmesh_t *mesh);
 
-void Mod_CreateCollisionMesh(dp_model_t *mod);
+void Mod_CreateCollisionMesh(model_t *mod);
 
 void Mod_FreeQ3Shaders(void);
 void Mod_LoadQ3Shaders(void);
 shader_t *Mod_LookupQ3Shader(const char *name);
 qbool Mod_LoadTextureFromQ3Shader(struct mempool_s *mempool, const char *modelname, texture_t *texture, const char *name, qbool warnmissing, qbool fallback, int defaulttexflags, int defaultmaterialflags);
-texture_shaderpass_t *Mod_CreateShaderPass(struct mempool_s *mempool, skinframe_t *skinframe);
+texture_shaderpass_t *Mod_CreateShaderPass(struct mempool_s *mempool, struct skinframe_s *skinframe);
 texture_shaderpass_t *Mod_CreateShaderPassFromQ3ShaderLayer(struct mempool_s *mempool, const char *modelname, q3shaderinfo_layer_t *layer, int layerindex, int texflags, const char *texturename);
 /// Sets up a material to render the provided skinframe.  See also R_SkinFrame_LoadInternalBGRA.
-void Mod_LoadCustomMaterial(struct mempool_s *mempool, texture_t *texture, const char *name, int supercontents, int materialflags, skinframe_t *skinframe);
+void Mod_LoadCustomMaterial(struct mempool_s *mempool, texture_t *texture, const char *name, int supercontents, int materialflags, struct skinframe_s *skinframe);
 /// Removes all shaderpasses from material, and optionally deletes the textures in the skinframes.
 void Mod_UnloadCustomMaterial(texture_t *texture, qbool purgeskins);
 
@@ -734,34 +707,34 @@ void R_Mod_DrawShadowMap(int side, struct entity_render_s *ent, const vec3_t rel
 void R_Mod_DrawLight(struct entity_render_s *ent, int numsurfaces, const int *surfacelist, const unsigned char *trispvs);
 
 // dynamic mesh building (every frame) for debugging and other uses
-void Mod_Mesh_Create(dp_model_t *mod, const char *name);
-void Mod_Mesh_Destroy(dp_model_t *mod);
-void Mod_Mesh_Reset(dp_model_t *mod);
-texture_t *Mod_Mesh_GetTexture(dp_model_t *mod, const char *name, int defaultdrawflags, int defaulttexflags, int defaultmaterialflags);
-msurface_t *Mod_Mesh_AddSurface(dp_model_t *mod, texture_t *tex, qbool batchwithprevioussurface);
-int Mod_Mesh_IndexForVertex(dp_model_t *mod, msurface_t *surf, float x, float y, float z, float nx, float ny, float nz, float s, float t, float u, float v, float r, float g, float b, float a);
-void Mod_Mesh_AddTriangle(dp_model_t *mod, msurface_t *surf, int e0, int e1, int e2);
-void Mod_Mesh_Validate(dp_model_t *mod);
-void Mod_Mesh_Finalize(dp_model_t *mod);
+void Mod_Mesh_Create(model_t *mod, const char *name);
+void Mod_Mesh_Destroy(model_t *mod);
+void Mod_Mesh_Reset(model_t *mod);
+texture_t *Mod_Mesh_GetTexture(model_t *mod, const char *name, int defaultdrawflags, int defaulttexflags, int defaultmaterialflags);
+msurface_t *Mod_Mesh_AddSurface(model_t *mod, texture_t *tex, qbool batchwithprevioussurface);
+int Mod_Mesh_IndexForVertex(model_t *mod, msurface_t *surf, float x, float y, float z, float nx, float ny, float nz, float s, float t, float u, float v, float r, float g, float b, float a);
+void Mod_Mesh_AddTriangle(model_t *mod, msurface_t *surf, int e0, int e1, int e2);
+void Mod_Mesh_Validate(model_t *mod);
+void Mod_Mesh_Finalize(model_t *mod);
 
 // Collision optimization using Bounding Interval Hierarchy
-void Mod_CollisionBIH_TracePoint(dp_model_t *model, const struct frameblend_s *frameblend, const struct skeleton_s *skeleton, struct trace_s *trace, const vec3_t start, int hitsupercontentsmask, int skipsupercontentsmask, int skipmaterialflagsmask);
-void Mod_CollisionBIH_TraceLine(dp_model_t *model, const struct frameblend_s *frameblend, const struct skeleton_s *skeleton, struct trace_s *trace, const vec3_t start, const vec3_t end, int hitsupercontentsmask, int skipsupercontentsmask, int skipmaterialflagsmask);
-void Mod_CollisionBIH_TraceBox(dp_model_t *model, const struct frameblend_s *frameblend, const struct skeleton_s *skeleton, struct trace_s *trace, const vec3_t start, const vec3_t boxmins, const vec3_t boxmaxs, const vec3_t end, int hitsupercontentsmask, int skipsupercontentsmask, int skipmaterialflagsmask);
-void Mod_CollisionBIH_TraceBrush(dp_model_t *model, const struct frameblend_s *frameblend, const struct skeleton_s *skeleton, struct trace_s *trace, struct colbrushf_s *start, struct colbrushf_s *end, int hitsupercontentsmask, int skipsupercontentsmask, int skipmaterialflagsmask);
-void Mod_CollisionBIH_TracePoint_Mesh(dp_model_t *model, const struct frameblend_s *frameblend, const struct skeleton_s *skeleton, struct trace_s *trace, const vec3_t start, int hitsupercontentsmask, int skipsupercontentsmask, int skipmaterialflagsmask);
+void Mod_CollisionBIH_TracePoint(model_t *model, const struct frameblend_s *frameblend, const struct skeleton_s *skeleton, struct trace_s *trace, const vec3_t start, int hitsupercontentsmask, int skipsupercontentsmask, int skipmaterialflagsmask);
+void Mod_CollisionBIH_TraceLine(model_t *model, const struct frameblend_s *frameblend, const struct skeleton_s *skeleton, struct trace_s *trace, const vec3_t start, const vec3_t end, int hitsupercontentsmask, int skipsupercontentsmask, int skipmaterialflagsmask);
+void Mod_CollisionBIH_TraceBox(model_t *model, const struct frameblend_s *frameblend, const struct skeleton_s *skeleton, struct trace_s *trace, const vec3_t start, const vec3_t boxmins, const vec3_t boxmaxs, const vec3_t end, int hitsupercontentsmask, int skipsupercontentsmask, int skipmaterialflagsmask);
+void Mod_CollisionBIH_TraceBrush(model_t *model, const struct frameblend_s *frameblend, const struct skeleton_s *skeleton, struct trace_s *trace, struct colbrushf_s *start, struct colbrushf_s *end, int hitsupercontentsmask, int skipsupercontentsmask, int skipmaterialflagsmask);
+void Mod_CollisionBIH_TracePoint_Mesh(model_t *model, const struct frameblend_s *frameblend, const struct skeleton_s *skeleton, struct trace_s *trace, const vec3_t start, int hitsupercontentsmask, int skipsupercontentsmask, int skipmaterialflagsmask);
 qbool Mod_CollisionBIH_TraceLineOfSight(struct model_s *model, const vec3_t start, const vec3_t end, const vec3_t acceptmins, const vec3_t acceptmaxs);
 int Mod_CollisionBIH_PointSuperContents(struct model_s *model, int frame, const vec3_t point);
 int Mod_CollisionBIH_PointSuperContents_Mesh(struct model_s *model, int frame, const vec3_t point);
-bih_t *Mod_MakeCollisionBIH(dp_model_t *model, qbool userendersurfaces, bih_t *out);
+bih_t *Mod_MakeCollisionBIH(model_t *model, qbool userendersurfaces, bih_t *out);
 
 // alias models
 struct frameblend_s;
 struct skeleton_s;
 void Mod_AliasInit(void);
-int Mod_Alias_GetTagMatrix(const dp_model_t *model, const struct frameblend_s *frameblend, const struct skeleton_s *skeleton, int tagindex, matrix4x4_t *outmatrix);
-int Mod_Alias_GetTagIndexForName(const dp_model_t *model, unsigned int skin, const char *tagname);
-int Mod_Alias_GetExtendedTagInfoForIndex(const dp_model_t *model, unsigned int skin, const struct frameblend_s *frameblend, const struct skeleton_s *skeleton, int tagindex, int *parentindex, const char **tagname, matrix4x4_t *tag_localmatrix);
+int Mod_Alias_GetTagMatrix(const model_t *model, const struct frameblend_s *frameblend, const struct skeleton_s *skeleton, int tagindex, matrix4x4_t *outmatrix);
+int Mod_Alias_GetTagIndexForName(const model_t *model, unsigned int skin, const char *tagname);
+int Mod_Alias_GetExtendedTagInfoForIndex(const model_t *model, unsigned int skin, const struct frameblend_s *frameblend, const struct skeleton_s *skeleton, int tagindex, int *parentindex, const char **tagname, matrix4x4_t *tag_localmatrix);
 
 void Mod_Skeletal_FreeBuffers(void);
 
@@ -769,22 +742,23 @@ void Mod_Skeletal_FreeBuffers(void);
 void Mod_SpriteInit(void);
 
 // loaders
-void Mod_2PSB_Load(dp_model_t *mod, void *buffer, void *bufferend);
-void Mod_BSP2_Load(dp_model_t *mod, void *buffer, void *bufferend);
-void Mod_HLBSP_Load(dp_model_t *mod, void *buffer, void *bufferend);
-void Mod_Q1BSP_Load(dp_model_t *mod, void *buffer, void *bufferend);
-void Mod_IBSP_Load(dp_model_t *mod, void *buffer, void *bufferend);
-void Mod_MAP_Load(dp_model_t *mod, void *buffer, void *bufferend);
-void Mod_OBJ_Load(dp_model_t *mod, void *buffer, void *bufferend);
-void Mod_IDP0_Load(dp_model_t *mod, void *buffer, void *bufferend);
-void Mod_IDP2_Load(dp_model_t *mod, void *buffer, void *bufferend);
-void Mod_IDP3_Load(dp_model_t *mod, void *buffer, void *bufferend);
-void Mod_ZYMOTICMODEL_Load(dp_model_t *mod, void *buffer, void *bufferend);
-void Mod_DARKPLACESMODEL_Load(dp_model_t *mod, void *buffer, void *bufferend);
-void Mod_PSKMODEL_Load(dp_model_t *mod, void *buffer, void *bufferend);
-void Mod_IDSP_Load(dp_model_t *mod, void *buffer, void *bufferend);
-void Mod_IDS2_Load(dp_model_t *mod, void *buffer, void *bufferend);
-void Mod_INTERQUAKEMODEL_Load(dp_model_t *mod, void *buffer, void *bufferend);
+void Mod_2PSB_Load(model_t *mod, void *buffer, void *bufferend);
+void Mod_BSP2_Load(model_t *mod, void *buffer, void *bufferend);
+void Mod_HLBSP_Load(model_t *mod, void *buffer, void *bufferend);
+void Mod_Q1BSP_Load(model_t *mod, void *buffer, void *bufferend);
+void Mod_IBSP_Load(model_t *mod, void *buffer, void *bufferend);
+void Mod_VBSP_Load(model_t *mod, void *buffer, void *bufferend);
+void Mod_MAP_Load(model_t *mod, void *buffer, void *bufferend);
+void Mod_OBJ_Load(model_t *mod, void *buffer, void *bufferend);
+void Mod_IDP0_Load(model_t *mod, void *buffer, void *bufferend);
+void Mod_IDP2_Load(model_t *mod, void *buffer, void *bufferend);
+void Mod_IDP3_Load(model_t *mod, void *buffer, void *bufferend);
+void Mod_ZYMOTICMODEL_Load(model_t *mod, void *buffer, void *bufferend);
+void Mod_DARKPLACESMODEL_Load(model_t *mod, void *buffer, void *bufferend);
+void Mod_PSKMODEL_Load(model_t *mod, void *buffer, void *bufferend);
+void Mod_IDSP_Load(model_t *mod, void *buffer, void *bufferend);
+void Mod_IDS2_Load(model_t *mod, void *buffer, void *bufferend);
+void Mod_INTERQUAKEMODEL_Load(model_t *mod, void *buffer, void *bufferend);
 
 #endif // MODEL_SHARED_H