Skip to content

Commit 7ffc349

Browse files
authored
Adsk Contrib - Improve the OSL integration (#1561) (#1586)
* Adsk Contrib - Improve the OSL integration Signed-off-by: Patrick Hodoul <[email protected]> * Fix a typo Signed-off-by: Patrick Hodoul <[email protected]>
1 parent 54e7212 commit 7ffc349

File tree

4 files changed

+210
-154
lines changed

4 files changed

+210
-154
lines changed

src/OpenColorIO/GpuShaderClassWrapper.cpp

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,141 @@ std::string GetArrayLengthVariableName(const std::string& variableName)
1515
return variableName + "_count";
1616
}
1717

18+
std::unique_ptr<GpuShaderClassWrapper> GpuShaderClassWrapper::CreateClassWrapper(GpuLanguage language)
19+
{
20+
switch(language)
21+
{
22+
case GPU_LANGUAGE_MSL_2_0:
23+
return std::unique_ptr<MetalShaderClassWrapper>(new MetalShaderClassWrapper);
24+
25+
case LANGUAGE_OSL_1:
26+
return std::unique_ptr<OSLShaderClassWrapper>(new OSLShaderClassWrapper);
27+
28+
// Most of the supported GPU shader languages do not have needs imposing a custom class
29+
// wrapper so, the default class wrapper does nothing.
30+
case GPU_LANGUAGE_CG:
31+
case GPU_LANGUAGE_GLSL_1_2:
32+
case GPU_LANGUAGE_GLSL_1_3:
33+
case GPU_LANGUAGE_GLSL_4_0:
34+
case GPU_LANGUAGE_HLSL_DX11:
35+
case GPU_LANGUAGE_GLSL_ES_1_0:
36+
case GPU_LANGUAGE_GLSL_ES_3_0:
37+
default:
38+
return std::unique_ptr<NullGpuShaderClassWrapper>(new NullGpuShaderClassWrapper);
39+
}
40+
}
41+
42+
std::unique_ptr<GpuShaderClassWrapper> NullGpuShaderClassWrapper::clone() const
43+
{
44+
return std::unique_ptr<NullGpuShaderClassWrapper>(new NullGpuShaderClassWrapper());
45+
}
46+
47+
std::unique_ptr<GpuShaderClassWrapper> OSLShaderClassWrapper::clone() const
48+
{
49+
return std::unique_ptr<OSLShaderClassWrapper>(new OSLShaderClassWrapper());
50+
}
51+
52+
std::string OSLShaderClassWrapper::getClassWrapperHeader(const std::string& originalHeader)
53+
{
54+
GpuShaderText st(LANGUAGE_OSL_1);
55+
56+
st.newLine() << "";
57+
st.newLine() << "/* All the includes */";
58+
st.newLine() << "";
59+
st.newLine() << "#include \"vector4.h\"";
60+
st.newLine() << "#include \"color4.h\"";
61+
62+
st.newLine() << "";
63+
st.newLine() << "/* All the generic helper methods */";
64+
65+
st.newLine() << "";
66+
st.newLine() << "vector4 __operator__mul__(matrix m, vector4 v)";
67+
st.newLine() << "{";
68+
st.indent();
69+
st.newLine() << "return vector4(v.x * m[0][0] + v.y * m[0][1] + v.z * m[0][2] + v.w * m[0][3], ";
70+
st.newLine() << " v.x * m[1][0] + v.y * m[1][1] + v.z * m[1][2] + v.w * m[1][3], ";
71+
st.newLine() << " v.x * m[2][0] + v.y * m[2][1] + v.z * m[2][2] + v.w * m[2][3], ";
72+
st.newLine() << " v.x * m[3][0] + v.y * m[3][1] + v.z * m[3][2] + v.w * m[3][3]);";
73+
st.dedent();
74+
st.newLine() << "}";
75+
76+
st.newLine() << "";
77+
st.newLine() << "vector4 __operator__mul__(color4 c, vector4 v)";
78+
st.newLine() << "{";
79+
st.indent();
80+
st.newLine() << "return vector4(c.rgb.r, c.rgb.g, c.rgb.b, c.a) * v;";
81+
st.dedent();
82+
st.newLine() << "}";
83+
84+
st.newLine() << "";
85+
st.newLine() << "vector4 __operator__mul__(vector4 v, color4 c)";
86+
st.newLine() << "{";
87+
st.indent();
88+
st.newLine() << "return v * vector4(c.rgb.r, c.rgb.g, c.rgb.b, c.a);";
89+
st.dedent();
90+
st.newLine() << "}";
91+
92+
st.newLine() << "";
93+
st.newLine() << "vector4 __operator__sub__(color4 c, vector4 v)";
94+
st.newLine() << "{";
95+
st.indent();
96+
st.newLine() << "return vector4(c.rgb.r, c.rgb.g, c.rgb.b, c.a) - v;";
97+
st.dedent();
98+
st.newLine() << "}";
99+
100+
st.newLine() << "";
101+
st.newLine() << "vector4 __operator__add__(vector4 v, color4 c)";
102+
st.newLine() << "{";
103+
st.indent();
104+
st.newLine() << "return v + vector4(c.rgb.r, c.rgb.g, c.rgb.b, c.a);";
105+
st.dedent();
106+
st.newLine() << "}";
107+
108+
st.newLine() << "";
109+
st.newLine() << "vector4 __operator__add__(color4 c, vector4 v)";
110+
st.newLine() << "{";
111+
st.indent();
112+
st.newLine() << "return vector4(c.rgb.r, c.rgb.g, c.rgb.b, c.a) + v;";
113+
st.dedent();
114+
st.newLine() << "}";
115+
116+
st.newLine() << "";
117+
st.newLine() << "vector4 pow(color4 c, vector4 v)";
118+
st.newLine() << "{";
119+
st.indent();
120+
st.newLine() << "return pow(vector4(c.rgb.r, c.rgb.g, c.rgb.b, c.a), v);";
121+
st.dedent();
122+
st.newLine() << "}";
123+
124+
st.newLine() << "";
125+
st.newLine() << "vector4 max(vector4 v, color4 c)";
126+
st.newLine() << "{";
127+
st.indent();
128+
st.newLine() << "return max(v, vector4(c.rgb.r, c.rgb.g, c.rgb.b, c.a));";
129+
st.dedent();
130+
st.newLine() << "}";
131+
132+
st.newLine() << "";
133+
st.newLine() << "/* The shader implementation */";
134+
st.newLine() << "";
135+
st.newLine() << "shader " << "OSL_" << m_functionName
136+
<< "(color4 inColor = {color(0), 1}, output color4 outColor = {color(0), 1})";
137+
st.newLine() << "{";
138+
139+
return st.string() + originalHeader;
140+
}
141+
142+
std::string OSLShaderClassWrapper::getClassWrapperFooter(const std::string & originalFooter)
143+
{
144+
GpuShaderText st(LANGUAGE_OSL_1);
145+
146+
st.newLine() << "";
147+
st.newLine() << "outColor = " << m_functionName << "(inColor);";
148+
st.newLine() << "}";
149+
150+
return originalFooter + st.string();
151+
}
152+
18153
std::string MetalShaderClassWrapper::generateClassWrapperHeader(GpuShaderText& kw) const
19154
{
20155
if(m_className.empty())

src/OpenColorIO/GpuShaderClassWrapper.h

Lines changed: 55 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
#ifndef INCLUDED_OCIO_GPUSHADERCLASSWRAPPER_H
55
#define INCLUDED_OCIO_GPUSHADERCLASSWRAPPER_H
66

7+
#include <memory>
78
#include <sstream>
89
#include <utility>
910
#include <vector>
10-
#include <memory>
1111

1212
#include <OpenColorIO/OpenColorIO.h>
1313

@@ -16,47 +16,76 @@
1616
namespace OCIO_NAMESPACE
1717
{
1818

19+
// Pure virtual base class for any specific class wrapper.
1920
class GpuShaderClassWrapper
2021
{
2122
public:
22-
virtual void prepareClassWrapper(const std::string& resourcePrefix,
23-
const std::string& functionName,
24-
const std::string& originalHeader) = 0;
25-
virtual std::string getClassWrapperHeader(const std::string& originalHeader) = 0;
26-
virtual std::string getClassWrapperFooter(const std::string& originalFooter) = 0;
27-
23+
24+
// Factory method to blindly get the right class wrapper.
25+
static std::unique_ptr<GpuShaderClassWrapper> CreateClassWrapper(GpuLanguage language);
26+
27+
virtual void prepareClassWrapper(const std::string & resourcePrefix,
28+
const std::string & functionName,
29+
const std::string & originalHeader) = 0;
30+
virtual std::string getClassWrapperHeader(const std::string & originalHeader) = 0;
31+
virtual std::string getClassWrapperFooter(const std::string & originalFooter) = 0;
32+
2833
virtual std::unique_ptr<GpuShaderClassWrapper> clone() const = 0;
29-
34+
3035
virtual ~GpuShaderClassWrapper() = default;
3136
};
3237

3338
class NullGpuShaderClassWrapper : public GpuShaderClassWrapper
3439
{
3540
public:
36-
void prepareClassWrapper(const std::string& /*resourcePrefix*/,
37-
const std::string& /*functionName*/,
38-
const std::string& /*originalHeader*/) final {}
39-
std::string getClassWrapperHeader(const std::string& originalHeader) final { return originalHeader; }
40-
std::string getClassWrapperFooter(const std::string& originalFooter) final { return originalFooter; }
41-
42-
std::unique_ptr<GpuShaderClassWrapper> clone() const final
41+
void prepareClassWrapper(const std::string & /*resourcePrefix*/,
42+
const std::string & /*functionName*/,
43+
const std::string & /*originalHeader*/) final
44+
{
45+
}
46+
std::string getClassWrapperHeader(const std::string & originalHeader) final
47+
{
48+
return originalHeader;
49+
}
50+
std::string getClassWrapperFooter(const std::string & originalFooter) final
51+
{
52+
return originalFooter;
53+
}
54+
55+
std::unique_ptr<GpuShaderClassWrapper> clone() const final;
56+
};
57+
58+
class OSLShaderClassWrapper : public GpuShaderClassWrapper
59+
{
60+
public:
61+
void prepareClassWrapper(const std::string & /*resourcePrefix*/,
62+
const std::string & functionName,
63+
const std::string & /*originalHeader*/) final
4364
{
44-
return std::unique_ptr<NullGpuShaderClassWrapper>(new NullGpuShaderClassWrapper());
65+
m_functionName = functionName;
4566
}
67+
68+
std::string getClassWrapperHeader(const std::string & originalHeader) final;
69+
std::string getClassWrapperFooter(const std::string & originalFooter) final;
70+
71+
std::unique_ptr<GpuShaderClassWrapper> clone() const final;
72+
73+
private:
74+
std::string m_functionName;
4675
};
4776

4877
class MetalShaderClassWrapper : public GpuShaderClassWrapper
4978
{
5079
public:
51-
void prepareClassWrapper(const std::string& resourcePrefix,
52-
const std::string& functionName,
53-
const std::string& originalHeader) final;
54-
std::string getClassWrapperHeader(const std::string& originalHeader) final;
55-
std::string getClassWrapperFooter(const std::string& originalFooter) final;
56-
80+
void prepareClassWrapper(const std::string & resourcePrefix,
81+
const std::string & functionName,
82+
const std::string & originalHeader) final;
83+
std::string getClassWrapperHeader(const std::string & originalHeader) final;
84+
std::string getClassWrapperFooter(const std::string & originalFooter) final;
85+
5786
std::unique_ptr<GpuShaderClassWrapper> clone() const final;
5887
MetalShaderClassWrapper& operator=(const MetalShaderClassWrapper& rhs);
59-
88+
6089
private:
6190
struct FunctionParam
6291
{
@@ -72,12 +101,12 @@ class MetalShaderClassWrapper : public GpuShaderClassWrapper
72101
std::string m_name;
73102
bool m_isArray;
74103
};
75-
76-
std::string getClassWrapperName(const std::string &resourcePrefix, const std::string& functionName);
104+
105+
static std::string getClassWrapperName(const std::string &resourcePrefix, const std::string &functionName);
77106
void extractFunctionParameters(const std::string& declaration);
78107
std::string generateClassWrapperHeader(GpuShaderText& st) const;
79108
std::string generateClassWrapperFooter(GpuShaderText& st, const std::string &ocioFunctionName) const;
80-
109+
81110
std::string m_className;
82111
std::string m_functionName;
83112
std::vector<FunctionParam> m_functionParameters;

0 commit comments

Comments
 (0)