From 5a44148f12064ce0071aba0fe86f8380f8e4c17c Mon Sep 17 00:00:00 2001 From: aignacio_sf <> Date: Fri, 16 Jun 2006 23:06:39 +0000 Subject: [PATCH] Add debug code to print out shader asm code in debug mode. Check for errors when the Cg generated shader code is loaded. --- panda/src/dxgsg9/dxShaderContext9.cxx | 180 +++++++++++++++++++++----- panda/src/dxgsg9/dxShaderContext9.h | 2 +- 2 files changed, 150 insertions(+), 32 deletions(-) diff --git a/panda/src/dxgsg9/dxShaderContext9.cxx b/panda/src/dxgsg9/dxShaderContext9.cxx index bbe9c89c55..1ce6881b01 100644 --- a/panda/src/dxgsg9/dxShaderContext9.cxx +++ b/panda/src/dxgsg9/dxShaderContext9.cxx @@ -26,6 +26,8 @@ #include #include +#define DEBUG_SHADER 0 + TypeHandle CLP(ShaderContext)::_type_handle; static char *vertex_shader_function_name = "vshader"; @@ -116,7 +118,7 @@ CLP(ShaderContext)(ShaderExpansion *s, GSG *gsg) : ShaderContext(s) { // Parse any directives in the source. // IGNORE SPECIFIC PROFILES IN DX - if (false) { + if (!false) { string directive; while (!s->parse_eof()) { s->parse_line(directive, true, true); @@ -226,7 +228,7 @@ suggest_cg_profile(const string &vpro, const string &fpro) return; } -// NO EQUIVALENT FUNCTIONALITY IN DX +// NO EQUIVALENT FUNCTIONALITY FROM GL TO DX /* // If the suggestion is parseable, but not supported, ignore silently. if ((!cgGLIsProfileSupported(_cg_profile[SHADER_type_vert]))|| @@ -249,21 +251,27 @@ suggest_cg_profile(const string &vpro, const string &fpro) CGprofile CLP(ShaderContext):: parse_cg_profile(const string &id, bool vertex) { - int nvprofiles = 4; - int nfprofiles = 4; - CGprofile vprofiles[] = { CG_PROFILE_ARBVP1, CG_PROFILE_VP20, CG_PROFILE_VP30, CG_PROFILE_VP40 }; - CGprofile fprofiles[] = { CG_PROFILE_ARBFP1, CG_PROFILE_FP20, CG_PROFILE_FP30, CG_PROFILE_FP40 }; + int i = 0; + CGprofile vprofiles[] = { CG_PROFILE_ARBVP1, CG_PROFILE_VP20, CG_PROFILE_VP30, CG_PROFILE_VP40, CG_PROFILE_UNKNOWN }; + CGprofile fprofiles[] = { CG_PROFILE_ARBFP1, CG_PROFILE_FP20, CG_PROFILE_FP30, CG_PROFILE_FP40, CG_PROFILE_UNKNOWN }; + + // near equivalent DX profiles + CGprofile dx_vprofiles[] = { CG_PROFILE_VS_2_0, CG_PROFILE_VS_1_1, CG_PROFILE_VS_2_X, CG_PROFILE_VS_3_0, CG_PROFILE_UNKNOWN }; + CGprofile dx_fprofiles[] = { CG_PROFILE_PS_2_0, CG_PROFILE_PS_1_1, CG_PROFILE_PS_2_X, CG_PROFILE_PS_3_0, CG_PROFILE_UNKNOWN }; + if (vertex) { - for (int i=0; i _cg_context != 0) { @@ -505,24 +547,35 @@ bind(GSG *gsg) { if (_cg_shader) { // Bind the shaders. + bind_state = true; hr = cgD3D9BindProgram(_cg_program[SHADER_type_vert]); if (FAILED (hr)) { - dxgsg9_cat.error() << "cgD3D9BindProgram vertex shader failed\n"; - } - hr = cgD3D9BindProgram(_cg_program[SHADER_type_frag]); - if (FAILED (hr)) { - dxgsg9_cat.error() << "cgD3D9BindProgram pixel shader failed\n"; + dxgsg9_cat.error() << "cgD3D9BindProgram vertex shader failed " << D3DERRORSTRING(hr); CGerror error = cgGetError(); if (error != CG_NO_ERROR) { dxgsg9_cat.error() << " CG ERROR: " << cgGetErrorString(error) << "\n"; } + + bind_state = false; + } + hr = cgD3D9BindProgram(_cg_program[SHADER_type_frag]); + if (FAILED (hr)) { + dxgsg9_cat.error() << "cgD3D9BindProgram pixel shader failed " << D3DERRORSTRING(hr); + + CGerror error = cgGetError(); + if (error != CG_NO_ERROR) { + dxgsg9_cat.error() << " CG ERROR: " << cgGetErrorString(error) << "\n"; + } + + bind_state = false; } } } } #endif + return bind_state; } //////////////////////////////////////////////////////////////////// @@ -569,6 +622,14 @@ unbind(GSG *gsg) { // state has changed except the external and internal // transforms. //////////////////////////////////////////////////////////////////// + +#if DEBUG_SHADER +float *global_data = 0; +ShaderContext::ShaderMatSpec *global_shader_mat_spec = 0; +InternalName *global_internal_name_0 = 0; +InternalName *global_internal_name_1 = 0; +#endif + void CLP(ShaderContext):: issue_parameters(GSG *gsg, bool altered) { @@ -589,11 +650,20 @@ issue_parameters(GSG *gsg, bool altered) data = val -> get_data ( ); + #if DEBUG_SHADER + // DEBUG + global_data = (float *) data; + global_shader_mat_spec = &_mat_spec[i]; + global_internal_name_0 = global_shader_mat_spec -> _arg [0]; + global_internal_name_1 = global_shader_mat_spec -> _arg [1]; + #endif + switch (_mat_spec[i]._piece) { case SMP_whole: // TRANSPOSE REQUIRED temp_matrix.transpose_from (*val); data = temp_matrix.get_data(); + hr = cgD3D9SetUniform (p, data); DBG_SH2 @@ -648,7 +718,15 @@ issue_parameters(GSG *gsg, bool altered) } if (FAILED (hr)) { + + string name = "unnamed"; + + if (_mat_spec[i]._arg [0]) { + name = _mat_spec[i]._arg [0] -> get_basename ( ); + } + dxgsg9_cat.error() + << "NAME " << name << "\n" << "MAT TYPE " << _mat_spec[i]._piece << " cgD3D9SetUniform failed " @@ -697,6 +775,12 @@ disable_shader_vertex_arrays(GSG *gsg) // it may unnecessarily disable arrays then immediately // reenable them. We may optimize this someday. //////////////////////////////////////////////////////////////////// + +// DEBUG +#if DEBUG_SHADER +VertexElementArray *global_vertex_element_array = 0; +#endif + void CLP(ShaderContext):: update_shader_vertex_arrays(CLP(ShaderContext) *prev, GSG *gsg) { @@ -727,6 +811,11 @@ update_shader_vertex_arrays(CLP(ShaderContext) *prev, GSG *gsg) stream_index = 0; vertex_element_array = new VertexElementArray (nvarying + 2); + #if DEBUG_SHADER + // DEBUG + global_vertex_element_array = vertex_element_array; + #endif + for (int i=0; i GetBufferPointer ( )); + if (error_message) + { + dxgsg9_cat.error() << error_message; + } + + error_messages -> Release ( ); + } +} diff --git a/panda/src/dxgsg9/dxShaderContext9.h b/panda/src/dxgsg9/dxShaderContext9.h index 0f819fc1ff..fe49012c6e 100644 --- a/panda/src/dxgsg9/dxShaderContext9.h +++ b/panda/src/dxgsg9/dxShaderContext9.h @@ -77,7 +77,7 @@ public: ~CLP(ShaderContext)(); INLINE bool valid(GSG *gsg); - void bind(GSG *gsg); + bool bind(GSG *gsg); void unbind(GSG *gsg); void issue_parameters(GSG *gsg, bool altered);