📄 vertex.c
字号:
/* * PSP Software Development Kit - http://www.pspdev.org * ----------------------------------------------------------------------- * Licensed under the BSD license, see LICENSE in PSPSDK root for details. * * Copyright (c) 2005 Jesper Svennevid */#include <pspkernel.h>#include <pspdisplay.h>#include <pspdebug.h>#include <pspctrl.h>#include <psprtc.h>#include <stdlib.h>#include <stdio.h>#include <math.h>#include <string.h>#include <pspgu.h>#include <pspgum.h>#include "../common/callbacks.h"#include "../common/vram.h"#include "../common/menu.h"PSP_MODULE_INFO("Vertex Speed Sample", 0, 1, 1);PSP_MAIN_THREAD_ATTR(THREAD_ATTR_USER);static unsigned int __attribute__((aligned(16))) list[262144];#define BUF_WIDTH (512)#define SCR_WIDTH (480)#define SCR_HEIGHT (272)// TODO: fix alignmentsint getVertexSize(int vertexFormat){ enum { SIZE, WEIGHTS, VERTEX, }; static int vertexValues[] = { 4, 0, GU_TEXTURE_BITS, SIZE, 2, 0, 1, 2, 4, 4, 9, GU_WEIGHT_BITS, WEIGHTS, 1, 0, 1, 2, 4, 8, 2, GU_COLOR_BITS, SIZE, 1, 0, 0, 0, 0, 2, 2, 2, 4, 4, 5, GU_NORMAL_BITS, SIZE, 3, 0, 1, 2, 4, 4, 7, GU_VERTEX_BITS, SIZE, 3, 0, 1, 2, 4, 0, 11, GU_VERTICES_BITS, VERTEX, 0, 0, 0, 0, }; int* current = vertexValues; int size = 0; while (current[0] || current[1] || current[2]) { int currentCount = current[0]; int currentShift = current[1]; int currentBits = current[2]; int mode = current[3]; int numElements = current[4]; current += 5; switch (mode) { case SIZE: { int elementSize = current[(vertexFormat & currentBits) >> currentShift]; // align if (elementSize > 0) size = (size + (elementSize-1)) & ~(elementSize-1); size += elementSize * numElements; } break; case WEIGHTS: { int elementSize = current[(vertexFormat & currentBits) >> currentShift]; int numWeights = ((vertexFormat & GU_WEIGHTS_BITS) >> 14)+1; if (elementSize > 0) size = (size + (elementSize-1)) & ~(elementSize-1); if (numWeights > 1) size += numWeights * elementSize; } break; case VERTEX: { int numVertices = ((vertexFormat & GU_VERTICES_BITS) >> 18); if (size > 0) size = (size + (4-1)) & ~(4-1); size += size * numVertices; } break; } current += currentCount; } // align to size of first element? if (size > 0) size = (size + (4-1)) & ~(4-1); return size;}void generateVertexBuffer(int vertexFormat, void* vertices, int batchSize){ int vertexSize = getVertexSize(vertexFormat);// int textureOffset = 0; int colorOffset = getVertexSize(vertexFormat & (GU_TEXTURE_BITS));// int normalOffset = getVertexSize(vertexFormat & (GU_TEXTURE_BITS|GU_COLOR_BITS)); int vertexOffset = getVertexSize(vertexFormat & (GU_TEXTURE_BITS|GU_COLOR_BITS|GU_NORMAL_BITS)); float batchScale = 1.0f / batchSize; int i; char* current = vertices; for (i = 0; i < batchSize; ++i) { memset(current,0,vertexSize); // TODO: render something nice/* float x = cosf((i * batchScale) * (GU_PI*2)); float y = sinf((i * batchScale) * (GU_PI*2)); switch (vertexFormat & GU_COLOR_BITS) { case GU_COLOR_5650: case GU_COLOR_5551: case GU_COLOR_4444: { unsigned short* color = (unsigned short*)(current + colorOffset); *color = i; } break; case GU_COLOR_8888: { unsigned int* color = (unsigned int*)(current + colorOffset); *color = i; } break; } switch (vertexFormat & GU_VERTEX_BITS) { case GU_VERTEX_8BIT: { char* vertex = (char*)(current + vertexOffset); vertex[0] = x * 127.5f; vertex[1] = y * 127.5f; } break; case GU_VERTEX_16BIT: { short* vertex = (short*)(current + vertexOffset); vertex[0] = x * 32767.5f; vertex[1] = y * 32767.5f; } break; case GU_VERTEX_32BITF: { float* vertex = (float*)(current + vertexOffset); vertex[0] = x; vertex[1] = y; } break; } */ current = current + vertexSize; }}typedef enum{ NoAction, TextureMenu, ColorMenu, NormalMenu, VertexMenu, WeightMenu, NumWeightsMenu, NumVerticesMenu, IndexBufferMenu, IndexFormatMenu, IndexMemoryMenu, IndexCacheMenu, MemoryMenu, QuitTrigger} MenuAction;int main(int argc, char* argv[]){ setupCallbacks(); // setup GU void* fbp0 = getStaticVramBuffer(BUF_WIDTH,SCR_HEIGHT,GU_PSM_8888); void* fbp1 = getStaticVramBuffer(BUF_WIDTH,SCR_HEIGHT,GU_PSM_8888); void* zbp = getStaticVramBuffer(BUF_WIDTH,SCR_HEIGHT,GU_PSM_4444); pspDebugScreenInit(); sceGuInit(); sceGuStart(GU_DIRECT,list); sceGuDrawBuffer(GU_PSM_8888,fbp0,BUF_WIDTH); sceGuDispBuffer(SCR_WIDTH,SCR_HEIGHT,fbp1,BUF_WIDTH); sceGuDepthBuffer(zbp,BUF_WIDTH); sceGuOffset(2048 - (SCR_WIDTH/2),2048 - (SCR_HEIGHT/2)); sceGuViewport(2048,2048,SCR_WIDTH,SCR_HEIGHT); sceGuDepthRange(65535,0); sceGuScissor(0,0,SCR_WIDTH,SCR_HEIGHT); sceGuEnable(GU_SCISSOR_TEST); sceGuDepthFunc(GU_GEQUAL); sceGuEnable(GU_DEPTH_TEST); sceGuFrontFace(GU_CW); sceGuShadeModel(GU_SMOOTH); sceGuEnable(GU_CULL_FACE); sceGuEnable(GU_TEXTURE_2D); sceGuEnable(GU_CLIP_PLANES); sceGuFinish(); sceGuSync(0,0); sceDisplayWaitVblankStart(); sceGuDisplay(GU_TRUE); // allocate vertex buffers int maxVertexSize = getVertexSize(GU_TEXTURE_32BITF|GU_COLOR_8888|GU_NORMAL_32BITF|GU_VERTEX_32BITF|GU_WEIGHT_32BITF|GU_WEIGHTS(8)|GU_VERTICES(8)); int batchSize = 1536; // max size = 608, 608 * 1536 = ~1MB, make sure you don't overrun vram limits if you change this void* ramVertexBuffer = malloc(batchSize * maxVertexSize); void* vramVertexBuffer = getStaticVramTexture(batchSize,maxVertexSize,GU_PSM_T8); // allocate index buffers void* ramIndexBuffer = malloc(batchSize * sizeof(unsigned short)); void* vramIndexBuffer = getStaticVramTexture(batchSize,sizeof(unsigned short),GU_PSM_T8); // run sample int val = 0; SceCtrlData oldPad; memset(&oldPad,0,sizeof(SceCtrlData)); sceCtrlSetSamplingCycle(0); sceCtrlSetSamplingMode(0); // create debug menu MenuContext* context = initMenu(); MenuItem* vertexFormatMenu = addMenuItem(context,0,createMenuContainer("Vertex Buffer"),NoAction,0); { MenuItem* textureMenu = addMenuItem(context,vertexFormatMenu,createMenuContainer("Texture"),NoAction,0); { addMenuItem(context,textureMenu,createRadioButton("Off",1),TextureMenu,0); addMenuItem(context,textureMenu,createRadioButton("8-bit",0),TextureMenu,GU_TEXTURE_8BIT); addMenuItem(context,textureMenu,createRadioButton("16-bit",0),TextureMenu,GU_TEXTURE_16BIT); addMenuItem(context,textureMenu,createRadioButton("32-bit",0),TextureMenu,GU_TEXTURE_32BITF); } MenuItem* colorMenu = addMenuItem(context,vertexFormatMenu,createMenuContainer("Color"),NoAction,0); { addMenuItem(context,colorMenu,createRadioButton("Off",0),ColorMenu,0); addMenuItem(context,colorMenu,createRadioButton("5650 (16-bit)",0),ColorMenu,GU_COLOR_5650); addMenuItem(context,colorMenu,createRadioButton("5551 (16-bit)",0),ColorMenu,GU_COLOR_5551); addMenuItem(context,colorMenu,createRadioButton("4444 (16-bit)",0),ColorMenu,GU_COLOR_4444); addMenuItem(context,colorMenu,createRadioButton("8888 (32-bit)",1),ColorMenu,GU_COLOR_8888); } MenuItem* normalMenu = addMenuItem(context,vertexFormatMenu,createMenuContainer("Normal"),NoAction,0); { addMenuItem(context,normalMenu,createRadioButton("Off",1),NormalMenu,0); addMenuItem(context,normalMenu,createRadioButton("8-bit",0),NormalMenu,GU_NORMAL_8BIT); addMenuItem(context,normalMenu,createRadioButton("16-bit",0),NormalMenu,GU_NORMAL_16BIT); addMenuItem(context,normalMenu,createRadioButton("32-bit",0),NormalMenu,GU_NORMAL_32BITF); } MenuItem* vertexMenu = addMenuItem(context,vertexFormatMenu,createMenuContainer("Vertex"),NoAction,0); { addMenuItem(context,vertexMenu,createRadioButton("Off",0),VertexMenu,0); addMenuItem(context,vertexMenu,createRadioButton("8-bit",0),VertexMenu,GU_VERTEX_8BIT); addMenuItem(context,vertexMenu,createRadioButton("16-bit",0),VertexMenu,GU_VERTEX_16BIT); addMenuItem(context,vertexMenu,createRadioButton("32-bit",1),VertexMenu,GU_VERTEX_32BITF); } MenuItem* weightMenu = addMenuItem(context,vertexFormatMenu,createMenuContainer("Weight"),NoAction,0); { addMenuItem(context,weightMenu,createRadioButton("Off",1),WeightMenu,0); addMenuItem(context,weightMenu,createRadioButton("8-bit",0),WeightMenu,GU_WEIGHT_8BIT); addMenuItem(context,weightMenu,createRadioButton("16-bit",0),WeightMenu,GU_WEIGHT_16BIT); addMenuItem(context,weightMenu,createRadioButton("32-bit",0),WeightMenu,GU_WEIGHT_32BITF); } MenuItem* numWeightsMenu = addMenuItem(context,vertexFormatMenu,createMenuContainer("Num. Weights"),NoAction,0); { addMenuItem(context,numWeightsMenu,createRadioButton("0",1),NumWeightsMenu,0);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -