I use this code: http://www.cleoag.ru/2013/05/12/directx-texture-hbitmap/ (part 2 + last comment).
Unfortunately, its execution is 170+ ms. Is it possible such a fabulous miracle to speed it up to 10 ms. Need a fundamentally new algorithm?
I use this code: http://www.cleoag.ru/2013/05/12/directx-texture-hbitmap/ (part 2 + last comment).
Unfortunately, its execution is 170+ ms. Is it possible such a fabulous miracle to speed it up to 10 ms. Need a fundamentally new algorithm?
On c # I don’t know how to do it. On c ++ / cli, here is the best thing that was achieved:
System::Drawing::Bitmap ^ ExtractBitmap2(ID3D11Texture2D* d3dtex, ID3D11Device* pDevice) { System::Diagnostics::Stopwatch ^ sw =gcnew System::Diagnostics::Stopwatch(); Int32 t1,t2; HRESULT hr; HBITMAP hBitmapTexture = NULL; HGDIOBJ hBitmap; D3D11_TEXTURE2D_DESC desc; ID3D11Texture2D* pNewTexture = NULL; D3D11_TEXTURE2D_DESC description; sw->Start(); t1=sw->ElapsedMilliseconds; d3dtex->GetDesc(&desc); d3dtex->GetDesc(&description); description.BindFlags = 0; description.CPUAccessFlags = D3D11_CPU_ACCESS_READ | D3D11_CPU_ACCESS_WRITE; description.Usage = D3D11_USAGE_STAGING; description.MiscFlags = 0; if (FAILED(pDevice->CreateTexture2D(&description, NULL, &pNewTexture))) { Console::WriteLine("CreateTexture2D failed!"); return nullptr; } ID3D11DeviceContext* ctx = NULL; pDevice->GetImmediateContext(&ctx); ctx->CopyResource(pNewTexture, d3dtex); D3D11_MAPPED_SUBRESOURCE resource; UINT subresource = D3D11CalcSubresource(0, 0, 0); ctx->Map(pNewTexture, subresource, D3D11_MAP_READ_WRITE, 0, &resource); // Copy from texture to bitmap buffer. uint8_t* sptr = reinterpret_cast<uint8_t*>(resource.pData); uint8_t* dptr = new uint8_t[desc.Width*desc.Height * 4]; UINT* p = NULL; int i; uint32_t x; t2=sw->ElapsedMilliseconds; Console::WriteLine("init time "+Convert::ToString(t2-t1)); t1=sw->ElapsedMilliseconds; for (size_t h = 0; h < desc.Height; ++h) { size_t msize = std::min<size_t>(desc.Width * 4, resource.RowPitch); memcpy_s(dptr, desc.Width * 4, sptr, msize); //// sptr += resource.RowPitch; dptr += desc.Width * 4; } t2=sw->ElapsedMilliseconds; Console::WriteLine("copy time "+Convert::ToString(t2-t1)); dptr -= desc.Width*desc.Height * 4; t1=sw->ElapsedMilliseconds; System::Drawing::Bitmap ^ bmp=gcnew System::Drawing::Bitmap(desc.Width,desc.Height,desc.Width*4, System::Drawing::Imaging::PixelFormat::Format32bppRgb,(IntPtr)dptr); t2=sw->ElapsedMilliseconds; Console::WriteLine("convert time "+Convert::ToString(t2-t1)); return bmp; } Time: 15-17 ms on Intel Core i3
Source: https://ru.stackoverflow.com/questions/718965/
All Articles