graphics - How to read data from a UTexture2D in C++ -
i trying read pixel data populated utexture2d in unreal engine c++ project. before post question here, tried use method described in link: https://answers.unrealengine.com/questions/25594/accessing-pixel-values-of-texture2d.html. however, doesn't work me. pixel values got texture garbage data.
i want depth values scenecapture2d , post-processing material contains scenetexture: depth node. need depth values available in c++ can further processing opencv. in directx11, staging texture can used cpu read, in unreal engine, don't know how create 'staging texture' dx11 has. can't correct pixel values current method makes me think may try access no-cpu-readable texture.
here experimental code reading data rgb utexture2d.
initialize rgb texture:
videotexturecolor= utexture2d::createtransient(640, 480, pf_b8g8r8a8); videotexturecolor->updateresource(); videoupdatetextureregioncolor = new fupdatetextureregion2d(0, 0, 0, 0, 640, 480); colorregiondata = new fupdatetextureregionsdata; pixeldepthdata.init(fcolor(0, 0, 0, 255), 640 * 480); // populate texture blue color (int = 0; < 640; i++) { (int j = 0; j < 480; j++) { int idx = j * 640 + i; pixeldepthdata[idx].b = 255; pixeldepthdata[idx].g = 0; pixeldepthdata[idx].r = 0; pixeldepthdata[idx].a = 255; } } updatetextureregions( videotexturecolor, (int32)0, (uint32)1, videoupdatetextureregioncolor, (uint32)(4 * 640), (uint32)4, (uint8*)pixeldepthdata.getdata(), false, colorregiondata );
then, update read value pixeldepthdata (tarray type) array , update texture values storing in pixeldepthdata, old value.
updatetextureregions( videotexturecolor, (int32)0, (uint32)1, videoupdatetextureregioncolor, (uint32)(4 * 640), (uint32)4, (uint8*)pixeldepthdata.getdata(), false, colorregiondata ); enqueue_unique_render_command_oneparameter( frealsensedelegator, arealsensedelegator*, realsensedelegator, this, { fcolor* tmpimagedataptr = static_cast<fcolor*>((realsensedelegator->videotexturecolor)->platformdata->mips[0].bulkdata.lock(lock_read_only)); (uint32 j = 0; j < 480; j++) { (uint32 = 0; < 640; i++) { uint32 idx = j * 640 + i; realsensedelegator->pixeldepthdata[idx] = tmpimagedataptr[idx]; realsensedelegator->pixeldepthdata[idx].a = 255; } } (realsensedelegator->videotexturecolor)->platformdata->mips[0].bulkdata.unlock(); } );
all got white color texture instead of blue color texture in visualization scene.
does know how read data of utexture2d object?
i figured out. have utexture2d's rhi texture reference first, , use rhilocktexture2d read it's data, , have in renderthread. following code example:
ftexture2dresource* utex2dres = (ftexture2dresource*)(realsensedelegator->videotexturepixeldepth)->resource; float* cpudataptr = (float*)rhilocktexture2d( utex2dres->gettexture2drhi(), 0, rlm_readonly, deststride, false); (uint32 j = 0; j < 480; j++) { (uint32 = 0; < 640; i++) { uint32 idx = j * 640 + i; // todo read pixel data right here } } rhiunlocktexture2d(utex2dres->gettexture2drhi(), 0, false);
to in render thread, have use macro such enqueue_unique_render_command_oneparameter // if 1 pass 1 parameter render thread, use one.+