Cycles OSL minor optimizations: recycle shading context, don't do memory
[blender.git] / intern / cycles / kernel / osl / osl_globals.h
1 /*
2  * Copyright 2011, Blender Foundation.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18
19 #ifndef __OSL_GLOBALS_H__
20 #define __OSL_GLOBALS_H__
21
22 #ifdef WITH_OSL
23
24 #include <OSL/oslexec.h>
25 #include <cmath>
26
27 #include "util_map.h"
28 #include "util_param.h"
29 #include "util_thread.h"
30 #include "util_vector.h"
31
32 #ifndef WIN32
33 using std::isfinite;
34 #endif
35
36 CCL_NAMESPACE_BEGIN
37
38 class OSLRenderServices;
39
40 struct OSLGlobals {
41         OSLGlobals()
42         {
43                 ss = NULL;
44                 ts = NULL;
45                 services = NULL;
46                 use = false;
47         }
48
49         bool use;
50
51         /* shading system */
52         OSL::ShadingSystem *ss;
53         OSL::TextureSystem *ts;
54         OSLRenderServices *services;
55
56         /* shader states */
57         vector<OSL::ShadingAttribStateRef> surface_state;
58         vector<OSL::ShadingAttribStateRef> volume_state;
59         vector<OSL::ShadingAttribStateRef> displacement_state;
60         OSL::ShadingAttribStateRef background_state;
61
62         /* attributes */
63         struct Attribute {
64                 TypeDesc type;
65                 AttributeElement elem;
66                 int offset;
67                 ParamValue value;
68         };
69
70         typedef unordered_map<ustring, Attribute, ustringHash> AttributeMap;
71         typedef unordered_map<ustring, int, ustringHash> ObjectNameMap;
72
73         vector<AttributeMap> attribute_map;
74         ObjectNameMap object_name_map;
75         vector<ustring> object_names;
76 };
77
78 /* trace() call result */
79 struct OSLTraceData {
80         Ray ray;
81         Intersection isect;
82         ShaderData sd;
83         bool setup;
84         bool init;
85 };
86
87 /* thread key for thread specific data lookup */
88 struct OSLThreadData {
89         OSL::ShaderGlobals globals;
90         OSL::PerThreadInfo *thread_info;
91         OSLTraceData tracedata;
92         OSL::ShadingContext *context[SHADER_CONTEXT_NUM];
93 };
94
95 CCL_NAMESPACE_END
96
97 #endif
98
99 #endif /* __OSL_GLOBALS_H__ */
100