ubuntu編譯執行shader
要領:
sudo apt-get install libglfw3-dev libgles2-mesa-dev
gcc glfw_triangle.c -lGLESv2 -lglfw
主要程式碼:
#include <stdio.h> #include <stdlib.h> #define GLFW_INCLUDE_ES2 #include <GLFW/glfw3.h> static const GLuint WIDTH = 800; static const GLuint HEIGHT = 600; static const GLchar* vertex_shader_source = "#version 100\n" "attribute vec3 position;\n" "void main() {\n" " gl_Position = vec4(position, 1.0);\n" "}\n"; static const GLchar* fragment_shader_source = "#version 100\n" "void main() {\n" " gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);\n" "}\n"; static const GLfloat vertices[] = { 0.0f, 0.5f, 0.0f, 0.5f, -0.5f, 0.0f, -0.5f, -0.5f, 0.0f, }; GLint common_get_shader_program(const char *vertex_shader_source, const char *fragment_shader_source) { enum Consts {INFOLOG_LEN = 512}; GLchar infoLog[INFOLOG_LEN]; GLint fragment_shader; GLint shader_program; GLint success; GLint vertex_shader; /* Vertex shader */ vertex_shader = glCreateShader(GL_VERTEX_SHADER); glShaderSource(vertex_shader, 1, &vertex_shader_source, NULL); glCompileShader(vertex_shader); glGetShaderiv(vertex_shader, GL_COMPILE_STATUS, &success); if (!success) { glGetShaderInfoLog(vertex_shader, INFOLOG_LEN, NULL, infoLog); printf("ERROR::SHADER::VERTEX::COMPILATION_FAILED\n%s\n", infoLog); } /* Fragment shader */ fragment_shader = glCreateShader(GL_FRAGMENT_SHADER); glShaderSource(fragment_shader, 1, &fragment_shader_source, NULL); glCompileShader(fragment_shader); glGetShaderiv(fragment_shader, GL_COMPILE_STATUS, &success); if (!success) { glGetShaderInfoLog(fragment_shader, INFOLOG_LEN, NULL, infoLog); printf("ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n%s\n", infoLog); } /* Link shaders */ shader_program = glCreateProgram(); glAttachShader(shader_program, vertex_shader); glAttachShader(shader_program, fragment_shader); glLinkProgram(shader_program); glGetProgramiv(shader_program, GL_LINK_STATUS, &success); if (!success) { glGetProgramInfoLog(shader_program, INFOLOG_LEN, NULL, infoLog); printf("ERROR::SHADER::PROGRAM::LINKING_FAILED\n%s\n", infoLog); } glDeleteShader(vertex_shader); glDeleteShader(fragment_shader); return shader_program; } int main(void) { GLuint shader_program, vbo; GLint pos; GLFWwindow* window; glfwInit(); glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_ES_API); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0); window = glfwCreateWindow(WIDTH, HEIGHT, __FILE__, NULL, NULL); glfwMakeContextCurrent(window); printf("GL_VERSION : %s\n", glGetString(GL_VERSION) ); printf("GL_RENDERER : %s\n", glGetString(GL_RENDERER) ); shader_program = common_get_shader_program(vertex_shader_source, fragment_shader_source); pos = glGetAttribLocation(shader_program, "position"); glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glViewport(0, 0, WIDTH, HEIGHT); glGenBuffers(1, &vbo); glBindBuffer(GL_ARRAY_BUFFER, vbo); glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); glVertexAttribPointer(pos, 3, GL_FLOAT, GL_FALSE, 0, (GLvoid*)0); glEnableVertexAttribArray(pos); glBindBuffer(GL_ARRAY_BUFFER, 0); while (!glfwWindowShouldClose(window)) { glfwPollEvents(); glClear(GL_COLOR_BUFFER_BIT); glUseProgram(shader_program); glDrawArrays(GL_TRIANGLES, 0, 3); glfwSwapBuffers(window); } glDeleteBuffers(1, &vbo); glfwTerminate(); return EXIT_SUCCESS; }
How do I get EGL and OpenGLES libraries for Ubuntu running on VirtualBox?
4
I have Ubuntu running on VirtualBox. Guest additions are installed and now the OS is hardware acceleration capable. I have OpenGL library in place.
Now I want to run apps using egl + opengles1.1 & 2.0. How do i get them on Ubuntu?
Are there any open-source libraries available?
The libraries should use the VirtualBox-provided hardware acceleration capabilities.
graphics virtualbox libraries opengl
81k20226364
asked Jan 17 '13 at 5:01
51113
-
It is very difficult to tell exactly what you are asking, can you be a bit more detailed? – RolandiXor♦ Jan 17 '13 at 5:27
4 Answers
GLFW, Mesa, Ubuntu 16.04 AMD64
I haven't tried it inside of Virtual Box, but this should work regardless since Mesa has a software implementation.
sudo apt-get install libglfw3-dev libgles2-mesa-dev
gcc glfw_triangle.c -lGLESv2 -lglfw
Output:
Source:
#include <stdio.h>
#include <stdlib.h>
#define GLFW_INCLUDE_ES2
#include <GLFW/glfw3.h>
static const GLuint WIDTH = 800;
static const GLuint HEIGHT = 600;
static const GLchar* vertex_shader_source =
"#version 100\n"
"attribute vec3 position;\n"
"void main() {\n"
" gl_Position = vec4(position, 1.0);\n"
"}\n";
static const GLchar* fragment_shader_source =
"#version 100\n"
"void main() {\n"
" gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);\n"
"}\n";
static const GLfloat vertices[] = {
0.0f, 0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
-0.5f, -0.5f, 0.0f,
};
GLint common_get_shader_program(const char *vertex_shader_source, const char *fragment_shader_source) {
enum Consts {INFOLOG_LEN = 512};
GLchar infoLog[INFOLOG_LEN];
GLint fragment_shader;
GLint shader_program;
GLint success;
GLint vertex_shader;
/* Vertex shader */
vertex_shader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertex_shader, 1, &vertex_shader_source, NULL);
glCompileShader(vertex_shader);
glGetShaderiv(vertex_shader, GL_COMPILE_STATUS, &success);
if (!success) {
glGetShaderInfoLog(vertex_shader, INFOLOG_LEN, NULL, infoLog);
printf("ERROR::SHADER::VERTEX::COMPILATION_FAILED\n%s\n", infoLog);
}
/* Fragment shader */
fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragment_shader, 1, &fragment_shader_source, NULL);
glCompileShader(fragment_shader);
glGetShaderiv(fragment_shader, GL_COMPILE_STATUS, &success);
if (!success) {
glGetShaderInfoLog(fragment_shader, INFOLOG_LEN, NULL, infoLog);
printf("ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n%s\n", infoLog);
}
/* Link shaders */
shader_program = glCreateProgram();
glAttachShader(shader_program, vertex_shader);
glAttachShader(shader_program, fragment_shader);
glLinkProgram(shader_program);
glGetProgramiv(shader_program, GL_LINK_STATUS, &success);
if (!success) {
glGetProgramInfoLog(shader_program, INFOLOG_LEN, NULL, infoLog);
printf("ERROR::SHADER::PROGRAM::LINKING_FAILED\n%s\n", infoLog);
}
glDeleteShader(vertex_shader);
glDeleteShader(fragment_shader);
return shader_program;
}
int main(void) {
GLuint shader_program, vbo;
GLint pos;
GLFWwindow* window;
glfwInit();
glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_ES_API);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
window = glfwCreateWindow(WIDTH, HEIGHT, __FILE__, NULL, NULL);
glfwMakeContextCurrent(window);
printf("GL_VERSION : %s\n", glGetString(GL_VERSION) );
printf("GL_RENDERER : %s\n", glGetString(GL_RENDERER) );
shader_program = common_get_shader_program(vertex_shader_source, fragment_shader_source);
pos = glGetAttribLocation(shader_program, "position");
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glViewport(0, 0, WIDTH, HEIGHT);
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glVertexAttribPointer(pos, 3, GL_FLOAT, GL_FALSE, 0, (GLvoid*)0);
glEnableVertexAttribArray(pos);
glBindBuffer(GL_ARRAY_BUFFER, 0);
while (!glfwWindowShouldClose(window)) {
glfwPollEvents();
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(shader_program);
glDrawArrays(GL_TRIANGLES, 0, 3);
glfwSwapBuffers(window);
}
glDeleteBuffers(1, &vbo);
glfwTerminate();
return EXIT_SUCCESS;
}
The key line lines of code are:
#define GLFW_INCLUDE_ES2
#include <GLFW/glfw3.h>
GLFW_INCLUDE_ES2
is documented at: http://www.glfw.org/docs/latest/build_guide.html#build_macros and a quick look at the source shows that it forwards to GLES:
#elif defined(GLFW_INCLUDE_ES2)
#include <GLES2/gl2.h>
#if defined(GLFW_INCLUDE_GLEXT)
#include <GLES2/gl2ext.h>
#endif
This source seems to be is in the common subset of GLES and OpenGL (like much of GLES), and also compiles with -lGL
if we remove the #define GLFW_INCLUDE_ES2
.
If we add things which are not in GLES like immediate rendering glBegin
, link fails as expected.
Credits: genpfult made the code much more correct.
ARM Mali OpenGL ES SDK
- download from: http://malideveloper.arm.com/resources/sdks/opengl-es-sdk-for-linux/
- open the documentation HTML on a browser
- follow the "Quick Start Guide", it's simple
Contains several interesting open source examples + windowing system boilerplate (X11 + EGL).
The build system supports easy cross compilation for ARM / Mali SoCs, but I haven't tested that yet.
The key component included seems to be the "OpenGL ES Emulator" http://malideveloper.arm.com/resources/tools/opengl-es-emulator/ which "maps OpenGL ES 3.2 API calls to the OpenGL API". But that does not ship with source, only precompiled.
Uses a custom enterprisey EULA that appears to be permissive, but yeah, ask your lawyer.
Tested on SDK v2.4.4.
1
answered Sep 6 '16 at 19:16
9,04444246
You can search for packages and package contents with apt-cache
:
> apt-cache search opengles
mesa-utils-extra - Miscellaneous Mesa utilies (opengles, egl)
The output says that OpenGLES probably is in the package mesa-utils-extra. Mesa 3D has a project page for OpenGLES and writes there:
Mesa implements OpenGL ES 1.1 and OpenGL ES 2.0. More informations about OpenGL ES can be found at http://www.khronos.org/opengles/.
EGL is also built into Mesa:
> apt-cache search mesa | grep -i egl
mesa-utils-extra - Miscellaneous Mesa utilies (opengles, egl)
libegl1-mesa - free implementation of the EGL API -- runtime
libegl1-mesa-dbg - free implementation of the EGL API -- debugging symbols
libegl1-mesa-dev - free implementation of the EGL API -- development files
libegl1-mesa-drivers - free implementation of the EGL API -- hardware drivers
libegl1-mesa-drivers-dbg - free implementation of the EGL API -- driver debugging symbols
So you need to install mesa-utils-extra
and probably also libegl1-mesa
.
27.5k1363108
answered Jan 17 '13 at 9:38
14.9k863118
-
Thank you very much for the reply. But Mesa do not use VirtualBox virtual GPU for hardware acceleration. While running Mesa on VBox it uses software rasterizer. My requirement is to strictly use the virtual box 3D acceleration for Opengles demos. – vboxuser Jan 17 '13 at 9:53
-
So maybe we should watch this/your question here: forums.virtualbox.org/… – qbi Jan 17 '13 at 10:08
Since the question has been asked, a package appeared and could help :
sudo apt-get install libgles2-mesa-dev
answered Apr 25 '14 at 12:43
1213
Try ARM OpenGL ES 2.0 Emulator, I myself haven't managed to make OpenGL ES 2.0 work, but 1.1 seems to run fine(simpleApp demo). As I understand, it's supposed to be hardware accelerated as the emulator uses platform GL
libraries and mesa3d is(not sure though) accelerated.
There's also libgles2-mesa
- but unfortunately I couldn't make it work. es2gears/es2tri samples crash as well as simpleApp linked against mesa libs.