OLD | NEW |
1 /* | 1 /* |
2 Copyright (c) 2009-2010 Sony Pictures Imageworks Inc., et al. | 2 Copyright (c) 2009-2010 Sony Pictures Imageworks Inc., et al. |
3 All Rights Reserved. | 3 All Rights Reserved. |
4 | 4 |
5 Redistribution and use in source and binary forms, with or without | 5 Redistribution and use in source and binary forms, with or without |
6 modification, are permitted provided that the following conditions are | 6 modification, are permitted provided that the following conditions are |
7 met: | 7 met: |
8 * Redistributions of source code must retain the above copyright | 8 * Redistributions of source code must retain the above copyright |
9 notice, this list of conditions and the following disclaimer. | 9 notice, this list of conditions and the following disclaimer. |
10 * Redistributions in binary form must reproduce the above copyright | 10 * Redistributions in binary form must reproduce the above copyright |
(...skipping 15 matching lines...) Expand all Loading... |
26 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 26 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 */ | 27 */ |
28 | 28 |
29 #include <vector> | 29 #include <vector> |
30 #include <string> | 30 #include <string> |
31 #include <cstdio> | 31 #include <cstdio> |
32 | 32 |
33 #include <boost/foreach.hpp> | 33 #include <boost/foreach.hpp> |
34 | 34 |
35 #include <OpenImageIO/dassert.h> | 35 #include <OpenImageIO/dassert.h> |
| 36 #include <OpenImageIO/sysutil.h> |
36 | 37 |
37 #include "oslconfig.h" | 38 #include "oslconfig.h" |
38 #include "oslclosure.h" | 39 #include "oslclosure.h" |
39 #include "oslexec_pvt.h" | 40 #include "oslexec_pvt.h" |
40 | 41 |
41 | 42 |
42 | 43 |
43 #ifdef OSL_NAMESPACE | 44 #ifdef OSL_NAMESPACE |
44 namespace OSL_NAMESPACE { | 45 namespace OSL_NAMESPACE { |
45 #endif | 46 #endif |
(...skipping 22 matching lines...) Expand all Loading... |
68 m_components.push_back (Component (Color3 (1, 1, 1), 0)); | 69 m_components.push_back (Component (Color3 (1, 1, 1), 0)); |
69 | 70 |
70 // Return the block of memory for the caller to new the ClosurePrimitive int
o | 71 // Return the block of memory for the caller to new the ClosurePrimitive int
o |
71 return &m_mem[0]; | 72 return &m_mem[0]; |
72 } | 73 } |
73 | 74 |
74 | 75 |
75 void | 76 void |
76 ClosureColor::add (const ClosureColor &A) | 77 ClosureColor::add (const ClosureColor &A) |
77 { | 78 { |
| 79 // Look at all of A's components, decide which can be merged with our |
| 80 // own (just summing weights) and which need to be appended as new |
| 81 // closure primitives. |
| 82 int my_ncomponents = ncomponents(); // how many components I have now |
| 83 int num_unmerged = 0; // how many more I'll need |
| 84 size_t new_bytes = 0; // how much more mem I'll need |
| 85 int *unmerged = ALLOCA (int, A.ncomponents()); // temp index list |
| 86 for (int ac = 0; ac < A.ncomponents(); ++ac) { |
| 87 const ClosurePrimitive *aprim (A.prim (ac)); |
| 88 const Component &acomp (A.component (ac)); |
| 89 if (acomp.weight[0] == 0.0f && acomp.weight[1] == 0.0f && |
| 90 acomp.weight[2] == 0.0f) |
| 91 continue; // don't bother adding a 0-weighted component |
| 92 bool merged = false; |
| 93 for (int c = 0; c < my_ncomponents; ++c) { |
| 94 if (prim(c)->name() == aprim->name() && |
| 95 prim(c)->mergeable (aprim)) { |
| 96 // We can merge with an existing component -- just add the |
| 97 // weights |
| 98 m_components[c].weight += acomp.weight; |
| 99 merged = true; |
| 100 break; |
| 101 } |
| 102 } |
| 103 if (! merged) { |
| 104 // Not a duplicate that can be merged. Remember this component |
| 105 // index and how much memory it'll need. |
| 106 unmerged[num_unmerged++] = ac; |
| 107 new_bytes += aprim->memsize(); |
| 108 } |
| 109 } |
| 110 |
| 111 // If we've merged everything and don't need to append, we're done |
| 112 if (! num_unmerged) |
| 113 return; |
| 114 |
78 // Grow our memory | 115 // Grow our memory |
79 size_t num_bytes = A.m_mem.size (); | |
80 size_t oldmemsize = m_mem.size (); | 116 size_t oldmemsize = m_mem.size (); |
81 m_mem.resize (oldmemsize + num_bytes); | 117 m_mem.resize (oldmemsize + new_bytes); |
82 | 118 |
83 // Copy A's memory at the end of ours | 119 // Append the components of A that we couldn't merge. |
84 memcpy(&m_mem[oldmemsize], &A.m_mem[0], num_bytes); | 120 for (int i = 0; i < num_unmerged; ++i) { |
85 | 121 int c = unmerged[i]; // next unmerged component index within A |
86 // Copy A's components and adjust memory offsets to refer to new position | 122 const Component &acomp (A.component (c)); |
87 BOOST_FOREACH (const Component &c, A.m_components) { | 123 const ClosurePrimitive *aprim (A.prim (c)); |
88 m_components.push_back(c); | 124 size_t asize = aprim->memsize(); |
89 m_components.back().memoffset += oldmemsize; | 125 memcpy (&m_mem[oldmemsize], &A.m_mem[acomp.memoffset], asize); |
| 126 m_components.push_back (acomp); |
| 127 m_components.back().memoffset = oldmemsize; |
| 128 oldmemsize += asize; |
90 } | 129 } |
91 } | 130 } |
92 | 131 |
93 | 132 |
94 | 133 |
95 void | 134 void |
96 ClosureColor::add (const ClosureColor &A, const ClosureColor &B) | 135 ClosureColor::add (const ClosureColor &A, const ClosureColor &B) |
97 { | 136 { |
98 if (this != &A) | 137 if (this != &A) |
99 *this = A; | 138 *this = A; |
100 add (B); | 139 add (B); |
101 } | 140 } |
102 | 141 |
103 | 142 |
104 void | 143 void |
105 ClosureColor::mul (const Color3 &w) | 144 ClosureColor::mul (const Color3 &w) |
106 { | 145 { |
| 146 // Handle scale by 0 trivially |
| 147 if (w[0] == 0.0f && w[1] == 0.0f && w[2] == 0.0f) { |
| 148 clear(); |
| 149 return; |
| 150 } |
| 151 |
107 // For every component, scale it | 152 // For every component, scale it |
108 BOOST_FOREACH (Component &c, m_components) | 153 BOOST_FOREACH (Component &c, m_components) |
109 c.weight *= w; | 154 c.weight *= w; |
110 } | 155 } |
111 | 156 |
112 | 157 |
113 | 158 |
114 void | 159 void |
115 ClosureColor::mul (float w) | 160 ClosureColor::mul (float w) |
116 { | 161 { |
| 162 // Handle scale by 0 trivially |
| 163 if (w == 0.0f) { |
| 164 clear(); |
| 165 return; |
| 166 } |
| 167 |
117 // For every component, scale it | 168 // For every component, scale it |
118 BOOST_FOREACH (Component &c, m_components) | 169 BOOST_FOREACH (Component &c, m_components) |
119 c.weight *= w; | 170 c.weight *= w; |
120 } | 171 } |
121 | 172 |
122 | 173 |
123 | 174 |
124 std::ostream & | 175 std::ostream & |
125 operator<< (std::ostream &out, const ClosureColor &closure) | 176 operator<< (std::ostream &out, const ClosureColor &closure) |
126 { | 177 { |
(...skipping 22 matching lines...) Expand all Loading... |
149 const ustring Labels::GLOSSY = ustring("G"); | 200 const ustring Labels::GLOSSY = ustring("G"); |
150 const ustring Labels::SINGULAR = ustring("S"); | 201 const ustring Labels::SINGULAR = ustring("S"); |
151 const ustring Labels::STRAIGHT = ustring("s"); | 202 const ustring Labels::STRAIGHT = ustring("s"); |
152 const ustring Labels::STOP = ustring("__stop__"); | 203 const ustring Labels::STOP = ustring("__stop__"); |
153 | 204 |
154 | 205 |
155 }; // namespace OSL | 206 }; // namespace OSL |
156 #ifdef OSL_NAMESPACE | 207 #ifdef OSL_NAMESPACE |
157 }; // end namespace OSL_NAMESPACE | 208 }; // end namespace OSL_NAMESPACE |
158 #endif | 209 #endif |
OLD | NEW |