2012-01-05 11:37:27 +00:00
|
|
|
/* SSNES - A Super Nintendo Entertainment System (SNES) Emulator frontend for libsnes.
|
2012-01-22 00:43:54 +00:00
|
|
|
* Copyright (C) 2010-2012 - Hans-Kristian Arntzen
|
|
|
|
* Copyright (C) 2011-2012 - Daniel De Matteis
|
2012-01-05 11:37:27 +00:00
|
|
|
*
|
|
|
|
* Some code herein may be based on code found in BSNES.
|
|
|
|
*
|
|
|
|
* SSNES is free software: you can redistribute it and/or modify it under the terms
|
|
|
|
* of the GNU General Public License as published by the Free Software Found-
|
|
|
|
* ation, either version 3 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* SSNES is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
|
|
|
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE. See the GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with SSNES.
|
|
|
|
* If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <xtl.h>
|
|
|
|
|
2012-01-05 12:30:13 +00:00
|
|
|
#include "../driver.h"
|
2012-01-21 22:59:27 +00:00
|
|
|
|
|
|
|
#include "xdk360_video.h"
|
2012-01-05 12:30:13 +00:00
|
|
|
#include "../general.h"
|
2012-01-05 11:37:27 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2012-01-07 13:54:55 +00:00
|
|
|
static const char* g_strPixelShaderProgram =
|
2012-01-07 14:00:51 +00:00
|
|
|
" sampler2D tex : register(s0); "
|
|
|
|
" struct PS_IN "
|
|
|
|
" { "
|
|
|
|
" float2 coord : TEXCOORD0; "
|
|
|
|
" }; "
|
|
|
|
" "
|
|
|
|
" float4 main(PS_IN input) : COLOR "
|
|
|
|
" { "
|
|
|
|
" return tex2D(tex, input.coord); "
|
|
|
|
" } ";
|
2012-01-07 13:54:55 +00:00
|
|
|
|
|
|
|
static const char* g_strVertexShaderProgram =
|
2012-01-07 14:03:09 +00:00
|
|
|
" struct VS_IN "
|
|
|
|
" "
|
|
|
|
" { "
|
|
|
|
" float2 pos : POSITION; "
|
|
|
|
" float2 coord : TEXCOORD0; "
|
|
|
|
" }; "
|
|
|
|
" "
|
|
|
|
" struct VS_OUT "
|
|
|
|
" { "
|
|
|
|
" float4 pos : POSITION; "
|
|
|
|
" float2 coord : TEXCOORD0; "
|
|
|
|
" }; "
|
|
|
|
" "
|
|
|
|
" VS_OUT main(VS_IN input) "
|
|
|
|
" { "
|
|
|
|
" VS_OUT output; "
|
|
|
|
" output.pos = float4(input.pos, 0.0, 1.0); "
|
|
|
|
" output.coord = input.coord; "
|
|
|
|
" return output; "
|
|
|
|
" } ";
|
2012-01-05 11:37:27 +00:00
|
|
|
|
|
|
|
typedef struct DrawVerticeFormats
|
|
|
|
{
|
2012-01-07 13:54:55 +00:00
|
|
|
float x, y;
|
|
|
|
float u, v;
|
2012-01-05 11:37:27 +00:00
|
|
|
} DrawVerticeFormats;
|
|
|
|
|
|
|
|
static bool g_quitting;
|
2012-01-21 22:59:27 +00:00
|
|
|
unsigned g_frame_count;
|
|
|
|
void *g_d3d;
|
2012-01-05 11:37:27 +00:00
|
|
|
|
2012-01-21 22:59:27 +00:00
|
|
|
static void xdk360_gfx_free(void * data)
|
2012-01-05 11:37:27 +00:00
|
|
|
{
|
2012-01-21 22:59:27 +00:00
|
|
|
if (g_d3d)
|
|
|
|
return;
|
|
|
|
|
2012-01-07 13:54:55 +00:00
|
|
|
xdk360_video_t *vid = (xdk360_video_t*)data;
|
2012-02-02 18:01:52 +00:00
|
|
|
|
2012-01-05 11:37:27 +00:00
|
|
|
if (!vid)
|
|
|
|
return;
|
|
|
|
|
2012-01-07 13:54:55 +00:00
|
|
|
vid->lpTexture->Release();
|
|
|
|
vid->vertex_buf->Release();
|
|
|
|
vid->pVertexDecl->Release();
|
|
|
|
vid->pPixelShader->Release();
|
|
|
|
vid->pVertexShader->Release();
|
|
|
|
vid->xdk360_render_device->Release();
|
|
|
|
vid->xdk360_device->Release();
|
|
|
|
|
2012-01-05 11:37:27 +00:00
|
|
|
free(vid);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *xdk360_gfx_init(const video_info_t *video, const input_driver_t **input, void **input_data)
|
|
|
|
{
|
2012-02-02 18:01:52 +00:00
|
|
|
if (g_d3d)
|
|
|
|
return g_d3d;
|
2012-01-21 22:59:27 +00:00
|
|
|
|
2012-01-07 14:30:52 +00:00
|
|
|
xdk360_video_t *vid = (xdk360_video_t*)calloc(1, sizeof(xdk360_video_t));
|
|
|
|
if (!vid)
|
2012-01-05 11:37:27 +00:00
|
|
|
return NULL;
|
|
|
|
|
2012-01-07 14:30:52 +00:00
|
|
|
vid->xdk360_device = Direct3DCreate9(D3D_SDK_VERSION);
|
|
|
|
if (!vid->xdk360_device)
|
2012-01-07 13:54:55 +00:00
|
|
|
{
|
2012-01-07 14:30:52 +00:00
|
|
|
free(vid);
|
2012-01-07 13:54:55 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-02-10 16:31:20 +00:00
|
|
|
// Get video settings
|
|
|
|
XVIDEO_MODE video_mode;
|
|
|
|
|
|
|
|
memset(&video_mode, 0, sizeof(video_mode));
|
2012-01-22 00:43:54 +00:00
|
|
|
|
2012-02-10 16:31:20 +00:00
|
|
|
XGetVideoMode(&video_mode);
|
|
|
|
|
|
|
|
memset(&vid->d3dpp, 0, sizeof(vid->d3dpp));
|
|
|
|
|
|
|
|
vid->d3dpp.BackBufferWidth = video_mode.fIsHiDef ? 1280 : 640;
|
|
|
|
vid->d3dpp.BackBufferHeight = video_mode.fIsHiDef ? 720 : 480;
|
2012-01-22 00:43:54 +00:00
|
|
|
vid->d3dpp.BackBufferFormat = (D3DFORMAT)MAKESRGBFMT(D3DFMT_A8R8G8B8);
|
|
|
|
vid->d3dpp.FrontBufferFormat = (D3DFORMAT)MAKESRGBFMT(D3DFMT_LE_X8R8G8B8);
|
|
|
|
vid->d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE;
|
|
|
|
vid->d3dpp.MultiSampleQuality = 0;
|
|
|
|
vid->d3dpp.BackBufferCount = 2;
|
|
|
|
vid->d3dpp.EnableAutoDepthStencil = TRUE;
|
|
|
|
vid->d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8;
|
|
|
|
vid->d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
|
|
|
|
vid->d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_ONE;
|
|
|
|
|
|
|
|
vid->xdk360_device->CreateDevice(0, D3DDEVTYPE_HAL, NULL, D3DCREATE_HARDWARE_VERTEXPROCESSING, &vid->d3dpp,
|
2012-01-07 14:30:52 +00:00
|
|
|
&vid->xdk360_render_device);
|
2012-01-05 11:37:27 +00:00
|
|
|
|
2012-01-07 13:54:55 +00:00
|
|
|
ID3DXBuffer* pShaderCodeV = NULL;
|
|
|
|
ID3DXBuffer* pShaderCodeP = NULL;
|
2012-01-05 11:37:27 +00:00
|
|
|
ID3DXBuffer* pErrorMsg = NULL;
|
|
|
|
|
2012-01-07 13:54:55 +00:00
|
|
|
HRESULT hr = D3DXCompileShader(g_strVertexShaderProgram, (UINT)strlen(g_strVertexShaderProgram),
|
|
|
|
NULL, NULL, "main", "vs_2_0", 0, &pShaderCodeV, &pErrorMsg, NULL);
|
2012-01-07 13:13:23 +00:00
|
|
|
|
2012-01-07 13:54:55 +00:00
|
|
|
if (SUCCEEDED(hr))
|
2012-01-07 13:13:23 +00:00
|
|
|
{
|
2012-01-07 13:54:55 +00:00
|
|
|
hr = D3DXCompileShader(g_strPixelShaderProgram, (UINT)strlen(g_strPixelShaderProgram),
|
|
|
|
NULL, NULL, "main", "ps_2_0", 0, &pShaderCodeP, &pErrorMsg, NULL);
|
2012-01-07 13:13:23 +00:00
|
|
|
}
|
|
|
|
|
2012-01-05 11:37:27 +00:00
|
|
|
if (FAILED(hr))
|
|
|
|
{
|
2012-01-07 13:54:55 +00:00
|
|
|
OutputDebugString(pErrorMsg ? (char*)pErrorMsg->GetBufferPointer() : "");
|
2012-01-07 14:30:52 +00:00
|
|
|
vid->xdk360_render_device->Release();
|
|
|
|
vid->xdk360_device->Release();
|
|
|
|
free(vid);
|
2012-01-07 13:54:55 +00:00
|
|
|
return NULL;
|
2012-01-05 11:37:27 +00:00
|
|
|
}
|
|
|
|
|
2012-01-07 14:30:52 +00:00
|
|
|
vid->xdk360_render_device->CreateVertexShader((const DWORD*)pShaderCodeV->GetBufferPointer(), &vid->pVertexShader);
|
|
|
|
vid->xdk360_render_device->CreatePixelShader((const DWORD*)pShaderCodeP->GetBufferPointer(), &vid->pPixelShader);
|
2012-01-07 13:54:55 +00:00
|
|
|
pShaderCodeV->Release();
|
|
|
|
pShaderCodeP->Release();
|
2012-01-05 11:37:27 +00:00
|
|
|
|
2012-01-07 14:30:52 +00:00
|
|
|
vid->xdk360_render_device->CreateTexture(512, 512, 1, 0, D3DFMT_LIN_X1R5G5B5,
|
|
|
|
0, &vid->lpTexture, NULL);
|
2012-01-05 11:37:27 +00:00
|
|
|
|
2012-01-07 14:21:23 +00:00
|
|
|
D3DLOCKED_RECT d3dlr;
|
2012-01-07 14:30:52 +00:00
|
|
|
if (SUCCEEDED(vid->lpTexture->LockRect(0, &d3dlr, NULL, D3DLOCK_NOSYSLOCK)))
|
2012-01-07 14:21:23 +00:00
|
|
|
{
|
|
|
|
memset(d3dlr.pBits, 0, 512 * d3dlr.Pitch);
|
2012-01-07 14:30:52 +00:00
|
|
|
vid->lpTexture->UnlockRect(0);
|
2012-01-07 14:21:23 +00:00
|
|
|
}
|
|
|
|
|
2012-01-07 14:30:52 +00:00
|
|
|
vid->last_width = 512;
|
|
|
|
vid->last_height = 512;
|
2012-01-07 14:21:23 +00:00
|
|
|
|
2012-01-07 14:30:52 +00:00
|
|
|
vid->xdk360_render_device->CreateVertexBuffer(4 * sizeof(DrawVerticeFormats), 0,
|
|
|
|
0, 0, &vid->vertex_buf, NULL);
|
2012-01-05 11:37:27 +00:00
|
|
|
|
2012-01-07 13:54:55 +00:00
|
|
|
static const DrawVerticeFormats init_verts[] = {
|
2012-01-07 14:21:23 +00:00
|
|
|
{ -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 },
|
2012-01-07 13:54:55 +00:00
|
|
|
};
|
2012-01-05 11:37:27 +00:00
|
|
|
|
2012-01-07 13:54:55 +00:00
|
|
|
void *verts_ptr;
|
2012-01-07 14:30:52 +00:00
|
|
|
vid->vertex_buf->Lock(0, 0, &verts_ptr, 0);
|
2012-01-07 13:54:55 +00:00
|
|
|
memcpy(verts_ptr, init_verts, sizeof(init_verts));
|
2012-01-07 14:30:52 +00:00
|
|
|
vid->vertex_buf->Unlock();
|
2012-01-05 11:37:27 +00:00
|
|
|
|
2012-01-07 13:54:55 +00:00
|
|
|
static const D3DVERTEXELEMENT9 VertexElements[] =
|
|
|
|
{
|
|
|
|
{ 0, 0 * sizeof(float), D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
|
|
|
|
{ 0, 2 * sizeof(float), D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0 },
|
2012-01-05 11:37:27 +00:00
|
|
|
D3DDECL_END()
|
2012-01-07 13:54:55 +00:00
|
|
|
};
|
2012-01-05 11:37:27 +00:00
|
|
|
|
2012-01-07 14:30:52 +00:00
|
|
|
vid->xdk360_render_device->CreateVertexDeclaration(VertexElements, &vid->pVertexDecl);
|
2012-01-05 11:37:27 +00:00
|
|
|
|
2012-01-07 14:30:52 +00:00
|
|
|
vid->xdk360_render_device->Clear(0, NULL, D3DCLEAR_TARGET,
|
2012-01-07 13:54:55 +00:00
|
|
|
0xff000000, 1.0f, 0);
|
2012-01-05 11:37:27 +00:00
|
|
|
|
2012-01-07 14:30:52 +00:00
|
|
|
vid->xdk360_render_device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
|
|
|
|
vid->xdk360_render_device->SetRenderState(D3DRS_ZENABLE, FALSE);
|
2012-01-07 14:11:57 +00:00
|
|
|
|
|
|
|
D3DVIEWPORT9 vp = {0};
|
2012-02-10 16:31:20 +00:00
|
|
|
vp.Width = video_mode.fIsHiDef ? 1280 : 640;
|
2012-02-10 16:32:43 +00:00
|
|
|
vp.Height = video_mode.fIsHiDef ? 720 : 480;
|
2012-01-07 14:11:57 +00:00
|
|
|
vp.MinZ = 0.0f;
|
|
|
|
vp.MaxZ = 1.0f;
|
2012-01-07 14:30:52 +00:00
|
|
|
vid->xdk360_render_device->SetViewport(&vp);
|
2012-01-07 14:11:57 +00:00
|
|
|
|
2012-01-07 14:30:52 +00:00
|
|
|
return vid;
|
2012-01-05 11:37:27 +00:00
|
|
|
}
|
|
|
|
|
2012-01-07 13:54:55 +00:00
|
|
|
static bool xdk360_gfx_frame(void *data, const void *frame,
|
|
|
|
unsigned width, unsigned height, unsigned pitch, const char *msg)
|
2012-01-05 11:37:27 +00:00
|
|
|
{
|
2012-01-07 13:54:55 +00:00
|
|
|
xdk360_video_t *vid = (xdk360_video_t*)data;
|
2012-01-21 22:59:27 +00:00
|
|
|
g_frame_count++;
|
2012-01-05 11:37:27 +00:00
|
|
|
|
2012-01-07 14:11:57 +00:00
|
|
|
vid->xdk360_render_device->Clear(0, NULL, D3DCLEAR_TARGET,
|
2012-01-07 13:54:55 +00:00
|
|
|
0xff000000, 1.0f, 0);
|
2012-01-05 11:37:27 +00:00
|
|
|
|
2012-01-07 14:21:23 +00:00
|
|
|
if (vid->last_width != width || vid->last_height != height)
|
|
|
|
{
|
|
|
|
D3DLOCKED_RECT d3dlr;
|
2012-01-07 14:28:39 +00:00
|
|
|
if (SUCCEEDED(vid->lpTexture->LockRect(0, &d3dlr, NULL, D3DLOCK_NOSYSLOCK)))
|
2012-01-07 14:21:23 +00:00
|
|
|
{
|
|
|
|
memset(d3dlr.pBits, 0, 512 * d3dlr.Pitch);
|
2012-01-07 14:28:39 +00:00
|
|
|
vid->lpTexture->UnlockRect(0);
|
2012-01-07 14:21:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
float tex_w = width / 512.0f;
|
|
|
|
float tex_h = height / 512.0f;
|
|
|
|
const 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 },
|
|
|
|
};
|
|
|
|
|
|
|
|
void *verts_ptr;
|
|
|
|
vid->vertex_buf->Lock(0, 0, &verts_ptr, 0);
|
|
|
|
memcpy(verts_ptr, verts, sizeof(verts));
|
|
|
|
vid->vertex_buf->Unlock();
|
|
|
|
|
|
|
|
vid->last_width = width;
|
|
|
|
vid->last_height = height;
|
|
|
|
}
|
|
|
|
|
2012-01-07 13:54:55 +00:00
|
|
|
D3DLOCKED_RECT d3dlr;
|
|
|
|
if (SUCCEEDED(vid->lpTexture->LockRect(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 * sizeof(uint16_t));
|
|
|
|
}
|
|
|
|
vid->lpTexture->UnlockRect(0);
|
|
|
|
}
|
2012-01-07 13:13:23 +00:00
|
|
|
|
2012-01-07 13:54:55 +00:00
|
|
|
vid->xdk360_render_device->SetTexture(0, vid->lpTexture);
|
2012-02-10 16:17:44 +00:00
|
|
|
vid->xdk360_render_device->SetSamplerState(0, D3DSAMP_MINFILTER, g_console.filter_type);
|
|
|
|
vid->xdk360_render_device->SetSamplerState(0, D3DSAMP_MAGFILTER, g_console.filter_type);
|
2012-01-07 13:59:11 +00:00
|
|
|
vid->xdk360_render_device->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_BORDER);
|
|
|
|
vid->xdk360_render_device->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_BORDER);
|
2012-01-05 11:37:27 +00:00
|
|
|
|
2012-01-07 13:54:55 +00:00
|
|
|
vid->xdk360_render_device->SetVertexShader(vid->pVertexShader);
|
|
|
|
vid->xdk360_render_device->SetPixelShader(vid->pPixelShader);
|
2012-01-07 13:13:23 +00:00
|
|
|
|
2012-01-07 13:54:55 +00:00
|
|
|
vid->xdk360_render_device->SetVertexDeclaration(vid->pVertexDecl);
|
|
|
|
vid->xdk360_render_device->SetStreamSource(0, vid->vertex_buf, 0, sizeof(DrawVerticeFormats));
|
2012-01-05 11:37:27 +00:00
|
|
|
|
2012-01-07 13:54:55 +00:00
|
|
|
vid->xdk360_render_device->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);
|
2012-01-05 11:37:27 +00:00
|
|
|
vid->xdk360_render_device->Present(NULL, NULL, NULL, NULL);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void xdk360_gfx_set_nonblock_state(void *data, bool state)
|
|
|
|
{
|
|
|
|
(void)data;
|
|
|
|
(void)state;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool xdk360_gfx_alive(void *data)
|
|
|
|
{
|
|
|
|
(void)data;
|
|
|
|
return !g_quitting;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool xdk360_gfx_focus(void *data)
|
|
|
|
{
|
|
|
|
(void)data;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-01-21 22:59:27 +00:00
|
|
|
// 360 needs a working graphics stack before SSNESeven starts.
|
|
|
|
// To deal with this main.c,
|
|
|
|
// the top level module owns the instance, and is created beforehand.
|
|
|
|
// When SSNES gets around to init it, it is already allocated.
|
|
|
|
// When SSNES wants to free it, it is ignored.
|
|
|
|
void xdk360_video_init(void)
|
|
|
|
{
|
|
|
|
video_info_t video_info = {0};
|
|
|
|
// Might have to supply correct values here.
|
|
|
|
video_info.vsync = true;
|
|
|
|
video_info.force_aspect = false;
|
|
|
|
video_info.smooth = true;
|
|
|
|
video_info.input_scale = 2;
|
2012-02-10 16:17:44 +00:00
|
|
|
|
2012-01-21 22:59:27 +00:00
|
|
|
g_d3d = xdk360_gfx_init(&video_info, NULL, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void xdk360_video_deinit(void)
|
|
|
|
{
|
|
|
|
void *data = g_d3d;
|
|
|
|
g_d3d = NULL;
|
|
|
|
xdk360_gfx_free(data);
|
|
|
|
}
|
|
|
|
|
2012-01-05 11:37:27 +00:00
|
|
|
const video_driver_t video_xdk360 = {
|
2012-01-05 12:30:13 +00:00
|
|
|
xdk360_gfx_init,
|
|
|
|
xdk360_gfx_frame,
|
|
|
|
xdk360_gfx_set_nonblock_state,
|
|
|
|
xdk360_gfx_alive,
|
|
|
|
xdk360_gfx_focus,
|
|
|
|
NULL,
|
|
|
|
xdk360_gfx_free,
|
|
|
|
"xdk360"
|
2012-01-05 11:37:27 +00:00
|
|
|
};
|
|
|
|
|