Ejemplo n.º 1
0
        public void CombineTexture(int2 startIndex, int targetSize, bool unloadCheck)
        {
            if (unloadCheck)
            {
                for (int y = 0; y < targetSize; ++y)
                {
                    for (int x = 0; x < targetSize; ++x)
                    {
                        int2 curIdx = startIndex + int2(x, y);
                        UnloadChunk(ref curIdx);
                    }
                }
            }
            int targetElement;

            if (!GetChunk(ref startIndex, targetSize, out targetElement))
            {
                return;
            }
            int3 *vtPtr = (int3 *)vtVariables.Ptr();

            *vtPtr = int3(startIndex, targetSize);
            shader.SetInts(ShaderIDs._VTVariables, vtVariables);
            texSize[0] = indexSize.x;
            texSize[1] = indexSize.y;
            shader.SetInts(ShaderIDs._IndexTextureSize, texSize);
            shader.SetTexture(3, ShaderIDs._IndexTexture, indexTex);
            for (int i = 0; i < allFormats.Length; ++i)
            {
                VirtualTextureFormat fmt     = allFormats[i];
                RenderTexture        blendRT = RenderTexture.GetTemporary(new RenderTextureDescriptor
                {
                    width             = (int)fmt.perElementSize,
                    height            = (int)fmt.perElementSize,
                    volumeDepth       = 1,
                    colorFormat       = fmt.format,
                    dimension         = TextureDimension.Tex2D,
                    enableRandomWrite = true,
                    msaaSamples       = 1
                });
                blendRT.Create();
                shader.SetInt(ShaderIDs._Count, (int)fmt.perElementSize);
                shader.SetTexture(3, ShaderIDs._TextureBuffer, textures[i]);
                shader.SetTexture(3, ShaderIDs._BlendTex, blendRT);
                int disp = Mathf.CeilToInt((int)fmt.perElementSize / 8f);
                shader.Dispatch(3, disp, disp, 1);
                Graphics.Blit(blendRT, textures[i], 0, targetElement);
                RenderTexture.ReleaseTemporary(blendRT);
            }
            vtVariables[0] = startIndex.x;
            vtVariables[1] = startIndex.y;
            vtVariables[2] = targetSize;
            vtVariables[3] = targetElement;
            shader.SetInts(ShaderIDs._VTVariables, vtVariables);
            shader.SetTexture(0, ShaderIDs._IndexTexture, indexTex);
            int dispatchCount = Mathf.CeilToInt(targetSize / 8f);

            shader.Dispatch(0, dispatchCount, dispatchCount, 1);
        }
