blob: 5b4f04eafa3b643b00754e50b55774f5a97a6850 [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"
17#include "libANGLE/renderer/gl/FunctionsGL.h"
18#include "libANGLE/renderer/gl/StateManagerGL.h"
Geoff Langf9a6f082015-01-22 13:32:49 -050019
20namespace rx
21{
22
Geoff Langc05f7062015-03-10 09:50:57 -070023static void SetUnpackStateForTexImage(StateManagerGL *stateManager, const gl::PixelUnpackState &unpack)
24{
25 const gl::Buffer *unpackBuffer = unpack.pixelBuffer.get();
26 if (unpackBuffer != nullptr)
27 {
28 UNIMPLEMENTED();
29 }
30 if (unpack.skipRows != 0 || unpack.skipPixels != 0 || unpack.imageHeight != 0 || unpack.skipImages != 0)
31 {
32 UNIMPLEMENTED();
33 }
34 stateManager->setPixelUnpackState(unpack.alignment, unpack.rowLength);
35}
36
37static bool UseTexImage2D(GLenum textureType)
38{
39 return textureType == GL_TEXTURE_2D || textureType == GL_TEXTURE_CUBE_MAP;
40}
41
42static bool UseTexImage3D(GLenum textureType)
43{
44 return textureType == GL_TEXTURE_2D_ARRAY || textureType == GL_TEXTURE_3D;
45}
46
47static bool CompatableTextureTarget(GLenum textureType, GLenum textureTarget)
48{
49 if (textureType != GL_TEXTURE_CUBE_MAP)
50 {
51 return textureType == textureTarget;
52 }
53 else
54 {
55 return gl::IsCubeMapTextureTarget(textureType);
56 }
57}
58
59TextureGL::TextureGL(GLenum type, const FunctionsGL *functions, StateManagerGL *stateManager)
60 : TextureImpl(),
61 mTextureType(type),
62 mFunctions(functions),
63 mStateManager(stateManager),
64 mAppliedSamplerState(),
65 mTextureID(0)
66{
67 ASSERT(mFunctions);
68 ASSERT(mStateManager);
69
70 mFunctions->genTextures(1, &mTextureID);
71}
Geoff Langf9a6f082015-01-22 13:32:49 -050072
73TextureGL::~TextureGL()
Geoff Langc05f7062015-03-10 09:50:57 -070074{
75 if (mTextureID)
76 {
77 mFunctions->deleteTextures(1, &mTextureID);
78 mTextureID = 0;
79 }
80}
Geoff Langf9a6f082015-01-22 13:32:49 -050081
82void TextureGL::setUsage(GLenum usage)
83{
Geoff Langc05f7062015-03-10 09:50:57 -070084 // GL_ANGLE_texture_usage not implemented for desktop GL
85 UNREACHABLE();
Geoff Langf9a6f082015-01-22 13:32:49 -050086}
87
88gl::Error TextureGL::setImage(GLenum target, size_t level, GLenum internalFormat, const gl::Extents &size, GLenum format, GLenum type,
89 const gl::PixelUnpackState &unpack, const uint8_t *pixels)
90{
Geoff Langc05f7062015-03-10 09:50:57 -070091 ASSERT(CompatableTextureTarget(mTextureType, target));
92
93 SetUnpackStateForTexImage(mStateManager, unpack);
94
95 mStateManager->bindTexture(mTextureType, mTextureID);
96 if (UseTexImage2D(mTextureType))
Jamie Madill67102f02015-03-16 10:41:42 -040097 {
Geoff Langc05f7062015-03-10 09:50:57 -070098 ASSERT(size.depth == 1);
99 mFunctions->texImage2D(target, level, internalFormat, size.width, size.height, 0, format, type, pixels);
100 }
101 else if (UseTexImage3D(mTextureType))
102 {
103 mFunctions->texImage3D(target, level, internalFormat, size.width, size.height, size.depth, 0, format, type, pixels);
104 }
105 else
106 {
107 UNREACHABLE();
Jamie Madill67102f02015-03-16 10:41:42 -0400108 }
109
Geoff Langc05f7062015-03-10 09:50:57 -0700110 return gl::Error(GL_NO_ERROR);
Geoff Langf9a6f082015-01-22 13:32:49 -0500111}
112
113gl::Error TextureGL::setSubImage(GLenum target, size_t level, const gl::Box &area, GLenum format, GLenum type,
114 const gl::PixelUnpackState &unpack, const uint8_t *pixels)
115{
Geoff Langc05f7062015-03-10 09:50:57 -0700116 ASSERT(CompatableTextureTarget(mTextureType, target));
117
118 SetUnpackStateForTexImage(mStateManager, unpack);
119
120 mStateManager->bindTexture(mTextureType, mTextureID);
121 if (UseTexImage2D(mTextureType))
Jamie Madill67102f02015-03-16 10:41:42 -0400122 {
Geoff Langc05f7062015-03-10 09:50:57 -0700123 ASSERT(area.z == 0 && area.depth == 1);
124 mFunctions->texSubImage2D(target, level, area.x, area.y, area.width, area.height, format, type, pixels);
125 }
126 else if (UseTexImage3D(mTextureType))
127 {
128 mFunctions->texSubImage3D(target, level, area.x, area.y, area.z, area.width, area.height, area.depth,
129 format, type, pixels);
130 }
131 else
132 {
133 UNREACHABLE();
Jamie Madill67102f02015-03-16 10:41:42 -0400134 }
135
Geoff Langc05f7062015-03-10 09:50:57 -0700136 return gl::Error(GL_NO_ERROR);
Geoff Langf9a6f082015-01-22 13:32:49 -0500137}
138
139gl::Error TextureGL::setCompressedImage(GLenum target, size_t level, GLenum internalFormat, const gl::Extents &size,
140 const gl::PixelUnpackState &unpack, const uint8_t *pixels)
141{
Geoff Langc05f7062015-03-10 09:50:57 -0700142 ASSERT(CompatableTextureTarget(mTextureType, target));
143
144 SetUnpackStateForTexImage(mStateManager, unpack);
145
146 const gl::InternalFormat &internalFormatInfo = gl::GetInternalFormatInfo(internalFormat);
147 size_t depthPitch = internalFormatInfo.computeDepthPitch(GL_UNSIGNED_BYTE, size.width, size.height,
148 unpack.alignment, unpack.rowLength);
149 size_t dataSize = internalFormatInfo.computeBlockSize(GL_UNSIGNED_BYTE, size.width, size.height) * depthPitch;
150
151 mStateManager->bindTexture(mTextureType, mTextureID);
152 if (UseTexImage2D(mTextureType))
Jamie Madill67102f02015-03-16 10:41:42 -0400153 {
Geoff Langc05f7062015-03-10 09:50:57 -0700154 ASSERT(size.depth == 1);
155 mFunctions->compressedTexImage2D(target, level, internalFormat, size.width, size.height, 0, dataSize, pixels);
156 }
157 else if (UseTexImage3D(mTextureType))
158 {
159 mFunctions->compressedTexImage3D(target, level, internalFormat, size.width, size.height, size.depth, 0,
160 dataSize, pixels);
161 }
162 else
163 {
164 UNREACHABLE();
Jamie Madill67102f02015-03-16 10:41:42 -0400165 }
166
Geoff Langc05f7062015-03-10 09:50:57 -0700167 return gl::Error(GL_NO_ERROR);
Geoff Langf9a6f082015-01-22 13:32:49 -0500168}
169
170gl::Error TextureGL::setCompressedSubImage(GLenum target, size_t level, const gl::Box &area, GLenum format,
171 const gl::PixelUnpackState &unpack, const uint8_t *pixels)
172{
Geoff Langc05f7062015-03-10 09:50:57 -0700173 ASSERT(CompatableTextureTarget(mTextureType, target));
174
175 SetUnpackStateForTexImage(mStateManager, unpack);
176
177 const gl::InternalFormat &internalFormatInfo = gl::GetInternalFormatInfo(format);
178 size_t depthPitch = internalFormatInfo.computeDepthPitch(GL_UNSIGNED_BYTE, area.width, area.height,
179 unpack.alignment, unpack.rowLength);
180 size_t dataSize = internalFormatInfo.computeBlockSize(GL_UNSIGNED_BYTE, area.width, area.height) * depthPitch;
181
182 mStateManager->bindTexture(mTextureType, mTextureID);
183 if (UseTexImage2D(mTextureType))
Jamie Madill67102f02015-03-16 10:41:42 -0400184 {
Geoff Langc05f7062015-03-10 09:50:57 -0700185 ASSERT(area.z == 0 && area.depth == 1);
186 mFunctions->compressedTexSubImage2D(target, level, area.x, area.y, area.width, area.height, format, dataSize,
187 pixels);
188 }
189 else if (UseTexImage3D(mTextureType))
190 {
191 mFunctions->compressedTexSubImage3D(target, level, area.x, area.y, area.z, area.width, area.height, area.depth,
192 format, dataSize, pixels);
193 }
194 else
195 {
196 UNREACHABLE();
Jamie Madill67102f02015-03-16 10:41:42 -0400197 }
198
Geoff Langc05f7062015-03-10 09:50:57 -0700199 return gl::Error(GL_NO_ERROR);
Geoff Langf9a6f082015-01-22 13:32:49 -0500200}
201
202gl::Error TextureGL::copyImage(GLenum target, size_t level, const gl::Rectangle &sourceArea, GLenum internalFormat,
203 const gl::Framebuffer *source)
204{
205 UNIMPLEMENTED();
206 return gl::Error(GL_INVALID_OPERATION);
207}
208
209gl::Error TextureGL::copySubImage(GLenum target, size_t level, const gl::Offset &destOffset, const gl::Rectangle &sourceArea,
210 const gl::Framebuffer *source)
211{
212 UNIMPLEMENTED();
213 return gl::Error(GL_INVALID_OPERATION);
214}
215
216gl::Error TextureGL::setStorage(GLenum target, size_t levels, GLenum internalFormat, const gl::Extents &size)
217{
Geoff Langc05f7062015-03-10 09:50:57 -0700218 // TODO: emulate texture storage with TexImage calls if on GL version <4.2 or the
219 // ARB_texture_storage extension is not available.
220
221 mStateManager->bindTexture(mTextureType, mTextureID);
222 if (UseTexImage2D(mTextureType))
223 {
224 ASSERT(size.depth == 1);
225 mFunctions->texStorage2D(target, levels, internalFormat, size.width, size.height);
226 }
227 else if (UseTexImage3D(mTextureType))
228 {
229 mFunctions->texStorage3D(target, levels, internalFormat, size.width, size.height, size.depth);
230 }
231 else
232 {
233 UNREACHABLE();
234 }
235
236 return gl::Error(GL_NO_ERROR);
Geoff Langf9a6f082015-01-22 13:32:49 -0500237}
238
239gl::Error TextureGL::generateMipmaps()
240{
Geoff Langc05f7062015-03-10 09:50:57 -0700241 mStateManager->bindTexture(mTextureType, mTextureID);
242 mFunctions->generateMipmap(mTextureType);
243 return gl::Error(GL_NO_ERROR);
Geoff Langf9a6f082015-01-22 13:32:49 -0500244}
245
246void TextureGL::bindTexImage(egl::Surface *surface)
247{
248 UNIMPLEMENTED();
249}
250
251void TextureGL::releaseTexImage()
252{
253 UNIMPLEMENTED();
254}
255
Geoff Langc05f7062015-03-10 09:50:57 -0700256template <typename T>
257static inline void SyncSamplerStateMember(const FunctionsGL *functions, const gl::SamplerState &newState,
258 gl::SamplerState &curState, GLenum textureType, GLenum name,
259 T(gl::SamplerState::*samplerMember))
260{
261 if (curState.*samplerMember != newState.*samplerMember)
262 {
263 curState.*samplerMember = newState.*samplerMember;
264 functions->texParameterf(textureType, name, curState.*samplerMember);
265 }
266}
267
268void TextureGL::syncSamplerState(const gl::SamplerState &samplerState) const
269{
270 if (mAppliedSamplerState != samplerState)
271 {
272 mStateManager->bindTexture(mTextureType, mTextureID);
273 SyncSamplerStateMember(mFunctions, samplerState, mAppliedSamplerState, mTextureType, GL_TEXTURE_MIN_FILTER, &gl::SamplerState::minFilter);
274 SyncSamplerStateMember(mFunctions, samplerState, mAppliedSamplerState, mTextureType, GL_TEXTURE_MAG_FILTER, &gl::SamplerState::magFilter);
275 SyncSamplerStateMember(mFunctions, samplerState, mAppliedSamplerState, mTextureType, GL_TEXTURE_WRAP_S, &gl::SamplerState::wrapS);
276 SyncSamplerStateMember(mFunctions, samplerState, mAppliedSamplerState, mTextureType, GL_TEXTURE_WRAP_T, &gl::SamplerState::wrapT);
277 SyncSamplerStateMember(mFunctions, samplerState, mAppliedSamplerState, mTextureType, GL_TEXTURE_WRAP_R, &gl::SamplerState::wrapR);
278 SyncSamplerStateMember(mFunctions, samplerState, mAppliedSamplerState, mTextureType, GL_TEXTURE_MAX_ANISOTROPY_EXT, &gl::SamplerState::maxAnisotropy);
279 SyncSamplerStateMember(mFunctions, samplerState, mAppliedSamplerState, mTextureType, GL_TEXTURE_BASE_LEVEL, &gl::SamplerState::baseLevel);
280 SyncSamplerStateMember(mFunctions, samplerState, mAppliedSamplerState, mTextureType, GL_TEXTURE_MAX_LEVEL, &gl::SamplerState::maxLevel);
281 SyncSamplerStateMember(mFunctions, samplerState, mAppliedSamplerState, mTextureType, GL_TEXTURE_MIN_LOD, &gl::SamplerState::minLod);
282 SyncSamplerStateMember(mFunctions, samplerState, mAppliedSamplerState, mTextureType, GL_TEXTURE_MAX_LOD, &gl::SamplerState::maxLod);
283 SyncSamplerStateMember(mFunctions, samplerState, mAppliedSamplerState, mTextureType, GL_TEXTURE_COMPARE_MODE, &gl::SamplerState::compareMode);
284 SyncSamplerStateMember(mFunctions, samplerState, mAppliedSamplerState, mTextureType, GL_TEXTURE_COMPARE_FUNC, &gl::SamplerState::compareFunc);
285 SyncSamplerStateMember(mFunctions, samplerState, mAppliedSamplerState, mTextureType, GL_TEXTURE_SWIZZLE_R, &gl::SamplerState::swizzleRed);
286 SyncSamplerStateMember(mFunctions, samplerState, mAppliedSamplerState, mTextureType, GL_TEXTURE_SWIZZLE_G, &gl::SamplerState::swizzleGreen);
287 SyncSamplerStateMember(mFunctions, samplerState, mAppliedSamplerState, mTextureType, GL_TEXTURE_SWIZZLE_B, &gl::SamplerState::swizzleBlue);
288 SyncSamplerStateMember(mFunctions, samplerState, mAppliedSamplerState, mTextureType, GL_TEXTURE_SWIZZLE_A, &gl::SamplerState::swizzleAlpha);
289 }
290}
291
292GLuint TextureGL::getTextureID() const
293{
294 return mTextureID;
295}
296
Geoff Langf9a6f082015-01-22 13:32:49 -0500297}