(XDK D3D) Split up render chain code into separate file

This commit is contained in:
twinaphex 2014-06-14 05:56:39 +02:00
parent b9bf87c5f0
commit e51dae46b7
2 changed files with 185 additions and 178 deletions

182
xdk/render_chain_xdk.cpp Normal file
View File

@ -0,0 +1,182 @@
//forward decls
static void renderchain_set_mvp(void *data, unsigned vp_width, unsigned vp_height, unsigned rotation);
static void renderchain_blit_to_texture(void *data, const void *frame,
unsigned width, unsigned height, unsigned pitch);
static void renderchain_set_mvp(void *data, unsigned vp_width, unsigned vp_height, unsigned rotation);
static void renderchain_set_vertices(void *data, unsigned pass, unsigned width, unsigned height);
#if defined(_XBOX360)
#define D3DTexture_Blit(d3d, desc, d3dlr, frame, width, height, pitch) \
d3d->tex->GetLevelDesc(0, &desc); \
XGCopySurface(d3dlr.pBits, d3dlr.Pitch, width, height, desc.Format, NULL, frame, pitch, desc.Format, NULL, 0, 0)
#else
#define D3DTexture_Blit(d3d, desc, d3dlr, frame, width, height, pitch) \
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 * d3d->pixel_size); \
}
#endif
static void renderchain_render_pass(void *data, const void *frame, unsigned width, unsigned height, unsigned pitch, unsigned rotation)
{
d3d_video_t *d3d = (d3d_video_t*)data;
LPDIRECT3DDEVICE d3dr = (LPDIRECT3DDEVICE)d3d->dev;
#ifndef _XBOX1
DWORD fetchConstant;
UINT64 pendingMask3;
#endif
#ifdef _XBOX1
d3dr->SetFlickerFilter(g_extern.console.screen.flicker_filter_index);
d3dr->SetSoftDisplayFilter(g_extern.lifecycle_state & (1ULL << MODE_VIDEO_SOFT_FILTER_ENABLE));
#endif
renderchain_blit_to_texture(d3d, frame, width, height, pitch);
renderchain_set_vertices(d3d, 1, width, height);
RD3DDevice_SetTexture(d3dr, 0, d3d->tex);
RD3DDevice_SetViewport(d3d->dev, &d3d->final_viewport);
D3DDevice_SetSamplerState_MinFilter(d3dr, 0, g_settings.video.smooth ? D3DTEXF_LINEAR : D3DTEXF_POINT);
D3DDevice_SetSamplerState_MagFilter(d3dr, 0, g_settings.video.smooth ? D3DTEXF_LINEAR : D3DTEXF_POINT);
D3DDevice_SetSamplerState_AddressU(d3dr, D3DSAMP_ADDRESSU, D3DTADDRESS_BORDER);
D3DDevice_SetSamplerState_AddressV(d3dr, D3DSAMP_ADDRESSV, D3DTADDRESS_BORDER);
#if defined(_XBOX1)
RD3DDevice_SetVertexShader(d3dr, D3DFVF_XYZ | D3DFVF_TEX1);
#elif defined(_XBOX360)
D3DDevice_SetVertexDeclaration(d3dr, d3d->vertex_decl);
#endif
for (unsigned i = 0; i < 4; i++)
{
D3DDevice_SetStreamSources(d3dr, i, d3d->vertex_buf, 0, sizeof(Vertex));
}
D3DDevice_DrawPrimitive(d3dr, D3DPT_TRIANGLESTRIP, 0, 2);
g_extern.frame_count++;
renderchain_set_mvp(d3d, d3d->screen_width, d3d->screen_height, d3d->dev_rotation);
}
static void renderchain_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;
Vertex 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)
{
renderchain_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 renderchain_set_mvp(void *data, unsigned vp_width, unsigned vp_height, unsigned rotation)
{
d3d_video_t *d3d = (d3d_video_t*)data;
LPDIRECT3DDEVICE d3dr = (LPDIRECT3DDEVICE)d3d->dev;
#if defined(_XBOX360) && defined(HAVE_HLSL)
hlsl_set_proj_matrix(XMMatrixRotationZ(rotation * (M_PI / 2.0)));
if (d3d->shader && d3d->shader->set_mvp)
d3d->shader->set_mvp(d3d, NULL);
#elif defined(_XBOX1)
D3DXMATRIX p_out, p_rotate, mat;
D3DXMatrixOrthoOffCenterLH(&mat, 0, vp_width, vp_height, 0, 0.0f, 1.0f);
D3DXMatrixIdentity(&p_out);
D3DXMatrixRotationZ(&p_rotate, rotation * (M_PI / 2.0));
RD3DDevice_SetTransform(d3dr, D3DTS_WORLD, &p_rotate);
RD3DDevice_SetTransform(d3dr, D3DTS_VIEW, &p_out);
RD3DDevice_SetTransform(d3dr, D3DTS_PROJECTION, &p_out);
#endif
}
static void renderchain_blit_to_texture(void *data, const void *frame,
unsigned width, unsigned height, unsigned pitch)
{
D3DSURFACE_DESC desc;
D3DLOCKED_RECT d3dlr;
d3d_video_t *d3d = (d3d_video_t*)data;
if (d3d->last_width != width || d3d->last_height != height)
{
D3DTexture_LockRectClear(d3d, d3d->tex, 0, d3dlr, NULL, D3DLOCK_NOSYSLOCK);
}
D3DTexture_LockRect(d3d->tex, 0, &d3dlr, NULL, D3DLOCK_NOSYSLOCK);
D3DTexture_Blit(d3d, desc, d3dlr, frame, width, height, pitch);
}

View File

@ -36,6 +36,8 @@
#include "../xdk/xdk_resources.h"
#include "render_chain_xdk.cpp"
static bool d3d_init_shader(void *data)
{
d3d_video_t *d3d = (d3d_video_t*)data;
@ -193,26 +195,6 @@ static void d3d_set_rotation(void *data, unsigned rot)
d3d->dev_rotation = rot;
}
static void set_mvp(void *data, unsigned vp_width, unsigned vp_height, unsigned rotation)
{
d3d_video_t *d3d = (d3d_video_t*)data;
LPDIRECT3DDEVICE d3dr = (LPDIRECT3DDEVICE)d3d->dev;
#if defined(_XBOX360) && defined(HAVE_HLSL)
hlsl_set_proj_matrix(XMMatrixRotationZ(rotation * (M_PI / 2.0)));
if (d3d->shader && d3d->shader->set_mvp)
d3d->shader->set_mvp(d3d, NULL);
#elif defined(_XBOX1)
D3DXMATRIX p_out, p_rotate, mat;
D3DXMatrixOrthoOffCenterLH(&mat, 0, vp_width, vp_height, 0, 0.0f, 1.0f);
D3DXMatrixIdentity(&p_out);
D3DXMatrixRotationZ(&p_rotate, rotation * (M_PI / 2.0));
RD3DDevice_SetTransform(d3dr, D3DTS_WORLD, &p_rotate);
RD3DDevice_SetTransform(d3dr, D3DTS_VIEW, &p_out);
RD3DDevice_SetTransform(d3dr, D3DTS_PROJECTION, &p_out);
#endif
}
static bool d3d_set_shader(void *data, enum rarch_shader_type type, const char *path)
{
/* TODO - stub */
@ -512,163 +494,6 @@ static void d3d_draw_texture(void *data)
}
#endif
#if defined(_XBOX360)
#define D3DTexture_Blit(d3d, desc, d3dlr, frame, width, height, pitch) \
d3d->tex->GetLevelDesc(0, &desc); \
XGCopySurface(d3dlr.pBits, d3dlr.Pitch, width, height, desc.Format, NULL, frame, pitch, desc.Format, NULL, 0, 0)
#else
#define D3DTexture_Blit(d3d, desc, d3dlr, frame, width, height, pitch) \
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 * d3d->pixel_size); \
}
#endif
static void blit_to_texture(void *data, const void *frame,
unsigned width, unsigned height, unsigned pitch)
{
D3DSURFACE_DESC desc;
D3DLOCKED_RECT d3dlr;
d3d_video_t *d3d = (d3d_video_t*)data;
if (d3d->last_width != width || d3d->last_height != height)
{
D3DTexture_LockRectClear(d3d, d3d->tex, 0, d3dlr, NULL, D3DLOCK_NOSYSLOCK);
}
D3DTexture_LockRect(d3d->tex, 0, &d3dlr, NULL, D3DLOCK_NOSYSLOCK);
D3DTexture_Blit(d3d, desc, d3dlr, frame, width, height, pitch);
}
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;
Vertex 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 render_pass(void *data, const void *frame, unsigned width, unsigned height,
unsigned pitch, unsigned rotation)
{
d3d_video_t *d3d = (d3d_video_t*)data;
LPDIRECT3DDEVICE d3dr = (LPDIRECT3DDEVICE)d3d->dev;
#ifndef _XBOX1
DWORD fetchConstant;
UINT64 pendingMask3;
#endif
#ifdef _XBOX1
d3dr->SetFlickerFilter(g_extern.console.screen.flicker_filter_index);
d3dr->SetSoftDisplayFilter(g_extern.lifecycle_state & (1ULL << MODE_VIDEO_SOFT_FILTER_ENABLE));
#endif
blit_to_texture(d3d, frame, width, height, pitch);
set_vertices(d3d, 1, width, height);
RD3DDevice_SetTexture(d3dr, 0, d3d->tex);
RD3DDevice_SetViewport(d3d->dev, &d3d->final_viewport);
D3DDevice_SetSamplerState_MinFilter(d3dr, 0, g_settings.video.smooth ? D3DTEXF_LINEAR : D3DTEXF_POINT);
D3DDevice_SetSamplerState_MagFilter(d3dr, 0, g_settings.video.smooth ? D3DTEXF_LINEAR : D3DTEXF_POINT);
D3DDevice_SetSamplerState_AddressU(d3dr, D3DSAMP_ADDRESSU, D3DTADDRESS_BORDER);
D3DDevice_SetSamplerState_AddressV(d3dr, D3DSAMP_ADDRESSV, D3DTADDRESS_BORDER);
#if defined(_XBOX1)
RD3DDevice_SetVertexShader(d3dr, D3DFVF_XYZ | D3DFVF_TEX1);
#elif defined(_XBOX360)
D3DDevice_SetVertexDeclaration(d3dr, d3d->vertex_decl);
#endif
for (unsigned i = 0; i < 4; i++)
{
D3DDevice_SetStreamSources(d3dr, i, d3d->vertex_buf, 0, sizeof(Vertex));
}
D3DDevice_DrawPrimitive(d3dr, D3DPT_TRIANGLESTRIP, 0, 2);
g_extern.frame_count++;
set_mvp(d3d, d3d->screen_width, d3d->screen_height, d3d->dev_rotation);
}
static void d3d_calculate_rect(void *data, unsigned width, unsigned height,
bool keep, float desired_aspect);
@ -890,7 +715,7 @@ static bool d3d_frame(void *data, const void *frame,
}
#ifdef _XBOX
render_pass(d3d, frame, width, height, pitch, d3d->dev_rotation);
renderchain_render_pass(d3d, frame, width, height, pitch, d3d->dev_rotation);
#else
if (!renderchain_render(d3d->chain, frame, width, height, pitch, d3d->dev_rotation))
{