resonance 1.0.1
Loading...
Searching...
No Matches
GfxDevice Class Reference

Owner of the display, the GIF packet buffer, and the frame timing counters. More...

#include <gfxdevice.h>

Collaboration diagram for GfxDevice:

Classes

struct  Rect
 Rectangle of four floats. More...

Public Member Functions

 GfxDevice ()
 Construct the device with an empty packet and the default feedback and clear settings.
 ~GfxDevice ()
 Undo what Init() installs.
void Terminate ()
 Restore the base render class factories that Init() replaced.
void ResetVramAndSavePacket ()
 Release every video memory record and save the packet under construction.
void SavePacket ()
 Wait for the GIF and VIF1 channels to stop, then copy the packet into mSavedPacket.
void RestorePacket ()
 Copy mSavedPacket back over the packet buffer, for the length SavePacket() used.
void Init (int nWidth, int nHeight, int nBitDepth)
 Bring up the display and the drawing subsystems.
void BeginFrame ()
 Start a frame.
void PresentFrame (int nSwapBuffers)
 Finish a frame and show it.
void InitDisplayMode ()
 Program the GS display registers for the recorded geometry.
int GetReservedVramWords () const
 Words of video memory the display and depth buffers occupy.
void SetGsReg (int nReg, unsigned long long qwValue, unsigned long long qwMask)
 Set the masked part of one GS register, unless it already agrees.
void ReserveGifSpace (int nQuadwords)
 Divert writes into a region carved off the end of the packet buffer.
void SwapGifWrite ()
 Exchange the write pointer with the one the reservation preserved.
void FlushReservedGif ()
 Copy the reserved region into the main stream and close the reservation.
void CloseGifTag (int bEndOfPacket)
 Fill in the loop count of the open GIFtag, and optionally end the packet.
void WriteGifTag (const GifQuadword *pTag)
 Start a GIFtag at the write pointer.
int FlushGifPacket (int bRetainOpenTag, int bOnlyWhenFull)
 Send the assembled packet and move to the other scratchpad half.
void EnterVu1Path ()
 Route later packets through VIF1 and VU1 rather than straight to the GIF.
void LeaveVu1Path ()
 Return to sending packets straight to the GIF.
void RestoreFrameBufferTarget ()
 Point FRAME_1 and XYOFFSET_1 back at the display buffer being drawn.
void SetClearColor (const Color &color)
 Record the colour both halves of the double buffer clear to.
void DrawRenderStatsOverlay ()
 Draw the render statistics as debug text.
void DrawSubsystemTimingGraph (int nFullScaleMs)
 Draw one bar per profiler timer, scaled against a full-scale time.
void SetupGsDrawContext ()
 Blend the displayed frame back over the frame being drawn.
void FlipFrameBuffer ()
 Put the draw environment of the current half, then run SwapBuffers().

Public Attributes

GifQuadword * mpWrite
 Where the next quadword is written.
GifQuadword * mpBuffer
 Scratchpad half currently being filled.
GifQuadword * mpSavedWrite
 Write pointer preserved across a reservation.
GifQuadword * mpReservedRegion
 Start of the reserved region, or null when none is open.
std::vector< GifQuadword > mSavedPacket
 Copy of the packet under construction, filled by SavePacket().
GifQuadword * mpOpenTag
 GIFtag whose loop count is still to be filled in, or null when none is open.
int mnDisplayWidth
 Display width in pixels, as Init() recorded it.
int mnDisplayHeight
 Display height in pixels.
int mnPixelBytes
 Framebuffer bytes per pixel, the Init() bit depth divided by 8.
int mnDepthBytes
 Depth buffer bytes per sample, which InitDisplayMode() derives from mnPixelBytes.
GifQuadword mAdTag
 A+D GIFtag SetGsReg() opens when the open tag is not already an A+D tag.
unsigned long long mGsRegs [kGsRegisterCount]
 Last value written to each GS register, indexed by register number.
