/src/core/GpuShaderUtils.cpp

http://github.com/imageworks/OpenColorIO · C++ · 193 lines · 148 code · 17 blank · 28 comment · 47 complexity · 3fa0851cf60bee2bf25b1e0988fa8d9d MD5 · raw file

  1. /*
  2. Copyright (c) 2003-2010 Sony Pictures Imageworks Inc., et al.
  3. All Rights Reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are
  6. met:
  7. * Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. * Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in the
  11. documentation and/or other materials provided with the distribution.
  12. * Neither the name of Sony Pictures Imageworks nor the names of its
  13. contributors may be used to endorse or promote products derived from
  14. this software without specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  16. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  17. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  18. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  19. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  21. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  22. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  23. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include <OpenColorIO/OpenColorIO.h>
  28. #include "GpuShaderUtils.h"
  29. #include "MathUtils.h"
  30. #include <cmath>
  31. #include <sstream>
  32. OCIO_NAMESPACE_ENTER
  33. {
  34. void Write_half4x4(std::ostream & os, const float * m44, GpuLanguage lang)
  35. {
  36. if(lang == GPU_LANGUAGE_CG)
  37. {
  38. os << "half4x4(";
  39. for(int i=0; i<16; i++)
  40. {
  41. if(i!=0) os << ", ";
  42. os << ClampToNormHalf(m44[i]);
  43. }
  44. os << ")";
  45. }
  46. else if(lang == GPU_LANGUAGE_GLSL_1_0 || lang == GPU_LANGUAGE_GLSL_1_3)
  47. {
  48. os << "mat4(";
  49. for(int i=0; i<16; i++)
  50. {
  51. if(i!=0) os << ", ";
  52. os << m44[i]; // Clamping to half is not necessary
  53. }
  54. os << ")";
  55. }
  56. else
  57. {
  58. throw Exception("Unsupported shader language.");
  59. }
  60. }
  61. void Write_half4(std::ostream & os, const float * v4, GpuLanguage lang)
  62. {
  63. if(lang == GPU_LANGUAGE_CG)
  64. {
  65. os << "half4(";
  66. for(int i=0; i<4; i++)
  67. {
  68. if(i!=0) os << ", ";
  69. os << ClampToNormHalf(v4[i]);
  70. }
  71. os << ")";
  72. }
  73. else if(lang == GPU_LANGUAGE_GLSL_1_0 || lang == GPU_LANGUAGE_GLSL_1_3)
  74. {
  75. os << "vec4(";
  76. for(int i=0; i<4; i++)
  77. {
  78. if(i!=0) os << ", ";
  79. os << v4[i]; // Clamping to half is not necessary
  80. }
  81. os << ")";
  82. }
  83. else
  84. {
  85. throw Exception("Unsupported shader language.");
  86. }
  87. }
  88. void Write_half3(std::ostream & os, const float * v3, GpuLanguage lang)
  89. {
  90. if(lang == GPU_LANGUAGE_CG)
  91. {
  92. os << "half3(";
  93. for(int i=0; i<3; i++)
  94. {
  95. if(i!=0) os << ", ";
  96. os << ClampToNormHalf(v3[i]);
  97. }
  98. os << ")";
  99. }
  100. else if(lang == GPU_LANGUAGE_GLSL_1_0 || lang == GPU_LANGUAGE_GLSL_1_3)
  101. {
  102. os << "vec3(";
  103. for(int i=0; i<3; i++)
  104. {
  105. if(i!=0) os << ", ";
  106. os << v3[i]; // Clamping to half is not necessary
  107. }
  108. os << ")";
  109. }
  110. else
  111. {
  112. throw Exception("Unsupported shader language.");
  113. }
  114. }
  115. std::string GpuTextHalf4x4(const float * m44, GpuLanguage lang)
  116. {
  117. std::ostringstream os;
  118. Write_half4x4(os, m44, lang);
  119. return os.str();
  120. }
  121. std::string GpuTextHalf4(const float * v4, GpuLanguage lang)
  122. {
  123. std::ostringstream os;
  124. Write_half4(os, v4, lang);
  125. return os.str();
  126. }
  127. std::string GpuTextHalf3(const float * v3, GpuLanguage lang)
  128. {
  129. std::ostringstream os;
  130. Write_half3(os, v3, lang);
  131. return os.str();
  132. }
  133. // Note that Cg and GLSL have opposite ordering for vec/mtx multiplication
  134. void Write_mtx_x_vec(std::ostream & os,
  135. const std::string & mtx, const std::string & vec,
  136. GpuLanguage lang)
  137. {
  138. if(lang == GPU_LANGUAGE_CG)
  139. {
  140. os << "mul( " << mtx << ", " << vec << ")";
  141. }
  142. else if(lang == GPU_LANGUAGE_GLSL_1_0 || lang == GPU_LANGUAGE_GLSL_1_3)
  143. {
  144. os << vec << " * " << mtx;
  145. }
  146. else
  147. {
  148. throw Exception("Unsupported shader language.");
  149. }
  150. }
  151. void Write_sampleLut3D_rgb(std::ostream & os, const std::string& variableName,
  152. const std::string& lutName, int lut3DEdgeLen,
  153. GpuLanguage lang)
  154. {
  155. float m = ((float) lut3DEdgeLen-1.0f) / (float) lut3DEdgeLen;
  156. float b = 1.0f / (2.0f * (float) lut3DEdgeLen);
  157. if(lang == GPU_LANGUAGE_CG)
  158. {
  159. os << "tex3D(";
  160. os << lutName << ", ";
  161. os << m << " * " << variableName << ".rgb + " << b << ").rgb;" << std::endl;
  162. }
  163. else if(lang == GPU_LANGUAGE_GLSL_1_0 || lang == GPU_LANGUAGE_GLSL_1_3)
  164. {
  165. os << "texture3D(";
  166. os << lutName << ", ";
  167. os << m << " * " << variableName << ".rgb + " << b << ").rgb;" << std::endl;
  168. }
  169. else
  170. {
  171. throw Exception("Unsupported shader language.");
  172. }
  173. }
  174. }
  175. OCIO_NAMESPACE_EXIT