Ejemplo n.º 1
0
        static GraphicsTextureView CreateTexture3DView(GraphicsCopyContext copyContext, GraphicsTexture texture3D, GraphicsBuffer uploadBuffer)
        {
            var uploadBufferView = uploadBuffer.CreateBufferView <byte>(checked ((uint)texture3D.ByteLength));
            var textureDataSpan  = uploadBufferView.Map <byte>();

            {
                var width = texture3D.PixelWidth;

                var height      = texture3D.PixelHeight;
                var bytesPerRow = texture3D.BytesPerRow;

                var depth         = texture3D.PixelDepth;
                var bytesPerLayer = texture3D.BytesPerLayer;

                for (var z = 0u; z < depth; z++)
                {
                    var layerIndex = z * bytesPerLayer;

                    for (var y = 0u; y < height; y++)
                    {
                        var rowIndex = layerIndex + (y * bytesPerRow);
                        var row      = (uint *)textureDataSpan.GetPointer(rowIndex);

                        for (var x = 0u; x < width; x++)
                        {
                            var red   = x * 256u / width;
                            var blue  = y * 256u / height;
                            var green = z * 256u / depth;
                            var alpha = 0xFFu;

                            row[x] = (alpha << 24) | (green << 16) | (blue << 8) | (red << 0);
                        }
                    }
                }
            }
            uploadBufferView.UnmapAndWrite();

            var texture3DView = texture3D.CreateView(0, 1);

            copyContext.Copy(texture3DView, uploadBufferView);
            return(texture3DView);
        }
Ejemplo n.º 2
0
        static GraphicsTextureView CreateTexture2DView(GraphicsCopyContext copyContext, GraphicsTexture texture2D, GraphicsBuffer uploadBuffer)
        {
            var uploadBufferView = uploadBuffer.CreateBufferView <byte>(checked ((uint)texture2D.ByteLength));
            var textureDataSpan  = uploadBufferView.Map <byte>();

            {
                var width = texture2D.PixelWidth;

                var height      = texture2D.PixelHeight;
                var bytesPerRow = texture2D.BytesPerRow;

                var cellWidth  = width / 8;
                var cellHeight = height / 8;

                for (var y = 0u; y < height; y++)
                {
                    var rowIndex = y * bytesPerRow;
                    var row      = (uint *)textureDataSpan.GetPointer(rowIndex);

                    for (var x = 0u; x < width; x++)
                    {
                        if ((x / cellWidth % 2) == (y / cellHeight % 2))
                        {
                            row[x] = 0xFF000000;
                        }
                        else
                        {
                            row[x] = 0xFFFFFFFF;
                        }
                    }
                }
            }
            uploadBufferView.UnmapAndWrite();

            var texture2DView = texture2D.CreateView(0, 1);

            copyContext.Copy(texture2DView, uploadBufferView);
            return(texture2DView);
        }