int mnSwapVblank
 Vertical blank count before which SwapBuffers() does not show the next frame.
int mnDrawBuffer
 Half of the double buffer being drawn, which RestoreFrameBufferTarget() selects by.
GsDoubleBuffer * mpDisplayBuffers
 Double buffer InitDisplayMode() builds at 0x006f36c0, addressed through its uncached alias.
int mnUseVu1
 Non-zero while geometry is submitted through VU1 rather than the software path.
GifQuadword * mpOpenVifDirect
 First quadword after the VIF DIRECT code that WriteGifTag() opened, or null.
int mFeedbackEnabled
 Non-zero while the feedback effect is enabled, stored as a whole word.
Rect mFeedbackRect
 Rectangle SetupGsDrawContext() draws and samples, in fractions of the display.
float mFeedbackAlpha
 Blend factor SetupGsDrawContext() writes into ALPHA_1 FIX after scaling it by 128.
int mFeedbackInset
 Inset of the texture window at both corners, in sixteenths of a texel.

Detailed Description

Owner of the display, the GIF packet buffer, and the frame timing counters.

The class is not polymorphic and has no RTTI. Its name is inferred from its methods. They build GIF packets, set GS registers, and flip the framebuffer.

Packets are assembled in PlayStation 2 scratchpad memory and double buffered between 0x70000000 and 0x70002000, the two halves of the 16 KiB region. Submitting a packet kicks a DMA channel at the half just filled and moves the write pointer to the other half. The next packet is therefore built while the previous one is in flight. The address given to the DMA controller is derived from the buffer base by moving bit 30 up to bit 31, the scratchpad flag.

Constructor & Destructor Documentation

◆ GfxDevice()

GfxDevice::GfxDevice ( )

Construct the device with an empty packet and the default feedback and clear settings.

mSavedPacket is sized to 0x200 zeroed quadwords, the size of one scratchpad half. The feedback rectangle covers the whole display at an alpha of 0.78, the clear colour is opaque black, and mAdTag becomes an A+D GIFtag with one register and no loops.

Ghidra address:
NTSC-U/C: 0x0049ac50
Ghidra address:
PAL: 0x004d8bd0

◆ ~GfxDevice()

GfxDevice::~GfxDevice ( )
inline

Undo what Init() installs.

The unit's static initialiser runs Terminate() on Rnd::ThePs and then destroys mSavedPacket, which identifies the destructor as inline around the call.

Member Function Documentation

◆ Terminate()

void GfxDevice::Terminate ( )

Restore the base render class factories that Init() replaced.

Every hook Init() pointed at a PlayStation 2 factory is pointed back at its base factory and the base class is registered again under the same key. Rnd::PsCam's teardown also destroys the default camera, and Rnd::PsEnviron's is called rather than inlined. The routine ends by calling Rnd::TheVRAM's destructor explicitly. The name is inferred.

Ghidra address:
NTSC-U/C: 0x0049afe0
Ghidra address:
PAL: 0x004d8ff8

◆ ResetVramAndSavePacket()

void GfxDevice::ResetVramAndSavePacket ( )

Release every video memory record and save the packet under construction.

MetSonyScreen calls it before handing the display to another renderer. The name is inferred.

Ghidra address:
NTSC-U/C: 0x0049fef0
Ghidra address:
PAL: 0x004ddf78

◆ SavePacket()

void GfxDevice::SavePacket ( )

Wait for the GIF and VIF1 channels to stop, then copy the packet into mSavedPacket.

The length copied is the whole quadwords between mpBuffer and mpWrite. The name is inferred.

Ghidra address:
NTSC-U/C: 0x0049ff28
Ghidra address:
PAL: 0x004ddfb0

◆ RestorePacket()

void GfxDevice::RestorePacket ( )

Copy mSavedPacket back over the packet buffer, for the length SavePacket() used.

InitDisplayMode() is the one caller. The name is inferred.

