void OpenGL::BindTexture ( GLuint _textureID ) { CrbDebugAssert ( this ); CrbReleaseAssert ( _textureID != SCREEN_SURFACE_ID ); CrbReleaseAssert ( _textureID != INVALID_SURFACE_ID ); CrbReleaseAssert ( _textureID != 0 ); glBindTexture ( GetTextureTarget(), _textureID ); }
void MacIOSurfaceTextureSourceOGL::BindTexture(GLenum aTextureUnit, gfx::Filter aFilter) { if (!gl()) { NS_WARNING("Trying to bind a texture without a GLContext"); return; } GLuint tex = mCompositor->GetTemporaryTexture(GetTextureTarget(), aTextureUnit); gl()->fActiveTexture(aTextureUnit); gl()->fBindTexture(LOCAL_GL_TEXTURE_RECTANGLE_ARB, tex); mSurface->CGLTexImageIOSurface2D(gl::GLContextCGL::Cast(gl())->GetCGLContext()); ApplyFilterToBoundTexture(gl(), aFilter, LOCAL_GL_TEXTURE_RECTANGLE_ARB); }
void SharedTextureSourceOGL::BindTexture(GLenum aTextureUnit, gfx::Filter aFilter) { if (!gl()) { NS_WARNING("Trying to bind a texture without a GLContext"); return; } GLuint tex = mCompositor->GetTemporaryTexture(GetTextureTarget(), aTextureUnit); gl()->fActiveTexture(aTextureUnit); gl()->fBindTexture(mTextureTarget, tex); if (!AttachSharedHandle(gl(), mShareType, mSharedHandle)) { NS_ERROR("Failed to bind shared texture handle"); return; } ApplyFilterToBoundTexture(gl(), aFilter, mTextureTarget); }
void LuaMatTexture::Unbind() const { if (type == LUATEX_NONE) return; if (enableTexParams && type == LUATEX_SHADOWMAP) { glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE_ARB, GL_NONE); glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE_ARB, GL_LUMINANCE); } if (!enable) return; const GLuint texType = GetTextureTarget(); if (texType == GL_TEXTURE_2D) glDisable(GL_TEXTURE_2D); }
void LuaMatTexture::Bind() const { const GLuint texID = GetTextureID(); const GLuint texType = GetTextureTarget(); if (texID != 0) { glBindTexture(texType, texID); if (enable && (texType == GL_TEXTURE_2D)) { glEnable(GL_TEXTURE_2D); } } else if (!enable && (texType == GL_TEXTURE_2D)) { glDisable(GL_TEXTURE_2D); } if (enableTexParams && type == LUATEX_SHADOWMAP) { glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC_ARB, GL_LEQUAL); glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE_ARB, GL_LUMINANCE); } }
void EGLImageTextureSource::BindTexture(GLenum aTextureUnit, gfx::Filter aFilter) { if (!gl()) { NS_WARNING("Trying to bind a texture without a GLContext"); return; } MOZ_ASSERT(DoesEGLContextSupportSharingWithEGLImage(gl()), "EGLImage not supported or disabled in runtime"); GLuint tex = mCompositor->GetTemporaryTexture(GetTextureTarget(), aTextureUnit); gl()->fActiveTexture(aTextureUnit); gl()->fBindTexture(mTextureTarget, tex); MOZ_ASSERT(mTextureTarget == LOCAL_GL_TEXTURE_2D); gl()->fEGLImageTargetTexture2D(LOCAL_GL_TEXTURE_2D, mImage); ApplyFilterToBoundTexture(gl(), aFilter, mTextureTarget); }
void nuiGLPainter::ApplyTexture(const nuiRenderState& rState, bool ForceApply) { // if ((rState.mTexturing && !rState.mpTexture) || (!rState.mTexturing && rState.mpTexture)) // { // printf("bleh!\n"); // char* bleh = NULL; // bleh[0] = 0; // } // 2D Textures: std::map<nuiTexture*, TextureInfo>::const_iterator it = mTextures.find(rState.mpTexture); bool uptodate = (it == mTextures.end()) ? false : ( !it->second.mReload && it->second.mTexture >= 0 ); if (ForceApply || (mState.mpTexture != rState.mpTexture) || (mState.mpTexture && !uptodate)) { GLenum intarget = 0; GLenum outtarget = 0; if (mState.mpTexture) { outtarget = GetTextureTarget(mState.mpTexture->IsPowerOfTwo()); //mState.mpTexture->UnapplyGL(this); #TODO Un apply the texture nuiCheckForGLErrors(); mState.mpTexture->Release(); nuiCheckForGLErrors(); } //NGL_OUT(_T("Change texture to 0x%x (%ls)\n"), rState.mpTexture, rState.mpTexture?rState.mpTexture->GetSource().GetChars() : nglString::Empty.GetChars()); mState.mpTexture = rState.mpTexture ; if (mState.mpTexture) { intarget = GetTextureTarget(mState.mpTexture->IsPowerOfTwo()); mState.mpTexture->Acquire(); nuiSurface* pSurface = mState.mpTexture->GetSurface(); if (pSurface) { std::map<nuiSurface*, FramebufferInfo>::const_iterator it = mFramebuffers.find(pSurface); bool create = (it == mFramebuffers.end()) ? true : false; if (create || pSurface->IsDirty()) { PushClipping(); nuiRenderState s(mState);// PushState(); PushProjectionMatrix(); PushMatrix(); #ifdef _OPENGL_ES_ if (mpSurfaceStack.empty()) { // mDefaultFramebuffer = 0; // mDefaultRenderbuffer = 0; glGetIntegerv(GL_FRAMEBUFFER_BINDING_NUI, &mDefaultFramebuffer); glGetIntegerv(GL_RENDERBUFFER_BINDING_NUI, (GLint *) &mDefaultRenderbuffer); } #endif PushSurface(); SetState(nuiRenderState()); ResetClipRect(); mClip.Set(0, 0, pSurface->GetWidth(), pSurface->GetHeight()); LoadMatrix(nglMatrixf()); NGL_ASSERT(pSurface); SetSurface(pSurface); //Set2DProjectionMatrix(nuiRect(0.0f, 0.0f, pSurface->GetWidth(), pSurface->GetHeight())); nuiMatrix m; m.Translate(-1.0f, 1.0f, 0.0f); m.Scale(2.0f / pSurface->GetWidth(), -2.0f / pSurface->GetHeight(), 1.0f); LoadProjectionMatrix(nuiRect(pSurface->GetWidth(), pSurface->GetHeight()), m); // clear the surface with transparent black: // nuiRenderState s2(mState);// PushState(); // mState.mClearColor = nuiColor(0.0f, 0.0f, 0.0f, 0.0f); SetState(mState); // ClearColor(); // SetState(s2); ////////////////////////////// nuiDrawContext Ctx(nuiRect(pSurface->GetWidth(), pSurface->GetHeight())); Ctx.SetPainter(this); pSurface->Realize(&Ctx); Ctx.SetPainter(NULL); ////////////////////////////// PopSurface(); PopMatrix(); PopProjectionMatrix(); //PopState(); SetState(s); PopClipping(); } } UploadTexture(mState.mpTexture); nuiCheckForGLErrors(); } //NGL_OUT(_T("Change texture type from 0x%x to 0x%x\n"), outtarget, intarget); mTextureTarget = intarget; if (intarget != outtarget) { // Texture Target has changed if (outtarget) { glDisable(outtarget); nuiCheckForGLErrors(); } //NGL_OUT(_T("disable outtarget\n")); if (intarget && mState.mTexturing && mState.mpTexture) { mState.mTexturing = rState.mTexturing; //NGL_OUT(_T("enable intarget\n")); glEnable(intarget); nuiCheckForGLErrors(); } } else { // Texture Target have not changed if (mState.mTexturing != rState.mTexturing) // Have texture on/off changed? { // Should enable or disable texturing mState.mTexturing = rState.mTexturing; if (mState.mTexturing) { glEnable(mTextureTarget); nuiCheckForGLErrors(); } else { glDisable(mTextureTarget); nuiCheckForGLErrors(); } } } } if (ForceApply || (mState.mTexturing != rState.mTexturing)) { // Texture have not changed, but texturing may have been enabled / disabled mState.mTexturing = rState.mTexturing; if (mState.mpTexture) { if (mTextureTarget && mState.mTexturing) { //NGL_OUT(_T("Enable 0x%x\n"), mTextureTarget); glEnable(mTextureTarget); nuiCheckForGLErrors(); } else { //NGL_OUT(_T("Disable 0x%x\n"), mTextureTarget); glDisable(mTextureTarget); nuiCheckForGLErrors(); } } else { if (mTextureTarget) { //NGL_OUT(_T("Disable 0x%x\n"), mTextureTarget); glDisable(mTextureTarget); } nuiCheckForGLErrors(); } } }
void nuiGLPainter::SetSurface(nuiSurface* pSurface) { if (mpSurface == pSurface) return; if (pSurface) pSurface->Acquire(); if (mpSurface) mpSurface->Release(); mpSurface = pSurface; if (pSurface) { std::map<nuiSurface*, FramebufferInfo>::const_iterator it = mFramebuffers.find(pSurface); bool create = (it == mFramebuffers.end()) ? true : false; GLuint width = (GLuint)pSurface->GetWidth(); GLuint height = (GLuint)pSurface->GetHeight(); nuiTexture* pTexture = pSurface->GetTexture(); if (pTexture && !pTexture->IsPowerOfTwo()) { switch (GetRectangleTextureSupport()) { case 0: width = (GLuint)pTexture->GetWidthPOT(); height= (GLuint)pTexture->GetHeightPOT(); break; case 1: case 2: break; } } FramebufferInfo info; if (create) { glGenFramebuffersNUI(1, &info.mFramebuffer); //printf("glGenFramebuffersNUI -> %d\n", info.mFramebuffer); nuiCheckForGLErrors(); glBindFramebufferNUI(GL_FRAMEBUFFER_NUI, info.mFramebuffer); nuiCheckForGLErrors(); glBindRenderbufferNUI(GL_RENDERBUFFER_NUI, 0); nuiCheckForGLErrors(); ///< Do we need a depth buffer if (pSurface->GetDepth()) { glGenRenderbuffersNUI(1, &info.mDepthbuffer); nuiCheckForGLErrors(); glBindRenderbufferNUI(GL_RENDERBUFFER_NUI, info.mDepthbuffer); nuiCheckForGLErrors(); glRenderbufferStorageNUI(GL_RENDERBUFFER_NUI, GL_DEPTH_COMPONENT16, width, height); nuiCheckForGLErrors(); glBindRenderbufferNUI(GL_RENDERBUFFER_NUI, 0); nuiCheckForGLErrors(); glFramebufferRenderbufferNUI(GL_FRAMEBUFFER_NUI, GL_DEPTH_ATTACHMENT_NUI, GL_RENDERBUFFER_NUI, info.mDepthbuffer); nuiCheckForGLErrors(); } ///< Do we need a stencil buffer if (pSurface->GetStencil()) { NGL_ASSERT(!"Stencil attachement not supported"); #ifndef _OPENGL_ES_ glGenRenderbuffersNUI(1, &info.mStencilbuffer); nuiCheckForGLErrors(); glBindRenderbufferNUI(GL_RENDERBUFFER_NUI, info.mStencilbuffer); nuiCheckForGLErrors(); glRenderbufferStorageNUI(GL_RENDERBUFFER_NUI, GL_STENCIL_INDEX, width, height); nuiCheckForGLErrors(); glBindRenderbufferNUI(GL_RENDERBUFFER_NUI, 0); nuiCheckForGLErrors(); glFramebufferRenderbufferNUI(GL_FRAMEBUFFER_NUI, GL_STENCIL_ATTACHMENT_NUI, GL_RENDERBUFFER_NUI, info.mStencilbuffer); nuiCheckForGLErrors(); #endif } ///< We definetly need a color attachement, either a texture, or a renderbuffer if (pTexture && pSurface->GetRenderToTexture()) { GLint oldTexture; glGetIntegerv(GL_TEXTURE_BINDING_2D, (GLint *) &oldTexture); UploadTexture(pTexture); std::map<nuiTexture*, TextureInfo>::iterator tex_it = mTextures.find(pTexture); NGL_ASSERT(tex_it != mTextures.end()); TextureInfo& tex_info(tex_it->second); // glBindTexture(GL_TEXTURE_2D, oldTexture); glBindTexture(GL_TEXTURE_2D, 0); nuiCheckForGLErrors(); glFramebufferTexture2DNUI(GL_FRAMEBUFFER_NUI, GL_COLOR_ATTACHMENT0_NUI, GetTextureTarget(pTexture->IsPowerOfTwo()), tex_info.mTexture, 0); //printf("surface texture -> %d\n", tex_info.mTexture); nuiCheckForGLErrors(); } else { glGenRenderbuffersNUI(1, &info.mRenderbuffer); nuiCheckForGLErrors(); glBindRenderbufferNUI(GL_RENDERBUFFER_NUI, info.mRenderbuffer); nuiCheckForGLErrors(); GLint pixelformat = pSurface->GetPixelFormat(); GLint internalPixelformat = pSurface->GetPixelFormat(); internalPixelformat = GL_RGBA; #if !defined(_OPENGL_ES_) && defined(_MACOSX_) if (internalPixelformat == GL_RGBA) { pixelformat = GL_BGRA; } else if (internalPixelformat == GL_RGB) { pixelformat = GL_BGR; } #endif glRenderbufferStorageNUI(GL_RENDERBUFFER_NUI, pixelformat, width, height); nuiCheckForGLErrors(); glFramebufferRenderbufferNUI(GL_FRAMEBUFFER_NUI, GL_COLOR_ATTACHMENT0_NUI, GL_RENDERBUFFER_NUI, info.mRenderbuffer); //printf("surface render buffer -> %d\n", info.mRenderbuffer); nuiCheckForGLErrors(); } CheckFramebufferStatus(); nuiCheckForGLErrors(); mFramebuffers[pSurface] = info; } else { /// !create info = it->second; glBindFramebufferNUI(GL_FRAMEBUFFER_NUI, info.mFramebuffer); glBindRenderbufferNUI(GL_RENDERBUFFER_NUI, info.mRenderbuffer); //printf("glBindFramebufferNUI -> %d\n", info.mFramebuffer); //printf("glBindRenderbufferNUI -> %d\n", info.mRenderbuffer); nuiCheckForGLErrors(); CheckFramebufferStatus(); } } else { /// !pSurface glBindFramebufferNUI(GL_FRAMEBUFFER_NUI, mDefaultFramebuffer); //printf("UNBIND glBindFramebufferNUI -> %d\n", mDefaultFramebuffer); glBindRenderbufferNUI(GL_RENDERBUFFER_NUI, mDefaultRenderbuffer); //printf("UNBIND glBindRenderbufferNUI -> %d\n", mDefaultRenderbuffer); nuiCheckForGLErrors(); CheckFramebufferStatus(); } }
void nuiGLPainter::UploadTexture(nuiTexture* pTexture) { nuiSurface* pSurface = pTexture->GetSurface(); float Width = pTexture->GetUnscaledWidth(); float Height = pTexture->GetUnscaledHeight(); GLenum target = GetTextureTarget(pTexture->IsPowerOfTwo()); bool changedctx = false; std::map<nuiTexture*, TextureInfo>::iterator it = mTextures.find(pTexture); if (it == mTextures.end()) it = mTextures.insert(mTextures.begin(), std::pair<nuiTexture*, TextureInfo>(pTexture, TextureInfo())); NGL_ASSERT(it != mTextures.end()); TextureInfo& info(it->second); nuiCheckForGLErrors(); if (!pTexture->IsPowerOfTwo()) { switch (GetRectangleTextureSupport()) { case 0: Width = pTexture->GetWidthPOT(); Height = pTexture->GetHeightPOT(); break; case 1: case 2: break; } } //NGL_OUT(_T("Apply Target: 0x%x\n"), target); nglImage* pImage = pTexture->GetImage(); { bool firstload = false; bool reload = info.mReload; if (!pSurface && !(pImage && pImage->GetPixelSize())) return; uint i; if (info.mTexture == (GLuint)-1) { // Generate a texture // if (mpSharedContext) // { // mpSharedContext->MakeCurrent(); // nuiCheckForGLErrors(); // changedctx = true; // } glGenTextures(1, &info.mTexture); //NGL_OUT(_T("nuiGLPainter::UploadTexture 0x%x : '%ls' / %d\n"), pTexture, pTexture->GetSource().GetChars(), info.mTexture); nuiCheckForGLErrors(); firstload = true; reload = true; } glBindTexture(target, info.mTexture); nuiCheckForGLErrors(); if (reload) { int type = 8; GLint pixelformat = 0; GLint internalPixelformat = 0; GLbyte* pBuffer = NULL; bool allocated = false; if (pImage) { type = pImage->GetBitDepth(); pixelformat = pImage->GetPixelFormat(); internalPixelformat = pImage->GetPixelFormat(); pBuffer = (GLbyte*)pImage->GetBuffer(); if (!GetRectangleTextureSupport()) { nuiCheckForGLErrors(); if (!pTexture->IsPowerOfTwo()) { pBuffer = (GLbyte*)malloc( (uint)(Width * Height * pImage->GetPixelSize())); if (!pBuffer) return; allocated = true; memset(pBuffer,0, (uint)(Width*Height * pImage->GetPixelSize())); for (i=0; i < pImage->GetHeight(); i++) { memcpy ( pBuffer + (uint)(Width * i * pImage->GetPixelSize()), pImage->GetBuffer() + pImage->GetBytesPerLine()*i, pImage->GetBytesPerLine() ); } } } glPixelStorei(GL_UNPACK_ALIGNMENT,1); nuiCheckForGLErrors(); switch (type) { case 16: case 15: type = GL_UNSIGNED_SHORT_5_5_5_1; break; case 8: case 24: case 32: type = GL_UNSIGNED_BYTE; break; } #if !defined(_OPENGL_ES_) && defined(_MACOSX_) glTexParameteri(target, GL_TEXTURE_STORAGE_HINT_APPLE, GL_STORAGE_CACHED_APPLE); glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE); #endif } else { NGL_ASSERT(pSurface); #if !defined(_OPENGL_ES_) && defined(_MACOSX_) internalPixelformat = pSurface->GetPixelFormat(); if (internalPixelformat == GL_RGBA) { internalPixelformat = GL_RGBA; pixelformat = GL_BGRA; type = GL_UNSIGNED_INT_8_8_8_8_REV; } else if (internalPixelformat == GL_RGB) { internalPixelformat = GL_RGB; pixelformat = GL_BGR; type = GL_UNSIGNED_BYTE; } else { pixelformat = pSurface->GetPixelFormat(); type = GL_UNSIGNED_BYTE; } glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE); #else internalPixelformat = pSurface->GetPixelFormat(); pixelformat = pSurface->GetPixelFormat(); type = GL_UNSIGNED_BYTE; #endif } #ifndef _MACOSX_ if (!firstload) { glTexSubImage2D ( target, 0, 0,0, (int)Width, (int)Height, pixelformat, type, pBuffer ); nuiCheckForGLErrors(); pTexture->ResetForceReload(); } else #endif { #ifndef _OPENGL_ES_ if (pTexture->GetAutoMipMap()) { #ifdef _MACOSX_ glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE); nuiCheckForGLErrors(); #endif glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, (int)log2(Width)); nuiCheckForGLErrors(); gluBuild2DMipmaps(target, internalPixelformat, (int)Width, (int)Height, pixelformat, type, pBuffer); nuiCheckForGLErrors(); } else #endif { glTexImage2D(target, 0, internalPixelformat, (int)Width, (int)Height, 0, pixelformat, type, pBuffer); } nuiCheckForGLErrors(); } info.mReload = false; if (allocated) free(pBuffer); //#FIXME // if (!pTexture->IsBufferRetained()) { // pTexture->ReleaseBuffer(); // } glTexParameteri(target, GL_TEXTURE_MIN_FILTER, pTexture->GetMinFilter()); nuiCheckForGLErrors(); glTexParameteri(target, GL_TEXTURE_MAG_FILTER, pTexture->GetMagFilter()); nuiCheckForGLErrors(); glTexParameteri(target, GL_TEXTURE_WRAP_S, pTexture->GetWrapS()); nuiCheckForGLErrors(); glTexParameteri(target, GL_TEXTURE_WRAP_T, pTexture->GetWrapT()); nuiCheckForGLErrors(); } } if (pTexture->GetPixelFormat() == eImagePixelAlpha) { glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE); glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_MODULATE); glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, GL_MODULATE); glTexEnvi(GL_TEXTURE_ENV, GL_SRC0_RGB, GL_TEXTURE); glTexEnvi(GL_TEXTURE_ENV, GL_SRC0_ALPHA, GL_TEXTURE); glTexEnvi(GL_TEXTURE_ENV, GL_SRC1_RGB, GL_PRIMARY_COLOR); glTexEnvi(GL_TEXTURE_ENV, GL_SRC1_ALPHA, GL_PRIMARY_COLOR); glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB, GL_SRC_ALPHA); glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA, GL_SRC_ALPHA); glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB, GL_SRC_COLOR); glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_ALPHA, GL_SRC_ALPHA); } else { glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, pTexture->GetEnvMode()); } nuiCheckForGLErrors(); if (changedctx) { mpContext->BeginSession(); nuiCheckForGLErrors(); glBindTexture(target, info.mTexture); nuiCheckForGLErrors(); glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, pTexture->GetEnvMode()); nuiCheckForGLErrors(); } glMatrixMode(GL_TEXTURE); nuiCheckForGLErrors(); glLoadIdentity(); nuiCheckForGLErrors(); uint32 rectangle = GetRectangleTextureSupport(); nuiCheckForGLErrors(); double rx = 1; double ry = 1; if (rectangle != 1) { rx = pTexture->GetUnscaledWidth() / Width; ry = pTexture->GetUnscaledHeight() / Height; #ifndef _OPENGL_ES_ if (target == GL_TEXTURE_RECTANGLE_ARB) { rx *= pTexture->GetUnscaledWidth(); ry *= pTexture->GetUnscaledHeight(); } #endif } if (pSurface) { glTranslatef(0, ry, 0); ry = -ry; } #ifndef _OPENGL_ES_ glScaled(rx, ry, 1); #else glScalef((float)rx, (float)ry, 1); #endif nuiCheckForGLErrors(); glMatrixMode(GL_MODELVIEW); nuiCheckForGLErrors(); }