Ejemplo n.º 2
0
        private void OutputData()
        {
            int len = resolution.x * resolution.y * resolution.z * sizeof(float3x3);

            byte[] allBytes = new byte[len + sizeof(int3)];
            int3 * startPtr = (int3 *)allBytes.Ptr();

            *startPtr = resolution;
            ++startPtr;
            UnsafeUtility.MemCpy(startPtr, finalData.unsafePtr, len);
            System.IO.File.WriteAllBytes("Assets/BinaryData/Irradiance/" + probeName + ".mpipe", allBytes);
        }
    protected override unsafe void OnUpdate()
    {
        EntityQuery          query   = GetEntityQuery(ComponentType.ReadWrite <TrailBufferElement>());
        NativeArray <Entity> entitis = query.ToEntityArray(Allocator.Persistent);
        int segmentCount             = entitis.Length;
        int trailElementCount        = 0;
        BufferFromEntity <TrailBufferElement> buffers = GetBufferFromEntity <TrailBufferElement>();

        for (int i = 0; i < entitis.Length; ++i)
        {
            trailElementCount += buffers[entitis[i]].Length;
        }

        if (!m_TrailElements.IsCreated || m_TrailElements.Length < trailElementCount)
        {
            if (m_TrailElements.IsCreated)
            {
                m_TrailElements.Dispose();
            }
            m_TrailElements = new NativeArray <float3>(CalcWrappingArraySize(trailElementCount), Allocator.Persistent);

            m_TrailElementBufferInShader?.Dispose();
            m_TrailElementBufferInShader = new ComputeBuffer(m_TrailElements.Length, sizeof(TrailBufferElement));
        }

        if (!m_Segments.IsCreated || m_Segments.Length < segmentCount)
        {
            if (m_Segments.IsCreated)
            {
                m_Segments.Dispose();
            }
            m_Segments = new NativeArray <int3>(CalcWrappingArraySize(segmentCount), Allocator.Persistent);

            m_SegmentBufferInShader?.Dispose();
            m_SegmentBufferInShader = new ComputeBuffer(m_Segments.Length, sizeof(TrailBufferElement));
        }

        int     offset           = 0;
        float3 *trailElementsPtr = (float3 *)m_TrailElements.GetUnsafePtr();
        int3 *  segmentsPtr      = (int3 *)m_Segments.GetUnsafePtr();

        for (int i = 0; i < segmentCount; ++i)
        {
            DynamicBuffer <float3> trailbuffer = buffers[entitis[i]].Reinterpret <float3>();
            Entity entity = entitis[i];

            int bufferlength = trailbuffer.Length;

            UnsafeUtility.MemCpy(trailElementsPtr, trailbuffer.GetUnsafePtr(), sizeof(float3) * bufferlength);
            *segmentsPtr = new int3(offset, bufferlength, entity.Index);

            offset += bufferlength;

            segmentsPtr++;
            trailElementsPtr += bufferlength;

            for (int j = 0; j < trailbuffer.Length; ++j)
            {
                if (trailbuffer[j].x == 0.0f && trailbuffer[j].y == 0.0f && trailbuffer[j].z == 0.0f)
                {
                    Debug.Log(entity.Index);
                }
            }
        }

        m_TrailElementBufferInShader.SetData(m_TrailElements);
        m_SegmentBufferInShader.SetData(m_Segments);

        m_MaterialPropertyBlock.SetBuffer("_Positions", m_TrailElementBufferInShader);
        m_MaterialPropertyBlock.SetBuffer("_Segments", m_SegmentBufferInShader);

        m_MeshInstancedArgs.SetData(m_TrailMesh, (uint)segmentCount);

        Graphics.DrawMeshInstancedIndirect(
            m_TrailMesh, 0, m_Material,
            new Bounds(Vector3.zero, Vector3.one * 1000),
            m_MeshInstancedArgs.m_Buffer, 0, m_MaterialPropertyBlock
            );

        entitis.Dispose();
    }
Ejemplo n.º 4
0
        private static unsafe void SubdivideBurst(Allocator allocator, float2 *points, int pointCount, int2 *edges, int edgeCount, float2 *outVertices, int *outIndices, int2 *outEdges, int arrayCount, float areaFactor, float areaThreshold, int refineIterations, int smoothenIterations, int3 *result)
        {
            NativeArray <int2> _edges = new NativeArray <int2>(edgeCount, allocator);

            for (int i = 0; i < _edges.Length; ++i)
            {
                _edges[i] = edges[i];
            }

            NativeArray <float2> _points = new NativeArray <float2>(pointCount, allocator);

            for (int i = 0; i < _points.Length; ++i)
            {
                _points[i] = points[i];
            }

            NativeArray <int>    _outIndices  = new NativeArray <int>(arrayCount, allocator);
            NativeArray <int2>   _outEdges    = new NativeArray <int2>(arrayCount, allocator);
            NativeArray <float2> _outVertices = new NativeArray <float2>(arrayCount, allocator);
            int outEdgeCount   = 0;
            int outIndexCount  = 0;
            int outVertexCount = 0;

            ModuleHandle.Subdivide(allocator, _points, _edges, ref _outVertices, ref outVertexCount, ref _outIndices, ref outIndexCount, ref _outEdges, ref outEdgeCount, areaFactor, areaThreshold, refineIterations, smoothenIterations);

            for (int i = 0; i < outEdgeCount; ++i)
            {
                outEdges[i] = _outEdges[i];
            }
            for (int i = 0; i < outIndexCount; ++i)
            {
                outIndices[i] = _outIndices[i];
            }
            for (int i = 0; i < outVertexCount; ++i)
            {
                outVertices[i] = _outVertices[i];
            }

            result->x = outVertexCount;
            result->y = outIndexCount;
            result->z = outEdgeCount;

            _outVertices.Dispose();
            _outEdges.Dispose();
            _outIndices.Dispose();
            _points.Dispose();
            _edges.Dispose();
        }
Ejemplo n.º 5
0
        private static unsafe void TessellateBurst(Allocator allocator, float2 *points, int pointCount, int2 *edges, int edgeCount, float2 *outVertices, int *outIndices, int2 *outEdges, int arrayCount, int3 *result)
        {
            NativeArray <int2> _edges = new NativeArray <int2>(edgeCount, allocator);

            for (int i = 0; i < _edges.Length; ++i)
            {
                _edges[i] = edges[i];
            }

            NativeArray <float2> _points = new NativeArray <float2>(pointCount, allocator);

            for (int i = 0; i < _points.Length; ++i)
            {
                _points[i] = points[i];
            }

            NativeArray <int>    _outIndices  = new NativeArray <int>(arrayCount, allocator);
            NativeArray <int2>   _outEdges    = new NativeArray <int2>(arrayCount, allocator);
            NativeArray <float2> _outVertices = new NativeArray <float2>(arrayCount, allocator);

            int outEdgeCount   = 0;
            int outIndexCount  = 0;
            int outVertexCount = 0;

            ModuleHandle.Tessellate(allocator, _points, _edges, ref _outVertices, ref outVertexCount, ref _outIndices, ref outIndexCount, ref _outEdges, ref outEdgeCount);

            for (int i = 0; i < outEdgeCount; ++i)
            {
                outEdges[i] = _outEdges[i];
            }
            for (int i = 0; i < outIndexCount; ++i)
            {
                outIndices[i] = _outIndices[i];
            }
            for (int i = 0; i < outVertexCount; ++i)
            {
                outVertices[i] = _outVertices[i];
            }

            result->x = outVertexCount;
            result->y = outIndexCount;
            result->z = outEdgeCount;

            _outVertices.Dispose();
            _outEdges.Dispose();
            _outIndices.Dispose();
            _points.Dispose();
            _edges.Dispose();
        }
Ejemplo n.º 6
0
 private void Awake()
 {
     if (!msb.isCreated)
     {
         msb = new MStringBuilder(30);
     }
     msb.Clear();
     msb.Add("Assets/BinaryData/Irradiance/");
     msb.Add(probeName);
     msb.Add(".mpipe");
     if (!System.IO.File.Exists(msb.str))
     {
         Debug.LogError("Probe: " + probeName + "read Error! ");
         Destroy(gameObject);
         return;
     }
     else
     {
         using (System.IO.FileStream fs = new System.IO.FileStream(msb.str, System.IO.FileMode.Open, System.IO.FileAccess.Read))
         {
             byte[] arr = GetByteArray(fs.Length);
             fs.Read(arr, 0, (int)fs.Length);
             int3 *res = (int3 *)arr.Ptr();
             resolution = *res;
             if (resolution.x * resolution.y * resolution.z * sizeof(float3x3) != fs.Length - sizeof(int3))
             {
                 Debug.LogError("Data size incorrect!");
                 Destroy(gameObject);
                 return;
             }
             NativeArray <float3x3> allDatas = new NativeArray <float3x3>(resolution.x * resolution.y * resolution.z, Allocator.Temp);
             UnsafeUtility.MemCpy(allDatas.GetUnsafePtr(), res + 1, sizeof(float3x3) * allDatas.Length);
             RenderTextureDescriptor desc = new RenderTextureDescriptor
             {
                 colorFormat       = RenderTextureFormat.ARGBHalf,
                 dimension         = TextureDimension.Tex3D,
                 enableRandomWrite = true,
                 width             = resolution.x,
                 height            = resolution.y,
                 volumeDepth       = resolution.z,
                 msaaSamples       = 1
             };
             src0             = new RenderTexture(desc);
             src1             = new RenderTexture(desc);
             desc.colorFormat = RenderTextureFormat.RHalf;
             src2             = new RenderTexture(desc);
             shBuffer         = new ComputeBuffer(allDatas.Length, sizeof(float3x3));
             shBuffer.SetData(allDatas);
             ComputeShader shader = resources.shaders.occlusionProbeCalculate;
             int3 *        arrPtr = (int3 *)resolutionArray.Ptr();
             *arrPtr = resolution;
             shader.SetBuffer(2, "_SHBuffer", shBuffer);
             shader.SetTexture(2, "_Src0", src0);
             shader.SetTexture(2, "_Src1", src1);
             shader.SetTexture(2, "_Src2", src2);
             shader.SetInts("_Resolution", resolutionArray);
             shader.Dispatch(2, Mathf.CeilToInt(resolution.x / 4f), Mathf.CeilToInt(resolution.y / 4f), Mathf.CeilToInt(resolution.z / 4f));
             shBuffer.Dispose();
             allDatas.Dispose();
         }
     }
 }
            public unsafe void Execute(int index)
            {
                int3 gridPosition = VoxelUtil.To3DIndex(index, chunkSize);

                NativeSlice <Voxel> voxels = voxelsWithNeighbor.Slice(neighborHashMap[chunkPosition] * chunkSize.x * chunkSize.y * chunkSize.z, chunkSize.x * chunkSize.y * chunkSize.z);

                Voxel voxel = voxels[index];

                VoxelLight voxelLight = new VoxelLight();

                if (voxel.data == Voxel.VoxelType.Air)
                {
                    lightDatas[index] = voxelLight;
                    return;
                }

                for (int direction = 0; direction < 6; direction++)
                {
                    // Todo : Code Cleanup!!

                    int3 down            = gridPosition;
                    int3 left            = gridPosition;
                    int3 top             = gridPosition;
                    int3 right           = gridPosition;
                    int3 leftDownCorner  = gridPosition;
                    int3 topLeftCorner   = gridPosition;
                    int3 topRightCorner  = gridPosition;
                    int3 rightDownCorner = gridPosition;

                    down[VoxelUtil.DirectionAlignedY[direction]]  -= 1;
                    left[VoxelUtil.DirectionAlignedX[direction]]  -= 1;
                    top[VoxelUtil.DirectionAlignedY[direction]]   += 1;
                    right[VoxelUtil.DirectionAlignedX[direction]] += 1;

                    leftDownCorner[VoxelUtil.DirectionAlignedX[direction]] -= 1;
                    leftDownCorner[VoxelUtil.DirectionAlignedY[direction]] -= 1;

                    topLeftCorner[VoxelUtil.DirectionAlignedX[direction]] -= 1;
                    topLeftCorner[VoxelUtil.DirectionAlignedY[direction]] += 1;

                    topRightCorner[VoxelUtil.DirectionAlignedX[direction]] += 1;
                    topRightCorner[VoxelUtil.DirectionAlignedY[direction]] += 1;

                    rightDownCorner[VoxelUtil.DirectionAlignedX[direction]] += 1;
                    rightDownCorner[VoxelUtil.DirectionAlignedY[direction]] -= 1;

                    int3 *neighbors = stackalloc int3[] { down, leftDownCorner, left, topLeftCorner, top, topRightCorner, right, rightDownCorner };

                    for (int i = 0; i < 8; i++)
                    {
                        neighbors[i][VoxelUtil.DirectionAlignedZ[direction]] += VoxelUtil.DirectionAlignedSign[direction];
                    }

                    for (int i = 0; i < 4; i++)
                    {
                        bool side1  = TransparencyCheck(voxels, neighbors[VoxelUtil.AONeighborOffsets[i * 3]]);
                        bool corner = TransparencyCheck(voxels, neighbors[VoxelUtil.AONeighborOffsets[i * 3 + 1]]);
                        bool side2  = TransparencyCheck(voxels, neighbors[VoxelUtil.AONeighborOffsets[i * 3 + 2]]);

                        if (side1 && side2)
                        {
                            voxelLight.ambient[i + direction * 4] = 0f;
                        }
                        else
                        {
                            voxelLight.ambient[i + direction * 4] = ((side1 ? 0f : 1f) + (side2 ? 0f : 1f) + (corner ? 0f : 1f)) / 3.0f;
                        }
                    }
                }

                lightDatas[index] = voxelLight;
            }
		/// <summary>
		/// Creates a new CudaRegisteredHostMemory_int3 from an existing IntPtr. IntPtr must be page size aligned (4KBytes)!
		/// </summary>
		/// <param name="hostPointer">must be page size aligned (4KBytes)</param>
		/// <param name="size">In elements</param>
		public CudaRegisteredHostMemory_int3(IntPtr hostPointer, SizeT size)
		{
			_intPtr = hostPointer;
			_size = size;
			_typeSize = (SizeT)Marshal.SizeOf(typeof(int3));
			_ptr = (int3*)_intPtr;
		}
            public unsafe void Execute(int index)
            {
                int3 gridPosition = VoxelUtil.To3DIndex(index, chunkSize);

                Voxel voxel = voxels[index];

                VoxelLight voxelLight = new VoxelLight();

                if (voxel.data == Voxel.VoxelType.Air)
                {
                    lightDatas[index] = voxelLight;
                    return;
                }

                for (int direction = 0; direction < 6; direction++)
                {
                    int3 neighborPosition = gridPosition + VoxelMeshBuilder.VoxelDirectionOffsets[direction];
                    if (VoxelMeshBuilder.TransparencyCheck(voxels, neighborPosition, chunkSize))
                    {
                        continue;
                    }

                    // Todo : Code Cleanup!!

                    int3 down            = gridPosition;
                    int3 left            = gridPosition;
                    int3 top             = gridPosition;
                    int3 right           = gridPosition;
                    int3 leftDownCorner  = gridPosition;
                    int3 topLeftCorner   = gridPosition;
                    int3 topRightCorner  = gridPosition;
                    int3 rightDownCorner = gridPosition;

                    down[VoxelMeshBuilder.DirectionAlignedY[direction]]  -= 1;
                    left[VoxelMeshBuilder.DirectionAlignedX[direction]]  -= 1;
                    top[VoxelMeshBuilder.DirectionAlignedY[direction]]   += 1;
                    right[VoxelMeshBuilder.DirectionAlignedX[direction]] += 1;

                    leftDownCorner[VoxelMeshBuilder.DirectionAlignedX[direction]] -= 1;
                    leftDownCorner[VoxelMeshBuilder.DirectionAlignedY[direction]] -= 1;

                    topLeftCorner[VoxelMeshBuilder.DirectionAlignedX[direction]] -= 1;
                    topLeftCorner[VoxelMeshBuilder.DirectionAlignedY[direction]] += 1;

                    topRightCorner[VoxelMeshBuilder.DirectionAlignedX[direction]] += 1;
                    topRightCorner[VoxelMeshBuilder.DirectionAlignedY[direction]] += 1;

                    rightDownCorner[VoxelMeshBuilder.DirectionAlignedX[direction]] += 1;
                    rightDownCorner[VoxelMeshBuilder.DirectionAlignedY[direction]] -= 1;

                    int3 *neighbors = stackalloc int3[] { down, leftDownCorner, left, topLeftCorner, top, topRightCorner, right, rightDownCorner };

                    for (int i = 0; i < 8; i++)
                    {
                        neighbors[i][VoxelMeshBuilder.DirectionAlignedZ[direction]] += (direction % 2 == 0) ? 1 : -1;
                    }

                    for (int i = 0; i < 4; i++)
                    {
                        bool side1  = VoxelMeshBuilder.TransparencyCheck(voxels, neighbors[AONeighborOffsets[i * 3]], chunkSize);
                        bool corner = VoxelMeshBuilder.TransparencyCheck(voxels, neighbors[AONeighborOffsets[i * 3 + 1]], chunkSize);
                        bool side2  = VoxelMeshBuilder.TransparencyCheck(voxels, neighbors[AONeighborOffsets[i * 3 + 2]], chunkSize);

                        if (side1 && side2)
                        {
                            voxelLight.ambient[i + direction * 4] = 0f;
                        }
                        else
                        {
                            voxelLight.ambient[i + direction * 4] = ((side1 ? 0 : 1) + (side2 ? 0 : 1) + (corner ? 0 : 1)) / 3.0f;
                        }
                    }
                }

                lightDatas[index] = voxelLight;
            }