blob: d656321267c586c209c31c9810114cd5524b2d26 [file] [log] [blame]
Geoff Langf9a6f082015-01-22 13:32:49 -05001//
2// Copyright 2015 The ANGLE Project Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7// TextureGL.cpp: Implements the class methods for TextureGL.
8
9#include "libANGLE/renderer/gl/TextureGL.h"
10
11#include "common/debug.h"
Geoff Langc05f7062015-03-10 09:50:57 -070012#include "common/utilities.h"
Jamie Madill67102f02015-03-16 10:41:42 -040013#include "libANGLE/State.h"
Geoff Langc05f7062015-03-10 09:50:57 -070014#include "libANGLE/angletypes.h"
15#include "libANGLE/formatutils.h"
16#include "libANGLE/renderer/gl/BufferGL.h"
Geoff Langfbfa47c2015-03-31 11:26:00 -040017#include "libANGLE/renderer/gl/FramebufferGL.h"
Geoff Langc05f7062015-03-10 09:50:57 -070018#include "libANGLE/renderer/gl/FunctionsGL.h"
19#include "libANGLE/renderer/gl/StateManagerGL.h"
Geoff Langfd216c42015-05-27 16:12:30 -040020#include "libANGLE/renderer/gl/formatutilsgl.h"
Geoff Langf9a6f082015-01-22 13:32:49 -050021
22namespace rx
23{
24
Geoff Langc05f7062015-03-10 09:50:57 -070025static void SetUnpackStateForTexImage(StateManagerGL *stateManager, const gl::PixelUnpackState &unpack)
26{
27 const gl::Buffer *unpackBuffer = unpack.pixelBuffer.get();
28 if (unpackBuffer != nullptr)
29 {
30 UNIMPLEMENTED();
31 }
Geoff Lang5ec53c82015-06-05 16:32:14 -040032 stateManager->setPixelUnpackState(unpack.alignment, unpack.rowLength, unpack.skipRows,
33 unpack.skipPixels, unpack.imageHeight, unpack.skipImages);
Geoff Langc05f7062015-03-10 09:50:57 -070034}
35
36static bool UseTexImage2D(GLenum textureType)
37{
38 return textureType == GL_TEXTURE_2D || textureType == GL_TEXTURE_CUBE_MAP;
39}
40
41static bool UseTexImage3D(GLenum textureType)
42{
43 return textureType == GL_TEXTURE_2D_ARRAY || textureType == GL_TEXTURE_3D;
44}
45
Geoff Lang968992e2015-03-17 18:01:49 -040046static bool CompatibleTextureTarget(GLenum textureType, GLenum textureTarget)
Geoff Langc05f7062015-03-10 09:50:57 -070047{
48 if (textureType != GL_TEXTURE_CUBE_MAP)
49 {
50 return textureType == textureTarget;
51 }
52 else
53 {
Geoff Langfb2a5592015-03-20 11:25:37 -040054 return gl::IsCubeMapTextureTarget(textureTarget);
Geoff Langc05f7062015-03-10 09:50:57 -070055 }
56}
57
Geoff Lang8d8044f2015-07-23 10:57:20 -040058static const nativegl::InternalFormat &GetNativeInternalFormat(const FunctionsGL *functions,
59 GLenum internalFormat,
60 GLenum type)
61{
62 const gl::InternalFormat &formatInfo = gl::GetInternalFormatInfo(internalFormat);
63 if (functions->standard == STANDARD_GL_DESKTOP && formatInfo.pixelBytes == 0 &&
64 formatInfo.componentType == GL_UNSIGNED_NORMALIZED)
65 {
66 // When the format is unsized with an unsized normalized type, let the driver choose the
67 // format. This works around some issues with rounding on very low precision types.
68 return nativegl::GetInternalFormatInfo(internalFormat, functions->standard);
69 }
70 else if (functions->standard == STANDARD_GL_DESKTOP ||
71 functions->isAtLeastGLES(gl::Version(3, 0)))
72 {
73 // On Desktop GL, use sized internal formats when the component type is not unsigned
74 // normalized. Passing an internal format of GL_RGBA will generate a GL_RGBA8 texture
75 // even if the provided type is GL_FLOAT.
76 GLenum sizedFormat = gl::GetSizedInternalFormat(internalFormat, type);
77 return nativegl::GetInternalFormatInfo(sizedFormat, functions->standard);
78 }
79 else
80 {
81 // For ES2, just pass what was provided.
82 return nativegl::GetInternalFormatInfo(internalFormat, functions->standard);
83 }
84}
85
Geoff Langc05f7062015-03-10 09:50:57 -070086TextureGL::TextureGL(GLenum type, const FunctionsGL *functions, StateManagerGL *stateManager)
87 : TextureImpl(),
88 mTextureType(type),
89 mFunctions(functions),
90 mStateManager(stateManager),
91 mAppliedSamplerState(),
92 mTextureID(0)
93{
94 ASSERT(mFunctions);
95 ASSERT(mStateManager);
96
97 mFunctions->genTextures(1, &mTextureID);
Geoff Lang90d98af2015-05-26 16:41:38 -040098 mStateManager->bindTexture(mTextureType, mTextureID);
Geoff Langc05f7062015-03-10 09:50:57 -070099}
Geoff Langf9a6f082015-01-22 13:32:49 -0500100
101TextureGL::~TextureGL()
Geoff Langc05f7062015-03-10 09:50:57 -0700102{
Geoff Lang1eb708e2015-05-04 14:58:23 -0400103 mStateManager->deleteTexture(mTextureID);
104 mTextureID = 0;
Geoff Langc05f7062015-03-10 09:50:57 -0700105}
Geoff Langf9a6f082015-01-22 13:32:49 -0500106
107void TextureGL::setUsage(GLenum usage)
108{
Geoff Langc05f7062015-03-10 09:50:57 -0700109 // GL_ANGLE_texture_usage not implemented for desktop GL
110 UNREACHABLE();
Geoff Langf9a6f082015-01-22 13:32:49 -0500111}
112
113gl::Error TextureGL::setImage(GLenum target, size_t level, GLenum internalFormat, const gl::Extents &size, GLenum format, GLenum type,
114 const gl::PixelUnpackState &unpack, const uint8_t *pixels)
115{
Geoff Langfb2a5592015-03-20 11:25:37 -0400116 UNUSED_ASSERTION_VARIABLE(&CompatibleTextureTarget); // Reference this function to avoid warnings.
Geoff Lang968992e2015-03-17 18:01:49 -0400117 ASSERT(CompatibleTextureTarget(mTextureType, target));
Geoff Langc05f7062015-03-10 09:50:57 -0700118
119 SetUnpackStateForTexImage(mStateManager, unpack);
120
Geoff Lang8d8044f2015-07-23 10:57:20 -0400121 const nativegl::InternalFormat &nativeInternalFormatInfo =
122 GetNativeInternalFormat(mFunctions, internalFormat, type);
Geoff Langfd216c42015-05-27 16:12:30 -0400123
Geoff Langc05f7062015-03-10 09:50:57 -0700124 mStateManager->bindTexture(mTextureType, mTextureID);
125 if (UseTexImage2D(mTextureType))
Jamie Madill67102f02015-03-16 10:41:42 -0400126 {
Geoff Langc05f7062015-03-10 09:50:57 -0700127 ASSERT(size.depth == 1);
Geoff Langfd216c42015-05-27 16:12:30 -0400128 mFunctions->texImage2D(target, level, nativeInternalFormatInfo.internalFormat, size.width, size.height, 0, format, type, pixels);
Geoff Langc05f7062015-03-10 09:50:57 -0700129 }
130 else if (UseTexImage3D(mTextureType))
131 {
Geoff Langfd216c42015-05-27 16:12:30 -0400132 mFunctions->texImage3D(target, level, nativeInternalFormatInfo.internalFormat, size.width, size.height, size.depth, 0, format, type, pixels);
Geoff Langc05f7062015-03-10 09:50:57 -0700133 }
134 else
135 {
136 UNREACHABLE();
Jamie Madill67102f02015-03-16 10:41:42 -0400137 }
138
Geoff Langc05f7062015-03-10 09:50:57 -0700139 return gl::Error(GL_NO_ERROR);
Geoff Langf9a6f082015-01-22 13:32:49 -0500140}
141
142gl::Error TextureGL::setSubImage(GLenum target, size_t level, const gl::Box &area, GLenum format, GLenum type,
143 const gl::PixelUnpackState &unpack, const uint8_t *pixels)
144{
Geoff Lang968992e2015-03-17 18:01:49 -0400145 ASSERT(CompatibleTextureTarget(mTextureType, target));
Geoff Langc05f7062015-03-10 09:50:57 -0700146
147 SetUnpackStateForTexImage(mStateManager, unpack);
148
149 mStateManager->bindTexture(mTextureType, mTextureID);
150 if (UseTexImage2D(mTextureType))
Jamie Madill67102f02015-03-16 10:41:42 -0400151 {
Geoff Langc05f7062015-03-10 09:50:57 -0700152 ASSERT(area.z == 0 && area.depth == 1);
153 mFunctions->texSubImage2D(target, level, area.x, area.y, area.width, area.height, format, type, pixels);
154 }
155 else if (UseTexImage3D(mTextureType))
156 {
157 mFunctions->texSubImage3D(target, level, area.x, area.y, area.z, area.width, area.height, area.depth,
158 format, type, pixels);
159 }
160 else
161 {
162 UNREACHABLE();
Jamie Madill67102f02015-03-16 10:41:42 -0400163 }
164
Geoff Langc05f7062015-03-10 09:50:57 -0700165 return gl::Error(GL_NO_ERROR);
Geoff Langf9a6f082015-01-22 13:32:49 -0500166}
167
168gl::Error TextureGL::setCompressedImage(GLenum target, size_t level, GLenum internalFormat, const gl::Extents &size,
Geoff Lang8509d862015-05-20 14:06:13 -0400169 const gl::PixelUnpackState &unpack, size_t imageSize, const uint8_t *pixels)
Geoff Langf9a6f082015-01-22 13:32:49 -0500170{
Geoff Lang968992e2015-03-17 18:01:49 -0400171 ASSERT(CompatibleTextureTarget(mTextureType, target));
Geoff Langc05f7062015-03-10 09:50:57 -0700172
173 SetUnpackStateForTexImage(mStateManager, unpack);
174
Geoff Lang8d8044f2015-07-23 10:57:20 -0400175 const nativegl::InternalFormat &nativeInternalFormatInfo =
176 GetNativeInternalFormat(mFunctions, internalFormat, GL_UNSIGNED_BYTE);
Geoff Langfd216c42015-05-27 16:12:30 -0400177
Geoff Langc05f7062015-03-10 09:50:57 -0700178 mStateManager->bindTexture(mTextureType, mTextureID);
179 if (UseTexImage2D(mTextureType))
Jamie Madill67102f02015-03-16 10:41:42 -0400180 {
Geoff Langc05f7062015-03-10 09:50:57 -0700181 ASSERT(size.depth == 1);
Geoff Langfd216c42015-05-27 16:12:30 -0400182 mFunctions->compressedTexImage2D(target, level, nativeInternalFormatInfo.internalFormat, size.width, size.height, 0, imageSize, pixels);
Geoff Langc05f7062015-03-10 09:50:57 -0700183 }
184 else if (UseTexImage3D(mTextureType))
185 {
Geoff Langfd216c42015-05-27 16:12:30 -0400186 mFunctions->compressedTexImage3D(target, level, nativeInternalFormatInfo.internalFormat, size.width, size.height, size.depth, 0,
Geoff Lang8509d862015-05-20 14:06:13 -0400187 imageSize, pixels);
Geoff Langc05f7062015-03-10 09:50:57 -0700188 }
189 else
190 {
191 UNREACHABLE();
Jamie Madill67102f02015-03-16 10:41:42 -0400192 }
193
Geoff Langc05f7062015-03-10 09:50:57 -0700194 return gl::Error(GL_NO_ERROR);
Geoff Langf9a6f082015-01-22 13:32:49 -0500195}
196
197gl::Error TextureGL::setCompressedSubImage(GLenum target, size_t level, const gl::Box &area, GLenum format,
Geoff Lang8509d862015-05-20 14:06:13 -0400198 const gl::PixelUnpackState &unpack, size_t imageSize, const uint8_t *pixels)
Geoff Langf9a6f082015-01-22 13:32:49 -0500199{
Geoff Lang968992e2015-03-17 18:01:49 -0400200 ASSERT(CompatibleTextureTarget(mTextureType, target));
Geoff Langc05f7062015-03-10 09:50:57 -0700201
202 SetUnpackStateForTexImage(mStateManager, unpack);
203
Geoff Langc05f7062015-03-10 09:50:57 -0700204 mStateManager->bindTexture(mTextureType, mTextureID);
205 if (UseTexImage2D(mTextureType))
Jamie Madill67102f02015-03-16 10:41:42 -0400206 {
Geoff Langc05f7062015-03-10 09:50:57 -0700207 ASSERT(area.z == 0 && area.depth == 1);
Geoff Lang8d8044f2015-07-23 10:57:20 -0400208 mFunctions->compressedTexSubImage2D(target, level, area.x, area.y, area.width, area.height,
209 format, imageSize, pixels);
Geoff Langc05f7062015-03-10 09:50:57 -0700210 }
211 else if (UseTexImage3D(mTextureType))
212 {
Geoff Lang8d8044f2015-07-23 10:57:20 -0400213 mFunctions->compressedTexSubImage3D(target, level, area.x, area.y, area.z, area.width,
214 area.height, area.depth, format, imageSize, pixels);
Geoff Langc05f7062015-03-10 09:50:57 -0700215 }
216 else
217 {
218 UNREACHABLE();
Jamie Madill67102f02015-03-16 10:41:42 -0400219 }
220
Geoff Langc05f7062015-03-10 09:50:57 -0700221 return gl::Error(GL_NO_ERROR);
Geoff Langf9a6f082015-01-22 13:32:49 -0500222}
223
224gl::Error TextureGL::copyImage(GLenum target, size_t level, const gl::Rectangle &sourceArea, GLenum internalFormat,
225 const gl::Framebuffer *source)
226{
Geoff Lang8d8044f2015-07-23 10:57:20 -0400227 const nativegl::InternalFormat &nativeInternalFormatInfo = GetNativeInternalFormat(
228 mFunctions, internalFormat, source->getImplementationColorReadType());
Geoff Langfd216c42015-05-27 16:12:30 -0400229
Geoff Langfbfa47c2015-03-31 11:26:00 -0400230 const FramebufferGL *sourceFramebufferGL = GetImplAs<FramebufferGL>(source);
231
232 mStateManager->bindTexture(mTextureType, mTextureID);
233 mStateManager->bindFramebuffer(GL_READ_FRAMEBUFFER, sourceFramebufferGL->getFramebufferID());
234
235 if (UseTexImage2D(mTextureType))
236 {
Geoff Langfd216c42015-05-27 16:12:30 -0400237 mFunctions->copyTexImage2D(target, level, nativeInternalFormatInfo.internalFormat, sourceArea.x, sourceArea.y,
Geoff Langfbfa47c2015-03-31 11:26:00 -0400238 sourceArea.width, sourceArea.height, 0);
239 }
240 else
241 {
242 UNREACHABLE();
243 }
244
245 return gl::Error(GL_NO_ERROR);
Geoff Langf9a6f082015-01-22 13:32:49 -0500246}
247
248gl::Error TextureGL::copySubImage(GLenum target, size_t level, const gl::Offset &destOffset, const gl::Rectangle &sourceArea,
249 const gl::Framebuffer *source)
250{
Geoff Langfbfa47c2015-03-31 11:26:00 -0400251 const FramebufferGL *sourceFramebufferGL = GetImplAs<FramebufferGL>(source);
252
253 mStateManager->bindTexture(mTextureType, mTextureID);
254 mStateManager->bindFramebuffer(GL_READ_FRAMEBUFFER, sourceFramebufferGL->getFramebufferID());
255
256 if (UseTexImage2D(mTextureType))
257 {
258 ASSERT(destOffset.z == 0);
259 mFunctions->copyTexSubImage2D(target, level, destOffset.x, destOffset.y,
260 sourceArea.x, sourceArea.y, sourceArea.width, sourceArea.height);
261 }
262 else if (UseTexImage3D(mTextureType))
263 {
264 mFunctions->copyTexSubImage3D(target, level, destOffset.x, destOffset.y, destOffset.z,
265 sourceArea.x, sourceArea.y, sourceArea.width, sourceArea.height);
266 }
267 else
268 {
269 UNREACHABLE();
270 }
271
272 return gl::Error(GL_NO_ERROR);
Geoff Langf9a6f082015-01-22 13:32:49 -0500273}
274
275gl::Error TextureGL::setStorage(GLenum target, size_t levels, GLenum internalFormat, const gl::Extents &size)
276{
Geoff Langc05f7062015-03-10 09:50:57 -0700277 // TODO: emulate texture storage with TexImage calls if on GL version <4.2 or the
278 // ARB_texture_storage extension is not available.
279
Geoff Langfd216c42015-05-27 16:12:30 -0400280 const nativegl::InternalFormat &nativeInternalFormatInfo = nativegl::GetInternalFormatInfo(internalFormat, mFunctions->standard);
281
Geoff Langc05f7062015-03-10 09:50:57 -0700282 mStateManager->bindTexture(mTextureType, mTextureID);
283 if (UseTexImage2D(mTextureType))
284 {
285 ASSERT(size.depth == 1);
Geoff Lang1c0ad622015-03-24 10:27:45 -0400286 if (mFunctions->texStorage2D)
287 {
Geoff Langfd216c42015-05-27 16:12:30 -0400288 mFunctions->texStorage2D(target, levels, nativeInternalFormatInfo.internalFormat, size.width, size.height);
Geoff Lang1c0ad622015-03-24 10:27:45 -0400289 }
290 else
291 {
292 // Make sure no pixel unpack buffer is bound
293 mStateManager->bindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
294
295 const gl::InternalFormat &internalFormatInfo = gl::GetInternalFormatInfo(internalFormat);
296
297 // Internal format must be sized
298 ASSERT(internalFormatInfo.pixelBytes != 0);
299
300 for (size_t level = 0; level < levels; level++)
301 {
302 gl::Extents levelSize(std::max(size.width >> level, 1),
303 std::max(size.height >> level, 1),
304 1);
305
306 if (mTextureType == GL_TEXTURE_2D)
307 {
Geoff Lang42c98f62015-05-20 14:09:25 -0400308 if (internalFormatInfo.compressed)
309 {
310 size_t dataSize = internalFormatInfo.computeBlockSize(GL_UNSIGNED_BYTE, levelSize.width, levelSize.height);
Geoff Langfd216c42015-05-27 16:12:30 -0400311 mFunctions->compressedTexImage2D(target, level, nativeInternalFormatInfo.internalFormat, levelSize.width, levelSize.height,
Geoff Lang42c98f62015-05-20 14:09:25 -0400312 0, dataSize, nullptr);
313 }
314 else
315 {
Geoff Langfd216c42015-05-27 16:12:30 -0400316 mFunctions->texImage2D(target, level, nativeInternalFormatInfo.internalFormat, levelSize.width, levelSize.height,
Geoff Lang42c98f62015-05-20 14:09:25 -0400317 0, internalFormatInfo.format, internalFormatInfo.type, nullptr);
318 }
Geoff Lang1c0ad622015-03-24 10:27:45 -0400319 }
320 else if (mTextureType == GL_TEXTURE_CUBE_MAP)
321 {
322 for (GLenum face = gl::FirstCubeMapTextureTarget; face <= gl::LastCubeMapTextureTarget; face++)
323 {
Geoff Lang42c98f62015-05-20 14:09:25 -0400324 if (internalFormatInfo.compressed)
325 {
326 size_t dataSize = internalFormatInfo.computeBlockSize(GL_UNSIGNED_BYTE, levelSize.width, levelSize.height);
Geoff Langfd216c42015-05-27 16:12:30 -0400327 mFunctions->compressedTexImage2D(face, level, nativeInternalFormatInfo.internalFormat, levelSize.width, levelSize.height,
Geoff Lang42c98f62015-05-20 14:09:25 -0400328 0, dataSize, nullptr);
329 }
330 else
331 {
Geoff Langfd216c42015-05-27 16:12:30 -0400332 mFunctions->texImage2D(face, level, nativeInternalFormatInfo.internalFormat, levelSize.width, levelSize.height,
Geoff Lang42c98f62015-05-20 14:09:25 -0400333 0, internalFormatInfo.format, internalFormatInfo.type, nullptr);
334 }
Geoff Lang1c0ad622015-03-24 10:27:45 -0400335 }
336 }
337 else
338 {
339 UNREACHABLE();
340 }
341 }
342 }
Geoff Langc05f7062015-03-10 09:50:57 -0700343 }
344 else if (UseTexImage3D(mTextureType))
345 {
Geoff Lang1c0ad622015-03-24 10:27:45 -0400346 if (mFunctions->texStorage3D)
347 {
Geoff Langfd216c42015-05-27 16:12:30 -0400348 mFunctions->texStorage3D(target, levels, nativeInternalFormatInfo.internalFormat, size.width, size.height, size.depth);
Geoff Lang1c0ad622015-03-24 10:27:45 -0400349 }
350 else
351 {
352 // Make sure no pixel unpack buffer is bound
353 mStateManager->bindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
354
355 const gl::InternalFormat &internalFormatInfo = gl::GetInternalFormatInfo(internalFormat);
356
357 // Internal format must be sized
358 ASSERT(internalFormatInfo.pixelBytes != 0);
359
360 for (size_t i = 0; i < levels; i++)
361 {
362 gl::Extents levelSize(std::max(size.width >> i, 1),
363 std::max(size.height >> i, 1),
364 mTextureType == GL_TEXTURE_3D ? std::max(size.depth >> i, 1) : size.depth);
365
Geoff Lang42c98f62015-05-20 14:09:25 -0400366 if (internalFormatInfo.compressed)
367 {
368 size_t dataSize = internalFormatInfo.computeBlockSize(GL_UNSIGNED_BYTE, levelSize.width, levelSize.height) * levelSize.depth;
Geoff Langfd216c42015-05-27 16:12:30 -0400369 mFunctions->compressedTexImage3D(target, i, nativeInternalFormatInfo.internalFormat, levelSize.width, levelSize.height, levelSize.depth,
Geoff Lang42c98f62015-05-20 14:09:25 -0400370 0, dataSize, nullptr);
371 }
372 else
373 {
Geoff Langfd216c42015-05-27 16:12:30 -0400374 mFunctions->texImage3D(target, i, nativeInternalFormatInfo.internalFormat, levelSize.width, levelSize.height, levelSize.depth,
Geoff Lang42c98f62015-05-20 14:09:25 -0400375 0, internalFormatInfo.format, internalFormatInfo.type, nullptr);
376 }
Geoff Lang1c0ad622015-03-24 10:27:45 -0400377 }
378 }
Geoff Langc05f7062015-03-10 09:50:57 -0700379 }
380 else
381 {
382 UNREACHABLE();
383 }
384
385 return gl::Error(GL_NO_ERROR);
Geoff Langf9a6f082015-01-22 13:32:49 -0500386}
387
Gregoire Payen de La Garanderie752ce192015-04-14 11:11:12 +0100388gl::Error TextureGL::generateMipmaps(const gl::SamplerState &samplerState)
Geoff Langf9a6f082015-01-22 13:32:49 -0500389{
Geoff Langc05f7062015-03-10 09:50:57 -0700390 mStateManager->bindTexture(mTextureType, mTextureID);
391 mFunctions->generateMipmap(mTextureType);
392 return gl::Error(GL_NO_ERROR);
Geoff Langf9a6f082015-01-22 13:32:49 -0500393}
394
395void TextureGL::bindTexImage(egl::Surface *surface)
396{
Geoff Lang03053202015-04-09 11:21:13 -0400397 ASSERT(mTextureType == GL_TEXTURE_2D);
398
399 // Make sure this texture is bound
400 mStateManager->bindTexture(mTextureType, mTextureID);
Geoff Langf9a6f082015-01-22 13:32:49 -0500401}
402
403void TextureGL::releaseTexImage()
404{
Geoff Lang03053202015-04-09 11:21:13 -0400405 // Not all Surface implementations reset the size of mip 0 when releasing, do it manually
406 ASSERT(mTextureType == GL_TEXTURE_2D);
407
408 mStateManager->bindTexture(mTextureType, mTextureID);
409 if (UseTexImage2D(mTextureType))
410 {
411 mFunctions->texImage2D(mTextureType, 0, GL_RGBA, 0, 0, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
412 }
413 else
414 {
415 UNREACHABLE();
416 }
Geoff Langf9a6f082015-01-22 13:32:49 -0500417}
418
Geoff Langc05f7062015-03-10 09:50:57 -0700419template <typename T>
420static inline void SyncSamplerStateMember(const FunctionsGL *functions, const gl::SamplerState &newState,
421 gl::SamplerState &curState, GLenum textureType, GLenum name,
422 T(gl::SamplerState::*samplerMember))
423{
424 if (curState.*samplerMember != newState.*samplerMember)
425 {
426 curState.*samplerMember = newState.*samplerMember;
Minmin Gong794e0002015-04-07 18:31:54 -0700427 functions->texParameterf(textureType, name, static_cast<GLfloat>(curState.*samplerMember));
Geoff Langc05f7062015-03-10 09:50:57 -0700428 }
429}
430
431void TextureGL::syncSamplerState(const gl::SamplerState &samplerState) const
432{
433 if (mAppliedSamplerState != samplerState)
434 {
435 mStateManager->bindTexture(mTextureType, mTextureID);
436 SyncSamplerStateMember(mFunctions, samplerState, mAppliedSamplerState, mTextureType, GL_TEXTURE_MIN_FILTER, &gl::SamplerState::minFilter);
437 SyncSamplerStateMember(mFunctions, samplerState, mAppliedSamplerState, mTextureType, GL_TEXTURE_MAG_FILTER, &gl::SamplerState::magFilter);
438 SyncSamplerStateMember(mFunctions, samplerState, mAppliedSamplerState, mTextureType, GL_TEXTURE_WRAP_S, &gl::SamplerState::wrapS);
439 SyncSamplerStateMember(mFunctions, samplerState, mAppliedSamplerState, mTextureType, GL_TEXTURE_WRAP_T, &gl::SamplerState::wrapT);
440 SyncSamplerStateMember(mFunctions, samplerState, mAppliedSamplerState, mTextureType, GL_TEXTURE_WRAP_R, &gl::SamplerState::wrapR);
441 SyncSamplerStateMember(mFunctions, samplerState, mAppliedSamplerState, mTextureType, GL_TEXTURE_MAX_ANISOTROPY_EXT, &gl::SamplerState::maxAnisotropy);
442 SyncSamplerStateMember(mFunctions, samplerState, mAppliedSamplerState, mTextureType, GL_TEXTURE_BASE_LEVEL, &gl::SamplerState::baseLevel);
443 SyncSamplerStateMember(mFunctions, samplerState, mAppliedSamplerState, mTextureType, GL_TEXTURE_MAX_LEVEL, &gl::SamplerState::maxLevel);
444 SyncSamplerStateMember(mFunctions, samplerState, mAppliedSamplerState, mTextureType, GL_TEXTURE_MIN_LOD, &gl::SamplerState::minLod);
445 SyncSamplerStateMember(mFunctions, samplerState, mAppliedSamplerState, mTextureType, GL_TEXTURE_MAX_LOD, &gl::SamplerState::maxLod);
446 SyncSamplerStateMember(mFunctions, samplerState, mAppliedSamplerState, mTextureType, GL_TEXTURE_COMPARE_MODE, &gl::SamplerState::compareMode);
447 SyncSamplerStateMember(mFunctions, samplerState, mAppliedSamplerState, mTextureType, GL_TEXTURE_COMPARE_FUNC, &gl::SamplerState::compareFunc);
448 SyncSamplerStateMember(mFunctions, samplerState, mAppliedSamplerState, mTextureType, GL_TEXTURE_SWIZZLE_R, &gl::SamplerState::swizzleRed);
449 SyncSamplerStateMember(mFunctions, samplerState, mAppliedSamplerState, mTextureType, GL_TEXTURE_SWIZZLE_G, &gl::SamplerState::swizzleGreen);
450 SyncSamplerStateMember(mFunctions, samplerState, mAppliedSamplerState, mTextureType, GL_TEXTURE_SWIZZLE_B, &gl::SamplerState::swizzleBlue);
451 SyncSamplerStateMember(mFunctions, samplerState, mAppliedSamplerState, mTextureType, GL_TEXTURE_SWIZZLE_A, &gl::SamplerState::swizzleAlpha);
452 }
453}
454
455GLuint TextureGL::getTextureID() const
456{
457 return mTextureID;
458}
459
Geoff Langf9a6f082015-01-22 13:32:49 -0500460}