2
0

shaderOp.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 "core/strings/stringFunctions.h"
  23. #include <stdarg.h>
  24. #include "shaderOp.h"
  25. //**************************************************************************
  26. // Shader Operations
  27. //**************************************************************************
  28. ShaderOp::ShaderOp( LangElement *in1, LangElement *in2 )
  29. {
  30. mInput[0] = in1;
  31. mInput[1] = in2;
  32. }
  33. //**************************************************************************
  34. // Declaration Operation - for variables
  35. //**************************************************************************
  36. DecOp::DecOp( Var *in1 ) : Parent( in1, NULL )
  37. {
  38. mInput[0] = in1;
  39. }
  40. //--------------------------------------------------------------------------
  41. // Print
  42. //--------------------------------------------------------------------------
  43. void DecOp::print( Stream &stream )
  44. {
  45. Var *var = dynamic_cast<Var*>( mInput[0] );
  46. WRITESTR( (char*)var->type );
  47. WRITESTR( " " );
  48. mInput[0]->print( stream );
  49. }
  50. //**************************************************************************
  51. // Echo operation - deletes incoming statement!
  52. //**************************************************************************
  53. EchoOp::EchoOp( const char * statement ) : Parent( NULL, NULL )
  54. {
  55. mStatement = statement;
  56. }
  57. //--------------------------------------------------------------------------
  58. // Destructor
  59. //--------------------------------------------------------------------------
  60. EchoOp::~EchoOp()
  61. {
  62. delete [] mStatement;
  63. }
  64. //--------------------------------------------------------------------------
  65. // Print
  66. //--------------------------------------------------------------------------
  67. void EchoOp::print( Stream &stream )
  68. {
  69. WRITESTR( mStatement );
  70. }
  71. //**************************************************************************
  72. // Index operation
  73. //**************************************************************************
  74. IndexOp::IndexOp( Var* var, U32 index ) : Parent( NULL, NULL )
  75. {
  76. mInput[0] = var;
  77. mIndex = index;
  78. }
  79. //--------------------------------------------------------------------------
  80. // Print
  81. //--------------------------------------------------------------------------
  82. void IndexOp::print( Stream &stream )
  83. {
  84. Var* var = dynamic_cast<Var*>(mInput[0]);
  85. mInput[0]->print(stream);
  86. if (var->arraySize > 1)
  87. {
  88. WRITESTR(String::ToString("[%d]", mIndex));
  89. }
  90. }
  91. //**************************************************************************
  92. // General operation
  93. //**************************************************************************
  94. GenOp::GenOp( const char * statement, ... ) : Parent( NULL, NULL )
  95. {
  96. VECTOR_SET_ASSOCIATION( mElemList );
  97. va_list args;
  98. va_start(args, statement);
  99. char* lastEntry = (char*)statement;
  100. while( 1 )
  101. {
  102. // search 'statement' for @ symbol
  103. char * str = dStrstr( lastEntry, (char *)"@" );
  104. if( !str )
  105. {
  106. // not found, handle end of line
  107. str = (char*)&statement[ dStrlen( (char*)statement ) ];
  108. U32 diff = str - lastEntry + 1;
  109. if( diff == 1 ) break;
  110. char * newStr = new char[diff];
  111. dMemcpy( (void*)newStr, lastEntry, diff );
  112. mElemList.push_back( new EchoOp( newStr ) );
  113. break;
  114. }
  115. // create and store statement fragment
  116. U32 diff = str - lastEntry + 1;
  117. if( diff == 1 )
  118. {
  119. // store langElement
  120. LangElement *elem = va_arg(args, LangElement* );
  121. AssertFatal( elem, "NULL arguement." );
  122. mElemList.push_back( elem );
  123. lastEntry++;
  124. continue;
  125. }
  126. char * newStr = new char[diff];
  127. dMemcpy( (void*)newStr, lastEntry, diff );
  128. newStr[diff-1] = '\0';
  129. lastEntry = str + 1;
  130. mElemList.push_back( new EchoOp( newStr ) );
  131. // store langElement
  132. LangElement *elem = va_arg(args, LangElement* );
  133. AssertFatal( elem, "NULL argument." );
  134. mElemList.push_back( elem );
  135. }
  136. va_end( args );
  137. }
  138. //--------------------------------------------------------------------------
  139. // Print
  140. //--------------------------------------------------------------------------
  141. void GenOp::print( Stream &stream )
  142. {
  143. for( U32 i=0; i<mElemList.size(); i++ )
  144. {
  145. mElemList[i]->print( stream );
  146. }
  147. }