static bool xdk_d3d_frame(void *data, const void *frame, unsigned width, unsigned height, unsigned pitch, const char *msg) { #ifndef _XBOX1 DWORD fetchConstant; UINT64 pendingMask3; #endif xdk_d3d_video_t *d3d = (xdk_d3d_video_t*)data; LPDIRECT3DDEVICE d3dr = (LPDIRECT3DDEVICE)d3d->d3d_render_device; uint64_t lifecycle_mode_state = g_extern.lifecycle_mode_state; #if 0 /* ifdef HAVE_FBO */ D3DSurface* pRenderTarget0; #endif if (d3d->last_width != width || d3d->last_height != height) { D3DLOCKED_RECT d3dlr; D3DTexture_LockRect(d3d->lpTexture, 0, &d3dlr, NULL, D3DLOCK_NOSYSLOCK); memset(d3dlr.pBits, 0, d3d->tex_w * d3dlr.Pitch); #if defined(_XBOX1) float tex_w = width; float tex_h = height; DrawVerticeFormats verts[] = { { -1.0f, -1.0f, 1.0f, 0.0f, tex_h }, { 1.0f, -1.0f, 1.0f, tex_w, tex_h }, { -1.0f, 1.0f, 1.0f, 0.0f, 0.0f }, { 1.0f, 1.0f, 1.0f, tex_w, 0.0f }, }; #elif defined(_XBOX360) float tex_w = width / ((float)d3d->tex_w); float tex_h = height / ((float)d3d->tex_h); DrawVerticeFormats verts[] = { { -1.0f, -1.0f, 0.0f, tex_h }, { 1.0f, -1.0f, tex_w, tex_h }, { -1.0f, 1.0f, 0.0f, 0.0f }, { 1.0f, 1.0f, tex_w, 0.0f }, }; #endif // Align texels and vertices (D3D9 quirk). for (unsigned i = 0; i < 4; i++) { verts[i].x -= 0.5f / ((float)d3d->tex_w); verts[i].y += 0.5f / ((float)d3d->tex_h); } #if defined(_XBOX1) BYTE *verts_ptr; #elif defined(_XBOX360) void *verts_ptr; #endif RD3DVertexBuffer_Lock(d3d->vertex_buf, 0, 0, &verts_ptr, 0); memcpy(verts_ptr, verts, sizeof(verts)); RD3DVertexBuffer_Unlock(d3d->vertex_buf); d3d->last_width = width; d3d->last_height = height; } #if 0 /* ifdef HAVE_FBO */ if (d3d->fbo_inited) { d3dr->GetRenderTarget(0, &pRenderTarget0); d3dr->SetRenderTarget(0, d3d->lpSurface); } #endif if (d3d->should_resize) { #ifdef _XBOX1 d3dr->SetFlickerFilter(g_extern.console.screen.flicker_filter_index); d3dr->SetSoftDisplayFilter(g_extern.lifecycle_mode_state & (1ULL << MODE_VIDEO_SOFT_FILTER_ENABLE)); #endif xdk_d3d_set_viewport(false); d3d->should_resize = false; } #ifdef HAVE_HLSL if (d3d->shader) d3d->shader->set_mvp(NULL); #endif RD3DDevice_SetTexture(d3dr, 0, d3d->lpTexture); #ifdef HAVE_HLSL if (d3d->shader) d3d->shader->use(1); #endif #if 0 /* ifdef HAVE_FBO */ if (d3d->fbo_inited) { #ifdef HAVE_HLSL if (d3d->shader) d3d->shader->set_params(width, height, d3d->tex_w, d3d->tex_h, g_settings.video.fbo.scale_x * width, g_settings.video.fbo.scale_y * height, g_extern.frame_count); #endif D3DVIEWPORT vp = {0}; vp.Width = g_settings.video.fbo.scale_x * width; vp.Height = g_settings.video.fbo.scale_y * height; vp.X = 0; vp.Y = 0; vp.MinZ = 0.0f; vp.MaxZ = 1.0f; RD3DDevice_SetViewport(d3dr, &vp); } else #endif { #ifdef HAVE_HLSL if (d3d->shader) d3d->shader->set_params(width, height, d3d->tex_w, d3d->tex_h, d3d->win_width, d3d->win_height, g_extern.frame_count, /* TODO - missing a bunch of params at the end */ NULL, NULL, NULL, 0); #endif } if (frame) { unsigned base_size = d3d->base_size; D3DLOCKED_RECT d3dlr; D3DTexture_LockRect(d3d->lpTexture, 0, &d3dlr, NULL, D3DLOCK_NOSYSLOCK); for (unsigned y = 0; y < height; y++) { const uint8_t *in = (const uint8_t*)frame + y * pitch; uint8_t *out = (uint8_t*)d3dlr.pBits + y * d3dlr.Pitch; memcpy(out, in, width * base_size); } } unsigned filter = g_settings.video.smooth ? D3DTEXF_LINEAR : D3DTEXF_POINT; RD3DDevice_SetSamplerState_MinFilter(d3dr, 0, filter); RD3DDevice_SetSamplerState_MagFilter(d3dr, 0, filter); RD3DDevice_SetSamplerState_AddressU(d3dr, D3DSAMP_ADDRESSU, D3DTADDRESS_BORDER); RD3DDevice_SetSamplerState_AddressV(d3dr, D3DSAMP_ADDRESSV, D3DTADDRESS_BORDER); #if defined(_XBOX1) RD3DDevice_SetVertexShader(d3dr, D3DFVF_XYZ | D3DFVF_TEX1); IDirect3DDevice8_SetStreamSource(d3dr, 0, d3d->vertex_buf, sizeof(DrawVerticeFormats)); #elif defined(_XBOX360) D3DDevice_SetVertexDeclaration(d3dr, d3d->v_decl); D3DDevice_SetStreamSource_Inline(d3dr, 0, d3d->vertex_buf, 0, sizeof(DrawVerticeFormats)); #endif RD3DDevice_DrawPrimitive(d3dr, D3DPT_TRIANGLESTRIP, 0, 2); #if 0 /* ifdef HAVE_FBO */ if (d3d->fbo_inited) { d3dr->Resolve(D3DRESOLVE_RENDERTARGET0, NULL, d3d->lpTexture_ot, NULL, 0, 0, NULL, 0, 0, NULL); d3dr->SetRenderTarget(0, pRenderTarget0); pRenderTarget0->Release(); RD3DDevice_SetTexture(d3dr, 0, &d3d->lpTexture_ot_as16srgb); #ifdef HAVE_HLSL hlsl_use(2); if (d3d->shader) d3d->shader->set_params(g_settings.video.fbo.scale_x * width, g_settings.video.fbo.scale_y * height, g_settings.video.fbo.scale_x * d3d->tex_w, g_settings.video.fbo.scale_y * d3d->tex_h, d3d->win_width, d3d->win_height, g_extern.frame_count); #endif xdk_d3d_set_viewport(false); RD3DDevice_SetSamplerState_MinFilter(d3dr, D3DSAMP_MINFILTER, g_settings.video.second_pass_smooth ? D3DTEXF_LINEAR : D3DTEXF_POINT); RD3DDevice_SetSamplerState_MagFilter(d3dr, D3DSAMP_MAGFILTER, g_settings.video.second_pass_smooth ? D3DTEXF_LINEAR : D3DTEXF_POINT); RD3DDevice_SetSamplerState_AddressU(d3dr, D3DSAMP_ADDRESSU, D3DTADDRESS_BORDER); RD3DDevice_SetSamplerState_AddressV(d3dr, D3DSAMP_ADDRESSV, D3DTADDRESS_BORDER); D3DDevice_SetVertexDeclaration(d3dr, d3d->v_decl); Direct3DDevice_SetStreamSource_Inline(d3dr, 0, d3d->vertex_buf, 0, sizeof(DrawVerticeFormats)); RD3DDevice_DrawPrimitive(d3dr, D3DPT_TRIANGLESTRIP, 0, 2); } #endif #if defined(HAVE_RGUI) || defined(HAVE_RMENU) || defined(HAVE_RMENU_XUI) #if defined(HAVE_RMENU_XUI) || defined(HAVE_RGUI) if (d3d->rgui_texture_enable) #endif xdk_d3d_draw_texture(d3d); #endif #if defined(_XBOX1) float msg_width = 60; float msg_height = 365; #elif defined(_XBOX360) float msg_width = (lifecycle_mode_state & (1ULL << MODE_MENU_HD)) ? 160 : 100; float msg_height = 120; #endif #if 0 if (msg) { font_parms.x = msg_width; font_parms.y = msg_height; d3d->font_ctx->render_msg(d3d, msg, &font_parms); } #endif d3d->ctx_driver->update_window_title(); gfx_ctx_xdk_swap_buffers(); g_extern.frame_count++; return true; }
static void set_vertices(void *data, unsigned pass, unsigned width, unsigned height) { d3d_video_t *d3d = (d3d_video_t*)data; if (d3d->last_width != width || d3d->last_height != height) { d3d->last_width = width; d3d->last_height = height; DrawVerticeFormats vert[4]; float tex_w = width; float tex_h = height; #ifdef _XBOX360 tex_w /= ((float)d3d->tex_w); tex_h /= ((float)d3d->tex_h); #endif vert[0].x = -1.0f; vert[1].x = 1.0f; vert[2].x = -1.0f; vert[3].x = 1.0f; vert[0].y = -1.0f; vert[1].y = -1.0f; vert[2].y = 1.0f; vert[3].y = 1.0f; #if defined(_XBOX1) vert[0].z = 1.0f; vert[1].z = 1.0f; vert[2].z = 1.0f; vert[3].z = 1.0f; vert[0].rhw = 0.0f; vert[1].rhw = tex_w; vert[2].rhw = 0.0f; vert[3].rhw = tex_w; vert[0].u = tex_h; vert[1].u = tex_h; vert[2].u = 0.0f; vert[3].u = 0.0f; vert[0].v = 0.0f; vert[1].v = 0.0f; vert[2].v = 0.0f; vert[3].v = 0.0f; #elif defined(_XBOX360) vert[0].u = 0.0f; vert[1].u = tex_w; vert[2].u = 0.0f; vert[3].u = tex_w; vert[0].v = tex_h; vert[1].v = tex_h; vert[2].v = 0.0f; vert[3].v = 0.0f; #endif // Align texels and vertices. for (unsigned i = 0; i < 4; i++) { vert[i].x -= 0.5f / ((float)d3d->tex_w); vert[i].y += 0.5f / ((float)d3d->tex_h); } #if defined(_XBOX1) BYTE *verts; #elif defined(_XBOX360) void *verts; #endif RD3DVertexBuffer_Lock(d3d->vertex_buf, 0, 0, &verts, 0); memcpy(verts, vert, sizeof(vert)); RD3DVertexBuffer_Unlock(d3d->vertex_buf); } if (d3d->shader) { set_mvp(d3d, d3d->screen_width, d3d->screen_height, d3d->dev_rotation); if (d3d->shader->use) d3d->shader->use(d3d, pass); if (d3d->shader->set_params) d3d->shader->set_params(d3d, width, height, d3d->tex_w, d3d->tex_h, d3d->screen_width, d3d->screen_height, g_extern.frame_count, NULL, NULL, NULL, 0); } }
static void *xdk_d3d_init(const video_info_t *video, const input_driver_t **input, void **input_data) { HRESULT ret; if (driver.video_data) { xdk_d3d_video_t *d3d = (xdk_d3d_video_t*)driver.video_data; // Reinitialize textures as we might have changed pixel formats. xdk_d3d_reinit_textures(d3d, video); return driver.video_data; } //we'll just use driver.video_data throughout here because it needs to //exist when we delegate initing to the context file driver.video_data = (xdk_d3d_video_t*)calloc(1, sizeof(xdk_d3d_video_t)); if (!driver.video_data) return NULL; xdk_d3d_video_t *d3d = (xdk_d3d_video_t*)driver.video_data; d3d->vsync = video->vsync; d3d->tex_w = RARCH_SCALE_BASE * video->input_scale; d3d->tex_h = RARCH_SCALE_BASE * video->input_scale; #if defined(_XBOX1) d3d->ctx_driver = gfx_ctx_init_first(GFX_CTX_DIRECT3D8_API, 8, 0); #elif defined(_XBOX360) d3d->ctx_driver = gfx_ctx_init_first(GFX_CTX_DIRECT3D9_API, 9, 0); #endif if (d3d->ctx_driver) { D3DPRESENT_PARAMETERS d3dpp; xdk_d3d_generate_pp(&d3dpp, video); ret = d3d->d3d_device->CreateDevice(0, D3DDEVTYPE_HAL, NULL, D3DCREATE_HARDWARE_VERTEXPROCESSING, &d3dpp, &d3d->d3d_render_device); if (ret != S_OK) RARCH_ERR("Failed at CreateDevice.\n"); RD3DDevice_Clear(d3d->d3d_render_device, 0, NULL, D3DCLEAR_TARGET, 0xff000000, 1.0f, 0); } else { free(d3d); return NULL; } RARCH_LOG("Found D3D context: %s\n", d3d->ctx_driver->ident); xdk_d3d_init_textures(d3d, video); #if defined(_XBOX1) // use an orthogonal matrix for the projection matrix D3DXMATRIX mat; D3DXMatrixOrthoOffCenterLH(&mat, 0, d3d->win_width , d3d->win_height , 0, 0.0f, 1.0f); d3d->d3d_render_device->SetTransform(D3DTS_PROJECTION, &mat); // use an identity matrix for the world and view matrices D3DXMatrixIdentity(&mat); d3d->d3d_render_device->SetTransform(D3DTS_WORLD, &mat); d3d->d3d_render_device->SetTransform(D3DTS_VIEW, &mat); ret = d3d->d3d_render_device->CreateVertexBuffer(4 * sizeof(DrawVerticeFormats), D3DUSAGE_WRITEONLY, D3DFVF_CUSTOMVERTEX, D3DPOOL_MANAGED, &d3d->vertex_buf); if (ret != S_OK) { RARCH_ERR("[xdk_d3d_init::] Failed at CreateVertexBuffer.\n"); return NULL; } const DrawVerticeFormats init_verts[] = { { -1.0f, -1.0f, 1.0f, 0.0f, 1.0f }, { 1.0f, -1.0f, 1.0f, 1.0f, 1.0f }, { -1.0f, 1.0f, 1.0f, 0.0f, 0.0f }, { 1.0f, 1.0f, 1.0f, 1.0f, 0.0f }, }; BYTE *verts_ptr; RD3DVertexBuffer_Lock(d3d->vertex_buf, 0, 0, &verts_ptr, 0); memcpy(verts_ptr, init_verts, sizeof(init_verts)); RD3DVertexBuffer_Unlock(d3d->vertex_buf); RD3DDevice_SetVertexShader(d3d->d3d_render_device, D3DFVF_XYZ | D3DFVF_TEX1); #elif defined(_XBOX360) ret = d3d->d3d_render_device->CreateVertexBuffer(4 * sizeof(DrawVerticeFormats), 0, 0, 0, &d3d->vertex_buf, NULL); if (ret != S_OK) { RARCH_ERR("[xdk_d3d_init::] Failed at CreateVertexBuffer.\n"); return NULL; } static const DrawVerticeFormats init_verts[] = { { -1.0f, -1.0f, 0.0f, 1.0f }, { 1.0f, -1.0f, 1.0f, 1.0f }, { -1.0f, 1.0f, 0.0f, 0.0f }, { 1.0f, 1.0f, 1.0f, 0.0f }, }; void *verts_ptr; RD3DVertexBuffer_Lock(d3d->vertex_buf, 0, 0, &verts_ptr, 0); memcpy(verts_ptr, init_verts, sizeof(init_verts)); RD3DVertexBuffer_Unlock(d3d->vertex_buf); static const D3DVERTEXELEMENT VertexElements[] = { { 0, 0 * sizeof(float), D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 }, { 0, 2 * sizeof(float), D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0 }, D3DDECL_END() }; ret = d3d->d3d_render_device->CreateVertexDeclaration(VertexElements, &d3d->v_decl); if (ret != S_OK) { RARCH_ERR("[xdk_d3d_init::] Failed at CreateVertexDeclaration.\n"); } #endif d3d->ctx_driver->get_video_size(&d3d->win_width, &d3d->win_height); RARCH_LOG("Detecting screen resolution: %ux%u.\n", d3d->win_width, d3d->win_height); d3d->ctx_driver->swap_interval(d3d->vsync ? 1 : 0); #ifdef HAVE_HLSL if (!hlsl_shader_init()) { RARCH_ERR("Shader init failed.\n"); d3d->ctx_driver->destroy(); free(d3d); return NULL; } RARCH_LOG("D3D: Loaded %u program(s).\n", d3d->shader->num_shaders()); #endif #if 0 /* ifdef HAVE_FBO */ xdk_d3d_init_fbo(d3d); #endif xdk_d3d_set_rotation(d3d, g_settings.video.rotation); //really returns driver.video_data to driver.video_data - see comment above return d3d; }