public unsafe BufferedQuery(VulkanRenderer gd, Device device, PipelineFull pipeline, CounterType type, bool result32Bit) { _api = gd.Api; _device = device; _pipeline = pipeline; _type = type; _result32Bit = result32Bit; _isSupported = QueryTypeSupported(gd, type); if (_isSupported) { QueryPipelineStatisticFlags flags = type == CounterType.PrimitivesGenerated ? QueryPipelineStatisticFlags.QueryPipelineStatisticGeometryShaderPrimitivesBit : 0; var queryPoolCreateInfo = new QueryPoolCreateInfo() { SType = StructureType.QueryPoolCreateInfo, QueryCount = 1, QueryType = GetQueryType(type), PipelineStatistics = flags }; gd.Api.CreateQueryPool(device, queryPoolCreateInfo, null, out _queryPool).ThrowOnError(); } var buffer = gd.BufferManager.Create(gd, sizeof(long), forConditionalRendering: true); _bufferMap = buffer.Map(0, sizeof(long)); _defaultValue = result32Bit ? DefaultValueInt : DefaultValue; Marshal.WriteInt64(_bufferMap, _defaultValue); _buffer = buffer; }
private async void HandleClientTraffic(Stream clientStream, Stream serverStream) { using (var bufferHolder = new BufferHolder(BufferSize)) { while (true) { var bytesRead = await ReadFromStream(clientStream, bufferHolder.Buffer, bufferHolder.RequestedLength); if (bytesRead == 0) { break; } var ok = await WriteToStream(serverStream, bufferHolder.Buffer, bytesRead); if (!ok) { break; } } OnClose(); } }
private void CleanUpAfterWebRequest(BufferHolder bufferHolder) { // Return the buffer to the pool for reuse. idleBuffers.Add(bufferHolder); // If we are running in the editor, we don't have an update loop, so we have to manually // start pending requests here. if (!Application.isPlaying) { StartPendingRequests(); } }
public void TranposeInto_Benchmark() { BufferHolder source = new BufferHolder(); source.Buffer.LoadFrom(Create8x8FloatData()); BufferHolder dest = new BufferHolder(); this.Output.WriteLine($"TranposeInto_PinningImpl_Benchmark X {Times} ..."); Stopwatch sw = Stopwatch.StartNew(); for (int i = 0; i < Times; i++) { source.Buffer.TransposeInto(ref dest.Buffer); } sw.Stop(); this.Output.WriteLine($"TranposeInto_PinningImpl_Benchmark finished in {sw.ElapsedMilliseconds} ms"); }
private void StartPendingRequests() { // Start pending web requests if we have idle buffers. PendingRequest pendingRequest; while (idleBuffers.Count > 0 && pendingRequests.Dequeue(out pendingRequest)) { // Service the request. // Fetch an idle BufferHolder. We will own that BufferHolder for the duration of the coroutine. BufferHolder bufferHolder = idleBuffers[idleBuffers.Count - 1]; // Remove it from the idle list because it's now in use. It will be returned to the pool // by HandleWebRequest, when it's done with it. idleBuffers.RemoveAt(idleBuffers.Count - 1); // Start the coroutine that will handle this web request. When the coroutine is done, // it will return the buffer to the pool. CoroutineRunner.StartCoroutine(this, HandleWebRequest(pendingRequest, bufferHolder)); } }
public unsafe BufferedQuery(VulkanGraphicsDevice gd, Device device, PipelineFull pipeline, CounterType type) { _api = gd.Api; _device = device; _pipeline = pipeline; var queryPoolCreateInfo = new QueryPoolCreateInfo() { SType = StructureType.QueryPoolCreateInfo, QueryCount = 1, QueryType = GetQueryType(type) }; gd.Api.CreateQueryPool(device, queryPoolCreateInfo, null, out _queryPool).ThrowOnError(); var buffer = gd.BufferManager.Create(gd, sizeof(long), forConditionalRendering: true); _bufferMap = buffer.Map(0, sizeof(long)); Marshal.WriteInt64(_bufferMap, -1L); _buffer = buffer; }
/// <summary> /// Co-routine that services one PendingRequest. This method must be called with StartCoroutine. /// </summary> /// <param name="request">The request to service.</param> private IEnumerator HandleWebRequest(PendingRequest request, BufferHolder bufferHolder) { // NOTE: This method runs on the main thread, but never blocks -- the blocking part of the work is // done by yielding the UnityWebRequest, which releases the main thread for other tasks while we // are waiting for the web request to complete (by the miracle of coroutines). // Let the caller create the UnityWebRequest, configuring it as they want. The caller can set the URL, // method, headers, anything they want. The only thing they can't do is call Send(), as we're in charge // of doing that. UnityWebRequest webRequest = request.creationCallback(); PtDebug.LogVerboseFormat("Web request: {0} {1}", webRequest.method, webRequest.url); bool cacheAllowed = cache != null && webRequest.method == "GET" && request.maxAgeMillis != CACHE_NONE; // Check the cache (if it's a GET request and cache is enabled). if (cacheAllowed) { bool cacheHit = false; byte[] cacheData = null; bool cacheReadDone = false; cache.RequestRead(webRequest.url, request.maxAgeMillis, (bool success, byte[] data) => { cacheHit = success; cacheData = data; cacheReadDone = true; }); while (!cacheReadDone) { yield return(null); } if (cacheHit) { PtDebug.LogVerboseFormat("Web request CACHE HIT: {0}, response: {1} bytes", webRequest.url, cacheData.Length); request.completionCallback(PolyStatus.Success(), /* responseCode */ 200, cacheData); // Return the buffer to the pool for reuse. CleanUpAfterWebRequest(bufferHolder); yield break; } else { PtDebug.LogVerboseFormat("Web request CACHE MISS: {0}.", webRequest.url); } } DownloadHandlerBuffer handler = new DownloadHandlerBuffer(); webRequest.downloadHandler = handler; // We need to asset that we actually succeeded in setting the download handler, because this can fail // if, for example, the creation callback mistakenly called Send(). PolyUtils.AssertTrue(webRequest.downloadHandler == handler, "Couldn't set download handler. It's either disposed of, or the creation callback mistakenly called Send()."); // Start the web request. This will suspend this coroutine until the request is done. PtDebug.LogVerboseFormat("Sending web request: {0}", webRequest.url); yield return(UnityCompat.SendWebRequest(webRequest)); // Request is finished. Call user-supplied callback. PtDebug.LogVerboseFormat("Web request finished: {0}, HTTP response code {1}, response: {2}", webRequest.url, webRequest.responseCode, webRequest.downloadHandler.text); PolyStatus status = UnityCompat.IsNetworkError(webRequest) ? PolyStatus.Error(webRequest.error) : PolyStatus.Success(); request.completionCallback(status, (int)webRequest.responseCode, webRequest.downloadHandler.data); // Cache the result, if applicable. if (!UnityCompat.IsNetworkError(webRequest) && cacheAllowed) { byte[] data = webRequest.downloadHandler.data; if (data != null && data.Length > 0) { byte[] copy = new byte[data.Length]; Buffer.BlockCopy(data, 0, copy, 0, data.Length); cache.RequestWrite(webRequest.url, copy); } } // Clean up. webRequest.Dispose(); CleanUpAfterWebRequest(bufferHolder); }
//! Try to serialize all game objects. public void Serialize() { #if HE_LOG_SERIALIZATION Debug.Log("---SERIALIZE---"); #endif // Find if we need to deserialize previous type map. // This could happen when we're in editor and get back from player to // editor. Type map is then set to null, because it isn't deserialized // in Awake() (because it isn't needed there, no need to lower // performance) so we can deserialize it. If reload was in progress, // then it is already loaded DeserializeTypeMapOptional(); // Create new list of buffer m_Buffer = new BufferHolder[m_SerializedObjects.Length]; IFormatter formatter = new BinaryFormatter(); // Traverse each object need to be serialized int i = 0; foreach (var obj in m_SerializedObjects) { MemoryStream stream = new MemoryStream(); SerializeObject(formatter, stream, obj, obj.GetType()); stream.Close(); // Save to solo buffer m_Buffer[i] = new BufferHolder(); m_Buffer[i].m_Buffer = stream.GetBuffer(); ++i; } // Make the copy to deserialized list m_DeserializedObjects = (MonoBehaviour[])m_SerializedObjects.Clone(); }