ATI_FS_GLGpuProgram.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. -----------------------------------------------------------------------------
  3. This source file is part of OGRE
  4. (Object-oriented Graphics Rendering Engine)
  5. For the latest info, see http://www.ogre3d.org/
  6. Copyright (c) 2000-2011 Torus Knot Software Ltd
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. -----------------------------------------------------------------------------
  23. */
  24. #include "ps_1_4.h"
  25. #include "OgreException.h"
  26. #include "OgreRenderSystem.h"
  27. #include "OgreRenderSystemCapabilities.h"
  28. #include "ATI_FS_GLGpuProgram.h"
  29. using namespace Ogre;
  30. ATI_FS_GLGpuProgram::ATI_FS_GLGpuProgram() :
  31. GLGpuProgram()
  32. {
  33. mProgramType = GL_FRAGMENT_SHADER_ATI;
  34. mProgramID = glGenFragmentShadersATI(1);
  35. }
  36. ATI_FS_GLGpuProgram::~ATI_FS_GLGpuProgram()
  37. {
  38. // have to call this here reather than in Resource destructor
  39. // since calling virtual methods in base destructors causes crash
  40. unload();
  41. }
  42. void ATI_FS_GLGpuProgram::bindProgram(void)
  43. {
  44. glEnable(mProgramType);
  45. glBindFragmentShaderATI(mProgramID);
  46. }
  47. void ATI_FS_GLGpuProgram::unbindProgram(void)
  48. {
  49. glDisable(mProgramType);
  50. }
  51. void ATI_FS_GLGpuProgram::bindProgramParameters(GpuProgramParametersSharedPtr params, uint16 mask)
  52. {
  53. // only supports float constants
  54. GpuLogicalBufferStructPtr floatStruct = params->getFloatLogicalBufferStruct();
  55. for (GpuLogicalIndexUseMap::const_iterator i = floatStruct->map.begin();
  56. i != floatStruct->map.end(); ++i)
  57. {
  58. if (i->second.variability & mask)
  59. {
  60. size_t logicalIndex = i->first;
  61. const float* pFloat = params->getFloatPointer(i->second.physicalIndex);
  62. // Iterate over the params, set in 4-float chunks (low-level)
  63. for (size_t j = 0; j < i->second.currentSize; j+=4)
  64. {
  65. glSetFragmentShaderConstantATI(GL_CON_0_ATI + logicalIndex, pFloat);
  66. pFloat += 4;
  67. ++logicalIndex;
  68. }
  69. }
  70. }
  71. }
  72. void ATI_FS_GLGpuProgram::bindProgramPassIterationParameters(GpuProgramParametersSharedPtr params)
  73. {
  74. if (params->hasPassIterationNumber())
  75. {
  76. size_t physicalIndex = params->getPassIterationNumberIndex();
  77. size_t logicalIndex = params->getFloatLogicalIndexForPhysicalIndex(physicalIndex);
  78. const float* pFloat = params->getFloatPointer(physicalIndex);
  79. glSetFragmentShaderConstantATI( GL_CON_0_ATI + (GLuint)logicalIndex, pFloat);
  80. }
  81. }
  82. void ATI_FS_GLGpuProgram::unloadImpl(void)
  83. {
  84. glDeleteFragmentShaderATI(mProgramID);
  85. }
  86. void ATI_FS_GLGpuProgram::loadFromSource(void)
  87. {
  88. PS_1_4 PS1_4Assembler;
  89. // attempt to compile the source
  90. #ifdef _DEBUG
  91. PS1_4Assembler.test(); // run compiler tests in debug mode
  92. #endif
  93. bool Error = !PS1_4Assembler.compile(mSource.c_str());
  94. if(!Error) {
  95. glBindFragmentShaderATI(mProgramID);
  96. glBeginFragmentShaderATI();
  97. // compile was successfull so send the machine instructions thru GL to GPU
  98. Error = !PS1_4Assembler.bindAllMachineInstToFragmentShader();
  99. glEndFragmentShaderATI();
  100. // check GL for GPU machine instruction bind erros
  101. if (Error)
  102. {
  103. OGRE_EXCEPT(Exception::ERR_INTERNAL_ERROR,
  104. "Cannot Bind ATI fragment shader :", "");
  105. }
  106. }
  107. else
  108. {
  109. // an error occured when compiling the ps_1_4 source code
  110. char buff[50];
  111. sprintf(buff,"error on line %d in pixel shader source\n", PS1_4Assembler.mCurrentLine);
  112. // TODO LOG PORT - Log this somewhere
  113. //LogManager::getSingleton().logMessage("Warning: atifs compiler reported the following errors:");
  114. //LogManager::getSingleton().logMessage(buff + mName);
  115. //OGRE_EXCEPT(Exception::ERR_INTERNAL_ERROR,
  116. // "Cannot Compile ATI fragment shader : \n\n" + buff , "");// +
  117. }
  118. }