(D3D9) Refactor render_chain.cpp - isolate Cg code

This commit is contained in:
twinaphex 2014-03-04 18:19:47 +01:00
parent 68a6da85b7
commit 16af303c9e
2 changed files with 117 additions and 115 deletions

View File

@ -16,6 +16,7 @@
#include "render_chain.hpp"
#include <string.h>
#ifdef HAVE_CG
static const char *stock_program =
"void main_vertex"
"("
@ -40,15 +41,114 @@ static const char *stock_program =
" return color * tex2D(s0, tex);"
"}";
RenderChain::~RenderChain()
bool RenderChain::compile_shaders(CGprogram &fPrg, CGprogram &vPrg, const std::string &shader)
{
CGprofile vertex_profile = cgD3D9GetLatestVertexProfile();
CGprofile fragment_profile = cgD3D9GetLatestPixelProfile();
RARCH_LOG("[D3D Cg]: Vertex profile: %s\n", cgGetProfileString(vertex_profile));
RARCH_LOG("[D3D Cg]: Fragment profile: %s\n", cgGetProfileString(fragment_profile));
const char **fragment_opts = cgD3D9GetOptimalOptions(fragment_profile);
const char **vertex_opts = cgD3D9GetOptimalOptions(vertex_profile);
if (shader.length() > 0)
{
RARCH_LOG("[D3D Cg]: Compiling shader: %s.\n", shader.c_str());
fPrg = cgCreateProgramFromFile(cgCtx, CG_SOURCE,
shader.c_str(), fragment_profile, "main_fragment", fragment_opts);
if (cgGetLastListing(cgCtx))
RARCH_ERR("[D3D Cg]: Fragment error:\n%s\n", cgGetLastListing(cgCtx));
vPrg = cgCreateProgramFromFile(cgCtx, CG_SOURCE,
shader.c_str(), vertex_profile, "main_vertex", vertex_opts);
if (cgGetLastListing(cgCtx))
RARCH_ERR("[D3D Cg]: Vertex error:\n%s\n", cgGetLastListing(cgCtx));
}
else
{
RARCH_LOG("[D3D Cg]: Compiling stock shader.\n");
fPrg = cgCreateProgram(cgCtx, CG_SOURCE, stock_program,
fragment_profile, "main_fragment", fragment_opts);
if (cgGetLastListing(cgCtx))
RARCH_ERR("[D3D Cg]: Fragment error:\n%s\n", cgGetLastListing(cgCtx));
vPrg = cgCreateProgram(cgCtx, CG_SOURCE, stock_program,
vertex_profile, "main_vertex", vertex_opts);
if (cgGetLastListing(cgCtx))
RARCH_ERR("[D3D Cg]: Vertex error:\n%s\n", cgGetLastListing(cgCtx));
}
if (!fPrg || !vPrg)
return false;
cgD3D9LoadProgram(fPrg, true, 0);
cgD3D9LoadProgram(vPrg, true, 0);
return true;
}
void RenderChain::set_shaders(CGprogram &fPrg, CGprogram &vPrg)
{
cgD3D9BindProgram(fPrg);
cgD3D9BindProgram(vPrg);
}
void RenderChain::destroy_stock_shaders(void)
{
clear();
#ifdef HAVE_CG
if (fStock)
cgDestroyProgram(fStock);
if (vStock)
cgDestroyProgram(vStock);
}
void RenderChain::set_shader_mvp(CGprogram &vPrg, D3DXMATRIX &tmp)
{
CGparameter cgpModelViewProj = cgGetNamedParameter(vPrg, "modelViewProj");
if (cgpModelViewProj)
cgD3D9SetUniformMatrix(cgpModelViewProj, &tmp);
}
#define set_cg_param(prog, param, val) do { \
CGparameter cgp = cgGetNamedParameter(prog, param); \
if (cgp) \
cgD3D9SetUniform(cgp, &val); \
} while(0)
void RenderChain::set_shader_params(Pass &pass,
unsigned video_w, unsigned video_h,
unsigned tex_w, unsigned tex_h,
unsigned viewport_w, unsigned viewport_h)
{
D3DXVECTOR2 video_size, texture_size, output_size;
video_size.x = video_w;
video_size.y = video_h;
texture_size.x = tex_w;
texture_size.y = tex_h;
output_size.x = viewport_w;
output_size.y = 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);
float frame_cnt = frame_count;
if (pass.info.pass->frame_count_mod)
frame_cnt = 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);
}
#endif
RenderChain::~RenderChain()
{
clear();
destroy_stock_shaders();
if (tracker)
state_tracker_free(tracker);
}
@ -310,7 +410,7 @@ bool RenderChain::render(const void *data,
end_render();
set_shaders(fStock, vStock);
set_cg_mvp(vStock, final_viewport.Width, final_viewport.Height, 0);
set_mvp(vStock, final_viewport.Width, final_viewport.Height, 0);
return true;
}
@ -381,65 +481,6 @@ bool RenderChain::create_first_pass(const LinkInfo &info, PixelFormat fmt)
return true;
}
bool RenderChain::compile_shaders(CGprogram &fPrg, CGprogram &vPrg, const std::string &shader)
{
#ifdef HAVE_CG
CGprofile vertex_profile = cgD3D9GetLatestVertexProfile();
CGprofile fragment_profile = cgD3D9GetLatestPixelProfile();
RARCH_LOG("[D3D Cg]: Vertex profile: %s\n", cgGetProfileString(vertex_profile));
RARCH_LOG("[D3D Cg]: Fragment profile: %s\n", cgGetProfileString(fragment_profile));
const char **fragment_opts = cgD3D9GetOptimalOptions(fragment_profile);
const char **vertex_opts = cgD3D9GetOptimalOptions(vertex_profile);
if (shader.length() > 0)
{
RARCH_LOG("[D3D Cg]: Compiling shader: %s.\n", shader.c_str());
fPrg = cgCreateProgramFromFile(cgCtx, CG_SOURCE,
shader.c_str(), fragment_profile, "main_fragment", fragment_opts);
if (cgGetLastListing(cgCtx))
RARCH_ERR("[D3D Cg]: Fragment error:\n%s\n", cgGetLastListing(cgCtx));
vPrg = cgCreateProgramFromFile(cgCtx, CG_SOURCE,
shader.c_str(), vertex_profile, "main_vertex", vertex_opts);
if (cgGetLastListing(cgCtx))
RARCH_ERR("[D3D Cg]: Vertex error:\n%s\n", cgGetLastListing(cgCtx));
}
else
{
RARCH_LOG("[D3D Cg]: Compiling stock shader.\n");
fPrg = cgCreateProgram(cgCtx, CG_SOURCE, stock_program,
fragment_profile, "main_fragment", fragment_opts);
if (cgGetLastListing(cgCtx))
RARCH_ERR("[D3D Cg]: Fragment error:\n%s\n", cgGetLastListing(cgCtx));
vPrg = cgCreateProgram(cgCtx, CG_SOURCE, stock_program,
vertex_profile, "main_vertex", vertex_opts);
if (cgGetLastListing(cgCtx))
RARCH_ERR("[D3D Cg]: Vertex error:\n%s\n", cgGetLastListing(cgCtx));
}
if (!fPrg || !vPrg)
return false;
cgD3D9LoadProgram(fPrg, true, 0);
cgD3D9LoadProgram(vPrg, true, 0);
#endif
return true;
}
void RenderChain::set_shaders(CGprogram &fPrg, CGprogram &vPrg)
{
#ifdef HAVE_CG
cgD3D9BindProgram(fPrg);
cgD3D9BindProgram(vPrg);
#endif
}
void RenderChain::set_vertices(Pass &pass,
unsigned width, unsigned height,
unsigned out_width, unsigned out_height,
@ -502,14 +543,11 @@ void RenderChain::set_vertices(Pass &pass,
pass.vertex_buf->Unlock();
}
#ifdef HAVE_CG
set_cg_mvp(pass.vPrg, vp_width, vp_height, rotation);
set_cg_params(pass,
set_mvp(pass.vPrg, vp_width, vp_height, rotation);
set_shader_params(pass,
width, height,
info.tex_w, info.tex_h,
vp_width, vp_height);
#endif
}
void RenderChain::set_viewport(const D3DVIEWPORT &vp)
@ -517,7 +555,7 @@ void RenderChain::set_viewport(const D3DVIEWPORT &vp)
dev->SetViewport(&vp);
}
void RenderChain::set_cg_mvp(CGprogram &vPrg,
void RenderChain::set_mvp(CGprogram &vPrg,
unsigned vp_width, unsigned vp_height,
unsigned rotation)
{
@ -532,44 +570,7 @@ void RenderChain::set_cg_mvp(CGprogram &vPrg,
D3DXMatrixMultiply(&proj, &ortho, &rot);
D3DXMatrixTranspose(&tmp, &proj);
#ifdef HAVE_CG
CGparameter cgpModelViewProj = cgGetNamedParameter(vPrg, "modelViewProj");
if (cgpModelViewProj)
cgD3D9SetUniformMatrix(cgpModelViewProj, &tmp);
#endif
}
#define set_cg_param(prog, param, val) do { \
CGparameter cgp = cgGetNamedParameter(prog, param); \
if (cgp) \
cgD3D9SetUniform(cgp, &val); \
} while(0)
void RenderChain::set_cg_params(Pass &pass,
unsigned video_w, unsigned video_h,
unsigned tex_w, unsigned tex_h,
unsigned viewport_w, unsigned viewport_h)
{
D3DXVECTOR2 video_size, texture_size, output_size;
video_size.x = video_w;
video_size.y = video_h;
texture_size.x = tex_w;
texture_size.y = tex_h;
output_size.x = viewport_w;
output_size.y = 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);
float frame_cnt = frame_count;
if (pass.info.pass->frame_count_mod)
frame_cnt = 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);
set_shader_mvp(vPrg, tmp);
}
void RenderChain::clear_texture(Pass &pass)
@ -677,7 +678,7 @@ void RenderChain::render_pass(Pass &pass, unsigned pass_index)
void RenderChain::log_info(const LinkInfo &info)
{
RARCH_LOG("[D3D9 Cg]: Render pass info:\n");
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);

