Index: mojo/apps/js/bindings/gl/context.cc |
diff --git a/mojo/apps/js/bindings/gl/context.cc b/mojo/apps/js/bindings/gl/context.cc |
index c0bb74a4dbe2effa906392434fa9878478e2f932..c7dc536e657b346a89f18092c5a2f83ffc6b713c 100644 |
--- a/mojo/apps/js/bindings/gl/context.cc |
+++ b/mojo/apps/js/bindings/gl/context.cc |
@@ -7,9 +7,24 @@ |
#include <GLES2/gl2.h> |
#include "gin/arguments.h" |
+#include "gin/array_buffer.h" |
#include "gin/object_template_builder.h" |
#include "mojo/public/gles2/gles2.h" |
+namespace gin { |
+template<> |
+struct Converter<GLboolean> { |
+ static bool FromV8(v8::Isolate* isolate, v8::Handle<v8::Value> val, |
+ GLboolean* out) { |
+ bool bool_val = false; |
+ if (!Converter<bool>::FromV8(isolate, val, &bool_val)) |
+ return false; |
+ *out = static_cast<GLboolean>(bool_val); |
+ return true; |
+ } |
+}; |
+} |
+ |
namespace mojo { |
namespace js { |
namespace gl { |
@@ -21,42 +36,111 @@ gin::Handle<Context> Context::Create(v8::Isolate* isolate, uint64_t encoded, |
return gin::CreateHandle(isolate, new Context(encoded, width, height)); |
} |
-gin::Handle<Shader> Context::CreateShader(const gin::Arguments& args, |
- GLenum type) { |
- gin::Handle<Shader> result; |
- GLuint glshader = glCreateShader(type); |
- if (glshader != 0u) { |
- result = Opaque::Create(args.isolate(), glshader); |
+void Context::BufferData(GLenum target, const gin::ArrayBufferView& buffer, |
+ GLenum usage) { |
+ glBufferData(target, buffer.num_bytes(), buffer.bytes(), usage); |
+} |
+ |
+void Context::CompileShader(const gin::Arguments& args, GLuint shader) { |
+ glCompileShader(shader); |
+ GLint compiled = 0; |
+ glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled); |
+ if (!compiled) { |
+ args.ThrowTypeError(std::string("Could not compile shader: ") + |
+ GetShaderInfoLog(shader)); |
} |
+} |
+ |
+GLuint Context::CreateBuffer() { |
+ GLuint result = 0; |
+ glGenBuffers(1, &result); |
return result; |
} |
-void Context::ShaderSource(gin::Handle<Shader> shader, |
- const std::string& source) { |
+void Context::DrawElements(GLenum mode, GLsizei count, GLenum type, |
+ uint64_t indices) { |
+ // This looks scary, but it's what WebGL does too: |
+ // http://www.khronos.org/registry/webgl/specs/latest/1.0/#5.1 |
+ glDrawElements(mode, count, type, reinterpret_cast<void*>(indices)); |
+} |
+ |
+GLint Context::GetAttribLocation(GLuint program, const std::string& name) { |
+ return glGetAttribLocation(program, name.c_str()); |
+} |
+ |
+std::string Context::GetProgramInfoLog(GLuint program) { |
+ GLint info_log_length = 0; |
+ glGetProgramiv(program, GL_INFO_LOG_LENGTH, &info_log_length); |
+ std::string info_log(info_log_length, 0); |
+ glGetProgramInfoLog(program, info_log_length, NULL, &info_log.at(0)); |
+ return info_log; |
+} |
+ |
+std::string Context::GetShaderInfoLog(GLuint shader) { |
+ GLint info_log_length = 0; |
+ glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &info_log_length); |
+ std::string info_log(info_log_length, 0); |
+ glGetShaderInfoLog(shader, info_log_length, NULL, &info_log.at(0)); |
+ return info_log; |
+} |
+ |
+GLint Context::GetUniformLocation(GLuint program, const std::string& name) { |
+ return glGetUniformLocation(program, name.c_str()); |
+} |
+ |
+void Context::ShaderSource(GLuint shader, const std::string& source) { |
const char* source_chars = source.c_str(); |
- glShaderSource(shader->value(), 1, &source_chars, NULL); |
+ glShaderSource(shader, 1, &source_chars, NULL); |
} |
-void Context::CompileShader(const gin::Arguments& args, |
- gin::Handle<Shader> shader) { |
- glCompileShader(shader->value()); |
- GLint compiled = 0; |
- glGetShaderiv(shader->value(), GL_COMPILE_STATUS, &compiled); |
- if (!compiled) { |
- // Or should |shader| do it when it is destroyed? |
- glDeleteShader(shader->value()); |
- args.ThrowTypeError("Could not compile shader"); |
- return; |
- } |
+void Context::UniformMatrix4fv(GLint location, GLboolean transpose, |
+ const gin::ArrayBufferView& buffer) { |
+ glUniformMatrix4fv(location, 1, transpose, |
+ static_cast<float*>(buffer.bytes())); |
+} |
+ |
+void Context::VertexAttribPointer(GLuint index, GLint size, GLenum type, |
+ GLboolean normalized, GLsizei stride, |
+ uint64_t offset) { |
+ glVertexAttribPointer(index, size, type, normalized, stride, |
+ reinterpret_cast<void*>(offset)); |
} |
gin::ObjectTemplateBuilder Context::GetObjectTemplateBuilder( |
v8::Isolate* isolate) { |
- return gin::Wrappable<Context>::GetObjectTemplateBuilder(isolate) |
+ return gin::ObjectTemplateBuilder(isolate) |
+ .SetValue("ARRAY_BUFFER", GL_ARRAY_BUFFER) |
+ .SetValue("COLOR_BUFFER_BIT", GL_COLOR_BUFFER_BIT) |
+ .SetValue("ELEMENT_ARRAY_BUFFER", GL_ELEMENT_ARRAY_BUFFER) |
+ .SetValue("FLOAT", GL_FLOAT) |
+ .SetValue("FRAGMENT_SHADER", GL_FRAGMENT_SHADER) |
+ .SetValue("STATIC_DRAW", GL_STATIC_DRAW) |
+ .SetValue("TRIANGLES", GL_TRIANGLES) |
+ .SetValue("UNSIGNED_SHORT", GL_UNSIGNED_SHORT) |
.SetValue("VERTEX_SHADER", GL_VERTEX_SHADER) |
- .SetMethod("createShader", CreateShader) |
+ .SetMethod("attachShader", glAttachShader) |
+ .SetMethod("bindBuffer", glBindBuffer) |
+ .SetMethod("bufferData", BufferData) |
+ .SetMethod("clear", glClear) |
+ .SetMethod("clearColor", glClearColor) |
+ .SetMethod("compileShader", CompileShader) |
+ .SetMethod("createBuffer", CreateBuffer) |
+ .SetMethod("createProgram", glCreateProgram) |
+ .SetMethod("createShader", glCreateShader) |
+ .SetMethod("deleteShader", glDeleteShader) |
+ .SetMethod("drawElements", DrawElements) |
+ .SetMethod("enableVertexAttribArray", glEnableVertexAttribArray) |
+ .SetMethod("getAttribLocation", GetAttribLocation) |
+ .SetMethod("getProgramInfoLog", GetProgramInfoLog) |
+ .SetMethod("getShaderInfoLog", GetShaderInfoLog) |
+ .SetMethod("getUniformLocation", GetUniformLocation) |
+ .SetMethod("linkProgram", glLinkProgram) |
.SetMethod("shaderSource", ShaderSource) |
- .SetMethod("compileShader", CompileShader); |
+ .SetMethod("swapBuffers", MojoGLES2SwapBuffers) |
+ .SetMethod("uniformMatrix4fv", UniformMatrix4fv) |
+ .SetMethod("useProgram", glUseProgram) |
+ .SetMethod("vertexAttribPointer", VertexAttribPointer) |
+ .SetMethod("viewport", glViewport); |
} |
Context::Context(uint64_t encoded, int width, int height) |