|
1 /* |
|
2 * Copyright (C) 2009 Apple Inc. All rights reserved. |
|
3 * |
|
4 * Redistribution and use in source and binary forms, with or without |
|
5 * modification, are permitted provided that the following conditions |
|
6 * are met: |
|
7 * 1. Redistributions of source code must retain the above copyright |
|
8 * notice, this list of conditions and the following disclaimer. |
|
9 * 2. Redistributions in binary form must reproduce the above copyright |
|
10 * notice, this list of conditions and the following disclaimer in the |
|
11 * documentation and/or other materials provided with the distribution. |
|
12 * |
|
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY |
|
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
|
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR |
|
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
|
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
|
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
|
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
|
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
24 */ |
|
25 |
|
26 #include "config.h" |
|
27 |
|
28 #if ENABLE(3D_CANVAS) |
|
29 |
|
30 #include "WebGLTexture.h" |
|
31 #include "WebGLRenderingContext.h" |
|
32 |
|
33 namespace WebCore { |
|
34 |
|
35 PassRefPtr<WebGLTexture> WebGLTexture::create(WebGLRenderingContext* ctx) |
|
36 { |
|
37 return adoptRef(new WebGLTexture(ctx)); |
|
38 } |
|
39 |
|
40 WebGLTexture::WebGLTexture(WebGLRenderingContext* ctx) |
|
41 : CanvasObject(ctx) |
|
42 , cubeMapRWrapModeInitialized(false) |
|
43 , m_target(0) |
|
44 , m_minFilter(GraphicsContext3D::NEAREST_MIPMAP_LINEAR) |
|
45 , m_magFilter(GraphicsContext3D::LINEAR) |
|
46 , m_wrapS(GraphicsContext3D::REPEAT) |
|
47 , m_wrapT(GraphicsContext3D::REPEAT) |
|
48 , m_isNPOT(false) |
|
49 , m_isComplete(false) |
|
50 , m_needToUseBlackTexture(false) |
|
51 { |
|
52 setObject(context()->graphicsContext3D()->createTexture()); |
|
53 } |
|
54 |
|
55 void WebGLTexture::setTarget(unsigned long target, int maxLevel) |
|
56 { |
|
57 if (!object()) |
|
58 return; |
|
59 // Target is finalized the first time bindTexture() is called. |
|
60 if (m_target) |
|
61 return; |
|
62 switch (target) { |
|
63 case GraphicsContext3D::TEXTURE_2D: |
|
64 m_target = target; |
|
65 m_info.resize(1); |
|
66 m_info[0].resize(maxLevel); |
|
67 break; |
|
68 case GraphicsContext3D::TEXTURE_CUBE_MAP: |
|
69 m_target = target; |
|
70 m_info.resize(6); |
|
71 for (int ii = 0; ii < 6; ++ii) |
|
72 m_info[ii].resize(maxLevel); |
|
73 break; |
|
74 } |
|
75 } |
|
76 |
|
77 void WebGLTexture::setParameteri(unsigned long pname, int param) |
|
78 { |
|
79 if (!object() || !m_target) |
|
80 return; |
|
81 switch (pname) { |
|
82 case GraphicsContext3D::TEXTURE_MIN_FILTER: |
|
83 switch (param) { |
|
84 case GraphicsContext3D::NEAREST: |
|
85 case GraphicsContext3D::LINEAR: |
|
86 case GraphicsContext3D::NEAREST_MIPMAP_NEAREST: |
|
87 case GraphicsContext3D::LINEAR_MIPMAP_NEAREST: |
|
88 case GraphicsContext3D::NEAREST_MIPMAP_LINEAR: |
|
89 case GraphicsContext3D::LINEAR_MIPMAP_LINEAR: |
|
90 m_minFilter = param; |
|
91 break; |
|
92 } |
|
93 break; |
|
94 case GraphicsContext3D::TEXTURE_MAG_FILTER: |
|
95 switch (param) { |
|
96 case GraphicsContext3D::NEAREST: |
|
97 case GraphicsContext3D::LINEAR: |
|
98 m_magFilter = param; |
|
99 break; |
|
100 } |
|
101 break; |
|
102 case GraphicsContext3D::TEXTURE_WRAP_S: |
|
103 switch (param) { |
|
104 case GraphicsContext3D::CLAMP_TO_EDGE: |
|
105 case GraphicsContext3D::MIRRORED_REPEAT: |
|
106 case GraphicsContext3D::REPEAT: |
|
107 m_wrapS = param; |
|
108 break; |
|
109 } |
|
110 break; |
|
111 case GraphicsContext3D::TEXTURE_WRAP_T: |
|
112 switch (param) { |
|
113 case GraphicsContext3D::CLAMP_TO_EDGE: |
|
114 case GraphicsContext3D::MIRRORED_REPEAT: |
|
115 case GraphicsContext3D::REPEAT: |
|
116 m_wrapT = param; |
|
117 break; |
|
118 } |
|
119 break; |
|
120 default: |
|
121 return; |
|
122 } |
|
123 update(); |
|
124 } |
|
125 |
|
126 void WebGLTexture::setParameterf(unsigned long pname, float param) |
|
127 { |
|
128 if (!object() || !m_target) |
|
129 return; |
|
130 int iparam = static_cast<int>(param); |
|
131 setParameteri(pname, iparam); |
|
132 } |
|
133 |
|
134 void WebGLTexture::setLevelInfo(unsigned long target, int level, unsigned long internalFormat, int width, int height, unsigned long type) |
|
135 { |
|
136 if (!object() || !m_target) |
|
137 return; |
|
138 // We assume level, internalFormat, width, height, and type have all been |
|
139 // validated already. |
|
140 int index = mapTargetToIndex(target); |
|
141 if (index < 0) |
|
142 return; |
|
143 m_info[index][level].setInfo(internalFormat, width, height, type); |
|
144 update(); |
|
145 } |
|
146 |
|
147 void WebGLTexture::generateMipmapLevelInfo() |
|
148 { |
|
149 if (!object() || !m_target) |
|
150 return; |
|
151 if (!canGenerateMipmaps()) |
|
152 return; |
|
153 if (m_isComplete) |
|
154 return; |
|
155 for (size_t ii = 0; ii < m_info.size(); ++ii) { |
|
156 const LevelInfo& info0 = m_info[ii][0]; |
|
157 int width = info0.width; |
|
158 int height = info0.height; |
|
159 int levelCount = computeLevelCount(width, height); |
|
160 for (int level = 1; level < levelCount; ++level) { |
|
161 width = std::max(1, width >> 1); |
|
162 height = std::max(1, height >> 1); |
|
163 LevelInfo& info = m_info[ii][level]; |
|
164 info.setInfo(info0.internalFormat, width, height, info0.type); |
|
165 } |
|
166 } |
|
167 m_isComplete = true; |
|
168 } |
|
169 |
|
170 unsigned long WebGLTexture::getInternalFormat() const |
|
171 { |
|
172 if (!object() || !m_target) |
|
173 return 0; |
|
174 return m_info[0][0].internalFormat; |
|
175 } |
|
176 |
|
177 bool WebGLTexture::isNPOT(unsigned width, unsigned height) |
|
178 { |
|
179 if (!width || !height) |
|
180 return false; |
|
181 if ((width & (width - 1)) || (height & (height - 1))) |
|
182 return true; |
|
183 return false; |
|
184 } |
|
185 |
|
186 bool WebGLTexture::isNPOT() const |
|
187 { |
|
188 if (!object()) |
|
189 return false; |
|
190 return m_isNPOT; |
|
191 } |
|
192 |
|
193 bool WebGLTexture::needToUseBlackTexture() const |
|
194 { |
|
195 if (!object()) |
|
196 return false; |
|
197 return m_needToUseBlackTexture; |
|
198 } |
|
199 |
|
200 void WebGLTexture::_deleteObject(Platform3DObject object) |
|
201 { |
|
202 context()->graphicsContext3D()->deleteTexture(object); |
|
203 } |
|
204 |
|
205 int WebGLTexture::mapTargetToIndex(unsigned long target) |
|
206 { |
|
207 if (m_target == GraphicsContext3D::TEXTURE_2D) { |
|
208 if (target == GraphicsContext3D::TEXTURE_2D) |
|
209 return 0; |
|
210 } else if (m_target == GraphicsContext3D::TEXTURE_CUBE_MAP) { |
|
211 switch (target) { |
|
212 case GraphicsContext3D::TEXTURE_CUBE_MAP_POSITIVE_X: |
|
213 return 0; |
|
214 case GraphicsContext3D::TEXTURE_CUBE_MAP_NEGATIVE_X: |
|
215 return 1; |
|
216 case GraphicsContext3D::TEXTURE_CUBE_MAP_POSITIVE_Y: |
|
217 return 2; |
|
218 case GraphicsContext3D::TEXTURE_CUBE_MAP_NEGATIVE_Y: |
|
219 return 3; |
|
220 case GraphicsContext3D::TEXTURE_CUBE_MAP_POSITIVE_Z: |
|
221 return 4; |
|
222 case GraphicsContext3D::TEXTURE_CUBE_MAP_NEGATIVE_Z: |
|
223 return 5; |
|
224 } |
|
225 } |
|
226 return -1; |
|
227 } |
|
228 |
|
229 bool WebGLTexture::canGenerateMipmaps() |
|
230 { |
|
231 if (isNPOT()) |
|
232 return false; |
|
233 const LevelInfo& first = m_info[0][0]; |
|
234 for (size_t ii = 0; ii < m_info.size(); ++ii) { |
|
235 const LevelInfo& info = m_info[ii][0]; |
|
236 if (!info.valid |
|
237 || info.width != first.width || info.height != first.height |
|
238 || info.internalFormat != first.internalFormat || info.type != first.type) |
|
239 return false; |
|
240 } |
|
241 return true; |
|
242 } |
|
243 |
|
244 int WebGLTexture::computeLevelCount(int width, int height) |
|
245 { |
|
246 // return 1 + log2Floor(std::max(width, height)); |
|
247 int n = std::max(width, height); |
|
248 if (n <= 0) |
|
249 return 0; |
|
250 int log = 0; |
|
251 int value = n; |
|
252 for (int ii = 4; ii >= 0; --ii) { |
|
253 int shift = (1 << ii); |
|
254 int x = (value >> shift); |
|
255 if (x) { |
|
256 value = x; |
|
257 log += shift; |
|
258 } |
|
259 } |
|
260 ASSERT(value == 1); |
|
261 return log + 1; |
|
262 } |
|
263 |
|
264 void WebGLTexture::update() |
|
265 { |
|
266 m_isNPOT = false; |
|
267 for (size_t ii = 0; ii < m_info.size(); ++ii) { |
|
268 if (isNPOT(m_info[ii][0].width, m_info[ii][0].height)) { |
|
269 m_isNPOT = true; |
|
270 break; |
|
271 } |
|
272 } |
|
273 m_isComplete = true; |
|
274 const LevelInfo& first = m_info[0][0]; |
|
275 int levelCount = computeLevelCount(first.width, first.height); |
|
276 if (levelCount < 1) |
|
277 m_isComplete = false; |
|
278 else { |
|
279 for (size_t ii = 0; ii < m_info.size() && m_isComplete; ++ii) { |
|
280 const LevelInfo& info0 = m_info[ii][0]; |
|
281 if (!info0.valid |
|
282 || info0.width != first.width || info0.height != first.height |
|
283 || info0.internalFormat != first.internalFormat || info0.type != first.type) { |
|
284 m_isComplete = false; |
|
285 break; |
|
286 } |
|
287 int width = info0.width; |
|
288 int height = info0.height; |
|
289 for (int level = 1; level < levelCount; ++level) { |
|
290 width = std::max(1, width >> 1); |
|
291 height = std::max(1, height >> 1); |
|
292 const LevelInfo& info = m_info[ii][level]; |
|
293 if (!info.valid |
|
294 || info.width != width || info.height != height |
|
295 || info.internalFormat != info0.internalFormat || info.type != info0.type) { |
|
296 m_isComplete = false; |
|
297 break; |
|
298 } |
|
299 |
|
300 } |
|
301 } |
|
302 } |
|
303 |
|
304 m_needToUseBlackTexture = false; |
|
305 // NPOT |
|
306 if (m_isNPOT && ((m_minFilter != GraphicsContext3D::NEAREST && m_minFilter != GraphicsContext3D::LINEAR) |
|
307 || m_wrapS != GraphicsContext3D::CLAMP_TO_EDGE || m_wrapT != GraphicsContext3D::CLAMP_TO_EDGE)) |
|
308 m_needToUseBlackTexture = true; |
|
309 // Completeness |
|
310 if (!m_isComplete && m_minFilter != GraphicsContext3D::NEAREST && m_minFilter != GraphicsContext3D::LINEAR) |
|
311 m_needToUseBlackTexture = true; |
|
312 } |
|
313 |
|
314 } |
|
315 |
|
316 #endif // ENABLE(3D_CANVAS) |