void initPlayer(player_s* p) { if(!p)return; initPhysicalPoint(&p->object, vect3Df(0,0,0), PLAYER_RADIUS); initCamera(&p->camera); md2InstanceInit(&p->gunInstance, &gunModel, &gunTextureOrange); md2InstanceInit(&p->ratmanInstance, &ratmanModel, &ratmanTexture); p->ratmanInstance.speed=0.1f; p->oldInPortal = p->inPortal = false; passthroughDvlb = DVLB_ParseFile((u32*)passthrough_vsh_shbin, passthrough_vsh_shbin_size); shaderProgramInit(&passthroughProgram); if(!passthroughDvlb)return; shaderProgramSetVsh(&passthroughProgram, &passthroughDvlb->DVLE[0]); rectangleVertexData = linearAlloc(sizeof(rectangleData)); memcpy(rectangleVertexData, rectangleData, sizeof(rectangleData)); crosshairVertexData = linearAlloc(sizeof(crosshairData)); memcpy(crosshairVertexData, crosshairData, sizeof(crosshairData)); p->flying = false; p->life = 80; p->walkCnt1 = 0; p->walkCnt2 = 0; }
void initEmancipation(void) { int i; for(i=0;i<NUMEMANCIPATORS;i++) { emancipators[i].used=false; } for(i=0;i<NUMEMANCIPATIONGRIDS;i++) { emancipationGrids[i].used=false; } md2ReadModel(&gridModel, "grid.md2"); textureLoad(&gridTexture, "balllauncher.png", GPU_TEXTURE_MAG_FILTER(GPU_LINEAR)|GPU_TEXTURE_MIN_FILTER(GPU_LINEAR), 0); textureLoad(&gridSurfaceTexture, "grid.png", GPU_TEXTURE_MAG_FILTER(GPU_LINEAR)|GPU_TEXTURE_MIN_FILTER(GPU_LINEAR)|GPU_TEXTURE_WRAP_S(GPU_REPEAT)|GPU_TEXTURE_WRAP_T(GPU_REPEAT), 0); md2InstanceInit(&gridInstance, &gridModel, &gridTexture); emancipationRectangleVertexData = linearAlloc(sizeof(emancipationRectangleData)); memcpy(emancipationRectangleVertexData, emancipationRectangleData, sizeof(emancipationRectangleData)); emancipationDvlb = DVLB_ParseFile((u32*)emancipation_vsh_shbin, emancipation_vsh_shbin_size); if(!emancipationDvlb)return; shaderProgramInit(&emancipationProgram); shaderProgramSetVsh(&emancipationProgram, &emancipationDvlb->DVLE[0]); emancipationUniformTextureDimensions = shaderInstanceGetUniformLocation(emancipationProgram.vertexShader, "textureDimensions"); emancipationSFX=createSFX("emancipation.raw", SOUND_FORMAT_16BIT); }
void glShaderBinary(GLsizei count, const GLuint *shaders, GLenum binaryFormat, const void *binary, GLsizei length) { if (count != 1 || shaders == 0 || binary == 0 || length <= 0) { return; } DVLB_s *dvlb = DVLB_ParseFile((u32*)binary, length); ctr_handle_set(CTR_HANDLE_SHADER, *shaders, dvlb); }
void glShaderSource(GLuint shader, GLsizei count, const GLchar **string, const GLint *length) { if (count != 1) { return; } DVLB_s *dvlb = DVLB_ParseFile((u32*)string, *length); ctr_handle_set(CTR_HANDLE_SHADER, shader, dvlb); }
void TextEngine::Initialize(gfxScreen_t scn) { //The two screens are different sizes if(scn == GFX_TOP) { target = C3D_RenderTargetCreate(240, 400, GPU_RB_RGBA8, GPU_RB_DEPTH24_STENCIL8); } else { target = C3D_RenderTargetCreate(240, 320, GPU_RB_RGBA8, GPU_RB_DEPTH24_STENCIL8); } C3D_RenderTargetSetClear(target, C3D_CLEAR_ALL, CLEAR_COLOR, 0); C3D_RenderTargetSetOutput(target, scn, GFX_LEFT, DISPLAY_TRANSFER_FLAGS); Result res = fontEnsureMapped(); if (R_FAILED(res)) printf("fontEnsureMapped: %08lX\n", res); vshader_dvlb = DVLB_ParseFile((u32*)vshader_v_shbin, vshader_v_shbin_size); shaderProgramInit(&program); shaderProgramSetVsh(&program, &vshader_dvlb->DVLE[0]); C3D_BindProgram(&program); // Get the location of the uniforms uLoc_projection = shaderInstanceGetUniformLocation(program.vertexShader, "projection"); // Configure attributes for use with the vertex shader C3D_AttrInfo* attrInfo = C3D_GetAttrInfo(); AttrInfo_Init(attrInfo); AttrInfo_AddLoader(attrInfo, 0, GPU_FLOAT, 3); // v0=position AttrInfo_AddLoader(attrInfo, 1, GPU_FLOAT, 2); // v1=texcoord if(scn == GFX_TOP) { // Compute the projection matrix Mtx_OrthoTilt(&projection, 0.0, 400.0, 240.0, 0.0, 0.0, 1.0); } else { Mtx_OrthoTilt(&projection, 0.0, 320.0, 240.0, 0.0, 0.0, 1.0); } // Configure depth test to overwrite pixels with the same depth (needed to draw overlapping glyphs) C3D_DepthTest(true, GPU_GEQUAL, GPU_WRITE_ALL); // Load the glyph texture sheets int i; TGLP_s* glyphInfo = fontGetGlyphInfo(); glyphSheets = malloc(sizeof(C3D_Tex)*glyphInfo->nSheets); for (i = 0; i < glyphInfo->nSheets; i ++) { C3D_Tex* tex = &glyphSheets[i]; tex->data = fontGetGlyphSheetTex(i); tex->fmt = glyphInfo->sheetFmt; tex->size = glyphInfo->sheetSize; tex->width = glyphInfo->sheetWidth; tex->height = glyphInfo->sheetHeight; tex->param = GPU_TEXTURE_MAG_FILTER(GPU_LINEAR) | GPU_TEXTURE_MIN_FILTER(GPU_LINEAR) | GPU_TEXTURE_WRAP_S(GPU_CLAMP_TO_EDGE) | GPU_TEXTURE_WRAP_T(GPU_CLAMP_TO_EDGE); } // Create the text vertex array textVtxArray = (textVertex_s*)linearAlloc(sizeof(textVertex_s)*TEXT_VTX_ARRAY_COUNT); }
void roomInit() { roomDvlb = DVLB_ParseFile((u32*)room_vsh_shbin, room_vsh_shbin_size); if(!roomDvlb)return; shaderProgramInit(&roomProgram); shaderProgramSetVsh(&roomProgram, &roomDvlb->DVLE[0]); roomUniformTextureDimensions = shaderInstanceGetUniformLocation(roomProgram.vertexShader, "textureDimensions"); }
static void sceneInit(void) { // Load the shaders and create a shader program // The geoshader stride is set to 6 so that it processes a triangle at a time vshader_dvlb = DVLB_ParseFile((u32*)vshader_shbin, vshader_shbin_size); gshader_dvlb = DVLB_ParseFile((u32*)gshader_shbin, gshader_shbin_size); shaderProgramInit(&program); shaderProgramSetVsh(&program, &vshader_dvlb->DVLE[0]); shaderProgramSetGsh(&program, &gshader_dvlb->DVLE[0], 6); // Get the location of the projection matrix uniform uLoc_projection = shaderInstanceGetUniformLocation(program.geometryShader, "projection"); // Compute the projection matrix m4x4_ortho_tilt(&projection, 0.0, 400.0, 0.0, 240.0, 0.0, 1.0); // Create the VBO (vertex buffer object) vbo_data = linearAlloc(sizeof(vertex_list)); memcpy(vbo_data, vertex_list, sizeof(vertex_list)); }
int sf2d_init_advanced(int gpucmd_size, int temppool_size) { if (sf2d_initialized) return 0; gpu_fb_addr = vramMemAlign(400*240*8, 0x100); gpu_depth_fb_addr = vramMemAlign(400*240*8, 0x100); gpu_cmd = linearAlloc(gpucmd_size * 4); pool_addr = linearAlloc(temppool_size); pool_size = temppool_size; gpu_cmd_size = gpucmd_size; //gfxInitDefault(); GPU_Init(NULL); //gfxSet3D(false); GPU_Reset(NULL, gpu_cmd, gpucmd_size); //Setup the shader dvlb = DVLB_ParseFile((u32 *)shader_vsh_shbin, shader_vsh_shbin_size); shaderProgramInit(&shader); shaderProgramSetVsh(&shader, &dvlb->DVLE[0]); //Get shader uniform descriptors projection_desc = shaderInstanceGetUniformLocation(shader.vertexShader, "projection"); shaderProgramUse(&shader); matrix_init_orthographic(ortho_matrix_top, 0.0f, 400.0f, 0.0f, 240.0f, 0.0f, 1.0f); matrix_init_orthographic(ortho_matrix_bot, 0.0f, 320.0f, 0.0f, 240.0f, 0.0f, 1.0f); matrix_gpu_set_uniform(ortho_matrix_top, projection_desc); //Register the apt callback hook //aptHook(&apt_hook_cookie, apt_hook_func, NULL); vblank_wait = 1; current_fps = 0.0f; frames = 0; last_time = osGetTime(); cur_screen = GFX_TOP; cur_side = GFX_LEFT; GPUCMD_Finalize(); GPUCMD_FlushAndRun(); gspWaitForP3D(); sf2d_pool_reset(); sf2d_initialized = 1; return 1; }
void portalInit() { portalDvlb = DVLB_ParseFile((u32*)portal_vsh_shbin, portal_vsh_shbin_size); if(!portalDvlb)return; shaderProgramInit(&portalProgram); shaderProgramSetVsh(&portalProgram, &portalDvlb->DVLE[0]); generatePortalGeometry(&portalVertexData, &portalNumVertices, &portalOutlineVertexData, &portalOutlineNumVertices); int i; for(i=0; i<NUM_PORTALS; i++)initPortal(&portals[i]); }
void init_gpu_stuff() { // Load the vertex shader, create a shader program and bind it vshader_dvlb = DVLB_ParseFile((u32*) vshader_shbin, vshader_shbin_size); shaderProgramInit(&program); shaderProgramSetVsh(&program, &vshader_dvlb->DVLE[0]); C3D_BindProgram(&program); // Get the location of the uniforms proj_uloc = shaderInstanceGetUniformLocation(program.vertexShader, "projection"); // Configure attributes for use with the vertex shader // Attribute format and element count are ignored in immediate mode C3D_AttrInfo* attrInfo = C3D_GetAttrInfo(); AttrInfo_Init(attrInfo); AttrInfo_AddLoader(attrInfo, 0, GPU_FLOAT, 3); // v0=position AttrInfo_AddLoader(attrInfo, 1, GPU_FLOAT, 2); // v1=texcoord AttrInfo_AddLoader(attrInfo, 2, GPU_FLOAT, 4); // v2=color // Compute the projection matrix // Note: we're setting top to 240 here so origin is at top left. Mtx_OrthoTilt(&proj_mat, 0.0, 400.0, 240.0, 0.0, 0.0, 1.0, true); // Configure buffers C3D_BufInfo* bufInfo = C3D_GetBufInfo(); BufInfo_Init(bufInfo); // Load textures load_texture(&background_tex, background_png, background_png_size); load_texture(&empty_tex, empty_png, empty_png_size); load_texture(&frame_tex, frame_png, frame_png_size); load_texture(&text_tex, ui_font_png, ui_font_png_size); load_texture(&tileset_tex, tileset_png, tileset_png_size); load_texture(&outline_tex, outline_png, outline_png_size); // Configure the first fragment shading substage to just pass through the texture color // See https://www.opengl.org/sdk/docs/man2/xhtml/glTexEnv.xml for more insight C3D_TexEnv* env = C3D_GetTexEnv(0); C3D_TexEnvSrc(env, C3D_Both, GPU_TEXTURE0, GPU_PRIMARY_COLOR, GPU_PRIMARY_COLOR); //C3D_TexEnvSrc(env, C3D_Both, GPU_TEXTURE0, 0, 0); //C3D_TexEnvOp(env, C3D_Both, 0, 0, 0); C3D_TexEnvFunc(env, C3D_Both, GPU_MODULATE); //C3D_TexEnvFunc(env, C3D_Both, GPU_REPLACE); // Configure depth test to overwrite pixels with the same depth (needed to draw overlapping sprites) C3D_DepthTest(true, GPU_GEQUAL, GPU_WRITE_ALL); C3D_FVUnifMtx4x4(GPU_VERTEX_SHADER, proj_uloc, &proj_mat); }
static void sceneInit(void) { // Load the vertex shader and create a shader program vshader_dvlb = DVLB_ParseFile((u32*)vshader_shbin, vshader_shbin_size); shaderProgramInit(&program); shaderProgramSetVsh(&program, &vshader_dvlb->DVLE[0]); // Get the location of the projection matrix uniform uLoc_projection = shaderInstanceGetUniformLocation(program.vertexShader, "projection"); // Compute the projection matrix m4x4_ortho_tilt(&projection, 0.0, 400.0, 0.0, 240.0, 0.0, 1.0); // Create the VBO (vertex buffer object) vbo_data = linearAlloc(sizeof(vertex_list)); memcpy(vbo_data, vertex_list, sizeof(vertex_list)); }
void OSystem_3DS::initGraphics() { _pfGame = Graphics::PixelFormat::createFormatCLUT8(); _pfGameTexture = Graphics::PixelFormat(4, 8, 8, 8, 8, 24, 16, 8, 0); C3D_Init(C3D_DEFAULT_CMDBUF_SIZE); // Initialize the render targets _renderTargetTop = C3D_RenderTargetCreate(240, 400, GPU_RB_RGBA8, GPU_RB_DEPTH24_STENCIL8); C3D_RenderTargetSetClear(_renderTargetTop, C3D_CLEAR_ALL, 0x0000000, 0); C3D_RenderTargetSetOutput(_renderTargetTop, GFX_TOP, GFX_LEFT, DISPLAY_TRANSFER_FLAGS); _renderTargetBottom = C3D_RenderTargetCreate(240, 320, GPU_RB_RGBA8, GPU_RB_DEPTH24_STENCIL8); C3D_RenderTargetSetClear(_renderTargetBottom, C3D_CLEAR_ALL, 0x00000000, 0); C3D_RenderTargetSetOutput(_renderTargetBottom, GFX_BOTTOM, GFX_LEFT, DISPLAY_TRANSFER_FLAGS); // Load and bind simple default shader (shader.v.pica) _dvlb = DVLB_ParseFile((u32*)shader_shbin, shader_shbin_size); shaderProgramInit(&_program); shaderProgramSetVsh(&_program, &_dvlb->DVLE[0]); C3D_BindProgram(&_program); _projectionLocation = shaderInstanceGetUniformLocation(_program.vertexShader, "projection"); _modelviewLocation = shaderInstanceGetUniformLocation(_program.vertexShader, "modelView"); C3D_AttrInfo *attrInfo = C3D_GetAttrInfo(); AttrInfo_Init(attrInfo); AttrInfo_AddLoader(attrInfo, 0, GPU_FLOAT, 3); // v0=position AttrInfo_AddLoader(attrInfo, 1, GPU_FLOAT, 2); // v1=texcoord Mtx_OrthoTilt(&_projectionTop, 0.0, 400.0, 240.0, 0.0, 0.0, 1.0); Mtx_OrthoTilt(&_projectionBottom, 0.0, 320.0, 240.0, 0.0, 0.0, 1.0); C3D_TexEnv *env = C3D_GetTexEnv(0); C3D_TexEnvSrc(env, C3D_Both, GPU_TEXTURE0, 0, 0); C3D_TexEnvOp(env, C3D_Both, 0, 0, 0); C3D_TexEnvFunc(env, C3D_Both, GPU_REPLACE); C3D_DepthTest(false, GPU_GEQUAL, GPU_WRITE_ALL); C3D_CullFace(GPU_CULL_NONE); }
bool ctrInitGpu() { // Load vertex shader binary uiShader = DVLB_ParseFile((u32*) uishader, uishader_size); if (uiShader == NULL) { return false; } // Create shader shaderProgramInit(&uiProgram); Result res = shaderProgramSetVsh(&uiProgram, &uiShader->DVLE[0]); if (res < 0) { return false; } res = shaderProgramSetGsh(&uiProgram, &uiShader->DVLE[1], 4); if (res < 0) { return false; } C3D_BindProgram(&uiProgram); GSH_FVEC_projectionMtx = shaderInstanceGetUniformLocation(uiProgram.geometryShader, "projectionMtx"); GSH_FVEC_textureMtx = shaderInstanceGetUniformLocation(uiProgram.geometryShader, "textureMtx"); // Allocate buffers ctrVertexBuffer = linearAlloc(VERTEX_BUFFER_SIZE); if (ctrVertexBuffer == NULL) { return false; } C3D_CullFace(GPU_CULL_NONE); C3D_DepthTest(false, GPU_ALWAYS, GPU_WRITE_ALL); C3D_AlphaBlend(GPU_BLEND_ADD, GPU_BLEND_ADD, GPU_SRC_ALPHA, GPU_ONE_MINUS_SRC_ALPHA, GPU_SRC_ALPHA, GPU_ONE_MINUS_SRC_ALPHA); C3D_AlphaTest(false, GPU_ALWAYS, 0); C3D_BlendingColor(0); C3D_AttrInfo* attrInfo = C3D_GetAttrInfo(); AttrInfo_Init(attrInfo); AttrInfo_AddLoader(attrInfo, 0, GPU_SHORT, 4); // in_pos AttrInfo_AddLoader(attrInfo, 1, GPU_SHORT, 4); // in_tc0 AttrInfo_AddLoader(attrInfo, 2, GPU_UNSIGNED_BYTE, 4); // in_col AttrInfo_AddLoader(attrInfo, 3, GPU_FLOAT, 2); // in_rot return true; }
void initialize(void) { // Load the vertex shader, create a shader program and bind it vshader_dvlb = DVLB_ParseFile((u32*)vshader_shbin, vshader_shbin_size); shaderProgramInit(&program); shaderProgramSetVsh(&program, &vshader_dvlb->DVLE[0]); C3D_BindProgram(&program); // Get the location of the uniforms uLoc_projection = shaderInstanceGetUniformLocation(program.vertexShader, "projection"); // Compute the projection matrix Mtx_OrthoTilt(&projection, -200.0, 200.0, -120.0, 120.0, 0.0, 1.0); // Update the uniforms C3D_FVUnifMtx4x4(GPU_VERTEX_SHADER, uLoc_projection, &projection); // Create the VBO (vertex buffer object) vbo_data = linearAlloc(sizeof(vertices)); memcpy(vbo_data, vertices, sizeof(vertices)); // Configure buffers C3D_BufInfo* bufInfo = C3D_GetBufInfo(); BufInfo_Init(bufInfo); BufInfo_Add(bufInfo, vbo_data, sizeof(vertices[0]), 2, 0x10); // Configure attributes for use with the vertex shader C3D_AttrInfo* attrInfo = C3D_GetAttrInfo(); AttrInfo_Init(attrInfo); AttrInfo_AddLoader(attrInfo, 0, GPU_FLOAT, 2); // v0=x,y AttrInfo_AddLoader(attrInfo, 1, GPU_FLOAT, 3); // v1=r,g,b // Configure the first fragment shading substage to just pass through the vertex color // See https://www.opengl.org/sdk/docs/man2/xhtml/glTexEnv.xml for more insight C3D_TexEnv* env = C3D_GetTexEnv(0); C3D_TexEnvSrc(env, C3D_Both, GPU_PRIMARY_COLOR, 0, 0); C3D_TexEnvOp(env, C3D_Both, 0, 0, 0); C3D_TexEnvFunc(env, C3D_Both, GPU_REPLACE); }
static void sceneInit(void) { // Load the vertex shader, create a shader program and bind it vshader_dvlb = DVLB_ParseFile((u32*)vshader_shbin, vshader_shbin_size); shaderProgramInit(&program); shaderProgramSetVsh(&program, &vshader_dvlb->DVLE[0]); C3D_BindProgram(&program); // Get the location of the uniforms uLoc_projection = shaderInstanceGetUniformLocation(program.vertexShader, "Projection"); // Configure attributes for use with the vertex shader C3D_AttrInfo* attrInfo = C3D_GetAttrInfo(); AttrInfo_Init(attrInfo); AttrInfo_AddLoader(attrInfo, 0, GPU_FLOAT, 3); // v0=position AttrInfo_AddFixed(attrInfo, 1); // v1=color // Set the fixed attribute (color) to solid white C3D_FixedAttribSet(1, 1.0, 1.0, 1.0, 1.0); // Compute the projection matrix Mtx_OrthoTilt(&projection, 0.0, 400.0, 0.0, 240.0, 0.0, 1.0); // Create the VBO (vertex buffer object) vbo_data = linearAlloc(sizeof(vertex_list)); memcpy(vbo_data, vertex_list, sizeof(vertex_list)); // Configure buffers C3D_BufInfo* bufInfo = C3D_GetBufInfo(); BufInfo_Init(bufInfo); BufInfo_Add(bufInfo, vbo_data, sizeof(vertex), 1, 0x0); // Configure the first fragment shading substage to just pass through the vertex color // See https://www.opengl.org/sdk/docs/man2/xhtml/glTexEnv.xml for more insight C3D_TexEnv* env = C3D_GetTexEnv(0); C3D_TexEnvSrc(env, C3D_Both, GPU_PRIMARY_COLOR, 0, 0); C3D_TexEnvOp(env, C3D_Both, 0, 0, 0); C3D_TexEnvFunc(env, C3D_Both, GPU_REPLACE); }
static void sceneInit(void) { // Load the shaders and create a shader program // The geoshader stride is set to 6 so that it processes a triangle at a time program_dvlb = DVLB_ParseFile((u32*)program_shbin, program_shbin_size); shaderProgramInit(&program); shaderProgramSetVsh(&program, &program_dvlb->DVLE[0]); shaderProgramSetGsh(&program, &program_dvlb->DVLE[1], 6); C3D_BindProgram(&program); // Get the location of the projection matrix uniform uLoc_projection = shaderInstanceGetUniformLocation(program.geometryShader, "projection"); // Configure attributes for use with the vertex shader C3D_AttrInfo* attrInfo = C3D_GetAttrInfo(); AttrInfo_Init(attrInfo); AttrInfo_AddLoader(attrInfo, 0, GPU_FLOAT, 3); // v0=position AttrInfo_AddLoader(attrInfo, 1, GPU_FLOAT, 4); // v1=color // Compute the projection matrix Mtx_OrthoTilt(&projection, 0.0, 400.0, 0.0, 240.0, 0.0, 1.0); // Create the VBO (vertex buffer object) vbo_data = linearAlloc(sizeof(vertex_list)); memcpy(vbo_data, vertex_list, sizeof(vertex_list)); // Configure buffers C3D_BufInfo* bufInfo = C3D_GetBufInfo(); BufInfo_Init(bufInfo); BufInfo_Add(bufInfo, vbo_data, sizeof(vertex), 2, 0x10); // Configure the first fragment shading substage to just pass through the vertex color // See https://www.opengl.org/sdk/docs/man2/xhtml/glTexEnv.xml for more insight C3D_TexEnv* env = C3D_GetTexEnv(0); C3D_TexEnvSrc(env, C3D_Both, GPU_PRIMARY_COLOR, 0, 0); C3D_TexEnvOp(env, C3D_Both, 0, 0, 0); C3D_TexEnvFunc(env, C3D_Both, GPU_REPLACE); }
void gpuLoadShader(u32 shader, const void* data, u32 size, u8 geometryStride) { if(data == NULL) { return; } ShaderData* shdr = (ShaderData*) shader; if(shdr == NULL) { return; } if(shdr->dvlb != NULL) { shaderProgramFree(&shdr->program); DVLB_Free(shdr->dvlb); } shdr->dvlb = DVLB_ParseFile((u32*) data, size); shaderProgramInit(&shdr->program); if(shdr->dvlb->numDVLE > 0) { shaderProgramSetVsh(&shdr->program, &shdr->dvlb->DVLE[0]); if(shdr->dvlb->numDVLE > 1) { shaderProgramSetGsh(&shdr->program, &shdr->dvlb->DVLE[1], geometryStride); } } }
bool gfxInit() { gfxInitDefault(); if(!C3D_Init(C3D_DEFAULT_CMDBUF_SIZE)) { gfxCleanup(); return false; } c3dInitialized = true; u32 displayFlags = GX_TRANSFER_FLIP_VERT(0) | GX_TRANSFER_OUT_TILED(0) | GX_TRANSFER_RAW_COPY(0) | GX_TRANSFER_IN_FORMAT(GX_TRANSFER_FMT_RGBA8) | GX_TRANSFER_OUT_FORMAT(GX_TRANSFER_FMT_RGB8) | GX_TRANSFER_SCALING(GX_TRANSFER_SCALE_NO); targetTop = C3D_RenderTargetCreate(240, 400, GPU_RB_RGBA8, GPU_RB_DEPTH24_STENCIL8); if(targetTop == NULL) { gfxCleanup(); return false; } C3D_RenderTargetSetClear(targetTop, C3D_CLEAR_ALL, 0, 0); C3D_RenderTargetSetOutput(targetTop, GFX_TOP, GFX_LEFT, displayFlags); targetBottom = C3D_RenderTargetCreate(240, 320, GPU_RB_RGBA8, GPU_RB_DEPTH24_STENCIL8); if(targetBottom == NULL) { gfxCleanup(); return false; } C3D_RenderTargetSetClear(targetBottom, C3D_CLEAR_ALL, 0, 0); C3D_RenderTargetSetOutput(targetBottom, GFX_BOTTOM, GFX_LEFT, displayFlags); dvlb = DVLB_ParseFile((u32*) default_shbin, default_shbin_len); if(dvlb == NULL) { gfxCleanup(); return false; } Result progInitRes = shaderProgramInit(&program); if(R_FAILED(progInitRes)) { gfxCleanup(); return false; } shaderInitialized = true; Result progSetVshRes = shaderProgramSetVsh(&program, &dvlb->DVLE[0]); if(R_FAILED(progSetVshRes)) { gfxCleanup(); return false; } C3D_BindProgram(&program); C3D_AttrInfo* attrInfo = C3D_GetAttrInfo(); if(attrInfo == NULL) { gfxCleanup(); return false; } AttrInfo_Init(attrInfo); AttrInfo_AddLoader(attrInfo, 0, GPU_FLOAT, 3); AttrInfo_AddLoader(attrInfo, 1, GPU_FLOAT, 2); C3D_TexEnv* env = C3D_GetTexEnv(0); if(env == NULL) { gfxCleanup(); return false; } C3D_TexEnvSrc(env, C3D_Both, GPU_TEXTURE0, 0, 0); C3D_TexEnvOp(env, C3D_Both, 0, 0, 0); C3D_TexEnvFunc(env, C3D_Both, GPU_REPLACE); C3D_DepthTest(true, GPU_GEQUAL, GPU_WRITE_ALL); Mtx_OrthoTilt(&projectionTop, 0.0, 400.0, 240.0, 0.0, 0.0, 1.0); Mtx_OrthoTilt(&projectionBottom, 0.0, 320.0, 240.0, 0.0, 0.0, 1.0); screenInit = false; borderInit = false; // Allocate and clear the screen buffer. screenBuffer = (u32*) linearAlloc(256 * 256 * sizeof(u32)); memset(screenBuffer, 0, 256 * 256 * sizeof(u32)); // Allocate and clear the scale2x buffer. scale2xBuffer = (u32*) linearAlloc(512 * 512 * sizeof(u32)); memset(scale2xBuffer, 0, 512 * 512 * sizeof(u32)); return true; }
bool ShaderProgram::load(const byte* vsh_data, Uint32 vsh_data_size) { unload(); if(vsh_data == nullptr || vsh_data_size == 0 || !RenderDevice::getInstance()->isInitialized()) { return false; } #ifdef _3DS _vertexShader = DVLB_ParseFile((u32*)vsh_data, vsh_data_size); if(_vertexShader == nullptr) { return false; } if(!_initialized) { shaderProgramInit(&_shaderProgram); _initialized = true; } shaderProgramSetVsh(&_shaderProgram, &_vertexShader->DVLE[0]); #else constexpr const char* fShaderData = "#version 330 core\n" "in vec4 color;\n" "in vec2 texCoords;\n" "out vec4 outColor;\n" "uniform sampler2D uTexture;\n" "uniform bool textureEnabled = true;\n" "void main() {\n" "if(textureEnabled) {" "outColor = texture(uTexture, texCoords) * color;\n" "} else {\n" "outColor = color;\n" "}\n" "}\n"; const GLint fShaderDataLen = strlen(fShaderData); const char* vShaderData = (const char*)vsh_data; const GLint vShaderDataLen = vsh_data_size; GLuint vshaderId = glCreateShader(GL_VERTEX_SHADER); GLuint fshaderId = glCreateShader(GL_FRAGMENT_SHADER); if (vshaderId == 0) { return false; } if (fshaderId == 0) { return false; } _program = glCreateProgram(); if (_program == 0) { return false; } glShaderSource(fshaderId, 1, &fShaderData, &fShaderDataLen); glShaderSource(vshaderId, 1, &vShaderData, &vShaderDataLen); glCompileShader(fshaderId); glCompileShader(vshaderId); GLint result = 0; glGetShaderiv(vshaderId, GL_COMPILE_STATUS, &result); if (result == GL_FALSE) { return false; } glGetShaderiv(fshaderId, GL_COMPILE_STATUS, &result); if (result == GL_FALSE) { return false; } glAttachShader(_program, vshaderId); glAttachShader(_program, fshaderId); glLinkProgram(_program); glGetProgramiv(_program, GL_LINK_STATUS, &result); if (result == GL_FALSE) { return false; } glValidateProgram(_program); glDeleteShader(vshaderId); glDeleteShader(fshaderId); _initialized = true; #endif // _3DS return true; }
static void* ctr_init(const video_info_t* video, const input_driver_t** input, void** input_data) { float refresh_rate; void* ctrinput = NULL; ctr_video_t* ctr = (ctr_video_t*)linearAlloc(sizeof(ctr_video_t)); if (!ctr) return NULL; memset(ctr, 0, sizeof(ctr_video_t)); ctr->vp.x = 0; ctr->vp.y = 0; ctr->vp.width = CTR_TOP_FRAMEBUFFER_WIDTH; ctr->vp.height = CTR_TOP_FRAMEBUFFER_HEIGHT; ctr->vp.full_width = CTR_TOP_FRAMEBUFFER_WIDTH; ctr->vp.full_height = CTR_TOP_FRAMEBUFFER_HEIGHT; ctr->display_list_size = 0x400; ctr->display_list = linearAlloc(ctr->display_list_size * sizeof(uint32_t)); GPU_Reset(NULL, ctr->display_list, ctr->display_list_size); ctr->rgb32 = video->rgb32; ctr->texture_width = video->input_scale * RARCH_SCALE_BASE; ctr->texture_height = video->input_scale * RARCH_SCALE_BASE; ctr->texture_linear = linearMemAlign(ctr->texture_width * ctr->texture_height * (ctr->rgb32? 4:2), 128); ctr->texture_swizzled = linearMemAlign(ctr->texture_width * ctr->texture_height * (ctr->rgb32? 4:2), 128); ctr->frame_coords = linearAlloc(3 * sizeof(ctr_vertex_t)); ctr->frame_coords->x0 = 0; ctr->frame_coords->y0 = 0; ctr->frame_coords->x1 = CTR_TOP_FRAMEBUFFER_WIDTH; ctr->frame_coords->y1 = CTR_TOP_FRAMEBUFFER_HEIGHT; ctr->frame_coords->u0 = 0; ctr->frame_coords->v0 = 0; ctr->frame_coords->u1 = CTR_TOP_FRAMEBUFFER_WIDTH; ctr->frame_coords->v1 = CTR_TOP_FRAMEBUFFER_HEIGHT; GSPGPU_FlushDataCache(ctr->frame_coords, sizeof(ctr_vertex_t)); ctr->menu.texture_width = 512; ctr->menu.texture_height = 512; ctr->menu.texture_linear = linearMemAlign(ctr->menu.texture_width * ctr->menu.texture_height * sizeof(uint16_t), 128); ctr->menu.texture_swizzled = linearMemAlign(ctr->menu.texture_width * ctr->menu.texture_height * sizeof(uint16_t), 128); ctr->menu.frame_coords = linearAlloc(sizeof(ctr_vertex_t)); ctr->menu.frame_coords->x0 = 40; ctr->menu.frame_coords->y0 = 0; ctr->menu.frame_coords->x1 = CTR_TOP_FRAMEBUFFER_WIDTH - 40; ctr->menu.frame_coords->y1 = CTR_TOP_FRAMEBUFFER_HEIGHT; ctr->menu.frame_coords->u0 = 0; ctr->menu.frame_coords->v0 = 0; ctr->menu.frame_coords->u1 = CTR_TOP_FRAMEBUFFER_WIDTH - 80; ctr->menu.frame_coords->v1 = CTR_TOP_FRAMEBUFFER_HEIGHT; GSPGPU_FlushDataCache(ctr->menu.frame_coords, sizeof(ctr_vertex_t)); ctr_set_scale_vector(&ctr->scale_vector, CTR_TOP_FRAMEBUFFER_WIDTH, CTR_TOP_FRAMEBUFFER_HEIGHT, ctr->texture_width, ctr->texture_height); ctr_set_scale_vector(&ctr->menu.scale_vector, CTR_TOP_FRAMEBUFFER_WIDTH, CTR_TOP_FRAMEBUFFER_HEIGHT, ctr->menu.texture_width, ctr->menu.texture_height); ctr->dvlb = DVLB_ParseFile((u32*)ctr_sprite_shbin, ctr_sprite_shbin_size); ctrGuSetVshGsh(&ctr->shader, ctr->dvlb, 2, 2); shaderProgramUse(&ctr->shader); GPU_SetViewport(VIRT_TO_PHYS(CTR_GPU_DEPTHBUFFER), VIRT_TO_PHYS(CTR_TOP_FRAMEBUFFER), 0, 0, CTR_TOP_FRAMEBUFFER_HEIGHT, CTR_TOP_FRAMEBUFFER_WIDTH); GPU_DepthMap(-1.0f, 0.0f); GPU_SetFaceCulling(GPU_CULL_NONE); GPU_SetStencilTest(false, GPU_ALWAYS, 0x00, 0xFF, 0x00); GPU_SetStencilOp(GPU_STENCIL_KEEP, GPU_STENCIL_KEEP, GPU_STENCIL_KEEP); GPU_SetBlendingColor(0, 0, 0, 0); // GPU_SetDepthTestAndWriteMask(true, GPU_GREATER, GPU_WRITE_ALL); GPU_SetDepthTestAndWriteMask(false, GPU_ALWAYS, GPU_WRITE_ALL); // GPU_SetDepthTestAndWriteMask(true, GPU_ALWAYS, GPU_WRITE_ALL); GPUCMD_AddMaskedWrite(GPUREG_EARLYDEPTH_TEST1, 0x1, 0); GPUCMD_AddWrite(GPUREG_EARLYDEPTH_TEST2, 0); GPU_SetAlphaBlending(GPU_BLEND_ADD, GPU_BLEND_ADD, GPU_SRC_ALPHA, GPU_ONE_MINUS_SRC_ALPHA, GPU_SRC_ALPHA, GPU_ONE_MINUS_SRC_ALPHA); GPU_SetAlphaTest(false, GPU_ALWAYS, 0x00); GPU_SetTextureEnable(GPU_TEXUNIT0); GPU_SetTexEnv(0, GPU_TEXTURE0, GPU_TEXTURE0, 0, 0, GPU_REPLACE, GPU_REPLACE, 0); GPU_SetTexEnv(1, GPU_PREVIOUS, GPU_PREVIOUS, 0, 0, 0, 0, 0); GPU_SetTexEnv(2, GPU_PREVIOUS, GPU_PREVIOUS, 0, 0, 0, 0, 0); GPU_SetTexEnv(3, GPU_PREVIOUS, GPU_PREVIOUS, 0, 0, 0, 0, 0); GPU_SetTexEnv(4, GPU_PREVIOUS, GPU_PREVIOUS, 0, 0, 0, 0, 0); GPU_SetTexEnv(5, GPU_PREVIOUS, GPU_PREVIOUS, 0, 0, 0, 0, 0); ctrGuSetAttributeBuffers(2, VIRT_TO_PHYS(ctr->menu.frame_coords), CTRGU_ATTRIBFMT(GPU_SHORT, 4) << 0 | CTRGU_ATTRIBFMT(GPU_SHORT, 4) << 4, sizeof(ctr_vertex_t)); GPUCMD_Finalize(); ctrGuFlushAndRun(true); gspWaitForEvent(GSPGPU_EVENT_P3D, false); if (input && input_data) { ctrinput = input_ctr.init(); *input = ctrinput ? &input_ctr : NULL; *input_data = ctrinput; } ctr->keep_aspect = true; ctr->should_resize = true; ctr->smooth = video->smooth; ctr->vsync = video->vsync; ctr->lcd_buttom_on = true; ctr->current_buffer_top = 0; ctr->empty_framebuffer = linearAlloc(320 * 240 * 2); memset(ctr->empty_framebuffer, 0, 320 * 240 * 2); refresh_rate = (32730.0 * 8192.0) / 4481134.0; driver_ctl(RARCH_DRIVER_CTL_SET_REFRESH_RATE, &refresh_rate); aptHook(&ctr->lcd_aptHook, ctr_lcd_aptHook, ctr); return ctr; }
static void* ctr_init(const video_info_t* video, const input_driver_t** input, void** input_data) { void* ctrinput = NULL; ctr_video_t* ctr = (ctr_video_t*)linearAlloc(sizeof(ctr_video_t)); if (!ctr) return NULL; // gfxInitDefault(); // gfxSet3D(false); memset(ctr, 0, sizeof(ctr_video_t)); ctr->display_list_size = 0x40000; ctr->display_list = linearAlloc(ctr->display_list_size * sizeof(uint32_t)); GPU_Reset(NULL, ctr->display_list, ctr->display_list_size); ctr->texture_width = 512; ctr->texture_height = 512; ctr->texture_linear = linearMemAlign(ctr->texture_width * ctr->texture_height * sizeof(uint32_t), 128); ctr->texture_swizzled = linearMemAlign(ctr->texture_width * ctr->texture_height * sizeof(uint32_t), 128); ctr->frame_coords = linearAlloc(sizeof(ctr_vertex_t)); ctr->frame_coords->x0 = 0; ctr->frame_coords->y0 = 0; ctr->frame_coords->x1 = CTR_TOP_FRAMEBUFFER_WIDTH; ctr->frame_coords->y1 = CTR_TOP_FRAMEBUFFER_HEIGHT; ctr->frame_coords->u = CTR_TOP_FRAMEBUFFER_WIDTH; ctr->frame_coords->v = CTR_TOP_FRAMEBUFFER_HEIGHT; GSPGPU_FlushDataCache(NULL, (u8*)ctr->frame_coords, sizeof(ctr_vertex_t)); ctr->menu.texture_width = 512; ctr->menu.texture_height = 512; ctr->menu.texture_linear = linearMemAlign(ctr->texture_width * ctr->texture_height * sizeof(uint16_t), 128); ctr->menu.texture_swizzled = linearMemAlign(ctr->texture_width * ctr->texture_height * sizeof(uint16_t), 128); ctr->menu.frame_coords = linearAlloc(sizeof(ctr_vertex_t)); ctr->menu.frame_coords->x0 = 40; ctr->menu.frame_coords->y0 = 0; ctr->menu.frame_coords->x1 = CTR_TOP_FRAMEBUFFER_WIDTH - 40; ctr->menu.frame_coords->y1 = CTR_TOP_FRAMEBUFFER_HEIGHT; ctr->menu.frame_coords->u = CTR_TOP_FRAMEBUFFER_WIDTH - 80; ctr->menu.frame_coords->v = CTR_TOP_FRAMEBUFFER_HEIGHT; GSPGPU_FlushDataCache(NULL, (u8*)ctr->menu.frame_coords, sizeof(ctr_vertex_t)); ctr_set_scale_vector(&ctr->scale_vector, CTR_TOP_FRAMEBUFFER_WIDTH, CTR_TOP_FRAMEBUFFER_HEIGHT, ctr->texture_width, ctr->texture_height); ctr_set_scale_vector(&ctr->menu.scale_vector, CTR_TOP_FRAMEBUFFER_WIDTH, CTR_TOP_FRAMEBUFFER_HEIGHT, ctr->menu.texture_width, ctr->menu.texture_height); ctr->dvlb = DVLB_ParseFile((u32*)ctr_sprite_shader_shbin, ctr_sprite_shader_shbin_size); ctrGuSetVshGsh(&ctr->shader, ctr->dvlb, 2, 2); shaderProgramUse(&ctr->shader); GPU_SetViewport(VIRT_TO_PHYS(CTR_GPU_DEPTHBUFFER), VIRT_TO_PHYS(CTR_GPU_FRAMEBUFFER), 0, 0, CTR_TOP_FRAMEBUFFER_HEIGHT, CTR_TOP_FRAMEBUFFER_WIDTH); // GPU_SetViewport(NULL, // VIRT_TO_PHYS(CTR_GPU_FRAMEBUFFER), // 0, 0, CTR_TOP_FRAMEBUFFER_HEIGHT, CTR_TOP_FRAMEBUFFER_WIDTH); GPU_DepthMap(-1.0f, 0.0f); GPU_SetFaceCulling(GPU_CULL_NONE); GPU_SetStencilTest(false, GPU_ALWAYS, 0x00, 0xFF, 0x00); GPU_SetStencilOp(GPU_KEEP, GPU_KEEP, GPU_KEEP); GPU_SetBlendingColor(0, 0, 0, 0); // GPU_SetDepthTestAndWriteMask(true, GPU_GREATER, GPU_WRITE_ALL); GPU_SetDepthTestAndWriteMask(false, GPU_ALWAYS, GPU_WRITE_ALL); // GPU_SetDepthTestAndWriteMask(true, GPU_ALWAYS, GPU_WRITE_ALL); GPUCMD_AddMaskedWrite(GPUREG_0062, 0x1, 0); GPUCMD_AddWrite(GPUREG_0118, 0); GPU_SetAlphaBlending(GPU_BLEND_ADD, GPU_BLEND_ADD, GPU_SRC_ALPHA, GPU_ONE_MINUS_SRC_ALPHA, GPU_SRC_ALPHA, GPU_ONE_MINUS_SRC_ALPHA); GPU_SetAlphaTest(false, GPU_ALWAYS, 0x00); GPU_SetTextureEnable(GPU_TEXUNIT0); GPU_SetTexEnv(0, GPU_TEVSOURCES(GPU_TEXTURE0, GPU_PRIMARY_COLOR, 0), GPU_TEVSOURCES(GPU_TEXTURE0, GPU_PRIMARY_COLOR, 0), GPU_TEVOPERANDS(0, 0, 0), GPU_TEVOPERANDS(0, 0, 0), GPU_MODULATE, GPU_MODULATE, 0xFFFFFFFF); GPU_SetTexEnv(1, GPU_PREVIOUS,GPU_PREVIOUS, 0, 0, 0, 0, 0); GPU_SetTexEnv(2, GPU_PREVIOUS,GPU_PREVIOUS, 0, 0, 0, 0, 0); GPU_SetTexEnv(3, GPU_PREVIOUS,GPU_PREVIOUS, 0, 0, 0, 0, 0); GPU_SetTexEnv(4, GPU_PREVIOUS,GPU_PREVIOUS, 0, 0, 0, 0, 0); GPU_SetTexEnv(5, GPU_PREVIOUS,GPU_PREVIOUS, 0, 0, 0, 0, 0); ctrGuSetAttributeBuffers(2, VIRT_TO_PHYS(ctr->menu.frame_coords), CTRGU_ATTRIBFMT(GPU_SHORT, 4) << 0 | CTRGU_ATTRIBFMT(GPU_SHORT, 2) << 4, sizeof(ctr_vertex_t)); GPUCMD_Finalize(); ctrGuFlushAndRun(true); gspWaitForEvent(GSPEVENT_P3D, false); if (input && input_data) { ctrinput = input_ctr.init(); *input = ctrinput ? &input_ctr : NULL; *input_data = ctrinput; } return ctr; }
//--------------------------------------------------------------------------------- static void sceneInit(void) { //--------------------------------------------------------------------------------- int i; // Load the vertex shader, create a shader program and bind it vshader_dvlb = DVLB_ParseFile((u32*)vshader_shbin, vshader_shbin_size); shaderProgramInit(&program); shaderProgramSetVsh(&program, &vshader_dvlb->DVLE[0]); C3D_BindProgram(&program); // Get the location of the uniforms uLoc_projection = shaderInstanceGetUniformLocation(program.vertexShader, "projection"); // Configure attributes for use with the vertex shader // Attribute format and element count are ignored in immediate mode C3D_AttrInfo* attrInfo = C3D_GetAttrInfo(); AttrInfo_Init(attrInfo); AttrInfo_AddLoader(attrInfo, 0, GPU_FLOAT, 3); // v0=position AttrInfo_AddLoader(attrInfo, 1, GPU_FLOAT, 2); // v2=texcoord // Compute the projection matrix // Note: we're setting top to 240 here so origin is at top left. Mtx_OrthoTilt(&projection, 0.0, 400.0, 240.0, 0.0, 0.0, 1.0); // Configure buffers C3D_BufInfo* bufInfo = C3D_GetBufInfo(); BufInfo_Init(bufInfo); unsigned char* image; unsigned width, height; lodepng_decode32(&image, &width, &height, ballsprites_png, ballsprites_png_size); u8 *gpusrc = linearAlloc(width*height*4); // GX_DisplayTransfer needs input buffer in linear RAM u8* src=image; u8 *dst=gpusrc; // lodepng outputs big endian rgba so we need to convert for(int i = 0; i<width*height; i++) { int r = *src++; int g = *src++; int b = *src++; int a = *src++; *dst++ = a; *dst++ = b; *dst++ = g; *dst++ = r; } // ensure data is in physical ram GSPGPU_FlushDataCache(gpusrc, width*height*4); // Load the texture and bind it to the first texture unit C3D_TexInit(&spritesheet_tex, width, height, GPU_RGBA8); // Convert image to 3DS tiled texture format C3D_SafeDisplayTransfer ((u32*)gpusrc, GX_BUFFER_DIM(width,height), (u32*)spritesheet_tex.data, GX_BUFFER_DIM(width,height), TEXTURE_TRANSFER_FLAGS); gspWaitForPPF(); C3D_TexSetFilter(&spritesheet_tex, GPU_LINEAR, GPU_NEAREST); C3D_TexBind(0, &spritesheet_tex); free(image); linearFree(gpusrc); // Configure the first fragment shading substage to just pass through the texture color // See https://www.opengl.org/sdk/docs/man2/xhtml/glTexEnv.xml for more insight C3D_TexEnv* env = C3D_GetTexEnv(0); C3D_TexEnvSrc(env, C3D_Both, GPU_TEXTURE0, 0, 0); C3D_TexEnvOp(env, C3D_Both, 0, 0, 0); C3D_TexEnvFunc(env, C3D_Both, GPU_REPLACE); srand(time(NULL)); for(i = 0; i < NUM_SPRITES; i++) { //random place and speed sprites[i].x = (rand() % (400 - 32 )) << 8; sprites[i].y = (rand() % (240 - 32 )) << 8 ; sprites[i].dx = (rand() & 0xFF) + 0x100; sprites[i].dy = (rand() & 0xFF) + 0x100; sprites[i].image = rand() & 3; if(rand() & 1) sprites[i].dx = -sprites[i].dx; if(rand() & 1) sprites[i].dy = -sprites[i].dy; } // Configure depth test to overwrite pixels with the same depth (needed to draw overlapping sprites) C3D_DepthTest(true, GPU_GEQUAL, GPU_WRITE_ALL); }