Skip to content

Commit

Permalink
Some optimizations and render table into separate quad for t he bump …
Browse files Browse the repository at this point in the history
…animation.
  • Loading branch information
MaikelChan committed Oct 28, 2021
1 parent eb39711 commit 44806b1
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 135 deletions.
11 changes: 9 additions & 2 deletions SpaceCadetPinball/vshader.v.pica
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

; Uniforms
.fvec projection[4], modelView[4]
.fvec uvOffset

; Constants
.constf myconst(0.0, 1.0, -1.0, -0.5)
Expand Down Expand Up @@ -33,8 +34,14 @@
dp4 outpos.z, projection[2], r1
dp4 outpos.w, projection[3], r1

; outtex = intex
mov outtc0, intex
; r2 = uvOffset
mov r2, uvOffset
; r3 = (intex.xy * r2.zw) + r2.xy
mul r3.xy, intex.xy, r2.zw
add r3.xy, r3.xy, r2.xy

; outtex = r3
mov outtc0, r3

; We're finished
end
Expand Down
100 changes: 7 additions & 93 deletions SpaceCadetPinball/wii_graphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ C3D_RenderTarget *wii_graphics::target = nullptr;

DVLB_s *wii_graphics::vshader_dvlb = nullptr;
shaderProgram_s wii_graphics::program = {};
int wii_graphics::uLoc_projection = 0;
int wii_graphics::uLoc_modelView = 0;
int8_t wii_graphics::uLoc_projection = 0;
int8_t wii_graphics::uLoc_modelView = 0;
int8_t wii_graphics::uLoc_uvOffset = 0;
C3D_Mtx wii_graphics::projection = {};
void *wii_graphics::vbo_data = nullptr;

Expand Down Expand Up @@ -55,6 +56,7 @@ void wii_graphics::Initialize()

uLoc_projection = shaderInstanceGetUniformLocation(program.vertexShader, "projection");
uLoc_modelView = shaderInstanceGetUniformLocation(program.vertexShader, "modelView");
uLoc_uvOffset = shaderInstanceGetUniformLocation(program.vertexShader, "uvOffset");

// Configure attributes for use with the vertex shader

Expand Down Expand Up @@ -133,92 +135,19 @@ void wii_graphics::SetModelViewMatrix(float x, float y, float w, float h)
C3D_FVUnifMtx4x4(GPU_VERTEX_SHADER, uLoc_modelView, &modelView);
}

void wii_graphics::DrawQuad()
void wii_graphics::DrawQuad(float x, float y, float w, float h, float uvX, float uvY, float uvW, float uvH)
{
SetModelViewMatrix(x, y, w, h);
C3D_FVUnifSet(GPU_VERTEX_SHADER, uLoc_uvOffset, uvX, uvY, uvW, uvH);
C3D_DrawArrays(GPU_TRIANGLE_STRIP, 0, vertex_list_count);
}

// void wii_graphics::Load2DModelViewMatrix(uint32_t matrixIndex, float x, float y)
// {
// Mtx modelMatrix;
// guMtxIdentity(modelMatrix);
// guMtxTransApply(modelMatrix, modelMatrix, x, y, -5.0f);

// Mtx viewMatrix;
// guVector cam = {0.0F, 0.0F, 0.0F};
// guVector up = {0.0F, 1.0F, 0.0F};
// guVector look = {0.0F, 0.0F, -1.0F};
// guLookAt(viewMatrix, &cam, &up, &look);

// Mtx modelViewMatrix;
// guMtxConcat(viewMatrix, modelMatrix, modelViewMatrix);
// GX_LoadPosMtxImm(modelViewMatrix, matrixIndex);

// GX_SetCurrentMtx(matrixIndex);
// }

// uint32_t wii_graphics::Create2DQuadDisplayList(void *displayList, float top, float bottom, float left, float right, float uvTop, float uvBottom, float uvLeft, float uvRight)
// {
// // Configure vertex formats

// GX_ClearVtxDesc();
// GX_SetVtxDesc(GX_VA_POS, GX_DIRECT);
// //GX_SetVtxDesc(GX_VA_CLR0, GX_DIRECT);
// GX_SetVtxDesc(GX_VA_TEX0, GX_DIRECT);

// GX_SetVtxAttrFmt(GX_VTXFMT0, GX_VA_POS, GX_POS_XYZ, GX_F32, 0);
// //GX_SetVtxAttrFmt(GX_VTXFMT0, GX_VA_CLR0, GX_CLR_RGBA, GX_RGB8, 0);
// GX_SetVtxAttrFmt(GX_VTXFMT0, GX_VA_TEX0, GX_TEX_ST, GX_F32, 0);

// memset(displayList, 0, MAX_DISPLAY_LIST_SIZE);

// // Invalidate vertex cache

// GX_InvVtxCache();

// // It's necessary to flush the data cache of the display list
// // just before filling it.

