void swapDisplayBuffers(window w) { #if OXYGINE_SDL if (!_context) return; #endif CHECKGL(); #if __S3E__ IwGLSwapBuffers(); #elif EMSCRIPTEN SDL_GL_SwapBuffers(); #elif OXYGINE_SDL SDL_Window *wnd = w; if (!wnd) wnd = _window; if (_context) { int status = SDL_GL_MakeCurrent(wnd, _context); if (status) { log::error("SDL_GL_MakeCurrent(): %s", SDL_GetError()); } SDL_GL_SwapWindow(wnd); } #endif IVideoDriver::instance->getStats(_videoStats); IVideoDriver::instance->swapped(); CHECKGL(); //sleep(1000/50); }
void VideoDriverGLES20::setShaderProgram(ShaderProgram* prog_) { CHECKGL(); ShaderProgramGL* prog = safeCast<ShaderProgramGL*>(prog_); unsigned int id = prog->getID(); oxglUseProgram(id); _program = id; CHECKGL(); }
void VideoDriverGL::setRenderTarget(spNativeTexture rt) { if (!rt) { glBindFramebuffer(GL_FRAMEBUFFER, _prevFBO); CHECKGL(); return; } glGetIntegerv(GL_FRAMEBUFFER_BINDING, &_prevFBO); NativeTextureGLES *gl = safeCast<NativeTextureGLES*>(rt.get()); glBindFramebuffer(GL_FRAMEBUFFER, gl->getFboID()); CHECKGL(); }
void VideoDriverGLES20::setShaderProgram(ShaderProgram* prog_) { CHECKGL(); ShaderProgramGL* prog = safeCast<ShaderProgramGL*>(prog_); unsigned int id = prog->getID(); glUseProgram(id); _program = id; int gl_error = glGetError(); if (gl_error != GL_NO_ERROR) { log::error("OpenGL error: %#x\n", gl_error); char buf[1024]; GLsizei len; glGetProgramInfoLog(id, sizeof(buf)-1, &len, buf); buf[len] = 0; log::error("OpenGL error: %s\n", buf); if (gl_error == GL_OUT_OF_MEMORY) { exit(0); } } // CHECKGL(); }
void VideoDriverGLES20::draw(PRIMITIVE_TYPE pt, const VertexDeclaration* decl_, const void* vdata, unsigned int size) { const VertexDeclarationGL* decl = static_cast<const VertexDeclarationGL*>(decl_); const unsigned char* verticesData = (const unsigned char*)vdata; const VertexDeclarationGL::Element* el = decl->elements; for (int i = 0; i < decl->numElements; ++i) { oxglEnableVertexAttribArray(el->index); oxglVertexAttribPointer(el->index, el->size, el->elemType, el->normalized, decl->size, verticesData + el->offset); el++; } size_t primitives = size / decl->size; glDrawArrays(getPT(pt), 0, (GLsizei)primitives); el = decl->elements; for (int i = 0; i < decl->numElements; ++i) { oxglDisableVertexAttribArray(el->index); el++; } #if OXYGINE_TRACE_VIDEO_STATS _debugAddPrimitives(pt, (int)(GLsizei)primitives); #endif CHECKGL(); }
void NativeTextureGLES::updateRegion(int x, int y, const ImageData& data_) { ImageData data = data_; assert(_width >= data.w - x); assert(_height >= data.h - y); glBindTexture(GL_TEXTURE_2D, (GLuint) _id); glPixel glp = SurfaceFormat2GL(_format); //saveImage(data, "test1.png"); MemoryTexture mt; if (_format != data.format) { mt.init(data.w, data.h, _format); mt.fill_zero(); mt.updateRegion(0, 0, data); data = mt.lock(); } glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, data.w, data.h, glp.format, glp.type, data.data); CHECKGL(); }
void VideoDriverGLES20::draw(PRIMITIVE_TYPE pt, const VertexDeclaration* decl_, const void* vdata, unsigned int verticesDataSize, const void* indicesData, unsigned int numIndices, bool indicesShortType) { const VertexDeclarationGL* decl = static_cast<const VertexDeclarationGL*>(decl_); const unsigned char* verticesData = (const unsigned char*)vdata; const VertexDeclarationGL::Element* el = decl->elements; for (int i = 0; i < decl->numElements; ++i) { oxglEnableVertexAttribArray(el->index); oxglVertexAttribPointer(el->index, el->size, el->elemType, el->normalized, decl->size, verticesData + el->offset); el++; } glDrawElements(getPT(pt), numIndices, indicesShortType ? GL_UNSIGNED_SHORT : GL_UNSIGNED_BYTE, indicesData); el = decl->elements; for (int i = 0; i < decl->numElements; ++i) { oxglDisableVertexAttribArray(el->index); el++; } #if OXYGINE_TRACE_VIDEO_STATS _debugAddPrimitives(pt, numIndices); #endif CHECKGL(); }
void VideoDriverGLES20::clear(const Color& color) { glClearColor(color.getRedF(), color.getGreenF(), color.getBlueF(), color.getAlphaF()); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); CHECKGL(); }
int ShaderProgramGL::getUniformLocation(const char *id) const { int i = oxglGetUniformLocation(_program, id); //if (i == -1) CHECKGL(); return i; }
unsigned int ShaderProgramGL::createShader(unsigned int type, const char* data, const char* prepend, const char* append) { GLuint shader = oxglCreateShader(type); const char* sources[16]; const char** ptr = &sources[0]; bool gles = false; #ifdef __S3E__ gles = true; #elif OXYGINE_SDL int profile = 0; SDL_GL_GetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, &profile); gles = profile == SDL_GL_CONTEXT_PROFILE_ES; #else #endif #ifndef EMSCRIPTEN if (!gles) { log::messageln("not gles version"); static const char nonGLES[] = "#define lowp\n" "#define mediump\n" "#define highp\n"; *ptr = nonGLES; ptr++; } #endif if (prepend) { *ptr = prepend; ptr++; } *ptr = data; ptr++; if (append) { *ptr = append; ptr++; } int num = (int)(ptr - sources); oxglShaderSource(shader, num, sources, 0); oxglCompileShader(shader); printShaderInfoLog(shader); CHECKGL(); return shader; }
unsigned int ShaderProgramGL::createProgram(int vs, int fs, const VertexDeclarationGL* decl) { int p = oxglCreateProgram(); oxglAttachShader(p, vs); oxglAttachShader(p, fs); for (int i = 0; i < decl->numElements; ++i) oxglBindAttribLocation(p, decl->elements[i].index, decl->elements[i].name); oxglLinkProgram(p); std::string log; bool success = getProgramBuildLog(p, log); if (success) { //log::messageln("compiled shader: %s", log.c_str()); } else { log::error("can't link gl program: %s", log.c_str()); oxglDeleteProgram(p); p = 0; } CHECKGL(); return p; }
void NativeTextureGLES::init(int w, int h, TextureFormat tf, bool rt) { release(); size_t id = createTexture(); if (rt) { if (!HAVE_NPOT_RT()) { w = nextPOT(w); h = nextPOT(h); } } glPixel p = SurfaceFormat2GL(tf); glTexImage2D(GL_TEXTURE_2D, 0, p.format, w, h, 0, p.format, p.type, 0); if (rt) { int prevFBO = 0; glGetIntegerv(GL_FRAMEBUFFER_BINDING, &prevFBO); glBindTexture(GL_TEXTURE_2D, 0); unsigned int fbo = 0; oxglGenFramebuffers(1, &fbo); oxglBindFramebuffer(GL_FRAMEBUFFER, fbo); oxglFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, (GLuint)id, 0); //printf("created fbo: %d\n", fbo); unsigned int status = oxglCheckFramebufferStatus(GL_FRAMEBUFFER); //log::message("fbo status %d\n", status); //GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_OES if (status != GL_FRAMEBUFFER_COMPLETE) { log::error("status != GL_FRAMEBUFFER_COMPLETE_OES"); } glViewport(0, 0, w, h); glClearColor(0, 0, 0, 0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); oxglBindFramebuffer(GL_FRAMEBUFFER, prevFBO); //log::message("fbo bind\n"); glBindTexture(GL_TEXTURE_2D, 0); _fbo = fbo; } _id = id; _width = w; _height = h; _format = tf; CHECKGL(); }
void VideoDriverGLES20::setUniform(const char *id, const Vector4 *v, int num) { int p = glGetUniformLocation(_program, id); if (p == -1) return; glUniform4fv(p, num, v->m); CHECKGL(); }
void VideoDriverGLES20::setUniformInt(const char* id, int v) { int location = oxglGetUniformLocation(_program, id); if (location == -1) return; oxglUniform1i(location, v); CHECKGL(); }
void VideoDriverGLES20::setUniform(const char* id, float val) { int p = oxglGetUniformLocation(_program, id); if (p == -1) return; oxglUniform1f(p, val); CHECKGL(); }
void VideoDriverGLES20::setUniform(const char* id, const Vector3* v, int num) { int p = oxglGetUniformLocation(_program, id); if (p == -1) return; oxglUniform3fv(p, num, &v->x); CHECKGL(); }
void VideoDriverGLES20::setUniform(const char* id, const Matrix* mat) { int p = oxglGetUniformLocation(_program, id); if (p == -1) return; oxglUniformMatrix4fv(p, 1, GL_FALSE, mat->ml); CHECKGL(); }
void VideoDriverGL::getViewport(Rect &r) const { GLint vp[4]; glGetIntegerv(GL_VIEWPORT, vp); r = Rect(vp[0], vp[1], vp[2], vp[3]);; CHECKGL(); }
void NativeTextureGLES::setLinearFilter(bool enable) { glBindTexture(GL_TEXTURE_2D, (GLuint) _id); unsigned int f = enable ? GL_LINEAR : GL_NEAREST; glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, f); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, f); CHECKGL(); }
void VideoDriverGLES20::setTexture(int sampler, spNativeTexture t) { oxglActiveTexture(GL_TEXTURE0 + sampler); if (t) glBindTexture(GL_TEXTURE_2D, (GLuint)((size_t)t->getHandle())); else glBindTexture(GL_TEXTURE_2D, 0); CHECKGL(); }
void VideoDriverGL::getViewport(Rect &r) const { GLint vp[4]; glGetIntegerv(GL_VIEWPORT, vp); r = Rect(vp[0], vp[1], vp[2], vp[3]);; //log::messageln("vp %d %d %d %d", vp[0], vp[1], vp[2], vp[3]); CHECKGL(); }
void init2() { #ifdef OXYGINE_QT setlocale(LC_ALL, "POSIX"); #endif #ifdef OXYGINE_SDL int missing = initGLExtensions(SDL_GL_GetProcAddress); #endif Point size = getDisplaySize(); log::messageln("display size: %d %d", size.x, size.y); #if __S3E__ int glversion = s3eGLGetInt(S3E_GL_VERSION); int major_gl = glversion >> 8; OX_ASSERT( major_gl == 2 && "gl version should be 2"); IVideoDriver::instance = new VideoDriverGLES20(); #endif IVideoDriver::instance = new VideoDriverGLES20(); CHECKGL(); IVideoDriver::instance->setDefaultSettings(); CHECKGL(); Renderer::initialize(); Resources::registerResourceType(ResAtlas::create, "atlas"); Resources::registerResourceType(ResBuffer::create, "buffer"); Resources::registerResourceType(ResFontBM::create, "font"); Resources::registerResourceType(ResFontBM::createBM, "bmfc_font"); Resources::registerResourceType(ResFontBM::createSD, "sdfont"); Resources::registerResourceType(ResStarlingAtlas::create, "starling"); CHECKGL(); log::messageln("oxygine initialized"); }
void NativeTextureGLES::setClamp2Edge(bool clamp2edge) { glBindTexture(GL_TEXTURE_2D, (GLuint) _id); unsigned int f = clamp2edge ? GL_CLAMP_TO_EDGE : GL_REPEAT; glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, f); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, f); CHECKGL(); }
void VideoDriverGL::setScissorRect(const Rect *rect) { if (rect) { glEnable(GL_SCISSOR_TEST); glScissor(rect->getX(), rect->getY(), rect->getWidth(), rect->getHeight()); } else glDisable(GL_SCISSOR_TEST); CHECKGL(); }
bool VideoDriverGL::getScissorRect(Rect &r) const { GLboolean scrTest = glIsEnabled(GL_SCISSOR_TEST); GLint box[4]; glGetIntegerv(GL_SCISSOR_BOX, box); r = Rect(box[0], box[1], box[2], box[3]); CHECKGL(); return scrTest ? true : false; }
bool beginRendering(window w) { #ifdef OXYGINE_SDL SDL_Window *wnd = w; if (!wnd) wnd = _window; SDL_GL_MakeCurrent(wnd, _context); #endif CHECKGL(); return Renderer::isReady(); }
void VideoDriverGLES20::setDefaultSettings() { //glEnable(GL_TEXTURE_2D); glDisable(GL_SCISSOR_TEST); glDisable(GL_DEPTH_TEST); glDisable(GL_BLEND); glDepthMask(GL_FALSE); glCullFace(GL_FRONT_AND_BACK); CHECKGL(); //setProgram(_us.getShaderProgram(0)->program); }
unsigned int ShaderProgramGL::createProgram(int vs, int fs, const VertexDeclarationGL *decl) { int p = oxglCreateProgram(); oxglAttachShader(p, vs); oxglAttachShader(p, fs); for (int i = 0; i < decl->numElements; ++i) oxglBindAttribLocation(p, decl->elements[i].index, decl->elements[i].name); oxglLinkProgram(p); CHECKGL(); return p; }
void VideoDriverGL::setState(STATE state, unsigned int value) { switch(state) { case STATE_BLEND: if (value) glEnable(GL_BLEND); else glDisable(GL_BLEND); break; default: OX_ASSERT(!"unknown state"); } CHECKGL(); }
void VideoDriverGLES20::setUniform(const char *id, const Matrix *mat) { int p = glGetUniformLocation(_program, id); if (p == -1) return; /* log::messageln("mat "); for (int i = 0; i < 16; ++i) { log::messageln("%g ", mat->ml[i]); } */ glUniformMatrix4fv(p, 1, GL_FALSE, mat->ml); CHECKGL(); }