customShaderFeature.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "shadergen/CustomShaderFeature.h"
  23. #include "shaderGen/HLSL/customFeatureHLSL.h"
  24. #include "math/mathIO.h"
  25. #include "scene/sceneRenderState.h"
  26. #include "core/stream/bitStream.h"
  27. #include "materials/sceneData.h"
  28. #include "gfx/gfxDebugEvent.h"
  29. #include "gfx/gfxTransformSaver.h"
  30. #include "renderInstance/renderPassManager.h"
  31. IMPLEMENT_CONOBJECT(CustomShaderFeatureData);
  32. ConsoleDocClass(CustomShaderFeatureData,
  33. "@brief An example scene object which renders using a callback.\n\n"
  34. "This class implements a basic SceneObject that can exist in the world at a "
  35. "3D position and render itself. Note that CustomShaderFeatureData handles its own "
  36. "rendering by submitting itself as an ObjectRenderInst (see "
  37. "renderInstance\renderPassmanager.h) along with a delegate for its render() "
  38. "function. However, the preffered rendering method in the engine is to submit "
  39. "a MeshRenderInst along with a Material, vertex buffer, primitive buffer, and "
  40. "transform and allow the RenderMeshMgr handle the actual rendering. You can "
  41. "see this implemented in RenderMeshExample.\n\n"
  42. "See the C++ code for implementation details.\n\n"
  43. "@ingroup Examples\n");
  44. //-----------------------------------------------------------------------------
  45. // Object setup and teardown
  46. //-----------------------------------------------------------------------------
  47. CustomShaderFeatureData::CustomShaderFeatureData()
  48. {
  49. }
  50. CustomShaderFeatureData::~CustomShaderFeatureData()
  51. {
  52. }
  53. //-----------------------------------------------------------------------------
  54. // Object Editing
  55. //-----------------------------------------------------------------------------
  56. void CustomShaderFeatureData::initPersistFields()
  57. {
  58. // SceneObject already handles exposing the transform
  59. Parent::initPersistFields();
  60. }
  61. bool CustomShaderFeatureData::onAdd()
  62. {
  63. if (!Parent::onAdd())
  64. return false;
  65. mFeatureHLSL = new CustomFeatureHLSL();
  66. mFeatureHLSL->mOwner = this;
  67. return true;
  68. }
  69. void CustomShaderFeatureData::onRemove()
  70. {
  71. Parent::onRemove();
  72. }
  73. //Shadergen setup functions
  74. void CustomShaderFeatureData::addVariable(String name, String type, String defaultValue)
  75. {
  76. mFeatureHLSL->addVariable(name, type, defaultValue);
  77. }
  78. void CustomShaderFeatureData::addUniform(String name, String type, String defaultValue, U32 arraySize)
  79. {
  80. mFeatureHLSL->addUniform(name, type, defaultValue, arraySize);
  81. }
  82. void CustomShaderFeatureData::addSampler(String name, String type, U32 arraySize)
  83. {
  84. mFeatureHLSL->addSampler(name, type, arraySize);
  85. }
  86. void CustomShaderFeatureData::addTexture(String name, String type, String samplerState, U32 arraySize)
  87. {
  88. mFeatureHLSL->addTexture(name, type, samplerState, arraySize);
  89. }
  90. void CustomShaderFeatureData::addConnector(String name, String elementName, String type)
  91. {
  92. mFeatureHLSL->addConnector(name, elementName, type);
  93. }
  94. bool CustomShaderFeatureData::hasFeature(String name)
  95. {
  96. return mFeatureHLSL->hasFeature(name);
  97. }
  98. void CustomShaderFeatureData::writeLine(String format, S32 argc, ConsoleValueRef *argv)
  99. {
  100. /*mOnObject = onObject;
  101. mArgc = argc;
  102. mArgv = new ConsoleValueRef[argc];
  103. for (int i = 0; i<argc; i++)
  104. {
  105. mArgv[i].value = new ConsoleValue();
  106. mArgv[i].value->type = ConsoleValue::TypeInternalString;
  107. mArgv[i].value->init();
  108. if (argv)
  109. {
  110. mArgv[i].value->setStringValue((const char*)argv[i]);
  111. }
  112. }*/
  113. mFeatureHLSL->writeLine(format, argc, argv);
  114. }
  115. /*//Actual shader processing
  116. void CustomShaderFeatureData::processVert(Vector<ShaderComponent*> &componentList,
  117. const MaterialFeatureData &fd)
  118. {
  119. mFeatureHLSL.processVert(componentList, fd);
  120. }
  121. void CustomShaderFeatureData::processPix(Vector<ShaderComponent*> &componentList,
  122. const MaterialFeatureData &fd)
  123. {
  124. mFeatureHLSL.processPix(componentList, fd);
  125. }
  126. void CustomShaderFeatureData::setTexData(Material::StageData &stageDat,
  127. const MaterialFeatureData &fd,
  128. RenderPassData &passData,
  129. U32 &texIndex)
  130. {
  131. mFeatureHLSL.setTexData(stageDat, fd, passData, texIndex);
  132. }*/
  133. DefineEngineMethod(CustomShaderFeatureData, addVariable, void, (String name, String type, String defaultValue), ("", "", ""), "")
  134. {
  135. object->addVariable(name, type, defaultValue);
  136. }
  137. DefineEngineMethod(CustomShaderFeatureData, addUniform, void, (String name, String type, String defaultValue, U32 arraySize), ("", "", "", 0), "")
  138. {
  139. object->addUniform(name, type, defaultValue, arraySize);
  140. }
  141. DefineEngineMethod(CustomShaderFeatureData, addSampler, void, (String name, U32 arraySize), ("", 0), "")
  142. {
  143. object->addSampler(name, "", arraySize);
  144. }
  145. DefineEngineMethod(CustomShaderFeatureData, addTexture, void, (String name, String type, String samplerState, U32 arraySize), ("", "", 0), "")
  146. {
  147. object->addTexture(name, type, samplerState, arraySize);
  148. }
  149. ConsoleMethod(CustomShaderFeatureData, writeLine, void, 3, 0, "( string format, string args... ) Dynamically call a method on an object.\n"
  150. "@param method Name of method to call.\n"
  151. "@param args Zero or more arguments for the method.\n"
  152. "@return The result of the method call.")
  153. {
  154. object->writeLine(argv[2], argc - 3, argv + 3);
  155. }
  156. DefineEngineMethod(CustomShaderFeatureData, hasFeature, bool, (String name), (""), "")
  157. {
  158. return object->hasFeature(name);
  159. }