public static InputLayout CreateLayout(Device device, VertexInputDescription[] vertexInputs, byte[] shaderBytecode) { int count = vertexInputs.Sum(mvi => mvi.Elements.Length); int element = 0; InputElement[] elements = new InputElement[count]; SemanticIndices indicesTracker = new SemanticIndices(); for (int vbSlot = 0; vbSlot < vertexInputs.Length; vbSlot++) { VertexInputDescription bufferInput = vertexInputs[vbSlot]; int numElements = bufferInput.Elements.Length; int currentOffset = 0; for (int i = 0; i < numElements; i++) { var genericElement = bufferInput.Elements[i]; elements[element] = new InputElement( GetSemanticName(genericElement.SemanticType), indicesTracker.GetAndIncrement(genericElement.SemanticType), ConvertGenericFormat(genericElement.ElementFormat), currentOffset, vbSlot, D3DFormats.VeldridToD3DVertexElementInputClass(genericElement.StorageClassifier), genericElement.InstanceStepRate); currentOffset += genericElement.SizeInBytes; element += 1; } } return(new InputLayout(device, shaderBytecode, elements)); }
public static Material CreateMaterial( this ResourceFactory factory, RenderContext rc, string vertexShaderName, string fragmentShaderName, VertexInputDescription vertexInputs, ShaderResourceDescription[] resources) { return(CreateMaterial(factory, rc, vertexShaderName, fragmentShaderName, new[] { vertexInputs }, resources)); }
public OpenGLVertexInput(VertexInputDescription genericInput) { VertexSizeInBytes = genericInput.VertexSizeInBytes; Elements = new OpenGLVertexInputElement[genericInput.Elements.Length]; int offset = 0; for (int i = 0; i < Elements.Length; i++) { var genericElement = genericInput.Elements[i]; Elements[i] = new OpenGLVertexInputElement(genericElement, offset); offset += genericElement.SizeInBytes; } }
private void InitializeContextObjects(RenderContext context) { _currentContext = context; ResourceFactory factory = context.ResourceFactory; _vb = factory.CreateVertexBuffer(VertexPositionNormalTexture.SizeInBytes * _vertices.Length, false); VertexDescriptor desc = new VertexDescriptor( VertexPositionNormalTexture.SizeInBytes, VertexPositionNormalTexture.ElementCount, IntPtr.Zero); _vb.SetVertexData(_vertices, desc); _ib = factory.CreateIndexBuffer(sizeof(ushort) * _indices.Length, false); _ib.SetIndices(_indices); VertexInputDescription materialInputs = new VertexInputDescription( VertexPositionNormalTexture.SizeInBytes, new VertexInputElement[] { new VertexInputElement("in_position", VertexSemanticType.Position, VertexElementFormat.Float3), new VertexInputElement("in_normal", VertexSemanticType.Normal, VertexElementFormat.Float3), new VertexInputElement("in_texCoord", VertexSemanticType.TextureCoordinate, VertexElementFormat.Float2) }); ShaderResourceDescription[] resources = new[] { new ShaderResourceDescription("ProjectionMatrixBuffer", ShaderConstantType.Matrix4x4), new ShaderResourceDescription("ViewMatrixBuffer", ShaderConstantType.Matrix4x4), new ShaderResourceDescription("LightBuffer", Unsafe.SizeOf <DirectionalLightBuffer>()), new ShaderResourceDescription("WorldMatrixBuffer", ShaderConstantType.Matrix4x4), new ShaderResourceDescription("InverseTransposeWorldMatrixBuffer", ShaderConstantType.Matrix4x4), new ShaderResourceDescription("surfaceTexture", ShaderResourceType.Texture), new ShaderResourceDescription("surfaceTexture", ShaderResourceType.Sampler) }; _material = factory.CreateMaterial( context, VertexShaderSource, FragmentShaderSource, materialInputs, resources); _worldBuffer = factory.CreateConstantBuffer(ShaderConstantType.Matrix4x4); _inverseTransposeWorldBuffer = factory.CreateConstantBuffer(ShaderConstantType.Matrix4x4); DeviceTexture2D texture = _texture.CreateDeviceTexture(factory); _textureBinding = factory.CreateShaderTextureBinding(texture); s_wireframeRasterizerState = factory.CreateRasterizerState(FaceCullingMode.None, TriangleFillMode.Wireframe, true, true); }
private void InitializeContextObjects(RenderContext context) { s_currentContext = context; ResourceFactory factory = context.ResourceFactory; s_vb0 = factory.CreateVertexBuffer(12 * s_cubeVertices.Length, false); VertexDescriptor desc = new VertexDescriptor(12, 1, IntPtr.Zero); s_vb0.SetVertexData(s_cubeVertices.Select(vpc => vpc.Position).ToArray(), desc); s_vb1 = factory.CreateVertexBuffer(16 * s_cubeVertices.Length, false); VertexDescriptor desc2 = new VertexDescriptor(16, 1, IntPtr.Zero); s_vb1.SetVertexData(s_cubeVertices.Select(vpc => vpc.Color).ToArray(), desc2); s_ib = factory.CreateIndexBuffer(s_cubeIndices, false); VertexInputDescription materialInputs0 = new VertexInputDescription( 12, new VertexInputElement[] { new VertexInputElement("in_position", VertexSemanticType.Position, VertexElementFormat.Float3), }); VertexInputDescription materialInputs1 = new VertexInputDescription( 16, new VertexInputElement[] { new VertexInputElement("in_color", VertexSemanticType.Color, VertexElementFormat.Float4) }); ShaderResourceDescription[] resources = new[] { new ShaderResourceDescription("ProjectionMatrixBuffer", ShaderConstantType.Matrix4x4), new ShaderResourceDescription("ModelViewMatrixBuffer", ShaderConstantType.Matrix4x4) }; s_material = factory.CreateMaterial( context, VertexShaderSource, FragmentShaderSource, materialInputs0, materialInputs1, resources); }
private void InitializeContextObjects(RenderContext rc) { var factory = rc.ResourceFactory; var mesh = LoadTeapotMesh(); _vertexBuffer = factory.CreateVertexBuffer(mesh.Vertices.Length * VertexPositionNormalTexture.SizeInBytes, false); _vertexBuffer.SetVertexData(mesh.Vertices, new VertexDescriptor(VertexPositionNormalTexture.SizeInBytes, 3, IntPtr.Zero)); _indexBuffer = factory.CreateIndexBuffer(mesh.Indices.Length * sizeof(int), false); _indexBuffer.SetIndices(mesh.Indices); VertexInputDescription materialInputs = new VertexInputDescription( VertexPositionNormalTexture.SizeInBytes, new VertexInputElement[] { new VertexInputElement("in_position", VertexSemanticType.Position, VertexElementFormat.Float3), new VertexInputElement("in_normal", VertexSemanticType.Normal, VertexElementFormat.Float3), new VertexInputElement("in_texCoord", VertexSemanticType.TextureCoordinate, VertexElementFormat.Float2) }); ShaderResourceDescription[] resources = new[] { new ShaderResourceDescription("ProjectionMatrixBuffer", ShaderConstantType.Matrix4x4), new ShaderResourceDescription("ViewMatrixBuffer", ShaderConstantType.Matrix4x4), new ShaderResourceDescription("LightBuffer", Unsafe.SizeOf <DirectionalLightBuffer>()), new ShaderResourceDescription("WorldMatrixBuffer", ShaderConstantType.Matrix4x4), new ShaderResourceDescription("InverseTransposeWorldMatrixBuffer", ShaderConstantType.Matrix4x4), new ShaderResourceDescription("surfaceTexture", ShaderResourceType.Texture), new ShaderResourceDescription("surfaceTexture", ShaderResourceType.Sampler) }; _material = factory.CreateMaterial( rc, "textured-vertex", "lit-frag", materialInputs, resources); _worldBuffer = factory.CreateConstantBuffer(ShaderConstantType.Matrix4x4); _inverseTransposeWorldBuffer = factory.CreateConstantBuffer(ShaderConstantType.Matrix4x4); DeviceTexture2D deviceTex = s_cubeTexture.CreateDeviceTexture(factory); _textureBinding = factory.CreateShaderTextureBinding(deviceTex); }
private VkPipeline CreateNewGraphicsPipeline(ref VkPipelineCacheKey cacheKey) { VkGraphicsPipelineCreateInfo pipelineCI = VkGraphicsPipelineCreateInfo.New(); // RenderPass pipelineCI.renderPass = cacheKey.RenderPass; pipelineCI.subpass = 0; pipelineCI.layout = cacheKey.PipelineLayout; // DynamicState VkPipelineDynamicStateCreateInfo dynamicStateCI = VkPipelineDynamicStateCreateInfo.New(); VkDynamicState *dynamicStates = stackalloc VkDynamicState[2]; dynamicStates[0] = VkDynamicState.Viewport; dynamicStates[1] = VkDynamicState.Scissor; dynamicStateCI.dynamicStateCount = 2; dynamicStateCI.pDynamicStates = dynamicStates; pipelineCI.pDynamicState = &dynamicStateCI; // ColorBlendState VkPipelineColorBlendAttachmentState colorBlendAttachementState = new VkPipelineColorBlendAttachmentState(); colorBlendAttachementState.colorWriteMask = VkColorComponentFlags.R | VkColorComponentFlags.G | VkColorComponentFlags.B | VkColorComponentFlags.A; colorBlendAttachementState.blendEnable = cacheKey.BlendState.IsBlendEnabled; colorBlendAttachementState.srcColorBlendFactor = VkFormats.VeldridToVkBlendFactor(cacheKey.BlendState.SourceColorBlend); colorBlendAttachementState.dstColorBlendFactor = VkFormats.VeldridToVkBlendFactor(cacheKey.BlendState.DestinationColorBlend); colorBlendAttachementState.colorBlendOp = VkFormats.VeldridToVkBlendOp(cacheKey.BlendState.ColorBlendFunction); colorBlendAttachementState.srcAlphaBlendFactor = VkFormats.VeldridToVkBlendFactor(cacheKey.BlendState.SourceAlphaBlend); colorBlendAttachementState.dstAlphaBlendFactor = VkFormats.VeldridToVkBlendFactor(cacheKey.BlendState.DestinationAlphaBlend); colorBlendAttachementState.alphaBlendOp = VkFormats.VeldridToVkBlendOp(cacheKey.BlendState.AlphaBlendFunction); VkPipelineColorBlendStateCreateInfo colorBlendStateCI = VkPipelineColorBlendStateCreateInfo.New(); if (cacheKey.Framebuffer.ColorTexture != null) { colorBlendStateCI.attachmentCount = 1; colorBlendStateCI.pAttachments = &colorBlendAttachementState; colorBlendStateCI.blendConstants_0 = cacheKey.BlendState.BlendFactor.R; colorBlendStateCI.blendConstants_1 = cacheKey.BlendState.BlendFactor.G; colorBlendStateCI.blendConstants_2 = cacheKey.BlendState.BlendFactor.B; colorBlendStateCI.blendConstants_3 = cacheKey.BlendState.BlendFactor.A; pipelineCI.pColorBlendState = &colorBlendStateCI; } // DepthStencilState VkPipelineDepthStencilStateCreateInfo depthStencilStateCI = VkPipelineDepthStencilStateCreateInfo.New(); depthStencilStateCI.depthCompareOp = VkFormats.VeldridToVkDepthComparison(cacheKey.DepthStencilState.DepthComparison); depthStencilStateCI.depthWriteEnable = cacheKey.DepthStencilState.IsDepthWriteEnabled; depthStencilStateCI.depthTestEnable = cacheKey.DepthStencilState.IsDepthEnabled; pipelineCI.pDepthStencilState = &depthStencilStateCI; // MultisampleState VkPipelineMultisampleStateCreateInfo multisampleStateCI = VkPipelineMultisampleStateCreateInfo.New(); multisampleStateCI.rasterizationSamples = VkSampleCountFlags.Count1; pipelineCI.pMultisampleState = &multisampleStateCI; // RasterizationState VkPipelineRasterizationStateCreateInfo rasterizationStateCI = ((VkRasterizerState)cacheKey.RasterizerState).RasterizerStateCreateInfo; rasterizationStateCI.lineWidth = 1f; pipelineCI.pRasterizationState = &rasterizationStateCI; // ViewportState VkPipelineViewportStateCreateInfo viewportStateCI = VkPipelineViewportStateCreateInfo.New(); viewportStateCI.viewportCount = 1; viewportStateCI.scissorCount = 1; pipelineCI.pViewportState = &viewportStateCI; // InputAssemblyState VkPipelineInputAssemblyStateCreateInfo inputAssemblyStateCI = VkPipelineInputAssemblyStateCreateInfo.New(); inputAssemblyStateCI.topology = cacheKey.PrimitiveTopology; pipelineCI.pInputAssemblyState = &inputAssemblyStateCI; // VertexInputState VkPipelineVertexInputStateCreateInfo vertexInputStateCI = VkPipelineVertexInputStateCreateInfo.New(); VertexInputDescription[] inputDescriptions = cacheKey.ShaderSet.InputLayout.InputDescriptions; uint bindingCount = (uint)inputDescriptions.Length; uint attributeCount = (uint)inputDescriptions.Sum(desc => desc.Elements.Length); VkVertexInputBindingDescription * bindingDescs = stackalloc VkVertexInputBindingDescription[(int)bindingCount]; VkVertexInputAttributeDescription *attributeDescs = stackalloc VkVertexInputAttributeDescription[(int)attributeCount]; int targetIndex = 0; int targetLocation = 0; for (int binding = 0; binding < inputDescriptions.Length; binding++) { VertexInputDescription inputDesc = inputDescriptions[binding]; bindingDescs[targetIndex] = new VkVertexInputBindingDescription() { binding = (uint)binding, inputRate = (inputDesc.Elements[0].StorageClassifier == VertexElementInputClass.PerInstance) ? VkVertexInputRate.Instance : VkVertexInputRate.Vertex, stride = ((VkVertexBuffer)cacheKey.VertexBindings[binding]).Stride }; uint currentOffset = 0; for (int location = 0; location < inputDesc.Elements.Length; location++) { VertexInputElement inputElement = inputDesc.Elements[location]; attributeDescs[targetIndex] = new VkVertexInputAttributeDescription() { format = VkFormats.VeldridToVkVertexElementFormat(inputElement.ElementFormat), binding = (uint)binding, location = (uint)(targetLocation + location), offset = currentOffset }; targetIndex += 1; currentOffset += inputElement.SizeInBytes; } targetLocation += inputDesc.Elements.Length; } vertexInputStateCI.vertexBindingDescriptionCount = bindingCount; vertexInputStateCI.pVertexBindingDescriptions = bindingDescs; vertexInputStateCI.vertexAttributeDescriptionCount = attributeCount; vertexInputStateCI.pVertexAttributeDescriptions = attributeDescs; pipelineCI.pVertexInputState = &vertexInputStateCI; // ShaderStage StackList <VkPipelineShaderStageCreateInfo> shaderStageCIs = new StackList <VkPipelineShaderStageCreateInfo>(); VkPipelineShaderStageCreateInfo vertexStage = VkPipelineShaderStageCreateInfo.New(); vertexStage.stage = VkShaderStageFlags.Vertex; vertexStage.module = cacheKey.ShaderSet.VertexShader.ShaderModule; vertexStage.pName = CommonStrings.main; shaderStageCIs.Add(vertexStage); VkPipelineShaderStageCreateInfo fragmentStage = VkPipelineShaderStageCreateInfo.New(); fragmentStage.stage = VkShaderStageFlags.Fragment; fragmentStage.module = cacheKey.ShaderSet.FragmentShader.ShaderModule; fragmentStage.pName = CommonStrings.main; shaderStageCIs.Add(fragmentStage); if (cacheKey.ShaderSet.TessellationControlShader != null) { VkPipelineShaderStageCreateInfo tcStage = VkPipelineShaderStageCreateInfo.New(); tcStage.stage = VkShaderStageFlags.TessellationControl; tcStage.module = cacheKey.ShaderSet.TessellationControlShader.ShaderModule; tcStage.pName = CommonStrings.main; shaderStageCIs.Add(tcStage); } if (cacheKey.ShaderSet.TessellationEvaluationShader != null) { VkPipelineShaderStageCreateInfo teStage = VkPipelineShaderStageCreateInfo.New(); teStage.stage = VkShaderStageFlags.TessellationEvaluation; teStage.module = cacheKey.ShaderSet.TessellationEvaluationShader.ShaderModule; teStage.pName = CommonStrings.main; shaderStageCIs.Add(teStage); } if (cacheKey.ShaderSet.GeometryShader != null) { VkPipelineShaderStageCreateInfo geometryStage = VkPipelineShaderStageCreateInfo.New(); geometryStage.stage = VkShaderStageFlags.Geometry; geometryStage.module = cacheKey.ShaderSet.GeometryShader.ShaderModule; geometryStage.pName = CommonStrings.main; shaderStageCIs.Add(geometryStage); } pipelineCI.stageCount = shaderStageCIs.Count; pipelineCI.pStages = (VkPipelineShaderStageCreateInfo *)shaderStageCIs.Data; VkResult result = vkCreateGraphicsPipelines(_device, VkPipelineCache.Null, 1, ref pipelineCI, null, out VkPipeline ret); CheckResult(result); return(ret); }