// DCInvalidateRange(displayList, MAX_DISPLAY_LIST_SIZE);

// // Start generating the display list

// GX_BeginDispList(displayList, MAX_DISPLAY_LIST_SIZE);

// GX_Begin(GX_TRIANGLESTRIP, GX_VTXFMT0, 4);
// GX_Position3f32(left, top, 0.0f);
// //GX_Color3f32(1.0f, 0.0f, 0.0f);
// GX_TexCoord2f32(uvLeft, uvTop);

// GX_Position3f32(right, top, 0.0f);
// //GX_Color3f32(0.0f, 1.0f, 0.0f);
// GX_TexCoord2f32(uvRight, uvTop);

// GX_Position3f32(left, bottom, 0.0f);
// //GX_Color3f32(0.0f, 0.0f, 1.0f);
// GX_TexCoord2f32(uvLeft, uvBottom);

// GX_Position3f32(right, bottom, 0.0f);
// //GX_Color3f32(1.0f, 1.0f, 1.0f);
// GX_TexCoord2f32(uvRight, uvBottom);

// GX_End();

// return GX_EndDispList();
// }

// void wii_graphics::CallDisplayList(void *displayList, uint32_t displayListSize)
// {
// GX_CallDispList(displayList, displayListSize);
// }

void wii_graphics::CreateTextureObject(C3D_Tex *textureObject, uint16_t width, uint16_t height, GPU_TEXCOLOR format, GPU_TEXTURE_WRAP_PARAM wrap, GPU_TEXTURE_FILTER_PARAM filter)
{
bool success = C3D_TexInit(textureObject, width, height, format);

if (!success)
{
fopen("Error creating texture object.", "r");
std::string message = "Error creating texture object.";
svcOutputDebugString(message.c_str(), message.length());
}
Expand Down Expand Up @@ -274,18 +203,3 @@ uint32_t wii_graphics::GetTextureSize(uint16_t width, uint16_t height, GPU_TEXCO
size *= (uint32_t)width * height / 8;
return C3D_TexCalcTotalSize(size, maxLevel);
}

// void wii_graphics::FlushDataCache(void *startAddress, uint32_t size)
// {
// DCFlushRange(startAddress, size);
// }

// void wii_graphics::SwapBuffers()
// {
// GX_CopyDisp(frameBuffer[currentFramebuffer], GX_TRUE);
// GX_DrawDone();
// VIDEO_SetNextFramebuffer(frameBuffer[currentFramebuffer]);
// VIDEO_Flush();
// VIDEO_WaitVSync();
// currentFramebuffer ^= 1;
// }
12 changes: 4 additions & 8 deletions SpaceCadetPinball/wii_graphics.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ class wii_graphics

static DVLB_s *vshader_dvlb;
static shaderProgram_s program;
static int uLoc_projection;
static int uLoc_modelView;
static int8_t uLoc_projection;
static int8_t uLoc_modelView;
static int8_t uLoc_uvOffset;
static C3D_Mtx projection;
static void *vbo_data;

Expand All @@ -28,14 +29,9 @@ class wii_graphics
static bool IsMainLoopRunning();
static void SetOrthoProjectionMatrix(float left, float right, float bottom, float top, float near, float far);
static void SetModelViewMatrix(float x, float y, float w, float h);
static void DrawQuad();
// static void Load2DModelViewMatrix(uint32_t matrixIndex, float x, float y);
// static uint32_t Create2DQuadDisplayList(void *displayList, float top, float bottom, float left, float right, float uvTop, float uvBottom, float uvLeft, float uvRight);
// static void CallDisplayList(void *displayList, uint32_t displayListSize);
static void DrawQuad(float x, float y, float w, float h, float uvX, float uvY, float uvW, float uvH);
static void CreateTextureObject(C3D_Tex *textureObject, uint16_t width, uint16_t height, GPU_TEXCOLOR format, GPU_TEXTURE_WRAP_PARAM wrap, GPU_TEXTURE_FILTER_PARAM filter);
static void UploadTextureObject(C3D_Tex *textureObject, uint8_t *textureData);
static void BindTextureObject(C3D_Tex *textureObject, int32_t mapIndex);
static uint32_t GetTextureSize(uint16_t width, uint16_t height, GPU_TEXCOLOR format, int32_t maxLevel);
// static void FlushDataCache(void *startAddress, uint32_t size);
// static void SwapBuffers();
};
69 changes: 37 additions & 32 deletions SpaceCadetPinball/winmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ int winmain::WinMain(LPCSTR lpCmdLine)
uint16_t texHeight = 512;
uint32_t textureSize = wii_graphics::GetTextureSize(texWidth, texHeight, GPU_RGBA8, 0);
uint8_t *textureData = (uint8_t *)linearAlloc(textureSize);
memset(textureData, 0xFF, textureSize);
memset(textureData, 0, textureSize);
C3D_Tex textureObject;
wii_graphics::CreateTextureObject(&textureObject, texWidth, texHeight, GPU_RGBA8, GPU_CLAMP_TO_EDGE, GPU_LINEAR);
wii_graphics::BindTextureObject(&textureObject, 0);
Expand Down Expand Up @@ -203,36 +203,45 @@ int winmain::WinMain(LPCSTR lpCmdLine)

