/// <summary>Initializes the GUI for this sample.</summary> /// <param name="application">The hosting <see cref="Application" />.</param> /// <param name="timeout">The <see cref="TimeSpan" /> after which this sample should stop running.</param> /// <param name="windowLocation">The <see cref="Vector2" /> that defines the initial window location.</param> /// <param name="windowSize">The <see cref="Vector2" /> that defines the initial window client rectangle size.</param> public override void Initialize(Application application, TimeSpan timeout, Vector2?windowLocation, Vector2?windowSize) { base.Initialize(application, timeout, windowLocation, windowSize); var graphicsDevice = GraphicsDevice; _constantBuffer = graphicsDevice.CreateConstantBuffer(64 * 1024, GraphicsCpuAccess.Write); _texture2D = graphicsDevice.CreateTexture2D(GraphicsFormat.R8G8B8A8_UNORM, 256, 256); _uploadBuffer = graphicsDevice.CreateUploadBuffer(1 * 1024 * 1024); _vertexBuffer = graphicsDevice.CreateVertexBuffer(64 * 1024); var copyCommandQueue = graphicsDevice.CopyCommandQueue; var copyContext = copyCommandQueue.RentContext(); { copyContext.Reset(); { _trianglePrimitive = CreateTrianglePrimitive(copyContext); } copyContext.Close(); copyContext.Execute(); } copyCommandQueue.ReturnContext(copyContext); _uploadBuffer.DisposeAllViews(); }
/// <summary>Initializes the GUI for this sample.</summary> /// <param name="application">The hosting <see cref="Application" />.</param> /// <param name="timeout">The <see cref="TimeSpan" /> after which this sample should stop running.</param> /// <param name="windowLocation">The <see cref="Vector2" /> that defines the initial window location.</param> /// <param name="windowSize">The <see cref="Vector2" /> that defines the initial window client rectangle size.</param> public override void Initialize(Application application, TimeSpan timeout, Vector2?windowLocation, Vector2?windowSize) { base.Initialize(application, timeout, windowLocation, windowSize); var graphicsDevice = GraphicsDevice; var verticeCount = 2 * 12 * (nuint)MathF.Pow(4, _recursionDepth); _constantBuffer = graphicsDevice.CreateConstantBuffer(64 * 1024, GraphicsCpuAccess.Write); _indexBuffer = graphicsDevice.CreateIndexBuffer(verticeCount * SizeOf <uint>()); _texture3D = graphicsDevice.CreateTexture3D(GraphicsFormat.R8G8B8A8_UNORM, 256, 256, 256); _uploadBuffer = graphicsDevice.CreateUploadBuffer(128 * 1024 * 1024); _vertexBuffer = graphicsDevice.CreateVertexBuffer(verticeCount * SizeOf <PosNormTex3DVertex>()); var copyCommandQueue = graphicsDevice.CopyCommandQueue; var copyContext = copyCommandQueue.RentContext(); { copyContext.Reset(); { _sierpinskiPrimitive = CreateSierpinskiPrimitive(copyContext); } copyContext.Close(); copyContext.Execute(); } copyCommandQueue.ReturnContext(copyContext); _uploadBuffer.DisposeAllViews(); }
/// <inheritdoc /> public override void Copy(GraphicsTexture destination, GraphicsBuffer source) => Copy((VulkanGraphicsTexture)destination, (VulkanGraphicsBuffer)source);
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); }
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); }
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); }
internal GraphicsTextureView(GraphicsTexture texture, in GraphicsTextureViewCreateOptions createOptions) : base(texture, in createOptions)