Skip to content

Commit f5d97c3

Browse files
committed
Surpass Vulkan warning on Unity 6000
1 parent 758bac5 commit f5d97c3

2 files changed

Lines changed: 120 additions & 36 deletions

File tree

Dev/Plugin/Assets/Effekseer/External/HDRP/EffekseerRendererHDRP.cs

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
#if EFFEKSEER_HDRP_SUPPORT
22

3+
using System;
34
using Effekseer.Internal;
45
using UnityEngine;
56
using UnityEngine.Rendering;
67
using UnityEngine.Rendering.HighDefinition;
78

89
namespace Effekseer
910
{
11+
[Serializable]
1012
class EffekseerRenderPassHDRP : UnityEngine.Rendering.HighDefinition.CustomPass
1113
{
1214
Effekseer.Internal.RenderTargetProperty prop = new Internal.RenderTargetProperty();
@@ -25,38 +27,61 @@ protected override void Setup(ScriptableRenderContext renderContext, CommandBuff
2527
base.Setup(renderContext, cmd);
2628
}
2729

28-
void Execute(RTHandle colorBuffer, RTHandle depthBuffer, CommandBuffer cmd, HDCamera hdCamera)
30+
bool TryPrepareRender(RTHandle colorBuffer, RTHandle depthBuffer, HDCamera hdCamera)
2931
{
30-
if (EffekseerSystem.Instance == null) return;
32+
if (hdCamera == null || hdCamera.camera == null || colorBuffer == null || depthBuffer == null)
33+
{
34+
return false;
35+
}
36+
37+
var colorRT = colorBuffer.rt;
38+
if (colorRT == null)
39+
{
40+
return false;
41+
}
42+
3143
prop.colorTargetIdentifier = new RenderTargetIdentifier(colorBuffer);
3244
prop.depthTargetIdentifier = new RenderTargetIdentifier(depthBuffer);
3345
prop.colorTargetRenderTexture = (UnityEngine.RenderTexture)colorBuffer;
3446
prop.depthTargetRenderTexture = depthBuffer;
3547
prop.renderFeature = Effekseer.Internal.RenderFeature.HDRP;
3648

37-
var colorRT = colorBuffer.rt;
38-
3949
// TODO : It needs to support VR and override
4050
prop.ActualScreenSize = new Vector2Int(hdCamera.actualWidth, hdCamera.actualHeight);
4151
prop.Viewport = new Rect(0, 0, hdCamera.camera.pixelRect.width, hdCamera.camera.pixelRect.height);
4252

4353
prop.colorTargetDescriptor = new UnityEngine.RenderTextureDescriptor(colorRT.width, colorRT.height, colorRT.format, 0, colorRT.mipmapCount);
4454
prop.colorTargetDescriptor.msaaSamples = hdCamera.msaaSamples == MSAASamples.None ? 1 : 2;
4555
prop.isRequiredToChangeViewport = true;
56+
return true;
57+
}
58+
59+
void Execute(RTHandle colorBuffer, RTHandle depthBuffer, CommandBuffer cmd, HDCamera hdCamera)
60+
{
61+
if (EffekseerSystem.Instance == null || cmd == null)
62+
{
63+
return;
64+
}
65+
66+
if (!TryPrepareRender(colorBuffer, depthBuffer, hdCamera))
67+
{
68+
return;
69+
}
70+
4671
EffekseerSystem.Instance.renderer.Render(hdCamera.camera, LayerMask.value, prop, cmd, true, blitter);
4772
}
4873

4974
#if UNITY_6000_0_OR_NEWER
5075
protected override void Execute(CustomPassContext ctx)
5176
{
77+
// Unity 6 HDRP executes custom passes through RenderGraph internally.
78+
// Keep the RenderGraph entry point isolated from the legacy path so future
79+
// HDRP changes stay localized in this file.
5280
Execute(ctx.cameraColorBuffer, ctx.cameraDepthBuffer, ctx.cmd, ctx.hdCamera);
53-
base.Execute(ctx);
5481
}
5582
#else
5683
protected override void Execute(ScriptableRenderContext renderContext, CommandBuffer cmd, HDCamera hdCamera, CullingResults cullingResult)
5784
{
58-
if (EffekseerSystem.Instance == null) return;
59-
6085
RTHandle colorBuffer;
6186
RTHandle depthBuffer;
6287
GetCameraBuffers(out colorBuffer, out depthBuffer);

Dev/Plugin/Assets/Effekseer/Scripts/EffekseerRendererUnity.cs

Lines changed: 88 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -412,33 +412,65 @@ struct Vertex
412412
public Color VColor;
413413
}
414414

415-
internal class EffekseerRendererUnity : IEffekseerRenderer
416-
{
417-
const CameraEvent cameraEvent = CameraEvent.AfterForwardAlpha;
418-
private StandardBlitter standardBlitter = new StandardBlitter();
419-
420-
class MaterialPropCollection
415+
internal class EffekseerRendererUnity : IEffekseerRenderer
421416
{
422-
List<MaterialPropertyBlock> materialPropBlocks = new List<MaterialPropertyBlock>();
423-
int materialPropBlockOffset = 0;
417+
const CameraEvent cameraEvent = CameraEvent.AfterForwardAlpha;
418+
private StandardBlitter standardBlitter = new StandardBlitter();
424419

425-
public void Reset()
420+
static bool IsScriptableRenderPipelineActive(RenderPipelineAsset currentRenderPipeline)
426421
{
427-
materialPropBlockOffset = 0;
422+
return currentRenderPipeline != null;
428423
}
429424

430-
public MaterialPropertyBlock GetNext()
425+
static bool RequiresGlobalBufferFallback()
431426
{
432-
if (materialPropBlockOffset >= materialPropBlocks.Count)
427+
#if UNITY_6000_0_OR_NEWER
428+
return SystemInfo.graphicsDeviceType == GraphicsDeviceType.Vulkan &&
429+
IsScriptableRenderPipelineActive(GraphicsSettings.currentRenderPipeline);
430+
#else
431+
return false;
432+
#endif
433+
}
434+
435+
static void SetBufferProperty(CommandBuffer commandBuffer, MaterialPropertyBlock prop, string name, ComputeBuffer buffer)
436+
{
437+
prop.SetBuffer(name, buffer);
438+
439+
// Workaround:
440+
// On Unity 6 SRP rendering with Vulkan, buffers bound only through
441+
// MaterialPropertyBlock can be treated as unbound for DrawProcedural.
442+
// Mirror the binding to a global shader property as well.
443+
// Keep this isolated here because it is likely compensating for a
444+
// Unity-side issue affecting HDRP/URP paths and may be removable once
445+
// Unity fixes it.
446+
if (RequiresGlobalBufferFallback())
433447
{
434-
materialPropBlocks.Add(new MaterialPropertyBlock());
448+
commandBuffer.SetGlobalBuffer(name, buffer);
435449
}
450+
}
451+
452+
class MaterialPropCollection
453+
{
454+
List<MaterialPropertyBlock> materialPropBlocks = new List<MaterialPropertyBlock>();
455+
int materialPropBlockOffset = 0;
456+
457+
public void Reset()
458+
{
459+
materialPropBlockOffset = 0;
460+
}
461+
462+
public MaterialPropertyBlock GetNext()
463+
{
464+
if (materialPropBlockOffset >= materialPropBlocks.Count)
465+
{
466+
materialPropBlocks.Add(new MaterialPropertyBlock());
467+
}
436468

437-
var ret = materialPropBlocks[materialPropBlockOffset];
438-
materialPropBlockOffset++;
439-
return ret;
469+
var ret = materialPropBlocks[materialPropBlockOffset];
470+
materialPropBlockOffset++;
471+
return ret;
472+
}
440473
}
441-
}
442474

443475
[StructLayout(LayoutKind.Sequential)]
444476
struct CustomDataBuffer
@@ -1203,14 +1235,14 @@ unsafe void RenderSprite(Plugin.UnityRenderParameter parameter, IntPtr infoBuffe
12031235

12041236
Debug.Assert(computeBuffer.HasBuffer(parameter.VertexBufferStride));
12051237
var vertexBuffer = computeBuffer.Get(parameter.VertexBufferStride, false);
1206-
if (vertexBuffer == null)
1238+
if (vertexBuffer == null || !vertexBuffer.IsValid())
12071239
{
12081240
Debug.LogWarning("Invalid allocation");
12091241
return;
12101242
}
12111243
Debug.Assert(vertexBuffer.IsValid());
12121244

1213-
prop.SetBuffer("buf_vertex", vertexBuffer);
1245+
SetBufferProperty(commandBuffer, prop, "buf_vertex", vertexBuffer);
12141246

12151247
prop.SetVector("mUVInversed", new Vector4(1.0f, -1.0f, 0.0f, 0.0f));
12161248
prop.SetVector("mUVInversedBack", new Vector4(0.0f, 1.0f, 0.0f, 0.0f));
@@ -1222,14 +1254,14 @@ unsafe void RenderSprite(Plugin.UnityRenderParameter parameter, IntPtr infoBuffe
12221254
if (isAdvanced)
12231255
{
12241256
var bufAd = computeBuffer.Get(sizeof(Effekseer.Plugin.AdvancedVertexParameter), false);
1225-
if (bufAd == null)
1257+
if (bufAd == null || !bufAd.IsValid())
12261258
{
12271259
Debug.LogWarning("Invalid allocation");
12281260
return;
12291261
}
12301262
Debug.Assert(bufAd.IsValid());
12311263

1232-
prop.SetBuffer("buf_ad", bufAd);
1264+
SetBufferProperty(commandBuffer, prop, "buf_ad", bufAd);
12331265
prop.SetFloat("buf_ad_offset", parameter.AdvancedDataOffset / sizeof(Effekseer.Plugin.AdvancedVertexParameter));
12341266

12351267
ApplyAdvancedParameter(parameter, prop);
@@ -1409,17 +1441,44 @@ unsafe void RenderModdel(Plugin.UnityRenderParameter parameter, IntPtr infoBuffe
14091441
ApplyReconstructionParameter(parameter, prop);
14101442
prop.SetVector("softParticleParam", parameter.SoftParticleParam);
14111443

1412-
prop.SetBuffer("buf_model_parameter", computeBuf1);
1444+
if (computeBuf1 == null || !computeBuf1.IsValid())
1445+
{
1446+
Debug.LogWarning("Invalid allocation");
1447+
offset += allocated;
1448+
count -= allocated;
1449+
continue;
1450+
}
1451+
1452+
SetBufferProperty(commandBuffer, prop, "buf_model_parameter", computeBuf1);
14131453

14141454
if (isAdvanced)
14151455
{
1416-
prop.SetBuffer("buf_model_parameter2", computeBuf2);
1456+
if (computeBuf2 == null || !computeBuf2.IsValid())
1457+
{
1458+
Debug.LogWarning("Invalid allocation");
1459+
offset += allocated;
1460+
count -= allocated;
1461+
continue;
1462+
}
1463+
1464+
SetBufferProperty(commandBuffer, prop, "buf_model_parameter2", computeBuf2);
1465+
}
1466+
1467+
if (model.VertexBuffer == null || !model.VertexBuffer.IsValid() ||
1468+
model.IndexBuffer == null || !model.IndexBuffer.IsValid() ||
1469+
model.VertexOffsets == null || !model.VertexOffsets.IsValid() ||
1470+
model.IndexOffsets == null || !model.IndexOffsets.IsValid())
1471+
{
1472+
Debug.LogWarning("Invalid allocation");
1473+
offset += allocated;
1474+
count -= allocated;
1475+
continue;
14171476
}
14181477

1419-
prop.SetBuffer("buf_vertex", model.VertexBuffer);
1420-
prop.SetBuffer("buf_index", model.IndexBuffer);
1421-
prop.SetBuffer("buf_vertex_offsets", model.VertexOffsets);
1422-
prop.SetBuffer("buf_index_offsets", model.IndexOffsets);
1478+
SetBufferProperty(commandBuffer, prop, "buf_vertex", model.VertexBuffer);
1479+
SetBufferProperty(commandBuffer, prop, "buf_index", model.IndexBuffer);
1480+
SetBufferProperty(commandBuffer, prop, "buf_vertex_offsets", model.VertexOffsets);
1481+
SetBufferProperty(commandBuffer, prop, "buf_index_offsets", model.IndexOffsets);
14231482

14241483
prop.SetVector("mUVInversed", new Vector4(1.0f, -1.0f, 0.0f, 0.0f));
14251484
prop.SetVector("mUVInversedBack", new Vector4(0.0f, 1.0f, 0.0f, 0.0f));
@@ -1491,15 +1550,15 @@ unsafe void RenderModdel(Plugin.UnityRenderParameter parameter, IntPtr infoBuffe
14911550
ComputeBuffer cb = null;
14921551
var all = customDataBuffers.Allocate((CustomDataBuffer*)((byte*)infoBuffer.ToPointer() + parameter.CustomData1BufferOffset), offset, count, ref cb);
14931552
if (all != allocated) throw new Exception();
1494-
prop.SetBuffer("buf_customData1", cb);
1553+
SetBufferProperty(commandBuffer, prop, "buf_customData1", cb);
14951554
}
14961555

14971556
if (efkMaterial.asset.CustomData2Count > 0)
14981557
{
14991558
ComputeBuffer cb = null;
15001559
var all = customDataBuffers.Allocate((CustomDataBuffer*)((byte*)infoBuffer.ToPointer() + parameter.CustomData2BufferOffset), offset, count, ref cb);
15011560
if (all != allocated) throw new Exception();
1502-
prop.SetBuffer("buf_customData2", cb);
1561+
SetBufferProperty(commandBuffer, prop, "buf_customData2", cb);
15031562
}
15041563

15051564
if (parameter.IsRefraction > 0 && background != null)

0 commit comments

Comments
 (0)