public void CreateAccelerationStructures() { acs = new AccelerationStructures(); long mTlasSize = 0; ID3D12Resource[] mpVertexBuffer = new ID3D12Resource[2]; mpVertexBuffer[0] = acs.CreateTriangleVB(mpDevice); mpVertexBuffer[1] = acs.CreatePlaneVB(mpDevice); // The first bottom-level buffer is for the plane and the triangle int[] vertexCount = new int[] { 3, 6 }; // Triangle has 3 vertices, plane has 6 AccelerationStructureBuffers[] bottomLevelBuffers = new AccelerationStructureBuffers[2]; bottomLevelBuffers[0] = acs.CreateBottomLevelAS(mpDevice, mpCmdList, mpVertexBuffer, vertexCount, 2); mpBottomLevelAS = new ID3D12Resource[2]; mpBottomLevelAS[0] = bottomLevelBuffers[0].pResult; // The second bottom-level buffer is for the triangle only bottomLevelBuffers[1] = acs.CreateBottomLevelAS(mpDevice, mpCmdList, mpVertexBuffer, vertexCount, 1); mpBottomLevelAS[1] = bottomLevelBuffers[1].pResult; // Create the TLAS AccelerationStructureBuffers topLevelBuffers = acs.CreateTopLevelAS(mpDevice, mpCmdList, mpBottomLevelAS, ref mTlasSize); // The tutorial doesn't have any resource lifetime management, so we flush and sync here. This is not required by the DXR spec - you can submit the list whenever you like as long as you take care of the resources lifetime. mFenceValue = context.SubmitCommandList(mpCmdList, mpCmdQueue, mpFence, mFenceValue); mpFence.SetEventOnCompletion(mFenceValue, mFenceEvent); mFenceEvent.WaitOne(); int bufferIndex = mpSwapChain.GetCurrentBackBufferIndex(); mpCmdList.Reset(mFrameObjects[0].pCmdAllocator, null); // Store the AS buffers. The rest of the buffers will be released once we exit the function mpTopLevelAS = topLevelBuffers.pResult; }
private void OnGUI() { if (GUILayout.Button("Start Render")) { PathTracer p = SceneView.lastActiveSceneView.camera.gameObject.GetComponent <PathTracer>(); if (p == null) { p = SceneView.lastActiveSceneView.camera.gameObject.AddComponent <PathTracer>(); p.Setup(SceneView.lastActiveSceneView.camera); } } if (GUILayout.Button("Stop Render")) { PathTracer p = SceneView.lastActiveSceneView.camera.gameObject.GetComponent <PathTracer>(); if (p != null) { p.Dispose(); Object.DestroyImmediate(p); } } EditorGUILayout.Space(); if (GUILayout.Button("Debug Uniform Grid")) { GameObject existing_grid = GameObject.Find("debug_grid"); if (existing_grid != null) { Object.DestroyImmediate(existing_grid.gameObject); } AccelerationStructures.BuildUniformGridGPU(); GameObject grid = GameObject.CreatePrimitive(PrimitiveType.Cube); grid.name = "debug_grid"; grid.transform.position = AccelerationStructures.SceneBounds.center; grid.transform.localScale = AccelerationStructures.SceneBounds.size; Material grid_material = new Material(Shader.Find("PathTracing/UniformGridDebug")); grid_material.SetBuffer("grid_data", AccelerationStructures.GridData); grid_material.SetVector("grid_origin", AccelerationStructures.GridInfo.grid_origin); grid_material.SetVector("grid_size", AccelerationStructures.GridInfo.grid_size); grid_material.SetInt("num_cells_x", (int)AccelerationStructures.GridInfo.nx); grid_material.SetInt("num_cells_y", (int)AccelerationStructures.GridInfo.ny); grid_material.SetInt("num_cells_z", (int)AccelerationStructures.GridInfo.nz); grid.GetComponent <MeshRenderer>().material = grid_material; } }
public void CreateAccelerationStructures() { acs = new AccelerationStructures(); AccelerationStructureBuffers bottomLevelBuffers = acs.CreateBottomLevelAS(mpDevice, mpCmdList); AccelerationStructureBuffers topLevelBuffers = acs.CreateTopLevelAS(mpDevice, mpCmdList, bottomLevelBuffers.pResult, ref mTlasSize); // The tutorial doesn't have any resource lifetime management, so we flush and sync here. This is not required by the DXR spec - you can submit the list whenever you like as long as you take care of the resources lifetime. mFenceValue = context.SubmitCommandList(mpCmdList, mpCmdQueue, mpFence, mFenceValue); mpFence.SetEventOnCompletion(mFenceValue, mFenceEvent); mFenceEvent.WaitOne(); int bufferIndex = mpSwapChain.GetCurrentBackBufferIndex(); mpCmdList.Reset(mFrameObjects[0].pCmdAllocator, null); // Store the AS buffers. The rest of the buffers will be released once we exit the function mpTopLevelAS = topLevelBuffers.pResult; mpBottomLevelAS = bottomLevelBuffers.pResult; }
//PUBLIC METHODS public void Setup(Camera cam) { //I must call this function every time the viewport is resized, VERY IMPORTANT scene_view_camera = cam; path_tracing_CS = Resources.Load <ComputeShader>("PathTracingCS"); path_tracing_kernel = path_tracing_CS.FindKernel("PathTrace_uniform_grid"); path_tracing_CS.SetVector("screen_size", new Vector4(cam.pixelRect.width, cam.pixelRect.height, 0, 0)); groups_x = Mathf.CeilToInt(cam.pixelRect.width / 8.0f); groups_y = Mathf.CeilToInt(cam.pixelRect.height / 8.0f); tonemap_blit = new Material(Shader.Find("PathTracing/Tonemap")); hdr_rt = new RenderTexture((int)cam.pixelRect.width, (int)cam.pixelRect.height, 0, RenderTextureFormat.ARGBFloat, RenderTextureReadWrite.Linear); hdr_rt.enableRandomWrite = true; hdr_rt.Create(); AccelerationStructures.BuildUniformGridGPU(); SetUniformGrid(); }