# Accessing GBuffer from C++

**URL:** <https://forums.unrealengine.com/t/accessing-gbuffer-from-c/414915>\
**Category:** Programming & Scripting\
**Tags:** unreal-engine\
**Created:** [January 18, 2018, 6:09pm UTC](https://forums.unrealengine.com/t/accessing-gbuffer-from-c/414915 "2018-01-18T18:09:11Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jcmack](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@jcmack](https://forums.unrealengine.com/u/jcmack)\
**Post date:** [January 18, 2018, 6:09pm UTC](https://forums.unrealengine.com/t/accessing-gbuffer-from-c/414915/1 "2018-01-18T18:09:11Z")

</div>

For a university project I am working on implementing an RGBD camera as a plugin for Unreal.

Right now the plugin works by using three SceneCaptureComponent2Ds in order to capture a fully rendered image of the scene, depth data and an object mask. I had some issues with SceneCaptureComponents (e.g. the captured image being darker than ingame), so my instructor suggested “skipping the middle man” getting the data directly from the GBuffer.

I found GetGBufferData in DeferredShadingCommon.ush, though I don’t whether it’s possible (and if yes, how) to access that function from within regular C++ code.

I also am aware of the option View Mode → Buffer Visualization Mode menu in the editor and tried to investigate how that is implemented, hoping to find some code to have a look at, but so far I haven’t found anything.

---

<div class="post-metadata">

**Author:** ![Anonymous\_45c7af3adcc306c2cea2bef1bc45ad57](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Anonymous\_45c7af3adcc306c2cea2bef1bc45ad57](https://forums.unrealengine.com/u/Anonymous_45c7af3adcc306c2cea2bef1bc45ad57)\
**Post date:** [February 12, 2019, 5:11pm UTC](https://forums.unrealengine.com/t/accessing-gbuffer-from-c/414915/2 "2019-02-12T17:11:26Z")

</div>

Hello!!!

This is another way than not to use SceneCapu：This is Base On Unreal4.18

Source Code by BaiPaoXiaoDao

```
    	ENQUEUE_UNIQUE_RENDER_COMMAND_ONEPARAMETER(
    		DZRenderSutioBP_InterceptSceneBaseColor,
    		UTexture2D*, vTextureAsset, TextureAsset,
    		{
    			/*if (!IsInRenderingThread())
    			return;*/
    			FRHICommandListImmediate& RHICmdList = GRHICommandList.GetImmediateCommandList();
    			//计数加一避免Render完成后直接清空了GBuffer,但会慢一帧，你猜
    			FSceneRenderTargets::Get(RHICmdList).AdjustGBufferRefCount(RHICmdList, 1);
    			static const FString ScrollingMessage(TEXT("Hello World: "));
    			GEngine->AddOnScreenDebugMessage(-1, 0.2f, FColor::Red, ScrollingMessage);
    			FSceneRenderTargets& SceneContext = FSceneRenderTargets::Get(RHICmdList);
    			if (SceneContext.GBufferA)
    			{
    				FTexture2DRHIRef vTextTarget = SceneContext.GetGBufferATexture();
    				FString vSiceStr = FString::Printf(TEXT("FSceneRenderTargets GBufferA Size = %d*%d"), vTextTarget->GetSizeX(), vTextTarget->GetSizeY());
    
    				GEngine->AddOnScreenDebugMessage(-1, 0.2f, FColor::Red, vSiceStr);
    			}
    			//移除
    			FSceneRenderTargets::Get(RHICmdList).AdjustGBufferRefCount(RHICmdList, -1);
    		}
    	);
```

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_2ee04ade](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_2ee04ade](https://forums.unrealengine.com/u/anonymous_user_2ee04ade)\
**Post date:** [June 4, 2020, 12:42am UTC](https://forums.unrealengine.com/t/accessing-gbuffer-from-c/414915/3 "2020-06-04T00:42:16Z")

</div>

我应该将这段代码添加到哪里？

---

<div class="post-metadata">

**Author:** ![Anonymous\_45c7af3adcc306c2cea2bef1bc45ad57](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Anonymous\_45c7af3adcc306c2cea2bef1bc45ad57](https://forums.unrealengine.com/u/Anonymous_45c7af3adcc306c2cea2bef1bc45ad57)\
**Post date:** [June 4, 2020, 2:24am UTC](https://forums.unrealengine.com/t/accessing-gbuffer-from-c/414915/4 "2020-06-04T02:24:55Z")

</div>

可以添加到扩展于UPrimitiveComponent的组件，收集绘制命令的地方

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_2ee04ade](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_2ee04ade](https://forums.unrealengine.com/u/anonymous_user_2ee04ade)\
**Post date:** [June 4, 2020, 1:44pm UTC](https://forums.unrealengine.com/t/accessing-gbuffer-from-c/414915/5 "2020-06-04T13:44:25Z")

</div>

我可以通过这种方法获取深度的数据吗？因为我就是希望模仿这种方法得到深度数据。

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_2ee04ade](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_2ee04ade](https://forums.unrealengine.com/u/anonymous_user_2ee04ade)\
**Post date:** [June 4, 2020, 1:45pm UTC](https://forums.unrealengine.com/t/accessing-gbuffer-from-c/414915/6 "2020-06-04T13:45:26Z")

</div>

具体是在那个函数里面我还是不清楚

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_2ee04ade](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_2ee04ade](https://forums.unrealengine.com/u/anonymous_user_2ee04ade)\
**Post date:** [June 4, 2020, 1:45pm UTC](https://forums.unrealengine.com/t/accessing-gbuffer-from-c/414915/7 "2020-06-04T13:45:32Z")

</div>

4.25的ENQUEUE\_UNIQUE\_RENDER\_COMMAND\_ONEPARAMETER宏命令好像被弃用了

---

<div class="post-metadata">

**Author:** ![Ekalyks](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/ekalyks/32/725076_2.png) [@Ekalyks](https://forums.unrealengine.com/u/Ekalyks)\
**Post date:** [December 14, 2023, 4:14am UTC](https://forums.unrealengine.com/t/accessing-gbuffer-from-c/414915/8 "2023-12-14T04:14:49Z")

</div>

你好，请问你有这段代码原博客的链接吗？
