No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ShaderTemplates.cs 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using System.IO;
  2. using UnityEngine;
  3. using UnityEditor;
  4. using UnityEditor.ProjectWindowCallback;
  5. namespace UnityEditor.Rendering.UnifiedRayTracing
  6. {
  7. internal class ShaderTemplates
  8. {
  9. // TODO: Uncomment when API is made public
  10. //[MenuItem("Assets/Create/Shader/Unified RayTracing Shader", false, 1)]
  11. internal static void CreateNewUnifiedRayTracingShader()
  12. {
  13. var action = ScriptableObject.CreateInstance<DoCreateUnifiedRayTracingShaders>();
  14. ProjectWindowUtil.StartNameEditingIfProjectWindowExists(0, action, "NewRayTracingShader.hlsl", null, null);
  15. }
  16. internal static Object CreateScriptAssetWithContent(string pathName, string templateContent)
  17. {
  18. string fullPath = Path.GetFullPath(pathName);
  19. File.WriteAllText(fullPath, templateContent);
  20. AssetDatabase.ImportAsset(pathName);
  21. return AssetDatabase.LoadAssetAtPath(pathName, typeof(Object));
  22. }
  23. internal class DoCreateUnifiedRayTracingShaders : EndNameEditAction
  24. {
  25. public override void Action(int instanceId, string pathName, string resourceFile)
  26. {
  27. var includeName = Path.GetFileNameWithoutExtension(pathName);
  28. Object o = CreateScriptAssetWithContent(pathName, shaderContent);
  29. CreateScriptAssetWithContent(Path.ChangeExtension(pathName, ".compute"), computeShaderContent.Replace("SHADERNAME", includeName));
  30. CreateScriptAssetWithContent(Path.ChangeExtension(pathName, ".raytrace"), raytracingShaderContent.Replace("SHADERNAME", includeName));
  31. ProjectWindowUtil.ShowCreatedAsset(o);
  32. }
  33. }
  34. const string computeShaderContent =
  35. @"#define UNIFIED_RT_BACKEND_COMPUTE
  36. #define UNIFIED_RT_GROUP_SIZE_X 16
  37. #define UNIFIED_RT_GROUP_SIZE_Y 8
  38. #include ""SHADERNAME.hlsl""
  39. #include_with_pragmas ""Packages/com.unity.rendering.light-transport/Runtime/UnifiedRayTracing/Compute/ComputeRaygenShader.hlsl""
  40. ";
  41. const string raytracingShaderContent =
  42. @"#define UNIFIED_RT_BACKEND_HARDWARE
  43. #include ""SHADERNAME.hlsl""
  44. #include_with_pragmas ""Packages/com.unity.rendering.light-transport/Runtime/UnifiedRayTracing/Hardware/HardwareRaygenShader.hlsl""
  45. ";
  46. const string shaderContent =
  47. @"#include ""Packages/com.unity.rendering.light-transport/Runtime/UnifiedRayTracing/FetchGeometry.hlsl""
  48. #include ""Packages/com.unity.rendering.light-transport/Runtime/UnifiedRayTracing/TraceRay.hlsl""
  49. UNIFIED_RT_DECLARE_ACCEL_STRUCT(_AccelStruct);
  50. void RayGenExecute(UnifiedRT::DispatchInfo dispatchInfo)
  51. {
  52. // Example code:
  53. UnifiedRT::Ray ray;
  54. ray.origin = 0;
  55. ray.direction = float3(0, 0, 1);
  56. ray.tMin = 0;
  57. ray.tMax = 1000.0f;
  58. UnifiedRT::RayTracingAccelStruct accelStruct = UNIFIED_RT_GET_ACCEL_STRUCT(_AccelStruct);
  59. UnifiedRT::Hit hitResult = UnifiedRT::TraceRayClosestHit(dispatchInfo, accelStruct, 0xFFFFFFFF, ray, 0);
  60. if (hitResult.IsValid())
  61. {
  62. UnifiedRT::HitGeomAttributes attributes = UnifiedRT::FetchHitGeomAttributes(accelStruct, hitResult);
  63. }
  64. }
  65. ";
  66. }
  67. }