-
Notifications
You must be signed in to change notification settings - Fork 405
Expand file tree
/
Copy pathoslmaterial.cpp
More file actions
199 lines (171 loc) · 6.29 KB
/
oslmaterial.cpp
File metadata and controls
199 lines (171 loc) · 6.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// Copyright Contributors to the Open Shading Language project.
// SPDX-License-Identifier: BSD-3-Clause
// https://github.com/AcademySoftwareFoundation/OpenShadingLanguage
#include "oslmaterial.h"
#include <iostream>
using std::cout;
using std::endl;
#if OSL_USE_BATCHED
template<int batch_width>
CustomBatchedRendererServices<batch_width>::CustomBatchedRendererServices(
BatchedOSLMaterial<batch_width>& m)
: OSL::BatchedRendererServices<batch_width>(m.texturesys()), m_sr(m)
{
}
#endif
OSLMaterial::OSLMaterial() {}
#if OSL_USE_BATCHED
template<int batch_width>
BatchedOSLMaterial<batch_width>::BatchedOSLMaterial() : m_batch(*this)
{
}
template BatchedOSLMaterial<8>::BatchedOSLMaterial();
template BatchedOSLMaterial<16>::BatchedOSLMaterial();
#endif
// Supported closures and parameters
struct EmptyParams {};
enum ClosureIDs {
EMISSION_ID,
BACKGROUND_ID,
MICROFACET_ID,
};
struct MicrofacetParams {
OSL::ustringhash dist;
OSL::Vec3 N, U;
float xalpha, yalpha, eta;
int refract;
};
void
register_closures(OSL::ShadingSystem* ss)
{
// "Describe the memory layout of each closure type to the OSL runtime"
constexpr int MaxParams = 32;
struct BuiltinClosures {
const char* name;
int id;
OSL::ClosureParam params[MaxParams]; // "upper bound"
};
using namespace OSL;
// Closures with support built into OSL, connected by the 1st string
BuiltinClosures supported[] = {
{ "emission", EMISSION_ID, { CLOSURE_FINISH_PARAM(EmptyParams) } },
{ "background", BACKGROUND_ID, { CLOSURE_FINISH_PARAM(EmptyParams) } },
{ "microfacet",
MICROFACET_ID,
{ CLOSURE_STRING_PARAM(MicrofacetParams, dist),
CLOSURE_VECTOR_PARAM(MicrofacetParams, N),
CLOSURE_VECTOR_PARAM(MicrofacetParams, U),
CLOSURE_FLOAT_PARAM(MicrofacetParams, xalpha),
CLOSURE_FLOAT_PARAM(MicrofacetParams, yalpha),
CLOSURE_FLOAT_PARAM(MicrofacetParams, eta),
CLOSURE_INT_PARAM(MicrofacetParams, refract),
CLOSURE_FINISH_PARAM(MicrofacetParams) } },
};
// Closure registration here enables that type of closure, when executing or compiling a shader
for (const BuiltinClosures& c : supported)
ss->register_closure(c.name, c.id, c.params, nullptr, nullptr);
}
void
process_bsdf_closure(const OSL::ClosureColor* closure)
{
static const ::OSL::ustringhash uh_ggx(OIIO::Strutil::strhash("ggx"));
//static const ::OSL::ustringhash uh_beckmann(OIIO::Strutil::strhash("beckmann"));
if (!closure)
return;
switch (closure->id) {
case OSL::ClosureColor::MUL: {
process_bsdf_closure(closure->as_mul()->closure);
break;
}
case OSL::ClosureColor::ADD: {
process_bsdf_closure(closure->as_add()->closureA);
process_bsdf_closure(closure->as_add()->closureB);
break;
}
default: {
const OSL::ClosureComponent* comp = closure->as_comp();
switch (comp->id) {
case EMISSION_ID: cout << "parsing emission closure" << endl; break;
case MICROFACET_ID: {
cout << "parsing microfacet closure" << endl;
const MicrofacetParams* mp = comp->as<MicrofacetParams>();
if (mp->dist.hash() == uh_ggx.hash()) {
cout << "uh_ggx" << endl;
} else {
cout << "uh_beckmann or default" << endl;
}
} break;
default:
OSL_ASSERT(false && "Invalid closure invoked in surface shader");
break;
}
} break;
}
}
void
OSLMaterial::run_test(OSL::ShadingSystem* ss, OSL::PerThreadInfo* thread_info,
OSL::ShadingContext* context, char* shader_name)
{
register_closures(ss);
OSL::ShaderGlobals globals;
globals_from_hit(globals);
std::vector<std::string> options;
// Create a new shader group
m_shaders.emplace_back();
m_shaders[0] = ss->ShaderGroupBegin(std::to_string(0));
OSL::ShaderGroupRef group = m_shaders[0];
//{
// OSL::OSLCompiler compiler;
// std::string name = std::string(shader_name) + ".osl";
// compiler.compile(name.c_str(), options);
//}
ss->Shader(*group, "surface", shader_name, "Test");
ss->ShaderGroupEnd(*group);
ss->execute(context, *group, globals);
const OSL::ClosureColor* closure = globals.Ci;
process_bsdf_closure(closure);
}
#if OSL_USE_BATCHED
template<int batch_width>
void
BatchedOSLMaterial<batch_width>::run_test(OSL::ShadingSystem* ss,
OSL::PerThreadInfo* thread_info,
OSL::ShadingContext* context,
char* shader_name)
{
register_closures(ss);
OSL::BatchedShaderGlobals<batch_width> batched_globals;
m_batch.globals_from_hit(batched_globals);
std::vector<std::string> options;
// Create a new shader group
m_shaders.emplace_back();
m_shaders[0] = ss->ShaderGroupBegin(std::to_string(0));
OSL::ShaderGroupRef group = m_shaders[0];
//{
// OSL::OSLCompiler compiler;
// std::string name = std::string(shader_name) + ".osl";
// compiler.compile(name.c_str(), options);
//}
ss->Shader(*group, "surface", shader_name, "Test");
ss->ShaderGroupEnd(*group);
// Run the shader that was just created
OSL::Block<int, batch_width> wide_shadeindex_block;
char* userdata_base_ptr = NULL;
char* output_base_ptr = NULL;
ss->batched<batch_width>().execute(*context, *group, batch_width,
wide_shadeindex_block, batched_globals,
userdata_base_ptr, output_base_ptr);
const OSL::ClosureColor* closure = batched_globals.varying.Ci[0];
process_bsdf_closure(closure);
}
template void
BatchedOSLMaterial<8>::run_test(OSL::ShadingSystem* ss,
OSL::PerThreadInfo* thread_info,
OSL::ShadingContext* context,
char* shader_name);
template void
BatchedOSLMaterial<16>::run_test(OSL::ShadingSystem* ss,
OSL::PerThreadInfo* thread_info,
OSL::ShadingContext* context,
char* shader_name);
#endif