static uintptr_t vita_load_texture(void *video_data, void *data, bool threaded, enum texture_filter_type filter_type) { unsigned int stride, pitch, j; uint32_t *tex32 = NULL; const uint32_t *frame32 = NULL; struct texture_image *image = (struct texture_image*)data; struct vita2d_texture *texture = vita2d_create_empty_texture_format(image->width, image->height,SCE_GXM_TEXTURE_FORMAT_U8U8U8U8_ARGB); if (!texture) return 0; if ((filter_type == TEXTURE_FILTER_MIPMAP_LINEAR) || (filter_type == TEXTURE_FILTER_LINEAR)) vita2d_texture_set_filters(texture, SCE_GXM_TEXTURE_FILTER_LINEAR, SCE_GXM_TEXTURE_FILTER_LINEAR); stride = vita2d_texture_get_stride(texture); stride /= 4; tex32 = vita2d_texture_get_datap(texture); frame32 = image->pixels; pitch = image->width; for (j = 0; j < image->height; j++) memcpy_neon( &tex32[j*stride], &frame32[j*pitch], pitch * sizeof(uint32_t)); return (uintptr_t)texture; }
static bool vita_get_current_sw_framebuffer(void *data, struct retro_framebuffer *framebuffer) { vita_video_t *vita = (vita_video_t*)data; if (!vita->texture || vita->width != framebuffer->width || vita->height != framebuffer->height) { if(vita->texture) { vita2d_wait_rendering_done(); vita2d_free_texture(vita->texture); vita->texture = NULL; } vita->width = framebuffer->width; vita->height = framebuffer->height; vita->texture = vita2d_create_empty_texture_format( vita->width, vita->height, vita->format); vita2d_texture_set_filters(vita->texture, vita->tex_filter,vita->tex_filter); } framebuffer->data = vita2d_texture_get_datap(vita->texture); framebuffer->pitch = vita2d_texture_get_stride(vita->texture); framebuffer->format = vita->rgb32 ? RETRO_PIXEL_FORMAT_XRGB8888 : RETRO_PIXEL_FORMAT_RGB565; framebuffer->memory_flags = 0; return true; }
Psp2Ui::Psp2Ui(int width, int height) : BaseUi() { starttick = sceKernelGetProcessTimeWide() / 1000; frame = 0; zoom_state = 0; trigger_state = false; vita2d_init(); vita2d_texture_set_alloc_memblock_type(SCE_KERNEL_MEMBLOCK_TYPE_USER_RW); current_display_mode.width = width; current_display_mode.height = height; current_display_mode.bpp = 32; const DynamicFormat format( 32, 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000, PF::Alpha); main_texture = vita2d_create_empty_texture_format( width, height, SCE_GXM_TEXTURE_FORMAT_A8B8G8R8); Bitmap::SetFormat(Bitmap::ChooseFormat(format)); main_surface = Bitmap::Create(vita2d_texture_get_datap(main_texture),width, height, vita2d_texture_get_stride(main_texture), format); #ifdef SUPPORT_AUDIO audio_.reset(new Psp2Audio()); #endif scePowerSetArmClockFrequency(444); sceCtrlSetSamplingMode(SCE_CTRL_MODE_ANALOG); }
static int atlas_add_glyph_pgf(vita2d_pgf *font, unsigned int character) { SceFontCharInfo char_info; if (sceFontGetCharInfo(font->font_handle, character, &char_info) < 0) return 0; int pos_x; int pos_y; if (!texture_atlas_insert(font->tex_atlas, character, char_info.bitmapWidth, char_info.bitmapHeight, char_info.bitmapLeft, char_info.bitmapTop, char_info.sfp26AdvanceH, char_info.sfp26AdvanceV, 0, &pos_x, &pos_y)) return 0; vita2d_texture *tex = font->tex_atlas->tex; SceFontGlyphImage glyph_image; glyph_image.pixelFormat = SCE_FONT_PIXELFORMAT_8; glyph_image.xPos64 = pos_x << 6; glyph_image.yPos64 = pos_y << 6; glyph_image.bufWidth = vita2d_texture_get_width(tex); glyph_image.bufHeight = vita2d_texture_get_height(tex); glyph_image.bytesPerLine = vita2d_texture_get_stride(tex); glyph_image.pad = 0; glyph_image.bufferPtr = (unsigned int)vita2d_texture_get_datap(tex); return sceFontGetCharGlyphImage(font->font_handle, character, &glyph_image) == 0; }
static void vita_set_texture_frame(void *data, const void *frame, bool rgb32, unsigned width, unsigned height, float alpha) { int i, j; void *tex_p; unsigned int stride; vita_video_t *vita = (vita_video_t*)data; (void)alpha; if (width != vita->menu.width && height != vita->menu.height && vita->menu.texture) { vita2d_free_texture(vita->menu.texture); vita->menu.texture = NULL; } if (!vita->menu.texture) { if (rgb32) { vita->menu.texture = vita2d_create_empty_texture(width, height); RARCH_LOG("Creating Frame RGBA8 texture: w: %i h: %i\n", width, height); } else { vita->menu.texture = vita2d_create_empty_texture_format(width, height, SCE_GXM_TEXTURE_FORMAT_U4U4U4U4_RGBA); RARCH_LOG("Creating Frame R5G6B5 texture: w: %i h: %i\n", width, height); } vita->menu.width = width; vita->menu.height = height; } vita2d_texture_set_filters(vita->menu.texture,SCE_GXM_TEXTURE_FILTER_LINEAR,SCE_GXM_TEXTURE_FILTER_LINEAR); tex_p = vita2d_texture_get_datap(vita->menu.texture); stride = vita2d_texture_get_stride(vita->menu.texture); if (rgb32) { uint32_t *tex32 = tex_p; const uint32_t *frame32 = frame; stride /= 4; for (i = 0; i < height; i++) for (j = 0; j < width; j++) tex32[j + i*stride] = frame32[j + i*width]; } else { uint16_t *tex16 = tex_p; const uint16_t *frame16 = frame; stride /= 2; for (i = 0; i < height; i++) for (j = 0; j < width; j++) tex16[j + i*stride] = frame16[j + i*width]; } }
Psp2Ui::Psp2Ui(int width, int height) : BaseUi() { starttick = sceKernelGetProcessTimeWide() / 1000; frame = 0; zoom_state = 0; in_use_shader = 0; touch_x_start = -1; trigger_state = false; set_shader = true; vita2d_init(); vita2d_set_vblank_wait(0); shaders[0] = vita2d_create_shader((SceGxmProgram*) opaque_v, (SceGxmProgram*) texture_f); shaders[1] = vita2d_create_shader((SceGxmProgram*) sharp_bilinear_v, (SceGxmProgram*) sharp_bilinear_f); shaders[2] = vita2d_create_shader((SceGxmProgram*) lcd3x_v, (SceGxmProgram*) lcd3x_f); shaders[3] = vita2d_create_shader((SceGxmProgram*) xbr_2x_fast_v, (SceGxmProgram*) xbr_2x_fast_f); gpu_texture = vita2d_create_empty_texture_format( width, height, SCE_GXM_TEXTURE_FORMAT_A8B8G8R8); vita2d_texture_set_alloc_memblock_type(SCE_KERNEL_MEMBLOCK_TYPE_USER_RW); current_display_mode.width = width; current_display_mode.height = height; current_display_mode.bpp = 32; const DynamicFormat format( 32, 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000, PF::Alpha); main_texture = vita2d_create_empty_texture_format( width, height, SCE_GXM_TEXTURE_FORMAT_A8B8G8R8); next_texture = vita2d_create_empty_texture_format( width, height, SCE_GXM_TEXTURE_FORMAT_A8B8G8R8); Bitmap::SetFormat(Bitmap::ChooseFormat(format)); main_surface = Bitmap::Create(vita2d_texture_get_datap(main_texture),width, height, vita2d_texture_get_stride(main_texture), format); #ifdef SUPPORT_AUDIO audio_.reset(new Psp2Audio()); #endif scePowerSetArmClockFrequency(444); scePowerSetBusClockFrequency(222); scePowerSetGpuClockFrequency(222); scePowerSetGpuXbarClockFrequency(222); sceCtrlSetSamplingMode(SCE_CTRL_MODE_ANALOG); sceTouchSetSamplingState(SCE_TOUCH_PORT_FRONT, SCE_TOUCH_SAMPLING_STATE_START); GPU_Mutex = sceKernelCreateSema("GPU Mutex", 0, 1, 1, NULL); GPU_Cleanup_Mutex = sceKernelCreateSema("GPU Cleanup Mutex", 0, 1, 1, NULL); GPU_Thread = sceKernelCreateThread("GPU Thread", &renderThread, 0x10000100, 0x10000, 0, 0, NULL); sceKernelStartThread(GPU_Thread, sizeof(GPU_Thread), &GPU_Thread); }
static int atlas_add_glyph(vita2d_pgf *font, unsigned int character) { SceFontHandle font_handle = font->font_handle_list->font_handle; SceFontCharInfo char_info; bp2d_position position; void *texture_data; vita2d_texture *tex = font->atlas->texture; vita2d_pgf_font_handle *tmp = font->font_handle_list; while (tmp) { if (tmp->in_font_group == NULL || tmp->in_font_group(character)) { font_handle = tmp->font_handle; break; } tmp = tmp->next; } if (sceFontGetCharInfo(font_handle, character, &char_info) < 0) return 0; bp2d_size size = { char_info.bitmapWidth, char_info.bitmapHeight }; texture_atlas_entry_data data = { char_info.bitmapLeft, char_info.bitmapTop, char_info.sfp26AdvanceH, char_info.sfp26AdvanceV, 0 }; if (!texture_atlas_insert(font->atlas, character, &size, &data, &position)) return 0; texture_data = vita2d_texture_get_datap(tex); SceFontGlyphImage glyph_image; glyph_image.pixelFormat = SCE_FONT_PIXELFORMAT_8; glyph_image.xPos64 = position.x << 6; glyph_image.yPos64 = position.y << 6; glyph_image.bufWidth = vita2d_texture_get_width(tex); glyph_image.bufHeight = vita2d_texture_get_height(tex); glyph_image.bytesPerLine = vita2d_texture_get_stride(tex); glyph_image.pad = 0; glyph_image.bufferPtr = (unsigned int)texture_data; return sceFontGetCharGlyphImage(font_handle, character, &glyph_image) == 0; }
static void *vita2d_font_init_font(void *gl_data, const char *font_path, float font_size) { unsigned int stride, pitch, j, k; const struct font_atlas *atlas = NULL; vita_font_t *font = (vita_font_t*)calloc(1, sizeof(*font)); if (!font) return NULL; if (!font_renderer_create_default((const void**)&font->font_driver, &font->font_data, font_path, font_size)) { RARCH_WARN("Couldn't initialize font renderer.\n"); free(font); return NULL; } atlas = font->font_driver->get_atlas(font->font_data); font->texture = vita2d_create_empty_texture_format(atlas->width,atlas->height,SCE_GXM_TEXTURE_FORMAT_U8_R111); if (!font->texture) { free(font); return NULL; } vita2d_texture_set_filters(font->texture, SCE_GXM_TEXTURE_FILTER_POINT, SCE_GXM_TEXTURE_FILTER_LINEAR); stride = vita2d_texture_get_stride(font->texture); uint8_t *tex32 = vita2d_texture_get_datap(font->texture); const uint8_t *frame32 = atlas->buffer; pitch = atlas->width; for (j = 0; j < atlas->height; j++) for (k = 0; k < atlas->width; k++) tex32[k + j*stride] = frame32[k + j*pitch]; return font; }
static bool vita2d_overlay_load(void *data, const void *image_data, unsigned num_images) { unsigned i,j,k; unsigned int stride, pitch; vita_video_t *vita = (vita_video_t*)data; const struct texture_image *images = (const struct texture_image*)image_data; vita2d_free_overlay(vita); vita->overlay = (struct vita_overlay_data*)calloc(num_images, sizeof(*vita->overlay)); if (!vita->overlay) return false; vita->overlays = num_images; for (i = 0; i < num_images; i++) { struct vita_overlay_data *o = (struct vita_overlay_data*)&vita->overlay[i]; o->width = images[i].width; o->height = images[i].height; o->tex = vita2d_create_empty_texture_format(o->width , o->height, SCE_GXM_TEXTURE_FORMAT_U8U8U8U8_ARGB); vita2d_texture_set_filters(o->tex,SCE_GXM_TEXTURE_FILTER_LINEAR,SCE_GXM_TEXTURE_FILTER_LINEAR); stride = vita2d_texture_get_stride(o->tex); stride /= 4; uint32_t *tex32 = vita2d_texture_get_datap(o->tex); const uint32_t *frame32 = images[i].pixels; pitch = o->width; for (j = 0; j < o->height; j++) for (k = 0; k < o->width; k++) tex32[k + j*stride] = frame32[k + j*pitch]; vita2d_overlay_tex_geom(vita, i, 0, 0, 1, 1); /* Default. Stretch to whole screen. */ vita2d_overlay_vertex_geom(vita, i, 0, 0, 1, 1); vita->overlay[i].alpha_mod = 1.0f; } return true; }
static bool vita2d_gfx_frame(void *data, const void *frame, unsigned width, unsigned height, uint64_t frame_count, unsigned pitch, const char *msg, video_frame_info_t *video_info) { void *tex_p; vita_video_t *vita = (vita_video_t *)data; if (frame) { if(!(vita->texture&&vita2d_texture_get_datap(vita->texture)==frame)) { unsigned i; unsigned int stride; if ((width != vita->width || height != vita->height) && vita->texture) { vita2d_free_texture(vita->texture); vita->texture = NULL; } if (!vita->texture) { RARCH_LOG("Creating texture: %ix%i\n", width, height); vita->width = width; vita->height = height; vita->texture = vita2d_create_empty_texture_format(width, height, vita->format); vita2d_texture_set_filters(vita->texture,vita->tex_filter,vita->tex_filter); } tex_p = vita2d_texture_get_datap(vita->texture); stride = vita2d_texture_get_stride(vita->texture); if (vita->format == SCE_GXM_TEXTURE_FORMAT_X8U8U8U8_1RGB) { stride /= 4; pitch /= 4; uint32_t *tex32 = tex_p; const uint32_t *frame32 = frame; for (i = 0; i < height; i++) memcpy_neon(&tex32[i*stride],&frame32[i*pitch],pitch*sizeof(uint32_t)); } else { stride /= 2; pitch /= 2; uint16_t *tex16 = tex_p; const uint16_t *frame16 = frame; for (i = 0; i < height; i++) memcpy_neon(&tex16[i*stride],&frame16[i*pitch],width*sizeof(uint16_t)); } } } if (vita->should_resize) vita2d_gfx_update_viewport(vita); vita2d_start_drawing(); vita2d_draw_rectangle(0,0,PSP_FB_WIDTH,PSP_FB_HEIGHT,vita2d_get_clear_color()); if (vita->texture) { if (vita->fullscreen) vita2d_draw_texture_scale(vita->texture, 0, 0, PSP_FB_WIDTH / (float)vita->width, PSP_FB_HEIGHT / (float)vita->height); else { const float radian = 90 * 0.0174532925f; const float rad = vita->rotation * radian; float scalex = vita->vp.width / (float)vita->width; float scaley = vita->vp.height / (float)vita->height; vita2d_draw_texture_scale_rotate(vita->texture,vita->vp.x, vita->vp.y, scalex, scaley, rad); } } #ifdef HAVE_OVERLAY if (vita->overlay_enable) vita2d_render_overlay(vita); #endif if (vita->menu.active) { #ifdef HAVE_MENU menu_driver_frame(video_info); #endif if(vita->menu.texture) { if (vita->fullscreen) vita2d_draw_texture_scale(vita->menu.texture, 0, 0, PSP_FB_WIDTH / (float)vita->menu.width, PSP_FB_HEIGHT / (float)vita->menu.height); else { if (vita->menu.width > vita->menu.height) { float scale = PSP_FB_HEIGHT / (float)vita->menu.height; float w = vita->menu.width * scale; vita2d_draw_texture_scale(vita->menu.texture, PSP_FB_WIDTH / 2.0f - w/2.0f, 0.0f, scale, scale); } else { float scale = PSP_FB_WIDTH / (float)vita->menu.width; float h = vita->menu.height * scale; vita2d_draw_texture_scale(vita->menu.texture, 0.0f, PSP_FB_HEIGHT / 2.0f - h/2.0f, scale, scale); } } } } if(!string_is_empty(msg)) font_driver_render_msg(video_info, NULL, msg, NULL); vita2d_end_drawing(); vita2d_swap_buffers(); return true; }
static int renderThread(unsigned int args, void* arg){ for (;;){ sceKernelWaitSema(GPU_Mutex, 1, NULL); memcpy(vita2d_texture_get_datap(gpu_texture), vita2d_texture_get_datap(next_texture), vita2d_texture_get_stride(gpu_texture)*240); sceKernelSignalSema(GPU_Mutex, 1); sceKernelWaitSema(GPU_Cleanup_Mutex, 1, NULL); if (main_texture == NULL) sceKernelExitDeleteThread(0); // Exit procedure vita2d_start_drawing(); if (set_shader){ Output::Post("Shader set to %s.",shader_names[in_use_shader]); set_shader = false; vita2d_texture_set_program(shaders[in_use_shader]->vertexProgram, shaders[in_use_shader]->fragmentProgram); vita2d_texture_set_wvp(shaders[in_use_shader]->wvpParam); vita2d_texture_set_vertexInput(&shaders[in_use_shader]->vertexInput); vita2d_texture_set_fragmentInput(&shaders[in_use_shader]->fragmentInput); } vita2d_clear_screen(); switch (zoom_state){ case 0: // 640x480 vita2d_draw_texture_scale(gpu_texture, 160, 32, 2.0, 2.0); break; case 1: // 725x544 vita2d_draw_texture_scale(gpu_texture, 117, 0, 2.266, 2.266); break; case 2: // 960x544 vita2d_draw_texture_scale(gpu_texture, 0, 0, 3, 2.266); break; } vita2d_end_drawing(); vita2d_wait_rendering_done(); vita2d_swap_buffers(); sceKernelSignalSema(GPU_Cleanup_Mutex, 1); } }
void Psp2Ui::UpdateDisplay() { sceKernelWaitSema(GPU_Mutex, 1, NULL); memcpy(vita2d_texture_get_datap(next_texture), vita2d_texture_get_datap(main_texture), vita2d_texture_get_stride(main_texture)*240); sceKernelSignalSema(GPU_Mutex, 1); }
static vita2d_texture *_vita2d_load_PNG_generic(const void *io_ptr, png_rw_ptr read_data_fn) { png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); if (png_ptr == NULL) { goto error_create_read; } png_infop info_ptr = png_create_info_struct(png_ptr); if (info_ptr == NULL) { goto error_create_info; } png_bytep *row_ptrs = NULL; if (setjmp(png_jmpbuf(png_ptr))) { png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)0); if (row_ptrs != NULL) free(row_ptrs); return NULL; } png_set_read_fn(png_ptr, (png_voidp)io_ptr, read_data_fn); png_set_sig_bytes(png_ptr, PNG_SIGSIZE); png_read_info(png_ptr, info_ptr); unsigned int width, height; int bit_depth, color_type; png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, NULL, NULL, NULL); if ((color_type == PNG_COLOR_TYPE_PALETTE && bit_depth <= 8) || (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8) || png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS) || (bit_depth == 16)) { png_set_expand(png_ptr); } if (bit_depth == 16) png_set_scale_16(png_ptr); if (bit_depth == 8 && color_type == PNG_COLOR_TYPE_RGB) png_set_filler(png_ptr, 0xFF, PNG_FILLER_AFTER); if (color_type == PNG_COLOR_TYPE_GRAY || color_type == PNG_COLOR_TYPE_GRAY_ALPHA) png_set_gray_to_rgb(png_ptr); if (color_type == PNG_COLOR_TYPE_PALETTE) { png_set_palette_to_rgb(png_ptr); png_set_filler(png_ptr, 0xFF, PNG_FILLER_AFTER); } if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8) png_set_expand_gray_1_2_4_to_8(png_ptr); if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) png_set_tRNS_to_alpha(png_ptr); if (bit_depth < 8) png_set_packing(png_ptr); png_read_update_info(png_ptr, info_ptr); row_ptrs = (png_bytep *)malloc(sizeof(png_bytep) * height); if (!row_ptrs) goto error_alloc_rows; vita2d_texture *texture = vita2d_create_empty_texture(width, height); if (!texture) goto error_create_tex; void *texture_data = vita2d_texture_get_datap(texture); unsigned int stride = vita2d_texture_get_stride(texture); int i; for (i = 0; i < height; i++) { row_ptrs[i] = (png_bytep)(texture_data + i*stride); } png_read_image(png_ptr, row_ptrs); png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)0); free(row_ptrs); return texture; error_create_tex: free(row_ptrs); error_alloc_rows: png_destroy_info_struct(png_ptr, &info_ptr); error_create_info: png_destroy_read_struct(&png_ptr, (png_infopp)0, (png_infopp)0); error_create_read: return NULL; }
static bool vita2d_gfx_frame(void *data, const void *frame, unsigned width, unsigned height, uint64_t frame_count, unsigned pitch, const char *msg) { int i, j; void *tex_p; unsigned int stride; vita_video_t *vita = (vita_video_t *)data; (void)frame; (void)width; (void)height; (void)pitch; (void)msg; if (frame) { if ((width != vita->width || height != vita->height) && vita->texture) { vita2d_free_texture(vita->texture); vita->texture = NULL; } if (!vita->texture) { RARCH_LOG("Creating texture: %ix%i\n", width, height); vita->width = width; vita->height = height; vita->texture = vita2d_create_empty_texture_format(width, height, vita->format); vita2d_texture_set_filters(vita->texture,vita->tex_filter,vita->tex_filter); } tex_p = vita2d_texture_get_datap(vita->texture); stride = vita2d_texture_get_stride(vita->texture); if (vita->format == SCE_GXM_TEXTURE_FORMAT_X8U8U8U8_1RGB) { stride /= 4; pitch /= 4; uint32_t *tex32 = tex_p; const uint32_t *frame32 = frame; for (i = 0; i < height; i++) for (j = 0; j < width; j++) tex32[j + i*stride] = frame32[j + i*pitch]; } else { stride /= 2; pitch /= 2; uint16_t *tex16 = tex_p; const uint16_t *frame16 = frame; for (i = 0; i < height; i++) for (j = 0; j < width; j++) tex16[j + i*stride] = frame16[j + i*pitch]; } } if (vita->should_resize) vita2d_gfx_update_viewport(vita); vita2d_start_drawing(); vita2d_clear_screen(); if (vita->texture) { if (vita->fullscreen) vita2d_draw_texture_scale(vita->texture, 0, 0, PSP_FB_WIDTH / (float)vita->width, PSP_FB_HEIGHT / (float)vita->height); else { const float radian = 90 * 0.0174532925f; const float rad = vita->rotation * radian; float scalex = vita->vp.width / (float)vita->width; float scaley = vita->vp.height / (float)vita->height; vita2d_draw_texture_scale_rotate(vita->texture, vita->vp.x, vita->vp.y, scalex, scaley, rad); } } if (vita->menu.active && vita->menu.texture) { if (vita->fullscreen) vita2d_draw_texture_scale(vita->menu.texture, 0, 0, PSP_FB_WIDTH / (float)vita->menu.width, PSP_FB_HEIGHT / (float)vita->menu.height); else { if (vita->menu.width > vita->menu.height) { float scale = PSP_FB_HEIGHT / (float)vita->menu.height; float w = vita->menu.width * scale; vita2d_draw_texture_scale(vita->menu.texture, PSP_FB_WIDTH / 2.0f - w/2.0f, 0.0f, scale, scale); } else { float scale = PSP_FB_WIDTH / (float)vita->menu.width; float h = vita->menu.height * scale; vita2d_draw_texture_scale(vita->menu.texture, 0.0f, PSP_FB_HEIGHT / 2.0f - h/2.0f, scale, scale); } } } vita2d_end_drawing(); vita2d_swap_buffers(); return true; }