OpenGL ES SDK for Android ARM Developer Center
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ETCMipmap.cpp
Go to the documentation of this file.
1 /* Copyright (c) 2013-2017, ARM Limited and Contributors
2  *
3  * SPDX-License-Identifier: MIT
4  *
5  * Permission is hereby granted, free of charge,
6  * to any person obtaining a copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
9  * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
14  * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
16  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
17  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19  */
20 
33 #define LOAD_MIPMAPS
34 #undef DISABLE_MIPMAPS
35 
36 #include <GLES2/gl2.h>
37 #include <GLES2/gl2ext.h>
38 
39 #include <string>
40 #include <sstream>
41 
42 #include <jni.h>
43 #include <android/log.h>
44 
45 #include "ETCMipmap.h"
46 #include "Shader.h"
47 #include "Text.h"
48 #include "Texture.h"
49 #include "ETCHeader.h"
50 #include "AndroidPlatform.h"
51 
52 using std::stringstream;
53 using std::string;
54 using namespace MaliSDK;
55 
56 string resourceDirectory = "/data/data/com.arm.malideveloper.openglessdk.etcmipmap/";
57 string textureFilename = "good_mip_";
58 string imageExtension = ".pkm";
59 
60 string vertexShaderFilename = "ETCMipmap_texture.vert";
61 string fragmentShaderFilename = "ETCMipmap_texture.frag";
62 
63 /* Texture variables. */
65 
66 /* Shader variables. */
70 GLint iLocPosition = -1;
71 GLint iLocTexCoord = -1;
72 GLint iLocSampler = -1;
73 
74 /* A text object to draw text on the screen. */
76 
77 bool setupGraphics(int w, int h)
78 {
79  LOGD("setupGraphics(%d, %d)", w, h);
80 
81  /* Full paths to the shader and texture files */
82  string texturePath = resourceDirectory + textureFilename;
83  string vertexShaderPath = resourceDirectory + vertexShaderFilename;
84  string fragmentShaderPath = resourceDirectory + fragmentShaderFilename;
85 
86  /* Initialize OpenGL ES. */
87  GL_CHECK(glEnable(GL_CULL_FACE));
88  GL_CHECK(glCullFace(GL_BACK));
89  GL_CHECK(glEnable(GL_DEPTH_TEST));
90  GL_CHECK(glEnable(GL_BLEND));
91  /* Should do src * (src alpha) + dest * (1-src alpha). */
92  GL_CHECK(glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA));
93 
94  /* Check which formats are supported. */
95  if (!Texture::isETCSupported(true))
96  {
97  LOGE("ETC1 not supported");
98  return false;
99  }
100 
101  /* Initialize the Text object and add some text. */
102  text = new Text(resourceDirectory.c_str(), w, h);
103  text->addString(0, 0, "Mipmapped ETC1 compressed texture", 255, 255, 0, 255);
104 
105  /*
106  * Initialize textures.
107  * For a texture to be considered 'complete' by OpenGL-ES you must either:
108  * auto-generate the Mipmap levels using glGenerateMipmap(GL_TEXTURE_2D), or
109  * disable Mipmap for minimised textures using
110  * glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
111  * as Mipmap is on by default.
112  */
113 #ifdef LOAD_MIPMAPS
114  /* Load all Mipmap levels from files. */
115  Texture::loadCompressedMipmaps(texturePath.c_str(), imageExtension.c_str(), &textureID);
116 #else /* LOAD_MIPMAPS */
117  /* Load just base level texture data. */
118  GL_CHECK(glGenTextures(1, &textureID));
119  GL_CHECK(glBindTexture(GL_TEXTURE_2D, textureID));
120  string mainTexturePath = texturePath + "0" + imageExtension;
121  unsigned char *textureData;
122  Texture::loadData(mainTexturePath.c_str(), &textureData);
123  ETCHeader loadedETCHeader = ETCHeader(textureData);
124  GL_CHECK(glCompressedTexImage2D(GL_TEXTURE_2D, 0, GL_ETC1_RGB8_OES,
125  loadedETCHeader.getWidth(), loadedETCHeader.getHeight(), 0,
126  loadedETCHeader.getPaddedWidth() * loadedETCHeader.getPaddedHeight() >> 1,
127  textureData + 16));
128  free(textureData);
129 
130 # ifdef DISABLE_MIPMAPS
131  /* Disable Mipmaps. */
132  GL_CHECK(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST));
133 # else /* DISABLE_MIPMAPS */
134  /* Auto generate Mipmaps. */
135  GL_CHECK(glGenerateMipmap(GL_TEXTURE_2D));
136 # endif /* DISABLE_MIPMAPS */
137 #endif /* LOAD_MIPMAPS */
138 
139  /* Process shaders. */
140  Shader::processShader(&vertexShaderID, vertexShaderPath.c_str(), GL_VERTEX_SHADER);
141  LOGD("vertexShaderID = %d", vertexShaderID);
142  Shader::processShader(&fragmentShaderID, fragmentShaderPath.c_str(), GL_FRAGMENT_SHADER);
143  LOGD("fragmentShaderID = %d", fragmentShaderID);
144  programID = GL_CHECK(glCreateProgram());
145  if (!programID)
146  {
147  LOGE("Could not create program.");
148  return false;
149  }
150  GL_CHECK(glAttachShader(programID, vertexShaderID));
151  GL_CHECK(glAttachShader(programID, fragmentShaderID));
152  GL_CHECK(glLinkProgram(programID));
153  GL_CHECK(glUseProgram(programID));
154 
155  /* Vertex positions. */
156  iLocPosition = GL_CHECK(glGetAttribLocation(programID, "a_v4Position"));
157  if(iLocPosition == -1)
158  {
159  LOGE("Attribute not found: \"a_v4Position\"");
160  exit(1);
161  }
162  GL_CHECK(glEnableVertexAttribArray(iLocPosition));
163 
164  /* Texture coordinates. */
165  iLocTexCoord = GL_CHECK(glGetAttribLocation(programID, "a_v2TexCoord"));
166  if(iLocTexCoord == -1)
167  {
168  LOGD("Warning: Attribute not found: \"a_v2TexCoord\"");
169  }
170  else
171  {
172  GL_CHECK(glEnableVertexAttribArray(iLocTexCoord));
173  }
174 
175  /* Set the sampler to point at the 0th texture unit. */
176  iLocSampler = GL_CHECK(glGetUniformLocation(programID, "u_s2dTexture"));
177  if(iLocSampler == -1)
178  {
179  LOGD("Warning: Uniform not found: \"u_s2dTexture\"");
180  }
181  else
182  {
183  GL_CHECK(glUniform1i(iLocSampler, 0));
184  }
185 
186  /* Set clear screen color. */
187  GL_CHECK(glClearColor(0.0f, 0.0f, 1.0f, 1.0));
188 
189  return true;
190 }
191 
192 void renderFrame(void)
193 {
194  GL_CHECK(glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT));
195 
196  GL_CHECK(glUseProgram(programID));
197 
198  /* Pass the plane vertices to the shader. */
199  GL_CHECK(glEnableVertexAttribArray(iLocPosition));
200  GL_CHECK(glVertexAttribPointer(iLocPosition, 3, GL_FLOAT, GL_FALSE, 0, vertices));
201 
202  if(iLocTexCoord != -1)
203  {
204  /* Pass the texture coordinates to the shader. */
205  GL_CHECK(glVertexAttribPointer(iLocTexCoord, 2, GL_FLOAT, GL_FALSE, 0, textureCoordinates));
206  GL_CHECK(glEnableVertexAttribArray(iLocTexCoord));
207  }
208 
209  /* Ensure the correct texture is bound to texture unit 0. */
210  GL_CHECK(glActiveTexture(GL_TEXTURE0));
211  GL_CHECK(glBindTexture(GL_TEXTURE_2D, textureID));
212 
213  GL_CHECK(glDrawElements(GL_TRIANGLE_STRIP, sizeof(indices) / sizeof(GLubyte), GL_UNSIGNED_BYTE, indices));
214 
215  /* Draw fonts. */
216  text->draw();
217 }
218 
219 extern "C"
220 {
222  (JNIEnv *env, jclass jcls, jint width, jint height)
223  {
224  /* Make sure that all resource files are in place. */
227 
228  /* Load all image assets from 0 to 8 */
229  string texturePathFull = textureFilename + "0" + imageExtension;
230  int numberOfImages = 9;
231  for(int allImages = 0; allImages < numberOfImages; allImages++)
232  {
233  stringstream imageNumber;
234  imageNumber << allImages;
235  texturePathFull.replace(textureFilename.length(), 1, imageNumber.str());
236  AndroidPlatform::getAndroidAsset(env, resourceDirectory.c_str(), texturePathFull.c_str());
237  }
238 
239  setupGraphics(width, height);
240  }
241 
243  (JNIEnv *env, jclass jcls)
244  {
245  renderFrame();
246  }
247 
249  (JNIEnv *, jclass)
250  {
251  delete text;
252  }
253 }
string fragmentShaderFilename
Definition: ETCMipmap.cpp:61
string resourceDirectory
Definition: ETCMipmap.cpp:56
JNIEXPORT void JNICALL Java_com_arm_malideveloper_openglessdk_etcmipmap_ETCMipmap_uninit(JNIEnv *, jclass)
Definition: ETCMipmap.cpp:249
Functions for drawing text in OpenGL ES.
Definition: Text.h:44
Class to extract information from the ETC headers of compressed textures.
Definition: ETCHeader.h:38
GLuint vertexShaderID
Definition: ETCMipmap.cpp:67
GLint GLsizei GLsizei height
Definition: gl2ext.h:179
GLuint programID
Definition: ETCMipmap.cpp:69
const float vertices[]
Definition: Cube.h:30
Text * text
Definition: ETCMipmap.cpp:75
GLint iLocTexCoord
Definition: ETCMipmap.cpp:71
GLint iLocPosition
Definition: ETCMipmap.cpp:70
void renderFrame(void)
Definition: ETCMipmap.cpp:192
#define LOGD(...)
Definition: AstcTextures.h:28
GLint iLocSampler
Definition: ETCMipmap.cpp:72
GLfloat GLfloat GLfloat w
Definition: gl2ext.h:2701
GLsizei GLenum const void * indices
Definition: gl2ext.h:322
static void loadCompressedMipmaps(const char *filenameBase, const char *filenameSuffix, GLuint *textureID)
Load compressed mipmaps into memory.
Definition: Texture.cpp:232
string vertexShaderFilename
Definition: ETCMipmap.cpp:60
GLfloat GLfloat f
Definition: gl2ext.h:2707
void addString(int xPosition, int yPosition, const char *string, int red, int green, int blue, int alpha)
Add a std::string to be drawn to the screen.
Definition: Text.cpp:157
string textureFilename
Definition: ETCMipmap.cpp:57
GLfloat GLfloat GLfloat GLfloat h
Definition: gl2ext.h:2701
#define GL_CHECK(x)
Definition: AstcTextures.h:59
GLuint textureID
Definition: ETCMipmap.cpp:64
bool setupGraphics(int w, int h)
Definition: ETCMipmap.cpp:77
unsigned char * textureData
Definition: ThreadSync.cpp:109
static void loadData(const char *filename, unsigned char **textureData)
Load texture data from a file into memory.
Definition: Texture.cpp:167
static bool getAndroidAsset(JNIEnv *JNIEnvironment, const char destinationDirectory[], const char filename[])
Extract an asset file from the APK.
#define GL_ETC1_RGB8_OES
Definition: gl2ext.h:257
#define LOGE(...)
Definition: AstcTextures.h:30
GLuint fragmentShaderID
Definition: ETCMipmap.cpp:68
GLint GLsizei width
Definition: gl2ext.h:179
string imageExtension
Definition: ETCMipmap.cpp:58
static bool isETCSupported(bool verbose=false)
Reports whether or not ETC (Ericsson Texture Compression) is supported.
Definition: Texture.cpp:46
JNIEXPORT void JNICALL Java_com_arm_malideveloper_openglessdk_etcmipmap_ETCMipmap_step(JNIEnv *env, jclass jcls)
Definition: ETCMipmap.cpp:243
static void processShader(GLuint *shader, const char *filename, GLint shaderType)
Create shader, load in source, compile, and dump debug as necessary.
Definition: Shader.cpp:29
void draw(void)
Draw the text to the screen.
Definition: Text.cpp:265
typedef GLuint(GL_APIENTRYP PFNGLGETDEBUGMESSAGELOGKHRPROC)(GLuint count
JNIEXPORT void JNICALL Java_com_arm_malideveloper_openglessdk_etcmipmap_ETCMipmap_init(JNIEnv *env, jclass jcls, jint width, jint height)
Definition: ETCMipmap.cpp:222
static const GLfloat textureCoordinates[]
Definition: ETCAtlasAlpha.h:97