/// <summary> /// Завершает инициализацию текущего экземпляра вектора /// </summary> protected override void FinalizeCreation() { GetAsInputArray = new VectorArray(cvInputArrayFromVectorOfInt); GetAsInputOutputArray = new VectorArray(cvInputOutputArrayFromVectorOfInt); GetAsOutputArray = new VectorArray(cvOutputArrayFromVectorOfInt); GetSize = new VectorSize(VectorOfIntGetSize); GetStartAddress = new VectorStartAddress(VectorOfIntGetStartAddress); ClearData = new VectorClear(VectorOfIntClear); Release = new VectorRelease(VectorOfIntRelease); }
public void Get_GetElementAtSpecificIndex_GetsTheElement() { var array = new VectorArray <int>(1000); for (var i = 0; i < 1000; i++) { array.Add(i); } Assert.AreEqual(10, array.Get(10)); }
public void Add_AddThousandIntElements_AddsElements() { var array = new VectorArray <int>(1000); for (var i = 0; i < 1000; i++) { array.Add(i); } Assert.AreEqual(1000, array.Size); }
public void Remove_RemoveElementAtIndex_RemovesTheElement() { var array = new VectorArray <int>(1000); for (var i = 0; i < 1000; i++) { array.Add(i); } var item = array.Remove(75); Assert.AreEqual(999, array.Size); Assert.AreEqual(75, item); }
public void Remove_RemoveElementAtNotExistingIndex_ThrowsException() { var array = new VectorArray <int>(1000); for (var i = 0; i < 1000; i++) { array.Add(i); } Assert.ThrowsException <IndexOutOfRangeException>(() => { array.Remove(1000); }); }
public void Add_AddElementAtSpecificIndex_AddsElement() { var array = new VectorArray <int>(1000); for (var i = 0; i < 1000; i++) { array.Add(i); } array.Add(9999, 10); Assert.AreEqual(1001, array.Size); Assert.AreEqual(9999, array.Get(10)); }
static void Main(string[] args) { IArray <int> single = new SingleArray <int>(); IArray <int> vector = new VectorArray <int>(1000); IArray <int> factor = new FactorArray <int>(); IArray <int> matrix = new MatrixArray <int>(); IArray <int> space = new SpaceArray <int>();; TestPut(single, 1000); TestPut(vector, 1000); TestPut(factor, 1000); TestPut(matrix, 1000); TestPut(space, 1000); Console.ReadLine(); }
protected bool Layout() { VectorArray <CachedEntry> tmp = m_EntryCache.Subrange(0, m_ActiveEntriesCount); tmp.Sort(); float curx = 0, cury = 0, curh = 0, xmax = m_Width, ymax = m_Height; uint curslice = 0; for (uint i = 0; i < m_ActiveEntriesCount; ++i) { // shadow atlas layouting CachedEntry ce = m_EntryCache[i]; Rect vp = ce.current.viewport; if (curx + vp.width > xmax) { curx = 0; cury += curh; } if (curx + vp.width > xmax || cury + curh > ymax) { curslice++; curx = 0; cury = 0; } if (curx + vp.width > xmax || cury + curh > ymax || curslice == m_Slices) { Debug.LogError("ERROR! Shadow atlasing failed."); return(false); } vp.x = curx; vp.y = cury; ce.current.viewport = vp; ce.current.slice = curslice; m_EntryCache[i] = ce; curx += vp.width; curh = curh >= vp.height ? curh : vp.height; } return(true); }
override public bool ReserveFinalize(FrameId frameId, ref VectorArray <ShadowData> entries, ref VectorArray <ShadowPayload> payload) { if (Layout()) { // patch up the shadow data contents with the result of the layouting step for (uint i = 0; i < m_ActiveEntriesCount; ++i) { CachedEntry ce = m_EntryCache[i]; ShadowData sd = entries[ce.key.shadowDataIdx]; // update the shadow data with the actual result of the layouting step sd.scaleOffset = new Vector4(ce.current.viewport.width * m_WidthRcp, ce.current.viewport.height * m_HeightRcp, ce.current.viewport.x * m_WidthRcp, ce.current.viewport.y * m_HeightRcp); sd.PackShadowmapId(m_TexSlot, m_SampSlot, ce.current.slice); // write back the correct results entries[ce.key.shadowDataIdx] = sd; } m_EntryCache.Purge(m_EntryCache.Count() - m_ActiveEntriesCount, (CachedEntry entry) => { Free(entry); }); return(true); } m_ActiveEntriesCount = 0; m_EntryCache.Reset((CachedEntry entry) => { Free(entry); }); return(false); }
abstract public bool Reserve(FrameId frameId, ref ShadowData shadowData, ShadowRequest sr, uint[] widths, uint[] heights, ref VectorArray <ShadowData> entries, ref VectorArray <ShadowPayload> payloads, VisibleLight[] lights);
protected override void PruneShadowCasters(Camera camera, VisibleLight[] lights, ref VectorArray <int> shadowRequests, ref ShadowRequestVector requestsGranted, out uint totalRequestCount) { Debug.Assert(shadowRequests.Count() > 0); // at this point the array is sorted in order of some importance determined by the prioritize function requestsGranted.Reserve(shadowRequests.Count()); totalRequestCount = 0; ShadowmapBase.ShadowRequest sreq = new ShadowmapBase.ShadowRequest(); uint totalSlots = ResetMaxShadows(); // there's a 1:1 mapping between the index in the shadowRequests array and the element in requestsGranted at the same index. // if the prune function skips requests it must make sure that the array is still compact m_TmpSortKeys.Reset(shadowRequests.Count()); for (uint i = 0, count = shadowRequests.Count(); i < count && totalSlots > 0; ++i) { int requestIdx = shadowRequests[i]; VisibleLight vl = lights[requestIdx]; bool add = false; int facecount = 0; GPUShadowType shadowType = GPUShadowType.Point; switch (vl.lightType) { case LightType.Directional: add = m_MaxShadows[(int)GPUShadowType.Directional, 0]-- >= 0; shadowType = GPUShadowType.Directional; facecount = m_ShadowSettings.directionalLightCascadeCount; break; case LightType.Point: add = m_MaxShadows[(int)GPUShadowType.Point, 0]-- >= 0; shadowType = GPUShadowType.Point; facecount = 6; break; case LightType.Spot: add = m_MaxShadows[(int)GPUShadowType.Spot, 0]-- >= 0; shadowType = GPUShadowType.Spot; facecount = 1; break; } if (add) { sreq.instanceId = vl.light.GetInstanceID(); sreq.index = (uint)requestIdx; sreq.facemask = (uint)(1 << facecount) - 1; sreq.shadowType = shadowType; totalRequestCount += (uint)facecount; requestsGranted.AddUnchecked(sreq); totalSlots--; } else { m_TmpSortKeys.AddUnchecked(requestIdx); } } // make sure that shadowRequests contains all light indices that are going to cast a shadow first, then the rest shadowRequests.Reset(); requestsGranted.ExtractTo(ref shadowRequests, (ShadowmapBase.ShadowRequest request) => { return((int)request.index); }); m_TmpSortKeys.ExtractTo(ref shadowRequests, (long idx) => { return((int)idx); }); }
protected bool Alloc(FrameId frameId, Key key, uint width, uint height, out uint cachedEntryIdx, VectorArray <ShadowPayload> payload) { CachedEntry ce = new CachedEntry(); ce.key = key; ce.current.frameId = frameId; ce.current.contentHash = -1; ce.current.slice = 0; ce.current.viewport = new Rect(0, 0, width, height); uint idx; if (m_EntryCache.FindFirst(out idx, ref key, (ref Key k, ref CachedEntry entry) => { return(k.id == entry.key.id && k.faceIdx == entry.key.faceIdx); })) { if (m_EntryCache[idx].current.viewport.width == width && m_EntryCache[idx].current.viewport.height == height) { ce.previous = m_EntryCache[idx].current; m_EntryCache[idx] = ce; cachedEntryIdx = m_ActiveEntriesCount; m_EntryCache.SwapUnchecked(m_ActiveEntriesCount++, idx); return(true); } else { m_EntryCache.SwapUnchecked(idx, m_EntryCache.Count() - 1); m_EntryCache.Purge(1, Free); } } idx = m_EntryCache.Count(); m_EntryCache.Add(ce); cachedEntryIdx = m_ActiveEntriesCount; m_EntryCache.SwapUnchecked(m_ActiveEntriesCount++, idx); return(true); }
override public bool Reserve(FrameId frameId, ref ShadowData shadowData, ShadowRequest sr, uint[] widths, uint[] heights, ref VectorArray <ShadowData> entries, ref VectorArray <ShadowPayload> payload, VisibleLight[] lights) { ShadowData sd = shadowData; ShadowData dummy = new ShadowData(); if (sr.shadowType != GPUShadowType.Point && sr.shadowType != GPUShadowType.Spot && sr.shadowType != GPUShadowType.Directional) { return(false); } if (sr.shadowType == GPUShadowType.Directional) { for (uint i = 0; i < k_MaxCascadesInShader; ++i) { m_TmpSplits[i].Set(0.0f, 0.0f, 0.0f, float.NegativeInfinity); } } Key key; key.id = sr.instanceId; key.faceIdx = 0; key.visibleIdx = (int)sr.index; key.shadowDataIdx = entries.Count(); uint originalEntryCount = entries.Count(); uint originalPayloadCount = payload.Count(); uint originalActiveEntries = m_ActiveEntriesCount; uint facecnt = sr.facecount; uint facemask = sr.facemask; uint bit = 1; int resIdx = 0; entries.Reserve(6); float nearPlaneOffset = QualitySettings.shadowNearPlaneOffset; while (facecnt > 0) { if ((bit & facemask) != 0) { uint width = widths[resIdx]; uint height = heights[resIdx]; uint ceIdx; if (!Alloc(frameId, key, width, height, out ceIdx, payload)) { entries.Purge(entries.Count() - originalEntryCount); payload.Purge(payload.Count() - originalPayloadCount); uint added = m_ActiveEntriesCount - originalActiveEntries; for (uint i = originalActiveEntries; i < m_ActiveEntriesCount; ++i) { m_EntryCache.Swap(i, m_EntryCache.Count() - i - 1); } m_EntryCache.Purge(added, Free); m_ActiveEntriesCount = originalActiveEntries; return(false); } // read CachedEntry ce = m_EntryCache[ceIdx]; // modify Matrix4x4 vp; if (sr.shadowType == GPUShadowType.Point) { vp = ShadowUtils.ExtractPointLightMatrix(lights[sr.index], key.faceIdx, 2.0f, out ce.current.view, out ce.current.proj, out ce.current.lightDir, out ce.current.splitData, m_CullResults, (int)sr.index); } else if (sr.shadowType == GPUShadowType.Spot) { vp = ShadowUtils.ExtractSpotLightMatrix(lights[sr.index], out ce.current.view, out ce.current.proj, out ce.current.lightDir, out ce.current.splitData); } else if (sr.shadowType == GPUShadowType.Directional) { vp = ShadowUtils.ExtractDirectionalLightMatrix(lights[sr.index], key.faceIdx, m_CascadeCount, m_CascadeRatios, nearPlaneOffset, width, height, out ce.current.view, out ce.current.proj, out ce.current.lightDir, out ce.current.splitData, m_CullResults, (int)sr.index); m_TmpSplits[key.faceIdx] = ce.current.splitData.cullingSphere; m_TmpSplits[key.faceIdx].w *= ce.current.splitData.cullingSphere.w; } else { vp = Matrix4x4.identity; // should never happen, though } // write :( m_EntryCache[ceIdx] = ce; sd.worldToShadow = vp.transpose; // apparently we need to transpose matrices that are sent to HLSL sd.scaleOffset = new Vector4(ce.current.viewport.width * m_WidthRcp, ce.current.viewport.height * m_HeightRcp, ce.current.viewport.x, ce.current.viewport.y); sd.texelSizeRcp = new Vector2(m_WidthRcp, m_HeightRcp); sd.PackShadowmapId(m_TexSlot, m_SampSlot, ce.current.slice); sd.shadowType = sr.shadowType; sd.payloadOffset = payload.Count(); entries.AddUnchecked(sd); resIdx++; facecnt--; key.shadowDataIdx++; } else { // we push a dummy face in, otherwise we'd need a mapping from face index to shadowData in the shader as well entries.AddUnchecked(dummy); } key.faceIdx++; bit <<= 1; } if (sr.shadowType == GPUShadowType.Directional) { ShadowPayload sp = new ShadowPayload(); payload.Reserve(k_MaxCascadesInShader); for (uint i = 0; i < k_MaxCascadesInShader; i++) { sp.Set(m_TmpSplits[i]); payload.AddUnchecked(sp); } } return(true); }
public static void Main() { Console.WriteLine( "Creating some objects:" ); Vector a = new Vector(3,4,5); Vector b = new Vector(10,11,12); Console.WriteLine( " Created " + a.print() ); Console.WriteLine( " Created " + b.print() ); // ----- Call an overloaded operator ----- // This calls the wrapper we placed around // // operator+(const Vector &a, const Vector &) // // It returns a new allocated object. Console.WriteLine( "Adding a+b" ); Vector c = example.addv(a,b); Console.WriteLine( " a+b = " + c.print() ); // Note: Unless we free the result, a memory leak will occur if the -noproxy commandline // is used as the proxy classes define finalizers which call the Dispose() method. When // -noproxy is not specified the memory management is controlled by the garbage collector. // You can still call Dispose(). It will free the c++ memory immediately, but not the // C# memory! You then must be careful not to call any member functions as it will // use a NULL c pointer on the underlying c++ object. We set the C# object to null // which will then throw a C# exception should we attempt to use it again. c.Dispose(); c = null; // ----- Create a vector array ----- Console.WriteLine( "Creating an array of vectors" ); VectorArray va = new VectorArray(10); Console.WriteLine( " va = " + va.ToString() ); // ----- Set some values in the array ----- // These operators copy the value of Vector a and Vector b to the vector array va.set(0,a); va.set(1,b); // This works, but it would cause a memory leak if -noproxy was used! va.set(2,example.addv(a,b)); // Get some values from the array Console.WriteLine( "Getting some array values" ); for (int i=0; i<5; i++) Console.WriteLine( " va(" + i + ") = " + va.get(i).print() ); // Watch under resource meter to check on this Console.WriteLine( "Making sure we don't leak memory." ); for (int i=0; i<1000000; i++) c = va.get(i%10); // ----- Clean up ----- // This could be omitted. The garbage collector would then clean up for us. Console.WriteLine( "Cleaning up" ); va.Dispose(); a.Dispose(); b.Dispose(); }
// allocate the shadow requests in the shadow map, only is called if shadowsCount > 0 - may modify shadowRequests and shadowsCount protected abstract void AllocateShadows(FrameId frameId, VisibleLight[] lights, uint totalGranted, ref VectorArray <ShadowmapBase.ShadowRequest> grantedRequests, ref VectorArray <int> shadowIndices, ref VectorArray <ShadowData> shadowmapDatas, ref VectorArray <ShadowPayload> shadowmapPayload);
abstract public bool ReserveFinalize(FrameId frameId, ref VectorArray <ShadowData> entries, ref VectorArray <ShadowPayload> payloads);
internal static HandleRef getCPtr(VectorArray obj) { return (obj == null) ? new HandleRef(null, IntPtr.Zero) : obj.swigCPtr; }
public static void Main() { Console.WriteLine("Creating some objects:"); Vector a = new Vector(3, 4, 5); Vector b = new Vector(10, 11, 12); Console.WriteLine(" Created " + a.print()); Console.WriteLine(" Created " + b.print()); // ----- Call an overloaded operator ----- // This calls the wrapper we placed around // // operator+(const Vector &a, const Vector &) // // It returns a new allocated object. Console.WriteLine("Adding a+b"); Vector c = example.addv(a, b); Console.WriteLine(" a+b = " + c.print()); // Note: Unless we free the result, a memory leak will occur if the -noproxy commandline // is used as the proxy classes define finalizers which call the Dispose() method. When // -noproxy is not specified the memory management is controlled by the garbage collector. // You can still call Dispose(). It will free the c++ memory immediately, but not the // C# memory! You then must be careful not to call any member functions as it will // use a NULL c pointer on the underlying c++ object. We set the C# object to null // which will then throw a C# exception should we attempt to use it again. c.Dispose(); c = null; // ----- Create a vector array ----- Console.WriteLine("Creating an array of vectors"); VectorArray va = new VectorArray(10); Console.WriteLine(" va = " + va.ToString()); // ----- Set some values in the array ----- // These operators copy the value of Vector a and Vector b to the vector array va.set(0, a); va.set(1, b); // This works, but it would cause a memory leak if -noproxy was used! va.set(2, example.addv(a, b)); // Get some values from the array Console.WriteLine("Getting some array values"); for (int i = 0; i < 5; i++) { Console.WriteLine(" va(" + i + ") = " + va.get(i).print()); } // Watch under resource meter to check on this Console.WriteLine("Making sure we don't leak memory."); for (int i = 0; i < 1000000; i++) { c = va.get(i % 10); } // ----- Clean up ----- // This could be omitted. The garbage collector would then clean up for us. Console.WriteLine("Cleaning up"); va.Dispose(); a.Dispose(); b.Dispose(); }
// prune the shadow requests - may modify shadowRequests and shadowsCountshadowRequestsCount protected abstract void PruneShadowCasters(Camera camera, VisibleLight[] lights, ref VectorArray <int> shadowRequests, ref VectorArray <ShadowmapBase.ShadowRequest> requestsGranted, out uint totalRequestCount);
override public bool Reserve(FrameId frameId, ref ShadowData shadowData, ShadowRequest sr, uint width, uint height, ref VectorArray <ShadowData> entries, ref VectorArray <ShadowPayload> payload, VisibleLight[] lights) { for (uint i = 0, cnt = sr.facecount; i < cnt; ++i) { m_TmpWidths[i] = width; m_TmpHeights[i] = height; } return(Reserve(frameId, ref shadowData, sr, m_TmpWidths, m_TmpHeights, ref entries, ref payload, lights)); }
/// <summary> /// Завершает инициализацию текущего экземпляра вектора /// </summary> protected override void FinalizeCreation() { GetAsInputArray = new VectorArray(cvInputArrayFromVectorOfPoint); GetAsInputOutputArray = new VectorArray(cvInputOutputArrayFromVectorOfPoint); GetAsOutputArray = new VectorArray(cvOutputArrayFromVectorOfPoint); GetSize = new VectorSize(VectorOfPointGetSize); GetStartAddress = new VectorStartAddress(VectorOfPointGetStartAddress); ClearData = new VectorClear(VectorOfPointClear); Release = new VectorRelease(VectorOfPointRelease); }
internal static HandleRef getCPtr(VectorArray obj) { return((obj == null) ? new HandleRef(null, IntPtr.Zero) : obj.swigCPtr); }