Skip to content

Commit

Permalink
Pre-draw sprites
Browse files Browse the repository at this point in the history
  • Loading branch information
viciious committed Sep 7, 2023
1 parent 77b4c5b commit 1d13921
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 31 deletions.
7 changes: 3 additions & 4 deletions mars.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ void Mars_Sec_R_WallPrep(void) ATTR_DATA_CACHE_ALIGN;
void Mars_Sec_R_SegCommands(void) ATTR_DATA_CACHE_ALIGN;
void Mars_Sec_R_DrawPlanes(void) ATTR_DATA_CACHE_ALIGN;
void Mars_Sec_R_PreDrawPlanes(void) ATTR_DATA_CACHE_ALIGN;
void Mars_Sec_R_DrawSprites(int sprscreenhalf) ATTR_DATA_CACHE_ALIGN;
void Mars_Sec_R_DrawPSprites(int sprscreenhalf) ATTR_DATA_CACHE_ALIGN;
void Mars_Sec_R_PreDrawSprites(void) ATTR_DATA_CACHE_ALIGN;
void Mars_Sec_R_DrawSprites(void) ATTR_DATA_CACHE_ALIGN;
void Mars_Sec_P_CheckSights(void) ATTR_DATA_CACHE_ALIGN;
void Mars_Sec_wipe_doMelt(void);

Expand Down Expand Up @@ -125,10 +125,9 @@ static inline void Mars_R_EndDrawPlanes(void)
}

// r_phase8
static inline void Mars_R_BeginDrawSprites(int sprscreenhalf)
static inline void Mars_R_BeginDrawSprites(void)
{
Mars_R_SecWait();
MARS_SYS_COMM6 = sprscreenhalf;
MARS_SYS_COMM4 = MARS_SECCMD_R_DRAW_SPRITES;
}

Expand Down
3 changes: 2 additions & 1 deletion marsnew.c
Original file line number Diff line number Diff line change
Expand Up @@ -329,9 +329,10 @@ void Mars_Secondary(void)
break;
case MARS_SECCMD_R_DRAW_PLANES:
Mars_Sec_R_DrawPlanes();
Mars_Sec_R_PreDrawSprites();
break;
case MARS_SECCMD_R_DRAW_SPRITES:
Mars_Sec_R_DrawSprites(MARS_SYS_COMM6);
Mars_Sec_R_DrawSprites();
break;
case MARS_SECCMD_M_ANIMATE_FIRE:
Mars_Sec_M_AnimateFire();
Expand Down
2 changes: 2 additions & 0 deletions r_local.h
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,8 @@ __attribute__((aligned(16)))
/* */
viswall_t * volatile vissprites/*[MAXVISSPRITES]*/, * volatile lastsprite_p, * volatile vissprite_p;
int * volatile gsortedsprites;
int * volatile sortedsprites;
int sprscreenhalf;

/* */
/* planes */
Expand Down
7 changes: 5 additions & 2 deletions r_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ extern pixel_t *screens[2]; /* [viewportWidth*viewportHeight]; */
*/

