Skip to content

Commit

Permalink
RenderGraph - Add Unit Test
Browse files Browse the repository at this point in the history
- I never was able to reproduce the issue described by a user.
- I asked them for a repro project, but they moved to a different API (ImportTexture) and are not interested to spend more time on the `ImportBuffer` one.
- They told me they will be able to send us their project in the near future, so we might want to investigate what happened (spending some time to try to reproduce the problem described)
- Meanwhile, I'm pushing this Unit Test, proving the API works well.

This PR is also fixing the Gizmo test, which was failing from time to time on Yamato and 100% of the time on my computer, locally.
  • Loading branch information
YohannVaastUnity authored and Evergreen committed Aug 19, 2024
1 parent b8bb7ca commit 92378cf
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#pragma kernel CSMain

RWStructuredBuffer<float> resultBuffer;

[numthreads(1, 1, 1)]
void CSMain(uint3 id : SV_DispatchThreadID)
{
// Fill the first four values with 1
for (int i = 0; i < 4; i++)
{
resultBuffer[i] = 1.0f;
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
using NUnit.Framework;
using System;
using UnityEngine.Rendering;
using UnityEngine.Experimental.Rendering;
using UnityEngine.Rendering.RenderGraphModule;
using System.Collections.Generic;
using UnityEngine.TestTools;
using System.Collections;
using Unity.Collections;
using System.Linq;
using System.IO;
using System.Drawing.Drawing2D;
using System.Runtime.ConstrainedExecution;

#if UNITY_EDITOR
using UnityEditor;
using UnityEditor.Rendering;
#endif
namespace UnityEngine.Rendering.Tests
Expand Down Expand Up @@ -1191,5 +1186,88 @@ RTHandle CreateRedTexture(int width, int height)
// Cleanup the temporary texture
return redTextureHandle;
}

class TestBuffersImport
{
public BufferHandle bufferHandle;
public ComputeShader computeShader;

}

private const string kPathToComputeShader = "Packages/com.unity.render-pipelines.core/Tests/Editor/BufferCopyTest.compute";

[Test]
public void ImportingBufferWorks()
{
// We need a real ScriptableRenderContext and a camera to execute the render graph
// add the default camera
var gameObject = new GameObject("testGameObject")
{
hideFlags = HideFlags.HideAndDontSave,
tag = "MainCamera"
};
var camera = gameObject.AddComponent<Camera>();
#if UNITY_EDITOR
var computeShader = AssetDatabase.LoadAssetAtPath<ComputeShader>(kPathToComputeShader);
#else
var computeShader = Resources.Load<ComputeShader>("_" + Path.GetFileNameWithoutExtension(kPathToComputeShader));
#endif
// Check if the compute shader was loaded successfully
if (computeShader == null)
{
Debug.LogError("Compute Shader not found!");
return;
}

// Define the size of the buffer (number of elements)
int bufferSize = 4; // We are only interested in the first four values

// Allocate the buffer with the given size and format
var buffer = new GraphicsBuffer(GraphicsBuffer.Target.Structured, bufferSize, sizeof(float));

// Initialize the buffer with zeros
float[] initialData = new float[bufferSize];
buffer.SetData(initialData);

// Ensure the data is set to 0.0f
for (int i = 0; i < bufferSize; i++)
{
Assert.IsTrue(initialData[i] == 0.0f);
}

m_RenderGraphTestPipeline.recordRenderGraphBody = (context, camera, cmd) =>
{
using (var builder = m_RenderGraph.AddComputePass<TestBuffersImport>("TestPass0", out var passData))
{
builder.AllowPassCulling(false);

passData.bufferHandle = m_RenderGraph.ImportBuffer(buffer);

builder.UseBuffer(passData.bufferHandle, AccessFlags.Write);

passData.computeShader = computeShader;

builder.SetRenderFunc((TestBuffersImport data, ComputeGraphContext ctx) =>
{
int kernel = data.computeShader.FindKernel("CSMain");

ctx.cmd.SetComputeBufferParam(data.computeShader, kernel, "resultBuffer", data.bufferHandle);
ctx.cmd.DispatchCompute(data.computeShader, kernel, 1, 1, 1);
});
}
};

camera.Render();

// Read back the data from the buffer
float[] result2 = new float[bufferSize];
buffer.GetData(result2);

// Ensure the data has been updated
for (int i = 0; i < bufferSize; i++)
{
Assert.IsTrue(result2[i] == 1.0f);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,21 @@ public class GizmoRenderingTests
public async Task GizmoRenderingWorks()
{
const string scenePath = "Assets/Scenes/CodeBasedTests/GizmoRendering.unity";
const int numFramesToWarmup = 4;

EditorSceneManager.OpenScene(scenePath);

SceneView sceneView = EditorWindow.CreateWindow<SceneView>();
var sceneView = EditorWindow.CreateWindow<SceneView>();
sceneView.overlayCanvas.overlaysEnabled = false;
sceneView.showGrid = false;

for (int i = 0; i < numFramesToWarmup; i++)
{
await Task.Yield();
}

Texture2D capturedTexture = await EditorWindowCapture.CaptureAsync(sceneView, new SceneViewCaptureSettings(width: 512, height: 512, TimeSpan.Zero, new TimeSpan(0, 0, 0, 1, 0), imageComparisonViewpoint: Camera.main.transform));
var captureSettings = new SceneViewCaptureSettings(512, 512, TimeSpan.Zero, new TimeSpan(0, 0, 0, 1, 0), Camera.main.transform);
var capturedTexture = await EditorWindowCapture.CaptureAsync(sceneView, captureSettings);
sceneView.Close();

ImageComparisonSettings imageComparisonSettings = new()
Expand Down

0 comments on commit 92378cf

Please sign in to comment.