unsigned int * PS3_allocTextureBuffer(int w, int h, int type) { //user interface texture if (type) { texture_buffer_ui = (u32*)rsxMemalign(128,(w * h * 4)); if(!texture_buffer_ui) return NULL; rsxAddressToOffset(texture_buffer_ui, &texture_offset_ui); textureW_ui = w; textureH_ui = h; return texture_buffer_ui; } else //video texture { texture_buffer = (u32*)rsxMemalign(128,(w * h * 4)); if(!texture_buffer) return NULL; rsxAddressToOffset(texture_buffer,&texture_offset); textureW = w; textureH = h; return texture_buffer; } }
static SMeshBuffer* createCube(f32 size) { u32 i; SMeshBuffer *buffer = new SMeshBuffer(); const u16 u[36] = { 0,1,2, 0,2,3, 1,4,5, 1,5,2, 4,7,6, 4,6,5, 7,0,3, 7,3,6, 9,2,5, 9,5,8, 0,10,11, 0,7,10}; buffer->cnt_indices = 36; buffer->indices = (u16*)rsxMemalign(128,buffer->cnt_indices*sizeof(u16)); for(i=0;i<36;i++) buffer->indices[i] = u[i]; buffer->cnt_vertices = 12; buffer->vertices = (S3DVertex*)rsxMemalign(128,buffer->cnt_vertices*sizeof(S3DVertex)); buffer->vertices[0] = S3DVertex(0,0,0, -1,-1,-1, 1, 0); buffer->vertices[1] = S3DVertex(1,0,0, 1,-1,-1, 1, 1); buffer->vertices[2] = S3DVertex(1,1,0, 1, 1,-1, 0, 1); buffer->vertices[3] = S3DVertex(0,1,0, -1, 1,-1, 0, 0); buffer->vertices[4] = S3DVertex(1,0,1, 1,-1, 1, 1, 0); buffer->vertices[5] = S3DVertex(1,1,1, 1, 1, 1, 0, 0); buffer->vertices[6] = S3DVertex(0,1,1, -1, 1, 1, 0, 1); buffer->vertices[7] = S3DVertex(0,0,1, -1,-1, 1, 1, 1); buffer->vertices[8] = S3DVertex(0,1,1, -1, 1, 1, 1, 0); buffer->vertices[9] = S3DVertex(0,1,0, -1, 1,-1, 1, 1); buffer->vertices[10] = S3DVertex(1,0,1, 1,-1, 1, 0, 1); buffer->vertices[11] = S3DVertex(1,0,0, 1,-1,-1, 0, 0); for(i=0;i<12;i++) { buffer->vertices[i].pos -= Vector3(0.5f,0.5f,0.5f); buffer->vertices[i].pos *= size; } return buffer; }
static SMeshBuffer* createQuad(f32 size, float z) { u32 i; SMeshBuffer *buffer = new SMeshBuffer(); const u16 u[6] = { 0,1,2, 0,3,1}; buffer->cnt_indices = 6; buffer->indices = (u16*)rsxMemalign(128, buffer->cnt_indices * sizeof(u16)); for(i = 0; i < 6; i++) buffer->indices[i] = u[i]; buffer->cnt_vertices = 4; buffer->vertices = (S3DVertex*)rsxMemalign(128, buffer->cnt_vertices * sizeof(S3DVertex)); // position, normal, texture buffer->vertices[0] = S3DVertex(0, -1, z, 0, 0, -1, 0, 1); buffer->vertices[1] = S3DVertex(1, 0, z, 0, 0, -1, 1, 0); buffer->vertices[2] = S3DVertex(0, 0, z, 0, 0, -1, 0, 0); buffer->vertices[3] = S3DVertex(1, -1, z, 0, 0, -1, 1, 1); //centre and resize for(i=0; i < 4; i++) { //center buffer->vertices[i].pos += Vector3(-0.5f, 0.5f, 0.0f); //resize buffer->vertices[i].pos *= size; } return buffer; }
int makeBuffer (rsxBuffer * buffer, u16 width, u16 height, int id) { int depth = sizeof (u32); int pitch = depth * width; int size = depth * width * height; buffer->ptr = (uint32_t *) rsxMemalign (64, size); if (buffer->ptr == NULL) goto error; if (rsxAddressToOffset (buffer->ptr, &buffer->offset) != 0) goto error; /* Register the display buffer with the RSX */ if (gcmSetDisplayBuffer (id, buffer->offset, pitch, width, height) != 0) goto error; buffer->width = width; buffer->height = height; buffer->id = id; return TRUE; error: if (buffer->ptr != NULL) rsxFree (buffer->ptr); return FALSE; }
void init_shader() { u32 fpsize = 0; vp_ucode = rsxVertexProgramGetUCode(vpo); projMatrix_id = rsxVertexProgramGetConst(vpo,"projMatrix"); modelViewMatrix_id = rsxVertexProgramGetConst(vpo,"modelViewMatrix"); vertexPosition_id = rsxVertexProgramGetAttrib(vpo,"vertexPosition"); vertexNormal_id = rsxVertexProgramGetAttrib(vpo,"vertexNormal"); vertexTexcoord_id = rsxVertexProgramGetAttrib(vpo,"vertexTexcoord"); fp_ucode = rsxFragmentProgramGetUCode(fpo,&fpsize); fp_buffer = (u32*)rsxMemalign(64,fpsize); memcpy(fp_buffer,fp_ucode,fpsize); rsxAddressToOffset(fp_buffer,&fp_offset); textureUnit_id = rsxFragmentProgramGetAttrib(fpo,"texture"); eyePosition_id = rsxFragmentProgramGetConst(fpo,"eyePosition"); globalAmbient_id = rsxFragmentProgramGetConst(fpo,"globalAmbient"); lightPosition_id = rsxFragmentProgramGetConst(fpo,"lightPosition"); lightColor_id = rsxFragmentProgramGetConst(fpo,"lightColor"); shininess_id = rsxFragmentProgramGetConst(fpo,"shininess"); Ks_id = rsxFragmentProgramGetConst(fpo,"Ks"); Kd_id = rsxFragmentProgramGetConst(fpo,"Kd"); }
int initScreen (void *host_addr, u32 size) { // gcmContextData *context = NULL; /* Context to keep track of the RSX // buffer. */ videoState state; videoConfiguration vconfig; videoResolution res; /* Screen Resolution */ /* Initilise Reality, which sets up the command buffer and shared IO memory */ context = rsxInit (CB_SIZE, size, host_addr); if (context == NULL) goto error; /* Get the state of the display */ if (videoGetState (0, 0, &state) != 0) goto error; /* Make sure display is enabled */ if (state.state != 0) goto error; /* Get the current resolution */ if (videoGetResolution (state.displayMode.resolution, &res) != 0) goto error; /* Configure the buffer format to xRGB */ memset (&vconfig, 0, sizeof (videoConfiguration)); vconfig.resolution = state.displayMode.resolution; vconfig.format = VIDEO_BUFFER_FORMAT_XRGB; vconfig.pitch = res.width * sizeof (u32); vconfig.aspect = state.displayMode.aspect; waitRSXIdle (context); if (videoConfigure (0, &vconfig, NULL, 0) != 0) goto error; if (videoGetState (0, 0, &state) != 0) goto error; gcmSetFlipMode (GCM_FLIP_VSYNC); // Wait for VSYNC to flip depth_pitch = res.width * sizeof (u32); depth_buffer = (u32 *) rsxMemalign (64, (res.height * depth_pitch) * 2); rsxAddressToOffset (depth_buffer, &depth_offset); gcmResetFlipStatus (); return 0; error: if (context) rsxFinish (context, 0); if (host_addr) free (host_addr); return 1; }
static struct sw_displaytarget * psl1ght_displaytarget_create(struct sw_winsys *ws, unsigned tex_usage, enum pipe_format format, unsigned width, unsigned height, unsigned alignment, unsigned *stride) { struct psl1ght_sw_winsys *psl1ght = psl1ght_sw_winsys(ws); struct psl1ght_sw_displaytarget *psdt; psdt = CALLOC_STRUCT(psl1ght_sw_displaytarget); if (!psdt) return NULL; psdt->width = width; psdt->height = height; psdt->stride = width * 4; psdt->bufferId = NO_BUFFER; psdt->data = rsxMemalign(64, psdt->height * psdt->stride); if (!psdt->data) { FREE(psdt); return NULL; } if (rsxAddressToOffset(psdt->data, &psdt->offset)) { rsxFree(psdt->data); FREE(psdt); return NULL; } *stride = psdt->stride; return (struct sw_displaytarget *) psdt; }
void init_screen(void *host_addr,u32 size) { printf("initializing screen....\n"); context = rsxInit(CB_SIZE,size,host_addr); videoState state; videoGetState(0,0,&state); videoGetResolution(state.displayMode.resolution,&res); videoConfiguration vconfig; memset(&vconfig,0,sizeof(videoConfiguration)); vconfig.resolution = state.displayMode.resolution; vconfig.format = VIDEO_BUFFER_FORMAT_XRGB; vconfig.pitch = res.width*sizeof(u32); waitRSXIdle(); videoConfigure(0,&vconfig,NULL,0); videoGetState(0,0,&state); gcmSetFlipMode(GCM_FLIP_VSYNC); display_width = res.width; display_height = res.height; color_pitch = display_width*sizeof(u32); color_buffer[0] = (u32*)rsxMemalign(64,(display_height*color_pitch)); color_buffer[1] = (u32*)rsxMemalign(64,(display_height*color_pitch)); rsxAddressToOffset(color_buffer[0],&color_offset[0]); rsxAddressToOffset(color_buffer[1],&color_offset[1]); gcmSetDisplayBuffer(0,color_offset[0],color_pitch,display_width,display_height); gcmSetDisplayBuffer(1,color_offset[1],color_pitch,display_width,display_height); depth_pitch = display_width*sizeof(u32); depth_buffer = (u32*)rsxMemalign(64,(display_height*depth_pitch)*2); rsxAddressToOffset(depth_buffer,&depth_offset); printf("screen initialized....\n"); }
/* Initilize everything. */ void init_screen(displayData *vdat) { int i; /* Allocate a 1Mb buffer, alligned to a 1Mb boundary to be our shared IO memory with the RSX. */ void *host_addr = memalign(1024*1024, 1024*1024); assert(host_addr != NULL); /* Initilise libRSX, which sets up the command buffer and shared IO memory */ vdat->context = rsxInit(0x10000, 1024*1024, host_addr); assert(vdat->context != NULL); videoState state; s32 status = videoGetState(0, 0, &state); // Get the state of the display assert(status == 0); assert(state.state == 0); // Make sure display is enabled /* Get the current resolution */ status = videoGetResolution(state.displayMode.resolution, &vdat->res); assert(status == 0); /* Configure the buffer format to xRGB */ videoConfiguration vconfig; memset(&vconfig, 0, sizeof(videoConfiguration)); vconfig.resolution = state.displayMode.resolution; vconfig.format = VIDEO_BUFFER_FORMAT_XRGB; vconfig.pitch = vdat->res.width * 4; vconfig.aspect=state.displayMode.aspect; status = videoConfigure(0, &vconfig, NULL, 0); assert(status == 0); status = videoGetState(0, 0, &state); assert(status == 0); gcmSetFlipMode(GCM_FLIP_VSYNC); /* Wait for VSYNC to flip */ /* Allocate and setup two buffers for the RSX to draw to the screen (double buffering) */ vdat->pitch = vdat->res.width*sizeof(u32); for (i=0; i<2; ++i) { vdat->buffer[i] = (u32*)rsxMemalign(64,vdat->res.width*vdat->pitch); assert(vdat->buffer[i] != NULL); status = rsxAddressToOffset(vdat->buffer[i], &vdat->offset[i]); assert(status==0); status = gcmSetDisplayBuffer(i, vdat->offset[i], vdat->pitch, vdat->res.width, vdat->res.height); assert(status==0); } gcmResetFlipStatus(); vdat->curr_fb = 0; vdat->framecnt = 0; flip(vdat); }
static void init_texture() { u32 i; u8 *buffer; const u8 *data = acid.pixel_data; texture_buffer = (u32*)rsxMemalign(128,(acid.width*acid.height*4)); if(!texture_buffer) return; rsxAddressToOffset(texture_buffer,&texture_offset); buffer = (u8*)texture_buffer; for(i=0;i<acid.width*acid.height*4;i+=4) { buffer[i + 1] = *data++; buffer[i + 2] = *data++; buffer[i + 3] = *data++; buffer[i + 0] = *data++; } }
void init_shader() { u32 fpsize = 0; vp_ucode = rsxVertexProgramGetUCode(vpo); projMatrix_id = rsxVertexProgramGetConst(vpo, "projMatrix"); modelViewMatrix_id = rsxVertexProgramGetConst(vpo, "modelViewMatrix"); vertexPosition_id = rsxVertexProgramGetAttrib(vpo, "vertexPosition"); vertexNormal_id = rsxVertexProgramGetAttrib(vpo, "vertexNormal"); vertexTexcoord_id = rsxVertexProgramGetAttrib(vpo, "vertexTexcoord"); fp_ucode = rsxFragmentProgramGetUCode(fpo, &fpsize); fp_buffer = (u32*) rsxMemalign(64, fpsize); memcpy(fp_buffer, fp_ucode, fpsize); rsxAddressToOffset(fp_buffer, &fp_offset); textureUnit_id = rsxFragmentProgramGetAttrib(fpo, "texture"); scanline_id = rsxFragmentProgramGetConst(fpo, "scanline"); }
/* initialize resc */ void rescInitialize ( videoData *vdata ) { dbgprintf ( "initializing" ) ; static s32 ret ; /* Get the state of the display */ ret = videoGetState (VIDEO_PRIMARY, 0, &vdata->state) ; if (ret != 0) { errprintf ( "ERROR %x", ret ) ; goto error; } /* Make sure display is enabled */ if (vdata->state.state != 0 ) { errprintf ( "ERROR %x", vdata->state.state ) ; goto error; } /* initialize resc configuration */ memset (&vdata->rconfig, 0, (size_t)sizeof( rescInitConfig )); vdata->rconfig.size = sizeof( rescInitConfig ) ; vdata->rconfig.resourcePolicy = RESC_MINIMUM_GPU_LOAD | RESC_CONSTANT_VRAM ; vdata->rconfig.supportModes = RESC_1920x1080 | RESC_1280x720 | RESC_720x576 | RESC_720x480 ; vdata->rconfig.ratioMode = (vdata->state.displayMode.aspect == ASPECT_4_3) ? RESC_LETTERBOX : RESC_FULLSCREEN ; vdata->rconfig.palTemporalMode = RESC_PAL_50 ; vdata->rconfig.interlaceMode = RESC_NORMAL_BILINEAR ; vdata->rconfig.flipMode = RESC_DISPLAY_VSYNC ; ret = rescInit ( &vdata->rconfig ) ; if ( ret != 0 ) { errprintf ( "ERROR %x", ret ) ; goto error; } rescVideoResolution2RescBufferMode ( vdata->state.displayMode.resolution, &vdata->bufMode ) ; /* 0 */ { vdata->dsts[0].format = RESC_SURFACE_A8R8G8B8 ; vdata->dsts[0].pitch = 0xc00 ; vdata->dsts[0].heightAlign = 64 ; ret = rescSetDsts ( RESC_720x480, &vdata->dsts[0] ) ; if ( ret != 0 ) { errprintf ( "ERROR %x", ret ) ; goto error; } } /* 1 */ { vdata->dsts[1].format = RESC_SURFACE_A8R8G8B8 ; vdata->dsts[1].pitch = 0xc00 ; vdata->dsts[1].heightAlign = 64 ; ret = rescSetDsts ( RESC_720x576, &vdata->dsts[1] ) ; if ( ret != 0 ) { errprintf ( "ERROR %x", ret ) ; goto error; } } /* 2 */ { vdata->dsts[2].format = RESC_SURFACE_A8R8G8B8 ; vdata->dsts[2].pitch = 0x1400 ; vdata->dsts[2].heightAlign = 64 ; ret = rescSetDsts ( RESC_1280x720, &vdata->dsts[2] ) ; if ( ret != 0 ) { errprintf ( "ERROR %x", ret ) ; goto error; } } /* 3 */ { vdata->dsts[3].format = RESC_SURFACE_A8R8G8B8 ; vdata->dsts[3].pitch = 0x2000 ; vdata->dsts[3].heightAlign = 32 ; ret = rescSetDsts ( RESC_1920x1080, &vdata->dsts[3] ) ; if ( ret != 0 ) { errprintf ( "ERROR %x", ret ) ; goto error; } } ret = rescSetDisplayMode ( vdata->bufMode ) ; if ( ret != 0 ) { errprintf ( "ERROR %d", ret ) ; goto error; } ret = rescGetBufferSize ( &vdata->cbuf.size, &vdata->vertbuf.size, &vdata->fragbuf.size ) ; if ( ret != 0 ) { errprintf ( "ERROR %d", ret ) ; goto error; } vdata->cbuf.ptr = (void*) rsxMemalign ( 0x10000, vdata->cbuf.size ) ; vdata->vertbuf.ptr = (void*) rsxMemalign ( 0x80, vdata->vertbuf.size ) ; vdata->fragbuf.ptr = (void*) rsxMemalign ( 0x80, vdata->fragbuf.size ) ; ret = rescSetBufferAddress ( vdata->cbuf.ptr, vdata->vertbuf.ptr, vdata->fragbuf.ptr ) ; if ( ret != 0 ) { errprintf ( "ERROR %d", ret ) ; goto error; } argprintf("Required size, color: 0x%x vertex: 0x%x fragment: 0x%x", vdata->cbuf.size, vdata->vertbuf.size, vdata->fragbuf.size); argprintf("Buf addr given, color: %p vertex: %p fragment: %p", vdata->cbuf.ptr, vdata->vertbuf.ptr, vdata->fragbuf.ptr); /* */ /* TILING BULLSHIT GOES HERE */ /* */ rescSetFlipHandler ( rescFlipCallback ) ; rescSetRenderTarget ( vdata ) ; return ; error: { return ; } }
void * tiny3d_AllocTexture(u32 size) { return rsxMemalign(16, size); }
static SMeshBuffer* createSphere(f32 radius,u32 polyCntX,u32 polyCntY) { u32 i,p1,p2,level; u32 x,y,polyCntXpitch; const f32 RECIPROCAL_PI = 1.0f/M_PI; SMeshBuffer *buffer = new SMeshBuffer(); if(polyCntX<2) polyCntX = 2; if(polyCntY<2) polyCntY = 2; if(polyCntX*polyCntY>32767) { if(polyCntX>polyCntY) polyCntX = 32767/polyCntY-1; else polyCntY = 32767/(polyCntX+1); } polyCntXpitch = polyCntX+1; buffer->cnt_vertices = (polyCntXpitch*polyCntY)+2; buffer->vertices = (S3DVertex*)rsxMemalign(128,buffer->cnt_vertices*sizeof(S3DVertex)); buffer->cnt_indices = (polyCntX*polyCntY)*6; buffer->indices = (u16*)rsxMemalign(128,buffer->cnt_indices*sizeof(u16)); i = 0; level = 0; for(p1=0;p1<polyCntY-1;p1++) { for(p2=0;p2<polyCntX-1;p2++) { const u32 curr = level + p2; buffer->indices[i++] = curr; buffer->indices[i++] = curr + polyCntXpitch; buffer->indices[i++] = curr + 1 + polyCntXpitch; buffer->indices[i++] = curr; buffer->indices[i++] = curr + 1 + polyCntXpitch; buffer->indices[i++] = curr + 1; } buffer->indices[i++] = level + polyCntX; buffer->indices[i++] = level + polyCntX - 1; buffer->indices[i++] = level + polyCntX - 1 + polyCntXpitch; buffer->indices[i++] = level + polyCntX; buffer->indices[i++] = level + polyCntX - 1 + polyCntXpitch; buffer->indices[i++] = level + polyCntX + polyCntXpitch; level += polyCntXpitch; } const u32 polyCntSq = polyCntXpitch*polyCntY; const u32 polyCntSq1 = polyCntSq+1; const u32 polyCntSqM1 = (polyCntY-1)*polyCntXpitch; for(p2=0;p2<polyCntX-1;p2++) { buffer->indices[i++] = polyCntSq; buffer->indices[i++] = p2; buffer->indices[i++] = p2+1; buffer->indices[i++] = polyCntSq1; buffer->indices[i++] = polyCntSqM1+p2; buffer->indices[i++] = polyCntSqM1+p2+1; } buffer->indices[i++] = polyCntSq; buffer->indices[i++] = polyCntX-1; buffer->indices[i++] = polyCntX; buffer->indices[i++] = polyCntSq1; buffer->indices[i++] = polyCntSqM1; buffer->indices[i++] = polyCntSqM1+polyCntX-1; f32 axz; f32 ay = 0; const f32 angelX = 2*M_PI/polyCntX; const f32 angelY = M_PI/polyCntY; i = 0; for(y=0;y<polyCntY;y++) { axz = 0; ay += angelY; const f32 sinay = sinf(ay); for(x=0;x<polyCntX;x++) { const Vector3 pos(static_cast<f32>(radius*cosf(axz)*sinay), static_cast<f32>(radius*cosf(ay)), static_cast<f32>(radius*sinf(axz)*sinay)); Vector3 normal = normalize(pos); f32 tu = 0.5F; if(y==0) { if(normal.getY()!=-1.0F && normal.getY()!=1.0F) tu = static_cast<f32>(acosf(clamp(normal.getX()/sinay,-1.0f,1.0f))*0.5F*RECIPROCAL_PI); if(normal.getZ()<0.0F) tu = 1-tu; } else tu = buffer->vertices[i - polyCntXpitch].u; buffer->vertices[i] = S3DVertex(pos.getX(),pos.getY(),pos.getZ(),normal.getX(),normal.getY(),normal.getZ(),tu,static_cast<f32>(ay*RECIPROCAL_PI)); axz += angelX; i++; } buffer->vertices[i] = S3DVertex(buffer->vertices[i-polyCntX]); buffer->vertices[i].u = 1.0F; i++; } buffer->vertices[i++] = S3DVertex(0.0F,radius,0.0F,0.0F,1.0F,0.0F,0.5F,0.0F); buffer->vertices[i] = S3DVertex(0.0F,-radius,0.0F,0.0F,-1.0F,0.0F,0.5F,1.0F); return buffer; }
static SMeshBuffer* createDonut(f32 outerRadius,f32 innerRadius,u32 polyCntX,u32 polyCntY) { u32 i,x,y,level; SMeshBuffer *buffer = new SMeshBuffer(); if(polyCntX<2) polyCntX = 2; if(polyCntY<2) polyCntY = 2; while(polyCntX*polyCntY>32767) { polyCntX /= 2; polyCntY /= 2; } f32 ay = 0; const f32 angleX = 2*M_PI/polyCntX; const f32 angleY = 2*M_PI/polyCntY; const u32 polyCntXpitch = polyCntX +1; const u32 polyCntYpitch = polyCntY + 1; buffer->cnt_vertices = polyCntYpitch*polyCntXpitch; buffer->vertices = (S3DVertex*)rsxMemalign(128,buffer->cnt_vertices*sizeof(S3DVertex)); buffer->cnt_indices = polyCntY*polyCntX*6; buffer->indices = (u16*)rsxMemalign(128,buffer->cnt_indices*sizeof(u16)); i = 0; for(y=0;y<=polyCntY;y++) { f32 axz = 0; const f32 sinay = sinf(ay); const f32 cosay = cosf(ay); const f32 tu = (f32)y/(f32)polyCntY; for(x=0;x<=polyCntX;x++) { const Vector3 pos(static_cast<f32>((outerRadius - (innerRadius*cosf(axz)))*cosay), static_cast<f32>((outerRadius - (innerRadius*cosf(axz)))*sinay), static_cast<f32>(innerRadius*sinf(axz))); const Vector3 nrm(static_cast<f32>(-cosf(axz)*cosay), static_cast<f32>(-cosf(axz)*sinay), static_cast<f32>(sinf(axz))); buffer->vertices[i] = S3DVertex(pos.getX(),pos.getY(),pos.getZ(),nrm.getX(),nrm.getY(),nrm.getZ(),tu,(f32)x/(f32)polyCntX); axz += angleX; i++; } ay += angleY; } i = 0; level = 0; for(y=0;y<polyCntY;y++) { for(x=0;x<polyCntX - 1;x++) { const u32 curr = level + x; buffer->indices[i++] = curr; buffer->indices[i++] = curr + polyCntXpitch; buffer->indices[i++] = curr + 1 + polyCntXpitch; buffer->indices[i++] = curr; buffer->indices[i++] = curr + 1 + polyCntXpitch; buffer->indices[i++] = curr + 1; } buffer->indices[i++] = level + polyCntX; buffer->indices[i++] = level + polyCntX - 1; buffer->indices[i++] = level + polyCntX - 1 + polyCntXpitch; buffer->indices[i++] = level + polyCntX; buffer->indices[i++] = level + polyCntX - 1 + polyCntXpitch; buffer->indices[i++] = level + polyCntX + polyCntXpitch; level += polyCntXpitch; } return buffer; }
// Initilize and rsx void init_screen(int command_buffer, int z_method) { // Allocate a 1Mb buffer, alligned to a 1Mb boundary to be our shared IO memory with the RSX. void *host_addr = memalign(1024*1024, command_buffer); assert(host_addr != NULL); if(z_method) zformat = REALITY_TARGET_FORMAT_ZETA_Z24S8; else zformat = REALITY_TARGET_FORMAT_ZETA_Z16; // Initilise Reality, which sets up the command buffer and shared IO memory context = rsxInit(0x10000, command_buffer, host_addr); assert(context != NULL); videoState state; assert(videoGetState(0, 0, &state) == 0); // Get the state of the display assert(state.state == 0); // Make sure display is enabled // Get the current resolution assert(videoGetResolution(state.displayMode.resolution, &Video_Resolution) == 0); Video_pitch = 4 * ((Video_Resolution.width + 15)/16) * 16; // each pixel is 4 bytes if(!z_method) // 16 bit float. Note it uses 1920 as minimun because i thinking to use buffer Z with setupRenderTarget2() with one surface > screen depth_pitch = 2 * ((Video_Resolution.width > 1920) ? (((Video_Resolution.width+31)/32)*32) : 1920); else // 32 bit float. Note it uses 1920 as minimun because i thinking to use buffer Z with setupRenderTarget2() with one surface > screen depth_pitch = 4 * ((Video_Resolution.width > 1920) ? (((Video_Resolution.width+15)/16)*16) : 1920); // Configure the buffer format to xRGB videoConfiguration vconfig; memset(&vconfig, 0, sizeof(videoConfiguration)); vconfig.resolution = state.displayMode.resolution; vconfig.format = VIDEO_BUFFER_FORMAT_XRGB; vconfig.pitch = Video_pitch; Video_aspect=vconfig.aspect=state.displayMode.aspect; assert(videoConfigure(0, &vconfig, NULL, 0) == 0); assert(videoGetState(0, 0, &state) == 0); s32 buffer_size = Video_pitch * Video_Resolution.height; s32 depth_buffer_size; if(!z_method) // 16 bit float. Note it uses 1088 as minimun because i thinking to use buffer Z with setupRenderTarget2() with one surface > screen depth_buffer_size = depth_pitch * ((Video_Resolution.height > 1088) ? (((Video_Resolution.height+31)/32)*32) : 1088); else // 32 bit float. Note it uses 1920 as minimun because i thinking to use buffer Z with setupRenderTarget2() with one surface > screen depth_buffer_size = depth_pitch * ((Video_Resolution.height > 1088) ? (((Video_Resolution.height+15)/16)*16) : 1088); printf("buffers will be 0x%x bytes\n", buffer_size); gcmSetFlipMode(GCM_FLIP_VSYNC); // Wait for VSYNC to flip // Allocate two buffers for the RSX to draw to the screen (double buffering) Video_buffer[0] = rsxMemalign(64, buffer_size); Video_buffer[1] = rsxMemalign(64, buffer_size); assert(Video_buffer[0] != NULL && Video_buffer[1] != NULL); depth_buffer = rsxMemalign(64, depth_buffer_size); assert(rsxAddressToOffset(Video_buffer[0], &offset[0]) == 0); assert(rsxAddressToOffset(Video_buffer[1], &offset[1]) == 0); // Setup the display buffers assert(gcmSetDisplayBuffer(0, offset[0], Video_pitch, Video_Resolution.width, Video_Resolution.height) == 0); assert(gcmSetDisplayBuffer(1, offset[1], Video_pitch, Video_Resolution.width, Video_Resolution.height) == 0); assert(rsxAddressToOffset(depth_buffer, &depth_offset) == 0); gcmResetFlipStatus(); flip(1); waitFlip(); }
int tiny3d_Init(u32 vertex_buff_size) { static int inited= 0; int n; int use_Z32 = 1; if(vertex_buff_size & TINY3D_Z16) use_Z32 = 0; vertex_buff_size &= 0x3fffffff; flag_vertex = VERTEX_LOCK; if(inited) return TINY3D_CANNOTINIT; inited = 1; if(vertex_buff_size <= 1024*1024) vertex_buff_size = 1024*1024; size_rsx_vertex = vertex_buff_size; init_screen(1024*1024*2, use_Z32); CtrlReg = gcmGetControlRegister(); n = 0; while(fpshader_list[n]) { // install fragments shaders in rsx memory u32 *frag_mem = rsxMemalign(256, (((realityFragmentProgram_internal *) fpshader_list[n])->size * 4 + 255) & ~255); if(!frag_mem) return TINY3D_OUTMEMORY; internal_reality_InstallFragmentProgram(context, fpshader_list[n], frag_mem); n++; } // shaders datas for(n = 0; n < 12; n++) { data_shader[n].off_project = internal_reality_VertexProgramGetConstant((internal_reality_VertexProgram*) vshader_text_normal_bin, "ProjMatrix"); data_shader[n].off_modelv = internal_reality_VertexProgramGetConstant((internal_reality_VertexProgram*) vshader_text_normal_bin, "WorldMatrix"); data_shader[n].off_position = internal_reality_VertexProgramGetInputAttribute((internal_reality_VertexProgram*) vshader_text_normal_bin, "inputvertex.vertex"); data_shader[n].off_texture = internal_reality_VertexProgramGetInputAttribute((internal_reality_VertexProgram*) vshader_text_normal_bin, "inputvertex.texcoord"); data_shader[n].off_texture2 = internal_reality_VertexProgramGetInputAttribute((internal_reality_VertexProgram*) vshader_text_normal_bin, "inputvertex.texcoord2"); data_shader[n].off_color = internal_reality_VertexProgramGetInputAttribute((internal_reality_VertexProgram*) vshader_text_normal_bin, "inputvertex.color"); data_shader[n].off_normal = internal_reality_VertexProgramGetInputAttribute((internal_reality_VertexProgram*) vshader_text_normal_bin, "inputvertex.normal"); data_shader[n].vp = (void *) vshader_text_normal_bin; data_shader[n].fp_alt[0] = (void *) NULL; data_shader[n].fp_alt[1] = (void *) NULL; data_shader[n].fp_yuv[0] = (void *) NULL; data_shader[n].fp_yuv[1] = (void *) NULL; data_shader[n].off_lightAmbient = internal_reality_VertexProgramGetConstant((internal_reality_VertexProgram*) vshader_text_normal_bin, "lightAmbient"); data_shader[n].off_lightColor = internal_reality_VertexProgramGetConstant((internal_reality_VertexProgram*) vshader_text_normal_bin, "lightColor"); data_shader[n].off_lightPosition = internal_reality_VertexProgramGetConstant((internal_reality_VertexProgram*) vshader_text_normal_bin, "lightPosition"); data_shader[n].off_cameraPosition = internal_reality_VertexProgramGetConstant((internal_reality_VertexProgram*) vshader_text_normal_bin, "CameraPosition"); data_shader[n].off_emissive = internal_reality_VertexProgramGetConstant((internal_reality_VertexProgram*) vshader_text_normal_bin, "Memissive"); data_shader[n].off_ambient = internal_reality_VertexProgramGetConstant((internal_reality_VertexProgram*) vshader_text_normal_bin, "Mambient"); data_shader[n].off_diffuse = internal_reality_VertexProgramGetConstant((internal_reality_VertexProgram*) vshader_text_normal_bin, "Mdiffuse"); data_shader[n].off_specular = internal_reality_VertexProgramGetConstant((internal_reality_VertexProgram*) vshader_text_normal_bin, "Mspecular"); data_shader[n].fixed_color = 0; } data_shader[0].off_texture = -1; // colorf data_shader[0].off_texture2 = -1; data_shader[0].off_normal = -1; data_shader[0].fp = &nv30_fp_color; data_shader[0].size_vertex = 16+16; data_shader[1].fixed_color = 1; // colori data_shader[1].off_texture = -1; data_shader[1].off_texture2 = -1; data_shader[1].off_normal = -1; data_shader[1].fp = &nv30_fp_color; data_shader[1].size_vertex = 16+4; data_shader[2].off_texture2 = -1; // texture data_shader[2].off_color = -1; data_shader[2].off_normal = -1; data_shader[2].fp = &nv30_fp_texture; data_shader[2].fp_yuv[0] = &nv30_fp_yuv; data_shader[2].fp_yuv[1] = &nv30_fp_yuv8; data_shader[2].size_vertex = 16+8; data_shader[3].off_color = -1; // texture + texture2 data_shader[3].off_normal = -1; data_shader[3].fp = &nv30_fp_texture2; data_shader[3].fp_alt[0] = &nv30_fp_texture2_alt; data_shader[3].fp_alt[1] = &nv30_fp_texture2_alt2; data_shader[3].size_vertex = 16+8+8; data_shader[4].off_texture2 = -1; // texture + colorf data_shader[4].off_normal = -1; data_shader[4].fp = &nv30_fp_texture_color; data_shader[4].fp_yuv[0] = &nv30_fp_yuv_color; data_shader[4].fp_yuv[1] = &nv30_fp_yuv_color8; data_shader[4].size_vertex = 16+16+8; data_shader[5].off_normal = -1; // texture + texture2 + colorf data_shader[5].fp = &nv30_fp_texture_color2; data_shader[5].fp_alt[0] = &nv30_fp_texture_color2_alt; data_shader[5].fp_alt[1] = &nv30_fp_texture_color2_alt2; data_shader[5].size_vertex = 16+16+8+8; data_shader[6].fixed_color = 1; // texture + colori data_shader[6].off_texture2 = -1; data_shader[6].off_normal = -1; data_shader[6].fp = &nv30_fp_texture_color; data_shader[6].fp_yuv[0] = &nv30_fp_yuv_color; data_shader[6].fp_yuv[1] = &nv30_fp_yuv_color8; data_shader[6].size_vertex = 16+4+8; data_shader[7].fixed_color = 1; // texture + texture2 + colori data_shader[7].off_normal = -1; data_shader[7].fp = &nv30_fp_texture_color2; data_shader[7].fp_alt[0] = &nv30_fp_texture_color2_alt; data_shader[7].fp_alt[1] = &nv30_fp_texture_color2_alt2; data_shader[7].size_vertex = 16+4+8+8; data_shader[8].off_texture = -1; // normal data_shader[8].off_texture2 = -1; data_shader[8].off_color = -1; data_shader[8].fp = &nv30_fp_color; data_shader[8].size_vertex = 16+12; data_shader[9].off_texture2 = -1; // normal + texture data_shader[9].off_color = -1; data_shader[9].fp = &nv30_fp_texture_color; data_shader[9].size_vertex = 16+12+8; data_shader[10].off_color = -1; // normal + texture + texture2 data_shader[10].fp = &nv30_fp_texture_color2; data_shader[10].fp_alt[0] = &nv30_fp_texture_color2_alt; data_shader[10].fp_alt[1] = &nv30_fp_texture_color2_alt2; data_shader[10].size_vertex = 16+12+8+8; current_shader = -1; rsx_vertex = rsxMemalign(64, vertex_buff_size); pos_rsx_vertex = 0; polygon = -1; off_head_vertex = off_start_vertex = 0; sysUtilUnregisterCallback(SYSUTIL_EVENT_SLOT3); sysUtilRegisterCallback(SYSUTIL_EVENT_SLOT3, tiny3d_callback, NULL); return TINY3D_OK; }
void* PS3_mallocRsx(int size) { return rsxMemalign(128, size); }
static s32 decodePNG(pngDecSource *src,pngData *out) { s32 mHandle,sHandle,ret; u32 space_allocated; u64 bytes_per_line; pngDecInfo DecInfo; pngDecInParam inParam; pngDecOutParam outParam; pngDecDataInfo DecDataInfo; pngDecThreadInParam InThdParam; pngDecThreadOutParam OutThdParam; InThdParam.enable = 0; InThdParam.ppu_prio = 512; InThdParam.spu_prio = 200; InThdParam.malloc_func = __get_addr32(__get_opd32(png_malloc)); InThdParam.malloc_arg = 0; // no args InThdParam.free_func = __get_addr32(__get_opd32(png_free)); InThdParam.free_arg = 0; // no args ret= pngDecCreate(&mHandle, &InThdParam, &OutThdParam); out->bmp_out = NULL; if(ret==0) { ret = pngDecOpen(mHandle,&sHandle,src,&space_allocated); if(ret==0) { ret = pngDecReadHeader(mHandle,sHandle,&DecInfo); if(ret==0) { inParam.cmd_ptr = 0; inParam.mode = PNGDEC_TOP_TO_BOTTOM; inParam.space = PNGDEC_ARGB; inParam.bit_depth = 8; inParam.pack_flag = 1; if(DecInfo.space==PNGDEC_GRAYSCALE_ALPHA || DecInfo.space==PNGDEC_RGBA || DecInfo.chunk_info&0x10) inParam.alpha_select = 0; else inParam.alpha_select = 1; inParam.alpha = 0xff; ret = pngDecSetParameter(mHandle,sHandle,&inParam,&outParam); } if(ret==0) { out->pitch = bytes_per_line = outParam.width*4; //out->bmp_out = malloc(out->pitch*outParam.height); out->bmp_out = rsxMemalign(64, out->pitch*outParam.height); if(!out->bmp_out) ret = -1; else { memset(out->bmp_out,0,(bytes_per_line*outParam.height)); ret = pngDecDecodeData(mHandle,sHandle,out->bmp_out,&bytes_per_line,&DecDataInfo); if(ret==0 && DecDataInfo.status==0) { out->width = outParam.width; out->height = outParam.height; ret = 0; } } } pngDecClose(mHandle,sHandle); } if(ret && out->bmp_out) { free(out->bmp_out); out->bmp_out = NULL; } pngDecDestroy(mHandle); } return ret; }