Ghidra address:
NTSC-U/C: 0x0049ff98
Ghidra address:
PAL: 0x004de020

◆ Init()

void GfxDevice::Init ( int nWidth,
int nHeight,
int nBitDepth )

Bring up the display and the drawing subsystems.

Labels g_profileTimers records 8 to 13 "setup", "vram", "billboard", "vert", "prim", and "sync", records the display geometry with mnPixelBytes as nBitDepth / 8, points the packet buffer at the first scratchpad half, and runs InitDisplayMode(). The PAL build instead rounds the width down to 640, 512, 384, or 256 and the height down to 512, 448, 256, or 224, chooses NTSC output for 448 and 224 and PAL output otherwise, and takes two bytes a pixel for a depth under 32 bits and four otherwise. It then installs the PlayStation 2 creators for meshes, cameras, materials, textures, environments, particle systems, and multi-meshes, and initialises Rnd::TheVRAM.

Parameters
nWidthThe display width in pixels.
nHeightThe display height in pixels.
nBitDepthThe framebuffer depth in bits.
Ghidra address:
NTSC-U/C: 0x0049ae20
Ghidra address:
PAL: 0x004d8da0

◆ BeginFrame()

void GfxDevice::BeginFrame ( )

Start a frame.

Runs SwapBuffers(), clears g_renderStats, draws Rnd::PsCam::sDefault, selects the default material, and starts the video memory frame. The timers of the frame just ended are copied into g_lastFrameProfileTimers, and every record of g_profileTimers then has its cycles and depth cleared.

Ghidra address:
NTSC-U/C: 0x0049b930
Ghidra address:
PAL: 0x004d9950

◆ PresentFrame()

void GfxDevice::PresentFrame ( int nSwapBuffers)

Finish a frame and show it.

Draws the feedback effect when mFeedbackEnabled is set, submits the packet, and ends the video memory frame. A swap then runs SwapBuffers() and inverts mnDrawBuffer once more.

Parameters
nSwapBuffersNon-zero to flip the framebuffer.
Ghidra address:
NTSC-U/C: 0x0049bac8
Ghidra address:
PAL: 0x004d9ae8

◆ InitDisplayMode()

void GfxDevice::InitDisplayMode ( )

Program the GS display registers for the recorded geometry.

