Forráskód Böngészése

Adding the shader generator

Josh Yelon 18 éve
szülő
commit
b3909c97fa

+ 7 - 6
panda/src/dxgsg9/dxGraphicsStateGuardian9.cxx

@@ -3157,13 +3157,10 @@ do_issue_shader() {
   DBG_SH3  dxgsg9_cat.debug ( ) << "SHADER: do_issue_shader\n"; DBG_E
 
   CLP(ShaderContext) *context = 0;
-  const ShaderAttrib *attr = _target_rs->get_generated_shader();
-  if (attr == 0) {
-    attr = _target._shader;
-    // if (attr is auto) then generate a shader.
-    // store that shader using set_generated_shader.
+  Shader *shader = 0;
+  if (_target._shader) {
+    shader = (Shader *)(_target._shader->get_shader());
   }
-  Shader *shader = (Shader *)(attr->get_shader());
   if (shader) {
     context = (CLP(ShaderContext) *)(shader->prepare_now(get_prepared_objects(), this));
   }
@@ -3447,6 +3444,10 @@ set_state_and_transform(const RenderState *target,
   target->store_into_slots(&_target);
   _state_rs = 0;
 
+  if (_target._shader && (_target._shader->auto_shader())) {
+    _target._shader = _target_rs->get_generated_shader();
+  }
+  
   if (_target._alpha_test != _state._alpha_test) {
     do_issue_alpha_test();
     _state._alpha_test = _target._alpha_test;

+ 1 - 0
panda/src/dxgsg9/dxShaderContext9.cxx

@@ -728,6 +728,7 @@ update_shader_texture_bindings(CLP(ShaderContext) *prev, GSG *gsg)
 
 #ifdef HAVE_CG
   if (_cg_context) {
+
     for (int i=0; i<(int)_shader->_tex_spec.size(); i++) {
       CGparameter p = _cg_parameter_map[_shader->_tex_spec[i]._id._seqno];
       if (p == NULL) {

+ 7 - 6
panda/src/glstuff/glGraphicsStateGuardian_src.cxx

@@ -3529,13 +3529,10 @@ do_issue_shade_model() {
 void CLP(GraphicsStateGuardian)::
 do_issue_shader() {
   CLP(ShaderContext) *context = 0;
-  const ShaderAttrib *attr = _target_rs->get_generated_shader();
-  if (attr == 0) {
-    attr = _target._shader;
-    // if (attr is auto) then generate a shader.
-    // store that shader using set_generated_shader.
+  Shader *shader = 0;
+  if (_target._shader) {
+    shader = (Shader *)(_target._shader->get_shader());
   }
-  Shader *shader = (Shader *)(attr->get_shader());
   if (shader) {
     context = (CLP(ShaderContext) *)(shader->prepare_now(get_prepared_objects(), this));
   }
@@ -5840,6 +5837,10 @@ set_state_and_transform(const RenderState *target,
   _target.clear_to_defaults();
   target->store_into_slots(&_target);
   _state_rs = 0;
+
+  if (_target._shader && (_target._shader->auto_shader())) {
+    _target._shader = _target_rs->get_generated_shader();
+  }
   
   if (_target._alpha_test != _state._alpha_test) {
     PStatTimer timer(_draw_set_state_alpha_test_pcollector);

+ 4 - 2
panda/src/gobj/shader.cxx

@@ -23,6 +23,8 @@
 
 #ifdef HAVE_CG
 #include "Cg/cg.h"
+#define JCG_PROFILE_GLSLV ((CGprofile)7007)
+#define JCG_PROFILE_GLSLF ((CGprofile)7008)
 #endif
 
 TypeHandle Shader::_type_handle;
@@ -1171,8 +1173,8 @@ Shader(const Filename &filename, const string &text) :
   if (_default_caps._ultimate_vprofile == 0) {
     _default_caps._active_vprofile = CG_PROFILE_UNKNOWN;
     _default_caps._active_fprofile = CG_PROFILE_UNKNOWN;
-    _default_caps._ultimate_vprofile = CG_PROFILE_VS_3_0;
-    _default_caps._ultimate_fprofile = CG_PROFILE_PS_3_0;
+    _default_caps._ultimate_vprofile = JCG_PROFILE_GLSLV;
+    _default_caps._ultimate_fprofile = JCG_PROFILE_GLSLF;
   }
   if (_header == "//Cg") {
     if (cg_analyze_shader(_default_caps)) {

+ 35 - 0
panda/src/pgraph/shaderGenerator.I

@@ -0,0 +1,35 @@
+// Filename: shaderGenerator.I
+// Created by: jyelon (15Dec07)
+//
+////////////////////////////////////////////////////////////////////
+//
+// PANDA 3D SOFTWARE
+// Copyright (c) 2001 - 2004, Disney Enterprises, Inc.  All rights reserved
+//
+// All use of this software is subject to the terms of the Panda 3d
+// Software license.  You should have received a copy of this license
+// along with this source code; you will also find a current copy of
+// the license at http://etc.cmu.edu/panda3d/docs/license/ .
+//
+// To contact the maintainers of this program write to
+// [email protected] .
+//
+////////////////////////////////////////////////////////////////////
+
+////////////////////////////////////////////////////////////////////
+//     Function: ShaderGenerator::Constructor
+//       Access: Private
+//  Description: This class is never constructed.
+////////////////////////////////////////////////////////////////////
+ShaderGenerator::
+ShaderGenerator() {
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: ShaderGenerator::Destructor
+//       Access: Private
+//  Description: This class is never constructed.
+////////////////////////////////////////////////////////////////////
+ShaderGenerator::
+~ShaderGenerator() {
+}

+ 69 - 0
panda/src/pgraph/shaderGenerator.cxx

@@ -0,0 +1,69 @@
+// Filename: shaderGenerator.cxx
+// Created by: jyelon (15Dec07)
+//
+////////////////////////////////////////////////////////////////////
+//
+// PANDA 3D SOFTWARE
+// Copyright (c) 2001 - 2004, Disney Enterprises, Inc.  All rights reserved
+//
+// All use of this software is subject to the terms of the Panda 3d
+// Software license.  You should have received a copy of this license
+// along with this source code; you will also find a current copy of
+// the license at http://etc.cmu.edu/panda3d/docs/license/ .
+//
+// To contact the maintainers of this program write to
+// [email protected] .
+//
+////////////////////////////////////////////////////////////////////
+
+#include "renderState.h"
+#include "shaderAttrib.h"
+#include "shaderGenerator.h"
+
+////////////////////////////////////////////////////////////////////
+//     Function: ShaderGenerator::synthesize_shader
+//       Access: Public, Static
+//  Description: This is the routine that implements the next-gen
+//               fixed function pipeline by synthesizing a shader.
+////////////////////////////////////////////////////////////////////
+CPT(RenderAttrib) ShaderGenerator::
+synthesize_shader(const RenderState *rs) {
+
+  // Stub code: just return this shader.
+  
+  PT(Shader) shader = Shader::make(
+    "//Cg \n"
+    "// \n"
+    "//Cg profile arbvp1 arbfp1 \n"
+    " \n"
+    "void vshader(float4 vtx_position   : POSITION, \n"
+    "             float3 vtx_normal     : NORMAL, \n"
+    "             float4 vtx_color      : COLOR, \n"
+    "             out float4 l_position : POSITION, \n"
+    "             out float4 l_brite    : TEXCOORD0, \n"
+    "             out float4 l_color    : COLOR, \n"
+    "             uniform float4 mspos_light, \n"
+    "             uniform float4x4 mat_modelproj) \n"
+    "{ \n"
+    "  l_position = mul(mat_modelproj, vtx_position); \n"
+    "  float3 N = normalize(vtx_normal); \n"
+    "  float3 lightVector = normalize(mspos_light - vtx_position); \n"
+    "  l_brite = max(dot(N,lightVector), 0); \n"
+    "  l_color = vtx_color; \n"
+    "} \n"
+    " \n"
+    " \n"
+    "void fshader(float4 l_brite     : TEXCOORD0,  \n"
+    "             float4 l_color     : COLOR, \n"
+    "             out float4 o_color : COLOR) \n"
+    "{ \n"
+    "  if (l_brite.x<0.5) l_brite=0.8; \n"
+    "  else l_brite=1.2; \n"
+    "  o_color=l_brite * l_color; \n"
+    "} \n"
+    );
+  CPT(RenderAttrib) rattr = rs->get_shader();
+  if (rattr == 0) rattr = ShaderAttrib::make();
+  return DCAST(ShaderAttrib, rattr)->set_shader(shader);
+}
+

+ 64 - 0
panda/src/pgraph/shaderGenerator.h

@@ -0,0 +1,64 @@
+// Filename: shaderGenerator.h
+// Created by: jyelon (15Dec07)
+//
+////////////////////////////////////////////////////////////////////
+//
+// PANDA 3D SOFTWARE
+// Copyright (c) 2001 - 2004, Disney Enterprises, Inc.  All rights reserved
+//
+// All use of this software is subject to the terms of the Panda 3d
+// Software license.  You should have received a copy of this license
+// along with this source code; you will also find a current copy of
+// the license at http://etc.cmu.edu/panda3d/docs/license/ .
+//
+// To contact the maintainers of this program write to
+// [email protected] .
+//
+////////////////////////////////////////////////////////////////////
+
+#ifndef SHADERGENERATOR_H
+#define SHADERGENERATOR_H
+
+#include "pandabase.h"
+
+////////////////////////////////////////////////////////////////////
+//       Class : ShaderGenerator
+// Description : The ShaderGenerator is a device that effectively
+//               replaces the classic fixed function pipeline with
+//               a 'next-gen' fixed function pipeline.  The next-gen
+//               fixed function pipeline supports features like 
+//               normal mapping, gloss mapping, cartoon lighting,
+//               and so forth.  It works by automatically generating
+//               a shader from a given RenderState.
+//
+//               The ShaderGenerator owes its existence to the 
+//               'Bamboo Team' at Carnegie Mellon's Entertainment
+//               Technology Center.  This is a group of students
+//               who, as a semester project, decided that next-gen
+//               graphics should be accessible to everyone, even if
+//               they don't know shader programming.  The group 
+//               consisted of:
+//
+//               Aaron Lo, Programmer
+//               Heegun Lee, Programmer
+//               Erin Fernandez, Artist/Tester
+//               Joe Grubb, Artist/Tester
+//               Ivan Ortega, Technical Artist/Tester
+//
+//               Thanks to them!
+//
+////////////////////////////////////////////////////////////////////
+
+class EXPCL_PANDA_PGRAPH ShaderGenerator {
+private:
+  INLINE ShaderGenerator();
+  INLINE ~ShaderGenerator();
+  
+public:
+  static CPT(RenderAttrib) synthesize_shader(const RenderState *rs);
+};
+
+#include "shaderGenerator.I"
+
+#endif  // SHADERGENERATOR_H
+