12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082 |
- #pragma hdrstop
- #include "../idlib/precompiled.h"
- #include "tr_local.h"
- #include "Model_local.h"
- idCVar r_skipStaticShadows( "r_skipStaticShadows", "0", CVAR_RENDERER | CVAR_BOOL, "skip static shadows" );
- idCVar r_skipDynamicShadows( "r_skipDynamicShadows", "0", CVAR_RENDERER | CVAR_BOOL, "skip dynamic shadows" );
- idCVar r_useParallelAddModels( "r_useParallelAddModels", "1", CVAR_RENDERER | CVAR_BOOL, "add all models in parallel with jobs" );
- idCVar r_useParallelAddShadows( "r_useParallelAddShadows", "1", CVAR_RENDERER | CVAR_INTEGER, "0 = off, 1 = threaded", 0, 1 );
- idCVar r_useShadowPreciseInsideTest( "r_useShadowPreciseInsideTest", "1", CVAR_RENDERER | CVAR_BOOL, "use a precise and more expensive test to determine whether the view is inside a shadow volume" );
- idCVar r_cullDynamicShadowTriangles( "r_cullDynamicShadowTriangles", "1", CVAR_RENDERER | CVAR_BOOL, "cull occluder triangles that are outside the light frustum so they do not contribute to the dynamic shadow volume" );
- idCVar r_cullDynamicLightTriangles( "r_cullDynamicLightTriangles", "1", CVAR_RENDERER | CVAR_BOOL, "cull surface triangles that are outside the light frustum so they do not get rendered for interactions" );
- idCVar r_forceShadowCaps( "r_forceShadowCaps", "0", CVAR_RENDERER | CVAR_BOOL, "0 = skip rendering shadow caps if view is outside shadow volume, 1 = always render shadow caps" );
- static const float CHECK_BOUNDS_EPSILON = 1.0f;
- viewEntity_t * R_SortViewEntities( viewEntity_t * vEntities ) {
- SCOPED_PROFILE_EVENT( "R_SortViewEntities" );
-
-
-
- viewEntity_t * dynamics = NULL;
- viewEntity_t * areas = NULL;
- viewEntity_t * others = NULL;
- for ( viewEntity_t * vEntity = vEntities; vEntity != NULL; ) {
- viewEntity_t * next = vEntity->next;
- const idRenderModel * model = vEntity->entityDef->parms.hModel;
- if ( model->IsDynamicModel() != DM_STATIC ) {
- vEntity->next = dynamics;
- dynamics = vEntity;
- } else if ( model->IsStaticWorldModel() ) {
- vEntity->next = areas;
- areas = vEntity;
- } else {
- vEntity->next = others;
- others = vEntity;
- }
- vEntity = next;
- }
-
- viewEntity_t * all = others;
- for ( viewEntity_t * vEntity = areas; vEntity != NULL; ) {
- viewEntity_t * next = vEntity->next;
- vEntity->next = all;
- all = vEntity;
- vEntity = next;
- }
- for ( viewEntity_t * vEntity = dynamics; vEntity != NULL; ) {
- viewEntity_t * next = vEntity->next;
- vEntity->next = all;
- all = vEntity;
- vEntity = next;
- }
- return all;
- }
- void R_ClearEntityDefDynamicModel( idRenderEntityLocal *def ) {
-
- for ( idInteraction *inter = def->firstInteraction; inter != NULL && !inter->IsEmpty(); inter = inter->entityNext ) {
- inter->FreeSurfaces();
- }
-
- if ( def->dynamicModel ) {
-
- def->dynamicModel = NULL;
- }
- def->dynamicModelFrameCount = 0;
- }
- bool R_IssueEntityDefCallback( idRenderEntityLocal *def ) {
- idBounds oldBounds = def->localReferenceBounds;
- def->archived = false;
- bool update;
- if ( tr.viewDef != NULL ) {
- update = def->parms.callback( &def->parms, &tr.viewDef->renderView );
- } else {
- update = def->parms.callback( &def->parms, NULL );
- }
- tr.pc.c_entityDefCallbacks++;
- if ( def->parms.hModel == NULL ) {
- common->Error( "R_IssueEntityDefCallback: dynamic entity callback didn't set model" );
- }
- if ( r_checkBounds.GetBool() ) {
- if ( oldBounds[0][0] > def->localReferenceBounds[0][0] + CHECK_BOUNDS_EPSILON ||
- oldBounds[0][1] > def->localReferenceBounds[0][1] + CHECK_BOUNDS_EPSILON ||
- oldBounds[0][2] > def->localReferenceBounds[0][2] + CHECK_BOUNDS_EPSILON ||
- oldBounds[1][0] < def->localReferenceBounds[1][0] - CHECK_BOUNDS_EPSILON ||
- oldBounds[1][1] < def->localReferenceBounds[1][1] - CHECK_BOUNDS_EPSILON ||
- oldBounds[1][2] < def->localReferenceBounds[1][2] - CHECK_BOUNDS_EPSILON ) {
- common->Printf( "entity %i callback extended reference bounds\n", def->index );
- }
- }
- return update;
- }
- idRenderModel *R_EntityDefDynamicModel( idRenderEntityLocal *def ) {
- if ( def->dynamicModelFrameCount == tr.frameCount ) {
- return def->dynamicModel;
- }
-
- bool callbackUpdate;
- if ( def->parms.callback != NULL ) {
- SCOPED_PROFILE_EVENT( "R_IssueEntityDefCallback" );
- callbackUpdate = R_IssueEntityDefCallback( def );
- } else {
- callbackUpdate = false;
- }
- idRenderModel *model = def->parms.hModel;
- if ( model == NULL ) {
- common->Error( "R_EntityDefDynamicModel: NULL model" );
- return NULL;
- }
- if ( model->IsDynamicModel() == DM_STATIC ) {
- def->dynamicModel = NULL;
- def->dynamicModelFrameCount = 0;
- return model;
- }
-
- if ( callbackUpdate || ( model->IsDynamicModel() == DM_CONTINUOUS && def->dynamicModelFrameCount != tr.frameCount ) ) {
- R_ClearEntityDefDynamicModel( def );
- }
-
- if ( def->dynamicModel == NULL ) {
- SCOPED_PROFILE_EVENT( "InstantiateDynamicModel" );
-
- def->cachedDynamicModel = model->InstantiateDynamicModel( &def->parms, tr.viewDef, def->cachedDynamicModel );
- if ( def->cachedDynamicModel != NULL && r_checkBounds.GetBool() ) {
- idBounds b = def->cachedDynamicModel->Bounds();
- if ( b[0][0] < def->localReferenceBounds[0][0] - CHECK_BOUNDS_EPSILON ||
- b[0][1] < def->localReferenceBounds[0][1] - CHECK_BOUNDS_EPSILON ||
- b[0][2] < def->localReferenceBounds[0][2] - CHECK_BOUNDS_EPSILON ||
- b[1][0] > def->localReferenceBounds[1][0] + CHECK_BOUNDS_EPSILON ||
- b[1][1] > def->localReferenceBounds[1][1] + CHECK_BOUNDS_EPSILON ||
- b[1][2] > def->localReferenceBounds[1][2] + CHECK_BOUNDS_EPSILON ) {
- common->Printf( "entity %i dynamic model exceeded reference bounds\n", def->index );
- }
- }
- def->dynamicModel = def->cachedDynamicModel;
- def->dynamicModelFrameCount = tr.frameCount;
- }
-
- if ( def->dynamicModel != NULL && model->DepthHack() != 0.0f && tr.viewDef != NULL ) {
- idPlane eye, clip;
- idVec3 ndc;
- R_TransformModelToClip( def->parms.origin, tr.viewDef->worldSpace.modelViewMatrix, tr.viewDef->projectionMatrix, eye, clip );
- R_TransformClipToDevice( clip, ndc );
- def->parms.modelDepthHack = model->DepthHack() * ( 1.0f - ndc.z );
- } else {
- def->parms.modelDepthHack = 0.0f;
- }
- return def->dynamicModel;
- }
- void R_SetupDrawSurfShader( drawSurf_t * drawSurf, const idMaterial * shader, const renderEntity_t * renderEntity ) {
- drawSurf->material = shader;
- drawSurf->sort = shader->GetSort();
-
- const float *constRegs = shader->ConstantRegisters();
- if ( likely( constRegs != NULL ) ) {
-
- drawSurf->shaderRegisters = constRegs;
- } else {
-
- const float * shaderParms = renderEntity->shaderParms;
-
-
-
-
- float generatedShaderParms[MAX_ENTITY_SHADER_PARMS];
- if ( unlikely( renderEntity->referenceShader != NULL ) ) {
-
- float refRegs[MAX_EXPRESSION_REGISTERS];
- renderEntity->referenceShader->EvaluateRegisters( refRegs, renderEntity->shaderParms,
- tr.viewDef->renderView.shaderParms,
- tr.viewDef->renderView.time[renderEntity->timeGroup] * 0.001f, renderEntity->referenceSound );
- const shaderStage_t * pStage = renderEntity->referenceShader->GetStage( 0 );
- memcpy( generatedShaderParms, renderEntity->shaderParms, sizeof( generatedShaderParms ) );
- generatedShaderParms[0] = refRegs[ pStage->color.registers[0] ];
- generatedShaderParms[1] = refRegs[ pStage->color.registers[1] ];
- generatedShaderParms[2] = refRegs[ pStage->color.registers[2] ];
- shaderParms = generatedShaderParms;
- }
-
- float * regs = (float *)R_FrameAlloc( shader->GetNumRegisters() * sizeof( float ), FRAME_ALLOC_SHADER_REGISTER );
- drawSurf->shaderRegisters = regs;
-
- shader->EvaluateRegisters( regs, shaderParms, tr.viewDef->renderView.shaderParms,
- tr.viewDef->renderView.time[renderEntity->timeGroup] * 0.001f, renderEntity->referenceSound );
- }
- }
- void R_SetupDrawSurfJoints( drawSurf_t * drawSurf, const srfTriangles_t * tri, const idMaterial * shader ) {
- if ( tri->staticModelWithJoints == NULL || !r_useGPUSkinning.GetBool() ) {
- drawSurf->jointCache = 0;
- return;
- }
- idRenderModelStatic * model = tri->staticModelWithJoints;
- assert( model->jointsInverted != NULL );
- if ( !vertexCache.CacheIsCurrent( model->jointsInvertedBuffer ) ) {
- const int alignment = glConfig.uniformBufferOffsetAlignment;
- model->jointsInvertedBuffer = vertexCache.AllocJoint( model->jointsInverted, ALIGN( model->numInvertedJoints * sizeof( idJointMat ), alignment ) );
- }
- drawSurf->jointCache = model->jointsInvertedBuffer;
- }
- void R_AddSingleModel( viewEntity_t * vEntity ) {
-
- vEntity->drawSurfs = NULL;
- vEntity->staticShadowVolumes = NULL;
- vEntity->dynamicShadowVolumes = NULL;
-
- const viewDef_t * viewDef = tr.viewDef;
- idRenderEntityLocal * entityDef = vEntity->entityDef;
- const renderEntity_t * renderEntity = &entityDef->parms;
- const idRenderWorldLocal * world = entityDef->world;
- if ( viewDef->isXraySubview && entityDef->parms.xrayIndex == 1 ) {
- return;
- } else if ( !viewDef->isXraySubview && entityDef->parms.xrayIndex == 2 ) {
- return;
- }
- SCOPED_PROFILE_EVENT( renderEntity->hModel == NULL ? "Unknown Model" : renderEntity->hModel->Name() );
-
- const float znear = ( viewDef->renderView.cramZNear ) ? ( r_znear.GetFloat() * 0.25f ) : r_znear.GetFloat();
-
- const bool modelIsVisible = !vEntity->scissorRect.IsEmpty();
- const bool addInteractions = modelIsVisible && ( !viewDef->isXraySubview || entityDef->parms.xrayIndex == 2 );
- const int entityIndex = entityDef->index;
-
-
-
-
-
-
-
-
- int numContactedLights = 0;
- static const int MAX_CONTACTED_LIGHTS = 128;
- viewLight_t * contactedLights[MAX_CONTACTED_LIGHTS];
- idInteraction * staticInteractions[MAX_CONTACTED_LIGHTS];
- if ( renderEntity->hModel == NULL ||
- renderEntity->hModel->ModelHasInteractingSurfaces() ||
- renderEntity->hModel->ModelHasShadowCastingSurfaces() ) {
- SCOPED_PROFILE_EVENT( "Find lights" );
- for ( viewLight_t * vLight = viewDef->viewLights; vLight != NULL; vLight = vLight->next ) {
- if ( vLight->scissorRect.IsEmpty() ) {
- continue;
- }
- if ( vLight->entityInteractionState != NULL ) {
-
- if ( vLight->entityInteractionState[entityIndex] == viewLight_t::INTERACTION_YES ) {
- contactedLights[numContactedLights] = vLight;
- staticInteractions[numContactedLights] = world->interactionTable[vLight->lightDef->index * world->interactionTableWidth + entityIndex];
- if ( ++numContactedLights == MAX_CONTACTED_LIGHTS ) {
- break;
- }
- }
- continue;
- }
- const idRenderLightLocal * lightDef = vLight->lightDef;
- if ( !lightDef->globalLightBounds.IntersectsBounds( entityDef->globalReferenceBounds ) ) {
- continue;
- }
- if ( R_CullModelBoundsToLight( lightDef, entityDef->localReferenceBounds, entityDef->modelRenderMatrix ) ) {
- continue;
- }
- if ( !modelIsVisible ) {
-
- if ( lightDef->areaNum != -1 ) {
-
-
- bool areasConnected = false;
- for ( areaReference_t *ref = entityDef->entityRefs; ref != NULL; ref = ref->ownerNext ) {
- if ( world->AreasAreConnected( lightDef->areaNum, ref->area->areaNum, PS_BLOCK_VIEW ) ) {
- areasConnected = true;
- break;
- }
- }
- if ( areasConnected == false ) {
-
- continue;
- }
- }
-
- idBounds shadowBounds;
- R_ShadowBounds( entityDef->globalReferenceBounds, lightDef->globalLightBounds, lightDef->globalLightOrigin, shadowBounds );
-
-
- if ( idRenderMatrix::CullBoundsToMVP( viewDef->worldSpace.mvp, shadowBounds ) ) {
- continue;
- }
- }
- contactedLights[numContactedLights] = vLight;
- staticInteractions[numContactedLights] = world->interactionTable[vLight->lightDef->index * world->interactionTableWidth + entityIndex];
- if ( ++numContactedLights == MAX_CONTACTED_LIGHTS ) {
- break;
- }
- }
- }
-
-
- if ( !modelIsVisible && numContactedLights == 0 ) {
- return;
- }
-
-
-
- idRenderModel * model = R_EntityDefDynamicModel( entityDef );
- if ( model == NULL || model->NumSurfaces() <= 0 ) {
- return;
- }
-
- if ( modelIsVisible ) {
- assert( !vEntity->scissorRect.IsEmpty() );
- if ( entityDef->decals != NULL && !r_skipDecals.GetBool() ) {
- entityDef->decals->CreateDeferredDecals( model );
- unsigned int numDrawSurfs = entityDef->decals->GetNumDecalDrawSurfs();
- for ( unsigned int i = 0; i < numDrawSurfs; i++ ) {
- drawSurf_t * decalDrawSurf = entityDef->decals->CreateDecalDrawSurf( vEntity, i );
- if ( decalDrawSurf != NULL ) {
- decalDrawSurf->linkChain = NULL;
- decalDrawSurf->nextOnLight = vEntity->drawSurfs;
- vEntity->drawSurfs = decalDrawSurf;
- }
- }
- }
- if ( entityDef->overlays != NULL && !r_skipOverlays.GetBool() ) {
- entityDef->overlays->CreateDeferredOverlays( model );
- unsigned int numDrawSurfs = entityDef->overlays->GetNumOverlayDrawSurfs();
- for ( unsigned int i = 0; i < numDrawSurfs; i++ ) {
- drawSurf_t * overlayDrawSurf = entityDef->overlays->CreateOverlayDrawSurf( vEntity, model, i );
- if ( overlayDrawSurf != NULL ) {
- overlayDrawSurf->linkChain = NULL;
- overlayDrawSurf->nextOnLight = vEntity->drawSurfs;
- vEntity->drawSurfs = overlayDrawSurf;
- }
- }
- }
- }
-
-
-
-
- vEntity->modelDepthHack = renderEntity->modelDepthHack;
- vEntity->weaponDepthHack = renderEntity->weaponDepthHack;
- vEntity->skipMotionBlur = renderEntity->skipMotionBlur;
- memcpy( vEntity->modelMatrix, entityDef->modelMatrix, sizeof( vEntity->modelMatrix ) );
- R_MatrixMultiply( entityDef->modelMatrix, viewDef->worldSpace.modelViewMatrix, vEntity->modelViewMatrix );
- idRenderMatrix viewMat;
- idRenderMatrix::Transpose( *(idRenderMatrix *)vEntity->modelViewMatrix, viewMat );
- idRenderMatrix::Multiply( viewDef->projectionRenderMatrix, viewMat, vEntity->mvp );
- if ( renderEntity->weaponDepthHack ) {
- idRenderMatrix::ApplyDepthHack( vEntity->mvp );
- }
- if ( renderEntity->modelDepthHack != 0.0f ) {
- idRenderMatrix::ApplyModelDepthHack( vEntity->mvp, renderEntity->modelDepthHack );
- }
-
-
- idVec3 localViewOrigin;
- R_GlobalPointToLocal( vEntity->modelMatrix, viewDef->renderView.vieworg, localViewOrigin );
-
-
-
- for ( int surfaceNum = 0; surfaceNum < model->NumSurfaces(); surfaceNum++ ) {
- const modelSurface_t *surf = model->Surface( surfaceNum );
-
- if ( r_singleSurface.GetInteger() >= 0 && surfaceNum != r_singleSurface.GetInteger() ) {
- continue;
- }
- srfTriangles_t * tri = surf->geometry;
- if ( tri == NULL ) {
- continue;
- }
- if ( tri->numIndexes == NULL ) {
- continue;
- }
- const idMaterial * shader = surf->shader;
- if ( shader == NULL ) {
- continue;
- }
- if ( !shader->IsDrawn() ) {
- continue;
- }
-
- if ( entityDef->parms.customShader != NULL ) {
-
-
- if ( shader->Deform() ) {
- continue;
- }
- shader = entityDef->parms.customShader;
- } else if ( entityDef->parms.customSkin ) {
- shader = entityDef->parms.customSkin->RemapShaderBySkin( shader );
- if ( shader == NULL ) {
- continue;
- }
- if ( !shader->IsDrawn() ) {
- continue;
- }
- }
-
- if ( tr.primaryRenderView.globalMaterial != NULL ) {
- shader = tr.primaryRenderView.globalMaterial;
- }
- SCOPED_PROFILE_EVENT( shader->GetName() );
-
- if ( r_checkBounds.GetBool() ) {
- for ( int j = 0; j < tri->numVerts; j++ ) {
- int k;
- for ( k = 0; k < 3; k++ ) {
- if ( tri->verts[j].xyz[k] > tri->bounds[1][k] + CHECK_BOUNDS_EPSILON
- || tri->verts[j].xyz[k] < tri->bounds[0][k] - CHECK_BOUNDS_EPSILON ) {
- common->Printf( "bad tri->bounds on %s:%s\n", entityDef->parms.hModel->Name(), shader->GetName() );
- break;
- }
- if ( tri->verts[j].xyz[k] > entityDef->localReferenceBounds[1][k] + CHECK_BOUNDS_EPSILON
- || tri->verts[j].xyz[k] < entityDef->localReferenceBounds[0][k] - CHECK_BOUNDS_EPSILON ) {
- common->Printf( "bad referenceBounds on %s:%s\n", entityDef->parms.hModel->Name(), shader->GetName() );
- break;
- }
- }
- if ( k != 3 ) {
- break;
- }
- }
- }
-
-
-
-
- const bool surfaceDirectlyVisible = modelIsVisible && !idRenderMatrix::CullBoundsToMVP( vEntity->mvp, tri->bounds );
- const bool gpuSkinned = ( tri->staticModelWithJoints != NULL && r_useGPUSkinning.GetBool() );
-
-
-
- drawSurf_t * baseDrawSurf = NULL;
- if ( surfaceDirectlyVisible ) {
-
- if ( !vertexCache.CacheIsCurrent( tri->indexCache ) ) {
- tri->indexCache = vertexCache.AllocIndex( tri->indexes, ALIGN( tri->numIndexes * sizeof( triIndex_t ), INDEX_CACHE_ALIGN ) );
- }
- if ( !vertexCache.CacheIsCurrent( tri->ambientCache ) ) {
-
- if ( shader->ReceivesLighting() && !tri->tangentsCalculated ) {
- assert( tri->staticModelWithJoints == NULL );
- R_DeriveTangents( tri );
- assert( false );
- }
- tri->ambientCache = vertexCache.AllocVertex( tri->verts, ALIGN( tri->numVerts * sizeof( idDrawVert ), VERTEX_CACHE_ALIGN ) );
- }
-
-
- baseDrawSurf = (drawSurf_t *)R_FrameAlloc( sizeof( *baseDrawSurf ), FRAME_ALLOC_DRAW_SURFACE );
- baseDrawSurf->frontEndGeo = tri;
- baseDrawSurf->space = vEntity;
- baseDrawSurf->scissorRect = vEntity->scissorRect;
- baseDrawSurf->extraGLState = 0;
- baseDrawSurf->renderZFail = 0;
- R_SetupDrawSurfShader( baseDrawSurf, shader, renderEntity );
-
- const deform_t shaderDeform = shader->Deform();
- if ( shaderDeform != DFRM_NONE ) {
- drawSurf_t * deformDrawSurf = R_DeformDrawSurf( baseDrawSurf );
- if ( deformDrawSurf != NULL ) {
-
- for ( drawSurf_t * surf = deformDrawSurf, * next = NULL; surf != NULL; surf = next ) {
- next = surf->nextOnLight;
- surf->linkChain = NULL;
- surf->nextOnLight = vEntity->drawSurfs;
- vEntity->drawSurfs = surf;
- }
- }
- }
-
-
- if ( shaderDeform == DFRM_NONE || shaderDeform == DFRM_PARTICLE || shaderDeform == DFRM_PARTICLE2 ) {
-
- if ( !vertexCache.CacheIsCurrent( tri->ambientCache ) ) {
- tri->ambientCache = vertexCache.AllocVertex( tri->verts, ALIGN( tri->numVerts * sizeof( tri->verts[0] ), VERTEX_CACHE_ALIGN ) );
- }
- if ( !vertexCache.CacheIsCurrent( tri->indexCache ) ) {
- tri->indexCache = vertexCache.AllocIndex( tri->indexes, ALIGN( tri->numIndexes * sizeof( tri->indexes[0] ), INDEX_CACHE_ALIGN ) );
- }
- R_SetupDrawSurfJoints( baseDrawSurf, tri, shader );
- baseDrawSurf->numIndexes = tri->numIndexes;
- baseDrawSurf->ambientCache = tri->ambientCache;
- baseDrawSurf->indexCache = tri->indexCache;
- baseDrawSurf->shadowCache = 0;
- baseDrawSurf->linkChain = NULL;
- baseDrawSurf->nextOnLight = vEntity->drawSurfs;
- vEntity->drawSurfs = baseDrawSurf;
- }
- }
-
-
-
- for ( int contactedLight = 0; contactedLight < numContactedLights; contactedLight++ ) {
- viewLight_t * vLight = contactedLights[contactedLight];
- const idRenderLightLocal * lightDef = vLight->lightDef;
- const idInteraction * interaction = staticInteractions[contactedLight];
-
- surfaceInteraction_t *surfInter = NULL;
- if ( interaction > INTERACTION_EMPTY && interaction->staticInteraction ) {
-
- assert( model->NumSurfaces() == interaction->numSurfaces );
- surfInter = &interaction->surfaces[surfaceNum];
- } else {
-
- if ( R_CullModelBoundsToLight( lightDef, tri->bounds, entityDef->modelRenderMatrix ) ) {
- continue;
- }
- }
-
-
- if ( shader->Spectrum() != lightDef->lightShader->Spectrum() ) {
- continue;
- }
-
-
- idVec3 localLightOrigin;
- R_GlobalPointToLocal( vEntity->modelMatrix, lightDef->globalLightOrigin, localLightOrigin );
-
-
-
- dynamicShadowVolumeParms_t * dynamicShadowParms = NULL;
- if ( addInteractions && surfaceDirectlyVisible && shader->ReceivesLighting() ) {
-
-
- if ( surfInter == NULL || surfInter->lightTrisIndexCache > 0 ) {
-
- drawSurf_t * lightDrawSurf = (drawSurf_t *)R_FrameAlloc( sizeof( *lightDrawSurf ), FRAME_ALLOC_DRAW_SURFACE );
- if ( surfInter != NULL ) {
-
- lightDrawSurf->numIndexes = surfInter->numLightTrisIndexes;
- lightDrawSurf->indexCache = surfInter->lightTrisIndexCache;
- } else {
-
- lightDrawSurf->numIndexes = tri->numIndexes;
- lightDrawSurf->indexCache = tri->indexCache;
-
- if ( r_cullDynamicLightTriangles.GetBool() ) {
- vertCacheHandle_t lightIndexCache = vertexCache.AllocIndex( NULL, ALIGN( lightDrawSurf->numIndexes * sizeof( triIndex_t ), INDEX_CACHE_ALIGN ) );
- if ( vertexCache.CacheIsCurrent( lightIndexCache ) ) {
- lightDrawSurf->indexCache = lightIndexCache;
- dynamicShadowParms = (dynamicShadowVolumeParms_t *)R_FrameAlloc( sizeof( dynamicShadowParms[0] ), FRAME_ALLOC_SHADOW_VOLUME_PARMS );
- dynamicShadowParms->verts = tri->verts;
- dynamicShadowParms->numVerts = tri->numVerts;
- dynamicShadowParms->indexes = tri->indexes;
- dynamicShadowParms->numIndexes = tri->numIndexes;
- dynamicShadowParms->silEdges = tri->silEdges;
- dynamicShadowParms->numSilEdges = tri->numSilEdges;
- dynamicShadowParms->joints = gpuSkinned ? tri->staticModelWithJoints->jointsInverted : NULL;
- dynamicShadowParms->numJoints = gpuSkinned ? tri->staticModelWithJoints->numInvertedJoints : 0;
- dynamicShadowParms->triangleBounds = tri->bounds;
- dynamicShadowParms->triangleMVP = vEntity->mvp;
- dynamicShadowParms->localLightOrigin = localLightOrigin;
- dynamicShadowParms->localViewOrigin = localViewOrigin;
- idRenderMatrix::Multiply( vLight->lightDef->baseLightProject, entityDef->modelRenderMatrix, dynamicShadowParms->localLightProject );
- dynamicShadowParms->zNear = znear;
- dynamicShadowParms->lightZMin = vLight->scissorRect.zmin;
- dynamicShadowParms->lightZMax = vLight->scissorRect.zmax;
- dynamicShadowParms->cullShadowTrianglesToLight = false;
- dynamicShadowParms->forceShadowCaps = false;
- dynamicShadowParms->useShadowPreciseInsideTest = false;
- dynamicShadowParms->useShadowDepthBounds = false;
- dynamicShadowParms->tempFacing = NULL;
- dynamicShadowParms->tempCulled = NULL;
- dynamicShadowParms->tempVerts = NULL;
- dynamicShadowParms->indexBuffer = NULL;
- dynamicShadowParms->shadowIndices = NULL;
- dynamicShadowParms->maxShadowIndices = 0;
- dynamicShadowParms->numShadowIndices = NULL;
- dynamicShadowParms->lightIndices = (triIndex_t *)vertexCache.MappedIndexBuffer( lightIndexCache );
- dynamicShadowParms->maxLightIndices = lightDrawSurf->numIndexes;
- dynamicShadowParms->numLightIndices = &lightDrawSurf->numIndexes;
- dynamicShadowParms->renderZFail = NULL;
- dynamicShadowParms->shadowZMin = NULL;
- dynamicShadowParms->shadowZMax = NULL;
- dynamicShadowParms->shadowVolumeState = & lightDrawSurf->shadowVolumeState;
- lightDrawSurf->shadowVolumeState = SHADOWVOLUME_UNFINISHED;
- dynamicShadowParms->next = vEntity->dynamicShadowVolumes;
- vEntity->dynamicShadowVolumes = dynamicShadowParms;
- }
- }
- }
- lightDrawSurf->ambientCache = tri->ambientCache;
- lightDrawSurf->shadowCache = 0;
- lightDrawSurf->frontEndGeo = tri;
- lightDrawSurf->space = vEntity;
- lightDrawSurf->material = shader;
- lightDrawSurf->extraGLState = 0;
- lightDrawSurf->scissorRect = vLight->scissorRect;
- lightDrawSurf->sort = 0.0f;
- lightDrawSurf->renderZFail = 0;
- lightDrawSurf->shaderRegisters = baseDrawSurf->shaderRegisters;
- R_SetupDrawSurfJoints( lightDrawSurf, tri, shader );
-
-
-
- if ( shader->Coverage() == MC_TRANSLUCENT ) {
- lightDrawSurf->linkChain = &vLight->translucentInteractions;
- } else if ( !lightDef->parms.noShadows && shader->TestMaterialFlag( MF_NOSELFSHADOW ) ) {
- lightDrawSurf->linkChain = &vLight->localInteractions;
- } else {
- lightDrawSurf->linkChain = &vLight->globalInteractions;
- }
- lightDrawSurf->nextOnLight = vEntity->drawSurfs;
- vEntity->drawSurfs = lightDrawSurf;
- }
- }
-
-
-
- if ( !shader->SurfaceCastsShadow() ) {
- continue;
- }
- if ( !lightDef->LightCastsShadows() ) {
- continue;
- }
- if ( tri->silEdges == NULL ) {
- continue;
- }
-
- if ( surfInter != NULL && surfInter->numShadowIndexes == 0 ) {
- continue;
- }
-
- if ( entityDef->parms.noShadow ) {
- continue;
- }
-
- if ( entityDef->parms.suppressShadowInLightID && entityDef->parms.suppressShadowInLightID == lightDef->parms.lightId ) {
- continue;
- }
- if ( lightDef->parms.prelightModel && lightDef->lightHasMoved == false &&
- entityDef->parms.hModel->IsStaticWorldModel() && !r_skipPrelightShadows.GetBool() ) {
-
-
- continue;
- }
-
-
-
- const bool forceShadowCaps = !addInteractions || r_forceShadowCaps.GetBool();
- drawSurf_t * shadowDrawSurf = (drawSurf_t *)R_FrameAlloc( sizeof( *shadowDrawSurf ), FRAME_ALLOC_DRAW_SURFACE );
- if ( surfInter != NULL ) {
- shadowDrawSurf->numIndexes = 0;
- shadowDrawSurf->indexCache = surfInter->shadowIndexCache;
- shadowDrawSurf->shadowCache = tri->shadowCache;
- shadowDrawSurf->scissorRect = vLight->scissorRect;
- shadowDrawSurf->shadowVolumeState = SHADOWVOLUME_DONE;
- if ( !r_skipStaticShadows.GetBool() ) {
- staticShadowVolumeParms_t * staticShadowParms = (staticShadowVolumeParms_t *)R_FrameAlloc( sizeof( staticShadowParms[0] ), FRAME_ALLOC_SHADOW_VOLUME_PARMS );
- staticShadowParms->verts = tri->staticShadowVertexes;
- staticShadowParms->numVerts = tri->numVerts * 2;
- staticShadowParms->indexes = surfInter->shadowIndexes;
- staticShadowParms->numIndexes = surfInter->numShadowIndexes;
- staticShadowParms->numShadowIndicesWithCaps = surfInter->numShadowIndexes;
- staticShadowParms->numShadowIndicesNoCaps = surfInter->numShadowIndexesNoCaps;
- staticShadowParms->triangleBounds = tri->bounds;
- staticShadowParms->triangleMVP = vEntity->mvp;
- staticShadowParms->localLightOrigin = localLightOrigin;
- staticShadowParms->localViewOrigin = localViewOrigin;
- staticShadowParms->zNear = znear;
- staticShadowParms->lightZMin = vLight->scissorRect.zmin;
- staticShadowParms->lightZMax = vLight->scissorRect.zmax;
- staticShadowParms->forceShadowCaps = forceShadowCaps;
- staticShadowParms->useShadowPreciseInsideTest = r_useShadowPreciseInsideTest.GetBool();
- staticShadowParms->useShadowDepthBounds = r_useShadowDepthBounds.GetBool();
- staticShadowParms->numShadowIndices = & shadowDrawSurf->numIndexes;
- staticShadowParms->renderZFail = & shadowDrawSurf->renderZFail;
- staticShadowParms->shadowZMin = & shadowDrawSurf->scissorRect.zmin;
- staticShadowParms->shadowZMax = & shadowDrawSurf->scissorRect.zmax;
- staticShadowParms->shadowVolumeState = & shadowDrawSurf->shadowVolumeState;
- shadowDrawSurf->shadowVolumeState = SHADOWVOLUME_UNFINISHED;
- staticShadowParms->next = vEntity->staticShadowVolumes;
- vEntity->staticShadowVolumes = staticShadowParms;
- }
- } else {
-
- if ( !vertexCache.CacheIsCurrent( tri->shadowCache ) ) {
- assert( !gpuSkinned );
-
-
-
-
- tri->shadowCache = vertexCache.AllocVertex( NULL, ALIGN( tri->numVerts * 2 * sizeof( idShadowVert ), VERTEX_CACHE_ALIGN ) );
- idShadowVert * shadowVerts = (idShadowVert *)vertexCache.MappedVertexBuffer( tri->shadowCache );
- idShadowVert::CreateShadowCache( shadowVerts, tri->verts, tri->numVerts );
- }
- const int maxShadowVolumeIndexes = tri->numSilEdges * 6 + tri->numIndexes * 2;
- shadowDrawSurf->numIndexes = 0;
- shadowDrawSurf->indexCache = vertexCache.AllocIndex( NULL, ALIGN( maxShadowVolumeIndexes * sizeof( triIndex_t ), INDEX_CACHE_ALIGN ) );
- shadowDrawSurf->shadowCache = tri->shadowCache;
- shadowDrawSurf->scissorRect = vLight->scissorRect;
- shadowDrawSurf->shadowVolumeState = SHADOWVOLUME_DONE;
-
- if ( vertexCache.CacheIsCurrent( shadowDrawSurf->indexCache ) && !r_skipDynamicShadows.GetBool() ) {
-
- if ( dynamicShadowParms == NULL ) {
- dynamicShadowParms = (dynamicShadowVolumeParms_t *)R_FrameAlloc( sizeof( dynamicShadowParms[0] ), FRAME_ALLOC_SHADOW_VOLUME_PARMS );
- } else {
-
- *dynamicShadowParms->shadowVolumeState = SHADOWVOLUME_DONE;
- }
- dynamicShadowParms->verts = tri->verts;
- dynamicShadowParms->numVerts = tri->numVerts;
- dynamicShadowParms->indexes = tri->indexes;
- dynamicShadowParms->numIndexes = tri->numIndexes;
- dynamicShadowParms->silEdges = tri->silEdges;
- dynamicShadowParms->numSilEdges = tri->numSilEdges;
- dynamicShadowParms->joints = gpuSkinned ? tri->staticModelWithJoints->jointsInverted : NULL;
- dynamicShadowParms->numJoints = gpuSkinned ? tri->staticModelWithJoints->numInvertedJoints : 0;
- dynamicShadowParms->triangleBounds = tri->bounds;
- dynamicShadowParms->triangleMVP = vEntity->mvp;
- dynamicShadowParms->localLightOrigin = localLightOrigin;
- dynamicShadowParms->localViewOrigin = localViewOrigin;
- idRenderMatrix::Multiply( vLight->lightDef->baseLightProject, entityDef->modelRenderMatrix, dynamicShadowParms->localLightProject );
- dynamicShadowParms->zNear = znear;
- dynamicShadowParms->lightZMin = vLight->scissorRect.zmin;
- dynamicShadowParms->lightZMax = vLight->scissorRect.zmax;
- dynamicShadowParms->cullShadowTrianglesToLight = r_cullDynamicShadowTriangles.GetBool();
- dynamicShadowParms->forceShadowCaps = forceShadowCaps;
- dynamicShadowParms->useShadowPreciseInsideTest = r_useShadowPreciseInsideTest.GetBool();
- dynamicShadowParms->useShadowDepthBounds = r_useShadowDepthBounds.GetBool();
- dynamicShadowParms->tempFacing = NULL;
- dynamicShadowParms->tempCulled = NULL;
- dynamicShadowParms->tempVerts = NULL;
- dynamicShadowParms->indexBuffer = NULL;
- dynamicShadowParms->shadowIndices = (triIndex_t *)vertexCache.MappedIndexBuffer( shadowDrawSurf->indexCache );
- dynamicShadowParms->maxShadowIndices = maxShadowVolumeIndexes;
- dynamicShadowParms->numShadowIndices = & shadowDrawSurf->numIndexes;
-
-
-
- dynamicShadowParms->renderZFail = & shadowDrawSurf->renderZFail;
- dynamicShadowParms->shadowZMin = & shadowDrawSurf->scissorRect.zmin;
- dynamicShadowParms->shadowZMax = & shadowDrawSurf->scissorRect.zmax;
- dynamicShadowParms->shadowVolumeState = & shadowDrawSurf->shadowVolumeState;
- shadowDrawSurf->shadowVolumeState = SHADOWVOLUME_UNFINISHED;
-
- if ( dynamicShadowParms->lightIndices == NULL ) {
- dynamicShadowParms->next = vEntity->dynamicShadowVolumes;
- vEntity->dynamicShadowVolumes = dynamicShadowParms;
- }
- tr.pc.c_createShadowVolumes++;
- }
- }
- assert( vertexCache.CacheIsCurrent( shadowDrawSurf->shadowCache ) );
- assert( vertexCache.CacheIsCurrent( shadowDrawSurf->indexCache ) );
- shadowDrawSurf->ambientCache = 0;
- shadowDrawSurf->frontEndGeo = NULL;
- shadowDrawSurf->space = vEntity;
- shadowDrawSurf->material = NULL;
- shadowDrawSurf->extraGLState = 0;
- shadowDrawSurf->sort = 0.0f;
- shadowDrawSurf->shaderRegisters = NULL;
- R_SetupDrawSurfJoints( shadowDrawSurf, tri, NULL );
-
- shadowDrawSurf->linkChain = shader->TestMaterialFlag( MF_NOSELFSHADOW ) ? &vLight->localShadows : &vLight->globalShadows;
- shadowDrawSurf->nextOnLight = vEntity->drawSurfs;
- vEntity->drawSurfs = shadowDrawSurf;
- }
- }
- }
- REGISTER_PARALLEL_JOB( R_AddSingleModel, "R_AddSingleModel" );
- void R_LinkDrawSurfToView( drawSurf_t * drawSurf, viewDef_t * viewDef ) {
-
- if ( viewDef->numDrawSurfs == viewDef->maxDrawSurfs ) {
- drawSurf_t **old = viewDef->drawSurfs;
- int count;
- if ( viewDef->maxDrawSurfs == 0 ) {
- viewDef->maxDrawSurfs = INITIAL_DRAWSURFS;
- count = 0;
- } else {
- count = viewDef->maxDrawSurfs * sizeof( viewDef->drawSurfs[0] );
- viewDef->maxDrawSurfs *= 2;
- }
- viewDef->drawSurfs = (drawSurf_t **)R_FrameAlloc( viewDef->maxDrawSurfs * sizeof( viewDef->drawSurfs[0] ), FRAME_ALLOC_DRAW_SURFACE_POINTER );
- memcpy( viewDef->drawSurfs, old, count );
- }
-
- viewDef->drawSurfs[viewDef->numDrawSurfs] = drawSurf;
- viewDef->numDrawSurfs++;
- }
- void R_AddModels() {
- SCOPED_PROFILE_EVENT( "R_AddModels" );
- tr.viewDef->viewEntitys = R_SortViewEntities( tr.viewDef->viewEntitys );
-
-
-
-
- if ( r_useParallelAddModels.GetBool() ) {
- for ( viewEntity_t * vEntity = tr.viewDef->viewEntitys; vEntity != NULL; vEntity = vEntity->next ) {
- tr.frontEndJobList->AddJob( (jobRun_t)R_AddSingleModel, vEntity );
- }
- tr.frontEndJobList->Submit();
- tr.frontEndJobList->Wait();
- } else {
- for ( viewEntity_t * vEntity = tr.viewDef->viewEntitys; vEntity != NULL; vEntity = vEntity->next ) {
- R_AddSingleModel( vEntity );
- }
- }
-
-
-
- if ( r_useParallelAddShadows.GetInteger() == 1 ) {
- for ( viewEntity_t * vEntity = tr.viewDef->viewEntitys; vEntity != NULL; vEntity = vEntity->next ) {
- for ( staticShadowVolumeParms_t * shadowParms = vEntity->staticShadowVolumes; shadowParms != NULL; shadowParms = shadowParms->next ) {
- tr.frontEndJobList->AddJob( (jobRun_t)StaticShadowVolumeJob, shadowParms );
- }
- for ( dynamicShadowVolumeParms_t * shadowParms = vEntity->dynamicShadowVolumes; shadowParms != NULL; shadowParms = shadowParms->next ) {
- tr.frontEndJobList->AddJob( (jobRun_t)DynamicShadowVolumeJob, shadowParms );
- }
- vEntity->staticShadowVolumes = NULL;
- vEntity->dynamicShadowVolumes = NULL;
- }
- tr.frontEndJobList->Submit();
-
- tr.frontEndJobList->Wait();
- } else {
- int start = Sys_Microseconds();
- for ( viewEntity_t * vEntity = tr.viewDef->viewEntitys; vEntity != NULL; vEntity = vEntity->next ) {
- for ( staticShadowVolumeParms_t * shadowParms = vEntity->staticShadowVolumes; shadowParms != NULL; shadowParms = shadowParms->next ) {
- StaticShadowVolumeJob( shadowParms );
- }
- for ( dynamicShadowVolumeParms_t * shadowParms = vEntity->dynamicShadowVolumes; shadowParms != NULL; shadowParms = shadowParms->next ) {
- DynamicShadowVolumeJob( shadowParms );
- }
- vEntity->staticShadowVolumes = NULL;
- vEntity->dynamicShadowVolumes = NULL;
- }
- int end = Sys_Microseconds();
- backEnd.pc.shadowMicroSec += end - start;
- }
-
-
-
- tr.viewDef->numDrawSurfs = 0;
- tr.viewDef->maxDrawSurfs = 0;
- for ( viewEntity_t * vEntity = tr.viewDef->viewEntitys; vEntity != NULL; vEntity = vEntity->next ) {
- for ( drawSurf_t * ds = vEntity->drawSurfs; ds != NULL; ) {
- drawSurf_t * next = ds->nextOnLight;
- if ( ds->linkChain == NULL ) {
- R_LinkDrawSurfToView( ds, tr.viewDef );
- } else {
- ds->nextOnLight = *ds->linkChain;
- *ds->linkChain = ds;
- }
- ds = next;
- }
- vEntity->drawSurfs = NULL;
- }
- }
|