/// <summary> /// /// </summary> /// <param name="ps"></param> /// <param name="flags"></param> void EnumFunc(PipelineState ps, SkyFlags flags) { ps.VertexInputElements = VertexInputElement.FromStructure <VertexColorTextureTBN>(); ps.RasterizerState = RasterizerState.CullNone; ps.BlendState = BlendState.Opaque; ps.DepthStencilState = flags.HasFlag(SkyFlags.FOG) ? DepthStencilState.None : DepthStencilState.Readonly; if (flags.HasFlag(SkyFlags.CLOUDS)) { ps.BlendState = BlendState.AlphaBlend; } //if (flags.HasFlag(SkyFlags.RED)) { // ps.BlendState = BlendState.Create(ColorChannels.Red); //} //if (flags.HasFlag(SkyFlags.BLUE)) { // ps.BlendState = BlendState.Create(ColorChannels.Blue); //} //if (flags.HasFlag(SkyFlags.GREEN)) { // ps.BlendState = BlendState.Create(ColorChannels.Green); //} if (flags.HasFlag(SkyFlags.BLUR_CLOUD)) { ps.BlendState = BlendState.AlphaBlend; } }
/// <summary> /// Initializes a new instance of the StateFactory /// </summary> /// <param name="device"></param> /// <param name="enumType"></param> /// <param name="ubershader"></param> /// <param name="vertexInputElements"></param> /// <param name="blendState"></param> /// <param name="rasterizerState"></param> private StateFactory ( Ubershader ubershader, Type enumType, Primitive primitive, VertexInputElement[] vertexInputElements ) : base(ubershader.GraphicsDevice) { this.ubershader = ubershader; Enumerate( enumType, ubershader, (ps,i) => { ps.VertexInputElements = vertexInputElements; ps.Primitive = primitive; } ); }
void EnumAction(PipelineState ps, Flags flag) { ps.Primitive = Primitive.PointList; ps.VertexInputElements = VertexInputElement.FromStructure <ParticleVertex>(); ps.DepthStencilState = DepthStencilState.None; var outputElements = new[] { new VertexOutputElement("SV_POSITION", 0, 0, 4), new VertexOutputElement("COLOR", 0, 0, 4), new VertexOutputElement("COLOR", 1, 0, 4), new VertexOutputElement("TEXCOORD", 0, 0, 4), new VertexOutputElement("TEXCOORD", 1, 0, 4), new VertexOutputElement("TEXCOORD", 2, 0, 4), }; if (flag == Flags.INJECTION || flag == Flags.SIMULATION) { ps.VertexOutputElements = outputElements; } if (flag == Flags.RENDER) { ps.BlendState = BlendState.Screen; ps.RasterizerState = RasterizerState.CullNone; } }
/// <summary> /// /// </summary> /// <param name="ps"></param> /// <param name="flags"></param> void StateEnum(PipelineState ps, Flags flags) { switch (flags) { case Flags.OPAQUE: ps.BlendState = BlendState.Opaque; break; case Flags.ALPHA_BLEND: ps.BlendState = BlendState.AlphaBlend; break; case Flags.ALPHA_BLEND_PREMUL: ps.BlendState = BlendState.AlphaBlendPremul; break; case Flags.ADDITIVE: ps.BlendState = BlendState.Additive; break; case Flags.SCREEN: ps.BlendState = BlendState.Screen; break; case Flags.MULTIPLY: ps.BlendState = BlendState.Multiply; break; case Flags.NEG_MULTIPLY: ps.BlendState = BlendState.NegMultiply; break; case Flags.ALPHA_ONLY: ps.BlendState = BlendState.AlphaMaskWrite; break; } ps.RasterizerState = RasterizerState.CullNone; ps.DepthStencilState = DepthStencilState.None; ps.Primitive = Primitive.TriangleList; ps.VertexInputElements = VertexInputElement.FromStructure(typeof(SpriteVertex)); }
public OpenGLVertexInputElement(VertexInputElement genericElement, int offset) { SizeInBytes = genericElement.SizeInBytes; ElementCount = FormatHelpers.GetElementCount(genericElement.ElementFormat); Type = GetGenericFormatType(genericElement.ElementFormat); Offset = offset; Normalized = genericElement.SemanticType == VertexSemanticType.Color && genericElement.ElementFormat == VertexElementFormat.Byte4; InstanceStepRate = genericElement.InstanceStepRate; }
/// <summary> /// /// </summary> /// <param name="ps"></param> /// <param name="flags"></param> void EnumFunc(PipelineState ps, SkyFlags flags) { ps.VertexInputElements = VertexInputElement.FromStructure <SkyVertex>(); // do not cull triangles for both for RH and LH coordinates // for direct view and cubemaps. ps.RasterizerState = RasterizerState.CullNone; ps.BlendState = BlendState.Opaque; ps.DepthStencilState = flags.HasFlag(SkyFlags.FOG) ? DepthStencilState.None : DepthStencilState.Readonly; }
/// <summary> /// Initializes a new instance of the StateFactory /// </summary> /// <param name="device"></param> /// <param name="ubershader"></param> private StateFactory ( Ubershader ubershader, Type enumType, Primitive primitive, VertexInputElement[] vertexInputElements, BlendState blendState, RasterizerState rasterizerState ) : base(ubershader.GraphicsDevice) { this.ubershader = ubershader; Enumerate( enumType, ubershader, (ps,i) => { ps.Primitive = primitive; ps.VertexInputElements = vertexInputElements; ps.BlendState = blendState; ps.RasterizerState = rasterizerState; } ); }
/// <summary> /// /// </summary> /// <param name="ps"></param> /// <param name="flags"></param> void Enum(PipelineState ps, RenderFlags flags) { ps.Primitive = Primitive.LineList; ps.VertexInputElements = VertexInputElement.FromStructure(typeof(LineVertex)); ps.RasterizerState = RasterizerState.CullNone; if (flags.HasFlag(RenderFlags.SOLID)) { ps.BlendState = BlendState.Opaque; ps.DepthStencilState = DepthStencilState.Default; } if (flags.HasFlag(RenderFlags.GHOST)) { ps.BlendState = BlendState.AlphaBlend; ps.DepthStencilState = DepthStencilState.None; } }
/// <summary> /// /// </summary> void LoadContent() { SafeDispose(ref factory); SafeDispose(ref vertexBuffers); SafeDispose(ref indexBuffers); surfaceShader = Game.Content.Load <Ubershader>("surface"); factory = new StateFactory(surfaceShader, typeof(SurfaceFlags), Primitive.TriangleList, VertexInputElement.FromStructure <VertexColorTextureTBN>()); scene = Game.Content.Load <Scene>(@"Scenes\testScene"); vertexBuffers = scene.Meshes .Select(mesh => VertexBuffer.Create(Game.GraphicsDevice, mesh.Vertices.Select(v => VertexColorTextureTBN.Convert(v)).ToArray())) .ToArray(); indexBuffers = scene.Meshes .Select(mesh => IndexBuffer.Create(Game.GraphicsDevice, mesh.GetIndices())) .ToArray(); surfaceProps = scene.Materials .Select(mtrl => new SurfaceProperties() { Diffuse = LoadTexture2D(mtrl.TexturePath, "|srgb", defaultDiffuse), Specular = LoadTexture2D(mtrl.TexturePath, "_spec", defaultSpecular), NormalMap = LoadTexture2D(mtrl.TexturePath, "_local", defaultNormalMap), Emission = LoadTexture2D(mtrl.TexturePath, "_glow|srgb", defaultEmission), }) .ToArray(); }
public MySceneDrawer(GraphicsDevice device, Scene scene) : base(device, scene) { constBuffer = new ConstantBuffer(GraphicsDevice, typeof(CBData)); constBufferBones = new ConstantBuffer(GraphicsDevice, typeof(Matrix), BoneCount); uberShader = Game.Content.Load <Ubershader>("render"); factory = new StateFactory(uberShader, typeof(RenderFlags), Primitive.TriangleList, VertexInputElement.FromStructure <VertexColorSkin>()); boneTransforms = new Matrix[BoneCount]; }
/// <summary> /// Load content /// </summary> public void LoadContent() { uberShader = Content.Load <Ubershader>("render"); factory = new StateFactory(uberShader, typeof(RenderFlags), Primitive.TriangleList, VertexInputElement.FromStructure <CubeVertex>()); texture = Content.Load <Texture2D>(@"Scenes\lena"); vb = new VertexBuffer(GraphicsDevice, typeof(CubeVertex), 24); ib = new IndexBuffer(GraphicsDevice, 36); // create a new space with physics space = new Space(); // update gravity force space.ForceUpdater.Gravity = new Vector3BEPU(0, -9.81f, 0); // add ground, ground has infinite mass Box ground = new Box(new Vector3BEPU(0, 0, 0), 50, 1, 50); space.Add(ground); // create boxes with random position and add color as a tag, then add box to space for (int i = 0; i < numberOfBoxes; i++) { Vector3Fusion vector = RandomExt.NextVector3(random, new Vector3Fusion(-10, 20, -10), new Vector3Fusion(10, 80, 10)); Box box = new Box(new Vector3BEPU(vector.X, vector.Y, vector.Z), 1, 1, 1, 1); box.Tag = RandomExt.NextColor(random); space.Add(box); } }
void LoadContent() { SafeDispose(ref factory); //texture = new Texture2D( GraphicsDevice, File.ReadAllBytes(@"D:\image.png"), false ); texture = Content.Load <Texture2D>("lena.tga"); ubershader = Content.Load <Ubershader>("test.hlsl"); factory = new StateFactory(ubershader, typeof(UberFlags), Primitive.TriangleList, VertexInputElement.FromStructure(typeof(Vertex))); }
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); }
/// <summary> /// Creates pipeline state factory /// </summary> /// <param name="type"></param> /// <param name="primitive"></param> /// <param name="vertexInputElements"></param> /// <returns></returns> public StateFactory CreateFactory ( Type type, Primitive primitive, VertexInputElement[] vertexInputElements ) { return CreateFactory( type, (ps,i) => { ps.VertexInputElements = vertexInputElements; ps.Primitive = primitive; }); }
public PipelineState ProducePipelineState(GlobeFlags flags, Primitive primitive, BlendState blendState, RasterizerState rasterizerState, DepthStencilState depthStencilState) { var t = Tuple.CreateNew(primitive, blendState, depthStencilState, rasterizerState, flags.HasFlag(GlobeFlags.USE_CARTCOORDS)); if (factories.ContainsKey(t)) { return(factories[t][(int)flags]); } var f = new StateFactory(shader, typeof(GlobeFlags), primitive, flags.HasFlag(GlobeFlags.USE_CARTCOORDS) ? VertexInputElement.FromStructure <CartVert>() : VertexInputElement.FromStructure <GeoVert>(), blendState, rasterizerState, depthStencilState); factories.Add(t, f); return(f[(int)flags]); }
void EnumFunc(GlobeFlags flags, PipelineState ps) { ps.VertexInputElements = flags.HasFlag(GlobeFlags.USE_CARTCOORDS) ? VertexInputElement.FromStructure <CartVert>() : VertexInputElement.FromStructure <GeoVert>(); }
/// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void InstancingDemo_Reloading(object sender, EventArgs e) { SafeDispose(ref factory); us = Content.Load <Ubershader>("test"); factory = new StateFactory(us, typeof(UberFlags), Primitive.TriangleList, VertexInputElement.FromStructure(typeof(Vertex)), BlendState.Additive, RasterizerState.CullNone, DepthStencilState.None); tex = Content.Load <Texture2D>("block"); }
/// <summary> /// Creates pipeline state factory /// </summary> public StateFactory CreateFactory ( Type type, Primitive primitive, VertexInputElement[] vertexInputElements, BlendState blendState, RasterizerState rasterizerState, DepthStencilState depthStencilState ) { return CreateFactory( type, (ps,i) => { ps.Primitive = primitive; ps.VertexInputElements = vertexInputElements; ps.BlendState = blendState; ps.RasterizerState = rasterizerState; ps.DepthStencilState = depthStencilState; }); }