|
| 1 | +/* |
| 2 | + * PSP Software Development Kit - https://github.com/pspdev |
| 3 | + * ----------------------------------------------------------------------- |
| 4 | + * Licensed under the BSD license, see LICENSE in PSPSDK root for details. |
| 5 | + * |
| 6 | + * PixelMask sample - Demonstrates sceGuPixelMask channel write-masking |
| 7 | + */ |
| 8 | + |
| 9 | +#include <pspkernel.h> |
| 10 | +#include <pspdisplay.h> |
| 11 | +#include <pspdebug.h> |
| 12 | +#include <stdlib.h> |
| 13 | +#include <stdio.h> |
| 14 | +#include <math.h> |
| 15 | +#include <string.h> |
| 16 | + |
| 17 | +#include <pspctrl.h> |
| 18 | +#include <pspgu.h> |
| 19 | +#include <psprtc.h> |
| 20 | + |
| 21 | +#include "../common/callbacks.h" |
| 22 | + |
| 23 | +PSP_MODULE_INFO("PixelMask Sample", 0, 1, 1); |
| 24 | +PSP_MAIN_THREAD_ATTR(THREAD_ATTR_USER); |
| 25 | + |
| 26 | +static unsigned int __attribute__((aligned(16))) list[1024]; |
| 27 | + |
| 28 | +#define BUF_WIDTH (512) |
| 29 | +#define SCR_WIDTH (480) |
| 30 | +#define SCR_HEIGHT (272) |
| 31 | + |
| 32 | +#define COLOR_BLACK 0x00000000 |
| 33 | + |
| 34 | +typedef struct PixelMaskMode |
| 35 | +{ |
| 36 | + const char *description; |
| 37 | + unsigned int mask; |
| 38 | +} PixelMaskMode; |
| 39 | + |
| 40 | +#define TEXTURE_FORMAT GU_PSM_8888 |
| 41 | +// In mask 0xAABBGGRR: 1-bits prevent writes to that bit, 0-bits allow writes. |
| 42 | +// Keep AA=0x00 so alpha writes remain enabled for all modes in this sample. |
| 43 | +static const PixelMaskMode modes[] = { |
| 44 | + {"Mask 0x00000000: All channels writable", 0x00000000}, |
| 45 | + {"Mask 0x0000FFFF: Only Blue (+Alpha) writable", 0x0000FFFF}, // R=0xFF, G=0xFF, B=0x00, A=0x00 |
| 46 | + {"Mask 0x00FF00FF: Only Green (+Alpha) writable", 0x00FF00FF}, // R=0xFF, G=0x00, B=0xFF, A=0x00 |
| 47 | + {"Mask 0x00FFFF00: Only Red (+Alpha) writable", 0x00FFFF00}, // R=0x00, G=0xFF, B=0xFF, A=0x00 |
| 48 | +}; |
| 49 | + |
| 50 | +// GU_PSM_8888, 8x8. Where the 2 first columns are red and the 2 last columns are green. |
| 51 | +static uint32_t __attribute__((aligned(16))) pixels[64] = { |
| 52 | + 0xFF0000FF, 0xFF0000FF, 0xFF00FF00, 0xFF00FF00, 0xFFFF0000, 0xFFFF0000, 0xFFFFFFFF, 0xFFFFFFF, |
| 53 | + 0xFF0000FF, 0xFF0000FF, 0xFF00FF00, 0xFF00FF00, 0xFFFF0000, 0xFFFF0000, 0xFFFFFFFF, 0xFFFFFFF, |
| 54 | + 0xFF0000FF, 0xFF0000FF, 0xFF00FF00, 0xFF00FF00, 0xFFFF0000, 0xFFFF0000, 0xFFFFFFFF, 0xFFFFFFF, |
| 55 | + 0xFF0000FF, 0xFF0000FF, 0xFF00FF00, 0xFF00FF00, 0xFFFF0000, 0xFFFF0000, 0xFFFFFFFF, 0xFFFFFFF, |
| 56 | + 0xFF0000FF, 0xFF0000FF, 0xFF00FF00, 0xFF00FF00, 0xFFFF0000, 0xFFFF0000, 0xFFFFFFFF, 0xFFFFFFF, |
| 57 | + 0xFF0000FF, 0xFF0000FF, 0xFF00FF00, 0xFF00FF00, 0xFFFF0000, 0xFFFF0000, 0xFFFFFFFF, 0xFFFFFFF, |
| 58 | + 0xFF0000FF, 0xFF0000FF, 0xFF00FF00, 0xFF00FF00, 0xFFFF0000, 0xFFFF0000, 0xFFFFFFFF, 0xFFFFFFF, |
| 59 | + 0xFF0000FF, 0xFF0000FF, 0xFF00FF00, 0xFF00FF00, 0xFFFF0000, 0xFFFF0000, 0xFFFFFFFF, 0xFFFFFFF, |
| 60 | +}; |
| 61 | + |
| 62 | +struct Vertex |
| 63 | +{ |
| 64 | + short u, v; |
| 65 | + short x, y, z; |
| 66 | +}; |
| 67 | + |
| 68 | +void blit() |
| 69 | +{ |
| 70 | + struct Vertex *vertices = (struct Vertex *)sceGuGetMemory(2 * sizeof(struct Vertex)); |
| 71 | + |
| 72 | + vertices[0].u = 0; |
| 73 | + vertices[0].v = 0; |
| 74 | + vertices[0].x = 0; |
| 75 | + vertices[0].y = 0; |
| 76 | + vertices[0].z = 0; |
| 77 | + |
| 78 | + vertices[1].u = 8; |
| 79 | + vertices[1].v = 8; |
| 80 | + vertices[1].x = SCR_WIDTH; |
| 81 | + vertices[1].y = SCR_HEIGHT; |
| 82 | + vertices[1].z = 0; |
| 83 | + |
| 84 | + sceGuDrawArray(GU_SPRITES, GU_TEXTURE_16BIT | GU_VERTEX_16BIT | GU_TRANSFORM_2D, 2, 0, vertices); |
| 85 | +} |
| 86 | + |
| 87 | +int main(int argc, char *argv[]) |
| 88 | +{ |
| 89 | + int mode_index = 0; |
| 90 | + int frame_counter = 0; |
| 91 | + int drawFormat = GU_PSM_5650; |
| 92 | + |
| 93 | + pspDebugScreenInit(); |
| 94 | + setupCallbacks(); |
| 95 | + |
| 96 | + // Setup GU |
| 97 | + |
| 98 | + void *fbp0 = guGetStaticVramBuffer(BUF_WIDTH, SCR_HEIGHT, drawFormat); |
| 99 | + void *fbp1 = guGetStaticVramBuffer(BUF_WIDTH, SCR_HEIGHT, drawFormat); |
| 100 | + |
| 101 | + sceGuInit(); |
| 102 | + |
| 103 | + sceGuStart(GU_DIRECT, list); |
| 104 | + sceGuDrawBuffer(drawFormat, fbp0, BUF_WIDTH); |
| 105 | + sceGuDispBuffer(SCR_WIDTH, SCR_HEIGHT, fbp1, BUF_WIDTH); |
| 106 | + sceGuOffset(2048 - (SCR_WIDTH / 2), 2048 - (SCR_HEIGHT / 2)); |
| 107 | + sceGuViewport(2048, 2048, SCR_WIDTH, SCR_HEIGHT); |
| 108 | + sceGuScissor(0, 0, SCR_WIDTH, SCR_HEIGHT); |
| 109 | + sceGuEnable(GU_SCISSOR_TEST); |
| 110 | + sceGuEnable(GU_TEXTURE_2D); |
| 111 | + sceGuClear(GU_COLOR_BUFFER_BIT | GU_DEPTH_BUFFER_BIT); |
| 112 | + |
| 113 | + sceGuTexMode(TEXTURE_FORMAT, 0, 0, 0); |
| 114 | + sceGuTexImage(0, 8, 8, 8, pixels); |
| 115 | + sceGuTexFunc(GU_TFX_REPLACE, GU_TCC_RGBA); // don't get influenced by any vertex colors |
| 116 | + sceGuTexFilter(GU_NEAREST, GU_NEAREST); // point-filtered sampling |
| 117 | + |
| 118 | + sceGuFinish(); |
| 119 | + sceGuSync(GU_SYNC_FINISH, GU_SYNC_WHAT_DONE); |
| 120 | + |
| 121 | + sceDisplayWaitVblankStart(); |
| 122 | + sceGuDisplay(GU_TRUE); |
| 123 | + |
| 124 | + sceKernelDcacheWritebackAll(); |
| 125 | + |
| 126 | + pspDebugScreenInitEx(fbp0, drawFormat, 1); |
| 127 | + |
| 128 | + while (running()) |
| 129 | + { |
| 130 | + sceGuStart(GU_DIRECT, list); |
| 131 | + |
| 132 | + sceGuClearColor(COLOR_BLACK); |
| 133 | + sceGuClear(GU_COLOR_BUFFER_BIT); |
| 134 | + |
| 135 | + // Apply current pixel mask and draw color bars |
| 136 | + sceGuPixelMask(modes[mode_index].mask); |
| 137 | + blit(); |
| 138 | + sceGuPixelMask(0); |
| 139 | + |
| 140 | + sceGuFinish(); |
| 141 | + sceGuSync(GU_SYNC_FINISH, GU_SYNC_WHAT_DONE); |
| 142 | + |
| 143 | + // HUD text |
| 144 | + pspDebugScreenSetOffset((int)fbp0); |
| 145 | + pspDebugScreenSetXY(0, 0); |
| 146 | + pspDebugScreenPrintf("sceGuPixelMask demo\n"); |
| 147 | + pspDebugScreenPrintf("%s\n", modes[mode_index].description); |
| 148 | + |
| 149 | + sceDisplayWaitVblankStart(); |
| 150 | + fbp0 = sceGuSwapBuffers(); |
| 151 | + |
| 152 | + // Rotate mode every ~2 seconds (assuming ~60 FPS) |
| 153 | + frame_counter++; |
| 154 | + if (frame_counter >= 120) |
| 155 | + { |
| 156 | + frame_counter = 0; |
| 157 | + mode_index = (mode_index + 1) % (sizeof(modes) / sizeof(modes[0])); |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + sceGuTerm(); |
| 162 | + |
| 163 | + sceKernelExitGame(); |
| 164 | + return 0; |
| 165 | +} |
0 commit comments