View File

@ -127,11 +127,17 @@ class RenderChain
bool create_first_pass(const LinkInfo &info, PixelFormat fmt);
#if defined(HAVE_CG)
bool compile_shaders(CGprogram &fPrg, CGprogram &vPrg, const std::string &shader);
void destroy_stock_shaders(void);
void set_shaders(CGprogram &fPrg, CGprogram &vPrg);
void set_cg_mvp(CGprogram &vPrg,
void set_shader_mvp(CGprogram &vPrg, D3DXMATRIX &tmp);
void set_shader_params(Pass &pass,
unsigned input_w, unsigned input_h,
unsigned tex_w, unsigned tex_h,
unsigned vp_w, unsigned vp_h);
#endif
void set_mvp(CGprogram &vPrg,
unsigned vp_width, unsigned vp_height,
unsigned rotation);
#endif
void set_vertices(Pass &pass,
unsigned width, unsigned height,
@ -140,11 +146,6 @@ class RenderChain
unsigned rotation);
void set_viewport(const D3DVIEWPORT &vp);
void set_cg_params(Pass &pass,
unsigned input_w, unsigned input_h,
unsigned tex_w, unsigned tex_h,
unsigned vp_w, unsigned vp_h);
void clear_texture(Pass &pass);
void blit_to_texture(const void *data,