private unsafe GraphicsPrimitive CreateGraphicsPrimitive(GraphicsContext graphicsContext, GraphicsBuffer vertexStagingBuffer, GraphicsBuffer indexStagingBuffer, GraphicsBuffer textureStagingBuffer) { var graphicsDevice = GraphicsDevice; var graphicsSurface = graphicsDevice.Surface; var graphicsPipeline = CreateGraphicsPipeline(graphicsDevice, "Sierpinski", "main", "main"); var constantBuffer = _constantBuffer; var indexBuffer = _indexBuffer; var vertexBuffer = _vertexBuffer; (var vertices, var indices) = (_sierpinskiShape == SierpinskiShape.Pyramid) ? SierpinskiPyramid.CreateMeshTetrahedron(_recursionDepth) : SierpinskiPyramid.CreateMeshQuad(_recursionDepth); var normals = SierpinskiPyramid.MeshNormals(vertices); var vertexBufferRegion = CreateVertexBufferRegion(graphicsContext, vertexBuffer, vertexStagingBuffer, vertices, normals); graphicsContext.Copy(vertexBuffer, vertexStagingBuffer); var indexBufferRegion = CreateIndexBufferRegion(graphicsContext, indexBuffer, indexStagingBuffer, indices); graphicsContext.Copy(indexBuffer, indexStagingBuffer); var inputResourceRegions = new GraphicsMemoryRegion <GraphicsResource>[3] { CreateConstantBufferRegion(graphicsContext, constantBuffer), CreateConstantBufferRegion(graphicsContext, constantBuffer), CreateTexture3DRegion(graphicsContext, textureStagingBuffer), }; return(graphicsDevice.CreatePrimitive(graphicsPipeline, vertexBufferRegion, SizeOf <PosNormTex3DVertex>(), indexBufferRegion, SizeOf <uint>(), inputResourceRegions));
private unsafe GraphicsPrimitive CreateQuadPrimitive(GraphicsContext graphicsContext, GraphicsBuffer vertexStagingBuffer, GraphicsBuffer indexStagingBuffer, GraphicsBuffer textureStagingBuffer) { var graphicsDevice = GraphicsDevice; var graphicsSurface = graphicsDevice.Surface; var graphicsPipeline = CreateGraphicsPipeline(graphicsDevice, "Texture3D", "main", "main"); var constantBuffer = _constantBuffer; var indexBuffer = _indexBuffer; var vertexBuffer = _vertexBuffer; var vertexBufferRegion = CreateVertexBufferRegion(graphicsContext, vertexBuffer, vertexStagingBuffer, aspectRatio: graphicsSurface.Width / graphicsSurface.Height); graphicsContext.Copy(vertexBuffer, vertexStagingBuffer); var indexBufferRegion = CreateIndexBufferRegion(graphicsContext, indexBuffer, indexStagingBuffer); graphicsContext.Copy(indexBuffer, indexStagingBuffer); var inputResourceRegions = new GraphicsMemoryRegion <GraphicsResource>[3] { CreateConstantBufferRegion(graphicsContext, constantBuffer), CreateConstantBufferRegion(graphicsContext, constantBuffer), CreateTexture3DRegion(graphicsContext, textureStagingBuffer), }; return(graphicsDevice.CreatePrimitive(graphicsPipeline, vertexBufferRegion, SizeOf <Texture3DVertex>(), indexBufferRegion, SizeOf <ushort>(), inputResourceRegions));
static GraphicsMemoryRegion <GraphicsResource> CreateTexture3DRegion(GraphicsContext graphicsContext, GraphicsBuffer textureStagingBuffer) { const uint TextureWidth = TEXTURE3D_SIDE_LENGTH; const uint TextureHeight = TEXTURE3D_SIDE_LENGTH; const uint TextureDepth = TEXTURE3D_SIDE_LENGTH; const uint TextureDz = TextureWidth * TextureHeight; const uint TexturePixels = TextureDz * TextureDepth; var texture3D = graphicsContext.Device.MemoryAllocator.CreateTexture(GraphicsTextureKind.ThreeDimensional, GraphicsResourceCpuAccess.None, TextureWidth, TextureHeight, (ushort)TextureDepth); var texture3DRegion = texture3D.Allocate(texture3D.Size, alignment: 4); var pTextureData = textureStagingBuffer.Map <uint>(in texture3DRegion); for (uint n = 0; n < TexturePixels; n++) { var x = n % TextureWidth; var y = n % TextureDz / TextureWidth; var z = n / TextureDz; x = x * 256 / TextureWidth; y = y * 256 / TextureHeight; z = z * 256 / TextureDepth; pTextureData[n] = 0xFF000000 | (z << 16) | (y << 8) | (x << 0); } textureStagingBuffer.UnmapAndWrite(in texture3DRegion); graphicsContext.Copy(texture3D, textureStagingBuffer); return(texture3DRegion); }
static GraphicsMemoryRegion <GraphicsResource> CreateTexture2DRegion(GraphicsContext graphicsContext, GraphicsBuffer textureStagingBuffer) { const uint TextureWidth = 256; const uint TextureHeight = 256; const uint TexturePixels = TextureWidth * TextureHeight; const uint CellWidth = TextureWidth / 8; const uint CellHeight = TextureHeight / 8; var texture2D = graphicsContext.Device.MemoryAllocator.CreateTexture(GraphicsTextureKind.TwoDimensional, GraphicsResourceCpuAccess.None, TextureWidth, TextureHeight); var texture2DRegion = texture2D.Allocate(texture2D.Size, alignment: 4); var pTextureData = textureStagingBuffer.Map <uint>(in texture2DRegion); for (uint n = 0; n < TexturePixels; n++) { var x = n % TextureWidth; var y = n / TextureWidth; pTextureData[n] = (x / CellWidth % 2) == (y / CellHeight % 2) ? 0xFF000000 : 0xFFFFFFFF; } textureStagingBuffer.UnmapAndWrite(in texture2DRegion); graphicsContext.Copy(texture2D, textureStagingBuffer); return(texture2DRegion); }
static GraphicsBuffer CreateVertexBuffer(GraphicsContext graphicsContext, GraphicsHeap graphicsHeap, GraphicsBuffer vertexStagingBuffer, float aspectRatio) { var vertexBuffer = graphicsHeap.CreateGraphicsBuffer(GraphicsBufferKind.Vertex, (ulong)(sizeof(TextureVertex) * 3), (ulong)sizeof(TextureVertex)); var pVertexBuffer = vertexStagingBuffer.Map <TextureVertex>(); pVertexBuffer[0] = new TextureVertex { Position = new Vector3(0.0f, 0.25f * aspectRatio, 0.0f), UV = new Vector2(0.5f, 1.0f), }; pVertexBuffer[1] = new TextureVertex { Position = new Vector3(0.25f, -0.25f * aspectRatio, 0.0f), UV = new Vector2(1.0f, 0.0f), }; pVertexBuffer[2] = new TextureVertex { Position = new Vector3(-0.25f, -0.25f * aspectRatio, 0.0f), UV = new Vector2(0.0f, 0.0f), }; vertexStagingBuffer.Unmap(0..(sizeof(TextureVertex) * 3)); graphicsContext.Copy(vertexBuffer, vertexStagingBuffer); return(vertexBuffer); }
static GraphicsTexture CreateTexture2D(GraphicsContext graphicsContext, GraphicsHeap graphicsHeap, GraphicsBuffer textureStagingBuffer) { const uint TextureWidth = 256; const uint TextureHeight = 256; const uint TextureSize = TextureWidth * TextureHeight; const uint CellWidth = TextureWidth / 8; const uint CellHeight = TextureHeight / 8; var texture2D = graphicsHeap.CreateGraphicsTexture(GraphicsTextureKind.TwoDimensional, TextureWidth, TextureHeight); var pTextureData = textureStagingBuffer.Map <uint>(); for (uint n = 0; n < TextureSize; n++) { var x = n % TextureWidth; var y = n / TextureWidth; if ((x / CellWidth % 2) == (y / CellHeight % 2)) { pTextureData[n] = 0xFF000000; } else { pTextureData[n] = 0xFFFFFFFF; } } textureStagingBuffer.Unmap(0..(int)TextureSize); graphicsContext.Copy(texture2D, textureStagingBuffer); return(texture2D); }
static GraphicsBuffer CreateVertexBuffer(GraphicsContext graphicsContext, GraphicsBuffer vertexStagingBuffer, float aspectRatio) { var vertexBuffer = graphicsContext.Device.MemoryAllocator.CreateBuffer(GraphicsBufferKind.Vertex, GraphicsResourceCpuAccess.None, (ulong)(sizeof(IdentityVertex) * 3)); var pVertexBuffer = vertexStagingBuffer.Map <IdentityVertex>(); pVertexBuffer[0] = new IdentityVertex { Position = new Vector3(0.0f, 0.25f * aspectRatio, 0.0f), Color = new Vector4(1.0f, 0.0f, 0.0f, 1.0f) }; pVertexBuffer[1] = new IdentityVertex { Position = new Vector3(0.25f, -0.25f * aspectRatio, 0.0f), Color = new Vector4(0.0f, 1.0f, 0.0f, 1.0f) }; pVertexBuffer[2] = new IdentityVertex { Position = new Vector3(-0.25f, -0.25f * aspectRatio, 0.0f), Color = new Vector4(0.0f, 0.0f, 1.0f, 1.0f) }; vertexStagingBuffer.Unmap(0..(sizeof(IdentityVertex) * 3)); graphicsContext.Copy(vertexBuffer, vertexStagingBuffer); return(vertexBuffer); }
private unsafe GraphicsPrimitive CreateQuadPrimitive(GraphicsContext graphicsContext, GraphicsBuffer vertexStagingBuffer, GraphicsBuffer indexStagingBuffer) { var graphicsDevice = GraphicsDevice; var graphicsSurface = graphicsDevice.Surface; var graphicsPipeline = CreateGraphicsPipeline(graphicsDevice, "Identity", "main", "main"); var indexBuffer = _indexBuffer; var vertexBuffer = _vertexBuffer; var vertexBufferRegion = CreateVertexBufferRegion(graphicsContext, vertexBuffer, vertexStagingBuffer, aspectRatio: graphicsSurface.Width / graphicsSurface.Height); graphicsContext.Copy(vertexBuffer, vertexStagingBuffer); var indexBufferRegion = CreateIndexBufferRegion(graphicsContext, indexBuffer, indexStagingBuffer); graphicsContext.Copy(indexBuffer, indexStagingBuffer); return(graphicsDevice.CreatePrimitive(graphicsPipeline, vertexBufferRegion, SizeOf <IdentityVertex>(), indexBufferRegion, SizeOf <ushort>()));
private unsafe GraphicsPrimitive CreateTrianglePrimitive(GraphicsContext graphicsContext, GraphicsBuffer vertexStagingBuffer, GraphicsBuffer textureStagingBuffer) { var graphicsDevice = GraphicsDevice; var graphicsSurface = graphicsDevice.Surface; var graphicsPipeline = CreateGraphicsPipeline(graphicsDevice, "Texture", "main", "main"); var vertexBuffer = _vertexBuffer; var vertexBufferRegion = CreateVertexBufferRegion(graphicsContext, vertexBuffer, vertexStagingBuffer, aspectRatio: graphicsSurface.Width / graphicsSurface.Height); graphicsContext.Copy(vertexBuffer, vertexStagingBuffer); var inputResourceRegions = new GraphicsMemoryRegion <GraphicsResource>[1] { CreateTexture2DRegion(graphicsContext, textureStagingBuffer) }; return(graphicsDevice.CreatePrimitive(graphicsPipeline, vertexBufferRegion, SizeOf <TextureVertex>(), inputResourceRegions: inputResourceRegions));
static GraphicsBuffer CreateIndexBuffer(GraphicsContext graphicsContext, GraphicsBuffer indexStagingBuffer) { var indexBuffer = graphicsContext.Device.MemoryAllocator.CreateBuffer(GraphicsBufferKind.Index, GraphicsResourceCpuAccess.None, sizeof(ushort) * 6); var pIndexBuffer = indexStagingBuffer.Map <ushort>(); pIndexBuffer[0] = 0; pIndexBuffer[1] = 1; pIndexBuffer[2] = 2; pIndexBuffer[3] = 0; pIndexBuffer[4] = 2; pIndexBuffer[5] = 3; indexStagingBuffer.Unmap(0..(sizeof(ushort) * 6)); graphicsContext.Copy(indexBuffer, indexStagingBuffer); return(indexBuffer); }
static GraphicsBuffer CreateIndexBuffer(GraphicsContext graphicsContext, GraphicsHeap graphicsHeap, GraphicsBuffer indexStagingBuffer) { var indexBuffer = graphicsHeap.CreateGraphicsBuffer(GraphicsBufferKind.Index, sizeof(ushort) * 6, sizeof(ushort)); var pIndexBuffer = indexStagingBuffer.Map <ushort>(); pIndexBuffer[0] = 0; pIndexBuffer[1] = 1; pIndexBuffer[2] = 2; pIndexBuffer[3] = 0; pIndexBuffer[4] = 2; pIndexBuffer[5] = 3; indexStagingBuffer.Unmap(0..(sizeof(ushort) * 6)); graphicsContext.Copy(indexBuffer, indexStagingBuffer); return(indexBuffer); }
GraphicsMemoryRegion <GraphicsResource> CreateTexture3DRegion(GraphicsContext graphicsContext, GraphicsBuffer textureStagingBuffer, bool isQuickAndDirty) { var textureWidth = isQuickAndDirty ? 64u : 256u; var textureHeight = isQuickAndDirty ? 64u : 256u; var textureDepth = isQuickAndDirty ? (ushort)64 : (ushort)256; var textureDz = textureWidth * textureHeight; var texturePixels = textureDz * textureDepth; var texture3D = graphicsContext.Device.MemoryAllocator.CreateTexture(GraphicsTextureKind.ThreeDimensional, GraphicsResourceCpuAccess.None, textureWidth, textureHeight, textureDepth, texelFormat: TexelFormat.R8G8B8A8_UNORM); var texture3DRegion = texture3D.Allocate(texture3D.Size, alignment: 4); var pTextureData = textureStagingBuffer.Map <uint>(in texture3DRegion); var random = new Random(Seed: 1); var isOnBlurring = true; // start with random speckles for (uint n = 0; n < texturePixels; n++) { // convert n to indices float x = n % textureWidth; float y = n % textureDz / textureWidth; float z = n / textureDz; // convert indices to fractions in the range [0, 1) x /= textureWidth; y /= textureHeight; z /= textureHeight; // make x,z relative to texture center x -= 0.5f; z -= 0.5f; // get radius from center, clamped to 0.5 var radius = MathF.Abs(x); // MathF.Sqrt(x * x + z * z); 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 = (float)random.NextDouble(); if (isOnBlurring && (rand < 0.99)) { rand = 0; } uint value = (byte)(rand * scale * 255); pTextureData[n] = (uint)(value | (value << 8) | (value << 16) | (value << 24)); } if (isOnBlurring) { // now smear them out to smooth smoke splotches var dy = textureWidth; var dz = dy * textureHeight; var falloffFactor = _isQuickAndDirty ? 0.9f : 0.95f; for (var z = 0; z < textureDepth; z++) { for (var y = 0; y < textureHeight; y++) { for (var x = 1; x < textureWidth; x++) { var n = x + (y * dy) + (z * dz); if ((pTextureData[n] & 0xFF) < falloffFactor * (pTextureData[n - 1] & 0xFF)) { uint value = (byte)(falloffFactor * (pTextureData[n - 1] & 0xFF)); pTextureData[n] = (uint)(value | (value << 8) | (value << 16) | (value << 24)); } } for (var x = (int)textureWidth - 2; x >= 0; x--) { var n = x + (y * dy) + (z * dz); if ((pTextureData[n] & 0xFF) < falloffFactor * (pTextureData[n + 1] & 0xFF)) { uint value = (byte)(falloffFactor * (pTextureData[n + 1] & 0xFF)); pTextureData[n] = (uint)(value | (value << 8) | (value << 16) | (value << 24)); } } } } for (var z = 0; z < textureDepth; z++) { for (var x = 0; x < textureWidth; x++) { for (var y = 1; y < textureHeight; y++) { var n = x + (y * dy) + (z * dz); if ((pTextureData[n] & 0xFF) < falloffFactor * (pTextureData[n - dy] & 0xFF)) { uint value = (byte)(falloffFactor * (pTextureData[n - dy] & 0xFF)); pTextureData[n] = (uint)(value | (value << 8) | (value << 16) | (value << 24)); } } for (var y = 0; y <= 0; y++) { var n = x + (y * dy) + (z * dz); if ((pTextureData[n] & 0xFF) < falloffFactor * (pTextureData[n - dy + dz] & 0xFF)) { uint value = (byte)(falloffFactor * (pTextureData[n - dy + dz] & 0xFF)); pTextureData[n] = (uint)(value | (value << 8) | (value << 16) | (value << 24)); } } for (var y = 1; y < textureHeight; y++) { var n = x + (y * dy) + (z * dz); if ((pTextureData[n] & 0xFF) < falloffFactor * (pTextureData[n - dy] & 0xFF)) { uint value = (byte)(falloffFactor * (pTextureData[n - dy] & 0xFF)); pTextureData[n] = (uint)(value | (value << 8) | (value << 16) | (value << 24)); } } for (var y = (int)textureHeight - 2; y >= 0; y--) { var n = x + (y * dy) + (z * dz); if ((pTextureData[n] & 0xFF) < falloffFactor * (pTextureData[n + dy] & 0xFF)) { uint value = (byte)(falloffFactor * (pTextureData[n + dy] & 0xFF)); pTextureData[n] = (uint)(value | (value << 8) | (value << 16) | (value << 24)); } } for (var y = (int)textureHeight - 1; y >= (int)textureHeight - 1; y--) { var n = x + (y * dy) + (z * dz); if ((pTextureData[n] & 0xFF) < falloffFactor * (pTextureData[n + dy - dz] & 0xFF)) { uint value = (byte)(falloffFactor * (pTextureData[n + dy - dz] & 0xFF)); pTextureData[n] = (uint)(value | (value << 8) | (value << 16) | (value << 24)); } } for (var y = (int)textureHeight - 2; y >= 0; y--) { var n = x + (y * dy) + (z * dz); if ((pTextureData[n] & 0xFF) < falloffFactor * (pTextureData[n + dy] & 0xFF)) { uint value = (byte)(falloffFactor * (pTextureData[n + dy] & 0xFF)); pTextureData[n] = (uint)(value | (value << 8) | (value << 16) | (value << 24)); } } } } for (var y = 0; y < textureHeight; y++) { for (var x = 0; x < textureWidth; x++) { for (var z = 1; z < textureDepth; z++) { var n = x + (y * dy) + (z * dz); if ((pTextureData[n] & 0xFF) < falloffFactor * (pTextureData[n - dz] & 0xFF)) { uint value = (byte)(falloffFactor * (pTextureData[n - 1] & 0xFF)); pTextureData[n] = (uint)(value | (value << 8) | (value << 16) | (value << 24)); } } for (var z = (int)textureDepth - 0; z >= 0; z--) { var n = x + (y * dy) + (z * dz); if ((pTextureData[n] & 0xFF) < falloffFactor * (pTextureData[n + dz] & 0xFF)) { uint value = (byte)(falloffFactor * (pTextureData[n + 1] & 0xFF)); pTextureData[n] = (uint)(value | (value << 8) | (value << 16) | (value << 24)); } } } } } textureStagingBuffer.UnmapAndWrite(in texture3DRegion); graphicsContext.Copy(texture3D, textureStagingBuffer); return(texture3DRegion); }