obs-studio/libobs-opengl/gl-shaderparser.c

665 lines
18 KiB
C
Raw Normal View History

/******************************************************************************
Copyright (C) 2013 by Hugh Bailey <obs.jim@gmail.com>
This program 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 Foundation, either version 2 of the License, or
(at your option) any later version.
This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
#include "gl-subsystem.h"
#include "gl-shaderparser.h"
static void gl_write_function_contents(struct gl_shader_parser *glsp,
struct cf_token **p_token, const char *end);
2013-10-09 15:48:16 -07:00
static inline struct shader_var *sp_getparam(struct gl_shader_parser *glsp,
struct cf_token *token)
{
size_t i;
for (i = 0; i < glsp->parser.params.num; i++) {
struct shader_var *param = glsp->parser.params.array+i;
if (strref_cmp(&token->str, param->name) == 0)
return param;
}
return NULL;
}
static inline size_t sp_getsampler(struct gl_shader_parser *glsp,
struct cf_token *token)
{
size_t i;
for (i = 0; i < glsp->parser.samplers.num; i++) {
struct shader_sampler *sampler = glsp->parser.samplers.array+i;
if (strref_cmp(&token->str, sampler->name) == 0)
return i;
}
return -1;
}
static inline int cmp_type(const char *name, const size_t name_len,
const char *type, const size_t type_len)
{
size_t min_len = (name_len < type_len) ? type_len : name_len;
return astrcmp_n(name, type, min_len);
}
static bool gl_write_type_n(struct gl_shader_parser *glsp,
const char *type, size_t len)
{
if (cmp_type(type, len, "float2", 6) == 0)
dstr_cat(&glsp->gl_string, "vec2");
else if (cmp_type(type, len, "float3", 6) == 0)
dstr_cat(&glsp->gl_string, "vec3");
else if (cmp_type(type, len, "float4", 6) == 0)
dstr_cat(&glsp->gl_string, "vec4");
else if (cmp_type(type, len, "float3x3", 8) == 0)
dstr_cat(&glsp->gl_string, "mat3x3");
else if (cmp_type(type, len, "float3x4", 8) == 0)
dstr_cat(&glsp->gl_string, "mat3x4");
else if (cmp_type(type, len, "float4x4", 8) == 0)
dstr_cat(&glsp->gl_string, "mat4x4");
else if (cmp_type(type, len, "texture2d", 9) == 0)
dstr_cat(&glsp->gl_string, "sampler2D");
else if (cmp_type(type, len, "texture3d", 9) == 0)
dstr_cat(&glsp->gl_string, "sampler3D");
else if (cmp_type(type, len, "texture_cube", 12) == 0)
dstr_cat(&glsp->gl_string, "samplerCube");
else if (cmp_type(type, len, "texture_rect", 12) == 0)
dstr_cat(&glsp->gl_string, "sampler2DRect");
else
return false;
return true;
}
static inline void gl_write_type(struct gl_shader_parser *glsp,
const char *type)
{
if (!gl_write_type_n(glsp, type, strlen(type)))
dstr_cat(&glsp->gl_string, type);
}
static inline bool gl_write_type_token(struct gl_shader_parser *glsp,
struct cf_token *token)
{
return gl_write_type_n(glsp, token->str.array, token->str.len);
}
static void gl_write_var(struct gl_shader_parser *glsp, struct shader_var *var)
{
if (var->var_type == SHADER_VAR_UNIFORM)
dstr_cat(&glsp->gl_string, "uniform ");
else if (var->var_type == SHADER_VAR_CONST)
dstr_cat(&glsp->gl_string, "const ");
gl_write_type(glsp, var->type);
dstr_cat(&glsp->gl_string, " ");
dstr_cat(&glsp->gl_string, var->name);
}
static inline void gl_write_params(struct gl_shader_parser *glsp)
{
size_t i;
for (i = 0; i < glsp->parser.params.num; i++) {
struct shader_var *var = glsp->parser.params.array+i;
gl_write_var(glsp, var);
dstr_cat(&glsp->gl_string, ";\n");
}
dstr_cat(&glsp->gl_string, "\n");
}
static void gl_write_storage_var(struct gl_shader_parser *glsp,
struct shader_var *var, bool input, const char *prefix);
/* unwraps a structure that's used for input/output */
static void gl_unwrap_storage_struct(struct gl_shader_parser *glsp,
struct shader_struct *st, const char *name, bool input,
const char *prefix)
{
struct dstr prefix_str;
size_t i;
dstr_init(&prefix_str);
if (prefix)
dstr_copy(&prefix_str, prefix);
dstr_cat(&prefix_str, name);
dstr_cat(&prefix_str, "_");
for (i = 0; i < st->vars.num; i++) {
struct shader_var *st_var = st->vars.array+i;
gl_write_storage_var(glsp, st_var, input, prefix_str.array);
}
dstr_free(&prefix_str);
}
static void gl_write_storage_var(struct gl_shader_parser *glsp,
struct shader_var *var, bool input, const char *prefix)
{
struct shader_struct *st = shader_parser_getstruct(&glsp->parser,
var->type);
if (st) {
gl_unwrap_storage_struct(glsp, st, var->name, input, prefix);
} else {
2013-10-17 17:21:42 -07:00
struct gl_parser_attrib attrib;
gl_parser_attrib_init(&attrib);
dstr_cat(&glsp->gl_string, input ? "in " : "out ");
if (prefix)
dstr_cat(&attrib.name, prefix);
dstr_cat(&attrib.name, var->name);
gl_write_type(glsp, var->type);
dstr_cat(&glsp->gl_string, " ");
dstr_cat_dstr(&glsp->gl_string, &attrib.name);
dstr_cat(&glsp->gl_string, ";\n");
attrib.input = input;
attrib.mapping = var->mapping;
da_push_back(glsp->attribs, &attrib);
}
}
static inline void gl_write_inputs(struct gl_shader_parser *glsp,
struct shader_func *main)
{
size_t i;
for (i = 0; i < main->params.num; i++)
gl_write_storage_var(glsp, main->params.array+i, true,
"inputval_");
dstr_cat(&glsp->gl_string, "\n");
}
static void gl_write_outputs(struct gl_shader_parser *glsp,
struct shader_func *main)
{
struct shader_var var = {0};
var.type = main->return_type;
var.name = "outputval";
if (main->mapping)
var.mapping = main->mapping;
gl_write_storage_var(glsp, &var, false, NULL);
dstr_cat(&glsp->gl_string, "\n");
}
static void gl_write_struct(struct gl_shader_parser *glsp,
struct shader_struct *st)
{
size_t i;
dstr_cat(&glsp->gl_string, "struct ");
dstr_cat(&glsp->gl_string, st->name);
dstr_cat(&glsp->gl_string, " {\n");
for (i = 0; i < st->vars.num; i++) {
struct shader_var *var = st->vars.array+i;
dstr_cat(&glsp->gl_string, "\t");
gl_write_var(glsp, var);
dstr_cat(&glsp->gl_string, ";\n");
}
dstr_cat(&glsp->gl_string, "};\n\n");
}
static void gl_write_interface_block(struct gl_shader_parser *glsp)
{
(API Change) Improve graphics API consistency Summary: - Prefix all graphics subsystem names with gs_ or GS_ - Unsquish funciton names (for example _setfloat to _set_float) - Changed create functions to be more consistent with the rest of the API elsewhere. For exmaple, instead of gs_create_texture/gs_texture_destroy, it's now gs_texture_create/gs_texture_destroy - Renamed gs_stencil_op enum to gs_stencil_op_type From: To: ----------------------------------------------------------- tvertarray gs_tvertarray vb_data gs_vb_data vbdata_create gs_vbdata_create vbdata_destroy gs_vbdata_destroy shader_param gs_shader_param gs_effect gs_effect effect_technique gs_effect_technique effect_pass gs_effect_pass effect_param gs_effect_param texture_t gs_texture_t stagesurf_t gs_stagesurf_t zstencil_t gs_zstencil_t vertbuffer_t gs_vertbuffer_t indexbuffer_t gs_indexbuffer_t samplerstate_t gs_samplerstate_t swapchain_t gs_swapchain_t texrender_t gs_texrender_t shader_t gs_shader_t sparam_t gs_sparam_t effect_t gs_effect_t technique_t gs_technique_t eparam_t gs_eparam_t device_t gs_device_t graphics_t graphics_t shader_param_type gs_shader_param_type SHADER_PARAM_UNKNOWN GS_SHADER_PARAM_UNKNOWN SHADER_PARAM_BOOL GS_SHADER_PARAM_BOOL SHADER_PARAM_FLOAT GS_SHADER_PARAM_FLOAT SHADER_PARAM_INT GS_SHADER_PARAM_INT SHADER_PARAM_STRING GS_SHADER_PARAM_STRING SHADER_PARAM_VEC2 GS_SHADER_PARAM_VEC2 SHADER_PARAM_VEC3 GS_SHADER_PARAM_VEC3 SHADER_PARAM_VEC4 GS_SHADER_PARAM_VEC4 SHADER_PARAM_MATRIX4X4 GS_SHADER_PARAM_MATRIX4X4 SHADER_PARAM_TEXTURE GS_SHADER_PARAM_TEXTURE shader_param_info gs_shader_param_info shader_type gs_shader_type SHADER_VERTEX GS_SHADER_VERTEX SHADER_PIXEL GS_SHADER_PIXEL shader_destroy gs_shader_destroy shader_numparams gs_shader_get_num_params shader_getparambyidx gs_shader_get_param_by_idx shader_getparambyname gs_shader_get_param_by_name shader_getviewprojmatrix gs_shader_get_viewproj_matrix shader_getworldmatrix gs_shader_get_world_matrix shader_getparaminfo gs_shader_get_param_info shader_setbool gs_shader_set_bool shader_setfloat gs_shader_set_float shader_setint gs_shader_set_int shader_setmatrix3 gs_shader_setmatrix3 shader_setmatrix4 gs_shader_set_matrix4 shader_setvec2 gs_shader_set_vec2 shader_setvec3 gs_shader_set_vec3 shader_setvec4 gs_shader_set_vec4 shader_settexture gs_shader_set_texture shader_setval gs_shader_set_val shader_setdefault gs_shader_set_default effect_property_type gs_effect_property_type EFFECT_NONE GS_EFFECT_NONE EFFECT_BOOL GS_EFFECT_BOOL EFFECT_FLOAT GS_EFFECT_FLOAT EFFECT_COLOR GS_EFFECT_COLOR EFFECT_TEXTURE GS_EFFECT_TEXTURE effect_param_info gs_effect_param_info effect_destroy gs_effect_destroy effect_gettechnique gs_effect_get_technique technique_begin gs_technique_begin technique_end gs_technique_end technique_beginpass gs_technique_begin_pass technique_beginpassbyname gs_technique_begin_pass_by_name technique_endpass gs_technique_end_pass effect_numparams gs_effect_get_num_params effect_getparambyidx gs_effect_get_param_by_idx effect_getparambyname gs_effect_get_param_by_name effect_updateparams gs_effect_update_params effect_getviewprojmatrix gs_effect_get_viewproj_matrix effect_getworldmatrix gs_effect_get_world_matrix effect_getparaminfo gs_effect_get_param_info effect_setbool gs_effect_set_bool effect_setfloat gs_effect_set_float effect_setint gs_effect_set_int effect_setmatrix4 gs_effect_set_matrix4 effect_setvec2 gs_effect_set_vec2 effect_setvec3 gs_effect_set_vec3 effect_setvec4 gs_effect_set_vec4 effect_settexture gs_effect_set_texture effect_setval gs_effect_set_val effect_setdefault gs_effect_set_default texrender_create gs_texrender_create texrender_destroy gs_texrender_destroy texrender_begin gs_texrender_begin texrender_end gs_texrender_end texrender_reset gs_texrender_reset texrender_gettexture gs_texrender_get_texture GS_BUILDMIPMAPS GS_BUILD_MIPMAPS GS_RENDERTARGET GS_RENDER_TARGET gs_device_name gs_get_device_name gs_device_type gs_get_device_type gs_entercontext gs_enter_context gs_leavecontext gs_leave_context gs_getcontext gs_get_context gs_renderstart gs_render_start gs_renderstop gs_render_stop gs_rendersave gs_render_save gs_getinput gs_get_input gs_geteffect gs_get_effect gs_create_effect_from_file gs_effect_create_from_file gs_create_effect gs_effect_create gs_create_vertexshader_from_file gs_vertexshader_create_from_file gs_create_pixelshader_from_file gs_pixelshader_create_from_file gs_create_texture_from_file gs_texture_create_from_file gs_resetviewport gs_reset_viewport gs_set2dmode gs_set_2d_mode gs_set3dmode gs_set_3d_mode gs_create_swapchain gs_swapchain_create gs_getsize gs_get_size gs_getwidth gs_get_width gs_getheight gs_get_height gs_create_texture gs_texture_create gs_create_cubetexture gs_cubetexture_create gs_create_volumetexture gs_voltexture_create gs_create_zstencil gs_zstencil_create gs_create_stagesurface gs_stagesurface_create gs_create_samplerstate gs_samplerstate_create gs_create_vertexshader gs_vertexshader_create gs_create_pixelshader gs_pixelshader_create gs_create_vertexbuffer gs_vertexbuffer_create gs_create_indexbuffer gs_indexbuffer_create gs_gettexturetype gs_get_texture_type gs_load_defaultsamplerstate gs_load_default_samplerstate gs_getvertexshader gs_get_vertex_shader gs_getpixelshader gs_get_pixel_shader gs_getrendertarget gs_get_render_target gs_getzstenciltarget gs_get_zstencil_target gs_setrendertarget gs_set_render_target gs_setcuberendertarget gs_set_cube_render_target gs_beginscene gs_begin_scene gs_draw gs_draw gs_endscene gs_end_scene gs_setcullmode gs_set_cull_mode gs_getcullmode gs_get_cull_mode gs_enable_depthtest gs_enable_depth_test gs_enable_stenciltest gs_enable_stencil_test gs_enable_stencilwrite gs_enable_stencil_write gs_blendfunction gs_blend_function gs_depthfunction gs_depth_function gs_stencilfunction gs_stencil_function gs_stencilop gs_stencil_op gs_setviewport gs_set_viewport gs_getviewport gs_get_viewport gs_setscissorrect gs_set_scissor_rect gs_create_texture_from_iosurface gs_texture_create_from_iosurface gs_create_gdi_texture gs_texture_create_gdi gs_is_compressed_format gs_is_compressed_format gs_num_total_levels gs_get_total_levels texture_setimage gs_texture_set_image cubetexture_setimage gs_cubetexture_set_image swapchain_destroy gs_swapchain_destroy texture_destroy gs_texture_destroy texture_getwidth gs_texture_get_width texture_getheight gs_texture_get_height texture_getcolorformat gs_texture_get_color_format texture_map gs_texture_map texture_unmap gs_texture_unmap texture_isrect gs_texture_is_rect texture_getobj gs_texture_get_obj cubetexture_destroy gs_cubetexture_destroy cubetexture_getsize gs_cubetexture_get_size cubetexture_getcolorformat gs_cubetexture_get_color_format volumetexture_destroy gs_voltexture_destroy volumetexture_getwidth gs_voltexture_get_width volumetexture_getheight gs_voltexture_get_height volumetexture_getdepth gs_voltexture_getdepth volumetexture_getcolorformat gs_voltexture_get_color_format stagesurface_destroy gs_stagesurface_destroy stagesurface_getwidth gs_stagesurface_get_width stagesurface_getheight gs_stagesurface_get_height stagesurface_getcolorformat gs_stagesurface_get_color_format stagesurface_map gs_stagesurface_map stagesurface_unmap gs_stagesurface_unmap zstencil_destroy gs_zstencil_destroy samplerstate_destroy gs_samplerstate_destroy vertexbuffer_destroy gs_vertexbuffer_destroy vertexbuffer_flush gs_vertexbuffer_flush vertexbuffer_getdata gs_vertexbuffer_get_data indexbuffer_destroy gs_indexbuffer_destroy indexbuffer_flush gs_indexbuffer_flush indexbuffer_getdata gs_indexbuffer_get_data indexbuffer_numindices gs_indexbuffer_get_num_indices indexbuffer_gettype gs_indexbuffer_get_type texture_rebind_iosurface gs_texture_rebind_iosurface texture_get_dc gs_texture_get_dc texture_release_dc gs_texture_release_dc
2014-08-07 23:42:07 -07:00
if (glsp->type == GS_SHADER_VERTEX) {
dstr_cat(&glsp->gl_string, "out gl_PerVertex {\n"
"\tvec4 gl_Position;\n};\n\n");
}
}
static inline void gl_write_structs(struct gl_shader_parser *glsp)
{
size_t i;
for (i = 0; i < glsp->parser.structs.num; i++) {
struct shader_struct *st = glsp->parser.structs.array+i;
gl_write_struct(glsp, st);
}
}
/*
* NOTE: HLSL-> GLSL intrinsic conversions
* atan2 -> atan
* clip -> (unsupported)
* ddx -> dFdx
* ddy -> dFdy
* fmod -> mod (XXX: these are different if sign is negative)
* frac -> fract
* lerp -> mix
* lit -> (unsupported)
* log10 -> (unsupported)
* mul -> (change to operator)
* rsqrt -> inversesqrt
* saturate -> (use clamp)
* tex* -> texture
* tex*grad -> textureGrad
* tex*lod -> textureLod
* tex*bias -> (use optional 'bias' value)
* tex*proj -> textureProj
*
* All else can be left as-is
*/
static bool gl_write_mul(struct gl_shader_parser *glsp,
struct cf_token **p_token)
{
struct cf_parser *cfp = &glsp->parser.cfp;
cfp->cur_token = *p_token;
2014-03-01 00:25:41 -08:00
if (!cf_next_token(cfp)) return false;
if (!cf_token_is(cfp, "(")) return false;
dstr_cat(&glsp->gl_string, "(");
gl_write_function_contents(glsp, &cfp->cur_token, ",");
dstr_cat(&glsp->gl_string, ") * (");
2014-03-01 00:25:41 -08:00
cf_next_token(cfp);
gl_write_function_contents(glsp, &cfp->cur_token, ")");
dstr_cat(&glsp->gl_string, "))");
*p_token = cfp->cur_token;
return true;
}
static bool gl_write_saturate(struct gl_shader_parser *glsp,
struct cf_token **p_token)
{
struct cf_parser *cfp = &glsp->parser.cfp;
cfp->cur_token = *p_token;
2014-03-01 00:25:41 -08:00
if (!cf_next_token(cfp)) return false;
if (!cf_token_is(cfp, "(")) return false;
dstr_cat(&glsp->gl_string, "clamp");
gl_write_function_contents(glsp, &cfp->cur_token, ")");
dstr_cat(&glsp->gl_string, ", 0.0, 1.0)");
*p_token = cfp->cur_token;
return true;
}
static inline bool gl_write_texture_call(struct gl_shader_parser *glsp,
struct shader_var *var, const char *call)
{
struct cf_parser *cfp = &glsp->parser.cfp;
2013-10-17 17:21:42 -07:00
size_t sampler_id = (size_t)-1;
2013-10-09 15:48:16 -07:00
2014-03-01 00:25:41 -08:00
if (!cf_next_token(cfp)) return false;
if (!cf_token_is(cfp, "(")) return false;
if (!cf_next_token(cfp)) return false;
2013-10-09 15:48:16 -07:00
sampler_id = sp_getsampler(glsp, cfp->cur_token);
2013-10-17 17:21:42 -07:00
if (sampler_id == (size_t)-1) return false;
2013-10-09 15:48:16 -07:00
2014-03-01 00:25:41 -08:00
if (!cf_next_token(cfp)) return false;
if (!cf_token_is(cfp, ",")) return false;
2013-10-09 15:48:16 -07:00
var->gl_sampler_id = sampler_id;
dstr_cat(&glsp->gl_string, call);
dstr_cat(&glsp->gl_string, "(");
dstr_cat(&glsp->gl_string, var->name);
dstr_cat(&glsp->gl_string, ", ");
return true;
}
/* processes texture.Sample(sampler, texcoord) */
static bool gl_write_texture_code(struct gl_shader_parser *glsp,
struct cf_token **p_token, struct shader_var *var)
{
struct cf_parser *cfp = &glsp->parser.cfp;
bool written = false;
cfp->cur_token = *p_token;
2014-03-01 00:25:41 -08:00
if (!cf_next_token(cfp)) return false;
if (!cf_token_is(cfp, ".")) return false;
if (!cf_next_token(cfp)) return false;
2014-03-01 00:25:41 -08:00
if (cf_token_is(cfp, "Sample"))
written = gl_write_texture_call(glsp, var, "texture");
2014-03-01 00:25:41 -08:00
else if (cf_token_is(cfp, "SampleBias"))
written = gl_write_texture_call(glsp, var, "texture");
2014-03-01 00:25:41 -08:00
else if (cf_token_is(cfp, "SampleGrad"))
written = gl_write_texture_call(glsp, var, "textureGrad");
2014-03-01 00:25:41 -08:00
else if (cf_token_is(cfp, "SampleLevel"))
written = gl_write_texture_call(glsp, var, "textureLod");
if (!written)
return false;
2014-03-01 00:25:41 -08:00
if (!cf_next_token(cfp)) return false;
gl_write_function_contents(glsp, &cfp->cur_token, ")");
dstr_cat(&glsp->gl_string, ")");
*p_token = cfp->cur_token;
return true;
}
static bool gl_write_intrinsic(struct gl_shader_parser *glsp,
struct cf_token **p_token)
{
struct cf_token *token = *p_token;
bool written = true;
if (strref_cmp(&token->str, "atan2") == 0) {
dstr_cat(&glsp->gl_string, "atan2");
} else if (strref_cmp(&token->str, "ddx") == 0) {
dstr_cat(&glsp->gl_string, "dFdx");
} else if (strref_cmp(&token->str, "ddy") == 0) {
dstr_cat(&glsp->gl_string, "dFdy");
} else if (strref_cmp(&token->str, "frac") == 0) {
dstr_cat(&glsp->gl_string, "fract");
} else if (strref_cmp(&token->str, "lerp") == 0) {
dstr_cat(&glsp->gl_string, "mix");
} else if (strref_cmp(&token->str, "fmod") == 0) {
dstr_cat(&glsp->gl_string, "mod");
} else if (strref_cmp(&token->str, "rsqrt") == 0) {
dstr_cat(&glsp->gl_string, "inversesqrt");
} else if (strref_cmp(&token->str, "saturate") == 0) {
written = gl_write_saturate(glsp, &token);
} else if (strref_cmp(&token->str, "mul") == 0) {
written = gl_write_mul(glsp, &token);
} else {
struct shader_var *var = sp_getparam(glsp, token);
if (var && astrcmp_n(var->type, "texture", 7) == 0)
written = gl_write_texture_code(glsp, &token, var);
else
written = false;
}
if (written)
*p_token = token;
return written;
}
static void gl_write_function_contents(struct gl_shader_parser *glsp,
struct cf_token **p_token, const char *end)
{
struct cf_token *token = *p_token;
if (token->type != CFTOKEN_NAME
|| ( !gl_write_type_token(glsp, token)
&& !gl_write_intrinsic(glsp, &token)))
dstr_cat_strref(&glsp->gl_string, &token->str);
while (token->type != CFTOKEN_NONE) {
token++;
if (end && strref_cmp(&token->str, end) == 0)
break;
if (token->type == CFTOKEN_NAME) {
if (!gl_write_type_token(glsp, token) &&
!gl_write_intrinsic(glsp, &token))
dstr_cat_strref(&glsp->gl_string, &token->str);
} else if (token->type == CFTOKEN_OTHER) {
if (*token->str.array == '{')
gl_write_function_contents(glsp, &token, "}");
else if (*token->str.array == '(')
gl_write_function_contents(glsp, &token, ")");
dstr_cat_strref(&glsp->gl_string, &token->str);
} else {
dstr_cat_strref(&glsp->gl_string, &token->str);
}
}
*p_token = token;
}
static void gl_write_function(struct gl_shader_parser *glsp,
struct shader_func *func)
{
size_t i;
struct cf_token *token;
gl_write_type(glsp, func->return_type);
dstr_cat(&glsp->gl_string, " ");
if (strcmp(func->name, "main") == 0)
dstr_cat(&glsp->gl_string, "_main_wrap");
else
dstr_cat(&glsp->gl_string, func->name);
dstr_cat(&glsp->gl_string, "(");
for (i = 0; i < func->params.num; i++) {
struct shader_var *param = func->params.array+i;
if (i > 0)
dstr_cat(&glsp->gl_string, ", ");
gl_write_var(glsp, param);
}
dstr_cat(&glsp->gl_string, ")\n");
token = func->start;
gl_write_function_contents(glsp, &token, "}");
dstr_cat(&glsp->gl_string, "}\n\n");
}
static inline void gl_write_functions(struct gl_shader_parser *glsp)
{
size_t i;
for (i = 0; i < glsp->parser.funcs.num; i++) {
struct shader_func *func = glsp->parser.funcs.array+i;
gl_write_function(glsp, func);
}
}
static inline void gl_write_main_interface_assign(
struct gl_shader_parser *glsp, struct shader_var *var,
const char *src)
{
/* vertex shaders: write gl_Position */
(API Change) Improve graphics API consistency Summary: - Prefix all graphics subsystem names with gs_ or GS_ - Unsquish funciton names (for example _setfloat to _set_float) - Changed create functions to be more consistent with the rest of the API elsewhere. For exmaple, instead of gs_create_texture/gs_texture_destroy, it's now gs_texture_create/gs_texture_destroy - Renamed gs_stencil_op enum to gs_stencil_op_type From: To: ----------------------------------------------------------- tvertarray gs_tvertarray vb_data gs_vb_data vbdata_create gs_vbdata_create vbdata_destroy gs_vbdata_destroy shader_param gs_shader_param gs_effect gs_effect effect_technique gs_effect_technique effect_pass gs_effect_pass effect_param gs_effect_param texture_t gs_texture_t stagesurf_t gs_stagesurf_t zstencil_t gs_zstencil_t vertbuffer_t gs_vertbuffer_t indexbuffer_t gs_indexbuffer_t samplerstate_t gs_samplerstate_t swapchain_t gs_swapchain_t texrender_t gs_texrender_t shader_t gs_shader_t sparam_t gs_sparam_t effect_t gs_effect_t technique_t gs_technique_t eparam_t gs_eparam_t device_t gs_device_t graphics_t graphics_t shader_param_type gs_shader_param_type SHADER_PARAM_UNKNOWN GS_SHADER_PARAM_UNKNOWN SHADER_PARAM_BOOL GS_SHADER_PARAM_BOOL SHADER_PARAM_FLOAT GS_SHADER_PARAM_FLOAT SHADER_PARAM_INT GS_SHADER_PARAM_INT SHADER_PARAM_STRING GS_SHADER_PARAM_STRING SHADER_PARAM_VEC2 GS_SHADER_PARAM_VEC2 SHADER_PARAM_VEC3 GS_SHADER_PARAM_VEC3 SHADER_PARAM_VEC4 GS_SHADER_PARAM_VEC4 SHADER_PARAM_MATRIX4X4 GS_SHADER_PARAM_MATRIX4X4 SHADER_PARAM_TEXTURE GS_SHADER_PARAM_TEXTURE shader_param_info gs_shader_param_info shader_type gs_shader_type SHADER_VERTEX GS_SHADER_VERTEX SHADER_PIXEL GS_SHADER_PIXEL shader_destroy gs_shader_destroy shader_numparams gs_shader_get_num_params shader_getparambyidx gs_shader_get_param_by_idx shader_getparambyname gs_shader_get_param_by_name shader_getviewprojmatrix gs_shader_get_viewproj_matrix shader_getworldmatrix gs_shader_get_world_matrix shader_getparaminfo gs_shader_get_param_info shader_setbool gs_shader_set_bool shader_setfloat gs_shader_set_float shader_setint gs_shader_set_int shader_setmatrix3 gs_shader_setmatrix3 shader_setmatrix4 gs_shader_set_matrix4 shader_setvec2 gs_shader_set_vec2 shader_setvec3 gs_shader_set_vec3 shader_setvec4 gs_shader_set_vec4 shader_settexture gs_shader_set_texture shader_setval gs_shader_set_val shader_setdefault gs_shader_set_default effect_property_type gs_effect_property_type EFFECT_NONE GS_EFFECT_NONE EFFECT_BOOL GS_EFFECT_BOOL EFFECT_FLOAT GS_EFFECT_FLOAT EFFECT_COLOR GS_EFFECT_COLOR EFFECT_TEXTURE GS_EFFECT_TEXTURE effect_param_info gs_effect_param_info effect_destroy gs_effect_destroy effect_gettechnique gs_effect_get_technique technique_begin gs_technique_begin technique_end gs_technique_end technique_beginpass gs_technique_begin_pass technique_beginpassbyname gs_technique_begin_pass_by_name technique_endpass gs_technique_end_pass effect_numparams gs_effect_get_num_params effect_getparambyidx gs_effect_get_param_by_idx effect_getparambyname gs_effect_get_param_by_name effect_updateparams gs_effect_update_params effect_getviewprojmatrix gs_effect_get_viewproj_matrix effect_getworldmatrix gs_effect_get_world_matrix effect_getparaminfo gs_effect_get_param_info effect_setbool gs_effect_set_bool effect_setfloat gs_effect_set_float effect_setint gs_effect_set_int effect_setmatrix4 gs_effect_set_matrix4 effect_setvec2 gs_effect_set_vec2 effect_setvec3 gs_effect_set_vec3 effect_setvec4 gs_effect_set_vec4 effect_settexture gs_effect_set_texture effect_setval gs_effect_set_val effect_setdefault gs_effect_set_default texrender_create gs_texrender_create texrender_destroy gs_texrender_destroy texrender_begin gs_texrender_begin texrender_end gs_texrender_end texrender_reset gs_texrender_reset texrender_gettexture gs_texrender_get_texture GS_BUILDMIPMAPS GS_BUILD_MIPMAPS GS_RENDERTARGET GS_RENDER_TARGET gs_device_name gs_get_device_name gs_device_type gs_get_device_type gs_entercontext gs_enter_context gs_leavecontext gs_leave_context gs_getcontext gs_get_context gs_renderstart gs_render_start gs_renderstop gs_render_stop gs_rendersave gs_render_save gs_getinput gs_get_input gs_geteffect gs_get_effect gs_create_effect_from_file gs_effect_create_from_file gs_create_effect gs_effect_create gs_create_vertexshader_from_file gs_vertexshader_create_from_file gs_create_pixelshader_from_file gs_pixelshader_create_from_file gs_create_texture_from_file gs_texture_create_from_file gs_resetviewport gs_reset_viewport gs_set2dmode gs_set_2d_mode gs_set3dmode gs_set_3d_mode gs_create_swapchain gs_swapchain_create gs_getsize gs_get_size gs_getwidth gs_get_width gs_getheight gs_get_height gs_create_texture gs_texture_create gs_create_cubetexture gs_cubetexture_create gs_create_volumetexture gs_voltexture_create gs_create_zstencil gs_zstencil_create gs_create_stagesurface gs_stagesurface_create gs_create_samplerstate gs_samplerstate_create gs_create_vertexshader gs_vertexshader_create gs_create_pixelshader gs_pixelshader_create gs_create_vertexbuffer gs_vertexbuffer_create gs_create_indexbuffer gs_indexbuffer_create gs_gettexturetype gs_get_texture_type gs_load_defaultsamplerstate gs_load_default_samplerstate gs_getvertexshader gs_get_vertex_shader gs_getpixelshader gs_get_pixel_shader gs_getrendertarget gs_get_render_target gs_getzstenciltarget gs_get_zstencil_target gs_setrendertarget gs_set_render_target gs_setcuberendertarget gs_set_cube_render_target gs_beginscene gs_begin_scene gs_draw gs_draw gs_endscene gs_end_scene gs_setcullmode gs_set_cull_mode gs_getcullmode gs_get_cull_mode gs_enable_depthtest gs_enable_depth_test gs_enable_stenciltest gs_enable_stencil_test gs_enable_stencilwrite gs_enable_stencil_write gs_blendfunction gs_blend_function gs_depthfunction gs_depth_function gs_stencilfunction gs_stencil_function gs_stencilop gs_stencil_op gs_setviewport gs_set_viewport gs_getviewport gs_get_viewport gs_setscissorrect gs_set_scissor_rect gs_create_texture_from_iosurface gs_texture_create_from_iosurface gs_create_gdi_texture gs_texture_create_gdi gs_is_compressed_format gs_is_compressed_format gs_num_total_levels gs_get_total_levels texture_setimage gs_texture_set_image cubetexture_setimage gs_cubetexture_set_image swapchain_destroy gs_swapchain_destroy texture_destroy gs_texture_destroy texture_getwidth gs_texture_get_width texture_getheight gs_texture_get_height texture_getcolorformat gs_texture_get_color_format texture_map gs_texture_map texture_unmap gs_texture_unmap texture_isrect gs_texture_is_rect texture_getobj gs_texture_get_obj cubetexture_destroy gs_cubetexture_destroy cubetexture_getsize gs_cubetexture_get_size cubetexture_getcolorformat gs_cubetexture_get_color_format volumetexture_destroy gs_voltexture_destroy volumetexture_getwidth gs_voltexture_get_width volumetexture_getheight gs_voltexture_get_height volumetexture_getdepth gs_voltexture_getdepth volumetexture_getcolorformat gs_voltexture_get_color_format stagesurface_destroy gs_stagesurface_destroy stagesurface_getwidth gs_stagesurface_get_width stagesurface_getheight gs_stagesurface_get_height stagesurface_getcolorformat gs_stagesurface_get_color_format stagesurface_map gs_stagesurface_map stagesurface_unmap gs_stagesurface_unmap zstencil_destroy gs_zstencil_destroy samplerstate_destroy gs_samplerstate_destroy vertexbuffer_destroy gs_vertexbuffer_destroy vertexbuffer_flush gs_vertexbuffer_flush vertexbuffer_getdata gs_vertexbuffer_get_data indexbuffer_destroy gs_indexbuffer_destroy indexbuffer_flush gs_indexbuffer_flush indexbuffer_getdata gs_indexbuffer_get_data indexbuffer_numindices gs_indexbuffer_get_num_indices indexbuffer_gettype gs_indexbuffer_get_type texture_rebind_iosurface gs_texture_rebind_iosurface texture_get_dc gs_texture_get_dc texture_release_dc gs_texture_release_dc
2014-08-07 23:42:07 -07:00
if (glsp->type == GS_SHADER_VERTEX &&
strcmp(var->mapping, "POSITION") == 0) {
dstr_cat(&glsp->gl_string, "\tgl_Position = ");
dstr_cat(&glsp->gl_string, src);
dstr_cat(&glsp->gl_string, var->name);
dstr_cat(&glsp->gl_string, ";\n");
}
}
static void gl_write_main_storage_assign(struct gl_shader_parser *glsp,
struct shader_var *var, const char *dst, const char *src,
bool input)
{
struct shader_struct *st;
struct dstr dst_copy = {0};
char ch_left = input ? '.' : '_';
char ch_right = input ? '_' : '.';
if (dst) {
dstr_copy(&dst_copy, dst);
dstr_cat_ch(&dst_copy, ch_left);
} else {
dstr_copy(&dst_copy, "\t");
}
dstr_cat(&dst_copy, var->name);
st = shader_parser_getstruct(&glsp->parser, var->type);
if (st) {
struct dstr src_copy = {0};
size_t i;
if (src)
dstr_copy(&src_copy, src);
dstr_cat(&src_copy, var->name);
dstr_cat_ch(&src_copy, ch_right);
for (i = 0; i < st->vars.num; i++) {
struct shader_var *st_var = st->vars.array+i;
gl_write_main_storage_assign(glsp, st_var,
dst_copy.array, src_copy.array, input);
}
dstr_free(&src_copy);
} else {
if (!dstr_is_empty(&dst_copy))
dstr_cat_dstr(&glsp->gl_string, &dst_copy);
dstr_cat(&glsp->gl_string, " = ");
if (src)
dstr_cat(&glsp->gl_string, src);
dstr_cat(&glsp->gl_string, var->name);
dstr_cat(&glsp->gl_string, ";\n");
if (!input)
gl_write_main_interface_assign(glsp, var, src);
}
dstr_free(&dst_copy);
}
static inline void gl_write_main_storage_inputs(struct gl_shader_parser *glsp,
struct shader_func *main)
{
gl_write_main_storage_assign(glsp, main->params.array, NULL,
"inputval_", true);
}
static inline void gl_write_main_storage_outputs(struct gl_shader_parser *glsp,
struct shader_func *main)
{
/* we only do this *if* we're writing a struct, because otherwise
* the call to 'main' already does the assignment for us */
if (!main->mapping) {
struct shader_var var = {0};
var.name = "outputval";
var.type = (char*)main->return_type;
dstr_cat(&glsp->gl_string, "\n");
gl_write_main_storage_assign(glsp, &var, NULL, NULL, false);
}
}
static inline void gl_write_main_vars(struct gl_shader_parser *glsp,
struct shader_func *main_func)
{
size_t i;
for (i = 0; i < main_func->params.num; i++) {
dstr_cat(&glsp->gl_string, "\t");
dstr_cat(&glsp->gl_string, main_func->params.array[i].type);
dstr_cat(&glsp->gl_string, " ");
dstr_cat(&glsp->gl_string, main_func->params.array[i].name);
dstr_cat(&glsp->gl_string, ";\n");
}
if (!main_func->mapping) {
dstr_cat(&glsp->gl_string, "\t");
dstr_cat(&glsp->gl_string, main_func->return_type);
dstr_cat(&glsp->gl_string, " outputval;\n\n");
}
}
static inline void gl_write_main_func_call(struct gl_shader_parser *glsp,
struct shader_func *main_func)
{
size_t i;
dstr_cat(&glsp->gl_string, "\n\toutputval = _main_wrap(");
for (i = 0; i < main_func->params.num; i++) {
if (i)
dstr_cat(&glsp->gl_string, ", ");
dstr_cat(&glsp->gl_string, main_func->params.array[i].name);
}
dstr_cat(&glsp->gl_string, ");\n");
}
static void gl_write_main(struct gl_shader_parser *glsp,
struct shader_func *main)
{
dstr_cat(&glsp->gl_string, "void main(void)\n{\n");
gl_write_main_vars(glsp, main);
gl_write_main_storage_inputs(glsp, main);
gl_write_main_func_call(glsp, main);
gl_write_main_storage_outputs(glsp, main);
dstr_cat(&glsp->gl_string, "}\n");
}
/* ugh, don't ask. I'll probably get rid of the need for this function later */
static void gl_rename_attributes(struct gl_shader_parser *glsp)
{
size_t i = 0, input_idx = 0, output_idx = 0;
for (i = 0; i < glsp->attribs.num; i++) {
struct gl_parser_attrib *attrib = glsp->attribs.array+i;
struct dstr new_name = {0};
const char *prefix;
size_t val;
if (attrib->input) {
prefix = glsp->input_prefix;
val = input_idx++;
} else {
prefix = glsp->output_prefix;
val = output_idx++;
}
dstr_printf(&new_name, "%s%u", prefix, (unsigned int)val);
dstr_replace(&glsp->gl_string, attrib->name.array,
new_name.array);
dstr_move(&attrib->name, &new_name);
}
}
static bool gl_shader_buildstring(struct gl_shader_parser *glsp)
{
struct shader_func *main_func;
main_func = shader_parser_getfunc(&glsp->parser, "main");
if (!main_func) {
blog(LOG_ERROR, "function 'main' not found");
return false;
}
dstr_copy(&glsp->gl_string, "#version 150\n\n");
gl_write_params(glsp);
gl_write_inputs(glsp, main_func);
gl_write_outputs(glsp, main_func);
gl_write_interface_block(glsp);
gl_write_structs(glsp);
gl_write_functions(glsp);
gl_write_main(glsp, main_func);
gl_rename_attributes(glsp);
return true;
}
bool gl_shader_parse(struct gl_shader_parser *glsp,
const char *shader_str, const char *file)
{
bool success = shader_parse(&glsp->parser, shader_str, file);
char *str = shader_parser_geterrors(&glsp->parser);
if (str) {
blog(LOG_WARNING, "Shader parser errors/warnings:\n%s\n", str);
bfree(str);
}
if (success)
success = gl_shader_buildstring(glsp);
return success;
}