public void Visit(MaterialGeneratorContext context) { RoughnessMap.Visit(context); invertBuffer ??= GraphicsBuffer.New(context.GraphicsDevice, Invert, ResourceFlags.ConstantBuffer, GraphicsHeapType.Upload).DisposeBy(context.GraphicsDevice); 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.New(GraphicsDevice, indexBuffer, stride, ResourceFlags.IndexBuffer).DisposeBy(GraphicsDevice); } 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 Visit(MaterialGeneratorContext context) { if (Texture != null) { context.ShaderResourceViews.Add(Texture); } colorChannelBuffer ??= GraphicsBuffer.New(context.GraphicsDevice, Channel, ResourceFlags.ConstantBuffer, GraphicsHeapType.Upload).DisposeBy(context.GraphicsDevice); context.ConstantBufferViews.Add(colorChannelBuffer); }
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.New(GraphicsDevice, positionBuffer, positionStride, ResourceFlags.VertexBuffer).DisposeBy(GraphicsDevice); if (hasNormal) { Span <byte> normalBuffer = GetVertexBuffer(normalIndex, out int normalStride); vertexBufferViews[1] = GraphicsBuffer.New(GraphicsDevice, normalBuffer, normalStride, ResourceFlags.VertexBuffer).DisposeBy(GraphicsDevice); } if (hasTangent) { Span <byte> tangentBuffer = GetVertexBuffer(tangentIndex, out int tangentStride); vertexBufferViews[2] = GraphicsBuffer.New(GraphicsDevice, tangentBuffer, tangentStride, ResourceFlags.VertexBuffer).DisposeBy(GraphicsDevice); } if (hasTexCoord0) { Span <byte> texCoord0Buffer = GetVertexBuffer(texCoord0Index, out int texCoord0Stride); vertexBufferViews[3] = GraphicsBuffer.New(GraphicsDevice, texCoord0Buffer, texCoord0Stride, ResourceFlags.VertexBuffer).DisposeBy(GraphicsDevice); if (!hasTangent) { Span <Vector4> tangentBuffer = VertexHelper.GenerateTangents(positionBuffer, texCoord0Buffer, indexBuffer, is32bitIndex); vertexBufferViews[2] = GraphicsBuffer.New(GraphicsDevice, tangentBuffer, ResourceFlags.VertexBuffer).DisposeBy(GraphicsDevice); } } } return(vertexBufferViews); }
private static async Task Main() { // Create graphics device using GraphicsDevice device = new GraphicsDevice(FeatureLevel.Level11_0); // 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.New(device, array.AsSpan(), GraphicsBufferFlags.ShaderResource); using GraphicsBuffer <float> destinationBuffer = GraphicsBuffer.New <float>(device, array.Length * 2, GraphicsBufferFlags.UnorderedAccess); 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 bool generateWithDelegate = true; ShaderGenerator shaderGenerator = generateWithDelegate ? CreateShaderGeneratorWithDelegate(sourceBuffer, destinationBuffer) : CreateShaderGeneratorWithClass(); ShaderGeneratorResult result = shaderGenerator.GenerateShader(); // Compile shader byte[] shaderBytecode = ShaderCompiler.Compile(DxcShaderStage.ComputeShader, result.ShaderSource, result.EntryPoints["compute"]); DescriptorRange1[] descriptorRanges = new DescriptorRange1[] { new DescriptorRange1(DescriptorRangeType.UnorderedAccessView, 1, 0), new DescriptorRange1(DescriptorRangeType.ShaderResourceView, 1, 0) }; RootParameter1 rootParameter = new RootParameter1(new RootDescriptorTable1(descriptorRanges), ShaderVisibility.All); var rootSignatureDescription = new VersionedRootSignatureDescription(new RootSignatureDescription1(RootSignatureFlags.None, new[] { rootParameter })); var rootSignature = device.CreateRootSignature(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); }
public void Visit(MaterialGeneratorContext context) { valueBuffer ??= GraphicsBuffer.New(context.GraphicsDevice, Value, GraphicsBufferFlags.ConstantBuffer, GraphicsHeapType.Upload).DisposeBy(context.GraphicsDevice); context.ConstantBufferViews.Add(valueBuffer); }
public void Visit(MaterialGeneratorContext context) { isBlackAndWhiteBuffer ??= GraphicsBuffer.New(context.GraphicsDevice, IsBlackAndWhite, ResourceFlags.ConstantBuffer, GraphicsHeapType.Upload).DisposeBy(context.GraphicsDevice); context.ConstantBufferViews.Add(isBlackAndWhiteBuffer); }