public void PathCompute() { float startTime = Time.realtimeSinceStartup; unfulfilledData[0] = 0; unfulfilledBuffer.SetData(unfulfilledData); int runsThisPass = 0; do { if (AtoB) { shader.SetBuffer(PathSolverHandle, "pathBufferFrom", pathBufferA); shader.SetBuffer(PathSolverHandle, "pathBufferTo", pathBufferB); } else { shader.SetBuffer(PathSolverHandle, "pathBufferFrom", pathBufferB); shader.SetBuffer(PathSolverHandle, "pathBufferTo", pathBufferA); } shader.Dispatch(PathSolverHandle, flowWidth / 8, flowHeight / 8, 1); currentRuns++; runsThisPass++; AtoB = !AtoB; } while (Time.realtimeSinceStartup - startTime < pathshare && runsThisPass < MaxRunsPerPass); if (currentRuns > runsBeforeCheck) { if (!waitingForRetrieval) { AsyncTextureReader.RequestBufferData(unfulfilledBuffer); waitingForRetrieval = true; } else { AsyncTextureReader.Status status = AsyncTextureReader.RetrieveBufferData(unfulfilledBuffer, unfulfilledData); if (status == AsyncTextureReader.Status.Succeeded) { waitingForRetrieval = false; if (unfulfilledData[0] == 0) { fulfilled = true; } } } } }
// Use this for initialization void Start() { _buffer = new ComputeBuffer(4, sizeof(float)); _buffer.SetData(new float[] { 1, 2, 3, 4 }); AsyncTextureReader.InitDebugLogs(); Pixels = new byte[DebugTexture.width * DebugTexture.height * 4]; Debug.LogFormat("Frame: {0}; Request Status: {1}", Time.frameCount, AsyncTextureReader.RequestTextureData(DebugTexture)); Debug.LogFormat("Frame: {0}; Retrieve Status: {1}", Time.frameCount, AsyncTextureReader.RetrieveTextureData(DebugTexture, Pixels)); #if UNITY_5_5_OR_NEWER AsyncTextureReader.RequestBufferData(_buffer); #endif }
public void Launch(ChunkRequest cr) { free = false; forgetNextResult = false; retrievedData = false; retrievedCount = false; cur = cr; // save current chunk request BlitNoise(); DispatchMC(); #if UNITY_ASYNC appendRequest = AsyncGPUReadback.Request(appendBuffer); argRequest = AsyncGPUReadback.Request(argBuffer); #else // this maybe can fail? should prob check for this case AsyncTextureReader.RequestBufferData(appendBuffer); AsyncTextureReader.RequestBufferData(argBuffer); needToFree = true; #endif }
public void Launch(Texture density) { // can be either Texture3D or RenderTexture Debug.Assert(density.dimension == UnityEngine.Rendering.TextureDimension.Tex3D); // set compute shader references Graphics.ClearRandomWriteTargets(); // not sure if needed anymore appendBuffer.SetCounterValue(0); MarchingCubesCS.SetBuffer(kernelMC, ShaderProps.trianglesRW, appendBuffer); MarchingCubesCS.SetTexture(kernelMC, ShaderProps.densityTexture, density); MarchingCubesCS.Dispatch(kernelMC, resolution / 8, resolution / 8, resolution / 8); argBuffer.SetData(defaultArgs); // copy the counter variables from first buffer into second ComputeBuffer.CopyCount(appendBuffer, argBuffer, 0); AsyncTextureReader.RequestBufferData(appendBuffer); AsyncTextureReader.RequestBufferData(argBuffer); working = true; // launch coroutine to wait and update }
// Update is called once per frame void Update() { if (operating) { if (!fulfilled) { PathCompute(); } else if (!pathreturn) { if (!waitingForRetrieval) { if (AtoB) { AsyncTextureReader.RequestBufferData(pathBufferB); } else { AsyncTextureReader.RequestBufferData(pathBufferA); } waitingForRetrieval = true; } else { AsyncTextureReader.Status status; if (AtoB) { status = AsyncTextureReader.RetrieveBufferData(pathBufferB, pathDataRaw); } else { status = AsyncTextureReader.RetrieveBufferData(pathBufferA, pathDataRaw); } if (status == AsyncTextureReader.Status.Succeeded) { Debug.Log(Time.realtimeSinceStartup - computestarttime); waitingForRetrieval = false; pathreturn = true; for (int i = 0; i < pathData.Length; i++) { pathData[i] = new pathInfo(System.BitConverter.ToUInt32(pathDataRaw, i * 16), System.BitConverter.ToInt32(pathDataRaw, i * 16 + 4), System.BitConverter.ToInt32(pathDataRaw, i * 16 + 8), System.BitConverter.ToUInt32(pathDataRaw, i * 16 + 12)); //debugArray[i].transform.localScale = new Vector3(1.0f, 1.0f, pathData[i].cost / 10.0f); } if (activePath == 1) { activePath = 0; } else { activePath = 1; } shader.SetInt("activePath", activePath); operating = false; //Debug.Log("hey now"); } } } } else { //edit if (edits.Count > 0) { while (edits.Count > 0) { for (int i = 0; i < edits[0].blocks.Count; i++) { if (edits[0].blocks[i].x > 0 && edits[0].blocks[i].x < flowWidth && edits[0].blocks[i].y > 0 && edits[0].blocks[i].y < flowHeight) { difficultyData[edits[0].blocks[i].x + edits[0].blocks[i].y * flowWidth] = edits[0].newVal; obstacles[edits[0].blocks[i].x, edits[0].blocks[i].y].SetActive(edits[0].newVal == 0u); } } edits.RemoveAt(0); } difficultyBuffer.SetData(difficultyData); NewDest(lastTarget.x, lastTarget.y); } } }