Advertisement
Guest User

new_shader_from_object.txt

a guest
May 16th, 2017
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.48 KB | None | 0 0
  1. Engine\source\ts\tsRenderState.h
  2.  
  3. class TSRenderState
  4. {
  5. protected: //test_material
  6. SimObjectId mCreatorObjectId; //test_material
  7. public: //test_material
  8. const SimObjectId &getCOI() const { return mCreatorObjectId; } //test_material
  9. void setCOI(const SimObjectId &val) { mCreatorObjectId = val; } //test_material
  10.  
  11. Engine\source\ts\tsRenderState.cpp (TSRenderState::TSRenderState())
  12. change
  13.  
  14. mAccuTex( NULL )
  15.  
  16. to:
  17.  
  18. mAccuTex( NULL ),
  19. mCreatorObjectId(0) //test_material Note: delete previous comma if we delete this
  20.  
  21. Engine\source\T3D\shapeBase.cpp (in void ShapeBase::prepBatchRender( after rdata.setFadeOverride(...);)
  22.  
  23. rdata.setCOI(getId()); //test_material
  24.  
  25. Engine\source\T3D\tsStatic.cpp (in void TSStatic::prepRenderImage( after rdata.setOriginSort( mUseOriginSort );)
  26.  
  27. rdata.setCOI(getId()); //test_material
  28.  
  29. Engine\source\renderInstance\renderPassManager.h (inside struct MeshRenderInst : public RenderInst add)
  30.  
  31. SimObjectId creatorObjectId; //test_material
  32.  
  33. Engine\source\renderInstance\renderPassManager.cpp (in void MeshRenderInst::clear() )
  34.  
  35. creatorObjectId = 0; //test_material
  36.  
  37. Engine\source\ts\tsMesh.cpp (in void TSMesh::innerRender( TSMaterialList *materials, const TSRenderState &rdata, TSVertexBufferHandle &vb, GFXPrimitiveBufferHandle &pb )
  38. careful, there is another TSMesh::innerRender. We must use the correct one.
  39. Then, after coreRI->type = RenderPassManager::RIT_Mesh;)
  40.  
  41. coreRI->creatorObjectId = rdata.getCOI(); //test_material
  42.  
  43. Engine\source\materials\sceneData.h (in struct SceneData{ after F32 visibility;)
  44.  
  45. SimObjectId creatorObjectId; //test_material
  46.  
  47. Engine\source\renderInstance\renderBinManager.cpp in (void RenderBinManager::setupSGData( )
  48.  
  49. data.creatorObjectId = ri->creatorObjectId; //test_material
  50.  
  51. Engine\source\materials\processedShaderMaterial.cpp
  52. 1st- #include our custom .h (we have there DeclareFeatureType( MFT_CustomShaderObject );)
  53. 2nd- In "void ProcessedShaderMaterial::_determineFeatures( " before "// Allow features to add themselves." add:
  54.  
  55. fd.features.addFeature( MFT_CustomShaderObject ); //test_material //to do: add a conditional
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62. ------------------------------------------------------------------------
  63. customShaderObject.h
  64. ------------------------------------------------------------------------
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71. #ifndef _CUSTOM_SHADERGEN_OBJECT_H_
  72. #define _CUSTOM_SHADERGEN_OBJECT_H_
  73.  
  74. #ifndef _SHADERGEN_H_
  75. #include "shaderGen/shaderGen.h"
  76. #endif
  77. #ifndef _FEATURETYPE_H_
  78. #include "materials/materialFeatureTypes.h"
  79. #endif
  80.  
  81. class GFXShaderConstHandle;
  82.  
  83. class CustomShaderObjectConstHandles : public ShaderFeatureConstHandles
  84. {
  85. public:
  86.  
  87. GFXShaderConstHandle *mAlphaMultiplier;
  88.  
  89. // ShaderFeatureConstHandles
  90. virtual void init( GFXShader *shader );
  91. virtual void setConsts( SceneRenderState *state,
  92. const SceneData &sgData,
  93. GFXShaderConstBuffer *buffer );
  94. };
  95.  
  96. //----------------------------------------------------------------------
  97.  
  98. #ifndef _SHADERGEN_HLSL_SHADERFEATUREHLSL_H_
  99. #include "shaderGen/HLSL/shaderFeatureHLSL.h"
  100. #endif
  101.  
  102. class CustomShaderObjectHLSL : public ShaderFeatureHLSL
  103. {
  104. protected:
  105.  
  106. //ShaderIncludeDependency mDep;
  107.  
  108. public:
  109.  
  110. CustomShaderObjectHLSL();
  111.  
  112. //virtual void processVert( Vector<ShaderComponent*> &componentList, const MaterialFeatureData &fd );
  113.  
  114. virtual void processPix( Vector<ShaderComponent*> &componentList, const MaterialFeatureData &fd );
  115.  
  116. virtual String getName() { return "CustomShaderObject"; }
  117.  
  118. virtual void determineFeature( Material *material,
  119. const GFXVertexFormat *vertexFormat,
  120. U32 stageNum,
  121. const FeatureType &type,
  122. const FeatureSet &features,
  123. MaterialFeatureData *outFeatureData );
  124.  
  125. virtual ShaderFeatureConstHandles* createConstHandles( GFXShader *shader, SimObject *userObject );
  126. };
  127.  
  128. //----------------------------------------------------------------------
  129.  
  130. DeclareFeatureType( MFT_CustomShaderObject );
  131.  
  132.  
  133. #endif // _CUSTOM_SHADERGEN_OBJECT_H_
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142.  
  143. ------------------------------------------------------------------------
  144. ------------------------------------------------------------------------
  145. ------------------------------------------------------------------------
  146.  
  147.  
  148.  
  149. ------------------------------------------------------------------------
  150. customShaderObject.cpp
  151. ------------------------------------------------------------------------
  152.  
  153.  
  154.  
  155.  
  156.  
  157. #include "platform/platform.h"
  158. #include "customShaderObject.h"
  159.  
  160. #include "materials/sceneData.h"
  161. #include "scene/sceneRenderState.h"
  162. #include "gfx/gfxShader.h"
  163. #include "gfx/gfxStructs.h"
  164.  
  165. #include "core/util/str.h"
  166.  
  167.  
  168. ImplementFeatureType( MFT_CustomShaderObject , MFG_PostProcess, 998.0f, true );
  169.  
  170. void CustomShaderObjectConstHandles::init( GFXShader *shader )
  171. {
  172. mAlphaMultiplier = shader->getShaderConstHandle( "$alphaMultiplier" );
  173. }
  174.  
  175. void CustomShaderObjectConstHandles::setConsts( SceneRenderState *state,
  176. const SceneData &sgData,
  177. GFXShaderConstBuffer *buffer )
  178. {
  179. PROFILE_SCOPE( CustomShaderObjectConstHandles_setConsts );
  180.  
  181. F32 alpha = 1.0f;
  182. SimObject *object = Sim::findObject( sgData.creatorObjectId );
  183. if( object )
  184. {
  185. String v = object->getDataField("cAlpha", "");
  186. if(!v.isEmpty())
  187. alpha = dAtof(v);
  188. }
  189.  
  190. buffer->setSafe( mAlphaMultiplier, alpha );
  191. }
  192.  
  193. //----------------------------------------------------------------------
  194.  
  195. #include "shaderGen/shaderGen.h"
  196. #include "shaderGen/featureMgr.h"
  197. #include "shaderGen/langElement.h"
  198. #include "shaderGen/shaderOp.h"
  199. #include "shaderGen/shaderGenVars.h"
  200. #include "core/module.h"
  201.  
  202.  
  203. static void _onRegisterFeatures( GFXAdapterType type )
  204. {
  205. if ( type == OpenGL )
  206. return;
  207.  
  208. FEATUREMGR->registerFeature( MFT_CustomShaderObject, new CustomShaderObjectHLSL );
  209.  
  210. }
  211.  
  212.  
  213. MODULE_BEGIN( CustomShaderObjectHLSL )
  214.  
  215. MODULE_INIT_AFTER( ShaderGen )
  216.  
  217. MODULE_INIT
  218. {
  219. SHADERGEN->getFeatureInitSignal().notify( _onRegisterFeatures );
  220. }
  221.  
  222. MODULE_END;
  223.  
  224.  
  225. CustomShaderObjectHLSL::CustomShaderObjectHLSL()
  226. //: mDep(String(Con::getVariable("$Core::CommonShaderPath")) + String("/wind.hlsl" ))
  227. {
  228. //addDependency( &mDep );
  229. }
  230.  
  231. void CustomShaderObjectHLSL::determineFeature( Material *material,
  232. const GFXVertexFormat *vertexFormat,
  233. U32 stageNum,
  234. const FeatureType &type,
  235. const FeatureSet &features,
  236. MaterialFeatureData *outFeatureData )
  237. {
  238. bool enabled = features.hasFeature( MFT_CustomShaderObject );
  239. outFeatureData->features.setFeature( type, enabled );
  240. }
  241.  
  242. void CustomShaderObjectHLSL::processPix( Vector<ShaderComponent*> &componentList, const MaterialFeatureData &fd )
  243. {
  244. // Find the constant value
  245. Var *alphaMult = (Var*)LangElement::find( "alphaMultiplier" );
  246. if ( !alphaMult )
  247. {
  248. alphaMult = new Var( "alphaMultiplier", "float" );
  249. alphaMult->uniform = true;
  250. alphaMult->constSortPos = cspPass;
  251. }
  252.  
  253. // Find output fragment
  254. Var *color = (Var*) LangElement::find( getOutputTargetVarName(DefaultTarget) );
  255. if ( !color )
  256. {
  257. color = new Var;
  258. color->setType( GFX->getAdapterType() == OpenGL ? "vec4" : "fragout" );
  259. color->setName( getOutputTargetVarName(DefaultTarget) );
  260.  
  261. if(GFX->getAdapterType() != OpenGL)
  262. color->setStructName( "OUT" );
  263.  
  264. output = new GenOp( "@ = float4(0,0,0,@)", color, alphaMult );
  265. }
  266. output = new GenOp( " @ *= float4(1,1,1,@);\r\n", color, alphaMult );
  267. }
  268.  
  269. ShaderFeatureConstHandles* CustomShaderObjectHLSL::createConstHandles( GFXShader *shader, SimObject *userObject )
  270. {
  271. CustomShaderObjectConstHandles *handles = new CustomShaderObjectConstHandles();
  272. handles->init( shader );
  273.  
  274. return handles;
  275. }
  276.  
  277.  
  278.  
  279.  
  280.  
  281.  
  282. ------------------------------------------------------------------------
  283. ------------------------------------------------------------------------
  284. ------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement