blob: cca6a45c0b965387a50ee5a8d8e873e138841363 [file] [log] [blame]
Nicolas Capens0bac2852016-05-07 06:09:58 -04001// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15// Texture.h: Defines the abstract Texture class and its concrete derived
16// classes Texture2D and TextureCubeMap. Implements GL texture objects and
17// related functionality. [OpenGL ES 2.0.24] section 3.7 page 63.
18
19#ifndef LIBGLES_CM_TEXTURE_H_
20#define LIBGLES_CM_TEXTURE_H_
21
22#include "Renderbuffer.h"
23#include "common/Object.hpp"
24#include "utilities.h"
25#include "libEGL/Texture.hpp"
26#include "common/debug.h"
27
28#include <GLES/gl.h>
29
30#include <vector>
31
Nicolas Capens31c07a32017-06-13 23:44:13 -040032namespace gl { class Surface; }
Nicolas Capens0bac2852016-05-07 06:09:58 -040033
34namespace es1
35{
36class Framebuffer;
37
38enum
39{
40 IMPLEMENTATION_MAX_TEXTURE_LEVELS = sw::MIPMAP_LEVELS,
41 IMPLEMENTATION_MAX_TEXTURE_SIZE = 1 << (IMPLEMENTATION_MAX_TEXTURE_LEVELS - 1),
42 IMPLEMENTATION_MAX_CUBE_MAP_TEXTURE_SIZE = 1 << (IMPLEMENTATION_MAX_TEXTURE_LEVELS - 1),
43 IMPLEMENTATION_MAX_RENDERBUFFER_SIZE = sw::OUTLINE_RESOLUTION,
44};
45
46class Texture : public egl::Texture
47{
48public:
49 explicit Texture(GLuint name);
50
Ben Clayton93317ec2019-02-20 08:53:50 +000051 sw::Resource *getResource() const override;
Nicolas Capens0bac2852016-05-07 06:09:58 -040052
53 virtual void addProxyRef(const Renderbuffer *proxy) = 0;
54 virtual void releaseProxy(const Renderbuffer *proxy) = 0;
55
56 virtual GLenum getTarget() const = 0;
57
58 bool setMinFilter(GLenum filter);
59 bool setMagFilter(GLenum filter);
60 bool setWrapS(GLenum wrap);
61 bool setWrapT(GLenum wrap);
62 bool setMaxAnisotropy(GLfloat textureMaxAnisotropy);
63 void setGenerateMipmap(GLboolean enable);
64 void setCropRect(GLint u, GLint v, GLint w, GLint h);
65
66 GLenum getMinFilter() const;
67 GLenum getMagFilter() const;
68 GLenum getWrapS() const;
69 GLenum getWrapT() const;
70 GLfloat getMaxAnisotropy() const;
71 GLboolean getGenerateMipmap() const;
72 GLint getCropRectU() const;
73 GLint getCropRectV() const;
74 GLint getCropRectW() const;
75 GLint getCropRectH() const;
76
77 virtual GLsizei getWidth(GLenum target, GLint level) const = 0;
78 virtual GLsizei getHeight(GLenum target, GLint level) const = 0;
Nicolas Capens3b4a25c2018-02-22 20:14:07 -050079 virtual GLint getFormat(GLenum target, GLint level) const = 0;
Nicolas Capensb3f54e82017-12-19 13:38:18 -050080 virtual int getTopLevel() const = 0;
Nicolas Capens0bac2852016-05-07 06:09:58 -040081
82 virtual bool isSamplerComplete() const = 0;
83 virtual bool isCompressed(GLenum target, GLint level) const = 0;
84 virtual bool isDepth(GLenum target, GLint level) const = 0;
85
Nicolas Capens0ccc71d2018-03-23 10:13:06 -040086 virtual Renderbuffer *getRenderbuffer(GLenum target, GLint level) = 0;
Nicolas Capens0bac2852016-05-07 06:09:58 -040087 virtual egl::Image *getRenderTarget(GLenum target, unsigned int level) = 0;
Nicolas Capens74a45042017-06-27 17:03:24 -040088 egl::Image *createSharedImage(GLenum target, unsigned int level);
Nicolas Capens0bac2852016-05-07 06:09:58 -040089 virtual bool isShared(GLenum target, unsigned int level) const = 0;
90
91 virtual void generateMipmaps() = 0;
92 virtual void autoGenerateMipmaps() = 0;
93
94 virtual void copySubImage(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height, Framebuffer *source) = 0;
95
96protected:
Nicolas Capens74a45042017-06-27 17:03:24 -040097 ~Texture() override;
Nicolas Capens0bac2852016-05-07 06:09:58 -040098
Nicolas Capens3b4a25c2018-02-22 20:14:07 -050099 void setImage(GLenum format, GLenum type, GLint unpackAlignment, const void *pixels, egl::Image *image);
100 void subImage(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels, egl::Image *image);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400101 void setCompressedImage(GLsizei imageSize, const void *pixels, egl::Image *image);
102 void subImageCompressed(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLsizei imageSize, const void *pixels, egl::Image *image);
103
104 bool copy(egl::Image *source, const sw::Rect &sourceRect, GLenum destFormat, GLint xoffset, GLint yoffset, egl::Image *dest);
105
106 bool isMipmapFiltered() const;
107
108 GLenum mMinFilter;
109 GLenum mMagFilter;
110 GLenum mWrapS;
111 GLenum mWrapT;
112 GLfloat mMaxAnisotropy;
113 GLboolean generateMipmap;
114 GLint cropRectU;
115 GLint cropRectV;
116 GLint cropRectW;
117 GLint cropRectH;
118
119 sw::Resource *resource;
120};
121
122class Texture2D : public Texture
123{
124public:
125 explicit Texture2D(GLuint name);
126
127 void addProxyRef(const Renderbuffer *proxy) override;
128 void releaseProxy(const Renderbuffer *proxy) override;
129 void sweep() override;
130
Nicolas Capens74a45042017-06-27 17:03:24 -0400131 GLenum getTarget() const override;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400132
Nicolas Capens74a45042017-06-27 17:03:24 -0400133 GLsizei getWidth(GLenum target, GLint level) const override;
134 GLsizei getHeight(GLenum target, GLint level) const override;
Nicolas Capens3b4a25c2018-02-22 20:14:07 -0500135 GLint getFormat(GLenum target, GLint level) const override;
Nicolas Capensb3f54e82017-12-19 13:38:18 -0500136 int getTopLevel() const override;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400137
Nicolas Capens3b4a25c2018-02-22 20:14:07 -0500138 void setImage(GLint level, GLsizei width, GLsizei height, GLint internalformat, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400139 void setCompressedImage(GLint level, GLenum format, GLsizei width, GLsizei height, GLsizei imageSize, const void *pixels);
Nicolas Capens3b4a25c2018-02-22 20:14:07 -0500140 void subImage(GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400141 void subImageCompressed(GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLsizei imageSize, const void *pixels);
142 void copyImage(GLint level, GLenum format, GLint x, GLint y, GLsizei width, GLsizei height, Framebuffer *source);
Ben Clayton93317ec2019-02-20 08:53:50 +0000143 void copySubImage(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height, Framebuffer *source) override;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400144
Nicolas Capens58df2f62016-06-07 14:48:56 -0400145 void setSharedImage(egl::Image *image);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400146
Nicolas Capens74a45042017-06-27 17:03:24 -0400147 bool isSamplerComplete() const override;
148 bool isCompressed(GLenum target, GLint level) const override;
149 bool isDepth(GLenum target, GLint level) const override;
150 void bindTexImage(gl::Surface *surface);
151 void releaseTexImage() override;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400152
Nicolas Capens74a45042017-06-27 17:03:24 -0400153 void generateMipmaps() override;
154 void autoGenerateMipmaps() override;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400155
Nicolas Capens0ccc71d2018-03-23 10:13:06 -0400156 Renderbuffer *getRenderbuffer(GLenum target, GLint level) override;
Nicolas Capens74a45042017-06-27 17:03:24 -0400157 egl::Image *getRenderTarget(GLenum target, unsigned int level) override;
158 bool isShared(GLenum target, unsigned int level) const override;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400159
160 egl::Image *getImage(unsigned int level);
161
162protected:
Nicolas Capens74a45042017-06-27 17:03:24 -0400163 ~Texture2D() override;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400164
165 bool isMipmapComplete() const;
166
167 egl::Image *image[IMPLEMENTATION_MAX_TEXTURE_LEVELS];
168
Nicolas Capens31c07a32017-06-13 23:44:13 -0400169 gl::Surface *mSurface;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400170
171 // A specific internal reference count is kept for colorbuffer proxy references,
172 // because, as the renderbuffer acting as proxy will maintain a binding pointer
173 // back to this texture, there would be a circular reference if we used a binding
174 // pointer here. This reference count will cause the pointer to be set to null if
175 // the count drops to zero, but will not cause deletion of the Renderbuffer.
176 Renderbuffer *mColorbufferProxy;
177 unsigned int mProxyRefs;
178};
179
180class TextureExternal : public Texture2D
181{
182public:
183 explicit TextureExternal(GLuint name);
184
Nicolas Capens74a45042017-06-27 17:03:24 -0400185 GLenum getTarget() const override;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400186
187protected:
Nicolas Capens74a45042017-06-27 17:03:24 -0400188 ~TextureExternal() override;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400189};
190}
191
192#endif // LIBGLES_CM_TEXTURE_H_