I’m attempting to test the viability of running a compute-shader inside OnUpdate() inside the ECS Job system. However, I’m not sure when and where the JobComponentSystem object is created so I can’t assign the compute shader below. I’m afraid my only option is to find it at runtime in the OnCreateManager() method. Even still, I don’t think you can access Resources from job’s threads at this time so I’m not quite sure how I’d find the compute-shader using pure scripting.
I thoroughly googled before coming here. I don’t think anybody else has been crazy enough to try something like this yet.
The following code compiles, but of course gives a NullReference Error for the compute Shader when it runs.
using System.Collections;
using Unity.Collections;
using UnityEngine;
using Unity.Entities;
using Unity.Jobs;
using Unity.Mathematics;
using Unity.Transforms;
public class PositionSystem : JobComponentSystem
{
public ComputeShader locGen;
public struct PositionData
{
public readonly ComponentDataArray<Position> Pos;
public readonly int Length;
}
[Unity.Burst.BurstCompile]
public struct PositionJob : IJobProcessComponentData<Position, Rotation>
{
public float deltaTime;
public void Execute(ref Position position, [ReadOnly] ref Rotation rotation)
{
//float3 value = position.Value;
//position.Value = value;
}
}
[Inject] PositionData m_Positions;
protected override JobHandle OnUpdate(JobHandle inputDeps)
{
int kernelHandle = locGen.FindKernel("CSMain");
RenderTexture tex = new RenderTexture(256, 256, 24);
tex.enableRandomWrite = true;
tex.Create();
locGen.SetTexture(kernelHandle, "Result", tex);
locGen.Dispatch(kernelHandle, 256 / 8, 256 / 8, 1);
/*
for (int i = 0; i < m_Positions.Length; i++)
{
//Debug.Log(m_Positions.Pos*.Value);*
}*/
PositionJob posJob = new PositionJob
{
deltaTime = Time.deltaTime
};
JobHandle positionHandle = posJob.Schedule(this, inputDeps);
positionHandle.Complete();
return positionHandle;
}
}
@5argon please halp