|
| 1 | +// adapted from the original acube demo by tkcne. |
| 2 | + |
| 3 | +// enjoy |
| 4 | + |
| 5 | +#include <stdlib.h> |
| 6 | +#include <stdio.h> |
| 7 | +#include <string.h> |
| 8 | +#include <malloc.h> |
| 9 | +#include <math.h> |
| 10 | +#include <gccore.h> |
| 11 | +#include <wiiuse/wpad.h> |
| 12 | + |
| 13 | +GXRModeObj *screenMode; |
| 14 | +static void *frameBuffer; |
| 15 | +static vu8 readyForCopy; |
| 16 | +static u32 depthTest; |
| 17 | +#define FIFO_SIZE (256*1024) |
| 18 | + |
| 19 | +// Amount of pixels to poke z values to each frame. |
| 20 | +// Increase this value to speed up the test. |
| 21 | +#define WORKERS 1000 |
| 22 | + |
| 23 | +s16 vertices[] ATTRIBUTE_ALIGN(32) = { |
| 24 | + 0, 15, 0, |
| 25 | + -15, -15, 0, |
| 26 | + 15, -15, 0}; |
| 27 | + |
| 28 | +u8 colors[] ATTRIBUTE_ALIGN(32) = { |
| 29 | + 255, 0, 0, 255, // red |
| 30 | + 0, 255, 0, 255, // green |
| 31 | + 0, 0, 255, 255}; // blue |
| 32 | + |
| 33 | +void update_screen(Mtx viewMatrix); |
| 34 | +static void copy_buffers(u32 unused); |
| 35 | + |
| 36 | +int main(void) { |
| 37 | + Mtx view; |
| 38 | + Mtx projection; |
| 39 | + GXColor backgroundColor = {0, 0, 0, 255}; |
| 40 | + void *fifoBuffer = NULL; |
| 41 | + depthTest = 0; |
| 42 | + |
| 43 | + VIDEO_Init(); |
| 44 | + WPAD_Init(); |
| 45 | + |
| 46 | + screenMode = VIDEO_GetPreferredMode(NULL); |
| 47 | + |
| 48 | + frameBuffer = MEM_K0_TO_K1(SYS_AllocateFramebuffer(screenMode)); |
| 49 | + |
| 50 | + // Initialise the console, required for printf |
| 51 | + console_init(frameBuffer,20,20,screenMode->fbWidth,screenMode->xfbHeight,screenMode->fbWidth*VI_DISPLAY_PIX_SZ); |
| 52 | + |
| 53 | + VIDEO_Configure(screenMode); |
| 54 | + VIDEO_SetNextFramebuffer(frameBuffer); |
| 55 | + VIDEO_SetPostRetraceCallback(copy_buffers); |
| 56 | + VIDEO_SetBlack(FALSE); |
| 57 | + VIDEO_Flush(); |
| 58 | + |
| 59 | + |
| 60 | + |
| 61 | + fifoBuffer = MEM_K0_TO_K1(memalign(32,FIFO_SIZE)); |
| 62 | + memset(fifoBuffer, 0, FIFO_SIZE); |
| 63 | + |
| 64 | + GX_Init(fifoBuffer, FIFO_SIZE); |
| 65 | + GX_SetCopyClear(backgroundColor, 0x00ffffff); |
| 66 | + GX_SetViewport(0,0,screenMode->fbWidth,screenMode->efbHeight,0,1); |
| 67 | + GX_SetDispCopyYScale((f32)screenMode->xfbHeight/(f32)screenMode->efbHeight); |
| 68 | + GX_SetScissor(0,0,screenMode->fbWidth,screenMode->efbHeight); |
| 69 | + GX_SetDispCopySrc(0,0,screenMode->fbWidth,screenMode->efbHeight); |
| 70 | + GX_SetDispCopyDst(screenMode->fbWidth,screenMode->xfbHeight); |
| 71 | + GX_SetCopyFilter(screenMode->aa,screenMode->sample_pattern, GX_TRUE,screenMode->vfilter); |
| 72 | + GX_SetFieldMode(screenMode->field_rendering, ((screenMode->viHeight==2*screenMode->xfbHeight)?GX_ENABLE:GX_DISABLE)); |
| 73 | + |
| 74 | + GX_SetCullMode(GX_CULL_NONE); |
| 75 | + GX_CopyDisp(frameBuffer,GX_TRUE); |
| 76 | + GX_SetDispCopyGamma(GX_GM_1_0); |
| 77 | + |
| 78 | + guVector camera = {0.0F, 0.0F, 0.0F}; |
| 79 | + guVector up = {0.0F, 1.0F, 0.0F}; |
| 80 | + guVector look = {0.0F, 0.0F, -1.0F}; |
| 81 | + |
| 82 | + guPerspective(projection, 60, 1.33F, 10.0F, 300.0F); |
| 83 | + GX_LoadProjectionMtx(projection, GX_PERSPECTIVE); |
| 84 | + |
| 85 | + GX_ClearVtxDesc(); |
| 86 | + GX_SetVtxDesc(GX_VA_POS, GX_INDEX8); |
| 87 | + GX_SetVtxDesc(GX_VA_CLR0, GX_INDEX8); |
| 88 | + GX_SetVtxAttrFmt(GX_VTXFMT0, GX_VA_POS, GX_POS_XYZ, GX_S16, 0); |
| 89 | + GX_SetVtxAttrFmt(GX_VTXFMT0, GX_VA_CLR0, GX_CLR_RGBA, GX_RGBA8, 0); |
| 90 | + GX_SetArray(GX_VA_POS, vertices, 3*sizeof(s16)); |
| 91 | + GX_SetArray(GX_VA_CLR0, colors, 4*sizeof(u8)); |
| 92 | + GX_SetNumChans(1); |
| 93 | + GX_SetNumTexGens(0); |
| 94 | + GX_SetTevOrder(GX_TEVSTAGE0, GX_TEXCOORDNULL, GX_TEXMAP_NULL, GX_COLOR0A0); |
| 95 | + GX_SetTevOp(GX_TEVSTAGE0, GX_PASSCLR); |
| 96 | + |
| 97 | + while (1) |
| 98 | + { |
| 99 | + guLookAt(view, &camera, &up, &look); |
| 100 | + GX_SetViewport(0,0,screenMode->fbWidth,screenMode->efbHeight,0,1); |
| 101 | + GX_InvVtxCache(); |
| 102 | + GX_InvalidateTexAll(); |
| 103 | + update_screen(view); |
| 104 | + |
| 105 | + WPAD_ScanPads(); |
| 106 | + if (WPAD_ButtonsDown(0) & WPAD_BUTTON_HOME) exit(0); |
| 107 | + } |
| 108 | + return 0; |
| 109 | +} |
| 110 | + |
| 111 | +void update_screen( Mtx viewMatrix ) |
| 112 | +{ |
| 113 | + Mtx modelView; |
| 114 | + |
| 115 | + guMtxIdentity(modelView); |
| 116 | + guMtxTransApply(modelView, modelView, 0.0F, 0.0F, -50.0F); |
| 117 | + guMtxConcat(viewMatrix,modelView,modelView); |
| 118 | + |
| 119 | + GX_LoadPosMtxImm(modelView, GX_PNMTX0); |
| 120 | + |
| 121 | + GX_Begin(GX_TRIANGLES, GX_VTXFMT0, 3); |
| 122 | + |
| 123 | + GX_Position1x8(0); |
| 124 | + GX_Color1x8(0); |
| 125 | + GX_Position1x8(1); |
| 126 | + GX_Color1x8(1); |
| 127 | + GX_Position1x8(2); |
| 128 | + GX_Color1x8(2); |
| 129 | + |
| 130 | + GX_End(); |
| 131 | + |
| 132 | + GX_DrawDone(); |
| 133 | + readyForCopy = GX_TRUE; |
| 134 | + |
| 135 | + // The console understands VT terminal escape codes |
| 136 | + // This positions the cursor on row 2, column 0 |
| 137 | + // we can use variables for this with format codes too |
| 138 | + // e.g. printf ("\x1b[%d;%dH", row, column ); |
| 139 | + printf("\x1b[2;0H"); |
| 140 | + |
| 141 | + printf("Testing Z pokes: %d %%\n", (100 * depthTest) / 0xFFFFFF); |
| 142 | + |
| 143 | + // Poke z values |
| 144 | + u32 i; |
| 145 | + for (i = 0; i < WORKERS && depthTest + i <= 0xFFFFFF; i++) { |
| 146 | + u32 x = i % screenMode->fbWidth; |
| 147 | + u32 y = i / screenMode->fbWidth; |
| 148 | + GX_PokeZ(x, y, depthTest + i); |
| 149 | + } |
| 150 | + |
| 151 | + u32 z; |
| 152 | + for (i = 0; i < WORKERS && depthTest + i <= 0xFFFFFF; i++) { |
| 153 | + u32 x = i % screenMode->fbWidth; |
| 154 | + u32 y = i / screenMode->fbWidth; |
| 155 | + GX_PeekZ(x, y, &z); |
| 156 | + if (z != depthTest + i) { |
| 157 | + printf("The Z value is INACCURATE!\n"); |
| 158 | + printf("Z Poke: %d -> %d\n", depthTest + i, z); |
| 159 | + break; |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + if (depthTest > 0xFFFFFF) |
| 164 | + printf("The Z values are accurate.\n"); |
| 165 | + else |
| 166 | + depthTest += i; |
| 167 | + |
| 168 | + // Mark the location |
| 169 | + GXColor color = { 0, 0, 255, 255 }; |
| 170 | + GX_PokeARGB(320, 264, color); |
| 171 | + |
| 172 | + VIDEO_WaitVSync(); |
| 173 | + return; |
| 174 | +} |
| 175 | + |
| 176 | +static void copy_buffers(u32 count __attribute__ ((unused))) |
| 177 | +{ |
| 178 | + if (readyForCopy==GX_TRUE) { |
| 179 | + GX_PokeZMode(GX_TRUE, GX_ALWAYS, GX_TRUE); |
| 180 | + GX_SetColorUpdate(GX_TRUE); |
| 181 | + GX_CopyDisp(frameBuffer,GX_TRUE); |
| 182 | + GX_Flush(); |
| 183 | + readyForCopy = GX_FALSE; |
| 184 | + } |
| 185 | +} |
0 commit comments