1// 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.
18
19#ifndef LIBGL_TEXTURE_H_
20#define LIBGL_TEXTURE_H_
21
22#include "Renderbuffer.h"
23#include "common/Object.hpp"
24#include "utilities.h"
25#include "common/debug.h"
26
27#define _GDI32_
28#include <windows.h>
29#include <GL/GL.h>
30#include <GL/glext.h>
31
32#include <vector>
33
34namespace gl
35{
36class Surface;
37class Config;
38class Framebuffer;
39
40enum
41{
42	IMPLEMENTATION_MAX_TEXTURE_LEVELS = sw::MIPMAP_LEVELS,
43	IMPLEMENTATION_MAX_TEXTURE_SIZE = 1 << (IMPLEMENTATION_MAX_TEXTURE_LEVELS - 1),
44	IMPLEMENTATION_MAX_CUBE_MAP_TEXTURE_SIZE = 1 << (IMPLEMENTATION_MAX_TEXTURE_LEVELS - 1),
45	IMPLEMENTATION_MAX_RENDERBUFFER_SIZE = sw::OUTLINE_RESOLUTION,
46};
47
48class Texture : public NamedObject
49{
50public:
51	explicit Texture(GLuint name);
52
53	virtual ~Texture();
54
55	sw::Resource *getResource() const;
56
57	virtual void addProxyRef(const Renderbuffer *proxy) = 0;
58	virtual void releaseProxy(const Renderbuffer *proxy) = 0;
59
60	virtual GLenum getTarget() const = 0;
61
62	bool setMinFilter(GLenum filter);
63	bool setMagFilter(GLenum filter);
64	bool setWrapS(GLenum wrap);
65	bool setWrapT(GLenum wrap);
66	bool setMaxAnisotropy(GLfloat textureMaxAnisotropy);
67	bool setMaxLevel(int level);
68
69	GLenum getMinFilter() const;
70	GLenum getMagFilter() const;
71	GLenum getWrapS() const;
72	GLenum getWrapT() const;
73	GLfloat getMaxAnisotropy() const;
74
75	virtual GLsizei getWidth(GLenum target, GLint level) const = 0;
76	virtual GLsizei getHeight(GLenum target, GLint level) const = 0;
77	virtual GLenum getFormat(GLenum target, GLint level) const = 0;
78	virtual GLenum getType(GLenum target, GLint level) const = 0;
79	virtual sw::Format getInternalFormat(GLenum target, GLint level) const = 0;
80	virtual int getTopLevel() const = 0;
81
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
86	virtual Renderbuffer *getRenderbuffer(GLenum target) = 0;
87	virtual Image *getRenderTarget(GLenum target, unsigned int level) = 0;
88
89	virtual void generateMipmaps() = 0;
90	virtual void copySubImage(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height, Framebuffer *source) = 0;
91
92protected:
93	void setImage(GLenum format, GLenum type, GLint unpackAlignment, const void *pixels, Image *image);
94	void subImage(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels, Image *image);
95	void setCompressedImage(GLsizei imageSize, const void *pixels, Image *image);
96	void subImageCompressed(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLsizei imageSize, const void *pixels, Image *image);
97
98	bool copy(Image *source, const sw::Rect &sourceRect, GLenum destFormat, GLint xoffset, GLint yoffset, Image *dest);
99
100	bool isMipmapFiltered() const;
101
102	GLenum mMinFilter;
103	GLenum mMagFilter;
104	GLenum mWrapS;
105	GLenum mWrapT;
106	GLfloat mMaxAnisotropy;
107	GLint mMaxLevel;
108
109	sw::Resource *resource;
110};
111
112class Texture2D : public Texture
113{
114public:
115	explicit Texture2D(GLuint name);
116
117	virtual ~Texture2D();
118
119	void addProxyRef(const Renderbuffer *proxy);
120	void releaseProxy(const Renderbuffer *proxy);
121
122	virtual GLenum getTarget() const;
123
124	virtual GLsizei getWidth(GLenum target, GLint level) const;
125	virtual GLsizei getHeight(GLenum target, GLint level) const;
126	virtual GLenum getFormat(GLenum target, GLint level) const;
127	virtual GLenum getType(GLenum target, GLint level) const;
128	virtual sw::Format getInternalFormat(GLenum target, GLint level) const;
129	virtual int getTopLevel() const;
130
131	void setImage(GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
132	void setCompressedImage(GLint level, GLenum format, GLsizei width, GLsizei height, GLsizei imageSize, const void *pixels);
133	void subImage(GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
134	void subImageCompressed(GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLsizei imageSize, const void *pixels);
135	void copyImage(GLint level, GLenum format, GLint x, GLint y, GLsizei width, GLsizei height, Framebuffer *source);
136	void copySubImage(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height, Framebuffer *source);
137
138	void setImage(Image *image);
139
140	virtual bool isSamplerComplete() const;
141	virtual bool isCompressed(GLenum target, GLint level) const;
142	virtual bool isDepth(GLenum target, GLint level) const;
143
144	virtual void generateMipmaps();
145
146	virtual Renderbuffer *getRenderbuffer(GLenum target);
147	virtual Image *getRenderTarget(GLenum target, unsigned int level);
148
149	Image *getImage(unsigned int level);
150
151protected:
152	bool isMipmapComplete() const;
153
154	Image *image[IMPLEMENTATION_MAX_TEXTURE_LEVELS];
155
156	// A specific internal reference count is kept for colorbuffer proxy references,
157	// because, as the renderbuffer acting as proxy will maintain a binding pointer
158	// back to this texture, there would be a circular reference if we used a binding
159	// pointer here. This reference count will cause the pointer to be set to null if
160	// the count drops to zero, but will not cause deletion of the Renderbuffer.
161	Renderbuffer *mColorbufferProxy;
162	unsigned int mProxyRefs;
163};
164
165class TextureCubeMap : public Texture
166{
167public:
168	explicit TextureCubeMap(GLuint name);
169
170	virtual ~TextureCubeMap();
171
172	void addProxyRef(const Renderbuffer *proxy);
173	void releaseProxy(const Renderbuffer *proxy);
174
175	virtual GLenum getTarget() const;
176
177	virtual GLsizei getWidth(GLenum target, GLint level) const;
178	virtual GLsizei getHeight(GLenum target, GLint level) const;
179	virtual GLenum getFormat(GLenum target, GLint level) const;
180	virtual GLenum getType(GLenum target, GLint level) const;
181	virtual sw::Format getInternalFormat(GLenum target, GLint level) const;
182	virtual int getTopLevel() const;
183
184	void setImage(GLenum target, GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
185	void setCompressedImage(GLenum target, GLint level, GLenum format, GLsizei width, GLsizei height, GLsizei imageSize, const void *pixels);
186
187	void subImage(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels);
188	void subImageCompressed(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLsizei imageSize, const void *pixels);
189	void copyImage(GLenum target, GLint level, GLenum format, GLint x, GLint y, GLsizei width, GLsizei height, Framebuffer *source);
190	virtual void copySubImage(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height, Framebuffer *source);
191
192	virtual bool isSamplerComplete() const;
193	virtual bool isCompressed(GLenum target, GLint level) const;
194	virtual bool isDepth(GLenum target, GLint level) const;
195
196	virtual void generateMipmaps();
197
198	virtual Renderbuffer *getRenderbuffer(GLenum target);
199	virtual Image *getRenderTarget(GLenum target, unsigned int level);
200
201	Image *getImage(int face, unsigned int level);
202
203private:
204	bool isCubeComplete() const;
205	bool isMipmapCubeComplete() const;
206
207	// face is one of the GL_TEXTURE_CUBE_MAP_* enumerants. Returns nullptr on failure.
208	Image *getImage(GLenum face, unsigned int level);
209
210	Image *image[6][IMPLEMENTATION_MAX_TEXTURE_LEVELS];
211
212	// A specific internal reference count is kept for colorbuffer proxy references,
213	// because, as the renderbuffer acting as proxy will maintain a binding pointer
214	// back to this texture, there would be a circular reference if we used a binding
215	// pointer here. This reference count will cause the pointer to be set to null if
216	// the count drops to zero, but will not cause deletion of the Renderbuffer.
217	Renderbuffer *mFaceProxies[6];
218	unsigned int mFaceProxyRefs[6];
219};
220
221}
222
223#endif   // LIBGL_TEXTURE_H_
224