Every register shadow is set to all ones (FOGCOL's to 1), and the GS, the DMA controller, and the display are reset for NTSC interlaced field output (in the PAL build, PAL output unless mnNtscOutput is set). The pixel depth selects the frame and depth buffer formats and mnDepthBytes; a depth other than 16, 24, or 32 bits reports "Unsupported video mode" and falls back to 32. The double buffer is then built and shown, the vertical blank handler installed, the saved packet restored, and the VU0 and VU1 microcode sent before VU1 runs its program at 0x3c0.

Ghidra address:
NTSC-U/C: 0x0049b138
Ghidra address:
PAL: 0x004d9150

◆ GetReservedVramWords()

int GfxDevice::GetReservedVramWords ( ) const

Words of video memory the display and depth buffers occupy.

Rnd::VRAM::Init() divides the result by the words in a block to find where the palette region begins. The depth buffer page, at 2048 words a page, locates the end of both frame buffers, and the display geometry at mnDepthBytes a pixel adds the depth buffer itself.

Returns
Words in use, at four bytes each.
Ghidra address:
NTSC-U/C: 0x0049fec0
Ghidra address:
PAL: 0x004ddf48

◆ SetGsReg()

void GfxDevice::SetGsReg ( int nReg,
unsigned long long qwValue,
unsigned long long qwMask )

Set the masked part of one GS register, unless it already agrees.

The device shadows every GS register in an array of 64-bit words, indexed by register number, and compares the masked incoming value against the shadow before emitting anything. A write that would not change the masked bits is dropped. Callers therefore pass a mask covering only the field they mean to set rather than the whole register.

A write that does change the register opens an A+D GIFtag from mAdTag unless the open tag is already one, and then submits the buffer if it has filled. Setting PRIM to a line strip, a triangle strip, or a triangle fan leaves the shadow's primitive type at 7, a value no caller sets. The next PRIM write therefore always reaches the GS and starts a new strip.

Parameters
nRegThe GS register number.
qwValueThe value to set, of which only the masked bits are used.
qwMaskThe bits of the register this call owns.
Ghidra address:
NTSC-U/C: 0x0049ffd0
Ghidra address:
PAL: 0x004de058

◆ ReserveGifSpace()

void GfxDevice::ReserveGifSpace ( int nQuadwords)

Divert writes into a region carved off the end of the packet buffer.

The region begins where the requested number of quadwords ends the buffer. The current write pointer is preserved for SwapGifWrite() to return to. A second reservation while one is open does nothing, and the whole routine does nothing while the software path is selected.

A caller reserves space, programs its registers into the region, swaps back to the main stream to append the bulk data, and finally splices the region in with FlushReservedGif(). That order exists because the register writes are only known to be needed once the bulk data has been measured.

Parameters
nQuadwordsThe space to reserve.
Ghidra address:
NTSC-U/C: 0x0049fd98
Ghidra address:
PAL: 0x004dde20

◆ SwapGifWrite()

void GfxDevice::SwapGifWrite ( )

Exchange the write pointer with the one the reservation preserved.

Calling it twice returns to where it started. That is how a caller alternates between the reserved region and the main stream. It does nothing unless a reservation is open.

Ghidra address:
NTSC-U/C: 0x0049fe08
Ghidra address:
PAL: 0x004dde90

◆ FlushReservedGif()

void GfxDevice::FlushReservedGif ( )

Copy the reserved region into the main stream and close the reservation.

The quadword count comes from how far the region's own write pointer advanced. Nothing is copied when the region is empty, and the reservation is closed either way.

Ghidra address:
NTSC-U/C: 0x0049fe38
Ghidra address:
PAL: 0x004ddec0

◆ CloseGifTag()

void GfxDevice::CloseGifTag ( int bEndOfPacket)

Fill in the loop count of the open GIFtag, and optionally end the packet.

The count is the quadwords written after the tag divided by the registers each loop consumes. That divisor is the tag's own NREG shifted right by its FLG. The same expression is correct for all three formats. A packed loop spends one quadword per register, a register list spends one per two, and an image loop spends one per four.

Ending the packet on the VU1 path also fills in the count of the open VIF DIRECT code.

Parameters
bEndOfPacketNon-zero to set the tag's end-of-packet bit.
Ghidra address:
NTSC-U/C: 0x004a0158
Ghidra address:
PAL: 0x004de1e0

◆ WriteGifTag()

void GfxDevice::WriteGifTag ( const GifQuadword * pTag)

Start a GIFtag at the write pointer.

The open tag is closed first without the end-of-packet bit. On the VU1 path a VIF DIRECT code goes ahead of the tag unless one is already open.

Parameters
pTagThe tag to write, whose loop count CloseGifTag() fills in later.
Ghidra address:
NTSC-U/C: 0x0049b5a8
Ghidra address:
PAL: 0x004d95c8

◆ FlushGifPacket()

int GfxDevice::FlushGifPacket ( int bRetainOpenTag,
int bOnlyWhenFull )

Send the assembled packet and move to the other scratchpad half.

Closes the open tag with the end-of-packet bit, then kicks VIF1 while the VU1 path is selected and the GIF otherwise. An empty buffer is not sent. Retaining the open tag reopens it in the new half, letting a caller submit in the middle of a primitive.

Every submission advances the video memory cache to its next lock generation and then runs the long operation poll callback through RunLongOperationPollProc().

Parameters
bRetainOpenTagNon-zero to reopen the current tag in the new buffer half.
bOnlyWhenFullNon-zero to submit only once the buffer is full.
Returns
Non-zero when a packet was sent.
Ghidra address:
NTSC-U/C: 0x0049b478
Ghidra address:
PAL: 0x004d9498

◆ EnterVu1Path()

void GfxDevice::EnterVu1Path ( )

Route later packets through VIF1 and VU1 rather than straight to the GIF.

Does nothing when the VU1 path is already selected. Otherwise it submits whatever the buffer has before switching, because a packet built for one path cannot be sent down the other.

Ghidra address:
NTSC-U/C: 0x004a0348
Ghidra address:
PAL: 0x004de3d0

◆ LeaveVu1Path()

void GfxDevice::LeaveVu1Path ( )

Return to sending packets straight to the GIF.

Does nothing unless the VU1 path is selected. Otherwise it ends the open tag, submits the buffer, and waits for the GS paths to drain before switching.

Ghidra address:
NTSC-U/C: 0x0049b838
Ghidra address:
PAL: 0x004d9858

◆ RestoreFrameBufferTarget()

void GfxDevice::RestoreFrameBufferTarget ( )

Point FRAME_1 and XYOFFSET_1 back at the display buffer being drawn.

Rnd::PsCam::DrawShowing() calls it when the previous camera drew into a texture. Both values come from the draw environment of the half mnDrawBuffer selects, and the binary open-codes the body of SetGsReg() for each.

Ghidra address:
NTSC-U/C: 0x0049b6b8
Ghidra address:
PAL: 0x004d96d8

◆ SetClearColor()

void GfxDevice::SetClearColor ( const Color & color)

Record the colour both halves of the double buffer clear to.

Stores the colour in mClearColor and writes it into the RGBAQ pair of each half's clear, red, green, and blue scaled by 255, alpha by 128, and Q at 1.0. The data cache is then written back, because the double buffer is read through its uncached alias.

Parameters
colorThe clear colour, each component from 0 to 1.
Ghidra address:
NTSC-U/C: 0x0049b368
Ghidra address:
PAL: 0x004d9388

◆ DrawRenderStatsOverlay()

void GfxDevice::DrawRenderStatsOverlay ( )

Draw the render statistics as debug text.

One line each for the frame rate and for every RenderStats counter, then the kilobytes of texture the VRAM table uploaded in the last frame.

Ghidra address:
NTSC-U/C: 0x0049bec8
Ghidra address:
PAL: 0x004d9ee8

◆ DrawSubsystemTimingGraph()

void GfxDevice::DrawSubsystemTimingGraph ( int nFullScaleMs)

Draw one bar per profiler timer, scaled against a full-scale time.

Parameters
nFullScaleMsMilliseconds a bar spanning 95 per cent of the display width stands for.
Ghidra address:
NTSC-U/C: 0x0049c778
Ghidra address:
PAL: 0x004da798

◆ SetupGsDrawContext()

void GfxDevice::SetupGsDrawContext ( )

Blend the displayed frame back over the frame being drawn.

Disables the depth test and depth writes, sets ALPHA_1 to blend by mFeedbackAlpha, and binds the frame buffer of the half not being drawn as a 1024 by 1024 texture. It then draws one sprite over mFeedbackRect that samples the same rectangle of the texture, inset by mFeedbackInset at both corners. The title covers only the register setup, although the routine also draws. Renderer::Draw() calls it while g_nLsdMode is set.

Ghidra address:
NTSC-U/C: 0x0049ccb8
Ghidra address:
PAL: 0x004dacd8

◆ FlipFrameBuffer()

void GfxDevice::FlipFrameBuffer ( )

Put the draw environment of the current half, then run SwapBuffers().

InitDisplayMode() and MetLoadGameScreen::OnFadeInDone() at 0x0028e118 are the callers.

Ghidra address:
NTSC-U/C: 0x004a0388
Ghidra address:
PAL: 0x004de410

Member Data Documentation

◆ mpWrite

GifQuadword* GfxDevice::mpWrite

Where the next quadword is written.

◆ mpBuffer

GifQuadword* GfxDevice::mpBuffer

Scratchpad half currently being filled.

◆ mpSavedWrite

GifQuadword* GfxDevice::mpSavedWrite

Write pointer preserved across a reservation.

◆ mpReservedRegion

GifQuadword* GfxDevice::mpReservedRegion

Start of the reserved region, or null when none is open.

◆ mSavedPacket

std::vector<GifQuadword> GfxDevice::mSavedPacket

Copy of the packet under construction, filled by SavePacket().

+0x10

◆ mpOpenTag

GifQuadword* GfxDevice::mpOpenTag

GIFtag whose loop count is still to be filled in, or null when none is open.

◆ mnDisplayWidth

int GfxDevice::mnDisplayWidth

Display width in pixels, as Init() recorded it.

Rnd::PsCam::ScreenToPixels() reads it.

◆ mnDisplayHeight

int GfxDevice::mnDisplayHeight

Display height in pixels.

Read alongside the width by the same routine.

◆ mnPixelBytes

int GfxDevice::mnPixelBytes

Framebuffer bytes per pixel, the Init() bit depth divided by 8.

+0x28

◆ mnDepthBytes

int GfxDevice::mnDepthBytes

Depth buffer bytes per sample, which InitDisplayMode() derives from mnPixelBytes.

Rnd::PsCam::DrawShowing() reads it to find the largest depth value. +0x2c

◆ mAdTag

GifQuadword GfxDevice::mAdTag

A+D GIFtag SetGsReg() opens when the open tag is not already an A+D tag.

+0x30

◆ mGsRegs

unsigned long long GfxDevice::mGsRegs[kGsRegisterCount]

Last value written to each GS register, indexed by register number.

+0x40

◆ mnSwapVblank

int GfxDevice::mnSwapVblank

Vertical blank count before which SwapBuffers() does not show the next frame.

InitDisplayMode() and SwapBuffers() set it to one past the current count. +0x440

◆ mnDrawBuffer

int GfxDevice::mnDrawBuffer

Half of the double buffer being drawn, which RestoreFrameBufferTarget() selects by.

+0x444

◆ mpDisplayBuffers

GsDoubleBuffer* GfxDevice::mpDisplayBuffers

Double buffer InitDisplayMode() builds at 0x006f36c0, addressed through its uncached alias.

GetReservedVramWords() reads the depth buffer page from it. +0x448

◆ mnUseVu1

int GfxDevice::mnUseVu1

Non-zero while geometry is submitted through VU1 rather than the software path.

+0x44c

◆ mpOpenVifDirect

GifQuadword* GfxDevice::mpOpenVifDirect

First quadword after the VIF DIRECT code that WriteGifTag() opened, or null.

On the VU1 path every run of GIF data travels inside a DIRECT code, whose immediate is the quadword count that follows it. WriteGifTag() writes the code with a count of zero, and CloseGifTag() with the end-of-packet bit fills the count in from this pointer. +0x450

◆ mFeedbackEnabled

int GfxDevice::mFeedbackEnabled

Non-zero while the feedback effect is enabled, stored as a whole word.

Written directly, with no accessor, by the panel's count-in component at 0x0041bc50, which sets and clears it during the song intro, and cleared by Overlay's destructor at 0x0041daec. +0x468

◆ mFeedbackRect

Rect GfxDevice::mFeedbackRect

Rectangle SetupGsDrawContext() draws and samples, in fractions of the display.

Renderer::Draw() writes it directly, with no accessor, before each call. +0x46c

◆ mFeedbackAlpha

float GfxDevice::mFeedbackAlpha

Blend factor SetupGsDrawContext() writes into ALPHA_1 FIX after scaling it by 128.

Renderer::Draw() writes it directly. +0x47c

◆ mFeedbackInset

int GfxDevice::mFeedbackInset

Inset of the texture window at both corners, in sixteenths of a texel.

Renderer::Draw() writes it directly. +0x480


The documentation for this class was generated from the following file: