Exemple #1
0
    void Awake()
    {
        var mr = GetComponent <MeshRenderer>();

        mr.shadowCastingMode = ShadowCastingMode.Off;
        mr.receiveShadows    = false;

        meshFilter = GetComponent <MeshFilter>();
        mesh       = new Mesh();

        mesh.MarkDynamic();

        meshFilter.mesh = mesh;


        int[] trisSample;
        Init(out size, out trisSample);

        arrayPool = new TrisArrayPool(trisSample, size);
        arrayPool.GetArray(200);
    }
        private async Task QueryCore(
            bool async,
            string queryText,
            PostgresCommand command,
            CancellationToken cancellationToken)
        {
            BindParameter[] parameters     = null;
            var             parameterCount = command.Parameters.Count;

            try
            {
                if (parameterCount > 0)
                {
                    parameters = ArrayPool <BindParameter>
                                 .GetArray(parameterCount);

                    var encoding = ClientState.ClientEncoding;

                    for (var i = 0; i < parameterCount; ++i)
                    {
                        var param = command.Parameters[i].Value;

                        if (param == null)
                        {
                            parameters[i] = new BindParameter {
                                ParameterByteCount = 0,
                                Parameters         = EmptyArray <byte> .Value
                            };

                            continue;
                        }

                        // TODO: This allocation fest is terrible. Make this
                        // write directly to the memorystream instead of having
                        // intermittent buffers for everything.

                        var paramString = param.ToString();

                        var maxBytes = encoding
                                       .GetMaxByteCount(paramString.Length);

                        var paramBuffer = ArrayPool <byte> .GetArray(maxBytes);

                        var actualBytes = encoding.GetBytes(
                            paramString, 0, paramString.Length,
                            paramBuffer, 0);

                        parameters[i] = new BindParameter {
                            ParameterByteCount = actualBytes,
                            Parameters         = paramBuffer
                        };
                    }
                }

                WriteMessage(new ParseMessage {
                    Query = queryText
                });

                WriteMessage(new BindMessage {
                    PreparedStatementName       = "",
                    ResultColumnFormatCodeCount = 1,
                    ParameterCount          = (short)parameterCount,
                    Parameters              = parameters,
                    ResultColumnFormatCodes =
                        QueryResultFormat == PostgresFormatCode.Binary
                            ? _binaryFormatCode
                            : _textFormatCode
                });

                WriteMessage(new DescribeMessage {
                    StatementTargetType = StatementTargetType.Portal
                });

                WriteMessage(new ExecuteMessage {
                });

                WriteMessage(new SyncMessage {
                });

                await FlushWrites(async, cancellationToken)
                .ConfigureAwait(false);
            }
            finally
            {
                if (parameters != null)
                {
                    for (var i = 0; i < parameterCount; ++i)
                    {
                        var param = parameters[i].Parameters;
                        ArrayPool.Free(ref param);
                    }
                }
            }
        }
        public static bool GenerateMesh(ChunkMesh mesh, Chunk chunk, ref double timer)
        {
            if (vertexPool.ArrayAvailble && indexPool.ArrayAvailble)
            {
                // Generate the mesh
                int vertexToken = 0;
                int indexToken  = 0;

                ChunkVertex[] vertices = vertexPool.GetArray(ref vertexToken);
                uint[]        indices  = indexPool.GetArray(ref indexToken);

                BlockStorage blocks      = chunk.GetBlocks();
                int          vertexCount = 0;
                int          indexCount  = 0;

#if _DEBUG_CHUNKS
                Stopwatch generationTimer = Stopwatch.StartNew();
#endif // _DEBUG_CHUNKS

                int    index    = 0;
                uint   posIndex = 0;
                uint   i;
                int    shift;
                uint   A, B;
                uint   lights;
                uint[] pos = new uint[3];

                for (pos[2] = 0; pos[2] < World.CHUNK_SIZE; pos[2]++)
                {
                    for (pos[1] = 0; pos[1] < World.CHUNK_SIZE; pos[1]++)
                    {
                        for (pos[0] = 0; pos[0] < World.CHUNK_SIZE; pos[0]++)
                        {
                            shift = 0;
                            A     = (uint)blocks.GetBlock(index);

                            for (i = 0; i < 3; i++)
                            {
                                if (pos[i] == World.CHUNK_MASK)
                                {
                                    B = 0;
                                }
                                else
                                {
                                    B = (uint)blocks.GetBlock(index + (1 << shift));
                                }

                                shift += World.LOG_CHUNK_SIZE;
                                if (A > 0 == B > 0)
                                {
                                    continue;
                                }

                                posIndex = pos[0] | pos[1] << 6 | pos[2] << 12;

                                if (B == 0)
                                {
                                    if (pos[i] == World.CHUNK_MASK)
                                    {
                                        lights = 0;
                                    }
                                    else
                                    {
                                        lights = chunk.lights.GetLightUInt(index);
                                    }

                                    //Current face
                                    vertices[vertexCount + 0].BlockType = A | (i << 16) | (0u << 23);
                                    vertices[vertexCount + 0].BlockData =
                                        (posIndex + Face_Data[(i * 4) + 0] // Position
                                         | lights << 18
                                        );


                                    vertices[vertexCount + 1].BlockType = A | (i << 16) | (3u << 23);
                                    vertices[vertexCount + 1].BlockData =
                                        (posIndex + Face_Data[(i * 4) + 1] // Position
                                         | lights << 18
                                        );
                                    vertices[vertexCount + 2].BlockType = A | (i << 16) | (2u << 23);
                                    vertices[vertexCount + 2].BlockData =
                                        (posIndex + Face_Data[(i * 4) + 2] // Position
                                         | lights << 18
                                        );
                                    vertices[vertexCount + 3].BlockType = A | (i << 16) | (1u << 23);
                                    vertices[vertexCount + 3].BlockData =
                                        (posIndex + Face_Data[(i * 4) + 3] // Position
                                         | lights << 18
                                        );

                                    indices[indexCount++] = (uint)vertexCount + 3;
                                    indices[indexCount++] = (uint)vertexCount + 2;
                                    indices[indexCount++] = (uint)vertexCount + 0;

                                    indices[indexCount++] = (uint)vertexCount + 2;
                                    indices[indexCount++] = (uint)vertexCount + 1;
                                    indices[indexCount++] = (uint)vertexCount + 0;
                                    vertexCount          += 4;
                                }
                                else
                                {
                                    //OtherFace
                                    lights = chunk.lights.GetLightUInt(index);
                                    //Current face
                                    vertices[vertexCount + 0].BlockType = A | (i << 16) | (0u << 23);
                                    vertices[vertexCount + 0].BlockData =
                                        (posIndex + Face_Data[(i * 4) + 0 + 12] + Block_Pos_Shifts[i] // Position
                                         | lights << 18
                                        );


                                    vertices[vertexCount + 1].BlockType = A | (i << 16) | (3u << 23);
                                    vertices[vertexCount + 1].BlockData =
                                        (posIndex + Face_Data[(i * 4) + 1 + 12] + Block_Pos_Shifts[i] // Position
                                         | lights << 18
                                        );
                                    vertices[vertexCount + 2].BlockType = A | (i << 16) | (2u << 23);
                                    vertices[vertexCount + 2].BlockData =
                                        (posIndex + Face_Data[(i * 4) + 2 + 12] + Block_Pos_Shifts[i] // Position
                                         | lights << 18
                                        );
                                    vertices[vertexCount + 3].BlockType = A | (i << 16) | (1u << 23);
                                    vertices[vertexCount + 3].BlockData =
                                        (posIndex + Face_Data[(i * 4) + 3 + 12] + Block_Pos_Shifts[i] // Position
                                         | lights << 18
                                        );

                                    indices[indexCount++] = (uint)vertexCount + 3;
                                    indices[indexCount++] = (uint)vertexCount + 2;
                                    indices[indexCount++] = (uint)vertexCount + 0;

                                    indices[indexCount++] = (uint)vertexCount + 2;
                                    indices[indexCount++] = (uint)vertexCount + 1;
                                    indices[indexCount++] = (uint)vertexCount + 0;
                                    vertexCount          += 4;
                                }
                            }

                            index++;
                        }
                    }
                }

#if _DEBUG_CHUNKS
                generationTimer.Stop();
                timer = generationTimer.ElapsedTicks / 10000.0;
#endif // _DEBUG_CHUNKS

                mesh.SetData(vertices, indices);
                vertexPool.ReturnArray(vertexToken);
                indexPool.ReturnArray(indexToken);

                return(true);
            }
            // Wait for free cycles
            _chunksToGenerate.Add(chunk);
            _meshesToGenerate.Add(mesh);
            return(false);
        }
        private async Task OpenAsync(bool async,
                                     CancellationToken cancellationToken)
        {
            ResetBuffer();

            _state = ConnectionState.Connecting;

            await CreateConnection(cancellationToken)
            .ConfigureAwait(false);

            const int messageCount = 3;
            var       messages     = ArrayPool <KeyValuePair <string, string> >
                                     .GetArray(messageCount);

            try
            {
                messages[0] = new KeyValuePair <string, string>(
                    "user", PostgresConnectionString.Username);
                messages[1] = new KeyValuePair <string, string>(
                    "client_encoding", PostgresConnectionString.Encoding);
                messages[2] = new KeyValuePair <string, string>(
                    "database", PostgresConnectionString.Database);

                WriteMessage(new StartupMessage {
                    MessageCount = messageCount,
                    Messages     = messages
                });
            }
            finally
            {
                ArrayPool.Free(ref messages);
            }

            await FlushWrites(async, cancellationToken)
            .ConfigureAwait(false);

            var authMessageTask = EnsureNextMessage <AuthenticationMessage>(
                async, cancellationToken);

            using (var authMessage = await authMessageTask
                                     .ConfigureAwait(false))
            {
                Authenticate(authMessage);
            }

            await FlushWrites(async, cancellationToken)
            .ConfigureAwait(false);

            var authMessageOkTask = EnsureNextMessage <AuthenticationMessage>(
                async, cancellationToken);

            using (var authOkMessage = await authMessageOkTask
                                       .ConfigureAwait(false))
            {
                authOkMessage.AsssertIsOk();
            }

            var foundIdleMessage = false;

            do
            {
                var message = await ReadNextMessage(async, cancellationToken)
                              .ConfigureAwait(false);

                switch (message)
                {
                case BackendKeyDataMessage keyDataMessage:
                    // TODO: Throw not supported exception?
                    break;

                case ReadyForQueryMessage readyMessage:
                    _state = ConnectionState.Open;
                    readyMessage.AssertType(TransactionIndicatorType.Idle);
                    foundIdleMessage = true;
                    break;

                default:
                    throw new PostgresInvalidMessageException(message);
                }
            } while (!foundIdleMessage);
        }