static void R_Setup (int displayplayer, visplane_t *visplanes_,
visplane_t **visplanes_hash_, viswallextra_t *viswallex_, int *sortedplanes_)
visplane_t **visplanes_hash_, viswallextra_t *viswallex_, int *sortedplanes_, int *sortedsprites_)
{
int i;
int damagecount, bonuscount;
Expand Down Expand Up @@ -664,6 +664,8 @@ static void R_Setup (int displayplayer, visplane_t *visplanes_,

// re-use the openings array in VRAM
vd.gsortedsprites = (void *)(((intptr_t)vd.visplanes[1].open + 3) & ~3);
vd.sortedsprites = sortedsprites_;
vd.sprscreenhalf = 0xffff;

vd.vissprites = (void *)vd.viswalls;

Expand Down Expand Up @@ -886,11 +888,12 @@ void R_RenderPlayerView(int displayplayer)
__attribute__((aligned(16)))
visplane_t *visplanes_hash_[NUM_VISPLANES_BUCKETS];
int sortedplanes_[MAXVISPLANES+1];
int sortedsprites_[MAXVISSPRITES+1];
viswallextra_t viswallex_[MAXWALLCMDS + 1] __attribute__((aligned(16)));

t_total = I_GetFRTCounter();

R_Setup(displayplayer, visplanes_, visplanes_hash_, viswallex_, sortedplanes_);
R_Setup(displayplayer, visplanes_, visplanes_hash_, viswallex_, sortedplanes_, sortedsprites_);

Mars_R_BeginWallPrep(drawworld);

Expand Down
105 changes: 81 additions & 24 deletions r_phase8.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,13 @@ void R_DrawVisSprite(vissprite_t* vis, unsigned short* spropening, int *fuzzpos,
void R_ClipVisSprite(vissprite_t *vis, unsigned short *spropening, int sprscreenhalf, int16_t *walls) ATTR_DATA_CACHE_ALIGN;
static void R_DrawSortedSprites(int *fuzzpos, int* sortedsprites, int sprscreenhalf) ATTR_DATA_CACHE_ALIGN;
static void R_DrawPSprites(int *fuzzpos, int sprscreenhalf) ATTR_DATA_CACHE_ALIGN;
static void R_PreDrawSprites(void) ATTR_DATA_CACHE_ALIGN;
void R_Sprites(void) ATTR_DATA_CACHE_ALIGN __attribute__((noinline));

static char spr_lock = 0;
static int R_TryLockSpr(void) ATTR_DATA_CACHE_ALIGN;
static void R_UnlockSpr(void) ATTR_DATA_CACHE_ALIGN;

void R_DrawVisSprite(vissprite_t *vis, unsigned short *spropening, int *fuzzpos, int sprscreenhalf)
{
patch_t *patch;
Expand Down Expand Up @@ -342,37 +347,40 @@ static void R_DrawPSprites(int *fuzzpos, int sprscreenhalf)
}
}

#ifdef MARS
void Mars_Sec_R_DrawSprites(int sprscreenhalf)
static int R_TryLockSpr(void)
{
Mars_ClearCacheLine(&vd.vissprites);
Mars_ClearCacheLine(&vd.lastsprite_p);
Mars_ClearCacheLine(&vd.vissprite_p);
Mars_ClearCacheLine(&vd.gsortedsprites);

// mobj sprites
//Mars_ClearCacheLines(vd.gsortedsprites, ((lastsprite_p - vissprites + 1) * sizeof(*vd.gsortedsprites) + 31) / 16);
int res;

R_DrawSortedSprites(&fuzzpos[1], vd.gsortedsprites, -sprscreenhalf);
__asm volatile (\
"tas.b %1\n\t" \
"movt %0\n\t" \
: "=&r" (res) \
: "m" (spr_lock) \
);

R_DrawPSprites(&fuzzpos[1], -sprscreenhalf);
return res != 0;
}

#endif
static void R_UnlockSpr(void)
{
spr_lock = 0;
}

//
// Render all sprites
//
void R_Sprites(void)
static void R_PreDrawSprites(void)
{
int i = 0, count;
int half, sortedcount;
unsigned midcount;
viswall_t *spr;
int sortedsprites[MAXVISSPRITES+1];
int *sortedsprites = vd.sortedsprites;
viswall_t *wc;
vertex_t *verts;

Mars_ClearCacheLine(&vd.sprscreenhalf);
if (vd.sprscreenhalf != 0xffff) {
return;
}

sortedcount = 0;
count = vd.lastsprite_p - vd.vissprites;
if (count > MAXVISSPRITES)
Expand Down Expand Up @@ -448,24 +456,73 @@ void R_Sprites(void)
}
}

vd.sprscreenhalf = half;
}

//
// Render all sprites
//
void R_Sprites(void)
{
int i;
int sortedcount;

#ifdef MARS
if (R_TryLockSpr())
{
R_PreDrawSprites();
R_UnlockSpr();
}

Mars_R_SecWait();

Mars_ClearCacheLine(&vd.sprscreenhalf);
Mars_ClearCacheLine(&vd.sortedsprites[0]);
sortedcount = vd.sortedsprites[0];
Mars_ClearCacheLines(vd.sortedsprites + 1, (sortedcount * sizeof(*vd.sortedsprites) + 31) / 16);

for (i = 0; i < sortedcount+1; i++)
vd.gsortedsprites[i] = sortedsprites[i];
#endif
vd.gsortedsprites[i] = vd.sortedsprites[i];

#ifdef MARS
Mars_R_BeginDrawSprites(half);
Mars_R_BeginDrawSprites();
#endif

R_DrawSortedSprites(&fuzzpos[0], sortedsprites, half);
R_DrawSortedSprites(&fuzzpos[0], vd.gsortedsprites, vd.sprscreenhalf);

R_DrawPSprites(0, half);
R_DrawPSprites(&fuzzpos[0], vd.sprscreenhalf);

#ifdef MARS
Mars_R_EndDrawSprites();
#endif
}

// EOF
#ifdef MARS

void Mars_Sec_R_PreDrawSprites(void)
{
if (R_TryLockSpr())
{
R_PreDrawSprites();
R_UnlockSpr();
}
}

void Mars_Sec_R_DrawSprites(void)
{
Mars_ClearCacheLine(&vd.vissprites);
Mars_ClearCacheLine(&vd.lastsprite_p);
Mars_ClearCacheLine(&vd.vissprite_p);
Mars_ClearCacheLine(&vd.gsortedsprites);
Mars_ClearCacheLine(&vd.sprscreenhalf);

// mobj sprites
//Mars_ClearCacheLines(vd.gsortedsprites, ((lastsprite_p - vissprites + 1) * sizeof(*vd.gsortedsprites) + 31) / 16);

R_DrawSortedSprites(&fuzzpos[1], vd.gsortedsprites, -vd.sprscreenhalf);

R_DrawPSprites(&fuzzpos[1], -vd.sprscreenhalf);
}

#endif

// EOF

0 comments on commit 1d13921

Please sign in to comment.