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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
|
/*
* Copyright 2011, Blender Foundation.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "device.h"
#include "graph.h"
#include "light.h"
#include "mesh.h"
#include "nodes.h"
#include "osl.h"
#include "scene.h"
#include "shader.h"
#include "svm.h"
#include "util_foreach.h"
CCL_NAMESPACE_BEGIN
/* Shader */
Shader::Shader()
{
name = "";
pass_id = 0;
graph = NULL;
graph_bump = NULL;
sample_as_light = true;
homogeneous_volume = false;
has_surface = false;
has_surface_transparent = false;
has_surface_emission = false;
has_volume = false;
has_displacement = false;
used = false;
need_update = true;
need_update_attributes = true;
}
Shader::~Shader()
{
delete graph;
delete graph_bump;
}
void Shader::set_graph(ShaderGraph *graph_)
{
/* assign graph */
delete graph;
delete graph_bump;
graph = graph_;
graph_bump = NULL;
}
void Shader::tag_update(Scene *scene)
{
/* update tag */
need_update = true;
scene->shader_manager->need_update = true;
/* if the shader previously was emissive, update light distribution,
* if the new shader is emissive, a light manager update tag will be
* done in the shader manager device update. */
if(sample_as_light && has_surface_emission)
scene->light_manager->need_update = true;
/* get requested attributes. this could be optimized by pruning unused
* nodes here already, but that's the job of the shader manager currently,
* and may not be so great for interactive rendering where you temporarily
* disconnect a node */
AttributeRequestSet prev_attributes = attributes;
attributes.clear();
foreach(ShaderNode *node, graph->nodes)
node->attributes(&attributes);
/* compare if the attributes changed, mesh manager will check
* need_update_attributes, update the relevant meshes and clear it. */
if(attributes.modified(prev_attributes)) {
need_update_attributes = true;
scene->mesh_manager->need_update = true;
}
}
void Shader::tag_used(Scene *scene)
{
/* if an unused shader suddenly gets used somewhere, it needs to be
* recompiled because it was skipped for compilation before */
if(!used) {
need_update = true;
scene->shader_manager->need_update = true;
}
}
/* Shader Manager */
ShaderManager::ShaderManager()
{
need_update = true;
}
ShaderManager::~ShaderManager()
{
}
ShaderManager *ShaderManager::create(Scene *scene)
{
ShaderManager *manager;
#ifdef WITH_OSL
if(scene->params.shadingsystem == SceneParams::OSL)
manager = new OSLShaderManager();
else
#endif
manager = new SVMShaderManager();
add_default(scene);
return manager;
}
uint ShaderManager::get_attribute_id(ustring name)
{
/* get a unique id for each name, for SVM attribute lookup */
AttributeIDMap::iterator it = unique_attribute_id.find(name);
if(it != unique_attribute_id.end())
return it->second;
uint id = (uint)ATTR_STD_NUM + unique_attribute_id.size();
unique_attribute_id[name] = id;
return id;
}
uint ShaderManager::get_attribute_id(AttributeStandard std)
{
return (uint)std;
}
int ShaderManager::get_shader_id(uint shader, Mesh *mesh, bool smooth)
{
/* get a shader id to pass to the kernel */
int id = shader*2;
/* index depends bump since this setting is not in the shader */
if(mesh && mesh->displacement_method != Mesh::DISPLACE_TRUE)
id += 1;
/* smooth flag */
if(smooth)
id |= SHADER_SMOOTH_NORMAL;
/* default flags */
id |= SHADER_CAST_SHADOW|SHADER_AREA_LIGHT;
return id;
}
void ShaderManager::device_update_shaders_used(Scene *scene)
{
/* figure out which shaders are in use, so SVM/OSL can skip compiling them
* for speed and avoid loading image textures into memory */
foreach(Shader *shader, scene->shaders)
shader->used = false;
scene->shaders[scene->default_surface]->used = true;
scene->shaders[scene->default_light]->used = true;
scene->shaders[scene->default_background]->used = true;
scene->shaders[scene->default_holdout]->used = true;
scene->shaders[scene->default_empty]->used = true;
foreach(Mesh *mesh, scene->meshes)
foreach(uint shader, mesh->used_shaders)
scene->shaders[shader]->used = true;
foreach(Light *light, scene->lights)
scene->shaders[light->shader]->used = true;
}
void ShaderManager::device_update_common(Device *device, DeviceScene *dscene, Scene *scene, Progress& progress)
{
device_free_common(device, dscene);
if(scene->shaders.size() == 0)
return;
uint shader_flag_size = scene->shaders.size()*4;
uint *shader_flag = dscene->shader_flag.resize(shader_flag_size);
uint i = 0;
foreach(Shader *shader, scene->shaders) {
uint flag = 0;
if(shader->sample_as_light)
flag |= SD_SAMPLE_AS_LIGHT;
if(shader->has_surface_transparent)
flag |= SD_HAS_SURFACE_TRANSPARENT;
if(shader->has_volume)
flag |= SD_HAS_VOLUME;
if(shader->homogeneous_volume)
flag |= SD_HOMOGENEOUS_VOLUME;
shader_flag[i++] = flag;
shader_flag[i++] = shader->pass_id;
shader_flag[i++] = flag;
shader_flag[i++] = shader->pass_id;
}
device->tex_alloc("__shader_flag", dscene->shader_flag);
}
void ShaderManager::device_free_common(Device *device, DeviceScene *dscene)
{
device->tex_free(dscene->shader_flag);
dscene->shader_flag.clear();
}
void ShaderManager::add_default(Scene *scene)
{
Shader *shader;
ShaderGraph *graph;
ShaderNode *closure, *out;
/* default surface */
{
graph = new ShaderGraph();
closure = graph->add(new DiffuseBsdfNode());
closure->input("Color")->value = make_float3(0.8f, 0.8f, 0.8f);
out = graph->output();
graph->connect(closure->output("BSDF"), out->input("Surface"));
shader = new Shader();
shader->name = "default_surface";
shader->graph = graph;
scene->shaders.push_back(shader);
scene->default_surface = scene->shaders.size() - 1;
}
/* default light */
{
graph = new ShaderGraph();
closure = graph->add(new EmissionNode());
closure->input("Color")->value = make_float3(0.8f, 0.8f, 0.8f);
closure->input("Strength")->value.x = 0.0f;
out = graph->output();
graph->connect(closure->output("Emission"), out->input("Surface"));
shader = new Shader();
shader->name = "default_light";
shader->graph = graph;
scene->shaders.push_back(shader);
scene->default_light = scene->shaders.size() - 1;
}
/* default background */
{
graph = new ShaderGraph();
closure = graph->add(new BackgroundNode());
closure->input("Color")->value = make_float3(0.8f, 0.8f, 0.8f);
out = graph->output();
graph->connect(closure->output("Background"), out->input("Surface"));
shader = new Shader();
shader->name = "default_background";
shader->graph = graph;
scene->shaders.push_back(shader);
scene->default_background = scene->shaders.size() - 1;
}
/* default holdout */
{
graph = new ShaderGraph();
closure = graph->add(new HoldoutNode());
out = graph->output();
graph->connect(closure->output("Holdout"), out->input("Surface"));
shader = new Shader();
shader->name = "default_holdout";
shader->graph = graph;
scene->shaders.push_back(shader);
scene->default_holdout = scene->shaders.size() - 1;
}
/* default empty */
{
graph = new ShaderGraph();
shader = new Shader();
shader->name = "default_empty";
shader->graph = graph;
scene->shaders.push_back(shader);
scene->default_empty = scene->shaders.size() - 1;
}
}
CCL_NAMESPACE_END
|