static GraphicsBufferView CreateVertexBufferView(GraphicsCopyContext copyContext, GraphicsBuffer vertexBuffer, GraphicsBuffer uploadBuffer, float aspectRatio) { var uploadBufferView = uploadBuffer.CreateBufferView <IdentityVertex>(3); var vertexBufferSpan = uploadBufferView.Map <IdentityVertex>(); { vertexBufferSpan[0] = new IdentityVertex { Color = Colors.Red, Position = Vector3.Create(0.0f, 0.25f * aspectRatio, 0.0f), }; vertexBufferSpan[1] = new IdentityVertex { Color = Colors.Lime, Position = Vector3.Create(0.25f, -0.25f * aspectRatio, 0.0f), }; vertexBufferSpan[2] = new IdentityVertex { Color = Colors.Blue, Position = Vector3.Create(-0.25f, -0.25f * aspectRatio, 0.0f), }; } uploadBufferView.UnmapAndWrite(); var vertexBufferView = vertexBuffer.CreateBufferView <IdentityVertex>(3); copyContext.Copy(vertexBufferView, uploadBufferView); return(vertexBufferView); }
static GraphicsBufferView CreateVertexBufferView(GraphicsCopyContext copyContext, GraphicsBuffer vertexBuffer, GraphicsBuffer uploadBuffer, float aspectRatio) { var uploadBufferView = uploadBuffer.CreateBufferView <IdentityVertex>(4); var vertexBufferSpan = uploadBufferView.Map <IdentityVertex>(); { vertexBufferSpan[0] = new IdentityVertex { // Color = Colors.Red, // y in this setup Position = Vector3.Create(-0.25f, 0.25f * aspectRatio, 0.0f), // ^ z the origin o }; // | / is in the middle // | / of the rendered scene vertexBufferSpan[1] = new IdentityVertex { // o------>x Color = Colors.Blue, // Position = Vector3.Create(0.25f, 0.25f * aspectRatio, 0.0f), // 0 ----- 1 }; // | \ | // | \ | vertexBufferSpan[2] = new IdentityVertex { // | \ | Color = Colors.Lime, // 3-------2 Position = Vector3.Create(0.25f, -0.25f * aspectRatio, 0.0f), // }; vertexBufferSpan[3] = new IdentityVertex { Color = Colors.Blue, Position = Vector3.Create(-0.25f, -0.25f * aspectRatio, 0.0f), }; } uploadBufferView.UnmapAndWrite(); var vertexBufferView = vertexBuffer.CreateBufferView <IdentityVertex>(4); copyContext.Copy(vertexBufferView, uploadBufferView); return(vertexBufferView); }
static GraphicsBufferView CreateVertexBufferView(GraphicsCopyContext copyContext, GraphicsBuffer vertexBuffer, GraphicsBuffer uploadBuffer, float aspectRatio) { var uploadBufferView = uploadBuffer.CreateBufferView <TextureVertex>(3); var vertexBufferSpan = uploadBufferView.Map <TextureVertex>(); { vertexBufferSpan[0] = new TextureVertex { Position = Vector3.Create(0.0f, 0.25f * aspectRatio, 0.0f), UV = Vector2.Create(0.5f, 0.0f) }; vertexBufferSpan[1] = new TextureVertex { Position = Vector3.Create(0.25f, -0.25f * aspectRatio, 0.0f), UV = Vector2.Create(1.0f, 1.0f) }; vertexBufferSpan[2] = new TextureVertex { Position = Vector3.Create(-0.25f, -0.25f * aspectRatio, 0.0f), UV = Vector2.Create(0.0f, 1.0f) }; } uploadBufferView.UnmapAndWrite(); var vertexBufferView = vertexBuffer.CreateBufferView <TextureVertex>(3); copyContext.Copy(vertexBufferView, uploadBufferView); return(vertexBufferView); }
static GraphicsBufferView CreateVertexBufferView(GraphicsCopyContext copyContext, GraphicsBuffer vertexBuffer, GraphicsBuffer uploadBuffer, float aspectRatio) { var uploadBufferView = uploadBuffer.CreateBufferView <Texture3DVertex>(4); var vertexBufferSpan = uploadBufferView.Map <Texture3DVertex>(); { vertexBufferSpan[0] = new Texture3DVertex { // Position = Vector3.Create(-0.5f, 0.5f, 0.0f), // y in this setup UVW = Vector3.Create(0, 1, 0.5f), // ^ z the origin o }; // | / is in the middle // | / of the rendered scene vertexBufferSpan[1] = new Texture3DVertex { // o------>x Position = Vector3.Create(0.5f, 0.5f, 0.0f), // UVW = Vector3.Create(1, 1, 0.5f), // 0 ----- 1 }; // | \ | // | \ | vertexBufferSpan[2] = new Texture3DVertex { // | \ | Position = Vector3.Create(0.5f, -0.5f, 0.0f), // 3-------2 UVW = Vector3.Create(1, 0, 0.5f), // }; vertexBufferSpan[3] = new Texture3DVertex { Position = Vector3.Create(-0.5f, -0.5f, 0.0f), UVW = Vector3.Create(0, 0, 0.5f), }; } uploadBufferView.UnmapAndWrite(); var vertexBufferView = vertexBuffer.CreateBufferView <Texture3DVertex>(4); copyContext.Copy(vertexBufferView, uploadBufferView); return(vertexBufferView); }
static GraphicsBufferView CreateConstantBufferView(GraphicsBuffer constantBuffer) { var constantBufferView = constantBuffer.CreateBufferView <Matrix4x4>(1); var constantBufferSpan = constantBufferView.Map <Matrix4x4>(); { constantBufferSpan[0] = Matrix4x4.Identity; } constantBufferView.UnmapAndWrite(); return(constantBufferView); }
static GraphicsBufferView CreateConstantBufferView(GraphicsBuffer constantBuffer, uint instanceCount) { var constantBufferView = constantBuffer.CreateBufferView <Matrix4x4>(instanceCount); var constantBufferSpan = constantBufferView.Map <Matrix4x4>(); { for (var index = 0u; index < instanceCount; index++) { constantBufferSpan[index] = Matrix4x4.Identity; } } constantBufferView.UnmapAndWrite(); return(constantBufferView); }
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 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 GraphicsBufferView CreateIndexBufferView(GraphicsCopyContext copyContext, GraphicsBuffer indexBuffer, GraphicsBuffer uploadBuffer) { var uploadBufferView = uploadBuffer.CreateBufferView <ushort>(6); var indexBufferSpan = uploadBufferView.Map <ushort>(); { // clockwise when looking at the triangle from the outside indexBufferSpan[0] = 0; indexBufferSpan[1] = 1; indexBufferSpan[2] = 2; indexBufferSpan[3] = 0; indexBufferSpan[4] = 2; indexBufferSpan[5] = 3; } uploadBufferView.UnmapAndWrite(); var indexBufferView = indexBuffer.CreateBufferView <ushort>(6); copyContext.Copy(indexBufferView, uploadBufferView); return(indexBufferView); }
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); }