Skip to content

Commit caec0f2

Browse files
committed
Support Dynamic resolution in HDRP
1 parent 8cb35d2 commit caec0f2

2 files changed

Lines changed: 150 additions & 106 deletions

File tree

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,12 @@ class EffekseerRenderPassHDRP : UnityEngine.Rendering.HighDefinition.CustomPass
1818

1919
public EffekseerRenderPassHDRP()
2020
{
21-
21+
#if UNITY_6000_0_OR_NEWER
22+
// Effekseer rebinds the camera depth explicitly during execution, so the
23+
// HDRP custom pass itself doesn't need to declare Camera depth here.
24+
targetColorBuffer = TargetBuffer.Camera;
25+
targetDepthBuffer = TargetBuffer.None;
26+
#endif
2227
}
2328

2429
protected override void Setup(ScriptableRenderContext renderContext, CommandBuffer cmd)
@@ -48,7 +53,15 @@ bool TryPrepareRender(RTHandle colorBuffer, RTHandle depthBuffer, HDCamera hdCam
4853

4954
// TODO : It needs to support VR and override
5055
prop.ActualScreenSize = new Vector2Int(hdCamera.actualWidth, hdCamera.actualHeight);
56+
prop.SourceViewport = hdCamera.camera.pixelRect;
57+
#if UNITY_6000_0_OR_NEWER
58+
// HDRP keeps the full-resolution RT allocated and shrinks the active viewport when
59+
// dynamic resolution is enabled, so keep the render viewport and the source
60+
// sampling viewport separate.
61+
prop.Viewport = new Rect(0, 0, hdCamera.actualWidth, hdCamera.actualHeight);
62+
#else
5163
prop.Viewport = new Rect(0, 0, hdCamera.camera.pixelRect.width, hdCamera.camera.pixelRect.height);
64+
#endif
5265

5366
prop.colorTargetDescriptor = new UnityEngine.RenderTextureDescriptor(colorRT.width, colorRT.height, colorRT.format, 0, colorRT.mipmapCount);
5467
prop.colorTargetDescriptor.msaaSamples = hdCamera.msaaSamples == MSAASamples.None ? 1 : 2;

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

Lines changed: 136 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ public enum RenderFeature
6666
PostProcess,
6767
}
6868

