| 151 | } |
| 152 | |
| 153 | bool GFXGLTextureObject::copyToBmp(GBitmap * bmp) |
| 154 | { |
| 155 | if (!bmp) |
| 156 | return false; |
| 157 | |
| 158 | // check format limitations |
| 159 | // at the moment we only support RGBA for the source (other 4 byte formats should |
| 160 | // be easy to add though) |
| 161 | AssertFatal(mFormat == GFXFormatR16G16B16A16F || mFormat == GFXFormatR8G8B8A8 || mFormat == GFXFormatR8G8B8A8_LINEAR_FORCE || mFormat == GFXFormatR8G8B8A8_SRGB, "copyToBmp: invalid format"); |
| 162 | if (mFormat != GFXFormatR16G16B16A16F && mFormat != GFXFormatR8G8B8A8 && mFormat != GFXFormatR8G8B8A8_LINEAR_FORCE && mFormat != GFXFormatR8G8B8A8_SRGB) |
| 163 | return false; |
| 164 | |
| 165 | AssertFatal(bmp->getWidth() == getWidth(), avar("GFXGLTextureObject::copyToBmp - Width mismatch: %i vs %i", bmp->getWidth(), getWidth())); |
| 166 | AssertFatal(bmp->getHeight() == getHeight(), avar("GFXGLTextureObject::copyToBmp - Height mismatch: %i vs %i", bmp->getHeight(), getHeight())); |
| 167 | |
| 168 | PROFILE_SCOPE(GFXGLTextureObject_copyToBmp); |
| 169 | |
| 170 | PRESERVE_TEXTURE(mBinding); |
| 171 | glBindTexture(mBinding, mHandle); |
| 172 | |
| 173 | U8 dstBytesPerPixel = GFXFormat_getByteSize( bmp->getFormat() ); |
| 174 | U8 srcBytesPerPixel = GFXFormat_getByteSize( mFormat ); |
| 175 | |
| 176 | FrameAllocatorMarker mem; |
| 177 | |
| 178 | |
| 179 | U32 mipLevels = getMipLevels(); |
| 180 | for (U32 mip = 0; mip < mipLevels; mip++) |
| 181 | { |
| 182 | U32 srcPixelCount = bmp->getSurfaceSize(mip)/ srcBytesPerPixel; |
| 183 | |
| 184 | U8* dest = bmp->getWritableBits(mip); |
| 185 | U8* orig = (U8*)mem.alloc(srcPixelCount * srcBytesPerPixel); |
| 186 | |
| 187 | glGetTexImage(mBinding, mip, GFXGLTextureFormat[mFormat], GFXGLTextureType[mFormat], orig); |
| 188 | if (mFormat == GFXFormatR16G16B16A16F) |
| 189 | { |
| 190 | dMemcpy(dest, orig, srcPixelCount * srcBytesPerPixel); |
| 191 | } |
| 192 | else |
| 193 | { |
| 194 | for (int i = 0; i < srcPixelCount; ++i) |
| 195 | { |
| 196 | dest[0] = orig[0]; |
| 197 | dest[1] = orig[1]; |
| 198 | dest[2] = orig[2]; |
| 199 | if (dstBytesPerPixel == 4) |
| 200 | dest[3] = orig[3]; |
| 201 | |
| 202 | orig += srcBytesPerPixel; |
| 203 | dest += dstBytesPerPixel; |
| 204 | } |
| 205 | } |
| 206 | } |
| 207 | glBindTexture(mBinding, NULL); |
| 208 | |
| 209 | return true; |
| 210 | } |
no test coverage detected