PageRenderTime 54ms CodeModel.GetById 35ms app.highlight 16ms RepoModel.GetById 1ms app.codeStats 0ms

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