Question by
sparkkhuang · Feb 22 at 10:47 AM ·
rendergameplay
unrecognized identifier 'INVALID_UTF8_STRING' at kernel xx?
Why does my code always report such an error under URP?
unrecognized identifier 'INVALID_UTF8_STRING' at kernel xx (:
================================
WaterSplashTest.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class WaterSplashTest : MonoBehaviour
{
private struct Particle
{
public Vector3 position;
public float lifetime;
public float showScale;
}
private struct Result
{
public float ScreenUVx;
public float ScreenUVy;
public Vector3 WorldPos;
public float Deep;
}
private Dictionary<Vector2,GameObject> objects = new Dictionary<Vector2,GameObject>();
public GameObject debugPrefab;
public GameObject prefabRoot;
public RenderTexture DepthRT;
//public RenderTexture DepthRT;
public Camera Camera;
private int particleCount;
public ComputeShader computeShader;
private const int SIZE_PARTICLE = 20;
private const int SIZE_DEBUG = 24;
//kernel ID
private int mComputeShaderKernelID;
//buffer,
ComputeBuffer particleBuffer;
ComputeBuffer resultBuffer;
private Particle[] particleResult;
private Result[] debugResult;
private const int WARP_SIZE = 256;
int width;
int height;
void Start()
{
width = Screen.width;
height = Screen.height;
DepthRT = new RenderTexture(width, height, 0, RenderTextureFormat.RHalf)
{
autoGenerateMips = false,
enableRandomWrite = true,
useMipMap = true,
filterMode = FilterMode.Point
};
DepthRT.Create();
particleCount = width * height / 10 /10 ;
objects = new Dictionary<Vector2, GameObject>();
Debug.LogError(height);
Debug.LogError(width);
//初始化粒子数据
Particle[] particleArray = new Particle[particleCount];
particleResult = new Particle[particleCount];
Result[] resultArray = new Result[particleCount];
debugResult = new Result[particleCount];
int index = 0;
for (int i = 0; i < width; i++)
{
if (i % 10 == 0)
{
for (int j = 0; j < height; j++)
{
if (j % 10 == 0)
{
GameObject go = GameObject.Instantiate(debugPrefab, prefabRoot.transform);
go.transform.position = Vector3.zero;
objects.Add(new Vector2(i, j), go);
particleArray[index].position.x = i;
particleArray[index].position.y = j;
particleArray[index].lifetime = Random.Range(0f, 25f);
index += 1;
}
}
}
}
particleBuffer = new ComputeBuffer(particleCount, SIZE_PARTICLE);
particleBuffer.SetData(particleArray);
resultBuffer = new ComputeBuffer(particleCount, SIZE_DEBUG);
resultBuffer.SetData(resultArray);
mComputeShaderKernelID = computeShader.FindKernel("WaterSplash");
computeShader.SetBuffer(mComputeShaderKernelID, "particleBuffer", particleBuffer);
computeShader.SetBuffer(mComputeShaderKernelID, "resultBuffer", resultBuffer);
computeShader.SetFloat("width", width);
}
void OnDestroy()
{
if (particleBuffer != null)
{
particleBuffer.Release();
particleBuffer.Dispose();
}
if(resultBuffer!= null)
{
resultBuffer.Release();
resultBuffer.Dispose();
}
}
#if UNITY_EDITOR
void Update()
{
#else
void OnPreRender()
{
#endif
DepthMake();
computeShader.SetMatrix("vpMatrixInv", vpMatrixMake());
computeShader.SetTexture(mComputeShaderKernelID, "DeptTex", DepthRT);
computeShader.SetFloat("deltaTime", Time.deltaTime);
computeShader.Dispatch(mComputeShaderKernelID, 8, 8, 1);
particleBuffer.GetData(particleResult);
resultBuffer.GetData(debugResult);
DebugTest();
}
private Matrix4x4 vpMatrixMake()
{
Matrix4x4 projMat = GL.GetGPUProjectionMatrix(Camera.projectionMatrix, false);
var vpMatrix = projMat * Camera.worldToCameraMatrix;
return vpMatrix.inverse;
}
void DebugTest()
{
if(debugResult == null || debugResult.Length == 0 || objects == null || objects.Count == 0)
{
return;
}
GameObject result;
Result temp;
for (int i = 0; i < debugResult.Length; i++)
{
temp = debugResult[i];
objects.TryGetValue(new Vector2(temp.ScreenUVx, temp.ScreenUVy), out result);
if(result == null)
{
continue;
}
result.transform.position = temp.WorldPos;
//result.GetComponent<Material>().color = Color.red * temp.Deep;
}
}
private void DepthMake()
{
RenderTexture tempRT;
tempRT = RenderTexture.GetTemporary(width, height, 0, DepthRT.format);
tempRT.filterMode = FilterMode.Point;
Graphics.Blit(Shader.GetGlobalTexture("_CameraDepthTexture"), tempRT);
Graphics.CopyTexture(tempRT, 0, 0, DepthRT, 0, 0);
}
}
============================= WaterSplash.compute
#pragma kernel WaterSplash
struct Particle
{
float3 position;
float lifetime;
float showScale;
};
struct Result
{
float ScreenUVx;
float ScreenUVy;
float3 WorldPos;
float Deep;
};
RWStructuredBuffer<Particle> particleBuffer;
RWStructuredBuffer<Result> resultBuffer;
RWTexture2D<float4> DeptTex;
float deltaTime;
float width;
float4x4 vpMatrixInv;
[numthreads(8,8,1)]
void WaterSplash (uint3 id : SV_DispatchThreadID)
{
float dept = DeptTex[id.xy].r;
//Reverse_Z
#if defined(UNITY_REVERSED_Z)
dept = 1 - dept;
#endif
float4 ndc = float4(id.x * 2 - 1, id.y * 2 - 1, dept * 2 - 1, 1);
float4 worldPos = mul(vpMatrixInv, ndc);
worldPos /= worldPos.w;
int index = id.x * width + id.y;
resultBuffer[index].WorldPos = float3(1,1,1);
resultBuffer[index].ScreenUVx = id.x;
resultBuffer[index].ScreenUVy = id.y;
resultBuffer[index].Deep = dept;
//float3 delta = float3(0, -2, 0);
particleBuffer[index].position = worldPos;
particleBuffer[index].lifetime += deltaTime;
if (particleBuffer[index].lifetime > 2)
{
particleBuffer[index].lifetime = 0;
}
particleBuffer[index].showScale = dept;
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613053334im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Color Mask / Chroma Key 0 Answers
Universal Render Pipeline 2D lights giving null reference exception 7 Answers
Missing depth output in URP shadergraph graph settings 0 Answers
Ideas for farming? 0 Answers