Ejemplo n.º 3
0
        static GraphicsTextureView CreateTexture3DView(GraphicsCopyContext copyContext, GraphicsTexture texture3D, GraphicsBuffer uploadBuffer, bool isQuickAndDirty)
        {
            var uploadBufferView = uploadBuffer.CreateBufferView <byte>(checked ((uint)texture3D.ByteLength));
            var textureDataSpan  = uploadBufferView.Map <byte>();

            {
                var random       = new Random(Seed: 20170526);
                var isOnBlurring = true;

                var width = texture3D.PixelWidth;

                var height      = texture3D.PixelHeight;
                var bytesPerRow = texture3D.BytesPerRow;

                var depth         = texture3D.PixelDepth;
                var bytesPerLayer = texture3D.BytesPerLayer;

                // start with random speckles
                for (var z = 0u; z < depth; z++)
                {
                    var layerIndex = z * bytesPerLayer;

                    for (var y = 0u; y < height; y++)
                    {
                        var rowIndex = layerIndex + (y * bytesPerRow);
                        var row      = (uint *)textureDataSpan.GetPointer(rowIndex);

                        for (var x = 0u; x < width; x++)
                        {
                            // convert indices to fractions in the range [0, 1)
                            var fx = (float)x / width;
                            var fz = (float)z / depth;

                            // make x,z relative to texture center
                            fx -= 0.5f;
                            fz -= 0.5f;

                            // get radius from center, clamped to 0.5
                            var radius = MathF.Abs(fx); // MathF.Sqrt(fx * fx + fz * fz);

                            if (radius > 0.5f)
                            {
                                radius = 0.5f;
                            }

                            // scale as 1 in center, tapering off to the edge
                            var scale = 2 * MathF.Abs(0.5f - radius);

                            // random value scaled by the above
                            var rand = random.NextSingle();

                            if (isOnBlurring && (rand < 0.99))
                            {
                                rand = 0;
                            }

                            uint value = (byte)(rand * scale * 255);
                            row[x] = value | (value << 8) | (value << 16) | (value << 24);
                        }
                    }
                }

                if (isOnBlurring)
                {
                    // now smear them out to smooth smoke splotches
                    var falloffFactor = isQuickAndDirty ? 0.9f : 0.95f;

                    for (var z = 0u; z < depth; z++)
                    {
                        var layerIndex = z * bytesPerLayer;

                        for (var y = 0u; y < height; y++)
                        {
                            var rowIndex = layerIndex + (y * bytesPerRow);
                            var row      = (uint *)textureDataSpan.GetPointer(rowIndex);

                            for (var x = 1u; x < width; x++)
                            {
                                if ((row[x] & 0xFF) < falloffFactor * (row[x - 1] & 0xFF))
                                {
                                    uint value = (byte)(falloffFactor * (row[x - 1] & 0xFF));
                                    row[x] = value | (value << 8) | (value << 16) | (value << 24);
                                }
                            }

                            for (var x = width - 2; x != uint.MaxValue; x = unchecked (x - 1))
                            {
                                if ((row[x] & 0xFF) < falloffFactor * (row[x + 1] & 0xFF))
                                {
                                    uint value = (byte)(falloffFactor * (row[x + 1] & 0xFF));
                                    row[x] = value | (value << 8) | (value << 16) | (value << 24);
                                }
                            }
                        }
                    }

                    for (var z = 0u; z < depth; z++)
                    {
                        var layerIndex = z * bytesPerLayer;

                        for (var x = 0u; x < width; x++)
                        {
                            for (var y = 1u; y < height; y++)
                            {
                                var rowIndex = layerIndex + (y * bytesPerRow);
                                var row      = (uint *)textureDataSpan.GetPointer(rowIndex);

                                var previousRowIndex = rowIndex - bytesPerRow;
                                var previousRow      = (uint *)textureDataSpan.GetPointer(previousRowIndex);

                                if ((row[x] & 0xFF) < falloffFactor * (previousRow[x] & 0xFF))
                                {
                                    uint value = (byte)(falloffFactor * (previousRow[x] & 0xFF));
                                    row[x] = value | (value << 8) | (value << 16) | (value << 24);
                                }
                            }

                            for (var y = 0u; y <= 0; y++)
                            {
                                var rowIndex = layerIndex + (y * bytesPerRow);
                                var row      = (uint *)textureDataSpan.GetPointer(rowIndex);

                                var previousRowOfNextLayerIndex = rowIndex + bytesPerLayer - bytesPerRow;
                                var previousRowOfNextLayer      = (uint *)textureDataSpan.GetPointer(previousRowOfNextLayerIndex);

                                if ((row[x] & 0xFF) < falloffFactor * (previousRowOfNextLayer[x] & 0xFF))
                                {
                                    uint value = (byte)(falloffFactor * (previousRowOfNextLayer[x] & 0xFF));
                                    row[x] = value | (value << 8) | (value << 16) | (value << 24);
                                }
                            }

                            for (var y = 1u; y < height; y++)
                            {
                                var rowIndex = layerIndex + (y * bytesPerRow);
                                var row      = (uint *)textureDataSpan.GetPointer(rowIndex);

                                var previousRowIndex = rowIndex - bytesPerRow;
                                var previousRow      = (uint *)textureDataSpan.GetPointer(previousRowIndex);

                                if ((row[x] & 0xFF) < falloffFactor * (previousRow[x] & 0xFF))
                                {
                                    uint value = (byte)(falloffFactor * (previousRow[x] & 0xFF));
                                    row[x] = value | (value << 8) | (value << 16) | (value << 24);
                                }
                            }

                            for (var y = height - 2; y != uint.MaxValue; y = unchecked (y - 1))
                            {
                                var rowIndex = layerIndex + (y * bytesPerRow);
                                var row      = (uint *)textureDataSpan.GetPointer(rowIndex);

                                var nextRowIndex = rowIndex + bytesPerRow;
                                var nextRow      = (uint *)textureDataSpan.GetPointer(nextRowIndex);

                                if ((row[x] & 0xFF) < falloffFactor * (nextRow[x] & 0xFF))
                                {
                                    uint value = (byte)(falloffFactor * (nextRow[x] & 0xFF));
                                    row[x] = value | (value << 8) | (value << 16) | (value << 24);
                                }
                            }

                            for (var y = height - 1; y >= height - 1; y--)
                            {
                                var rowIndex = layerIndex + (y * bytesPerRow);
                                var row      = (uint *)textureDataSpan.GetPointer(rowIndex);

                                var nextRowOfPreviousLayerIndex = rowIndex + bytesPerRow - bytesPerLayer;
                                var nextRowOfPreviousLayer      = (uint *)textureDataSpan.GetPointer(nextRowOfPreviousLayerIndex);

                                if ((row[x] & 0xFF) < falloffFactor * (nextRowOfPreviousLayer[x] & 0xFF))
                                {
                                    uint value = (byte)(falloffFactor * (nextRowOfPreviousLayer[x] & 0xFF));
                                    row[x] = value | (value << 8) | (value << 16) | (value << 24);
                                }
                            }

                            for (var y = height - 2; y != uint.MaxValue; y = unchecked (y - 1))
                            {
                                var rowIndex = layerIndex + (y * bytesPerRow);
                                var row      = (uint *)textureDataSpan.GetPointer(rowIndex);

                                var nextRowIndex = rowIndex + bytesPerRow;
                                var nextRow      = (uint *)textureDataSpan.GetPointer(nextRowIndex);

                                if ((row[x] & 0xFF) < falloffFactor * (nextRow[x] & 0xFF))
                                {
                                    uint value = (byte)(falloffFactor * (nextRow[x] & 0xFF));
                                    row[x] = value | (value << 8) | (value << 16) | (value << 24);
                                }
                            }
                        }
                    }

                    for (var y = 0u; y < height; y++)
                    {
                        for (var x = 0u; x < width; x++)
                        {
                            if (x != 0)
                            {
                                for (var z = 1u; z < depth; z++)
                                {
                                    var layerIndex = z * bytesPerLayer;

                                    var rowIndex = layerIndex + (y * bytesPerRow);
                                    var row      = (uint *)textureDataSpan.GetPointer(rowIndex);

                                    var sameRowOfPreviousLayerIndex = rowIndex - bytesPerLayer;
                                    var sameRowOfPreviousLayer      = (uint *)textureDataSpan.GetPointer(sameRowOfPreviousLayerIndex);

                                    if ((row[x] & 0xFF) < falloffFactor * (sameRowOfPreviousLayer[x] & 0xFF))
                                    {
                                        uint value = (byte)(falloffFactor * (row[x - 1] & 0xFF));
                                        row[x] = value | (value << 8) | (value << 16) | (value << 24);
                                    }
                                }
                            }

                            if (x != (width - 1))
                            {
                                for (var z = depth - 1u; z != uint.MaxValue; z = unchecked (z - 1))
                                {
                                    var layerIndex = z * bytesPerLayer;

                                    var rowIndex = layerIndex + (y * bytesPerRow);
                                    var row      = (uint *)textureDataSpan.GetPointer(rowIndex);

                                    if ((row[x] & 0xFF) < falloffFactor * (row[x + bytesPerLayer] & 0xFF))
                                    {
                                        uint value = (byte)(falloffFactor * (row[x + 1] & 0xFF));
                                        row[x] = value | (value << 8) | (value << 16) | (value << 24);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            uploadBufferView.UnmapAndWrite();

            var texture3DView = texture3D.CreateView(0, 1);

            copyContext.Copy(texture3DView, uploadBufferView);
            return(texture3DView);
        }