RetroArch/gfx/shader_hlsl.c

442 lines
12 KiB
C
Raw Normal View History

2012-04-21 21:13:50 +00:00
/* RetroArch - A frontend for libretro.
2014-01-01 00:50:59 +00:00
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
* Copyright (C) 2011-2014 - Daniel De Matteis
*
2012-04-21 21:13:50 +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.
*
2012-04-21 21:13:50 +00:00
* 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.
*
2012-04-21 21:31:57 +00:00
* You should have received a copy of the GNU General Public License along with RetroArch.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include "shader_hlsl.h"
2013-04-12 18:06:08 +00:00
#include "shader_parse.h"
#ifdef _XBOX
#include <xtl.h>
#endif
2013-04-12 13:14:14 +00:00
static const char *stock_hlsl_program =
"void main_vertex\n"
"(\n"
" float4 position : POSITION,\n"
" float4 color : COLOR,\n"
"\n"
" uniform float4x4 modelViewProj,\n"
"\n"
" float4 texCoord : TEXCOORD0,\n"
" out float4 oPosition : POSITION,\n"
" out float4 oColor : COLOR,\n"
" out float2 otexCoord : TEXCOORD\n"
")\n"
"{\n"
" oPosition = mul(modelViewProj, position);\n"
" oColor = color;\n"
" otexCoord = texCoord;\n"
"}\n"
"\n"
"struct output\n"
"{\n"
" float4 color: COLOR;\n"
"};\n"
"\n"
"struct input\n"
"{\n"
" float2 video_size;\n"
" float2 texture_size;\n"
" float2 output_size;\n"
" float frame_count;\n"
" float frame_direction;\n"
" float frame_rotation;\n"
"};\n"
"\n"
"output main_fragment(float2 texCoord : TEXCOORD0,\n"
"uniform sampler2D decal : TEXUNIT0, uniform input IN)\n"
"{\n"
" output OUT;\n"
" OUT.color = tex2D(decal, texCoord);\n"
" return OUT;\n"
"}\n";
2013-04-12 13:14:14 +00:00
struct hlsl_program
{
LPDIRECT3DVERTEXSHADER vprg;
LPDIRECT3DPIXELSHADER fprg;
D3DXHANDLE vid_size_f;
D3DXHANDLE tex_size_f;
D3DXHANDLE out_size_f;
D3DXHANDLE frame_cnt_f;
D3DXHANDLE frame_dir_f;
D3DXHANDLE vid_size_v;
D3DXHANDLE tex_size_v;
D3DXHANDLE out_size_v;
D3DXHANDLE frame_cnt_v;
D3DXHANDLE frame_dir_v;
D3DXHANDLE mvp;
LPD3DXCONSTANTTABLE v_ctable;
LPD3DXCONSTANTTABLE f_ctable;
XMMATRIX mvp_val; /* TODO: Move to D3DXMATRIX here */
};
static LPDIRECT3DDEVICE d3d_device_ptr;
2012-04-21 21:25:32 +00:00
static struct hlsl_program prg[RARCH_HLSL_MAX_SHADERS] = {0};
static bool hlsl_active = false;
static unsigned active_index = 0;
2013-04-12 18:06:08 +00:00
static struct gfx_shader *cg_shader;
void hlsl_set_proj_matrix(XMMATRIX rotation_value)
{
if (hlsl_active)
prg[active_index].mvp_val = rotation_value;
}
2012-04-15 18:02:51 +00:00
#define set_param_2f(param, xy, constanttable) \
2012-04-16 16:11:20 +00:00
if (param) constanttable->SetFloatArray(d3d_device_ptr, param, xy, 2)
2012-04-16 02:21:49 +00:00
#define set_param_1f(param, x, constanttable) \
if (param) constanttable->SetFloat(d3d_device_ptr, param, x)
2012-04-15 18:02:51 +00:00
static void hlsl_set_params(unsigned width, unsigned height,
2012-04-15 18:02:51 +00:00
unsigned tex_width, unsigned tex_height,
2012-04-16 02:21:49 +00:00
unsigned out_width, unsigned out_height,
unsigned frame_counter,
const struct gl_tex_info *info,
const struct gl_tex_info *prev_info,
const struct gl_tex_info *fbo_info, unsigned fbo_info_cnt)
{
if (!hlsl_active)
return;
2012-04-16 16:11:20 +00:00
const float ori_size[2] = { (float)width, (float)height };
const float tex_size[2] = { (float)tex_width, (float)tex_height };
const float out_size[2] = { (float)out_width, (float)out_height };
float frame_cnt = frame_counter;
2012-04-18 00:57:46 +00:00
prg[active_index].f_ctable->SetDefaults(d3d_device_ptr);
prg[active_index].v_ctable->SetDefaults(d3d_device_ptr);
2012-04-15 18:02:51 +00:00
set_param_2f(prg[active_index].vid_size_f, ori_size, prg[active_index].f_ctable);
set_param_2f(prg[active_index].tex_size_f, tex_size, prg[active_index].f_ctable);
set_param_2f(prg[active_index].out_size_f, out_size, prg[active_index].f_ctable);
2012-04-18 00:57:46 +00:00
set_param_1f(prg[active_index].frame_cnt_f, frame_cnt, prg[active_index].f_ctable);
2012-04-17 19:27:47 +00:00
set_param_1f(prg[active_index].frame_dir_f, g_extern.frame_is_reverse ? -1.0 : 1.0,prg[active_index].f_ctable);
2012-04-15 18:02:51 +00:00
set_param_2f(prg[active_index].vid_size_v, ori_size, prg[active_index].v_ctable);
set_param_2f(prg[active_index].tex_size_v, tex_size, prg[active_index].v_ctable);
set_param_2f(prg[active_index].out_size_v, out_size, prg[active_index].v_ctable);
2012-04-18 00:57:46 +00:00
set_param_1f(prg[active_index].frame_cnt_v, frame_cnt, prg[active_index].v_ctable);
2012-04-17 19:27:47 +00:00
set_param_1f(prg[active_index].frame_dir_v, g_extern.frame_is_reverse ? -1.0 : 1.0,prg[active_index].v_ctable);
2012-04-15 18:02:51 +00:00
2013-04-12 18:06:08 +00:00
/* TODO - set lookup textures/FBO textures/state parameters/etc */
}
static bool load_program(unsigned index, const char *prog, bool path_is_file)
{
HRESULT ret, ret_fp, ret_vp;
ID3DXBuffer *listing_f = NULL;
ID3DXBuffer *listing_v = NULL;
ID3DXBuffer *code_f = NULL;
ID3DXBuffer *code_v = NULL;
if (path_is_file)
{
2012-04-16 16:11:20 +00:00
ret_fp = D3DXCompileShaderFromFile(prog, NULL, NULL,
2012-04-17 19:27:47 +00:00
"main_fragment", "ps_3_0", 0, &code_f, &listing_f, &prg[index].f_ctable);
2012-04-16 16:11:20 +00:00
ret_vp = D3DXCompileShaderFromFile(prog, NULL, NULL,
2012-04-17 19:27:47 +00:00
"main_vertex", "vs_3_0", 0, &code_v, &listing_v, &prg[index].v_ctable);
}
else
{
2012-04-14 20:19:39 +00:00
/* TODO - crashes currently - to do with 'end of line' of stock shader */
2013-04-12 13:14:14 +00:00
ret_fp = D3DXCompileShader(prog, strlen(prog), NULL, NULL,
2012-04-17 19:27:47 +00:00
"main_fragment", "ps_3_0", 0, &code_f, &listing_f, &prg[index].f_ctable );
2013-04-12 13:14:14 +00:00
ret_vp = D3DXCompileShader(prog, strlen(prog), NULL, NULL,
2012-04-17 19:27:47 +00:00
"main_vertex", "vs_3_0", 0, &code_v, &listing_v, &prg[index].v_ctable );
}
if (ret_fp < 0 || ret_vp < 0 || listing_v || listing_f)
{
2013-04-12 18:06:08 +00:00
RARCH_ERR("Cg/HLSL error:\n");
if(listing_f)
2012-04-21 21:25:32 +00:00
RARCH_ERR("Fragment:\n%s\n", (char*)listing_f->GetBufferPointer());
if(listing_v)
2012-04-21 21:25:32 +00:00
RARCH_ERR("Vertex:\n%s\n", (char*)listing_v->GetBufferPointer());
ret = false;
goto end;
}
d3d_device_ptr->CreatePixelShader((const DWORD*)code_f->GetBufferPointer(), &prg[index].fprg);
d3d_device_ptr->CreateVertexShader((const DWORD*)code_v->GetBufferPointer(), &prg[index].vprg);
code_f->Release();
code_v->Release();
end:
2012-04-16 16:11:20 +00:00
if (listing_f)
listing_f->Release();
2012-04-16 16:11:20 +00:00
if (listing_v)
listing_v->Release();
return ret;
}
2013-04-12 18:06:08 +00:00
static bool load_stock(void)
{
2013-04-12 18:06:08 +00:00
if (!load_program(0, stock_hlsl_program, false))
{
2012-04-21 21:25:32 +00:00
RARCH_ERR("Failed to compile passthrough shader, is something wrong with your environment?\n");
return false;
}
return true;
}
static void set_program_attributes(unsigned i)
{
prg[i].vid_size_f = prg[i].f_ctable->GetConstantByName(NULL, "$IN.video_size");
prg[i].tex_size_f = prg[i].f_ctable->GetConstantByName(NULL, "$IN.texture_size");
prg[i].out_size_f = prg[i].f_ctable->GetConstantByName(NULL, "$IN.output_size");
prg[i].frame_cnt_f = prg[i].f_ctable->GetConstantByName(NULL, "$IN.frame_count");
prg[i].frame_dir_f = prg[i].f_ctable->GetConstantByName(NULL, "$IN.frame_direction");
prg[i].vid_size_v = prg[i].v_ctable->GetConstantByName(NULL, "$IN.video_size");
prg[i].tex_size_v = prg[i].v_ctable->GetConstantByName(NULL, "$IN.texture_size");
prg[i].out_size_v = prg[i].v_ctable->GetConstantByName(NULL, "$IN.output_size");
prg[i].frame_cnt_v = prg[i].v_ctable->GetConstantByName(NULL, "$IN.frame_count");
prg[i].frame_dir_v = prg[i].v_ctable->GetConstantByName(NULL, "$IN.frame_direction");
prg[i].mvp = prg[i].v_ctable->GetConstantByName(NULL, "$modelViewProj");
prg[i].mvp_val = XMMatrixIdentity();
}
2013-04-12 18:06:08 +00:00
static bool load_shader(const char *cgp_path, unsigned i)
{
2013-04-12 18:06:08 +00:00
char path_buf[PATH_MAX];
fill_pathname_resolve_relative(path_buf, cgp_path,
cg_shader->pass[i].source.cg, sizeof(path_buf));
2013-04-12 18:06:08 +00:00
RARCH_LOG("Loading Cg/HLSL shader: \"%s\".\n", path_buf);
2013-04-12 18:06:08 +00:00
if (!load_program(i + 1, path_buf, true))
return false;
2013-04-12 18:06:08 +00:00
return true;
}
static bool load_plain(const char *path)
{
2013-04-12 18:06:08 +00:00
if (!load_stock())
return false;
cg_shader = (struct gfx_shader*)calloc(1, sizeof(*cg_shader));
if (!cg_shader)
return false;
2013-04-12 18:06:08 +00:00
cg_shader->passes = 1;
2013-04-12 18:06:08 +00:00
if (path && path[0] != '\0')
2013-01-22 13:00:34 +00:00
{
2013-04-12 18:06:08 +00:00
RARCH_LOG("Loading Cg/HLSL file: %s\n", path);
strlcpy(cg_shader->pass[0].source.cg, path, sizeof(cg_shader->pass[0].source.cg));
if (!load_program(1, path, true))
return false;
}
else
{
RARCH_LOG("Loading stock Cg/HLSL file.\n");
prg[1] = prg[0];
2013-01-22 13:00:34 +00:00
}
return true;
}
static void hlsl_deinit_progs(void)
{
2012-05-06 21:40:29 +00:00
for (unsigned i = 1; i < RARCH_HLSL_MAX_SHADERS; i++)
{
2012-05-06 21:40:29 +00:00
if (prg[i].fprg && prg[i].fprg != prg[0].fprg)
prg[i].fprg->Release();
2012-05-06 21:40:29 +00:00
if (prg[i].vprg && prg[i].vprg != prg[0].vprg)
prg[i].vprg->Release();
2012-05-06 21:40:29 +00:00
2012-05-07 03:34:41 +00:00
prg[i].fprg = NULL;
2013-04-12 13:14:14 +00:00
prg[i].vprg = NULL;
}
2012-05-06 21:40:29 +00:00
if (prg[0].fprg)
prg[0].fprg->Release();
if (prg[0].vprg)
prg[0].vprg->Release();
2013-01-22 13:00:34 +00:00
2012-05-07 03:34:41 +00:00
prg[0].fprg = NULL;
prg[0].vprg = NULL;
}
static void hlsl_deinit_state(void)
{
hlsl_active = false;
2012-05-06 21:40:29 +00:00
hlsl_deinit_progs();
memset(prg, 0, sizeof(prg));
d3d_device_ptr = NULL;
2013-04-12 18:06:08 +00:00
free(cg_shader);
cg_shader = NULL;
}
static bool load_preset(const char *path)
{
2013-04-12 18:06:08 +00:00
if (!load_stock())
return false;
RARCH_LOG("Loading Cg meta-shader: %s\n", path);
config_file_t *conf = config_file_new(path);
if (!conf)
{
RARCH_ERR("Failed to load preset.\n");
return false;
}
if (!cg_shader)
cg_shader = (struct gfx_shader*)calloc(1, sizeof(*cg_shader));
if (!cg_shader)
return false;
if (!gfx_shader_read_conf_cgp(conf, cg_shader))
{
RARCH_ERR("Failed to parse CGP file.\n");
config_file_free(conf);
return false;
}
config_file_free(conf);
if (cg_shader->passes > RARCH_HLSL_MAX_SHADERS - 3)
{
RARCH_WARN("Too many shaders ... Capping shader amount to %d.\n", RARCH_HLSL_MAX_SHADERS - 3);
cg_shader->passes = RARCH_HLSL_MAX_SHADERS - 3;
}
for (unsigned i = 0; i < cg_shader->passes; i++)
{
if (!load_shader(path, i))
{
RARCH_ERR("Failed to load shaders ...\n");
return false;
}
}
/* TODO - textures / imports */
return true;
}
static bool hlsl_init(const char *path)
{
xdk_d3d_video_t *d3d = (xdk_d3d_video_t*)driver.video_data;
2013-04-12 18:06:08 +00:00
if (path && strcmp(path_get_extension(path), ".cgp") == 0)
{
if (!load_preset(path))
return false;
}
else
{
if (!load_plain(path))
return false;
}
2013-04-12 18:06:08 +00:00
for(unsigned i = 1; i <= cg_shader->passes; i++)
2012-05-06 19:57:01 +00:00
set_program_attributes(i);
d3d_device_ptr = d3d->d3d_render_device;
d3d->d3d_render_device->SetVertexShader(prg[1].vprg);
d3d->d3d_render_device->SetPixelShader(prg[1].fprg);
hlsl_active = true;
return true;
}
2013-04-12 18:06:08 +00:00
// Full deinit.
static void hlsl_deinit(void)
2013-04-12 18:06:08 +00:00
{
if (!hlsl_active)
return;
hlsl_deinit_state();
}
static void hlsl_use(unsigned index)
{
2013-04-12 18:06:08 +00:00
if (hlsl_active && prg[index].vprg && prg[index].fprg)
2012-05-06 19:57:01 +00:00
{
active_index = index;
#ifdef _XBOX
D3DDevice_SetVertexShader(d3d_device_ptr, prg[index].vprg);
D3DDevice_SetPixelShader(d3d_device_ptr, prg[index].fprg);
#else
2012-05-06 19:57:01 +00:00
d3d_device_ptr->SetVertexShader(prg[index].vprg);
d3d_device_ptr->SetPixelShader(prg[index].fprg);
#endif
2012-05-06 19:57:01 +00:00
}
}
static unsigned hlsl_num(void)
{
2013-04-12 18:06:08 +00:00
if (hlsl_active)
return cg_shader->passes;
else
return 0;
}
static bool hlsl_filter_type(unsigned index, bool *smooth)
2013-04-12 18:06:08 +00:00
{
if (hlsl_active && index)
{
if (cg_shader->pass[index - 1].filter == RARCH_FILTER_UNSPEC)
return false;
*smooth = cg_shader->pass[index - 1].filter = RARCH_FILTER_LINEAR;
return true;
}
else
return false;
}
2013-04-12 18:06:08 +00:00
static void hlsl_shader_scale(unsigned index, struct gfx_fbo_scale *scale)
2013-04-12 18:06:08 +00:00
{
if (hlsl_active && index)
*scale = cg_shader->pass[index - 1].fbo;
else
scale->valid = false;
}
static bool hlsl_set_mvp(const math_matrix *mat)
{
/* TODO: Move to D3DXMATRIX here */
if(hlsl_active && prg[active_index].mvp)
{
prg[active_index].v_ctable->SetMatrix(d3d_device_ptr, prg[active_index].mvp, (D3DXMATRIX*)&prg[active_index].mvp_val);
return true;
}
else
return false;
}
const gl_shader_backend_t hlsl_backend = {
hlsl_init,
hlsl_deinit,
hlsl_set_params,
hlsl_use,
hlsl_num,
hlsl_filter_type,
2013-09-15 16:49:18 +00:00
NULL, /* hlsl_wrap_type */
hlsl_shader_scale,
2013-09-15 16:49:18 +00:00
NULL, /* hlsl_set_coords */
hlsl_set_mvp,
2013-09-15 16:49:18 +00:00
NULL, /* hlsl_get_prev_textures */
RARCH_SHADER_HLSL,
};