Hash :
5ec53c82
Author :
Date :
2015-06-05T16:32:14
Handle the remaining pixel unpack parameters in TextureGL. BUG=angleproject:884 Change-Id: I9e248e623f3b342f878bf65c9686768b90cf5ed8 Reviewed-on: https://chromium-review.googlesource.com/275692 Tested-by: Geoff Lang <geofflang@chromium.org> Reviewed-by: Corentin Wallez <cwallez@chromium.org> Reviewed-by: Brandon Jones <bajones@chromium.org>
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 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
//
// Copyright 2015 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// TextureGL.cpp: Implements the class methods for TextureGL.
#include "libANGLE/renderer/gl/TextureGL.h"
#include "common/debug.h"
#include "common/utilities.h"
#include "libANGLE/State.h"
#include "libANGLE/angletypes.h"
#include "libANGLE/formatutils.h"
#include "libANGLE/renderer/gl/BufferGL.h"
#include "libANGLE/renderer/gl/FramebufferGL.h"
#include "libANGLE/renderer/gl/FunctionsGL.h"
#include "libANGLE/renderer/gl/StateManagerGL.h"
#include "libANGLE/renderer/gl/formatutilsgl.h"
namespace rx
{
static void SetUnpackStateForTexImage(StateManagerGL *stateManager, const gl::PixelUnpackState &unpack)
{
const gl::Buffer *unpackBuffer = unpack.pixelBuffer.get();
if (unpackBuffer != nullptr)
{
UNIMPLEMENTED();
}
stateManager->setPixelUnpackState(unpack.alignment, unpack.rowLength, unpack.skipRows,
unpack.skipPixels, unpack.imageHeight, unpack.skipImages);
}
static bool UseTexImage2D(GLenum textureType)
{
return textureType == GL_TEXTURE_2D || textureType == GL_TEXTURE_CUBE_MAP;
}
static bool UseTexImage3D(GLenum textureType)
{
return textureType == GL_TEXTURE_2D_ARRAY || textureType == GL_TEXTURE_3D;
}
static bool CompatibleTextureTarget(GLenum textureType, GLenum textureTarget)
{
if (textureType != GL_TEXTURE_CUBE_MAP)
{
return textureType == textureTarget;
}
else
{
return gl::IsCubeMapTextureTarget(textureTarget);
}
}
TextureGL::TextureGL(GLenum type, const FunctionsGL *functions, StateManagerGL *stateManager)
: TextureImpl(),
mTextureType(type),
mFunctions(functions),
mStateManager(stateManager),
mAppliedSamplerState(),
mTextureID(0)
{
ASSERT(mFunctions);
ASSERT(mStateManager);
mFunctions->genTextures(1, &mTextureID);
mStateManager->bindTexture(mTextureType, mTextureID);
}
TextureGL::~TextureGL()
{
mStateManager->deleteTexture(mTextureID);
mTextureID = 0;
}
void TextureGL::setUsage(GLenum usage)
{
// GL_ANGLE_texture_usage not implemented for desktop GL
UNREACHABLE();
}
gl::Error TextureGL::setImage(GLenum target, size_t level, GLenum internalFormat, const gl::Extents &size, GLenum format, GLenum type,
const gl::PixelUnpackState &unpack, const uint8_t *pixels)
{
UNUSED_ASSERTION_VARIABLE(&CompatibleTextureTarget); // Reference this function to avoid warnings.
ASSERT(CompatibleTextureTarget(mTextureType, target));
SetUnpackStateForTexImage(mStateManager, unpack);
const nativegl::InternalFormat &nativeInternalFormatInfo = nativegl::GetInternalFormatInfo(internalFormat, mFunctions->standard);
mStateManager->bindTexture(mTextureType, mTextureID);
if (UseTexImage2D(mTextureType))
{
ASSERT(size.depth == 1);
mFunctions->texImage2D(target, level, nativeInternalFormatInfo.internalFormat, size.width, size.height, 0, format, type, pixels);
}
else if (UseTexImage3D(mTextureType))
{
mFunctions->texImage3D(target, level, nativeInternalFormatInfo.internalFormat, size.width, size.height, size.depth, 0, format, type, pixels);
}
else
{
UNREACHABLE();
}
return gl::Error(GL_NO_ERROR);
}
gl::Error TextureGL::setSubImage(GLenum target, size_t level, const gl::Box &area, GLenum format, GLenum type,
const gl::PixelUnpackState &unpack, const uint8_t *pixels)
{
ASSERT(CompatibleTextureTarget(mTextureType, target));
SetUnpackStateForTexImage(mStateManager, unpack);
mStateManager->bindTexture(mTextureType, mTextureID);
if (UseTexImage2D(mTextureType))
{
ASSERT(area.z == 0 && area.depth == 1);
mFunctions->texSubImage2D(target, level, area.x, area.y, area.width, area.height, format, type, pixels);
}
else if (UseTexImage3D(mTextureType))
{
mFunctions->texSubImage3D(target, level, area.x, area.y, area.z, area.width, area.height, area.depth,
format, type, pixels);
}
else
{
UNREACHABLE();
}
return gl::Error(GL_NO_ERROR);
}
gl::Error TextureGL::setCompressedImage(GLenum target, size_t level, GLenum internalFormat, const gl::Extents &size,
const gl::PixelUnpackState &unpack, size_t imageSize, const uint8_t *pixels)
{
ASSERT(CompatibleTextureTarget(mTextureType, target));
SetUnpackStateForTexImage(mStateManager, unpack);
const nativegl::InternalFormat &nativeInternalFormatInfo = nativegl::GetInternalFormatInfo(internalFormat, mFunctions->standard);
mStateManager->bindTexture(mTextureType, mTextureID);
if (UseTexImage2D(mTextureType))
{
ASSERT(size.depth == 1);
mFunctions->compressedTexImage2D(target, level, nativeInternalFormatInfo.internalFormat, size.width, size.height, 0, imageSize, pixels);
}
else if (UseTexImage3D(mTextureType))
{
mFunctions->compressedTexImage3D(target, level, nativeInternalFormatInfo.internalFormat, size.width, size.height, size.depth, 0,
imageSize, pixels);
}
else
{
UNREACHABLE();
}
return gl::Error(GL_NO_ERROR);
}
gl::Error TextureGL::setCompressedSubImage(GLenum target, size_t level, const gl::Box &area, GLenum format,
const gl::PixelUnpackState &unpack, size_t imageSize, const uint8_t *pixels)
{
ASSERT(CompatibleTextureTarget(mTextureType, target));
SetUnpackStateForTexImage(mStateManager, unpack);
const nativegl::InternalFormat &nativeInternalFormatInfo = nativegl::GetInternalFormatInfo(format, mFunctions->standard);
mStateManager->bindTexture(mTextureType, mTextureID);
if (UseTexImage2D(mTextureType))
{
ASSERT(area.z == 0 && area.depth == 1);
mFunctions->compressedTexSubImage2D(target, level, area.x, area.y, area.width, area.height, nativeInternalFormatInfo.internalFormat, imageSize,
pixels);
}
else if (UseTexImage3D(mTextureType))
{
mFunctions->compressedTexSubImage3D(target, level, area.x, area.y, area.z, area.width, area.height, area.depth,
nativeInternalFormatInfo.internalFormat, imageSize, pixels);
}
else
{
UNREACHABLE();
}
return gl::Error(GL_NO_ERROR);
}
gl::Error TextureGL::copyImage(GLenum target, size_t level, const gl::Rectangle &sourceArea, GLenum internalFormat,
const gl::Framebuffer *source)
{
const nativegl::InternalFormat &nativeInternalFormatInfo = nativegl::GetInternalFormatInfo(internalFormat, mFunctions->standard);
const FramebufferGL *sourceFramebufferGL = GetImplAs<FramebufferGL>(source);
mStateManager->bindTexture(mTextureType, mTextureID);
mStateManager->bindFramebuffer(GL_READ_FRAMEBUFFER, sourceFramebufferGL->getFramebufferID());
if (UseTexImage2D(mTextureType))
{
mFunctions->copyTexImage2D(target, level, nativeInternalFormatInfo.internalFormat, sourceArea.x, sourceArea.y,
sourceArea.width, sourceArea.height, 0);
}
else
{
UNREACHABLE();
}
return gl::Error(GL_NO_ERROR);
}
gl::Error TextureGL::copySubImage(GLenum target, size_t level, const gl::Offset &destOffset, const gl::Rectangle &sourceArea,
const gl::Framebuffer *source)
{
const FramebufferGL *sourceFramebufferGL = GetImplAs<FramebufferGL>(source);
mStateManager->bindTexture(mTextureType, mTextureID);
mStateManager->bindFramebuffer(GL_READ_FRAMEBUFFER, sourceFramebufferGL->getFramebufferID());
if (UseTexImage2D(mTextureType))
{
ASSERT(destOffset.z == 0);
mFunctions->copyTexSubImage2D(target, level, destOffset.x, destOffset.y,
sourceArea.x, sourceArea.y, sourceArea.width, sourceArea.height);
}
else if (UseTexImage3D(mTextureType))
{
mFunctions->copyTexSubImage3D(target, level, destOffset.x, destOffset.y, destOffset.z,
sourceArea.x, sourceArea.y, sourceArea.width, sourceArea.height);
}
else
{
UNREACHABLE();
}
return gl::Error(GL_NO_ERROR);
}
gl::Error TextureGL::setStorage(GLenum target, size_t levels, GLenum internalFormat, const gl::Extents &size)
{
// TODO: emulate texture storage with TexImage calls if on GL version <4.2 or the
// ARB_texture_storage extension is not available.
const nativegl::InternalFormat &nativeInternalFormatInfo = nativegl::GetInternalFormatInfo(internalFormat, mFunctions->standard);
mStateManager->bindTexture(mTextureType, mTextureID);
if (UseTexImage2D(mTextureType))
{
ASSERT(size.depth == 1);
if (mFunctions->texStorage2D)
{
mFunctions->texStorage2D(target, levels, nativeInternalFormatInfo.internalFormat, size.width, size.height);
}
else
{
// Make sure no pixel unpack buffer is bound
mStateManager->bindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
const gl::InternalFormat &internalFormatInfo = gl::GetInternalFormatInfo(internalFormat);
// Internal format must be sized
ASSERT(internalFormatInfo.pixelBytes != 0);
for (size_t level = 0; level < levels; level++)
{
gl::Extents levelSize(std::max(size.width >> level, 1),
std::max(size.height >> level, 1),
1);
if (mTextureType == GL_TEXTURE_2D)
{
if (internalFormatInfo.compressed)
{
size_t dataSize = internalFormatInfo.computeBlockSize(GL_UNSIGNED_BYTE, levelSize.width, levelSize.height);
mFunctions->compressedTexImage2D(target, level, nativeInternalFormatInfo.internalFormat, levelSize.width, levelSize.height,
0, dataSize, nullptr);
}
else
{
mFunctions->texImage2D(target, level, nativeInternalFormatInfo.internalFormat, levelSize.width, levelSize.height,
0, internalFormatInfo.format, internalFormatInfo.type, nullptr);
}
}
else if (mTextureType == GL_TEXTURE_CUBE_MAP)
{
for (GLenum face = gl::FirstCubeMapTextureTarget; face <= gl::LastCubeMapTextureTarget; face++)
{
if (internalFormatInfo.compressed)
{
size_t dataSize = internalFormatInfo.computeBlockSize(GL_UNSIGNED_BYTE, levelSize.width, levelSize.height);
mFunctions->compressedTexImage2D(face, level, nativeInternalFormatInfo.internalFormat, levelSize.width, levelSize.height,
0, dataSize, nullptr);
}
else
{
mFunctions->texImage2D(face, level, nativeInternalFormatInfo.internalFormat, levelSize.width, levelSize.height,
0, internalFormatInfo.format, internalFormatInfo.type, nullptr);
}
}
}
else
{
UNREACHABLE();
}
}
}
}
else if (UseTexImage3D(mTextureType))
{
if (mFunctions->texStorage3D)
{
mFunctions->texStorage3D(target, levels, nativeInternalFormatInfo.internalFormat, size.width, size.height, size.depth);
}
else
{
// Make sure no pixel unpack buffer is bound
mStateManager->bindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
const gl::InternalFormat &internalFormatInfo = gl::GetInternalFormatInfo(internalFormat);
// Internal format must be sized
ASSERT(internalFormatInfo.pixelBytes != 0);
for (size_t i = 0; i < levels; i++)
{
gl::Extents levelSize(std::max(size.width >> i, 1),
std::max(size.height >> i, 1),
mTextureType == GL_TEXTURE_3D ? std::max(size.depth >> i, 1) : size.depth);
if (internalFormatInfo.compressed)
{
size_t dataSize = internalFormatInfo.computeBlockSize(GL_UNSIGNED_BYTE, levelSize.width, levelSize.height) * levelSize.depth;
mFunctions->compressedTexImage3D(target, i, nativeInternalFormatInfo.internalFormat, levelSize.width, levelSize.height, levelSize.depth,
0, dataSize, nullptr);
}
else
{
mFunctions->texImage3D(target, i, nativeInternalFormatInfo.internalFormat, levelSize.width, levelSize.height, levelSize.depth,
0, internalFormatInfo.format, internalFormatInfo.type, nullptr);
}
}
}
}
else
{
UNREACHABLE();
}
return gl::Error(GL_NO_ERROR);
}
gl::Error TextureGL::generateMipmaps(const gl::SamplerState &samplerState)
{
mStateManager->bindTexture(mTextureType, mTextureID);
mFunctions->generateMipmap(mTextureType);
return gl::Error(GL_NO_ERROR);
}
void TextureGL::bindTexImage(egl::Surface *surface)
{
ASSERT(mTextureType == GL_TEXTURE_2D);
// Make sure this texture is bound
mStateManager->bindTexture(mTextureType, mTextureID);
}
void TextureGL::releaseTexImage()
{
// Not all Surface implementations reset the size of mip 0 when releasing, do it manually
ASSERT(mTextureType == GL_TEXTURE_2D);
mStateManager->bindTexture(mTextureType, mTextureID);
if (UseTexImage2D(mTextureType))
{
mFunctions->texImage2D(mTextureType, 0, GL_RGBA, 0, 0, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
}
else
{
UNREACHABLE();
}
}
template <typename T>
static inline void SyncSamplerStateMember(const FunctionsGL *functions, const gl::SamplerState &newState,
gl::SamplerState &curState, GLenum textureType, GLenum name,
T(gl::SamplerState::*samplerMember))
{
if (curState.*samplerMember != newState.*samplerMember)
{
curState.*samplerMember = newState.*samplerMember;
functions->texParameterf(textureType, name, static_cast<GLfloat>(curState.*samplerMember));
}
}
void TextureGL::syncSamplerState(const gl::SamplerState &samplerState) const
{
if (mAppliedSamplerState != samplerState)
{
mStateManager->bindTexture(mTextureType, mTextureID);
SyncSamplerStateMember(mFunctions, samplerState, mAppliedSamplerState, mTextureType, GL_TEXTURE_MIN_FILTER, &gl::SamplerState::minFilter);
SyncSamplerStateMember(mFunctions, samplerState, mAppliedSamplerState, mTextureType, GL_TEXTURE_MAG_FILTER, &gl::SamplerState::magFilter);
SyncSamplerStateMember(mFunctions, samplerState, mAppliedSamplerState, mTextureType, GL_TEXTURE_WRAP_S, &gl::SamplerState::wrapS);
SyncSamplerStateMember(mFunctions, samplerState, mAppliedSamplerState, mTextureType, GL_TEXTURE_WRAP_T, &gl::SamplerState::wrapT);
SyncSamplerStateMember(mFunctions, samplerState, mAppliedSamplerState, mTextureType, GL_TEXTURE_WRAP_R, &gl::SamplerState::wrapR);
SyncSamplerStateMember(mFunctions, samplerState, mAppliedSamplerState, mTextureType, GL_TEXTURE_MAX_ANISOTROPY_EXT, &gl::SamplerState::maxAnisotropy);
SyncSamplerStateMember(mFunctions, samplerState, mAppliedSamplerState, mTextureType, GL_TEXTURE_BASE_LEVEL, &gl::SamplerState::baseLevel);
SyncSamplerStateMember(mFunctions, samplerState, mAppliedSamplerState, mTextureType, GL_TEXTURE_MAX_LEVEL, &gl::SamplerState::maxLevel);
SyncSamplerStateMember(mFunctions, samplerState, mAppliedSamplerState, mTextureType, GL_TEXTURE_MIN_LOD, &gl::SamplerState::minLod);
SyncSamplerStateMember(mFunctions, samplerState, mAppliedSamplerState, mTextureType, GL_TEXTURE_MAX_LOD, &gl::SamplerState::maxLod);
SyncSamplerStateMember(mFunctions, samplerState, mAppliedSamplerState, mTextureType, GL_TEXTURE_COMPARE_MODE, &gl::SamplerState::compareMode);
SyncSamplerStateMember(mFunctions, samplerState, mAppliedSamplerState, mTextureType, GL_TEXTURE_COMPARE_FUNC, &gl::SamplerState::compareFunc);
SyncSamplerStateMember(mFunctions, samplerState, mAppliedSamplerState, mTextureType, GL_TEXTURE_SWIZZLE_R, &gl::SamplerState::swizzleRed);
SyncSamplerStateMember(mFunctions, samplerState, mAppliedSamplerState, mTextureType, GL_TEXTURE_SWIZZLE_G, &gl::SamplerState::swizzleGreen);
SyncSamplerStateMember(mFunctions, samplerState, mAppliedSamplerState, mTextureType, GL_TEXTURE_SWIZZLE_B, &gl::SamplerState::swizzleBlue);
SyncSamplerStateMember(mFunctions, samplerState, mAppliedSamplerState, mTextureType, GL_TEXTURE_SWIZZLE_A, &gl::SamplerState::swizzleAlpha);
}
}
GLuint TextureGL::getTextureID() const
{
return mTextureID;
}
}