RetroArch/gfx/drivers_renderchain/d3d9_cg_renderchain.c

1704 lines
51 KiB
C
Raw Normal View History

2012-10-26 19:09:30 +00:00
/* RetroArch - A frontend for libretro.
2014-01-01 00:50:59 +00:00
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
2017-01-22 12:40:32 +00:00
* Copyright (C) 2011-2017 - Daniel De Matteis
*
2012-10-26 19:09:30 +00:00
* RetroArch 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.
*
* RetroArch 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 RetroArch.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include <string.h>
#include <math.h>
2016-09-11 13:07:07 +00:00
#ifdef HAVE_CONFIG_H
#include "../../config.h"
#endif
2017-04-19 00:17:34 +00:00
#include "../include/Cg/cg.h"
#include "../include/Cg/cgD3D9.h"
2015-09-19 01:50:35 +00:00
#include <retro_inline.h>
2017-06-28 02:41:38 +00:00
#include <retro_math.h>
2015-09-19 01:50:35 +00:00
#include <compat/strl.h>
2016-01-09 01:37:48 +00:00
#include <string/stdstring.h>
2015-09-19 01:50:35 +00:00
#include "../drivers/d3d.h"
#include "../drivers/d3d_shaders/opaque.cg.d3d9.h"
2016-09-11 13:07:07 +00:00
2017-04-19 00:17:34 +00:00
#include "../video_driver.h"
#include "../../configuration.h"
#include "../../verbosity.h"
2012-10-26 19:09:30 +00:00
2017-10-02 18:18:36 +00:00
#define D3D_DEFAULT_NONPOW2 ((UINT)-2)
#define D3D_FILTER_LINEAR (3 << 0)
#define D3D_FILTER_POINT (2 << 0)
2017-09-05 02:08:44 +00:00
#define d3d9_cg_set_param_1f(param, x) if (param) cgD3D9SetUniform(param, x)
2017-04-24 08:31:25 +00:00
#define D3D_PI 3.14159265358979323846264338327
2017-09-19 12:54:55 +00:00
#define set_cg_param(prog, param, val) do { \
CGparameter cgp = cgGetNamedParameter(prog, param); \
if (cgp) \
cgD3D9SetUniform(cgp, &val); \
} while(0)
#define VECTOR_LIST_TYPE unsigned
#define VECTOR_LIST_NAME unsigned
#include "../../libretro-common/lists/vector_list.c"
#undef VECTOR_LIST_TYPE
#undef VECTOR_LIST_NAME
struct lut_info
{
LPDIRECT3DTEXTURE tex;
char id[64];
bool smooth;
};
2016-01-08 17:12:49 +00:00
struct CGVertex
{
float x, y, z;
float u, v;
float lut_u, lut_v;
float r, g, b, a;
};
2015-04-05 16:44:12 +00:00
struct Pass
{
2017-10-03 00:45:06 +00:00
struct LinkInfo info;
LPDIRECT3DTEXTURE tex;
LPDIRECT3DVERTEXBUFFER vertex_buf;
CGprogram vPrg, fPrg;
unsigned last_width, last_height;
LPDIRECT3DVERTEXDECLARATION vertex_decl;
struct unsigned_vector_list *attrib_map;
};
2017-10-03 00:45:06 +00:00
#define VECTOR_LIST_TYPE struct Pass
#define VECTOR_LIST_NAME pass
#include "../../libretro-common/lists/vector_list.c"
#undef VECTOR_LIST_TYPE
#undef VECTOR_LIST_NAME
2017-10-03 00:45:06 +00:00
#define VECTOR_LIST_TYPE struct lut_info
#define VECTOR_LIST_NAME lut_info
#include "../../libretro-common/lists/vector_list.c"
#undef VECTOR_LIST_TYPE
#undef VECTOR_LIST_NAME
2015-04-05 22:29:53 +00:00
typedef struct cg_renderchain
{
unsigned pixel_size;
2017-10-02 01:52:53 +00:00
unsigned frame_count;
struct
{
LPDIRECT3DTEXTURE tex[TEXTURES];
LPDIRECT3DVERTEXBUFFER vertex_buf[TEXTURES];
unsigned ptr;
unsigned last_width[TEXTURES];
unsigned last_height[TEXTURES];
} prev;
2017-10-02 01:52:53 +00:00
CGprogram vStock;
CGprogram fStock;
LPDIRECT3DDEVICE dev;
const video_info_t *video_info;
state_tracker_t *state_tracker;
D3DVIEWPORT *final_viewport;
CGcontext cgCtx;
struct pass_vector_list *passes;
struct lut_info_vector_list *luts;
struct unsigned_vector_list *bound_tex;
struct unsigned_vector_list *bound_vert;
2015-04-05 22:32:07 +00:00
} cg_renderchain_t;
2017-09-05 02:08:44 +00:00
static INLINE bool d3d9_cg_validate_param_name(const char *name)
{
unsigned i;
static const char *illegal[] = {
"PREV.",
"PREV1.",
"PREV2.",
"PREV3.",
"PREV4.",
"PREV5.",
"PREV6.",
"ORIG.",
"IN.",
"PASS",
};
if (!name)
return false;
for (i = 0; i < sizeof(illegal) / sizeof(illegal[0]); i++)
if (strstr(name, illegal[i]) == name)
return false;
return true;
}
static INLINE CGparameter d3d9_cg_find_param_from_semantic(
CGparameter param, const char *sem)
{
2016-01-08 17:12:49 +00:00
for (; param; param = cgGetNextParameter(param))
{
2016-01-08 17:12:49 +00:00
const char *semantic = NULL;
if (cgGetParameterType(param) == CG_STRUCT)
{
2016-01-09 06:26:45 +00:00
CGparameter ret = d3d9_cg_find_param_from_semantic(
cgGetFirstStructParameter(param), sem);
if (ret)
return ret;
}
2016-01-08 17:12:49 +00:00
if ( cgGetParameterDirection(param) != CG_IN
2016-01-08 17:12:49 +00:00
|| cgGetParameterVariability(param) != CG_VARYING)
continue;
semantic = cgGetParameterSemantic(param);
if (!semantic)
continue;
if (string_is_equal(sem, semantic) &&
2017-09-05 02:08:44 +00:00
d3d9_cg_validate_param_name(cgGetParameterName(param)))
2016-01-08 17:12:49 +00:00
return param;
}
return NULL;
}
2016-01-09 01:06:45 +00:00
static bool d3d9_cg_load_program(void *data,
2017-09-05 02:08:44 +00:00
void *fragment_data, void *vertex_data,
const char *prog, bool path_is_file)
{
2016-04-17 01:51:24 +00:00
const char *list = NULL;
2016-01-09 01:02:02 +00:00
char *listing_f = NULL;
char *listing_v = NULL;
CGprogram *fPrg = (CGprogram*)fragment_data;
CGprogram *vPrg = (CGprogram*)vertex_data;
CGprofile vertex_profile = cgD3D9GetLatestVertexProfile();
CGprofile fragment_profile = cgD3D9GetLatestPixelProfile();
const char **fragment_opts = cgD3D9GetOptimalOptions(fragment_profile);
const char **vertex_opts = cgD3D9GetOptimalOptions(vertex_profile);
2016-01-09 01:02:02 +00:00
cg_renderchain_t *cg_data = (cg_renderchain_t*)data;
RARCH_LOG("[D3D Cg]: Vertex profile: %s\n", cgGetProfileString(vertex_profile));
RARCH_LOG("[D3D Cg]: Fragment profile: %s\n", cgGetProfileString(fragment_profile));
2016-01-09 02:13:58 +00:00
if (path_is_file && !string_is_empty(prog))
2016-01-09 01:02:02 +00:00
*fPrg = cgCreateProgramFromFile(cg_data->cgCtx, CG_SOURCE,
2016-01-09 02:13:58 +00:00
prog, fragment_profile, "main_fragment", fragment_opts);
else
*fPrg = cgCreateProgram(cg_data->cgCtx, CG_SOURCE, stock_cg_d3d9_program,
fragment_profile, "main_fragment", fragment_opts);
2016-04-17 01:51:24 +00:00
list = cgGetLastListing(cg_data->cgCtx);
if (list)
listing_f = strdup(list);
if (path_is_file && !string_is_empty(prog))
*vPrg = cgCreateProgramFromFile(cg_data->cgCtx, CG_SOURCE,
prog, vertex_profile, "main_vertex", vertex_opts);
else
*vPrg = cgCreateProgram(cg_data->cgCtx, CG_SOURCE, stock_cg_d3d9_program,
vertex_profile, "main_vertex", vertex_opts);
2016-04-17 01:51:24 +00:00
list = cgGetLastListing(cg_data->cgCtx);
if (list)
listing_v = strdup(list);
if (!fPrg || !vPrg)
2016-10-08 06:39:30 +00:00
goto error;
cgD3D9LoadProgram(*fPrg, true, 0);
cgD3D9LoadProgram(*vPrg, true, 0);
2016-04-17 01:51:24 +00:00
2016-01-09 01:02:02 +00:00
free(listing_f);
free(listing_v);
2016-10-08 06:39:30 +00:00
return true;
error:
RARCH_ERR("CG error: %s\n", cgGetErrorString(cgGetError()));
if (listing_f)
RARCH_ERR("Fragment:\n%s\n", listing_f);
else if (listing_v)
RARCH_ERR("Vertex:\n%s\n", listing_v);
free(listing_f);
free(listing_v);
return false;
}
2017-09-05 02:08:44 +00:00
static void d3d9_cg_renderchain_set_shader_params(
cg_renderchain_t *chain,
2017-10-03 00:45:06 +00:00
struct Pass *pass,
unsigned video_w, unsigned video_h,
unsigned tex_w, unsigned tex_h,
unsigned viewport_w, unsigned viewport_h)
{
float frame_cnt;
2017-10-02 18:18:36 +00:00
float video_size[2];
float texture_size[2];
float output_size[2];
2017-10-02 18:18:36 +00:00
video_size[0] = video_w;
video_size[1] = video_h;
texture_size[0] = tex_w;
texture_size[1] = tex_h;
output_size[0] = viewport_w;
output_size[1] = viewport_h;
set_cg_param(pass->vPrg, "IN.video_size", video_size);
set_cg_param(pass->fPrg, "IN.video_size", video_size);
set_cg_param(pass->vPrg, "IN.texture_size", texture_size);
set_cg_param(pass->fPrg, "IN.texture_size", texture_size);
set_cg_param(pass->vPrg, "IN.output_size", output_size);
set_cg_param(pass->fPrg, "IN.output_size", output_size);
frame_cnt = chain->frame_count;
if (pass->info.pass->frame_count_mod)
frame_cnt = chain->frame_count % pass->info.pass->frame_count_mod;
set_cg_param(pass->fPrg, "IN.frame_count", frame_cnt);
set_cg_param(pass->vPrg, "IN.frame_count", frame_cnt);
}
2016-01-08 17:12:49 +00:00
2016-04-23 12:17:32 +00:00
#define DECL_FVF_TEXCOORD(stream, offset, index) \
{ (WORD)(stream), (WORD)(offset * sizeof(float)), D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, \
D3DDECLUSAGE_TEXCOORD, (BYTE)(index) }
#define DECL_FVF_COLOR(stream, offset, index) \
{ (WORD)(stream), (WORD)(offset * sizeof(float)), D3DDECLTYPE_FLOAT4, D3DDECLMETHOD_DEFAULT, \
D3DDECLUSAGE_COLOR, (BYTE)(index) } \
2017-09-05 02:08:44 +00:00
static bool d3d9_cg_renderchain_init_shader_fvf(void *data, void *pass_data)
{
CGparameter param;
unsigned index, i, count;
unsigned tex_index = 0;
bool texcoord0_taken = false;
bool texcoord1_taken = false;
bool stream_taken[4] = {false};
cg_renderchain_t *chain = (cg_renderchain_t*)data;
2017-10-03 00:45:06 +00:00
struct Pass *pass = (struct Pass*)pass_data;
static const D3DVERTEXELEMENT decl_end = D3DDECL_END();
D3DVERTEXELEMENT decl[MAXD3DDECLLENGTH] = {{0}};
bool *indices = NULL;
if (cgD3D9GetVertexDeclaration(pass->vPrg, decl) == CG_FALSE)
return false;
for (count = 0; count < MAXD3DDECLLENGTH; count++)
{
if (string_is_equal_fast(&decl_end, &decl[count], sizeof(decl_end)))
break;
}
/* This is completely insane.
* We do not have a good and easy way of setting up our
* attribute streams, so we have to do it ourselves, yay!
*
* Stream 0 => POSITION
* Stream 1 => TEXCOORD0
* Stream 2 => TEXCOORD1
* Stream 3 => COLOR (Not really used for anything.)
* Stream {4..N} => Texture coord streams for varying resources
* which have no semantics.
*/
indices = (bool*)calloc(1, count * sizeof(*indices));
2016-01-09 06:26:45 +00:00
param = d3d9_cg_find_param_from_semantic(cgGetFirstParameter(pass->vPrg, CG_PROGRAM), "POSITION");
if (!param)
2016-01-09 06:26:45 +00:00
param = d3d9_cg_find_param_from_semantic(cgGetFirstParameter(pass->vPrg, CG_PROGRAM), "POSITION0");
if (param)
{
static const D3DVERTEXELEMENT element =
2017-09-05 01:42:39 +00:00
{
0, 0 * sizeof(float),
D3DDECLTYPE_FLOAT3,
D3DDECLMETHOD_DEFAULT,
D3DDECLUSAGE_POSITION,
0
};
stream_taken[0] = true;
index = cgGetParameterResourceIndex(param);
2016-04-23 12:17:32 +00:00
decl[index] = element;
indices[index] = true;
2016-04-23 12:17:32 +00:00
RARCH_LOG("[FVF]: POSITION semantic found.\n");
}
2016-01-09 06:26:45 +00:00
param = d3d9_cg_find_param_from_semantic(cgGetFirstParameter(pass->vPrg, CG_PROGRAM), "TEXCOORD");
if (!param)
2016-01-09 06:26:45 +00:00
param = d3d9_cg_find_param_from_semantic(cgGetFirstParameter(pass->vPrg, CG_PROGRAM), "TEXCOORD0");
if (param)
{
2016-04-23 12:17:32 +00:00
static const D3DVERTEXELEMENT tex_coord0 = DECL_FVF_TEXCOORD(1, 3, 0);
stream_taken[1] = true;
texcoord0_taken = true;
RARCH_LOG("[FVF]: TEXCOORD0 semantic found.\n");
index = cgGetParameterResourceIndex(param);
decl[index] = tex_coord0;
indices[index] = true;
}
2016-01-09 06:26:45 +00:00
param = d3d9_cg_find_param_from_semantic(cgGetFirstParameter(pass->vPrg, CG_PROGRAM), "TEXCOORD1");
if (param)
{
2016-04-23 12:17:32 +00:00
static const D3DVERTEXELEMENT tex_coord1 = DECL_FVF_TEXCOORD(2, 5, 1);
stream_taken[2] = true;
texcoord1_taken = true;
RARCH_LOG("[FVF]: TEXCOORD1 semantic found.\n");
index = cgGetParameterResourceIndex(param);
decl[index] = tex_coord1;
indices[index] = true;
}
2016-01-09 06:26:45 +00:00
param = d3d9_cg_find_param_from_semantic(cgGetFirstParameter(pass->vPrg, CG_PROGRAM), "COLOR");
if (!param)
2016-01-09 06:26:45 +00:00
param = d3d9_cg_find_param_from_semantic(cgGetFirstParameter(pass->vPrg, CG_PROGRAM), "COLOR0");
if (param)
{
2016-04-23 12:17:32 +00:00
static const D3DVERTEXELEMENT color = DECL_FVF_COLOR(3, 7, 0);
stream_taken[3] = true;
RARCH_LOG("[FVF]: COLOR0 semantic found.\n");
index = cgGetParameterResourceIndex(param);
decl[index] = color;
indices[index] = true;
}
/* Stream {0, 1, 2, 3} might be already taken. Find first vacant stream. */
2016-04-23 12:17:32 +00:00
for (index = 0; index < 4; index++)
{
2017-09-05 01:42:39 +00:00
if (stream_taken[index] == false)
break;
2016-04-23 12:17:32 +00:00
}
/* Find first vacant texcoord declaration. */
if (texcoord0_taken && texcoord1_taken)
tex_index = 2;
else if (texcoord1_taken && !texcoord0_taken)
tex_index = 0;
else if (texcoord0_taken && !texcoord1_taken)
tex_index = 1;
for (i = 0; i < count; i++)
{
if (indices[i])
unsigned_vector_list_append(pass->attrib_map, 0);
else
{
D3DVERTEXELEMENT elem = DECL_FVF_TEXCOORD(index, 3, tex_index);
unsigned_vector_list_append(pass->attrib_map, index);
decl[i] = elem;
/* Find next vacant stream. */
2016-04-23 12:17:32 +00:00
while ((++index < 4) && stream_taken[index])
index++;
/* Find next vacant texcoord declaration. */
2016-04-23 12:17:32 +00:00
if ((++tex_index == 1) && texcoord1_taken)
tex_index++;
}
}
free(indices);
return d3d_vertex_declaration_new(chain->dev,
decl, (void**)&pass->vertex_decl);
}
2017-09-05 02:08:44 +00:00
static void d3d9_cg_renderchain_bind_orig(cg_renderchain_t *chain,
void *pass_data)
{
unsigned index;
CGparameter param;
2017-10-02 18:18:36 +00:00
float video_size[2];
float texture_size[2];
2017-10-03 00:45:06 +00:00
struct Pass *pass = (struct Pass*)pass_data;
video_size[0] = chain->passes->data[0].last_width;
video_size[1] = chain->passes->data[0].last_height;
texture_size[0] = chain->passes->data[0].info.tex_w;
texture_size[1] = chain->passes->data[0].info.tex_h;
set_cg_param(pass->vPrg, "ORIG.video_size", video_size);
set_cg_param(pass->fPrg, "ORIG.video_size", video_size);
set_cg_param(pass->vPrg, "ORIG.texture_size", texture_size);
set_cg_param(pass->fPrg, "ORIG.texture_size", texture_size);
param = cgGetNamedParameter(pass->fPrg, "ORIG.texture");
if (param)
{
index = cgGetParameterResourceIndex(param);
d3d_set_texture(chain->dev, index, chain->passes->data[0].tex);
2015-04-05 15:41:04 +00:00
d3d_set_sampler_magfilter(chain->dev, index,
d3d_translate_filter(chain->passes->data[0].info.pass->filter));
d3d_set_sampler_minfilter(chain->dev, index,
d3d_translate_filter(chain->passes->data[0].info.pass->filter));
2015-04-05 15:41:04 +00:00
d3d_set_sampler_address_u(chain->dev, index, D3DTADDRESS_BORDER);
d3d_set_sampler_address_v(chain->dev, index, D3DTADDRESS_BORDER);
unsigned_vector_list_append(chain->bound_tex, index);
}
param = cgGetNamedParameter(pass->vPrg, "ORIG.tex_coord");
if (param)
{
LPDIRECT3DVERTEXBUFFER vert_buf = (LPDIRECT3DVERTEXBUFFER)chain->passes->data[0].vertex_buf;
2016-01-08 19:40:26 +00:00
index = pass->attrib_map->data[cgGetParameterResourceIndex(param)];
2017-10-03 00:45:06 +00:00
d3d_set_stream_source(chain->dev, index,
vert_buf, 0, sizeof(struct CGVertex));
unsigned_vector_list_append(chain->bound_vert, index);
}
}
2017-09-05 02:08:44 +00:00
static void d3d9_cg_renderchain_bind_prev(void *data, const void *pass_data)
{
unsigned i, index;
2017-10-02 18:18:36 +00:00
float texture_size[2];
2015-06-13 00:10:06 +00:00
char attr_texture[64] = {0};
char attr_input_size[64] = {0};
char attr_tex_size[64] = {0};
char attr_coord[64] = {0};
2016-01-08 19:28:18 +00:00
cg_renderchain_t *chain = (cg_renderchain_t*)data;
2017-10-03 00:45:06 +00:00
struct Pass *pass = (struct Pass*)pass_data;
static const char *prev_names[] = {
"PREV",
"PREV1",
"PREV2",
"PREV3",
"PREV4",
"PREV5",
"PREV6",
};
texture_size[0] = chain->passes->data[0].info.tex_w;
texture_size[1] = chain->passes->data[0].info.tex_h;
for (i = 0; i < TEXTURES - 1; i++)
{
CGparameter param;
2017-10-02 18:18:36 +00:00
float video_size[2];
snprintf(attr_texture, sizeof(attr_texture), "%s.texture", prev_names[i]);
snprintf(attr_input_size, sizeof(attr_input_size), "%s.video_size", prev_names[i]);
snprintf(attr_tex_size, sizeof(attr_tex_size), "%s.texture_size", prev_names[i]);
snprintf(attr_coord, sizeof(attr_coord), "%s.tex_coord", prev_names[i]);
2017-10-02 18:18:36 +00:00
video_size[0] = chain->prev.last_width[(chain->prev.ptr - (i + 1)) & TEXTURESMASK];
video_size[1] = chain->prev.last_height[(chain->prev.ptr - (i + 1)) & TEXTURESMASK];
set_cg_param(pass->vPrg, attr_input_size, video_size);
set_cg_param(pass->fPrg, attr_input_size, video_size);
set_cg_param(pass->vPrg, attr_tex_size, texture_size);
set_cg_param(pass->fPrg, attr_tex_size, texture_size);
param = cgGetNamedParameter(pass->fPrg, attr_texture);
if (param)
{
LPDIRECT3DTEXTURE tex;
index = cgGetParameterResourceIndex(param);
tex = (LPDIRECT3DTEXTURE)
chain->prev.tex[(chain->prev.ptr - (i + 1)) & TEXTURESMASK];
2015-04-05 15:44:27 +00:00
d3d_set_texture(chain->dev, index, tex);
unsigned_vector_list_append(chain->bound_tex, index);
2015-04-05 15:41:04 +00:00
d3d_set_sampler_magfilter(chain->dev, index,
d3d_translate_filter(chain->passes->data[0].info.pass->filter));
d3d_set_sampler_minfilter(chain->dev, index,
d3d_translate_filter(chain->passes->data[0].info.pass->filter));
2015-04-05 15:41:04 +00:00
d3d_set_sampler_address_u(chain->dev, index, D3DTADDRESS_BORDER);
d3d_set_sampler_address_v(chain->dev, index, D3DTADDRESS_BORDER);
}
param = cgGetNamedParameter(pass->vPrg, attr_coord);
if (param)
{
LPDIRECT3DVERTEXBUFFER vert_buf = (LPDIRECT3DVERTEXBUFFER)
chain->prev.vertex_buf[(chain->prev.ptr - (i + 1)) & TEXTURESMASK];
index = pass->attrib_map->data[cgGetParameterResourceIndex(param)];
2017-10-03 00:45:06 +00:00
d3d_set_stream_source(chain->dev, index,
vert_buf, 0, sizeof(struct CGVertex));
unsigned_vector_list_append(chain->bound_vert, index);
}
}
}
2017-09-05 02:08:44 +00:00
static void d3d9_cg_renderchain_add_lut_internal(void *data,
unsigned index, unsigned i)
{
2015-04-06 14:08:23 +00:00
cg_renderchain_t *chain = (cg_renderchain_t*)data;
if (!chain)
return;
d3d_set_texture(chain->dev, index, chain->luts->data[i].tex);
2015-04-05 15:41:04 +00:00
d3d_set_sampler_magfilter(chain->dev, index,
d3d_translate_filter(chain->luts->data[i].smooth ? RARCH_FILTER_LINEAR : RARCH_FILTER_NEAREST));
d3d_set_sampler_minfilter(chain->dev, index,
d3d_translate_filter(chain->luts->data[i].smooth ? RARCH_FILTER_LINEAR : RARCH_FILTER_NEAREST));
2015-04-05 15:41:04 +00:00
d3d_set_sampler_address_u(chain->dev, index, D3DTADDRESS_BORDER);
d3d_set_sampler_address_v(chain->dev, index, D3DTADDRESS_BORDER);
unsigned_vector_list_append(chain->bound_tex, index);
}
2017-09-05 02:08:44 +00:00
static void d3d9_cg_renderchain_bind_pass(
cg_renderchain_t *chain,
2017-10-03 00:45:06 +00:00
struct Pass *pass, unsigned pass_index)
{
unsigned i, index;
/* We only bother binding passes which are two indices behind. */
if (pass_index < 3)
return;
for (i = 1; i < pass_index - 1; i++)
{
CGparameter param;
2017-10-02 18:18:36 +00:00
float video_size[2];
float texture_size[2];
2015-06-13 00:10:06 +00:00
char pass_base[64] = {0};
char attr_texture[64] = {0};
char attr_input_size[64] = {0};
char attr_tex_size[64] = {0};
char attr_coord[64] = {0};
snprintf(pass_base, sizeof(pass_base), "PASS%u", i);
snprintf(attr_texture, sizeof(attr_texture), "%s.texture", pass_base);
snprintf(attr_input_size, sizeof(attr_input_size), "%s.video_size", pass_base);
snprintf(attr_tex_size, sizeof(attr_tex_size), "%s.texture_size", pass_base);
snprintf(attr_coord, sizeof(attr_coord), "%s.tex_coord", pass_base);
video_size[0] = chain->passes->data[i].last_width;
video_size[1] = chain->passes->data[i].last_height;
texture_size[0] = chain->passes->data[i].info.tex_w;
texture_size[1] = chain->passes->data[i].info.tex_h;
set_cg_param(pass->vPrg, attr_input_size, video_size);
set_cg_param(pass->fPrg, attr_input_size, video_size);
set_cg_param(pass->vPrg, attr_tex_size, texture_size);
set_cg_param(pass->fPrg, attr_tex_size, texture_size);
param = cgGetNamedParameter(pass->fPrg, attr_texture);
if (param)
{
index = cgGetParameterResourceIndex(param);
unsigned_vector_list_append(chain->bound_tex, index);
d3d_set_texture(chain->dev, index, chain->passes->data[i].tex);
2015-04-05 15:41:04 +00:00
d3d_set_sampler_magfilter(chain->dev, index,
d3d_translate_filter(chain->passes->data[i].info.pass->filter));
d3d_set_sampler_minfilter(chain->dev, index,
d3d_translate_filter(chain->passes->data[i].info.pass->filter));
2015-04-05 15:41:04 +00:00
d3d_set_sampler_address_u(chain->dev, index, D3DTADDRESS_BORDER);
d3d_set_sampler_address_v(chain->dev, index, D3DTADDRESS_BORDER);
}
param = cgGetNamedParameter(pass->vPrg, attr_coord);
if (param)
{
index = pass->attrib_map->data[cgGetParameterResourceIndex(param)];
d3d_set_stream_source(chain->dev, index, chain->passes->data[i].vertex_buf,
2017-10-03 00:45:06 +00:00
0, sizeof(struct CGVertex));
unsigned_vector_list_append(chain->bound_vert, index);
}
}
}
2016-01-08 17:23:40 +00:00
static void d3d9_cg_deinit_progs(void *data)
2015-04-05 21:19:10 +00:00
{
unsigned i;
2016-01-08 18:16:11 +00:00
cg_renderchain_t *cg_data = (cg_renderchain_t*)data;
2016-01-08 17:23:40 +00:00
2016-01-08 18:16:11 +00:00
if (!cg_data)
2017-09-05 01:42:39 +00:00
return;
2015-04-05 21:19:10 +00:00
2016-01-08 17:23:40 +00:00
RARCH_LOG("CG: Destroying programs.\n");
if (cg_data->passes->count >= 1)
2015-04-05 21:19:10 +00:00
{
d3d_vertex_buffer_free(NULL, cg_data->passes->data[0].vertex_decl);
2016-01-08 18:16:11 +00:00
for (i = 1; i < cg_data->passes->count; i++)
2016-01-08 18:16:11 +00:00
{
if (cg_data->passes->data[i].tex)
d3d_texture_free(cg_data->passes->data[i].tex);
cg_data->passes->data[i].tex = NULL;
2017-09-05 02:08:44 +00:00
d3d_vertex_buffer_free(
cg_data->passes->data[i].vertex_buf,
cg_data->passes->data[i].vertex_decl);
2016-01-09 00:12:50 +00:00
if (cg_data->passes->data[i].fPrg)
cgDestroyProgram(cg_data->passes->data[i].fPrg);
if (cg_data->passes->data[i].vPrg)
cgDestroyProgram(cg_data->passes->data[i].vPrg);
2016-01-08 18:16:11 +00:00
}
2015-04-05 21:19:10 +00:00
}
2016-01-08 18:16:11 +00:00
if (cg_data->fStock)
cgDestroyProgram(cg_data->fStock);
if (cg_data->vStock)
cgDestroyProgram(cg_data->vStock);
2015-11-09 16:18:21 +00:00
}
2016-01-08 18:16:11 +00:00
static void d3d9_cg_destroy_resources(void *data)
2015-11-09 16:18:21 +00:00
{
unsigned i;
2016-01-08 18:16:11 +00:00
cg_renderchain_t *cg_data = (cg_renderchain_t*)data;
2015-11-09 16:18:21 +00:00
for (i = 0; i < TEXTURES; i++)
{
2016-01-08 18:16:11 +00:00
if (cg_data->prev.tex[i])
d3d_texture_free(cg_data->prev.tex[i]);
if (cg_data->prev.vertex_buf[i])
d3d_vertex_buffer_free(cg_data->prev.vertex_buf[i], NULL);
2015-11-09 16:18:21 +00:00
}
2016-01-08 18:16:11 +00:00
d3d9_cg_deinit_progs(cg_data);
2015-04-05 21:19:10 +00:00
for (i = 0; i < cg_data->luts->count; i++)
2015-04-05 21:19:10 +00:00
{
if (cg_data->luts->data[i].tex)
d3d_texture_free(cg_data->luts->data[i].tex);
2015-04-05 21:19:10 +00:00
}
2016-01-08 19:28:18 +00:00
if (cg_data->state_tracker)
2016-01-08 17:23:40 +00:00
{
2016-01-08 19:28:18 +00:00
state_tracker_free(cg_data->state_tracker);
cg_data->state_tracker = NULL;
2016-01-08 17:23:40 +00:00
}
2016-01-08 18:16:11 +00:00
cgD3D9UnloadAllPrograms();
cgD3D9SetDevice(NULL);
2015-04-05 21:19:10 +00:00
}
2016-01-08 17:23:40 +00:00
static void d3d9_cg_deinit_context_state(void *data)
{
2016-01-08 18:16:11 +00:00
cg_renderchain_t *cg_data = (cg_renderchain_t*)data;
if (cg_data->cgCtx)
2016-01-08 17:23:40 +00:00
{
RARCH_LOG("CG: Destroying context.\n");
2016-01-08 18:16:11 +00:00
cgDestroyContext(cg_data->cgCtx);
2016-01-08 17:23:40 +00:00
}
2016-01-08 18:16:11 +00:00
cg_data->cgCtx = NULL;
}
2017-09-05 02:08:44 +00:00
void d3d9_cg_renderchain_free(void *data)
{
2016-01-08 18:16:11 +00:00
cg_renderchain_t *cg_data = (cg_renderchain_t*)data;
2016-01-08 18:16:11 +00:00
if (!cg_data)
return;
2016-01-08 18:16:11 +00:00
d3d9_cg_destroy_resources(cg_data);
if (cg_data->passes)
{
unsigned i;
for (i = 0; i < cg_data->passes->count; i++)
{
if (cg_data->passes->data[i].attrib_map)
free(cg_data->passes->data[i].attrib_map);
}
pass_vector_list_free(cg_data->passes);
cg_data->passes = NULL;
}
lut_info_vector_list_free(cg_data->luts);
unsigned_vector_list_free(cg_data->bound_tex);
unsigned_vector_list_free(cg_data->bound_vert);
cg_data->luts = NULL;
cg_data->bound_tex = NULL;
cg_data->bound_vert = NULL;
2016-01-08 18:16:11 +00:00
d3d9_cg_deinit_context_state(cg_data);
2017-10-03 00:45:06 +00:00
free(cg_data);
2012-10-26 19:09:30 +00:00
}
2017-09-05 02:08:44 +00:00
static void *d3d9_cg_renderchain_new(void)
{
2017-10-03 00:45:06 +00:00
cg_renderchain_t *renderchain = (cg_renderchain_t*)calloc(1, sizeof(*renderchain));
if (!renderchain)
return NULL;
2017-10-03 00:45:06 +00:00
renderchain->passes = pass_vector_list_new();
renderchain->luts = lut_info_vector_list_new();
renderchain->bound_tex = unsigned_vector_list_new();
renderchain->bound_vert = unsigned_vector_list_new();
return renderchain;
}
2017-09-05 02:08:44 +00:00
static bool d3d9_cg_renderchain_init_shader(void *data,
void *renderchain_data)
2015-04-05 15:59:03 +00:00
{
d3d_video_t *d3d = (d3d_video_t*)data;
cg_renderchain_t *renderchain = (cg_renderchain_t*)renderchain_data;
2015-04-05 15:59:03 +00:00
if (!d3d || !renderchain)
2015-04-05 15:59:03 +00:00
return false;
renderchain->cgCtx = cgCreateContext();
if (!renderchain->cgCtx)
2016-01-08 19:46:33 +00:00
{
RARCH_ERR("Failed to create Cg context.\n");
2015-04-05 15:59:03 +00:00
return false;
2016-01-08 19:46:33 +00:00
}
2015-04-05 15:59:03 +00:00
2017-10-03 00:49:06 +00:00
if (FAILED(cgD3D9SetDevice((IDirect3DDevice9*)d3d->dev)))
2015-04-05 15:59:03 +00:00
return false;
return true;
}
2017-09-05 02:08:44 +00:00
static void d3d9_cg_renderchain_log_info(
void *data, const void *info_data)
{
2017-10-03 00:45:06 +00:00
const struct LinkInfo *info = (const struct LinkInfo*)info_data;
RARCH_LOG("[D3D]: Render pass info:\n");
RARCH_LOG("\tTexture width: %u\n", info->tex_w);
RARCH_LOG("\tTexture height: %u\n", info->tex_h);
RARCH_LOG("\tScale type (X): ");
switch (info->pass->fbo.type_x)
{
case RARCH_SCALE_INPUT:
RARCH_LOG("Relative @ %fx\n", info->pass->fbo.scale_x);
break;
case RARCH_SCALE_VIEWPORT:
RARCH_LOG("Viewport @ %fx\n", info->pass->fbo.scale_x);
break;
case RARCH_SCALE_ABSOLUTE:
RARCH_LOG("Absolute @ %u px\n", info->pass->fbo.abs_x);
break;
}
RARCH_LOG("\tScale type (Y): ");
switch (info->pass->fbo.type_y)
{
case RARCH_SCALE_INPUT:
RARCH_LOG("Relative @ %fx\n", info->pass->fbo.scale_y);
break;
case RARCH_SCALE_VIEWPORT:
RARCH_LOG("Viewport @ %fx\n", info->pass->fbo.scale_y);
break;
case RARCH_SCALE_ABSOLUTE:
RARCH_LOG("Absolute @ %u px\n", info->pass->fbo.abs_y);
break;
}
RARCH_LOG("\tBilinear filter: %s\n",
info->pass->filter == RARCH_FILTER_LINEAR ? "true" : "false");
}
2017-09-05 02:08:44 +00:00
static bool d3d9_cg_renderchain_create_first_pass(
cg_renderchain_t *chain,
2017-10-03 00:45:06 +00:00
const struct LinkInfo *info, unsigned fmt)
2015-04-05 21:19:10 +00:00
{
unsigned i;
2017-10-03 00:45:06 +00:00
struct Pass pass;
2017-10-02 17:01:23 +00:00
D3DMATRIX ident;
2015-04-05 21:19:10 +00:00
if (!chain)
2017-09-05 01:42:39 +00:00
return false;
pass.attrib_map = unsigned_vector_list_new();
2017-10-02 17:01:23 +00:00
d3d_matrix_identity(&ident);
2015-04-05 21:19:10 +00:00
2016-01-07 05:38:28 +00:00
d3d_set_transform(chain->dev, D3DTS_WORLD, &ident);
d3d_set_transform(chain->dev, D3DTS_VIEW, &ident);
2015-04-05 21:19:10 +00:00
pass.info = *info;
pass.last_width = 0;
pass.last_height = 0;
pass.attrib_map = unsigned_vector_list_new();
2015-04-05 21:19:10 +00:00
chain->prev.ptr = 0;
for (i = 0; i < TEXTURES; i++)
{
chain->prev.last_width[i] = 0;
chain->prev.last_height[i] = 0;
chain->prev.vertex_buf[i] = d3d_vertex_buffer_new(
2017-10-03 00:45:06 +00:00
chain->dev, 4 * sizeof(struct CGVertex),
D3DUSAGE_WRITEONLY, 0, D3DPOOL_DEFAULT, NULL);
2015-04-05 21:19:10 +00:00
if (!chain->prev.vertex_buf[i])
return false;
2016-01-07 05:38:28 +00:00
chain->prev.tex[i] = d3d_texture_new(chain->dev, NULL,
2015-11-11 03:16:18 +00:00
info->tex_w, info->tex_h, 1, 0,
2017-09-05 01:42:39 +00:00
(fmt == RETRO_PIXEL_FORMAT_RGB565) ? D3DFMT_R5G6B5 : D3DFMT_X8R8G8B8,
D3DPOOL_MANAGED, 0, 0, 0, NULL, NULL);
2015-04-05 21:19:10 +00:00
if (!chain->prev.tex[i])
return false;
2016-01-07 05:38:28 +00:00
d3d_set_texture(chain->dev, 0, chain->prev.tex[i]);
d3d_set_sampler_minfilter(chain->dev, 0,
d3d_translate_filter(info->pass->filter));
2016-01-07 05:38:28 +00:00
d3d_set_sampler_magfilter(chain->dev, 0,
d3d_translate_filter(info->pass->filter));
2016-01-07 05:38:28 +00:00
d3d_set_sampler_address_u(chain->dev, 0, D3DTADDRESS_BORDER);
d3d_set_sampler_address_v(chain->dev, 0, D3DTADDRESS_BORDER);
d3d_set_texture(chain->dev, 0, NULL);
2015-04-05 21:19:10 +00:00
}
2016-01-09 01:06:45 +00:00
d3d9_cg_load_program(chain, &pass.fPrg,
2016-01-09 01:17:43 +00:00
&pass.vPrg, info->pass->source.path, true);
2015-04-05 21:19:10 +00:00
2017-09-05 02:08:44 +00:00
if (!d3d9_cg_renderchain_init_shader_fvf(chain, &pass))
2015-04-05 21:19:10 +00:00
return false;
pass_vector_list_append(chain->passes, pass);
2015-04-05 21:19:10 +00:00
return true;
}
2017-09-05 02:08:44 +00:00
static bool d3d9_cg_renderchain_init(void *data,
const void *_video_info,
2015-04-05 02:30:50 +00:00
void *dev_,
const void *final_viewport_,
2015-11-11 01:42:15 +00:00
const void *info_data, bool rgb32)
2012-10-26 19:09:30 +00:00
{
2017-10-03 00:45:06 +00:00
const struct LinkInfo *info = (const struct LinkInfo*)info_data;
2015-11-11 01:42:15 +00:00
d3d_video_t *d3d = (d3d_video_t*)data;
cg_renderchain_t *chain = (cg_renderchain_t*)d3d->renderchain_data;
const video_info_t *video_info = (const video_info_t*)_video_info;
2015-11-11 01:42:15 +00:00
unsigned fmt = (rgb32) ? RETRO_PIXEL_FORMAT_XRGB8888 : RETRO_PIXEL_FORMAT_RGB565;
2017-09-05 01:42:39 +00:00
if (!chain)
return false;
2017-09-05 02:08:44 +00:00
if (!d3d9_cg_renderchain_init_shader(d3d, chain))
{
RARCH_ERR("Failed to initialize shader subsystem.\n");
return false;
}
2015-04-05 02:30:50 +00:00
chain->dev = (LPDIRECT3DDEVICE)dev_;
2015-04-04 23:15:54 +00:00
chain->video_info = video_info;
2016-01-08 19:28:18 +00:00
chain->state_tracker = NULL;
2015-04-05 13:53:06 +00:00
chain->final_viewport = (D3DVIEWPORT*)final_viewport_;
2015-04-04 23:15:54 +00:00
chain->frame_count = 0;
2015-04-05 14:48:22 +00:00
chain->pixel_size = (fmt == RETRO_PIXEL_FORMAT_RGB565) ? 2 : 4;
2015-04-04 23:15:54 +00:00
2017-09-05 02:08:44 +00:00
if (!d3d9_cg_renderchain_create_first_pass(chain, info, fmt))
return false;
2017-09-05 02:08:44 +00:00
d3d9_cg_renderchain_log_info(chain, info);
2016-01-09 01:37:48 +00:00
if (!d3d9_cg_load_program(chain, &chain->fStock, &chain->vStock, NULL, false))
return false;
2017-09-19 12:54:55 +00:00
cgD3D9BindProgram(chain->fStock);
cgD3D9BindProgram(chain->vStock);
2016-01-09 02:53:06 +00:00
return true;
2012-10-26 19:09:30 +00:00
}
2017-09-05 02:08:44 +00:00
static bool d3d9_cg_renderchain_set_pass_size(
cg_renderchain_t *chain,
unsigned pass_index, unsigned width, unsigned height)
2012-10-26 19:09:30 +00:00
{
2017-10-03 00:45:06 +00:00
struct Pass *pass = (struct Pass*)&chain->passes->data[pass_index];
2015-01-17 05:30:39 +00:00
2015-04-05 21:19:10 +00:00
if (width != pass->info.tex_w || height != pass->info.tex_h)
2012-10-26 19:09:30 +00:00
{
2015-04-05 21:19:10 +00:00
d3d_texture_free(pass->tex);
2015-01-17 05:30:39 +00:00
2015-04-05 21:19:10 +00:00
pass->info.tex_w = width;
pass->info.tex_h = height;
2016-01-07 05:38:28 +00:00
pass->tex = d3d_texture_new(chain->dev, NULL,
2015-11-11 03:16:18 +00:00
width, height, 1,
D3DUSAGE_RENDERTARGET,
chain->passes->data[chain->passes->count - 1].info.pass->fbo.fp_fbo ?
2015-11-11 03:16:18 +00:00
D3DFMT_A32B32G32R32F : D3DFMT_A8R8G8B8,
D3DPOOL_DEFAULT, 0, 0, 0,
NULL, NULL);
2017-09-05 01:42:39 +00:00
2015-04-05 21:19:10 +00:00
if (!pass->tex)
return false;
2012-10-26 19:09:30 +00:00
2016-01-07 05:38:28 +00:00
d3d_set_texture(chain->dev, 0, pass->tex);
d3d_set_sampler_address_u(chain->dev, 0, D3DTADDRESS_BORDER);
d3d_set_sampler_address_v(chain->dev, 0, D3DTADDRESS_BORDER);
d3d_set_texture(chain->dev, 0, NULL);
2012-10-26 19:09:30 +00:00
}
2015-04-05 21:19:10 +00:00
return true;
2012-10-26 19:09:30 +00:00
}
2017-09-05 02:08:44 +00:00
static void d3d9_cg_renderchain_convert_geometry(
void *data,
const void *info_data,
unsigned *out_width,
unsigned *out_height,
unsigned width,
unsigned height,
void *final_viewport_data)
{
2017-10-03 00:45:06 +00:00
const struct LinkInfo *info = (const struct LinkInfo*)info_data;
cg_renderchain_t *chain = (cg_renderchain_t*)data;
D3DVIEWPORT *final_viewport = (D3DVIEWPORT*)final_viewport_data;
if (!chain || !info)
return;
switch (info->pass->fbo.type_x)
{
case RARCH_SCALE_VIEWPORT:
*out_width = info->pass->fbo.scale_x * final_viewport->Width;
break;
case RARCH_SCALE_ABSOLUTE:
*out_width = info->pass->fbo.abs_x;
break;
case RARCH_SCALE_INPUT:
*out_width = info->pass->fbo.scale_x * width;
break;
}
switch (info->pass->fbo.type_y)
{
case RARCH_SCALE_VIEWPORT:
*out_height = info->pass->fbo.scale_y * final_viewport->Height;
break;
case RARCH_SCALE_ABSOLUTE:
*out_height = info->pass->fbo.abs_y;
break;
case RARCH_SCALE_INPUT:
*out_height = info->pass->fbo.scale_y * height;
break;
}
}
static void d3d_recompute_pass_sizes(cg_renderchain_t *chain,
d3d_video_t *d3d)
{
unsigned i;
2017-10-03 00:45:06 +00:00
struct LinkInfo link_info = {0};
2017-10-03 00:49:06 +00:00
unsigned current_width = d3d->video_info.input_scale * RARCH_SCALE_BASE;
unsigned current_height = d3d->video_info.input_scale * RARCH_SCALE_BASE;
unsigned out_width = 0;
unsigned out_height = 0;
2017-10-03 00:49:06 +00:00
link_info.pass = &d3d->shader.pass[0];
link_info.tex_w = current_width;
link_info.tex_h = current_height;
2017-09-05 02:08:44 +00:00
if (!d3d9_cg_renderchain_set_pass_size(chain, 0,
current_width, current_height))
{
RARCH_ERR("[D3D]: Failed to set pass size.\n");
return;
}
for (i = 1; i < d3d->shader.passes; i++)
{
2017-09-05 02:08:44 +00:00
d3d9_cg_renderchain_convert_geometry(chain,
2017-09-05 01:42:39 +00:00
&link_info,
&out_width, &out_height,
current_width, current_height, &d3d->final_viewport);
link_info.tex_w = next_pow2(out_width);
link_info.tex_h = next_pow2(out_height);
2017-09-05 02:08:44 +00:00
if (!d3d9_cg_renderchain_set_pass_size(chain, i,
link_info.tex_w, link_info.tex_h))
{
RARCH_ERR("[D3D]: Failed to set pass size.\n");
return;
}
2015-04-06 14:15:29 +00:00
current_width = out_width;
current_height = out_height;
link_info.pass = &d3d->shader.pass[i];
}
}
2017-09-05 02:08:44 +00:00
static void d3d9_cg_renderchain_set_final_viewport(
void *data,
void *renderchain_data,
const void *viewport_data)
{
2015-04-05 16:33:42 +00:00
d3d_video_t *d3d = (d3d_video_t*)data;
2015-04-05 22:29:53 +00:00
cg_renderchain_t *chain = (cg_renderchain_t*)renderchain_data;
2015-04-05 02:30:50 +00:00
const D3DVIEWPORT *final_viewport = (const D3DVIEWPORT*)viewport_data;
2015-01-17 05:30:39 +00:00
if (chain)
chain->final_viewport = (D3DVIEWPORT*)final_viewport;
d3d_recompute_pass_sizes(chain, d3d);
}
2017-09-05 02:08:44 +00:00
static bool d3d9_cg_renderchain_add_pass(
void *data,
const void *info_data)
2012-10-26 19:09:30 +00:00
{
2017-10-03 00:45:06 +00:00
struct Pass pass;
const struct LinkInfo *info = (const struct LinkInfo*)info_data;
cg_renderchain_t *chain = (cg_renderchain_t*)data;
2015-01-17 05:30:39 +00:00
2017-10-03 00:45:06 +00:00
pass.info = *info;
pass.last_width = 0;
pass.last_height = 0;
pass.attrib_map = unsigned_vector_list_new();
2012-10-26 19:09:30 +00:00
d3d9_cg_load_program(chain, &pass.fPrg,
2017-09-05 01:42:39 +00:00
&pass.vPrg, info->pass->source.path, true);
2014-09-08 18:08:49 +00:00
2017-09-05 02:08:44 +00:00
if (!d3d9_cg_renderchain_init_shader_fvf(chain, &pass))
return false;
2012-10-26 19:09:30 +00:00
2017-10-03 00:45:06 +00:00
pass.vertex_buf = d3d_vertex_buffer_new(chain->dev,
4 * sizeof(struct CGVertex),
D3DUSAGE_WRITEONLY, 0, D3DPOOL_DEFAULT, NULL);
if (!pass.vertex_buf)
return false;
2012-10-26 19:09:30 +00:00
2016-01-07 05:38:28 +00:00
pass.tex = d3d_texture_new(
chain->dev,
NULL,
info->tex_w,
info->tex_h,
1,
D3DUSAGE_RENDERTARGET,
chain->passes->data[chain->passes->count - 1].info.pass->fbo.fp_fbo
2016-01-07 05:38:28 +00:00
? D3DFMT_A32B32G32R32F : D3DFMT_A8R8G8B8,
D3DPOOL_DEFAULT, 0, 0, 0, NULL, NULL);
2014-09-13 16:36:35 +00:00
if (!pass.tex)
return false;
2012-10-26 19:09:30 +00:00
2016-01-07 05:38:28 +00:00
d3d_set_texture(chain->dev, 0, pass.tex);
d3d_set_sampler_address_u(chain->dev, 0, D3DTADDRESS_BORDER);
d3d_set_sampler_address_v(chain->dev, 0, D3DTADDRESS_BORDER);
d3d_set_texture(chain->dev, 0, NULL);
2012-10-26 19:09:30 +00:00
pass_vector_list_append(chain->passes, pass);
2012-10-26 19:09:30 +00:00
2017-09-05 02:08:44 +00:00
d3d9_cg_renderchain_log_info(chain, info);
return true;
2012-10-26 19:09:30 +00:00
}
2017-09-05 02:08:44 +00:00
static bool d3d9_cg_renderchain_add_lut(void *data,
const char *id, const char *path, bool smooth)
2012-10-26 19:09:30 +00:00
{
2017-10-03 00:45:06 +00:00
struct lut_info info;
2015-11-11 01:04:42 +00:00
cg_renderchain_t *chain = (cg_renderchain_t*)data;
2016-01-07 05:38:28 +00:00
LPDIRECT3DTEXTURE lut = d3d_texture_new(
chain->dev,
2017-09-05 01:42:39 +00:00
path,
2017-10-02 18:18:36 +00:00
D3D_DEFAULT_NONPOW2,
D3D_DEFAULT_NONPOW2,
2017-09-05 01:42:39 +00:00
0,
0,
D3DFMT_FROM_FILE,
D3DPOOL_MANAGED,
2017-10-02 18:18:36 +00:00
smooth ? D3D_FILTER_LINEAR : D3D_FILTER_POINT,
2017-09-05 01:42:39 +00:00
0,
0,
NULL,
NULL
);
2015-04-05 00:31:24 +00:00
RARCH_LOG("[D3D]: LUT texture loaded: %s.\n", path);
2016-01-07 03:42:49 +00:00
info.tex = lut;
info.smooth = smooth;
2016-06-27 06:09:34 +00:00
strlcpy(info.id, id, sizeof(info.id));
2014-09-13 16:25:31 +00:00
if (!lut)
return false;
2012-10-26 19:09:30 +00:00
2016-01-07 05:38:28 +00:00
d3d_set_texture(chain->dev, 0, lut);
d3d_set_sampler_address_u(chain->dev, 0, D3DTADDRESS_BORDER);
d3d_set_sampler_address_v(chain->dev, 0, D3DTADDRESS_BORDER);
d3d_set_texture(chain->dev, 0, NULL);
2012-10-26 19:09:30 +00:00
lut_info_vector_list_append(chain->luts, info);
return true;
2012-10-26 19:09:30 +00:00
}
2017-09-05 02:08:44 +00:00
static void d3d9_cg_renderchain_add_state_tracker(
void *data, void *tracker_data)
2012-10-26 19:09:30 +00:00
{
2015-04-04 23:05:51 +00:00
state_tracker_t *tracker = (state_tracker_t*)tracker_data;
2015-04-05 22:29:53 +00:00
cg_renderchain_t *chain = (cg_renderchain_t*)data;
2016-01-08 19:28:18 +00:00
if (chain->state_tracker)
state_tracker_free(chain->state_tracker);
chain->state_tracker = tracker;
2012-10-26 19:09:30 +00:00
}
2017-09-05 02:08:44 +00:00
static void d3d9_cg_renderchain_start_render(cg_renderchain_t *chain)
2012-10-26 19:09:30 +00:00
{
chain->passes->data[0].tex = chain->prev.tex[chain->prev.ptr];
chain->passes->data[0].vertex_buf = chain->prev.vertex_buf[chain->prev.ptr];
chain->passes->data[0].last_width = chain->prev.last_width[chain->prev.ptr];
chain->passes->data[0].last_height = chain->prev.last_height[chain->prev.ptr];
2012-10-26 19:09:30 +00:00
}
2017-09-05 02:08:44 +00:00
static void d3d9_cg_renderchain_end_render(cg_renderchain_t *chain)
2012-10-26 19:09:30 +00:00
{
chain->prev.last_width[chain->prev.ptr] = chain->passes->data[0].last_width;
chain->prev.last_height[chain->prev.ptr] = chain->passes->data[0].last_height;
chain->prev.ptr = (chain->prev.ptr + 1) & TEXTURESMASK;
2012-10-26 19:09:30 +00:00
}
static void d3d9_cg_renderchain_set_shader_mvp(
cg_renderchain_t *chain, CGprogram vPrg,
2015-04-05 16:54:14 +00:00
unsigned vp_width, unsigned vp_height,
unsigned rotation)
{
D3DMATRIX proj, ortho, rot, matrix;
2015-04-05 16:54:14 +00:00
2017-10-02 17:01:23 +00:00
d3d_matrix_ortho_off_center_lh(&ortho, 0, vp_width, 0, vp_height, 0, 1);
d3d_matrix_identity(&rot);
d3d_matrix_rotation_z(&rot, rotation * (D3D_PI / 2.0));
2015-04-05 16:54:14 +00:00
2017-10-02 17:01:23 +00:00
d3d_matrix_multiply(&proj, &ortho, &rot);
d3d_matrix_transpose(&matrix, &proj);
CGparameter cgpModelViewProj = cgGetNamedParameter(vPrg, "modelViewProj");
if (cgpModelViewProj)
cgD3D9SetUniformMatrix(cgpModelViewProj, &matrix);
}
static void d3d9_cg_renderchain_set_mvp(
void *chain_data,
void *data,
unsigned vp_width, unsigned vp_height,
unsigned rotation)
{
cg_renderchain_t *chain = (cg_renderchain_t*)chain_data;
2015-04-05 16:54:14 +00:00
d3d9_cg_renderchain_set_shader_mvp(chain, chain->vStock, vp_width, vp_height, rotation);
2015-04-05 16:54:14 +00:00
}
2017-09-05 02:08:44 +00:00
static void cg_d3d9_renderchain_set_vertices(
cg_renderchain_t *chain,
2017-10-03 00:45:06 +00:00
struct Pass *pass,
unsigned width, unsigned height,
unsigned out_width, unsigned out_height,
unsigned vp_width, unsigned vp_height,
unsigned rotation)
{
2017-10-03 00:45:06 +00:00
const struct LinkInfo *info = (const struct LinkInfo*)&pass->info;
if (pass->last_width != width || pass->last_height != height)
{
2017-10-03 00:45:06 +00:00
struct CGVertex vert[4];
unsigned i;
void *verts = NULL;
2017-10-03 00:45:06 +00:00
float _u = (float)(width) / info->tex_w;
float _v = (float)(height) / info->tex_h;
pass->last_width = width;
pass->last_height = height;
vert[0].x = 0.0f;
vert[0].y = out_height;
2016-10-08 06:39:30 +00:00
vert[0].z = 0.5f;
vert[0].u = 0.0f;
vert[0].v = 0.0f;
vert[0].lut_u = 0.0f;
vert[0].lut_v = 0.0f;
2016-10-08 06:39:30 +00:00
vert[0].r = 1.0f;
vert[0].g = 1.0f;
vert[0].b = 1.0f;
vert[0].a = 1.0f;
vert[1].x = out_width;
vert[1].y = out_height;
vert[1].z = 0.5f;
vert[1].u = _u;
vert[1].v = 0.0f;
vert[1].lut_u = 1.0f;
vert[1].lut_v = 0.0f;
2016-10-08 06:39:30 +00:00
vert[1].r = 1.0f;
vert[1].g = 1.0f;
vert[1].b = 1.0f;
vert[1].a = 1.0f;
vert[2].x = 0.0f;
vert[2].y = 0.0f;
vert[2].z = 0.5f;
vert[2].u = 0.0f;
vert[2].v = _v;
vert[2].lut_u = 0.0f;
vert[2].lut_v = 1.0f;
2016-10-08 06:39:30 +00:00
vert[2].r = 1.0f;
vert[2].g = 1.0f;
vert[2].b = 1.0f;
vert[2].a = 1.0f;
2016-10-08 06:39:30 +00:00
vert[3].x = out_width;
vert[3].y = 0.0f;
vert[3].z = 0.5f;
vert[3].u = _u;
vert[3].v = _v;
vert[3].lut_u = 1.0f;
vert[3].lut_v = 1.0f;
vert[3].r = 1.0f;
vert[3].g = 1.0f;
vert[3].b = 1.0f;
vert[3].a = 1.0f;
/* Align texels and vertices.
2016-10-08 06:39:30 +00:00
*
* Fixes infamous 'half-texel offset' issue of D3D9
* http://msdn.microsoft.com/en-us/library/bb219690%28VS.85%29.aspx.
*/
for (i = 0; i < 4; i++)
{
vert[i].x -= 0.5f;
vert[i].y += 0.5f;
}
verts = d3d_vertex_buffer_lock(pass->vertex_buf);
memcpy(verts, vert, sizeof(vert));
d3d_vertex_buffer_unlock(pass->vertex_buf);
}
2017-09-05 02:08:44 +00:00
if (chain)
{
d3d9_cg_renderchain_set_shader_mvp(
2017-09-05 02:08:44 +00:00
chain, pass->vPrg, vp_width, vp_height, rotation);
if (pass)
d3d9_cg_renderchain_set_shader_params(chain, pass,
width, height,
info->tex_w, info->tex_h,
vp_width, vp_height);
}
}
2017-09-05 02:08:44 +00:00
static void cg_d3d9_renderchain_set_viewport(
cg_renderchain_t *chain,
D3DVIEWPORT *vp)
2015-04-05 21:19:10 +00:00
{
2016-01-07 08:22:04 +00:00
d3d_set_viewports(chain->dev, vp);
2015-04-05 21:19:10 +00:00
}
2017-09-05 02:08:44 +00:00
static void cg_d3d9_renderchain_blit_to_texture(
2015-11-11 01:01:00 +00:00
cg_renderchain_t *chain,
const void *frame,
unsigned width, unsigned height,
unsigned pitch)
{
D3DLOCKED_RECT d3dlr;
2017-10-03 00:45:06 +00:00
struct Pass *first = (struct Pass*)&chain->passes->data[0];
if (first->last_width != width || first->last_height != height)
{
d3d_lock_rectangle(first->tex, 0, &d3dlr,
2015-11-09 14:34:16 +00:00
NULL, first->info.tex_h, D3DLOCK_NOSYSLOCK);
d3d_lock_rectangle_clear(first->tex, 0, &d3dlr,
NULL, first->info.tex_h, D3DLOCK_NOSYSLOCK);
}
2015-04-06 22:46:46 +00:00
d3d_texture_blit(chain->pixel_size, first->tex,
&d3dlr, frame, width, height, pitch);
}
2017-09-05 02:08:44 +00:00
static void cg_d3d9_renderchain_unbind_all(cg_renderchain_t *chain)
2015-04-05 22:08:15 +00:00
{
unsigned i;
/* Have to be a bit anal about it.
* Render targets hate it when they have filters apparently.
*/
for (i = 0; i < chain->bound_tex->count; i++)
2015-04-05 22:08:15 +00:00
{
2016-01-07 05:38:28 +00:00
d3d_set_sampler_minfilter(chain->dev,
chain->bound_tex->data[i], D3DTEXF_POINT);
2016-01-07 05:38:28 +00:00
d3d_set_sampler_magfilter(chain->dev,
chain->bound_tex->data[i], D3DTEXF_POINT);
d3d_set_texture(chain->dev, chain->bound_tex->data[i], NULL);
2015-04-05 22:08:15 +00:00
}
for (i = 0; i < chain->bound_vert->count; i++)
d3d_set_stream_source(chain->dev, chain->bound_vert->data[i], 0, 0, 0);
2015-04-05 22:08:15 +00:00
if (chain->bound_tex)
{
unsigned_vector_list_free(chain->bound_tex);
chain->bound_tex = unsigned_vector_list_new();
}
if (chain->bound_vert)
{
unsigned_vector_list_free(chain->bound_vert);
chain->bound_vert = unsigned_vector_list_new();
}
2015-04-05 22:08:15 +00:00
}
2017-09-05 02:08:44 +00:00
static void cg_d3d9_renderchain_render_pass(
cg_renderchain_t *chain,
2017-10-03 00:45:06 +00:00
struct Pass *pass,
unsigned pass_index)
{
2016-01-08 19:28:18 +00:00
unsigned i, index;
2017-09-19 12:54:55 +00:00
cgD3D9BindProgram(pass->fPrg);
cgD3D9BindProgram(pass->vPrg);
2016-01-07 05:38:28 +00:00
d3d_set_texture(chain->dev, 0, pass->tex);
d3d_set_sampler_minfilter(chain->dev, 0,
d3d_translate_filter(pass->info.pass->filter));
2016-01-07 05:38:28 +00:00
d3d_set_sampler_magfilter(chain->dev, 0,
d3d_translate_filter(pass->info.pass->filter));
2016-01-07 05:38:28 +00:00
d3d_set_vertex_declaration(chain->dev, pass->vertex_decl);
for (i = 0; i < 4; i++)
2016-01-07 05:38:28 +00:00
d3d_set_stream_source(chain->dev, i,
2017-10-03 00:45:06 +00:00
pass->vertex_buf, 0,
sizeof(struct CGVertex));
2016-01-09 02:19:03 +00:00
/* Set orig texture. */
2017-09-05 02:08:44 +00:00
d3d9_cg_renderchain_bind_orig(chain, pass);
2016-01-08 19:28:18 +00:00
2016-01-09 02:19:03 +00:00
/* Set prev textures. */
2017-09-05 02:08:44 +00:00
d3d9_cg_renderchain_bind_prev(chain, (const void*)pass);
2016-01-08 19:28:18 +00:00
/* Set lookup textures */
for (i = 0; i < chain->luts->count; i++)
2016-01-08 19:28:18 +00:00
{
CGparameter vparam;
CGparameter fparam = cgGetNamedParameter(
pass->fPrg, chain->luts->data[i].id);
2016-01-08 19:28:18 +00:00
int bound_index = -1;
if (fparam)
{
index = cgGetParameterResourceIndex(fparam);
bound_index = index;
2017-09-05 02:08:44 +00:00
d3d9_cg_renderchain_add_lut_internal(chain, index, i);
2016-01-08 19:28:18 +00:00
}
vparam = cgGetNamedParameter(pass->vPrg, chain->luts->data[i].id);
2016-01-08 19:28:18 +00:00
if (vparam)
{
index = cgGetParameterResourceIndex(vparam);
if (index != (unsigned)bound_index)
2017-09-05 02:08:44 +00:00
d3d9_cg_renderchain_add_lut_internal(chain, index, i);
2016-01-08 19:28:18 +00:00
}
}
2017-09-05 02:08:44 +00:00
d3d9_cg_renderchain_bind_pass(chain, pass, pass_index);
2016-01-09 02:19:03 +00:00
2016-01-08 19:28:18 +00:00
/* Set state parameters. */
if (chain->state_tracker)
{
/* Only query uniforms in first pass. */
2016-01-08 19:36:20 +00:00
static struct state_tracker_uniform tracker_info[GFX_MAX_VARIABLES];
2016-01-08 19:28:18 +00:00
static unsigned cnt = 0;
if (pass_index == 1)
cnt = state_tracker_get_uniform(chain->state_tracker, tracker_info,
2016-01-08 19:36:20 +00:00
GFX_MAX_VARIABLES, chain->frame_count);
2016-01-08 19:28:18 +00:00
for (i = 0; i < cnt; i++)
{
CGparameter param_f = cgGetNamedParameter(
pass->fPrg, tracker_info[i].id);
CGparameter param_v = cgGetNamedParameter(
pass->vPrg, tracker_info[i].id);
2017-09-05 02:08:44 +00:00
d3d9_cg_set_param_1f(param_f, &tracker_info[i].value);
d3d9_cg_set_param_1f(param_v, &tracker_info[i].value);
2016-01-08 19:28:18 +00:00
}
}
2016-01-07 05:38:28 +00:00
d3d_draw_primitive(chain->dev, D3DPT_TRIANGLESTRIP, 0, 2);
/* So we don't render with linear filter into render targets,
* which apparently looked odd (too blurry). */
2016-01-07 05:38:28 +00:00
d3d_set_sampler_minfilter(chain->dev, 0, D3DTEXF_POINT);
d3d_set_sampler_magfilter(chain->dev, 0, D3DTEXF_POINT);
2017-09-05 02:08:44 +00:00
cg_d3d9_renderchain_unbind_all(chain);
}
2017-09-05 02:08:44 +00:00
static bool d3d9_cg_renderchain_render(
2015-11-11 01:05:20 +00:00
void *data,
2015-11-11 01:04:42 +00:00
const void *frame_data,
2015-11-11 01:01:00 +00:00
unsigned width, unsigned height,
unsigned pitch, unsigned rotation)
2012-10-26 19:09:30 +00:00
{
2015-11-11 01:42:15 +00:00
LPDIRECT3DDEVICE d3dr;
2015-01-17 05:30:39 +00:00
LPDIRECT3DSURFACE back_buffer, target;
unsigned i, current_width, current_height, out_width = 0, out_height = 0;
2017-10-03 00:45:06 +00:00
struct Pass *last_pass = NULL;
2015-11-11 01:42:15 +00:00
d3d_video_t *d3d = (d3d_video_t*)data;
cg_renderchain_t *chain = d3d ? (cg_renderchain_t*)d3d->renderchain_data : NULL;
if (chain)
2017-09-05 02:08:44 +00:00
{
2015-11-11 01:42:15 +00:00
d3dr = (LPDIRECT3DDEVICE)chain->dev;
2017-09-05 02:08:44 +00:00
d3d9_cg_renderchain_start_render(chain);
}
2015-04-04 23:15:54 +00:00
current_width = width;
current_height = height;
d3d9_cg_renderchain_convert_geometry(chain, &chain->passes->data[0].info,
2015-04-04 22:44:54 +00:00
&out_width, &out_height,
current_width, current_height, chain->final_viewport);
2015-01-17 05:30:39 +00:00
2017-09-05 02:08:44 +00:00
cg_d3d9_renderchain_blit_to_texture(chain,
frame_data, width, height, pitch);
2012-10-26 19:09:30 +00:00
2015-01-17 05:30:39 +00:00
/* Grab back buffer. */
2017-10-01 20:38:30 +00:00
d3d_device_get_render_target(d3dr, 0, (void**)&back_buffer);
2012-10-26 19:09:30 +00:00
2015-01-17 05:30:39 +00:00
/* In-between render target passes. */
for (i = 0; i < chain->passes->count - 1; i++)
2012-10-26 19:09:30 +00:00
{
2017-10-03 00:45:06 +00:00
D3DVIEWPORT viewport = {0};
struct Pass *from_pass = (struct Pass*)&chain->passes->data[i];
struct Pass *to_pass = (struct Pass*)&chain->passes->data[i + 1];
2012-10-26 19:09:30 +00:00
2017-10-03 00:45:06 +00:00
d3d_texture_get_surface_level(to_pass->tex, 0, (void**)&target);
2017-10-01 21:19:44 +00:00
d3d_device_set_render_target(d3dr, 0, (void*)target);
2012-10-26 19:09:30 +00:00
2017-09-05 02:08:44 +00:00
d3d9_cg_renderchain_convert_geometry(chain, &from_pass->info,
2015-04-04 22:44:54 +00:00
&out_width, &out_height,
current_width, current_height, chain->final_viewport);
2012-10-26 19:09:30 +00:00
/* Clear out whole FBO. */
2015-04-04 23:15:54 +00:00
viewport.Width = to_pass->info.tex_w;
viewport.Height = to_pass->info.tex_h;
2015-04-04 23:15:54 +00:00
viewport.MinZ = 0.0f;
viewport.MaxZ = 1.0f;
2015-04-05 13:53:06 +00:00
2016-01-07 08:22:04 +00:00
d3d_set_viewports(d3dr, &viewport);
2014-09-13 18:09:55 +00:00
d3d_clear(d3dr, 0, 0, D3DCLEAR_TARGET, 0, 1, 0);
2017-09-05 01:42:39 +00:00
2015-04-04 23:15:54 +00:00
viewport.Width = out_width;
2013-04-21 13:39:14 +00:00
viewport.Height = out_height;
2012-10-26 19:09:30 +00:00
2017-09-05 02:08:44 +00:00
if (chain)
cg_d3d9_renderchain_set_viewport(chain, &viewport);
cg_d3d9_renderchain_set_vertices(chain, from_pass,
2012-10-26 19:09:30 +00:00
current_width, current_height,
out_width, out_height,
out_width, out_height, 0);
2017-09-05 02:08:44 +00:00
if (chain)
cg_d3d9_renderchain_render_pass(chain, from_pass, i + 1);
2012-10-26 19:09:30 +00:00
current_width = out_width;
current_height = out_height;
2017-10-01 20:28:48 +00:00
d3d_surface_free(target);
2012-10-26 19:09:30 +00:00
}
/* Final pass */
2017-10-01 21:19:44 +00:00
d3d_device_set_render_target(d3dr, 0, (void*)back_buffer);
2015-04-04 23:48:44 +00:00
2017-10-03 00:45:06 +00:00
last_pass = (struct Pass*)&chain->passes->
data[chain->passes->count - 1];
2012-10-26 19:09:30 +00:00
2017-09-05 02:08:44 +00:00
d3d9_cg_renderchain_convert_geometry(chain, &last_pass->info,
2015-04-04 22:44:54 +00:00
&out_width, &out_height,
current_width, current_height, chain->final_viewport);
2017-09-05 02:08:44 +00:00
if (chain)
cg_d3d9_renderchain_set_viewport(chain, chain->final_viewport);
cg_d3d9_renderchain_set_vertices(chain, last_pass,
2017-09-05 01:42:39 +00:00
current_width, current_height,
out_width, out_height,
chain->final_viewport->Width, chain->final_viewport->Height,
rotation);
2017-09-05 02:08:44 +00:00
if (chain)
cg_d3d9_renderchain_render_pass(chain,
last_pass, chain->passes->count);
2012-10-26 19:09:30 +00:00
chain->frame_count++;
2012-10-26 19:09:30 +00:00
2017-10-01 20:28:48 +00:00
d3d_surface_free(back_buffer);
2012-10-26 19:09:30 +00:00
2017-09-05 02:08:44 +00:00
if (chain)
{
d3d9_cg_renderchain_end_render(chain);
2017-09-19 12:54:55 +00:00
cgD3D9BindProgram(chain->fStock);
cgD3D9BindProgram(chain->vStock);
d3d9_cg_renderchain_set_shader_mvp(
2017-09-05 02:08:44 +00:00
chain, chain->vStock, chain->final_viewport->Width,
chain->final_viewport->Height, 0);
}
2015-01-17 05:30:39 +00:00
2012-10-26 19:09:30 +00:00
return true;
}
2017-09-05 02:08:44 +00:00
static void d3d9_cg_renderchain_set_font_rect(
void *data,
const void *font_data)
{
2015-11-11 17:20:07 +00:00
settings_t *settings = config_get_ptr();
d3d_video_t *d3d = (d3d_video_t*)data;
2017-04-28 12:57:55 +00:00
float pos_x = settings->floats.video_msg_pos_x;
float pos_y = settings->floats.video_msg_pos_y;
float font_size = settings->floats.video_font_size;
2015-11-11 17:20:07 +00:00
const struct font_params *params = (const struct font_params*)font_data;
if (params)
{
pos_x = params->x;
pos_y = params->y;
font_size *= params->scale;
}
if (!d3d)
return;
d3d->font_rect.left = d3d->video_info.width * pos_x;
d3d->font_rect.right = d3d->video_info.width;
d3d->font_rect.top = (1.0f - pos_y) * d3d->video_info.height - font_size;
d3d->font_rect.bottom = d3d->video_info.height;
d3d->font_rect_shifted = d3d->font_rect;
d3d->font_rect_shifted.left -= 2;
d3d->font_rect_shifted.right -= 2;
d3d->font_rect_shifted.top += 2;
d3d->font_rect_shifted.bottom += 2;
}
2017-09-05 02:08:44 +00:00
static bool d3d9_cg_renderchain_read_viewport(
void *data, uint8_t *buffer, bool is_idle)
{
unsigned width, height;
D3DLOCKED_RECT rect;
LPDIRECT3DSURFACE target = NULL;
LPDIRECT3DSURFACE dest = NULL;
bool ret = true;
2016-01-07 05:38:28 +00:00
d3d_video_t *d3d = (d3d_video_t*)data;
LPDIRECT3DDEVICE d3dr = (LPDIRECT3DDEVICE)d3d->dev;
video_driver_get_size(&width, &height);
(void)d3d;
(void)data;
(void)buffer;
if (
!d3d_device_get_render_target(d3dr, 0, (void**)&target) ||
!d3d_device_create_offscreen_plain_surface(d3dr, width, height,
D3DFMT_X8R8G8B8, D3DPOOL_SYSTEMMEM, (void**)&dest, NULL) ||
!d3d_device_get_render_target_data(d3dr, (void*)target, (void*)dest)
)
{
ret = false;
goto end;
}
2017-10-01 22:45:33 +00:00
if (d3d_surface_lock_rect(dest, (void*)&rect))
{
unsigned x, y;
unsigned pitchpix = rect.Pitch / 4;
const uint32_t *pixels = (const uint32_t*)rect.pBits;
pixels += d3d->final_viewport.X;
pixels += (d3d->final_viewport.Height - 1) * pitchpix;
pixels -= d3d->final_viewport.Y * pitchpix;
for (y = 0; y < d3d->final_viewport.Height; y++, pixels -= pitchpix)
{
for (x = 0; x < d3d->final_viewport.Width; x++)
{
*buffer++ = (pixels[x] >> 0) & 0xff;
*buffer++ = (pixels[x] >> 8) & 0xff;
*buffer++ = (pixels[x] >> 16) & 0xff;
}
}
2017-10-01 22:45:33 +00:00
d3d_surface_unlock_rect((void*)dest);
}
else
ret = false;
end:
if (target)
2017-10-01 20:28:48 +00:00
d3d_surface_free(target);
if (dest)
2017-10-01 20:28:48 +00:00
d3d_surface_free(dest);
return ret;
}
2017-09-05 02:08:44 +00:00
static void d3d9_cg_renderchain_viewport_info(
void *data, struct video_viewport *vp)
{
unsigned width, height;
d3d_video_t *d3d = (d3d_video_t*)data;
if (!d3d || !vp)
return;
video_driver_get_size(&width, &height);
vp->x = d3d->final_viewport.X;
vp->y = d3d->final_viewport.Y;
vp->width = d3d->final_viewport.Width;
vp->height = d3d->final_viewport.Height;
vp->full_width = width;
vp->full_height = height;
}
d3d_renderchain_driver_t cg_d3d9_renderchain = {
2017-11-15 15:51:29 +00:00
d3d9_cg_renderchain_set_mvp,
2017-09-05 02:08:44 +00:00
d3d9_cg_renderchain_free,
d3d9_cg_renderchain_new,
2015-04-06 21:26:14 +00:00
NULL,
2017-09-05 02:08:44 +00:00
d3d9_cg_renderchain_init,
d3d9_cg_renderchain_set_final_viewport,
d3d9_cg_renderchain_add_pass,
d3d9_cg_renderchain_add_lut,
d3d9_cg_renderchain_add_state_tracker,
d3d9_cg_renderchain_render,
d3d9_cg_renderchain_convert_geometry,
d3d9_cg_renderchain_set_font_rect,
d3d9_cg_renderchain_read_viewport,
d3d9_cg_renderchain_viewport_info,
2015-04-06 13:52:41 +00:00
"cg_d3d9",
};