void WorldView::adjust_render_texture_to_pixel_box_ (Ogre::PixelBox &dst) { size_t width (dst.getWidth()), height (dst.getHeight()); if ((width != texture_-> getWidth()) || (height != texture_-> getHeight())) create_render_texture_ (width, height); }
void WebView::setMask(std::string maskFileName, std::string groupName) { if(usingMask) { if(maskTexUnit) { matPass->removeTextureUnitState(1); maskTexUnit = 0; } if(!TextureManager::getSingleton().getByName(viewName + "MaskTexture").isNull()) TextureManager::getSingleton().remove(viewName + "MaskTexture"); } if(alphaCache) { delete[] alphaCache; alphaCache = 0; } if(maskFileName == "") { usingMask = false; maskImageParameters.first = ""; maskImageParameters.second = ""; if(isWebViewTransparent) { setTransparent(true); update(); } return; } maskImageParameters.first = maskFileName; maskImageParameters.second = groupName; if(!maskTexUnit) { maskTexUnit = matPass->createTextureUnitState(); maskTexUnit->setIsAlpha(true); maskTexUnit->setTextureFiltering(FO_NONE, FO_NONE, FO_NONE); maskTexUnit->setColourOperationEx(LBX_SOURCE1, LBS_CURRENT, LBS_CURRENT); maskTexUnit->setAlphaOperation(LBX_MODULATE); } Image srcImage; srcImage.load(maskFileName, groupName); Ogre::PixelBox srcPixels = srcImage.getPixelBox(); unsigned char* conversionBuf = 0; if(srcImage.getFormat() != Ogre::PF_BYTE_A) { size_t dstBpp = Ogre::PixelUtil::getNumElemBytes(Ogre::PF_BYTE_A); conversionBuf = new unsigned char[srcImage.getWidth() * srcImage.getHeight() * dstBpp]; Ogre::PixelBox convPixels(Ogre::Box(0, 0, srcImage.getWidth(), srcImage.getHeight()), Ogre::PF_BYTE_A, conversionBuf); Ogre::PixelUtil::bulkPixelConversion(srcImage.getPixelBox(), convPixels); srcPixels = convPixels; } TexturePtr maskTexture = TextureManager::getSingleton().createManual( viewName + "MaskTexture", ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, TEX_TYPE_2D, texWidth, texHeight, 0, PF_BYTE_A, TU_STATIC_WRITE_ONLY); HardwarePixelBufferSharedPtr pixelBuffer = maskTexture->getBuffer(); pixelBuffer->lock(HardwareBuffer::HBL_DISCARD); const PixelBox& pixelBox = pixelBuffer->getCurrentLock(); size_t maskTexDepth = Ogre::PixelUtil::getNumElemBytes(pixelBox.format); alphaCachePitch = pixelBox.rowPitch; alphaCache = new unsigned char[alphaCachePitch*texHeight]; uint8* buffer = static_cast<uint8*>(pixelBox.data); memset(buffer, 0, alphaCachePitch * texHeight); size_t minRowSpan = std::min(alphaCachePitch, srcPixels.rowPitch); size_t minHeight = std::min(texHeight, (unsigned short)srcPixels.getHeight()); if(maskTexDepth == 1) { for(unsigned int row = 0; row < minHeight; row++) memcpy(buffer + row * alphaCachePitch, (unsigned char*)srcPixels.data + row * srcPixels.rowPitch, minRowSpan); memcpy(alphaCache, buffer, alphaCachePitch*texHeight); } else if(maskTexDepth == 4) { size_t destRowOffset, srcRowOffset, cacheRowOffset; for(unsigned int row = 0; row < minHeight; row++) { destRowOffset = row * alphaCachePitch * maskTexDepth; srcRowOffset = row * srcPixels.rowPitch; cacheRowOffset = row * alphaCachePitch; for(unsigned int col = 0; col < minRowSpan; col++) alphaCache[cacheRowOffset + col] = buffer[destRowOffset + col * maskTexDepth + 3] = ((unsigned char*)srcPixels.data)[srcRowOffset + col]; } } else { OGRE_EXCEPT(Ogre::Exception::ERR_RT_ASSERTION_FAILED, "Unexpected depth and format were encountered while creating a PF_BYTE_A HardwarePixelBuffer. Pixel format: " + StringConverter::toString((uint32)pixelBox.format) + ", Depth:" + StringConverter::toString(maskTexDepth), "WebView::setMask"); } pixelBuffer->unlock(); if(conversionBuf) delete[] conversionBuf; maskTexUnit->setTextureName(viewName + "MaskTexture"); usingMask = true; }
void Terrain::_createAtlasPixmap(size_t pixmapId) { const TerrainData::Pixmap& pixmap = mData->mPixmaps[pixmapId]; size_t textureId = pixmap.textureId; assert(textureId < mData->mTextures.size()); const Ogre::String& textureName = mData->mTextures[textureId]; // If the atlas texture already exist, use it. AtlasArray::const_iterator it; for (it = mAtlases.begin(); it != mAtlases.end(); ++it) { if (it->texture->getName() == textureName) break; } if (it != mAtlases.end()) { // Fill up atlas pixmap info size_t atlasId = it - mAtlases.begin() + 1; mAtlasPixmaps[pixmapId].atlasId = atlasId; mAtlasPixmaps[pixmapId].left = pixmap.left; mAtlasPixmaps[pixmapId].top = pixmap.top; mAtlasPixmaps[pixmapId].right = pixmap.right; mAtlasPixmaps[pixmapId].bottom = pixmap.bottom; return; } // If texture already loaded and is composited, use it without any modify. Ogre::TexturePtr texture = Ogre::TextureManager::getSingleton().getByName(textureName); if (!texture.isNull() && (texture->getWidth() > mAtlasPixmapSize || texture->getHeight() > mAtlasPixmapSize)) { mAtlases.push_back(Atlas()); Atlas& atlas = mAtlases.back(); atlas.texture = texture; // Fill up atlas pixmap info size_t atlasId = mAtlases.size(); mAtlasPixmaps[pixmapId].atlasId = atlasId; mAtlasPixmaps[pixmapId].left = pixmap.left; mAtlasPixmaps[pixmapId].top = pixmap.top; mAtlasPixmaps[pixmapId].right = pixmap.right; mAtlasPixmaps[pixmapId].bottom = pixmap.bottom; return; } // Load the image Ogre::Image image; image.load(textureName, BRUSH_RESOURCE_GROUP_NAME); // If the image is composited, use it without any modify. if (image.getWidth() > mAtlasPixmapSize || image.getHeight() > mAtlasPixmapSize) { mAtlases.push_back(Atlas()); Atlas& atlas = mAtlases.back(); // re-use the loaded image avoid load it again atlas.texture = Ogre::TextureManager::getSingleton() .loadImage(textureName, BRUSH_RESOURCE_GROUP_NAME, image); // Fill up atlas pixmap info size_t atlasId = mAtlases.size(); mAtlasPixmaps[pixmapId].atlasId = atlasId; mAtlasPixmaps[pixmapId].left = pixmap.left; mAtlasPixmaps[pixmapId].top = pixmap.top; mAtlasPixmaps[pixmapId].right = pixmap.right; mAtlasPixmaps[pixmapId].bottom = pixmap.bottom; return; } // Composite into the atlas texture. bool isTransparent = image.getHasAlpha(); AtlasAllocInfo& allocInfo = isTransparent ? mTransparentAtlasAllocInfo : mSolidAtlasAllocInfo; if (allocInfo.blockId >= mMaxAtlasBlockId) { // Use special name to avoid confuse with other reference with this texture Ogre::String atlasName = "<Terrain/Atlas>:" + Ogre::StringConverter::toString(mAtlases.size()); mAtlases.push_back(Atlas()); Atlas& atlas = mAtlases.back(); Ogre::PixelFormat pixelFormat = isTransparent ? Ogre::PF_A8R8G8B8 : Ogre::PF_X8R8G8B8; atlas.image.bind(new Ogre::Image); atlas.image->loadDynamicImage(0, mAtlasTextureSize, mAtlasTextureSize, 1, pixelFormat, true, 1, mAtlasNumMipMaps); memset(atlas.image->getData(), 0, atlas.image->getSize()); atlas.texture = Ogre::TextureManager::getSingleton().createManual(atlasName, BRUSH_RESOURCE_GROUP_NAME, Ogre::TEX_TYPE_2D, atlas.image->getWidth(), atlas.image->getHeight(), mAtlasNumMipMaps, atlas.image->getFormat(), Ogre::TU_DYNAMIC_WRITE_ONLY_DISCARDABLE, this); allocInfo.atlasId = mAtlases.size(); allocInfo.blockId = 0; } // Copy origin pixmap to atlas image Atlas& atlas = mAtlases[allocInfo.atlasId - 1]; int blockX = allocInfo.blockId % mAtlasBlockSize; int blockY = allocInfo.blockId / mAtlasBlockSize; Ogre::PixelBox dst = atlas.image->getPixelBox().getSubVolume(Ogre::Box( blockX * mAtlasPixmapSize, blockY * mAtlasPixmapSize, blockX * mAtlasPixmapSize + mAtlasPixmapSize, blockY * mAtlasPixmapSize + mAtlasPixmapSize)); Ogre::PixelBox src = image.getPixelBox().getSubVolume(Ogre::Box( fast_ifloor(pixmap.left * image.getWidth() + 0.5f), fast_ifloor(pixmap.top * image.getHeight() + 0.5f), fast_ifloor(pixmap.right * image.getWidth() + 0.5f), fast_ifloor(pixmap.bottom * image.getHeight() + 0.5f))); if (src.getWidth() == mAtlasPixmapSize && src.getHeight() == mAtlasPixmapSize) Ogre::PixelUtil::bulkPixelConversion(src, dst); else Ogre::Image::scale(src, dst); // Generate mipmaps manual for (size_t mipmap = 1; mipmap <= mAtlasNumMipMaps; ++mipmap) { src = dst; size_t pixmapSize = mAtlasPixmapSize >> mipmap; dst = atlas.image->getPixelBox(0, mipmap).getSubVolume(Ogre::Box( blockX * pixmapSize, blockY * pixmapSize, blockX * pixmapSize + pixmapSize, blockY * pixmapSize + pixmapSize)); Ogre::Image::scale(src, dst); } // Notify that the atlas texture need to reload if (atlas.texture->isLoaded()) atlas.texture->unload(); ++allocInfo.blockId; // Fill up atlas pixmap info mAtlasPixmaps[pixmapId].atlasId = allocInfo.atlasId; mAtlasPixmaps[pixmapId].left = blockX * mAtlasBlockTexCoordInc + 0.5f / mAtlasTextureSize; mAtlasPixmaps[pixmapId].top = blockY * mAtlasBlockTexCoordInc + 0.5f / mAtlasTextureSize; mAtlasPixmaps[pixmapId].right = mAtlasPixmaps[pixmapId].left + mAtlasBlockTexCoordInc - 1.0f / mAtlasTextureSize; mAtlasPixmaps[pixmapId].bottom = mAtlasPixmaps[pixmapId].top + mAtlasBlockTexCoordInc - 1.0f / mAtlasTextureSize; }
void GPUSurfFeatureDetector::update(Ogre::PixelBox& frame) { mNbFeatureFound = 0; int width = frame.getWidth(); int height = frame.getHeight(); if (resize(frame.getWidth(), frame.getHeight())) { std::cout << "Warning resizing to " << width << "x" <<height << std::endl; } mWebcamTexture->getBuffer(0, 0)->blitFromMemory(frame); for (int i=0; i<mNbOctave; ++i) { mGPGPURoot->compute(mGrayResults[i], mGrayOperations[i]); mGPGPURoot->compute(mGxResults[i], mGxOperations[i]); mGPGPURoot->compute(mGyResults[i], mGyOperations[i]); mGPGPURoot->compute(mHResults[i], mHOperations[i]); mNMSOperations[i]->setParameter("threshold", (float) mThreshold); mGPGPURoot->compute(mNMSResults[i], mNMSOperations[i]); } /* for (int i=0; i<mNbOctave; ++i) { std::stringstream name; name << "gaussian_" << i<< ".png"; mGrayResults[i]->save(name.str()); } */ glFinish(); mCudaRoot->synchronize(); //std::cout << "0 Cuda Error : " << mCudaRoot->getLastError() <<std::endl; for (int i=0; i<mNbOctave; ++i) { mCudaNMSTexture->map(); mCudaRoot->synchronize(); //std::cout << "1 Cuda Error : " << mCudaRoot->getLastError() <<std::endl; Ogre::Cuda::TextureDeviceHandle textureHandle = mCudaNMSTexture->getDeviceHandle(0, i); mCudaRoot->synchronize(); //std::cout << "2 Cuda Error : " << mCudaRoot->getLastError() <<std::endl; mCudaNMSTexture->updateReading(textureHandle); mCudaRoot->synchronize(); //std::cout << "3 Cuda Error : " << mCudaRoot->getLastError() <<std::endl; mNbFeatureFound += extractFeatureLocationCuda(textureHandle.width, textureHandle.height, textureHandle.getPointer(), mDeviceScanPlan[i], i, mDeviceFeatureCounterPass1[i], mDeviceFeatureCounterPass2[i], mDeviceFeatureFound, mNbFeatureFound); mCudaRoot->synchronize(); mCudaNMSTexture->unmap(); //std::cout << "6 Cuda Error : " << mCudaRoot->getLastError() <<std::endl; //std::cout << "4 Cuda Error : " << mCudaRoot->getLastError() <<std::endl; mCudaRoot->synchronize(); //std::cout << "5 Cuda Error : " << mCudaRoot->getLastError() <<std::endl; } mCudaRoot->synchronize(); if (mNbFeatureFound < mNbFeatureMax) { cudaMemcpy(mHostFeatureFound, mDeviceFeatureFound, mNbFeatureFound*sizeof(Feature), cudaMemcpyDeviceToHost); } else { mNbFeatureFound = 0; //should launch again with a bigger feature buffer... } }