public PlanarTerrain(int blockSize) : base() { //if (terrainEffect == null) throw new ArgumentNullException("terrainEffect"); BlockSize = blockSize; NarrowHeightTexture = new Texture2D(Formats.Vector2f, blockSize / 16, blockSize); SmallHeightTexture = new Texture2D(Formats.Vector2f, blockSize / 16, blockSize / 16); SmallHeightData = new float[(blockSize / 16) * (blockSize / 16) * 2]; BlockBuffer = CreateBlockBuffer(); Texture2D randomTexture = Texture.CreateRandom4nb(256, 256); var builder = ShaderBuilder.CreateFromAssemblyResource("Glare.Graphics.Shaders.Terrain.glsl"); Program = new Program( builder.VertexShader("Common", "Vertex"), builder.FragmentShader("Common", "Fragment")); Program.Uniforms["terrainSize"].Set(blockSize); Program.Uniforms["randomTexture"].Set(randomTexture); UnitBuffer = GraphicsBuffer.Create(UnitVertices); }
public void Accept(ShaderGeneratorContext context) { RoughnessMap.Accept(context); invertBuffer ??= GraphicsBuffer.Create(context.GraphicsDevice, Invert, ResourceFlags.None, GraphicsHeapType.Upload); context.ConstantBufferViews.Add(invertBuffer); }
public Task <Mesh> GetMeshAsync(int meshIndex) { GltfLoader.Schema.Mesh mesh = gltf.Meshes[meshIndex]; Span <byte> indexBuffer = Span <byte> .Empty; GraphicsBuffer <byte>?indexBufferView = null; bool is32bitIndex = false; if (mesh.Primitives[0].Indices.HasValue) { indexBuffer = GetIndexBuffer(mesh, out int stride); is32bitIndex = stride == sizeof(int); indexBufferView = GraphicsBuffer.Create(GraphicsDevice, indexBuffer, stride, ResourceFlags.None); } GraphicsBuffer[] vertexBufferViews = GetVertexBufferViews(mesh, indexBuffer, is32bitIndex); int materialIndex = 0; if (mesh.Primitives[0].Material.HasValue) { materialIndex = mesh.Primitives[0].Material ?? throw new Exception(); } Node node = gltf.Nodes.First(n => n.Mesh == meshIndex); float[] matrix = node.Matrix; Matrix4x4 worldMatrix = Matrix4x4.Transpose(new Matrix4x4( matrix[0], matrix[1], matrix[2], matrix[3], matrix[4], matrix[5], matrix[6], matrix[7], matrix[8], matrix[9], matrix[10], matrix[11], matrix[12], matrix[13], matrix[14], matrix[15])); float[] s = node.Scale; float[] r = node.Rotation; float[] t = node.Translation; Vector3 scale = new Vector3(s[0], s[1], s[2]); Quaternion rotation = new Quaternion(r[0], r[1], r[2], r[3]); Vector3 translation = new Vector3(t[0], t[1], t[2]); worldMatrix *= Matrix4x4.CreateScale(scale) * Matrix4x4.CreateFromQuaternion(rotation) * Matrix4x4.CreateTranslation(translation); MeshDraw meshDraw = new MeshDraw { IndexBufferView = indexBufferView, VertexBufferViews = vertexBufferViews }; return(Task.FromResult(new Mesh(meshDraw) { MaterialIndex = materialIndex, WorldMatrix = worldMatrix })); }
public void Accept(ShaderGeneratorContext context) { if (Texture != null) { context.ShaderResourceViews.Add(Texture); } colorChannelBuffer ??= GraphicsBuffer.Create(context.GraphicsDevice, Channel, ResourceFlags.None, GraphicsHeapType.Upload); context.ConstantBufferViews.Add(colorChannelBuffer); }
public static async Task RunAsync(GraphicsDevice device) { int count = 100; int sumCount = 10; List <Task> tasks = new List <Task>(); for (int i = 0; i < count; i++) { int index = i; Console.WriteLine($"Scheduling task {index}"); tasks.Add(Task.Run(async() => { GraphicsBuffer <float> buffer = GraphicsBuffer.Create <float>(device, sumCount, ResourceFlags.AllowUnorderedAccess); TestShader shader = new TestShader(buffer, Sigmoid); ShaderGeneratorContext context = new ShaderGeneratorContext(device); context.Visit(shader); ShaderGeneratorResult result = new ShaderGenerator(shader).GenerateShader(); PipelineState pipelineState = await context.CreateComputePipelineStateAsync(); DescriptorSet?descriptorSet = context.CreateShaderResourceViewDescriptorSet(); using (CommandList commandList = new CommandList(device, CommandListType.Compute)) { commandList.SetPipelineState(pipelineState); if (descriptorSet != null) { commandList.SetComputeRootDescriptorTable(0, descriptorSet); } commandList.Dispatch(1, 1, 1); await commandList.FlushAsync(); } float sum = buffer.GetData().Sum(); Console.WriteLine($"Thread: {index}, sum: {sum}."); })); } Console.WriteLine("Awaiting tasks..."); Console.WriteLine($"Task count: {tasks.Count}"); await Task.WhenAll(tasks); Console.WriteLine("DONE!"); }
private GraphicsBuffer[] GetVertexBufferViews(GltfLoader.Schema.Mesh mesh, Span <byte> indexBuffer = default, bool is32bitIndex = false) { GraphicsBuffer[] vertexBufferViews = new GraphicsBuffer[4]; Dictionary <string, int> attributes = mesh.Primitives[0].Attributes; bool hasPosition = attributes.TryGetValue("POSITION", out int positionIndex); bool hasNormal = attributes.TryGetValue("NORMAL", out int normalIndex); bool hasTangent = attributes.TryGetValue("TANGENT", out int tangentIndex); bool hasTexCoord0 = attributes.TryGetValue("TEXCOORD_0", out int texCoord0Index); if (hasPosition) { Span <byte> positionBuffer = GetVertexBuffer(positionIndex, out int positionStride); vertexBufferViews[0] = GraphicsBuffer.Create(GraphicsDevice, positionBuffer, positionStride, ResourceFlags.None); if (hasNormal) { Span <byte> normalBuffer = GetVertexBuffer(normalIndex, out int normalStride); vertexBufferViews[1] = GraphicsBuffer.Create(GraphicsDevice, normalBuffer, normalStride, ResourceFlags.None); } if (hasTangent) { Span <byte> tangentBuffer = GetVertexBuffer(tangentIndex, out int tangentStride); vertexBufferViews[2] = GraphicsBuffer.Create(GraphicsDevice, tangentBuffer, tangentStride, ResourceFlags.None); } if (hasTexCoord0) { Span <byte> texCoord0Buffer = GetVertexBuffer(texCoord0Index, out int texCoord0Stride); vertexBufferViews[3] = GraphicsBuffer.Create(GraphicsDevice, texCoord0Buffer, texCoord0Stride, ResourceFlags.None); if (!hasTangent) { Span <Vector4> tangentBuffer = VertexHelper.GenerateTangents(positionBuffer, texCoord0Buffer, indexBuffer, is32bitIndex); vertexBufferViews[2] = GraphicsBuffer.Create(GraphicsDevice, tangentBuffer, ResourceFlags.None); } } } return(vertexBufferViews); }
public ForwardRenderer(GraphicsDevice device) { //Scene.Processors.Add(renderableProcessor); samplerState = SamplerState.Create(device, null); descriptorPool = new DisposableObjectPool <DescriptorSet>(() => new DescriptorSet(device, geometryShader.Layout)); constantBufferPool = new DisposableObjectPool <GraphicsBuffer>(() => GraphicsBuffer.Create(device, constants, false)); geometryShader = new GeometryShader(device); geometryShader.Initialize(); pipelineStateDescription = new PipelineStateDescription() { InputLayout = PositionColorTexture.Layout, RootSignature = geometryShader.RootSignature, VertexShader = geometryShader.VertexShader, PixelShader = geometryShader.PixelShader, }; pipelineState = PipelineState.Create(device, pipelineStateDescription); }
public static VertexBufferBinding Load(GraphicsDevice device, String filename, Matrix3?positionTransform = null) { var library = Injector.Global.Resolve <Library>(); var loader = new WavefrontObjLoader(); using (var stream = library.OpenRead(filename)) { loader.LoadObj(stream); } if (positionTransform.HasValue) { Matrix3 transform = positionTransform.Value; for (int j = 0; j < loader.VertexList.Count; j++) { loader.VertexList[j].Vector = Matrix3.Transform(loader.VertexList[j].Vector, transform); } } // vertex buffer var vertexCount = loader.TriangleCount * 3; var vertices = new PositionColorTexture[vertexCount]; int i = 0; foreach (var face in loader.FaceList) { var v = face.VertexIndexList; var t = face.TextureVertexIndexList; vertices[i + 0] = new PositionColorTexture { Position = loader.VertexList[v[0] - 1].Vector, Color = new Vector4(1, 1, 1, 1), Texture = loader.TextureList[t[0] - 1].Vector }; vertices[i + 2] = new PositionColorTexture { Position = loader.VertexList[v[1] - 1].Vector, Color = new Vector4(1, 1, 1, 1), Texture = loader.TextureList[t[1] - 1].Vector }; vertices[i + 1] = new PositionColorTexture { Position = loader.VertexList[v[2] - 1].Vector, Color = new Vector4(1, 1, 1, 1), Texture = loader.TextureList[t[2] - 1].Vector }; i += 3; if (v.Length == 4) { vertices[i + 0] = new PositionColorTexture { Position = loader.VertexList[v[2] - 1].Vector, Color = new Vector4(1, 1, 1, 1), Texture = loader.TextureList[t[2] - 1].Vector }; vertices[i + 2] = new PositionColorTexture { Position = loader.VertexList[v[3] - 1].Vector, Color = new Vector4(1, 1, 1, 1), Texture = loader.TextureList[t[3] - 1].Vector }; vertices[i + 1] = new PositionColorTexture { Position = loader.VertexList[v[0] - 1].Vector, Color = new Vector4(1, 1, 1, 1), Texture = loader.TextureList[t[0] - 1].Vector }; i += 3; } } var buffer = GraphicsBuffer.Create(device, vertices, false); return(new VertexBufferBinding { Buffer = buffer, Count = vertexCount, Declaration = PositionColorTexture.Layout, Stride = PositionColorTexture.Layout.Stride, }); }
public void Accept(ShaderGeneratorContext context) { isBlackAndWhiteBuffer ??= GraphicsBuffer.Create(context.GraphicsDevice, IsBlackAndWhite, ResourceFlags.None, GraphicsHeapType.Upload); context.ConstantBufferViews.Add(isBlackAndWhiteBuffer); }
public static async Task RunAsync(GraphicsDevice device) { bool generateWithDelegate = false; // Create graphics buffer int width = 10; int height = 10; float[] array = new float[width * height]; for (int i = 0; i < array.Length; i++) { array[i] = i; } float[] outputArray = new float[width * height]; using GraphicsBuffer <float> sourceBuffer = GraphicsBuffer.Create <float>(device, array, ResourceFlags.None); using GraphicsBuffer <float> destinationBuffer = GraphicsBuffer.Create <float>(device, array.Length * 2, ResourceFlags.AllowUnorderedAccess); GraphicsBuffer <float> slicedDestinationBuffer = destinationBuffer.Slice(20, 60); slicedDestinationBuffer = slicedDestinationBuffer.Slice(10, 50); DescriptorSet descriptorSet = new DescriptorSet(device, 2); descriptorSet.AddUnorderedAccessViews(slicedDestinationBuffer); descriptorSet.AddShaderResourceViews(sourceBuffer); // Generate computer shader ShaderGenerator shaderGenerator = generateWithDelegate ? CreateShaderGeneratorWithDelegate(sourceBuffer, destinationBuffer) : CreateShaderGeneratorWithClass(); ShaderGeneratorResult result = shaderGenerator.GenerateShader(); // Compile shader byte[] shaderBytecode = ShaderCompiler.Compile(ShaderStage.ComputeShader, result.ShaderSource, result.EntryPoints["compute"]); DescriptorRange[] descriptorRanges = new DescriptorRange[] { new DescriptorRange(DescriptorRangeType.UnorderedAccessView, 1, 0), new DescriptorRange(DescriptorRangeType.ShaderResourceView, 1, 0) }; RootParameter rootParameter = new RootParameter(new RootDescriptorTable(descriptorRanges), ShaderVisibility.All); RootSignatureDescription rootSignatureDescription = new RootSignatureDescription(RootSignatureFlags.None, new[] { rootParameter }); RootSignature rootSignature = new RootSignature(device, rootSignatureDescription); PipelineState pipelineState = new PipelineState(device, rootSignature, shaderBytecode); // Execute computer shader using (CommandList commandList = new CommandList(device, CommandListType.Compute)) { commandList.SetPipelineState(pipelineState); commandList.SetComputeRootDescriptorTable(0, descriptorSet); commandList.Dispatch(1, 1, 1); await commandList.FlushAsync(); } // Print matrix Console.WriteLine("Before:"); PrintMatrix(array, width, height); destinationBuffer.GetData(outputArray.AsSpan()); Console.WriteLine(); Console.WriteLine("After:"); PrintMatrix(outputArray, width, height); }