# Lock/Read RHITexture on Vulkan

**URL:** <https://forums.unrealengine.com/t/lock-read-rhitexture-on-vulkan/465529>\
**Category:** Rendering\
**Tags:** rhi, vulkan, unreal-engine, lock, question\
**Created:** [May 20, 2020, 4:35pm UTC](https://forums.unrealengine.com/t/lock-read-rhitexture-on-vulkan/465529 "2020-05-20T16:35:36Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ThD\_ManiaC](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/thd_maniac/32/1194665_2.png) [@ThD\_ManiaC](https://forums.unrealengine.com/u/ThD_ManiaC)\
**Post date:** [May 20, 2020, 4:35pm UTC](https://forums.unrealengine.com/t/lock-read-rhitexture-on-vulkan/465529/1 "2020-05-20T16:35:36Z")

</div>

Hi folks,

in code, i use a **FTexture2DRHIRef** to store the results from a compute-shader. to read the data on the cpu, i use **FRHICommandListImmediate::LockTexture2D( …, EResourceLockMode::RLM\_ReadOnly, … )** which works fine on DirectX12.

On **Vulkan** , this fails and informs that **Reading is not implemented yet** :  
[https://github.com/EpicGames/UnrealEngine/blob/release/Engine/Source/Runtime/VulkanRHI/Private/VulkanTexture.cpp#L1246](https://github.com/EpicGames/UnrealEngine/blob/release/Engine/Source/Runtime/VulkanRHI/Private/VulkanTexture.cpp#L1246)

Is there any **proper way/workaround** to read single-channel texture-values from the gpu on the cpu? or any estimation on the roadmap when this part could be implemented? or any idea how to upgrade this in the engine-code?

FByteBulkData::Lock( LOCK\_READ\_ONLY ) is not suitable since i do not use UTexture.  
FRHICommandListImmediate::ReadSurfaceData( … ) does only work with four-channel textures, but i use a single-channel format (to read and write in the shader simultaniously).

Cheers

---

<div class="post-metadata">

**Author:** ![ThD\_ManiaC](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/thd_maniac/32/1194665_2.png) [@ThD\_ManiaC](https://forums.unrealengine.com/u/ThD_ManiaC)\
**Post date:** [May 22, 2020, 10:11am UTC](https://forums.unrealengine.com/t/lock-read-rhitexture-on-vulkan/465529/2 "2020-05-22T10:11:02Z")

</div>

Looks like **RHICmdList.MapStagingSurface()** does work fine as kind of a workaround. Had to create another RHITexture2D for that, with the **TexCreate\_CPUReadback | TexCreate\_CPUWritable** flags applied. Then you do an additional **RHICmdList.CopyTexture()** and read then the “mapped-surface” on the cpu.

```
int ThreadGroupAmount = FMath::DivideAndRoundUp( GetResolution().X, THREADSPERGROUP );
TShaderMapRef< FOutputShader1D > ComputeShader( GetGlobalShaderMap( GMaxRHIFeatureLevel ) );
FComputeShaderUtils::Dispatch( RHICmdList, ComputeShader, GetIOParameters(), FIntVector( ThreadGroupAmount, 1, 1 ) );
		
RHICmdList.CopyTexture( IORHITextureGPU, IORHITextureCPU, FRHICopyTextureInfo() );

TArray<float> OutputArray;

void* OutputBuffer = NULL;
int32 Width; int32 Height;
RHICmdList.MapStagingSurface( IORHITextureCPU, OutputBuffer, Width, Height );
{
	for( int i = 0; i < GetResolution().X; ++i )
	{
		OutputArray.Add( (float)((uint8*)OutputBuffer)[i] / 255.0f );
	}
}
RHICmdList.UnmapStagingSurface( IORHITextureCPU );

```

Since i only use 1D-Data and i do know the Resolution of ma data, i do not care about the Width and Height of the map. Don’t know how this behaves on other Pixel-Formats.

Also i do not do any WaitForDistpatch(), Flushes, Texture-Transition or so which are usually used in the engine-code on things like that. Still works fine. I would be very happy about some suggestions in this regard 😃
