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 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... } }