Skip to content
Browse files

all methods are implemented for triangle.js, not rendering anything t…

…hough
  • Loading branch information...
1 parent b890792 commit d573cac41a45a0154e3d3cad140c576ee1914b99 @tmpvar committed Aug 20, 2011
Showing with 291 additions and 101 deletions.
  1. +16 −46 example/js/main.cc
  2. +43 −47 example/triangle.js
  3. +232 −8 src/js/spidermonkey.cc
View
62 example/js/main.cc
@@ -21,14 +21,23 @@ static JSClass global_class = {
/* JSAPI variables. */
-JSRuntime *rt;
-JSContext *cx;
-JSObject *global;
+JSRuntime *rt = NULL;
+JSContext *cx = NULL;
+JSObject *global = NULL;
void c_exit(int code) {
- JS_DestroyContext(cx);
- JS_DestroyRuntime(rt);
- JS_ShutDown();
+ if (cx != NULL) {
+ JS_DestroyContext(cx);
+ }
+
+ if (rt != NULL) {
+ JS_DestroyRuntime(rt);
+ }
+
+ if (global != NULL) {
+ JS_ShutDown();
+ }
+
exit(code);
}
@@ -90,9 +99,7 @@ static JSFunctionSpec module_global_functions[] = {
JS_FS("webgl_rendering_context_blendEquationSeparate", webgl_rendering_context_blendEquationSeparate, 2 /* GLenum modeRGB, GLenum modeAlpha */, 0),
JS_FS("webgl_rendering_context_blendFunc", webgl_rendering_context_blendFunc, 2 /* GLenum sfactor, GLenum dfactor */, 0),
JS_FS("webgl_rendering_context_blendFuncSeparate", webgl_rendering_context_blendFuncSeparate, 4 /* GLenum srcRGB, GLenum dstRGB, GLenum srcAlpha, GLenum dstAlpha */, 0),
- JS_FS("webgl_rendering_context_bufferData", webgl_rendering_context_bufferData, 3 /* GLenum target, GLsizeiptr size, GLenum usage */, 0),
- JS_FS("webgl_rendering_context_bufferData", webgl_rendering_context_bufferData, 3 /* GLenum target, ArrayBufferView data, GLenum usage */, 0),
- JS_FS("webgl_rendering_context_bufferData", webgl_rendering_context_bufferData, 3 /* GLenum target, ArrayBuffer data, GLenum usage */, 0),
+ JS_FS("webgl_rendering_context_bufferData", webgl_rendering_context_bufferData, 3, 0),
JS_FS("webgl_rendering_context_bufferSubData", webgl_rendering_context_bufferSubData, 3 /* GLenum target, GLintptr offset, ArrayBufferView data */, 0),
JS_FS("webgl_rendering_context_bufferSubData", webgl_rendering_context_bufferSubData, 3 /* GLenum target, GLintptr offset, ArrayBuffer data */, 0),
JS_FS("webgl_rendering_context_checkFramebufferStatus", webgl_rendering_context_checkFramebufferStatus, 1 /* GLenum target */, 0),
@@ -256,42 +263,13 @@ void setupGlobals(JSContext *cx, JSObject *global) {
}
}
-
-
-
-
-JSBool printer_construct(JSContext *cx, uintN argc, jsval *argv)
-{
- cout << "constructed!" << endl;
- return JS_TRUE;
-}
-
-void printer_finalize(JSContext *cx, JSObject *obj)
-{
-}
-
-static JSClass printer_class = {
- "Printer",
- JSCLASS_HAS_PRIVATE | JSCLASS_CONSTRUCT_PROTOTYPE,
- JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_StrictPropertyStub,
- JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, printer_finalize,
- JSCLASS_NO_OPTIONAL_MEMBERS
-};
-
-void render() {
- glFlush();
-}
-
-
int main(int argc, char **argv)
{
if (argc < 2) {
cout << "Usage: webgljs path/to/app.js" << endl;
c_exit(EXIT_FAILURE);
}
-
-
const char* filename = argv[1];
char *script = getFileContents(filename);
@@ -305,10 +283,6 @@ int main(int argc, char **argv)
if (cx == NULL)
c_exit(EXIT_FAILURE);
-
-
-
-
JS_SetOptions(cx, JSOPTION_VAROBJFIX | JSOPTION_JIT | JSOPTION_METHODJIT);
JS_SetVersion(cx, JSVERSION_LATEST);
JS_SetErrorReporter(cx, reportError);
@@ -321,10 +295,6 @@ int main(int argc, char **argv)
if (!JS_InitStandardClasses(cx, global))
c_exit(EXIT_FAILURE);
-
- // SET UP CUSTOM CLASS
- JS_InitClass(cx, global, NULL, &printer_class, printer_construct, 0, NULL, NULL, NULL, NULL);
-
jsval rval;
JSString *str;
JSBool ok;
View
90 example/triangle.js
@@ -1,64 +1,60 @@
-var WebGLRenderingContext = require('../lib/webgl.js');
+var webgl = require('../lib/webgl.js');
+var vertices = [
+ 0.75, 0.75, 0.0, 1.0,
+ 0.75, -0.75, 0.0, 1.0,
+ -0.75, -0.75, 0.0, 1.0,
+];
+
+function e(name, result) {
+ var e = ctx.getError()
+ if (e) {
+ console.log(name, 'failed!', e);
+ fail();
+ } else {
+ console.log(name, 'passed!');
+ }
+ return result;
+}
-var ctx = new WebGLRenderingContext();
-var program = ctx.createProgram();
+var ctx = new webgl.WebGLRenderingContext();
+var program = e('create program', ctx.createProgram());
var shaders = {
vertex : "attribute vec3 pos;\nvoid main() {\n gl_Position = vec4(pos, 1.0);\n}",
frag : "void main() {\n gl_FragColor = vec4(1.0, 0, 1, 1.0);\n}"
};
-var vertexShader = ctx.createShader(ctx.VERTEX_SHADER);
-var fragShader = ctx.createShader(ctx.FRAGMENT_SHADER);
+var vertexShader = e('create shader', ctx.createShader(ctx.VERTEX_SHADER));
+var fragShader = e('create shader', ctx.createShader(ctx.FRAGMENT_SHADER));
-ctx.shaderSource(vertexShader, shaders.vertex);
-ctx.shaderSource(fragShader, shaders.frag);
-
-ctx.compileShader(vertexShader);
-ctx.compileShader(fragShader);
-
-ctx.attachShader(program, vertexShader);
-ctx.attachShader(program, fragShader);
-ctx.linkProgram(program);
+e('shader source', ctx.shaderSource(vertexShader, shaders.vertex));
+e('shader source', ctx.shaderSource(fragShader, shaders.frag));
+e('compile shader', ctx.compileShader(vertexShader));
+e('compile shader', ctx.compileShader(fragShader));
+e('attach shader', ctx.attachShader(program, vertexShader));
+e('attach shader', ctx.attachShader(program, fragShader));
+e('link program', ctx.linkProgram(program));
if (!ctx.getProgramParameter(program, ctx.LINK_STATUS)) {
- console.log("Could not link program");
+ console.log("Could not link program.\n Error:", ctx.getError());
+ console.log(ctx.getProgramInfoLog(program));
fail();
}
-ctx.useProgram(program);
-var attr = ctx.getAttribLocation(program, "pos");
-var vertexBuffer = ctx.createBuffer();
-
-vertexBuffer.itemSize = 3;
-vertexBuffer.numItems = 3;
+e('use program', ctx.useProgram(program));
+var attr = e('attribute location', ctx.getAttribLocation(program, "pos"));
+var vertexBuffer = e('create buffer', ctx.createBuffer());
-/*
- const float vertexPositions[] = {
- 0.75f, 0.75f, 0.0f, 1.0f,
- 0.75f, -0.75f, 0.0f, 1.0f,
- -0.75f, -0.75f, 0.0f, 1.0f,
- };
-*/
-
-var vertices = [
- 0.0, 1.0, 0.0,
- -1.0, -1.0, 0.0,
- 1.0, -1.0, 0.0
-];
-
-ctx.bindBuffer(ctx.ARRAY_BUFFER, vertexBuffer);
-ctx.bufferData(ctx.ARRAY_BUFFER, new Float32Array(vertexBuffer));
+e('bind buffer', ctx.bindBuffer(ctx.ARRAY_BUFFER, vertexBuffer));
+e('buffer data', ctx.bufferData(ctx.ARRAY_BUFFER, new Float32Array(vertexBuffer)));
var a = 10000;
while(a--) {
-
- ctx.clearColor(0.5, 0.5, 0.5, 1);
- ctx.clear(ctx.COLOR_BUFFER_BIT, ctx.DEPTH_BUFFER_BIT);
- ctx.bindBuffer(ctx.ARRAY_BUFFER, vertexBuffer);
- ctx.enableVertexAttribArray(attr);
- ctx.vertexAttribPointer(attr, vertexBuffer.itemSize, ctx.FLOAT, false, 0, 0);
- ctx.drawArrays(ctx.TRIANGLES, 0, 3);
- ctx.flush();
+ e('clear color', ctx.clearColor(0.5, 0.5, 0.5, 1));
+ e('clear', ctx.clear(ctx.COLOR_BUFFER_BIT | ctx.DEPTH_BUFFER_BIT));
+ e('bind buffer', ctx.bindBuffer(ctx.ARRAY_BUFFER, vertexBuffer));
+ e('enable vertex attrib array', ctx.enableVertexAttribArray(attr));
+ e('vertex pointer', ctx.vertexAttribPointer(attr, 3, ctx.FLOAT, false, 0, 0));
+ e('draw arrays', ctx.drawArrays(ctx.TRIANGLES, 0, 3));
+ e('flush', ctx.flush());
}
-
-fail();
+console.log("DONE");
View
240 src/js/spidermonkey.cc
@@ -30,6 +30,15 @@ JSBool webgl_rendering_context_activeTexture(JSContext *cx, uintN argc, jsval *a
JSBool webgl_rendering_context_attachShader(JSContext *cx, uintN argc, jsval *argv) {
+ int program;
+ int shader;
+
+ if (!JS_ConvertArguments(cx, argc, JS_ARGV(cx, argv), "ii", &program, &shader)) {
+ return JS_FALSE;
+ }
+
+ glAttachShader(program, shader);
+
return JS_TRUE;
}
@@ -40,6 +49,15 @@ JSBool webgl_rendering_context_bindAttribLocation(JSContext *cx, uintN argc, jsv
JSBool webgl_rendering_context_bindBuffer(JSContext *cx, uintN argc, jsval *argv) {
+ int target;
+ int buffer;
+
+ if (!JS_ConvertArguments(cx, argc, JS_ARGV(cx, argv), "ii", &target, &buffer)) {
+ return JS_FALSE;
+ }
+
+ glBindBuffer(target, buffer);
+
return JS_TRUE;
}
@@ -86,6 +104,28 @@ JSBool webgl_rendering_context_blendFuncSeparate(JSContext *cx, uintN argc, jsva
JSBool webgl_rendering_context_bufferData(JSContext *cx, uintN argc, jsval *argv) {
+
+ int target;
+ JSObject *array;
+ int usage;
+
+ if (!JS_ConvertArguments(cx, argc, JS_ARGV(cx, argv), "ioi", &target, &array, &usage)) {
+ return JS_FALSE;
+ }
+
+ // TODO: convert it into a typed array?
+ if (!js_IsTypedArray(array)) {
+ return JS_FALSE;
+ }
+
+ js::TypedArray *tarray = js::TypedArray::fromJSObject(array);
+ JS_ASSERT(tarray);
+
+ cout << "data " << tarray->data << " type:" << tarray->type << endl;
+ uintN srclen = tarray->length;
+
+ glBufferData(target, srclen, tarray->data, usage);
+
return JS_TRUE;
}
@@ -102,18 +142,40 @@ JSBool webgl_rendering_context_checkFramebufferStatus(JSContext *cx, uintN argc,
JSBool webgl_rendering_context_clear(JSContext *cx, uintN argc, jsval *argv) {
+ int bits;
+
+ if (!JS_ConvertArguments(cx, argc, JS_ARGV(cx, argv), "i", &bits)) {
+ return JS_FALSE;
+ }
- glClear(GL_DEPTH);
+ glClear(bits);
return JS_TRUE;
}
JSBool webgl_rendering_context_clearColor(JSContext *cx, uintN argc, jsval *argv) {
- glClearColor(0,0,0,1);
+
+ jsdouble r;
+ jsdouble g;
+ jsdouble b;
+ jsdouble a;
+ if (!JS_ConvertArguments(cx, argc, JS_ARGV(cx, argv), "dddd", &r, &g, &b, &a)) {
+ return JS_FALSE;
+ }
+
+ glClearColor(r,g,b,a);
+
return JS_TRUE;
}
JSBool webgl_rendering_context_clearDepth(JSContext *cx, uintN argc, jsval *argv) {
+ jsdouble clampd;
+ if (!JS_ConvertArguments(cx, argc, JS_ARGV(cx, argv), "d", &clampd)) {
+ return JS_FALSE;
+ }
+
+ glClearDepth(clampd);
+
return JS_TRUE;
}
@@ -129,6 +191,13 @@ JSBool webgl_rendering_context_colorMask(JSContext *cx, uintN argc, jsval *argv)
JSBool webgl_rendering_context_compileShader(JSContext *cx, uintN argc, jsval *argv) {
+ int id;
+ if (!JS_ConvertArguments(cx, argc, JS_ARGV(cx, argv), "i", &id)) {
+ return JS_FALSE;
+ }
+
+ glCompileShader(id);
+
return JS_TRUE;
}
@@ -145,7 +214,9 @@ JSBool webgl_rendering_context_copyTexSubImage2D(JSContext *cx, uintN argc, jsva
JSBool webgl_rendering_context_createBuffer(JSContext *cx, uintN argc, jsval *argv) {
-
+ unsigned int buffer;
+ glGenBuffers(1, &buffer);
+ JS_SET_RVAL(cx, argv, INT_TO_JSVAL(buffer));
return JS_TRUE;
}
@@ -155,18 +226,32 @@ JSBool webgl_rendering_context_createFramebuffer(JSContext *cx, uintN argc, jsva
}
JSBool webgl_rendering_context_createProgram(JSContext *cx, uintN argc, jsval *argv) {
- GLenum program = glCreateProgram();
- JS_SET_RVAL(cx, argv, DOUBLE_TO_JSVAL(program));
+
+ GLenum ret = glCreateProgram();
+ JS_SET_RVAL(cx, argv, INT_TO_JSVAL(ret));
+
return JS_TRUE;
}
JSBool webgl_rendering_context_createRenderbuffer(JSContext *cx, uintN argc, jsval *argv) {
+ GLenum ret = glCreateProgram();
+ JS_SET_RVAL(cx, argv, INT_TO_JSVAL(ret));
+
return JS_TRUE;
}
JSBool webgl_rendering_context_createShader(JSContext *cx, uintN argc, jsval *argv) {
+ int type;
+
+ if (!JS_ConvertArguments(cx, argc, JS_ARGV(cx, argv), "i", &type)) {
+ return JS_FALSE;
+ }
+
+ GLenum ret = glCreateShader(type);
+ JS_SET_RVAL(cx, argv, INT_TO_JSVAL(ret));
+
return JS_TRUE;
}
@@ -240,6 +325,13 @@ JSBool webgl_rendering_context_disable(JSContext *cx, uintN argc, jsval *argv) {
JSBool webgl_rendering_context_disableVertexAttribArray(JSContext *cx, uintN argc, jsval *argv) {
+ int attr;
+ if (!JS_ConvertArguments(cx, argc, JS_ARGV(cx, argv), "i", &attr)) {
+ return JS_FALSE;
+ }
+
+ glDisableVertexAttribArray(attr);
+
return JS_TRUE;
}
@@ -250,6 +342,16 @@ JSBool webgl_rendering_context_drawArrays(JSContext *cx, uintN argc, jsval *argv
JSBool webgl_rendering_context_drawElements(JSContext *cx, uintN argc, jsval *argv) {
+ int mode;
+ int first;
+ int count;
+
+ if (!JS_ConvertArguments(cx, argc, JS_ARGV(cx, argv), "iii", &mode, &first, &count)) {
+ return JS_FALSE;
+ }
+
+ glDrawArrays(mode, first, count);
+
return JS_TRUE;
}
@@ -261,6 +363,13 @@ JSBool webgl_rendering_context_enable(JSContext *cx, uintN argc, jsval *argv) {
JSBool webgl_rendering_context_enableVertexAttribArray(JSContext *cx, uintN argc, jsval *argv) {
+ int attr;
+ if (!JS_ConvertArguments(cx, argc, JS_ARGV(cx, argv), "i", &attr)) {
+ return JS_FALSE;
+ }
+
+ glEnableVertexAttribArray(attr);
+
return JS_TRUE;
}
@@ -316,6 +425,17 @@ JSBool webgl_rendering_context_getAttachedShaders(JSContext *cx, uintN argc, jsv
JSBool webgl_rendering_context_getAttribLocation(JSContext *cx, uintN argc, jsval *argv) {
+ int program;
+ JSString *js_attr;
+ if (!JS_ConvertArguments(cx, argc, JS_ARGV(cx, argv), "iS", &program, &js_attr)) {
+ return JS_FALSE;
+ }
+
+ const char *attr = JS_EncodeString(cx, js_attr);
+ int ret = glGetAttribLocation(program, attr);
+
+ JS_SET_RVAL(cx, argv, INT_TO_JSVAL(ret));
+
return JS_TRUE;
}
@@ -332,7 +452,7 @@ JSBool webgl_rendering_context_getBufferParameter(JSContext *cx, uintN argc, jsv
JSBool webgl_rendering_context_getError(JSContext *cx, uintN argc, jsval *argv) {
-
+ JS_SET_RVAL(cx, argv, INT_TO_JSVAL(glGetError()));
return JS_TRUE;
}
@@ -344,11 +464,53 @@ JSBool webgl_rendering_context_getFramebufferAttachmentParameter(JSContext *cx,
JSBool webgl_rendering_context_getProgramParameter(JSContext *cx, uintN argc, jsval *argv) {
+ int param;
+ int program;
+
+ if (!JS_ConvertArguments(cx, argc, JS_ARGV(cx, argv), "ii", &program, &param)) {
+ return JS_FALSE;
+ }
+
+ int status;
+ glGetProgramiv(program, param, &status);
+
+ bool b_status = (status == GL_TRUE) ? true : false;
+
+ switch (param) {
+ case GL_DELETE_STATUS:
+ case GL_LINK_STATUS:
+ case GL_VALIDATE_STATUS:
+ JS_SET_RVAL(cx, argv, BOOLEAN_TO_JSVAL(status));
+ break;
+
+ // all others are int return types
+ default:
+ JS_SET_RVAL(cx, argv, INT_TO_JSVAL(status));
+ break;
+ }
+
return JS_TRUE;
}
JSBool webgl_rendering_context_getProgramInfoLog(JSContext *cx, uintN argc, jsval *argv) {
+ int program;
+
+ if (!JS_ConvertArguments(cx, argc, JS_ARGV(cx, argv), "i", &program)) {
+ return JS_FALSE;
+ }
+
+ int logLength = 0;
+ int maxLength;
+
+ glGetProgramiv(program, GL_INFO_LOG_LENGTH, &maxLength);
+ char infoLog[maxLength];
+
+ glGetProgramInfoLog(program, maxLength, &logLength, infoLog);
+
+ JSString *jsLogString = JS_NewStringCopyN(cx, infoLog, logLength);
+ JS_SET_RVAL(cx, argv, STRING_TO_JSVAL(jsLogString));
+
return JS_TRUE;
}
@@ -363,6 +525,22 @@ JSBool webgl_rendering_context_getShaderParameter(JSContext *cx, uintN argc, jsv
}
JSBool webgl_rendering_context_getShaderInfoLog(JSContext *cx, uintN argc, jsval *argv) {
+ int shader;
+
+ if (!JS_ConvertArguments(cx, argc, JS_ARGV(cx, argv), "i", &shader)) {
+ return JS_FALSE;
+ }
+
+ int logLength = 0;
+ int maxLength;
+
+ glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &maxLength);
+ char infoLog[maxLength];
+
+ glGetShaderInfoLog(shader, maxLength, &logLength, infoLog);
+
+ JSString *jsLogString = JS_NewStringCopyN(cx, infoLog, logLength);
+ JS_SET_RVAL(cx, argv, STRING_TO_JSVAL(jsLogString));
return JS_TRUE;
}
@@ -451,6 +629,13 @@ JSBool webgl_rendering_context_lineWidth(JSContext *cx, uintN argc, jsval *argv)
JSBool webgl_rendering_context_linkProgram(JSContext *cx, uintN argc, jsval *argv) {
+ int program;
+ if (!JS_ConvertArguments(cx, argc, JS_ARGV(cx, argv), "i", &program)) {
+ return JS_FALSE;
+ }
+
+ glLinkProgram(program);
+
return JS_TRUE;
}
@@ -489,6 +674,17 @@ JSBool webgl_rendering_context_scissor(JSContext *cx, uintN argc, jsval *argv) {
JSBool webgl_rendering_context_shaderSource(JSContext *cx, uintN argc, jsval *argv) {
+ int id;
+ JSString *js_source;
+ if (!JS_ConvertArguments(cx, argc, JS_ARGV(cx, argv), "iS", &id, &js_source)) {
+ return JS_FALSE;
+ }
+
+ const char *source = JS_EncodeString(cx, js_source);
+
+ int length = JS_GetStringLength(js_source);
+ glShaderSource(id, 1, &source, &length);
+
return JS_TRUE;
}
@@ -644,6 +840,13 @@ JSBool webgl_rendering_context_uniformMatrix4fv(JSContext *cx, uintN argc, jsval
JSBool webgl_rendering_context_useProgram(JSContext *cx, uintN argc, jsval *argv) {
+ int program;
+ if (!JS_ConvertArguments(cx, argc, JS_ARGV(cx, argv), "i", &program)) {
+ return JS_FALSE;
+ }
+
+ glUseProgram(program);
+
return JS_TRUE;
}
@@ -696,13 +899,34 @@ JSBool webgl_rendering_context_vertexAttrib4fv(JSContext *cx, uintN argc, jsval
JSBool webgl_rendering_context_vertexAttribPointer(JSContext *cx, uintN argc, jsval *argv) {
+ unsigned int attr;
+ int size;
+ int type;
+ bool normalized;
+ int stride;
+ int offset;
+
+ if (!JS_ConvertArguments(cx, argc, JS_ARGV(cx, argv), "iiibii", &attr, &size, &type, &normalized, &stride, &offset)) {
+ return JS_FALSE;
+ }
+
+ glVertexAttribPointer(attr, size, type, normalized, stride, (void *)&offset);
+
return JS_TRUE;
}
JSBool webgl_rendering_context_viewport(JSContext *cx, uintN argc, jsval *argv) {
- return JS_TRUE;
-}
+ int x;
+ int y;
+ int w;
+ int h;
+ if (!JS_ConvertArguments(cx, argc, JS_ARGV(cx, argv), "iiibii", &x, &y, &w, &h)) {
+ return JS_FALSE;
+ }
+ glViewport(x, y, w, h);
+ return JS_TRUE;
+}

0 comments on commit d573cac

Please sign in to comment.
Something went wrong with that request. Please try again.