uint32_t dstOffset = 0;
uint32_t widthCount = 0;
constexpr uint32_t tileSize = 8;
constexpr uint32_t subTileSize1 = 4;
constexpr uint32_t subTileSize2 = 2;
uint32_t widthBytes = render::vscreen->Width * tileSize * 4;

for (int32_t y = 0; y < render::vscreen->Height; y += 8)
for (int32_t y = 0; y < render::vscreen->Height; y += tileSize)
{
for (int32_t x = 0; x < render::vscreen->Width; x += 8)
for (int32_t x = 0; x < render::vscreen->Width; x += tileSize)
{
for (uint8_t by1 = 0; by1 < 8; by1 += 4)
for (uint8_t ty = 0; ty < tileSize; ty += subTileSize1)
{
for (uint8_t bx1 = 0; bx1 < 8; bx1 += 4)
for (uint8_t tx = 0; tx < tileSize; tx += subTileSize1)
{
for (uint8_t by2 = 0; by2 < 4; by2 += 2)
for (uint8_t sty1 = 0; sty1 < subTileSize1; sty1 += subTileSize2)
{
for (uint8_t bx2 = 0; bx2 < 4; bx2 += 2)
for (uint8_t stx1 = 0; stx1 < subTileSize1; stx1 += subTileSize2)
{
for (uint8_t by3 = 0; by3 < 2; by3++)
for (uint8_t sty2 = 0; sty2 < subTileSize2; sty2++)
{
for (uint8_t bx3 = 0; bx3 < 2; bx3++)
uint32_t index = (y + ty + sty1 + sty2) * render::vscreen->Width + (x + tx + stx1);
Rgba color0 = render::vscreen->BmpBufPtr1[index + 0].rgba;
Rgba color1 = render::vscreen->BmpBufPtr1[index + 1].rgba;

textureData[dstOffset + 0] = color0.Alpha;
textureData[dstOffset + 1] = color0.Blue;
textureData[dstOffset + 2] = color0.Green;
textureData[dstOffset + 3] = color0.Red;

textureData[dstOffset + 4] = color1.Alpha;
textureData[dstOffset + 5] = color1.Blue;
textureData[dstOffset + 6] = color1.Green;
textureData[dstOffset + 7] = color1.Red;

dstOffset += 8;
widthCount += 8;
if (widthCount == widthBytes)
{
Rgba color = render::vscreen->BmpBufPtr1[(y + by1 + by2 + by3) * render::vscreen->Width + (x + bx1 + bx2 + bx3)].rgba;

textureData[dstOffset + 0] = color.Alpha;
textureData[dstOffset + 1] = color.Blue;
textureData[dstOffset + 2] = color.Green;
textureData[dstOffset + 3] = color.Red;
dstOffset += 4;
widthCount += 4;
if (widthCount == render::vscreen->Width * 8 * 4)
{
dstOffset += (texWidth - render::vscreen->Width) * 8 * 4;
widthCount = 0;
}
dstOffset += (texWidth - render::vscreen->Width) << 5;
widthCount = 0;
}
}
}
Expand All @@ -245,19 +254,15 @@ int winmain::WinMain(LPCSTR lpCmdLine)
wii_graphics::UploadTextureObject(&textureObject, textureData);
}

// Render fullscreen quad

// Update uniforms
// Render fullscreen quads

wii_graphics::BeginRender();

wii_graphics::SetModelViewMatrix(0, render::vscreen->Height - texHeight, texWidth, texHeight);
wii_graphics::DrawQuad();
// wii_graphics::Load2DModelViewMatrix(GX_PNMTX0, render::get_offset_x(), render::get_offset_y());
// wii_graphics::CallDisplayList(boardDisplayList, boardDisplayListSize);

// wii_graphics::Load2DModelViewMatrix(GX_PNMTX0, 0.0f, 0.0f);
// wii_graphics::CallDisplayList(sidebarDisplayList, sidebarDisplayListSize);
constexpr float separationX = 375;
float tableWidthCoefficient = separationX / texWidth;
float y = render::vscreen->Height - texHeight;
wii_graphics::DrawQuad(render::get_offset_x(), y - render::get_offset_y(), separationX, texHeight, 0.0, 0.0, tableWidthCoefficient, 1.0);
wii_graphics::DrawQuad(separationX, y, render::vscreen->Width - separationX, texHeight, tableWidthCoefficient, 0.0, (render::vscreen->Width - separationX) / texWidth, 1.0);

wii_graphics::FinishRender();
}
Expand Down

0 comments on commit 44806b1

Please sign in to comment.