本篇讨论UnityGraphicsPrograming第一册第五章内容 SPHFluid(粒子法流体)。

通过1-3 Boids和1-4 StableFluid的学习,我们可以总结出一些此类效果的特点。
1.使用了ComputeShader大规模计算
这些计算的输出结果通常是一个很长的数组,每一份数据都对应一个实例、一个像素。
实例:当程序化渲染大量mesh时,决定mesh的位置与速度(Object到World的转换矩阵)
像素:得到的RT可用于后处理、物体表面蒙皮。
将这些计算委托给GPU可以充分利用GPU的并行计算能力,执行逻辑上与CPU无异。

2.使用了Graphics绘制命令
要绘制出图像,离不开draw call,通常是Graphics.Draw...之类的命令。

▲Graphics类
activeColorBuffer:(GetOnly)返回激活中的RT的color buffer部分
activeColorGamut:(GetOnly)返回激活中的色域
activeDepthBuffer:(GetOnly)返回激活中的RT的depth buffer部分
activeTier:设备的图形级别,初始值根据硬件设定。更改此值会影响随后加载的shader。
preserveFramebufferAlpha:(GetOnly)是否保留FrameBuffer的A通道

Blit
source和destination不应相同,可使用临时RT进行周转。
渲染一个刚好覆盖屏幕的长方形(Quad),source经过特定shader处理得到destination。
这通常用于后期处理效果,source会作为_MainTex属性参与shader计算。
当destination为null时,screen backbuffer或相机的target会作为render target(URP未验证)。
Blit方法的render target(color and depth)是destination,SetRenderTarget对其无效;
当希望使用source的depth(stencil) buffer时,可选方案有:
1.使用其他没有隐藏细节的API,完成与Blit等效的过程
2.将source和destination的color区域调换,再交换两者,之后再Blit(一共3次Blit)
3.将source和destination的的depth区域调换,之后再Blit(参考URP,CopyDepthPass)

BlitMultiTap
规则上类似于Blit,但是对_MainTex采样时,根据输入的多个偏移值,多次偏移采样求平均值。
参考URP,MotionBlur相关shader。

ClearRandomWriteTargets
SetRandomWriteTarget之后使用ClearRandomWriteTargets.
random write target可以是ComputeBuffer 或 enableRandomWrite为true的RenderTexture。
Shader Model 4.5+ "unordered access views" (UAV) 使资源可被多线程同时读写。

ConvertTexture 转换format和dimension类型
CopyTexture 复制Texture内容
CreateAsyncGraphicsFence Shortcut for calling Graphics.CreateGraphicsFence with GraphicsFenceType.AsyncQueueSynchronization as the first parameter.
CreateGraphicsFence Creates a GraphicsFence which will be passed after the last Blit, Clear, Draw, Dispatch or Texture Copy command prior to this call has been completed on the GPU.
DrawMesh Draw a mesh.
DrawMeshInstanced Draw the same mesh multiple times using GPU instancing.
DrawMeshInstancedIndirect Draw the same mesh multiple times using GPU instancing.
DrawMeshNow Draw a mesh immediately.
DrawProcedural Draws procedural geometry on the GPU.
DrawProceduralIndirect Draws procedural geometry on the GPU.
DrawProceduralIndirectNow Draws procedural geometry on the GPU.
DrawProceduralNow Draws procedural geometry on the GPU.
DrawTexture Draw a texture in screen coordinates.
ExecuteCommandBuffer Execute a command buffer.
ExecuteCommandBufferAsync Executes a command buffer on an async compute queue with the queue selected based on the ComputeQueueType parameter passed.It is required that all of the commands within the command buffer be of a type suitable for execution on the async compute queues. If the buffer contains any commands that are not appropriate then an error will be logged and displayed in the editor window. Specifically the following commands are permitted in a CommandBuffer intended for async execution:CommandBuffer.BeginSampleCommandBuffer.CopyCounterValueCommandBuffer.CopyTextureCommandBuffer.CreateGPUFenceCommandBuffer.DisableShaderKeywordCommandBuffer.DispatchComputeCommandBuffer.EnableShaderKeywordCommandBuffer.EndSampleCommandBuffer.GetTemporaryRTCommandBuffer.GetTemporaryRTArrayCommandBuffer.IssuePluginEventCommandBuffer.ReleaseTemporaryRTCommandBuffer.SetComputeBufferParamCommandBuffer.SetComputeFloatParamCommandBuffer.SetComputeFloatParamsCommandBuffer.SetComputeIntParamCommandBuffer.SetComputeIntParamsCommandBuffer.SetComputeMatrixArrayParamCommandBuffer.SetComputeMatrixParamCommandBuffer.SetComputeTextureParamCommandBuffer.SetComputeVectorParamCommandBuffer.SetComputeVectorArrayParamCommandBuffer.SetGlobalBufferCommandBuffer.SetGlobalColorCommandBuffer.SetGlobalFloatCommandBuffer.SetGlobalFloatArrayCommandBuffer.SetGlobalIntCommandBuffer.SetGlobalMatrixCommandBuffer.SetGlobalMatrixArrayCommandBuffer.SetGlobalTextureCommandBuffer.SetGlobalVectorCommandBuffer.SetGlobalVectorArrayCommandBuffer.WaitOnGPUFenceAll of the commands within the buffer are guaranteed to be executed on the same queue. If the target platform does not support async compute queues then the work is dispatched on the graphics queue.
SetRandomWriteTarget Set random write target for Shader Model 4.5 level pixel shaders.
SetRenderTarget Sets current render target.
WaitOnAsyncGraphicsFence

GL

SystemInfo.graphicsDeviceVersion
SystemInfo.graphicsDeviceName
SystemInfo.graphicsDeviceVendor


关注成长,注重因果。