69-
public class RenderTargetProperty
70-
{
71-
public RenderFeature renderFeature = RenderFeature.PostProcess;
69+
public class RenderTargetProperty
70+
{
71+
public RenderFeature renderFeature = RenderFeature.PostProcess;
7272

7373
/// <summary>
7474
/// Ring buffer (it should be better implement)
@@ -77,11 +77,12 @@ public class RenderTargetProperty
7777

7878
public int? colorBufferID = null;
7979
public RenderTargetIdentifier colorTargetIdentifier;
80-
public RenderTargetIdentifier? depthTargetIdentifier;
81-
public RenderTextureDescriptor colorTargetDescriptor;
82-
public Vector2? ActualScreenSize;
83-
public Rect? Viewport;
84-
public bool isRequiredToChangeViewport = false;
80+
public RenderTargetIdentifier? depthTargetIdentifier;
81+
public RenderTextureDescriptor colorTargetDescriptor;
82+
public Vector2? ActualScreenSize;
83+
public Rect? Viewport;
84+
public Rect? SourceViewport;
85+
public bool isRequiredToChangeViewport = false;
8586
public RenderTexture colorTargetRenderTexture = null;
8687
public RenderTexture depthTargetRenderTexture = null;
8788
public bool canGrabDepth = false;
@@ -93,18 +94,41 @@ public class RenderTargetProperty
9394
List<Material> blitMaterials = new List<Material>();
9495
List<Material> blitArrayMaterials = new List<Material>();
9596

96-
Material grabDepthMat;
97-
98-
public bool xrRendering = false;
99-
100-
public RenderTargetProperty()
101-
{
102-
}
103-
104-
internal void ApplyToCommandBuffer(CommandBuffer cb, DepthRenderTexture depthRenderTexture, IEffekseerBlitter blitter)
105-
{
106-
if (depthRenderTexture != null)
107-
{
97+
Material grabDepthMat;
98+
99+
public bool xrRendering = false;
100+
101+
public RenderTargetProperty()
102+
{
103+
}
104+
105+
void SetTemporaryRenderTextureViewport(CommandBuffer cb, RenderTexture renderTexture)
106+
{
107+
if (renderTexture != null)
108+
{
109+
cb.SetViewport(new Rect(0, 0, renderTexture.width, renderTexture.height));
110+
}
111+
}
112+
113+
Rect GetSourceViewport()
114+
{
115+
if (SourceViewport.HasValue)
116+
{
117+
return SourceViewport.Value;
118+
}
119+
120+
if (Viewport.HasValue)
121+
{
122+
return Viewport.Value;
123+
}
124+
125+
return new Rect();
126+
}
127+
128+
internal void ApplyToCommandBuffer(CommandBuffer cb, DepthRenderTexture depthRenderTexture, IEffekseerBlitter blitter)
129+
{
130+
if (depthRenderTexture != null)
131+
{
108132
#if UNITY_EDITOR
109133
if (grabDepthMat == null)
110134
{
@@ -133,23 +157,25 @@ internal void ApplyToCommandBuffer(CommandBuffer cb, DepthRenderTexture depthRen
133157
blitter.SetRenderTarget(cb, depthRenderTexture.renderTexture, xrRendering);
134158
cb.ClearRenderTarget(true, true, new Color(0, 0, 0));
135159
}
136-
}
137-
else if (renderFeature == RenderFeature.HDRP)
138-
{
139-
var normalizedArea = new Vector4(
140-
Viewport.Value.width / depthTargetRenderTexture.width,
141-
Viewport.Value.height / depthTargetRenderTexture.height,
142-
Viewport.Value.x / depthTargetRenderTexture.width,
143-
Viewport.Value.y / depthTargetRenderTexture.height);
144-
145-
var m = AllocateBlitArrayMaterial();
146-
m.SetTexture("_BackgroundTex", depthTargetRenderTexture);
147-
m.SetVector("textureArea", normalizedArea);
148-
cb.SetRenderTarget(depthRenderTexture.renderTexture);
149-
cb.ClearRenderTarget(true, true, new Color(0, 0, 0));
150-
cb.Blit(null, depthRenderTexture.renderTexture, m);
151-
}
152-
else
160+
}
161+
else if (renderFeature == RenderFeature.HDRP)
162+
{
163+
var sourceViewport = GetSourceViewport();
164+
var normalizedArea = new Vector4(
165+
sourceViewport.width / depthTargetRenderTexture.width,
166+
sourceViewport.height / depthTargetRenderTexture.height,
167+
sourceViewport.x / depthTargetRenderTexture.width,
168+
sourceViewport.y / depthTargetRenderTexture.height);
169+
170+
var m = AllocateBlitArrayMaterial();
171+
m.SetTexture("_BackgroundTex", depthTargetRenderTexture);
172+
m.SetVector("textureArea", normalizedArea);
173+
cb.SetRenderTarget(depthRenderTexture.renderTexture);
174+
SetTemporaryRenderTextureViewport(cb, depthRenderTexture.renderTexture);
175+
cb.ClearRenderTarget(true, true, new Color(0, 0, 0));
176+
cb.Blit(null, depthRenderTexture.renderTexture, m);
177+
}
178+
else
153179
{
154180
throw new Exception();
155181
}
@@ -159,34 +185,39 @@ internal void ApplyToCommandBuffer(CommandBuffer cb, DepthRenderTexture depthRen
159185
}
160186
}
161187

162-
internal void ApplyToCommandBuffer(CommandBuffer cb, BackgroundRenderTexture backgroundRenderTexture, IEffekseerBlitter blitter)
163-
{
164-
if (isRequiredToChangeViewport)
165-
{
166-
var normalizedArea = new Vector4(
167-
Viewport.Value.width / colorTargetRenderTexture.width,
168-
Viewport.Value.height / colorTargetRenderTexture.height,
169-
Viewport.Value.x / colorTargetRenderTexture.width,
170-
Viewport.Value.y / colorTargetRenderTexture.height);
171-
172-
if (colorTargetRenderTexture.dimension == TextureDimension.Tex2DArray)
173-
{
174-
var m = AllocateBlitArrayMaterial();
175-
m.SetTexture("_BackgroundTex", colorTargetRenderTexture);
176-
m.SetVector("textureArea", normalizedArea);
177-
blitter.SetRenderTarget(cb, backgroundRenderTexture.renderTexture, xrRendering);
178-
cb.ClearRenderTarget(true, true, new Color(0, 0, 0));
179-
blitter.Blit(cb, colorTargetIdentifier, backgroundRenderTexture.renderTexture, m, xrRendering);
180-
}
181-
else
182-
{
183-
var m = AllocateBlitMaterial();
184-
m.SetTexture("_BackgroundTex", colorTargetRenderTexture);
185-
m.SetVector("textureArea", normalizedArea);
186-
blitter.SetRenderTarget(cb, backgroundRenderTexture.renderTexture, xrRendering);
187-
cb.ClearRenderTarget(true, true, new Color(0, 0, 0));
188-
blitter.Blit(cb, colorTargetIdentifier, backgroundRenderTexture.renderTexture, m, xrRendering);
189-
}
188+
internal void ApplyToCommandBuffer(CommandBuffer cb, BackgroundRenderTexture backgroundRenderTexture, IEffekseerBlitter blitter)
189+
{
190+
if (isRequiredToChangeViewport)
191+
{
192+
var sourceViewport = GetSourceViewport();
193+
var normalizedArea = new Vector4(
194+
sourceViewport.width / colorTargetRenderTexture.width,
195+
sourceViewport.height / colorTargetRenderTexture.height,
196+
sourceViewport.x / colorTargetRenderTexture.width,
197+
sourceViewport.y / colorTargetRenderTexture.height);
198+
199+
// Dynamic resolution changes the render viewport, but the source region for the
200+
// background/depth copy still matches the camera's full viewport.
201+
if (colorTargetRenderTexture.dimension == TextureDimension.Tex2DArray)
202+
{
203+
var m = AllocateBlitArrayMaterial();
204+
m.SetTexture("_BackgroundTex", colorTargetRenderTexture);
205+
m.SetVector("textureArea", normalizedArea);
206+
blitter.SetRenderTarget(cb, backgroundRenderTexture.renderTexture, xrRendering);
207+
SetTemporaryRenderTextureViewport(cb, backgroundRenderTexture.renderTexture);
208+
cb.ClearRenderTarget(true, true, new Color(0, 0, 0));
209+
cb.Blit(null, backgroundRenderTexture.renderTexture, m);
210+
}
211+
else
212+
{
213+
var m = AllocateBlitMaterial();
214+
m.SetTexture("_BackgroundTex", colorTargetRenderTexture);
215+
m.SetVector("textureArea", normalizedArea);
216+
blitter.SetRenderTarget(cb, backgroundRenderTexture.renderTexture, xrRendering);
217+
SetTemporaryRenderTextureViewport(cb, backgroundRenderTexture.renderTexture);
218+
cb.ClearRenderTarget(true, true, new Color(0, 0, 0));
219+
cb.Blit(null, backgroundRenderTexture.renderTexture, m);
220+
}
190221
}
191222
else if (isRequiredToCopyBackground)
192223
{
@@ -273,17 +304,17 @@ internal static int ScaledClamp(int value, float scale)
273304
return v;
274305
}
275306

276-
internal static float DistortionBufferScale
277-
{
278-
get
279-
{
280-
return 1.0f;
281-
}
282-
}
283-
284-
internal static bool IsDistortionEnabled
285-
{
286-
get
307+
internal static float DistortionBufferScale
308+
{
309+
get
310+
{
311+
return 1.0f;
312+
}
313+
}
314+
315+
internal static bool IsDistortionEnabled
316+
{
317+
get
287318
{
288319
#if UNITY_IOS || UNITY_ANDROID || UNITY_WEBGL || UNITY_SWITCH
289320
return EffekseerSettings.Instance.enableDistortionMobile;
@@ -313,14 +344,14 @@ internal class BackgroundRenderTexture
313344

314345
public IntPtr ptr = IntPtr.Zero;
315346

316-
public static Vector2Int GetRequiredSize(Camera camera, RenderTargetProperty renderTargetProperty)
317-
{
318-
if (renderTargetProperty != null)
319-
{
320-
var width = renderTargetProperty.colorTargetDescriptor.width;
321-
var height = renderTargetProperty.colorTargetDescriptor.height;
322-
return new Vector2Int(width, height);
323-
}
347+
public static Vector2Int GetRequiredSize(Camera camera, RenderTargetProperty renderTargetProperty)
348+
{
349+
if (renderTargetProperty != null)
350+
{
351+
var width = renderTargetProperty.colorTargetDescriptor.width;
352+
var height = renderTargetProperty.colorTargetDescriptor.height;
353+
return new Vector2Int(width, height);
354+
}
324355

325356
if (camera != null)
326357
{
@@ -332,19 +363,19 @@ public static Vector2Int GetRequiredSize(Camera camera, RenderTargetProperty ren
332363
return new Vector2Int();
333364
}
334365

335-
public BackgroundRenderTexture(int width, int height, int depth, RenderTextureFormat format, RenderTargetProperty renderTargetProperty)
336-
{
337-
if (renderTargetProperty != null)
338-
{
339-
width = renderTargetProperty.colorTargetDescriptor.width;
340-
height = renderTargetProperty.colorTargetDescriptor.height;
341-
}
342-
343-
if (renderTargetProperty != null)
344-
{
345-
renderTexture = new RenderTexture(renderTargetProperty.colorTargetDescriptor);
346-
renderTexture.antiAliasing = 1;
347-
}
366+
public BackgroundRenderTexture(int width, int height, int depth, RenderTextureFormat format, RenderTargetProperty renderTargetProperty)
367+
{
368+
if (renderTargetProperty != null)
369+
{
370+
width = renderTargetProperty.colorTargetDescriptor.width;
371+
height = renderTargetProperty.colorTargetDescriptor.height;
372+
}
373+
374+
if (renderTargetProperty != null)
375+
{
376+
renderTexture = new RenderTexture(renderTargetProperty.colorTargetDescriptor);
377+
renderTexture.antiAliasing = 1;
378+
}
348379
else
349380
{
350381
if (XRSettings.enabled)
@@ -418,13 +449,13 @@ internal class DepthRenderTexture
418449
internal RenderTexture renderTexture;
419450
public IntPtr ptr = IntPtr.Zero;
420451

421-
public DepthRenderTexture(int width, int height, RenderTargetProperty renderTargetProperty)
422-
{
423-
if (renderTargetProperty != null)
424-
{
425-
width = renderTargetProperty.colorTargetDescriptor.width;
426-
height = renderTargetProperty.colorTargetDescriptor.height;
427-
}
452+
public DepthRenderTexture(int width, int height, RenderTargetProperty renderTargetProperty)
453+
{
454+
if (renderTargetProperty != null)
455+
{
456+
width = renderTargetProperty.colorTargetDescriptor.width;
457+
height = renderTargetProperty.colorTargetDescriptor.height;
458+
}
428459

429460
RenderTextureDescriptor desc = new RenderTextureDescriptor(width, height, RenderTextureFormat.RHalf);
430461

@@ -478,4 +509,4 @@ public int height
478509
}
479510
}
480511
}
481-
}
512+
}

0 commit comments

Comments
 (0)