private void CreateShaders() { var vertexLayout = new VertexLayoutDescription ( 16, 0, new VertexElementDescription[] { new VertexElementDescription("Position", VertexElementFormat.Float2, VertexElementSemantic.Position), new VertexElementDescription("VTex", VertexElementFormat.Float2, VertexElementSemantic.TextureCoordinate) } ); var uniformDescriptions = new ResourceLayoutElementDescription[2][]; uniformDescriptions[0] = new ResourceLayoutElementDescription[] { new ResourceLayoutElementDescription("Factors", ResourceKind.UniformBuffer, ShaderStages.Fragment) }; uniformDescriptions[1] = new ResourceLayoutElementDescription[] { new ResourceLayoutElementDescription("Sampler", ResourceKind.TextureReadOnly, ShaderStages.Fragment), new ResourceLayoutElementDescription("Texture", ResourceKind.Sampler, ShaderStages.Fragment) }; _shaderPackage = _shaderLoader.CreateShaderPackage("Vertex2D", AssetSourceEnum.Embedded, "ColourFragment", AssetSourceEnum.Embedded, vertexLayout, uniformDescriptions); }
private List <IDisposable> createTransformationPipelineUniform() { _transformationPipelineBuffer = _factory.CreateBuffer(new BufferDescription(192, BufferUsage.UniformBuffer | BufferUsage.Dynamic)); ResourceLayoutElementDescription resourceLayoutElementDescription = new ResourceLayoutElementDescription("transformPipeline", ResourceKind.UniformBuffer, ShaderStages.Vertex); ResourceLayoutElementDescription[] resourceLayoutElementDescriptions = { resourceLayoutElementDescription }; ResourceLayoutDescription resourceLayoutDescription = new ResourceLayoutDescription(resourceLayoutElementDescriptions); BindableResource[] bindableResources = new BindableResource[] { _transformationPipelineBuffer }; _transformationPipelineResourceLayout = _factory.CreateResourceLayout(resourceLayoutDescription); ResourceSetDescription resourceSetDescription = new ResourceSetDescription(_transformationPipelineResourceLayout, bindableResources); _transformationPipelineResourceSet = _factory.CreateResourceSet(resourceSetDescription); //_graphicsDevice.UpdateBuffer(_transformationPipelineBuffer,0,_camera.ViewMatrix); GraphicsDevice.UpdateBuffer(_transformationPipelineBuffer, 64, Camera.ProjectionMatrix); return(new List <IDisposable>() { _transformationPipelineBuffer, _transformationPipelineResourceLayout, _transformationPipelineResourceSet }); }
private void CreateShaders() { var vertexLayout = new VertexLayoutDescription ( 16, 0, new VertexElementDescription[] { new VertexElementDescription("Position", VertexElementFormat.Float2, VertexElementSemantic.Position), new VertexElementDescription("VTex", VertexElementFormat.Float2, VertexElementSemantic.TextureCoordinate) } ); var uniformDescriptions = new ResourceLayoutElementDescription[8][]; uniformDescriptions[0] = new ResourceLayoutElementDescription[] { new ResourceLayoutElementDescription("Sampler_Source", ResourceKind.TextureReadOnly, ShaderStages.Fragment), new ResourceLayoutElementDescription("Texture_Source", ResourceKind.Sampler, ShaderStages.Fragment) }; uniformDescriptions[1] = new ResourceLayoutElementDescription[] { new ResourceLayoutElementDescription("Sampler_Noise", ResourceKind.TextureReadOnly, ShaderStages.Fragment), new ResourceLayoutElementDescription("Texture_Noise", ResourceKind.Sampler, ShaderStages.Fragment) }; uniformDescriptions[2] = new ResourceLayoutElementDescription[] { new ResourceLayoutElementDescription("Sampler_ScanlineMask", ResourceKind.TextureReadOnly, ShaderStages.Fragment), new ResourceLayoutElementDescription("Texture_ScanlineMask", ResourceKind.Sampler, ShaderStages.Fragment) }; uniformDescriptions[3] = new ResourceLayoutElementDescription[] { new ResourceLayoutElementDescription("PixellateFactors", ResourceKind.UniformBuffer, ShaderStages.Fragment) }; uniformDescriptions[4] = new ResourceLayoutElementDescription[] { new ResourceLayoutElementDescription("EdgeDetectionFactors", ResourceKind.UniformBuffer, ShaderStages.Fragment) }; uniformDescriptions[5] = new ResourceLayoutElementDescription[] { new ResourceLayoutElementDescription("StaticFactors", ResourceKind.UniformBuffer, ShaderStages.Fragment) }; uniformDescriptions[6] = new ResourceLayoutElementDescription[] { new ResourceLayoutElementDescription("OldMovieFactors", ResourceKind.UniformBuffer, ShaderStages.Fragment) }; uniformDescriptions[7] = new ResourceLayoutElementDescription[] { new ResourceLayoutElementDescription("CrtEffectFactors", ResourceKind.UniformBuffer, ShaderStages.Fragment) }; _shaderPackage = _shaderLoader.CreateShaderPackage("Vertex2D", AssetSourceEnum.Embedded, "StyleFragment", AssetSourceEnum.Embedded, vertexLayout, uniformDescriptions); }
private void AssertEqual(ResourceLayoutElementDescription a, ResourceLayoutElementDescription b) { Assert.Equal(a.Name, b.Name); Assert.Equal(a.Kind, b.Kind); Assert.Equal(a.Options, b.Options); Assert.Equal(a.Stages, b.Stages); }
public static ResourceLayout GenerateResourceLayout(DisposeCollectorResourceFactory factory, string name, ResourceKind resourceKind, ShaderStages shaderStages) { var resourceLayoutElementDescription = new ResourceLayoutElementDescription(name, resourceKind, shaderStages); ResourceLayoutElementDescription[] resourceLayoutElementDescriptions = { resourceLayoutElementDescription }; var resourceLayoutDescription = new ResourceLayoutDescription(resourceLayoutElementDescriptions); return(factory.CreateResourceLayout(resourceLayoutDescription)); }
public _EffectTexture(GraphicsDevice device, string inputName) { Name = inputName; _ExtDevice = device; // Input var texDesc = new ResourceLayoutElementDescription(inputName, ResourceKind.TextureReadOnly, ShaderStages.Fragment); _OwnedTextureLayout = device.ResourceFactory.CreateResourceLayout(new ResourceLayoutDescription(texDesc)); }
public _EffectSampler(GraphicsDevice device, string samplerName) { Name = samplerName; _ExtDevice = device; var smpDesc = new ResourceLayoutElementDescription(samplerName, ResourceKind.Sampler, ShaderStages.Fragment); _OwnedSamplerLayout = device.ResourceFactory.CreateResourceLayout(new ResourceLayoutDescription(smpDesc)); _OwnedSamplerResourceSets = new _EffectSamplers(device, _OwnedSamplerLayout); }
public Effect( GraphicsDevice graphicsDevice, string shaderName, params VertexLayoutDescription[] vertexDescriptors) { GraphicsDevice = graphicsDevice; ID = _nextID++; var shaderCodeExtension = GetBytecodeExtension(graphicsDevice.BackendType); Shader CreateShader(ShaderStages shaderStage, string entryPoint) { var embeddedResourceName = $"OpenSage.Graphics.Shaders.Compiled.{shaderName}-{shaderStage.ToString().ToLowerInvariant()}{shaderCodeExtension}"; using (var shaderStream = typeof(Effect).Assembly.GetManifestResourceStream(embeddedResourceName)) { var shaderBytecode = shaderStream.ReadAllBytes(); var shader = graphicsDevice.ResourceFactory.CreateShader(new ShaderDescription(shaderStage, shaderBytecode, entryPoint)); shader.Name = shaderName; return(shader); } } _vertexShader = AddDisposable(CreateShader(ShaderStages.Vertex, "VS")); _pixelShader = AddDisposable(CreateShader(ShaderStages.Fragment, "PS")); _cachedPipelineStates = new Dictionary <EffectPipelineStateHandle, Pipeline>(); _vertexDescriptors = vertexDescriptors; var shaderDefinition = ShaderDefinitions.GetShaderDefinition(shaderName); _parameters = new Dictionary <string, EffectParameter>(); _resourceLayouts = new ResourceLayout[shaderDefinition.ResourceBindings.Count]; for (var i = 0u; i < shaderDefinition.ResourceBindings.Count; i++) { var resourceBinding = shaderDefinition.ResourceBindings[(int)i]; var resourceLayoutDescription = new ResourceLayoutElementDescription( resourceBinding.Name, resourceBinding.Type, resourceBinding.Stages); var parameter = AddDisposable(new EffectParameter( graphicsDevice, resourceBinding, resourceLayoutDescription, i)); _parameters[parameter.Name] = parameter; _resourceLayouts[i] = parameter.ResourceLayout; } }
public _EffectUniform(GraphicsDevice device, string name, ShaderStages stage, int byteCount) { _Device = device; Name = name; var desc = new ResourceLayoutElementDescription(name, ResourceKind.UniformBuffer, stage); _ByteCount = byteCount; _DeviceBuffer = device.ResourceFactory.CreateBuffer(new BufferDescription((uint)byteCount, BufferUsage.UniformBuffer)); _ResourceLayout = device.ResourceFactory.CreateResourceLayout(new ResourceLayoutDescription(desc)); _ResourceSet = device.ResourceFactory.CreateResourceSet(new ResourceSetDescription(_ResourceLayout, _DeviceBuffer)); }
public Effect( GraphicsDevice graphicsDevice, string shaderName, VertexLayoutDescription[] vertexDescriptors) { _graphicsDevice = graphicsDevice; ID = _nextID++; const string shaderNamespace = "OpenSage.Graphics.Shaders.Compiled"; var shaderCodeExtension = GetBytecodeExtension(graphicsDevice.BackendType); using (var shaderStream = typeof(Effect).Assembly.GetManifestResourceStream($"{shaderNamespace}.{shaderName}-vertex{shaderCodeExtension}")) { var vertexShaderBytecode = shaderStream.ReadAllBytes(); _vertexShader = AddDisposable(graphicsDevice.ResourceFactory.CreateShader(new ShaderDescription(ShaderStages.Vertex, vertexShaderBytecode, "VS"))); } using (var shaderStream = typeof(Effect).Assembly.GetManifestResourceStream($"{shaderNamespace}.{shaderName}-fragment{shaderCodeExtension}")) { var pixelShaderBytecode = shaderStream.ReadAllBytes(); _pixelShader = AddDisposable(graphicsDevice.ResourceFactory.CreateShader(new ShaderDescription(ShaderStages.Fragment, pixelShaderBytecode, "PS"))); } _cachedPipelineStates = new Dictionary <EffectPipelineStateHandle, Pipeline>(); _vertexDescriptors = vertexDescriptors; var shaderDefinition = ShaderDefinitions.GetShaderDefinition(shaderName); _parameters = new Dictionary <string, EffectParameter>(); _resourceLayouts = new ResourceLayout[shaderDefinition.ResourceBindings.Length]; for (var i = 0u; i < shaderDefinition.ResourceBindings.Length; i++) { var resourceBinding = shaderDefinition.ResourceBindings[i]; var resourceLayoutDescription = new ResourceLayoutElementDescription( resourceBinding.Name, resourceBinding.Type, resourceBinding.Stages); var parameter = AddDisposable(new EffectParameter( graphicsDevice, resourceBinding, resourceLayoutDescription, i)); _parameters[parameter.Name] = parameter; _resourceLayouts[i] = parameter.ResourceLayout; } }
internal Uniform(string name, BufferUsage bufferUsage, ShaderStages shaderStages, ResourceLayoutElementOptions options) { BufferUsage = bufferUsage; ShaderStages = shaderStages; ResourceLayoutElementDescription = new ResourceLayoutElementDescription( name, ResourceKind.UniformBuffer, ShaderStages, options); }
private ResourceSet CreateFragmentShaderUniformResourceSet(DeviceBuffer buffer, string elementName) { var resourceLayoutElementDescription = new ResourceLayoutElementDescription(elementName, ResourceKind.UniformBuffer, ShaderStages.Fragment); var resourceLayout = _systemComponents.Factory.CreateResourceLayout( new ResourceLayoutDescription( resourceLayoutElementDescription ) ); return(_systemComponents.Factory.CreateResourceSet( new ResourceSetDescription(resourceLayout, buffer) )); }
private void CreateSizeDependentResources() { renderTargetColorTexture = factory.CreateTexture(TextureDescription.Texture2D( width, height, 1, 1, PixelFormat.R8_G8_B8_A8_UNorm, TextureUsage.RenderTarget | TextureUsage.Sampled)); renderTargetColorTextureView = factory.CreateTextureView(renderTargetColorTexture); renderTargetDepthTexture = factory.CreateTexture(TextureDescription.Texture2D( width, height, 1, 1, PixelFormat.R32_Float, TextureUsage.DepthStencil)); renderTargetFramebuffer = factory.CreateFramebuffer(new FramebufferDescription(renderTargetDepthTexture, renderTargetColorTexture)); // final render pipeline ResourceLayoutElementDescription[] layoutDescriptions = new ResourceLayoutElementDescription[] { new ResourceLayoutElementDescription("ScreenMap", ResourceKind.TextureReadOnly, ShaderStages.Fragment), new ResourceLayoutElementDescription("ScreenMapSampler", ResourceKind.Sampler, ShaderStages.Fragment), }; BindableResource[] bindableResources = new BindableResource[] { renderTargetColorTextureView, graphicsDevice.PointSampler, }; resourceLayout = factory.CreateResourceLayout(new ResourceLayoutDescription(layoutDescriptions)); pipeline = factory.CreateGraphicsPipeline( new GraphicsPipelineDescription( BlendStateDescription.SingleOverrideBlend, new DepthStencilStateDescription( depthTestEnabled: false, depthWriteEnabled: false, comparisonKind: ComparisonKind.Always), new RasterizerStateDescription( cullMode: FaceCullMode.Back, fillMode: PolygonFillMode.Solid, frontFace: FrontFace.Clockwise, depthClipEnabled: false, scissorTestEnabled: false), PrimitiveTopology.TriangleList, new ShaderSetDescription( vertexLayouts: new VertexLayoutDescription[] { new VertexLayoutDescription( new VertexElementDescription("Position", VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float3) ) }, shaders: CreateShaders(finalVertexCode, finalFragmentCode)), new ResourceLayout[] { resourceLayout }, swapchain.Framebuffer.OutputDescription) ); resourceSet = factory.CreateResourceSet(new ResourceSetDescription(resourceLayout, bindableResources)); }
private void CreateShadersAndFactorsUniform() { var vertexLayout = new VertexLayoutDescription ( 16, 0, new VertexElementDescription[] { new VertexElementDescription("Position", VertexElementFormat.Float2, VertexElementSemantic.Position), new VertexElementDescription("VTex", VertexElementFormat.Float2, VertexElementSemantic.TextureCoordinate) } ); var uniformDescriptions = new ResourceLayoutElementDescription[3][]; uniformDescriptions[0] = new ResourceLayoutElementDescription[] { new ResourceLayoutElementDescription("UniformBlock", ResourceKind.UniformBuffer, ShaderStages.Fragment) }; uniformDescriptions[1] = new ResourceLayoutElementDescription[] { new ResourceLayoutElementDescription("Sampler_Source", ResourceKind.TextureReadOnly, ShaderStages.Fragment), new ResourceLayoutElementDescription("Texture_Source", ResourceKind.Sampler, ShaderStages.Fragment) }; uniformDescriptions[2] = new ResourceLayoutElementDescription[] { new ResourceLayoutElementDescription("Sampler_Processed", ResourceKind.TextureReadOnly, ShaderStages.Fragment), new ResourceLayoutElementDescription("Texture_Processed", ResourceKind.Sampler, ShaderStages.Fragment) }; _shaderPackage = _shaderLoader.CreateShaderPackage("Vertex2D", AssetSourceEnum.Embedded, "BloomMixFragment", AssetSourceEnum.Embedded, vertexLayout, uniformDescriptions); var uniformBlockResourceLayoutDescription = uniformDescriptions[0][0]; _uniformBlockBuffer = _systemComponents.Factory.CreateBuffer(new BufferDescription(MixingShaderFactors.SizeInBytes, BufferUsage.UniformBuffer | BufferUsage.Dynamic)); var uniformBlockResourceLayout = _systemComponents.Factory.CreateResourceLayout( new ResourceLayoutDescription( uniformBlockResourceLayoutDescription ) ); _uniformBlockResourceSet = _systemComponents.Factory.CreateResourceSet( new ResourceSetDescription(uniformBlockResourceLayout, _uniformBlockBuffer) ); }
private void CreateBuffer() { _factorsBuffer = _systemComponents.Factory.CreateBuffer(new BufferDescription(ColourEffectFactors.SizeInBytes, BufferUsage.UniformBuffer | BufferUsage.Dynamic)); //Duplicated across here and renderer... var resourceLayoutElementDescription = new ResourceLayoutElementDescription("Factors", ResourceKind.UniformBuffer, ShaderStages.Fragment); var resourceLayout = _systemComponents.Factory.CreateResourceLayout( new ResourceLayoutDescription( resourceLayoutElementDescription ) ); FactorsResourceSet = _systemComponents.Factory.CreateResourceSet( new ResourceSetDescription(resourceLayout, _factorsBuffer) ); }
private void CreateShadersAndFactorsUniform() { var vertexLayout = new VertexLayoutDescription ( 16, 0, new VertexElementDescription[] { new VertexElementDescription("Position", VertexElementFormat.Float2, VertexElementSemantic.Position), new VertexElementDescription("VTex", VertexElementFormat.Float2, VertexElementSemantic.TextureCoordinate) } ); var uniformDescriptions = new ResourceLayoutElementDescription[3][]; uniformDescriptions[0] = new ResourceLayoutElementDescription[] { new ResourceLayoutElementDescription("DistortionFactor", ResourceKind.UniformBuffer, ShaderStages.Fragment) }; uniformDescriptions[1] = new ResourceLayoutElementDescription[] { new ResourceLayoutElementDescription("SamplerGradientMap", ResourceKind.TextureReadOnly, ShaderStages.Fragment), new ResourceLayoutElementDescription("TextureGradientMap", ResourceKind.Sampler, ShaderStages.Fragment) }; uniformDescriptions[2] = new ResourceLayoutElementDescription[] { new ResourceLayoutElementDescription("SamplerImage", ResourceKind.TextureReadOnly, ShaderStages.Fragment), new ResourceLayoutElementDescription("TextureImage", ResourceKind.Sampler, ShaderStages.Fragment) }; _shaderPackage = _shaderLoader.CreateShaderPackage("Vertex2D", AssetSourceEnum.Embedded, "DistortionFragment", AssetSourceEnum.Embedded, vertexLayout, uniformDescriptions); _distortionFactorBuffer = _systemComponents.Factory.CreateBuffer(new BufferDescription(DistortionFactorUniform.SizeInBytes, BufferUsage.UniformBuffer | BufferUsage.Dynamic)); var distortionFactorBufferResourceLayout = _systemComponents.Factory.CreateResourceLayout( new ResourceLayoutDescription( uniformDescriptions[0] ) ); _distortionFactorUniformResourceSet = _systemComponents.Factory.CreateResourceSet( new ResourceSetDescription(distortionFactorBufferResourceLayout, _distortionFactorBuffer) ); }
private void CreateShaders() { var vertexLayout = new VertexLayoutDescription ( 32, 0, new VertexElementDescription[] { new VertexElementDescription("VertPosition", VertexElementFormat.Float3, VertexElementSemantic.Position), new VertexElementDescription("VertNormal", VertexElementFormat.Float3, VertexElementSemantic.Normal), new VertexElementDescription("VertTexCoord", VertexElementFormat.Float2, VertexElementSemantic.TextureCoordinate) } ); var uniformDescriptions = new ResourceLayoutElementDescription[5][]; uniformDescriptions[0] = new ResourceLayoutElementDescription[] { new ResourceLayoutElementDescription("VertexUniforms", ResourceKind.UniformBuffer, ShaderStages.Vertex) }; uniformDescriptions[1] = new ResourceLayoutElementDescription[] { new ResourceLayoutElementDescription("FragUniforms", ResourceKind.UniformBuffer, ShaderStages.Fragment) }; uniformDescriptions[2] = new ResourceLayoutElementDescription[] { new ResourceLayoutElementDescription("Texture", ResourceKind.TextureReadOnly, ShaderStages.Fragment), new ResourceLayoutElementDescription("Sampler", ResourceKind.Sampler, ShaderStages.Fragment) }; uniformDescriptions[3] = new ResourceLayoutElementDescription[] { new ResourceLayoutElementDescription("LightProperties", ResourceKind.UniformBuffer, ShaderStages.Fragment) }; uniformDescriptions[4] = new ResourceLayoutElementDescription[] { new ResourceLayoutElementDescription("LightsUniformBlock", ResourceKind.UniformBuffer, ShaderStages.Fragment) }; _shaderPackage = _shaderLoader.CreateShaderPackage("Vertex3D", AssetSourceEnum.Embedded, "MeshFragment", AssetSourceEnum.Embedded, vertexLayout, uniformDescriptions); }
internal static void ValidateResourceSet(GraphicsDevice gd, ref ResourceSetDescription description) { ResourceLayoutElementDescription[] elements = description.Layout.Description.Elements; BindableResource[] resources = description.BoundResources; if (elements.Length != resources.Length) { throw new VeldridException( $"The number of resources specified ({resources.Length}) must be equal to the number of resources in the {nameof(ResourceLayout)} ({elements.Length})."); } for (uint i = 0; i < elements.Length; i++) { ValidateResourceKind(elements[i].Kind, resources[i], i); } for (int i = 0; i < description.Layout.Description.Elements.Length; i++) { ResourceLayoutElementDescription element = description.Layout.Description.Elements[i]; if (element.Kind == ResourceKind.UniformBuffer || element.Kind == ResourceKind.StructuredBufferReadOnly || element.Kind == ResourceKind.StructuredBufferReadWrite) { DeviceBufferRange range = Util.GetBufferRange(description.BoundResources[i], 0); if (!gd.Features.BufferRangeBinding && (range.Offset != 0 || range.SizeInBytes != range.Buffer.SizeInBytes)) { throw new VeldridException($"The {nameof(DeviceBufferRange)} in slot {i} uses a non-zero offset or less-than-full size, " + $"which requires {nameof(GraphicsDeviceFeatures)}.{nameof(GraphicsDeviceFeatures.BufferRangeBinding)}."); } uint alignment = element.Kind == ResourceKind.UniformBuffer ? gd.UniformBufferMinOffsetAlignment : gd.StructuredBufferMinOffsetAlignment; if ((range.Offset % alignment) != 0) { throw new VeldridException($"The {nameof(DeviceBufferRange)} in slot {i} has an invalid offset: {range.Offset}. " + $"The offset for this buffer must be a multiple of {alignment}."); } } } }
private void CreateShaders() { var vertexLayout = new VertexLayoutDescription ( 56, 0, new VertexElementDescription[] { new VertexElementDescription("isWorld", VertexElementFormat.UInt1, VertexElementSemantic.TextureCoordinate), new VertexElementDescription("TexuringType", VertexElementFormat.UInt1, VertexElementSemantic.TextureCoordinate), new VertexElementDescription("Position", VertexElementFormat.Float3, VertexElementSemantic.Position), new VertexElementDescription("Color", VertexElementFormat.Float4, VertexElementSemantic.Color), new VertexElementDescription("TexCoord0", VertexElementFormat.Float2, VertexElementSemantic.TextureCoordinate), new VertexElementDescription("TexCoord1", VertexElementFormat.Float2, VertexElementSemantic.TextureCoordinate), new VertexElementDescription("TexWeight0", VertexElementFormat.Float1, VertexElementSemantic.TextureCoordinate) } ); var uniformDescriptions = new ResourceLayoutElementDescription[3][]; //Thes resource layouts will match those that are used to create the resouce sets for the camera matrices and textures. //It may be useful to feed a fixed version to both higher up. Also, I am assuming the string label means nothing as //For textures they can be either 0 or 1.. anyway, i'm sure i get headaches later when using different graphics api backends uniformDescriptions[0] = new ResourceLayoutElementDescription[] { new ResourceLayoutElementDescription("WorldViewProjection", ResourceKind.UniformBuffer, ShaderStages.Vertex) }; uniformDescriptions[1] = new ResourceLayoutElementDescription[] { new ResourceLayoutElementDescription("texSampler0", ResourceKind.TextureReadOnly, ShaderStages.Fragment), new ResourceLayoutElementDescription("tex0", ResourceKind.Sampler, ShaderStages.Fragment) }; uniformDescriptions[2] = new ResourceLayoutElementDescription[] { new ResourceLayoutElementDescription("texSampler1", ResourceKind.TextureReadOnly, ShaderStages.Fragment), new ResourceLayoutElementDescription("tex1", ResourceKind.Sampler, ShaderStages.Fragment) }; _shaderPackage = _shaderLoader.CreateShaderPackage("DrawingVertex", AssetSourceEnum.Embedded, "DrawingFragment", AssetSourceEnum.Embedded, vertexLayout, uniformDescriptions); }
private void CreateShaders() { var vertexLayout = new VertexLayoutDescription ( 56, 0, new VertexElementDescription[] { new VertexElementDescription("isWorld", VertexElementFormat.UInt1, VertexElementSemantic.TextureCoordinate), new VertexElementDescription("TexuringType", VertexElementFormat.UInt1, VertexElementSemantic.TextureCoordinate), new VertexElementDescription("Position", VertexElementFormat.Float3, VertexElementSemantic.Position), new VertexElementDescription("Color", VertexElementFormat.Float4, VertexElementSemantic.Color), new VertexElementDescription("TexCoord0", VertexElementFormat.Float2, VertexElementSemantic.TextureCoordinate), new VertexElementDescription("TexCoord1", VertexElementFormat.Float2, VertexElementSemantic.TextureCoordinate), new VertexElementDescription("TexWeight0", VertexElementFormat.Float1, VertexElementSemantic.TextureCoordinate) } ); var uniformDescriptions = new ResourceLayoutElementDescription[3][]; uniformDescriptions[0] = new ResourceLayoutElementDescription[] { new ResourceLayoutElementDescription("WorldViewProjection", ResourceKind.UniformBuffer, ShaderStages.Vertex) }; uniformDescriptions[1] = new ResourceLayoutElementDescription[] { new ResourceLayoutElementDescription("texSampler0", ResourceKind.TextureReadOnly, ShaderStages.Fragment), new ResourceLayoutElementDescription("tex0", ResourceKind.Sampler, ShaderStages.Fragment) }; uniformDescriptions[2] = new ResourceLayoutElementDescription[] { new ResourceLayoutElementDescription("texSampler1", ResourceKind.TextureReadOnly, ShaderStages.Fragment), new ResourceLayoutElementDescription("tex1", ResourceKind.Sampler, ShaderStages.Fragment) }; _shaderPackage = _shaderLoader.CreateShaderPackage("DrawingVertex", AssetSourceEnum.Embedded, "DistortionHeightFragment", AssetSourceEnum.Embedded, vertexLayout, uniformDescriptions); }
private void ProcessResourceSetLayouts(ResourceLayout[] layouts) { int resourceLayoutCount = layouts.Length; _setInfos = new SetBindingsInfo[resourceLayoutCount]; int lastTextureLocation = -1; int relativeTextureIndex = -1; int relativeImageIndex = -1; uint storageBlockIndex = 0; // Tracks OpenGL ES storage buffers. for (uint setSlot = 0; setSlot < resourceLayoutCount; setSlot++) { ResourceLayout setLayout = layouts[setSlot]; OpenGLResourceLayout glSetLayout = Util.AssertSubtype <ResourceLayout, OpenGLResourceLayout>(setLayout); ResourceLayoutElementDescription[] resources = glSetLayout.Elements; Dictionary <uint, OpenGLUniformBinding> uniformBindings = new Dictionary <uint, OpenGLUniformBinding>(); Dictionary <uint, OpenGLTextureBindingSlotInfo> textureBindings = new Dictionary <uint, OpenGLTextureBindingSlotInfo>(); Dictionary <uint, OpenGLSamplerBindingSlotInfo> samplerBindings = new Dictionary <uint, OpenGLSamplerBindingSlotInfo>(); Dictionary <uint, OpenGLShaderStorageBinding> storageBufferBindings = new Dictionary <uint, OpenGLShaderStorageBinding>(); List <int> samplerTrackedRelativeTextureIndices = new List <int>(); for (uint i = 0; i < resources.Length; i++) { ResourceLayoutElementDescription resource = resources[i]; if (resource.Kind == ResourceKind.UniformBuffer) { string resourceName = resource.Name; int byteCount = Encoding.UTF8.GetByteCount(resourceName) + 1; byte * resourceNamePtr = stackalloc byte[byteCount]; fixed(char *charPtr = resourceName) { int bytesWritten = Encoding.UTF8.GetBytes(charPtr, resourceName.Length, resourceNamePtr, byteCount); Debug.Assert(bytesWritten == byteCount - 1); } resourceNamePtr[byteCount - 1] = 0; // Add null terminator. uint blockIndex = glGetUniformBlockIndex(_program, resourceNamePtr); CheckLastError(); if (blockIndex != GL_INVALID_INDEX) { int blockSize; glGetActiveUniformBlockiv(_program, blockIndex, ActiveUniformBlockParameter.UniformBlockDataSize, &blockSize); CheckLastError(); uniformBindings[i] = new OpenGLUniformBinding(_program, blockIndex, (uint)blockSize); } } else if (resource.Kind == ResourceKind.TextureReadOnly) { string resourceName = resource.Name; int byteCount = Encoding.UTF8.GetByteCount(resourceName) + 1; byte * resourceNamePtr = stackalloc byte[byteCount]; fixed(char *charPtr = resourceName) { int bytesWritten = Encoding.UTF8.GetBytes(charPtr, resourceName.Length, resourceNamePtr, byteCount); Debug.Assert(bytesWritten == byteCount - 1); } resourceNamePtr[byteCount - 1] = 0; // Add null terminator. int location = glGetUniformLocation(_program, resourceNamePtr); CheckLastError(); relativeTextureIndex += 1; textureBindings[i] = new OpenGLTextureBindingSlotInfo() { RelativeIndex = relativeTextureIndex, UniformLocation = location }; lastTextureLocation = location; samplerTrackedRelativeTextureIndices.Add(relativeTextureIndex); } else if (resource.Kind == ResourceKind.TextureReadWrite) { string resourceName = resource.Name; int byteCount = Encoding.UTF8.GetByteCount(resourceName) + 1; byte * resourceNamePtr = stackalloc byte[byteCount]; fixed(char *charPtr = resourceName) { int bytesWritten = Encoding.UTF8.GetBytes(charPtr, resourceName.Length, resourceNamePtr, byteCount); Debug.Assert(bytesWritten == byteCount - 1); } resourceNamePtr[byteCount - 1] = 0; // Add null terminator. int location = glGetUniformLocation(_program, resourceNamePtr); CheckLastError(); relativeImageIndex += 1; textureBindings[i] = new OpenGLTextureBindingSlotInfo() { RelativeIndex = relativeImageIndex, UniformLocation = location }; } else if (resource.Kind == ResourceKind.StructuredBufferReadOnly || resource.Kind == ResourceKind.StructuredBufferReadWrite) { uint storageBlockBinding; if (_gd.BackendType == GraphicsBackend.OpenGL) { string resourceName = resource.Name; int byteCount = Encoding.UTF8.GetByteCount(resourceName) + 1; byte * resourceNamePtr = stackalloc byte[byteCount]; fixed(char *charPtr = resourceName) { int bytesWritten = Encoding.UTF8.GetBytes(charPtr, resourceName.Length, resourceNamePtr, byteCount); Debug.Assert(bytesWritten == byteCount - 1); } resourceNamePtr[byteCount - 1] = 0; // Add null terminator. storageBlockBinding = glGetProgramResourceIndex( _program, ProgramInterface.ShaderStorageBlock, resourceNamePtr); CheckLastError(); } else { storageBlockBinding = storageBlockIndex; storageBlockIndex += 1; } storageBufferBindings[i] = new OpenGLShaderStorageBinding(storageBlockBinding); } else { Debug.Assert(resource.Kind == ResourceKind.Sampler); int[] relativeIndices = samplerTrackedRelativeTextureIndices.ToArray(); samplerTrackedRelativeTextureIndices.Clear(); samplerBindings[i] = new OpenGLSamplerBindingSlotInfo() { RelativeIndices = relativeIndices }; } } _setInfos[setSlot] = new SetBindingsInfo(uniformBindings, textureBindings, samplerBindings, storageBufferBindings); } }
// TODO: Abstract Resource Crreation for Uniforms, Vertex Layouts, Disposing override protected void CreateResources() { //string filePath = Path.Combine(AppContext.BaseDirectory, "Models/sphere.obj"); // huge //string filePath = Path.Combine(AppContext.BaseDirectory, "Models/sphere_centered.obj"); // no texture coordiantes string filePath = "armor/armor.dae"; _model = AssimpLoader.LoadFromFileWithRealtimeMaterial <VertexPositionNormalTextureTangent>(AppContext.BaseDirectory, filePath, VertexPositionNormalTextureTangent.HenzaiType); //GeometryUtils.GenerateSphericalTextureCoordinatesFor(_model.meshes[0], UVMappingTypes.Spherical_Coordinates,true); GeometryUtils.GenerateTangentSpaceFor(_model); // _model = new Model<VertexPositionNormalTextureTangent>(GeometryFactory.generateSphere(100,100,1.0f)); /// Uniform 1 - Camera _cameraProjViewBuffer = _factory.CreateBuffer(new BufferDescription(192, BufferUsage.UniformBuffer | BufferUsage.Dynamic)); var resourceLayoutElementDescription = new ResourceLayoutElementDescription("projViewWorld", ResourceKind.UniformBuffer, ShaderStages.Vertex); ResourceLayoutElementDescription[] resourceLayoutElementDescriptions = { resourceLayoutElementDescription }; var resourceLayoutDescription = new ResourceLayoutDescription(resourceLayoutElementDescriptions); BindableResource[] bindableResources = new BindableResource[] { _cameraProjViewBuffer }; _cameraResourceLayout = _factory.CreateResourceLayout(resourceLayoutDescription); var resourceSetDescription = new ResourceSetDescription(_cameraResourceLayout, bindableResources); _cameraResourceSet = _factory.CreateResourceSet(resourceSetDescription); // Uniform 2 - Material _materialBuffer = _factory.CreateBuffer(new BufferDescription(64, BufferUsage.UniformBuffer)); var resourceLayoutElementDescriptionMaterial = new ResourceLayoutElementDescription("material", ResourceKind.UniformBuffer, ShaderStages.Fragment); ResourceLayoutElementDescription[] resourceLayoutElementDescriptionsMaterial = { resourceLayoutElementDescriptionMaterial }; var resourceLayoutDescriptionMaterial = new ResourceLayoutDescription(resourceLayoutElementDescriptionsMaterial); BindableResource[] bindableResourcesMaterial = new BindableResource[] { _materialBuffer }; _materialResourceLayout = _factory.CreateResourceLayout(resourceLayoutDescriptionMaterial); var resourceSetDescriptionMaterial = new ResourceSetDescription(_materialResourceLayout, bindableResourcesMaterial); _materialResourceSet = _factory.CreateResourceSet(resourceSetDescriptionMaterial); // Uniform 3 - Light _lightBuffer = _factory.CreateBuffer(new BufferDescription(16, BufferUsage.UniformBuffer)); var resourceLayoutElementDescriptionLight = new ResourceLayoutElementDescription("light", ResourceKind.UniformBuffer, ShaderStages.Vertex); ResourceLayoutElementDescription[] resourceLayoutElementDescriptionsLight = { resourceLayoutElementDescriptionLight }; var resourceLayoutDescriptionLight = new ResourceLayoutDescription(resourceLayoutElementDescriptionsLight); BindableResource[] bindableResourcesLight = new BindableResource[] { _lightBuffer }; _lightResourceLayout = _factory.CreateResourceLayout(resourceLayoutDescriptionLight); var resourceSetDescriptionLight = new ResourceSetDescription(_lightResourceLayout, bindableResourcesLight); _lightResourceSet = _factory.CreateResourceSet(resourceSetDescriptionLight); for (int i = 0; i < _model.MeshCount; i++) { var mesh = _model.GetMesh(i); DeviceBuffer vertexBuffer = _factory.CreateBuffer(new BufferDescription(mesh.Vertices.LengthUnsigned() * VertexPositionNormalTextureTangent.SizeInBytes, BufferUsage.VertexBuffer)); DeviceBuffer indexBuffer = _factory.CreateBuffer(new BufferDescription(mesh.Indices.LengthUnsigned() * sizeof(ushort), BufferUsage.IndexBuffer)); _vertexBuffers.Add(vertexBuffer); _indexBuffers.Add(indexBuffer); GraphicsDevice.UpdateBuffer(vertexBuffer, 0, mesh.Vertices); GraphicsDevice.UpdateBuffer(indexBuffer, 0, mesh.Indices); } //Texture Samper // ImageSharpTexture diffuseTexture = new ImageSharpTexture(Path.Combine(AppContext.BaseDirectory, "Textures", "earth.jpg")); // ImageSharpTexture diffuseTexture = new ImageSharpTexture(Path.Combine(AppContext.BaseDirectory, "Textures", "Water.jpg")); ImageSharpTexture diffuseTexture = new ImageSharpTexture(Path.Combine(AppContext.BaseDirectory, "armor", "diffuse.png")); Texture sphereDiffuseTexture = diffuseTexture.CreateDeviceTexture(GraphicsDevice, _factory); TextureView sphereDiffuseTextureView = _factory.CreateTextureView(sphereDiffuseTexture); // ImageSharpTexture normalTexture = new ImageSharpTexture(Path.Combine(AppContext.BaseDirectory, "Textures", "WaterNorm.jpg")); ImageSharpTexture normalTexture = new ImageSharpTexture(Path.Combine(AppContext.BaseDirectory, "armor", "normal.png")); Texture sphereNormalTexture = normalTexture.CreateDeviceTexture(GraphicsDevice, _factory); TextureView sphereNormalTextureView = _factory.CreateTextureView(sphereNormalTexture); ResourceLayout textureLayout = _factory.CreateResourceLayout( new ResourceLayoutDescription( new ResourceLayoutElementDescription("DiffuseTexture", ResourceKind.TextureReadOnly, ShaderStages.Fragment), new ResourceLayoutElementDescription("DiffuseSampler", ResourceKind.Sampler, ShaderStages.Fragment), new ResourceLayoutElementDescription("NormTexture", ResourceKind.TextureReadOnly, ShaderStages.Fragment), new ResourceLayoutElementDescription("NormSampler", ResourceKind.Sampler, ShaderStages.Fragment) )); var sampler = _factory.CreateSampler(new SamplerDescription { AddressModeU = SamplerAddressMode.Wrap, AddressModeV = SamplerAddressMode.Wrap, AddressModeW = SamplerAddressMode.Wrap, Filter = SamplerFilter.MinLinear_MagLinear_MipLinear, LodBias = 0, MinimumLod = 0, MaximumLod = uint.MaxValue, MaximumAnisotropy = 0, }); _textureResourceSet = _factory.CreateResourceSet(new ResourceSetDescription( textureLayout, sphereDiffuseTextureView, GraphicsDevice.LinearSampler, sphereNormalTextureView, sampler )); VertexLayoutDescription vertexLayout = new VertexLayoutDescription( new VertexElementDescription("Position", VertexElementSemantic.Position, VertexElementFormat.Float3), new VertexElementDescription("Normal", VertexElementSemantic.Normal, VertexElementFormat.Float3), new VertexElementDescription("UV", VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float2), new VertexElementDescription("Tangent", VertexElementSemantic.Normal, VertexElementFormat.Float3) ); _vertexShader = IO.LoadShader("PhongTexture", ShaderStages.Vertex, GraphicsDevice); _fragmentShader = IO.LoadShader("PhongTexture", ShaderStages.Fragment, GraphicsDevice); GraphicsPipelineDescription pipelineDescription = new GraphicsPipelineDescription() { BlendState = BlendStateDescription.SingleOverrideBlend, DepthStencilState = DepthStencilStateDescription.DepthOnlyLessEqual, RasterizerState = new RasterizerStateDescription( cullMode: FaceCullMode.Back, fillMode: PolygonFillMode.Solid, frontFace: FrontFace.Clockwise, depthClipEnabled: true, scissorTestEnabled: false ), PrimitiveTopology = PrimitiveTopology.TriangleList, ResourceLayouts = new ResourceLayout[] { _cameraResourceLayout, _lightResourceLayout, _materialResourceLayout, textureLayout }, ShaderSet = new ShaderSetDescription( vertexLayouts: new VertexLayoutDescription[] { vertexLayout }, shaders: new Shader[] { _vertexShader, _fragmentShader } ), Outputs = GraphicsDevice.SwapchainFramebuffer.OutputDescription }; _pipeline = _factory.CreateGraphicsPipeline(pipelineDescription); }
private void CreateShaders() { //Vertex layout is the same for all custom shaders var vertexLayout = new VertexLayoutDescription ( 16, 0, new VertexElementDescription[] { new VertexElementDescription("Position", VertexElementFormat.Float2, VertexElementSemantic.Position), new VertexElementDescription("VTex", VertexElementFormat.Float2, VertexElementSemantic.TextureCoordinate) } ); //Build up the uniform descriptions & create buffers where required var uniformDescriptions = new List <ResourceLayoutElementDescription[]>(); var numUniforms = _userUniformDescriptions.Length; var textureCount = 0; for (var n = 0; n < numUniforms; n++) { var desc = _userUniformDescriptions[n]; switch (desc.UniformType) { case ShaderUniformType.Texture: if (textureCount >= 4) { _frameworkMessenger.Report("Customer Shader has more than 4 textures, not currently allowed, skipping uniform"); continue; } textureCount++; uniformDescriptions.Add( new ResourceLayoutElementDescription[] { new ResourceLayoutElementDescription(string.Concat("Sampler_", desc.Name), ResourceKind.TextureReadOnly, ShaderStages.Fragment), new ResourceLayoutElementDescription(string.Concat("Texture_", desc.Name), ResourceKind.Sampler, ShaderStages.Fragment) } ); break; case ShaderUniformType.Data: var description = new ResourceLayoutElementDescription[] { new ResourceLayoutElementDescription(desc.Name, ResourceKind.UniformBuffer, ShaderStages.Fragment) }; uniformDescriptions.Add(description); var buffer = CreateUniformBuffer(description, desc); _uniformBuffers[n] = buffer; _uniformBufferNameLookup.Add(desc.Name, n); break; } } var vertexShaderFileName = "Vertex2D"; if (_useSpirvCompile) { //Different Vertex Shaders account for differences in backend (incl. Texcoord origin) switch (_systemComponents.Device.BackendType) { case GraphicsApi.Direct3D11: case GraphicsApi.Metal: vertexShaderFileName = "Vertex2D-TcTopLeft"; break; case GraphicsApi.Vulkan: case GraphicsApi.OpenGL: vertexShaderFileName = "Vertex2D-TcBottomLeft"; break; case GraphicsApi.OpenGLES: case GraphicsApi.SystemDefault: throw new Yak2DException("Type is either unsupported or erroneously stored as system default"); } } _shaderPackage = _shaderLoader.CreateShaderPackage(vertexShaderFileName, AssetSourceEnum.Embedded, _fragmentShaderFilename, _shaderFileAssetType, vertexLayout, uniformDescriptions.ToArray(), _useSpirvCompile, _useSpirvCompile); }
private void ProcessResourceSetLayouts(ResourceLayout[] layouts) { int resourceLayoutCount = layouts.Length; _setInfos = new SetBindingsInfo[resourceLayoutCount]; int lastTextureLocation = -1; int relativeTextureIndex = -1; int relativeImageIndex = -1; uint storageBlockIndex = 0; // Tracks OpenGL ES storage buffers. for (uint setSlot = 0; setSlot < resourceLayoutCount; setSlot++) { ResourceLayout setLayout = layouts[setSlot]; OpenGLResourceLayout glSetLayout = Util.AssertSubtype <ResourceLayout, OpenGLResourceLayout>(setLayout); ResourceLayoutElementDescription[] resources = glSetLayout.Elements; Dictionary <uint, OpenGLUniformBinding> uniformBindings = new Dictionary <uint, OpenGLUniformBinding>(); Dictionary <uint, OpenGLTextureBindingSlotInfo> textureBindings = new Dictionary <uint, OpenGLTextureBindingSlotInfo>(); Dictionary <uint, OpenGLSamplerBindingSlotInfo> samplerBindings = new Dictionary <uint, OpenGLSamplerBindingSlotInfo>(); Dictionary <uint, OpenGLShaderStorageBinding> storageBufferBindings = new Dictionary <uint, OpenGLShaderStorageBinding>(); List <int> samplerTrackedRelativeTextureIndices = new List <int>(); for (uint i = 0; i < resources.Length; i++) { ResourceLayoutElementDescription resource = resources[i]; if (resource.Kind == ResourceKind.UniformBuffer) { string resourceName = resource.Name; int byteCount = Encoding.UTF8.GetByteCount(resourceName) + 1; byte * resourceNamePtr = stackalloc byte[byteCount]; fixed(char *charPtr = resourceName) { int bytesWritten = Encoding.UTF8.GetBytes(charPtr, resourceName.Length, resourceNamePtr, byteCount); Debug.Assert(bytesWritten == byteCount - 1); } resourceNamePtr[byteCount - 1] = 0; // Add null terminator. uint blockIndex = glGetUniformBlockIndex(_program, resourceNamePtr); CheckLastError(); if (blockIndex != GL_INVALID_INDEX) { int blockSize; glGetActiveUniformBlockiv(_program, blockIndex, ActiveUniformBlockParameter.UniformBlockDataSize, &blockSize); CheckLastError(); uniformBindings[i] = new OpenGLUniformBinding(_program, blockIndex, (uint)blockSize); } #if DEBUG && GL_VALIDATE_SHADER_RESOURCE_NAMES else { uint uniformBufferIndex = 0; uint bufferNameByteCount = 64; byte *bufferNamePtr = stackalloc byte[(int)bufferNameByteCount]; var names = new List <string>(); while (true) { uint actualLength; glGetActiveUniformBlockName(_program, uniformBufferIndex, bufferNameByteCount, &actualLength, bufferNamePtr); if (glGetError() != 0) { break; } string name = Encoding.UTF8.GetString(bufferNamePtr, (int)actualLength); names.Add(name); uniformBufferIndex++; } throw new VeldridException($"Unable to bind uniform buffer \"{resourceName}\" by name. Valid names for this pipeline are: {string.Join(", ", names)}"); } #endif } else if (resource.Kind == ResourceKind.TextureReadOnly) { string resourceName = resource.Name; int byteCount = Encoding.UTF8.GetByteCount(resourceName) + 1; byte * resourceNamePtr = stackalloc byte[byteCount]; fixed(char *charPtr = resourceName) { int bytesWritten = Encoding.UTF8.GetBytes(charPtr, resourceName.Length, resourceNamePtr, byteCount); Debug.Assert(bytesWritten == byteCount - 1); } resourceNamePtr[byteCount - 1] = 0; // Add null terminator. int location = glGetUniformLocation(_program, resourceNamePtr); CheckLastError(); #if DEBUG && GL_VALIDATE_SHADER_RESOURCE_NAMES if (location == -1) { ReportInvalidResourceName(resourceName); } #endif relativeTextureIndex += 1; textureBindings[i] = new OpenGLTextureBindingSlotInfo() { RelativeIndex = relativeTextureIndex, UniformLocation = location }; lastTextureLocation = location; samplerTrackedRelativeTextureIndices.Add(relativeTextureIndex); } else if (resource.Kind == ResourceKind.TextureReadWrite) { string resourceName = resource.Name; int byteCount = Encoding.UTF8.GetByteCount(resourceName) + 1; byte * resourceNamePtr = stackalloc byte[byteCount]; fixed(char *charPtr = resourceName) { int bytesWritten = Encoding.UTF8.GetBytes(charPtr, resourceName.Length, resourceNamePtr, byteCount); Debug.Assert(bytesWritten == byteCount - 1); } resourceNamePtr[byteCount - 1] = 0; // Add null terminator. int location = glGetUniformLocation(_program, resourceNamePtr); CheckLastError(); #if DEBUG && GL_VALIDATE_SHADER_RESOURCE_NAMES if (location == -1) { ReportInvalidResourceName(resourceName); } #endif relativeImageIndex += 1; textureBindings[i] = new OpenGLTextureBindingSlotInfo() { RelativeIndex = relativeImageIndex, UniformLocation = location }; } else if (resource.Kind == ResourceKind.StructuredBufferReadOnly || resource.Kind == ResourceKind.StructuredBufferReadWrite) { uint storageBlockBinding; if (_gd.BackendType == GraphicsBackend.OpenGL) { string resourceName = resource.Name; int byteCount = Encoding.UTF8.GetByteCount(resourceName) + 1; byte * resourceNamePtr = stackalloc byte[byteCount]; fixed(char *charPtr = resourceName) { int bytesWritten = Encoding.UTF8.GetBytes(charPtr, resourceName.Length, resourceNamePtr, byteCount); Debug.Assert(bytesWritten == byteCount - 1); } resourceNamePtr[byteCount - 1] = 0; // Add null terminator. storageBlockBinding = glGetProgramResourceIndex( _program, ProgramInterface.ShaderStorageBlock, resourceNamePtr); CheckLastError(); } else { storageBlockBinding = storageBlockIndex; storageBlockIndex += 1; } storageBufferBindings[i] = new OpenGLShaderStorageBinding(storageBlockBinding); } else { Debug.Assert(resource.Kind == ResourceKind.Sampler); int[] relativeIndices = samplerTrackedRelativeTextureIndices.ToArray(); samplerTrackedRelativeTextureIndices.Clear(); samplerBindings[i] = new OpenGLSamplerBindingSlotInfo() { RelativeIndices = relativeIndices }; } } _setInfos[setSlot] = new SetBindingsInfo(uniformBindings, textureBindings, samplerBindings, storageBufferBindings); } }
public IPipelineBuilder With(ResourceLayoutElementDescription resourceLayoutDescr) { resLayoutElements.Last().Add(resourceLayoutDescr); return(this); }
private void CreateShadersAndFactorsUniform() { var vertexLayout = new VertexLayoutDescription ( 16, 0, new VertexElementDescription[] { new VertexElementDescription("Position", VertexElementFormat.Float2, VertexElementSemantic.Position), new VertexElementDescription("VTex", VertexElementFormat.Float2, VertexElementSemantic.TextureCoordinate) } ); var uniformDescriptions = new ResourceLayoutElementDescription[3][]; uniformDescriptions[0] = new ResourceLayoutElementDescription[] { new ResourceLayoutElementDescription("Sampler", ResourceKind.TextureReadOnly, ShaderStages.Fragment), new ResourceLayoutElementDescription("Texture", ResourceKind.Sampler, ShaderStages.Fragment) }; uniformDescriptions[1] = new ResourceLayoutElementDescription[] { new ResourceLayoutElementDescription("LinearSampledGaussianUniforms", ResourceKind.UniformBuffer, ShaderStages.Fragment) }; uniformDescriptions[2] = new ResourceLayoutElementDescription[] { new ResourceLayoutElementDescription("WeightsAndOffsets", ResourceKind.UniformBuffer, ShaderStages.Fragment) }; _shaderPackage = _shaderLoader.CreateShaderPackage("Vertex2D", AssetSourceEnum.Embedded, "LinearFilterGaussianBlur1DFragment", AssetSourceEnum.Embedded, vertexLayout, uniformDescriptions); var gaussianUniformBlockResourceLayoutDescription = uniformDescriptions[1][0]; _gaussianFactorsUniformBlockBuffer = _systemComponents.Factory.CreateBuffer(new BufferDescription(GaussianBlurFactors.SizeInBytes, BufferUsage.UniformBuffer | BufferUsage.Dynamic)); var gaussianFactorsResourceLayout = _systemComponents.Factory.CreateResourceLayout( new ResourceLayoutDescription( gaussianUniformBlockResourceLayoutDescription ) ); _gaussianFactorsUniformBlockResourceSet = _systemComponents.Factory.CreateResourceSet( new ResourceSetDescription(gaussianFactorsResourceLayout, _gaussianFactorsUniformBlockBuffer) ); var weightsAndOffsetsResourceLayoutDescription = uniformDescriptions[2][0]; _weightsAndOffsetsDeviceBuffer = _systemComponents.Factory.CreateBuffer(new BufferDescription((uint)_gaussianWeightsAndOffsetsCache.WeightsAndOffsetsArraySize * GaussianBlurArrayComponent.SizeInBytes, BufferUsage.UniformBuffer | BufferUsage.Dynamic)); var weightsAndOffsetsResourceLayoutFactors = _systemComponents.Factory.CreateResourceLayout( new ResourceLayoutDescription( weightsAndOffsetsResourceLayoutDescription ) ); _weightsAndOffsetsResourceSet = _systemComponents.Factory.CreateResourceSet( new ResourceSetDescription(weightsAndOffsetsResourceLayoutFactors, _weightsAndOffsetsDeviceBuffer) ); }
// TODO: Abstract Resource Crreation for Uniforms, Vertex Layouts, Disposing override protected void CreateResources() { RgbaFloat lightColor = RgbaFloat.LightGrey; var lightLookAt = new Vector4(0, 0, 0, 1); var lightCam = new OrthographicCamera(35, 35, Light.DEFAULT_POSITION, lightLookAt); _sceneRuntimeState.Light = new Light(lightCam, lightColor, 0.1f); // string filePath = Path.Combine(AppContext.BaseDirectory, "Models/sphere.obj"); //string filePath = Path.Combine(AppContext.BaseDirectory, "Models/300_polygon_sphere_100mm.STL"); // string filePath = "Models/sphere_centered.obj"; string filePath = "Models/chinesedragon.dae"; // string filePath = "Models/Box.dae"; _model = AssimpLoader.LoadFromFileWithRealtimeMaterial <VertexPositionNormal>(AppContext.BaseDirectory, filePath, VertexPositionNormal.HenzaiType); //GeometryUtils.GenerateSphericalTextureCoordinatesFor(_model.meshes[0]); /// Uniform 1 - Camera _cameraProjViewBuffer = _factory.CreateBuffer(new BufferDescription(Camera.SizeInBytes, BufferUsage.UniformBuffer | BufferUsage.Dynamic)); var resourceLayoutElementDescription = new ResourceLayoutElementDescription("projViewWorld", ResourceKind.UniformBuffer, ShaderStages.Vertex); ResourceLayoutElementDescription[] resourceLayoutElementDescriptions = { resourceLayoutElementDescription }; var resourceLayoutDescription = new ResourceLayoutDescription(resourceLayoutElementDescriptions); BindableResource[] bindableResources = new BindableResource[] { _cameraProjViewBuffer }; _cameraResourceLayout = _factory.CreateResourceLayout(resourceLayoutDescription); var resourceSetDescription = new ResourceSetDescription(_cameraResourceLayout, bindableResources); _cameraResourceSet = _factory.CreateResourceSet(resourceSetDescription); // Uniform 2 - Material _materialBuffer = _factory.CreateBuffer(new BufferDescription(RealtimeMaterial.SizeInBytes, BufferUsage.UniformBuffer)); var resourceLayoutElementDescriptionMaterial = new ResourceLayoutElementDescription("material", ResourceKind.UniformBuffer, ShaderStages.Fragment); ResourceLayoutElementDescription[] resourceLayoutElementDescriptionsMaterial = { resourceLayoutElementDescriptionMaterial }; var resourceLayoutDescriptionMaterial = new ResourceLayoutDescription(resourceLayoutElementDescriptionsMaterial); BindableResource[] bindableResourcesMaterial = new BindableResource[] { _materialBuffer }; _materialResourceLayout = _factory.CreateResourceLayout(resourceLayoutDescriptionMaterial); var resourceSetDescriptionMaterial = new ResourceSetDescription(_materialResourceLayout, bindableResourcesMaterial); _materialResourceSet = _factory.CreateResourceSet(resourceSetDescriptionMaterial); // Uniform 3 - Light _lightBuffer = _factory.CreateBuffer(new BufferDescription(Light.SizeInBytes, BufferUsage.UniformBuffer)); var resourceLayoutElementDescriptionLight = new ResourceLayoutElementDescription("light", ResourceKind.UniformBuffer, ShaderStages.Fragment); ResourceLayoutElementDescription[] resourceLayoutElementDescriptionsLight = { resourceLayoutElementDescriptionLight }; var resourceLayoutDescriptionLight = new ResourceLayoutDescription(resourceLayoutElementDescriptionsLight); BindableResource[] bindableResourcesLight = new BindableResource[] { _lightBuffer }; _lightResourceLayout = _factory.CreateResourceLayout(resourceLayoutDescriptionLight); var resourceSetDescriptionLight = new ResourceSetDescription(_lightResourceLayout, bindableResourcesLight); _lightResourceSet = _factory.CreateResourceSet(resourceSetDescriptionLight); for (int i = 0; i < _model.MeshCount; i++) { var mesh = _model.GetMesh(i); DeviceBuffer vertexBuffer = _factory.CreateBuffer(new BufferDescription(mesh.Vertices.LengthUnsigned() * VertexPositionNormal.SizeInBytes, BufferUsage.VertexBuffer)); DeviceBuffer indexBuffer = _factory.CreateBuffer(new BufferDescription(mesh.Indices.LengthUnsigned() * sizeof(ushort), BufferUsage.IndexBuffer)); _vertexBuffers.Add(vertexBuffer); _indexBuffers.Add(indexBuffer); GraphicsDevice.UpdateBuffer(vertexBuffer, 0, mesh.Vertices); GraphicsDevice.UpdateBuffer(indexBuffer, 0, mesh.Indices); } VertexLayoutDescription vertexLayout = new VertexLayoutDescription( new VertexElementDescription("Position", VertexElementSemantic.Position, VertexElementFormat.Float3), new VertexElementDescription("Normal", VertexElementSemantic.Normal, VertexElementFormat.Float3) //new VertexElementDescription("UV",VertexElementSemantic.TextureCoordinate,VertexElementFormat.Float2) ); _vertexShader = IO.LoadShader("Phong", ShaderStages.Vertex, GraphicsDevice); _fragmentShader = IO.LoadShader("Phong", ShaderStages.Fragment, GraphicsDevice); GraphicsPipelineDescription pipelineDescription = new GraphicsPipelineDescription() { BlendState = BlendStateDescription.SingleOverrideBlend, DepthStencilState = DepthStencilStateDescription.DepthOnlyLessEqual, RasterizerState = new RasterizerStateDescription( cullMode: FaceCullMode.Back, fillMode: PolygonFillMode.Solid, // Wireframe doesnt seem to work with metal frontFace: FrontFace.Clockwise, depthClipEnabled: true, scissorTestEnabled: false ), PrimitiveTopology = PrimitiveTopology.TriangleList, //ResourceLayouts = new ResourceLayout[] {_cameraResourceLayout,_materialResourceLayout,_lightResourceLayout}, ResourceLayouts = new ResourceLayout[] { _cameraResourceLayout, _lightResourceLayout, _materialResourceLayout }, ShaderSet = new ShaderSetDescription( vertexLayouts: new VertexLayoutDescription[] { vertexLayout }, shaders: new Shader[] { _vertexShader, _fragmentShader } ), Outputs = GraphicsDevice.SwapchainFramebuffer.OutputDescription }; _pipeline = _factory.CreateGraphicsPipeline(pipelineDescription); }
override protected void CreateResources() { _cameraProjViewBuffer = _factory.CreateBuffer(new BufferDescription(128, BufferUsage.UniformBuffer | BufferUsage.Dynamic)); ResourceLayoutElementDescription resourceLayoutElementDescription = new ResourceLayoutElementDescription("projView", ResourceKind.UniformBuffer, ShaderStages.Vertex); ResourceLayoutElementDescription[] resourceLayoutElementDescriptions = { resourceLayoutElementDescription }; ResourceLayoutDescription resourceLayoutDescription = new ResourceLayoutDescription(resourceLayoutElementDescriptions); BindableResource[] bindableResources = new BindableResource[] { _cameraProjViewBuffer }; _resourceLayout = _factory.CreateResourceLayout(resourceLayoutDescription); ResourceSetDescription resourceSetDescription = new ResourceSetDescription(_resourceLayout, bindableResources); _resourceSet = _factory.CreateResourceSet(resourceSetDescription); Mesh <VertexPositionNDCColor> coloredQuad = GeometryFactory.GenerateColorQuadNDC_XY(RgbaFloat.Red, RgbaFloat.Green, RgbaFloat.Blue, RgbaFloat.Yellow); ushort[] quadIndicies = GeometryFactory.GenerateQuadIndicies_TriangleStrip_CW(); float[] _xOffset = { -2f, 2f }; // declare (VBO) buffers _vertexBuffer = _factory.CreateBuffer(new BufferDescription(coloredQuad.Vertices.LengthUnsigned() * VertexPositionNDCColor.SizeInBytes, BufferUsage.VertexBuffer)); _indexBuffer = _factory.CreateBuffer(new BufferDescription(quadIndicies.LengthUnsigned() * sizeof(ushort), BufferUsage.IndexBuffer)); _xOffsetBuffer = _factory.CreateBuffer(new BufferDescription(_xOffset.LengthUnsigned() * sizeof(float), BufferUsage.VertexBuffer)); // fill buffers with data GraphicsDevice.UpdateBuffer(_vertexBuffer, 0, coloredQuad.Vertices); GraphicsDevice.UpdateBuffer(_indexBuffer, 0, quadIndicies); GraphicsDevice.UpdateBuffer(_xOffsetBuffer, 0, _xOffset); GraphicsDevice.UpdateBuffer(_cameraProjViewBuffer, 0, Camera.ViewMatrix); GraphicsDevice.UpdateBuffer(_cameraProjViewBuffer, 64, Camera.ProjectionMatrix); VertexLayoutDescription vertexLayout = new VertexLayoutDescription( new VertexElementDescription("Position", VertexElementSemantic.Position, VertexElementFormat.Float2), new VertexElementDescription("Color", VertexElementSemantic.Color, VertexElementFormat.Float4) ); VertexElementDescription vertexElementPerInstance = new VertexElementDescription("xOff", VertexElementSemantic.Position, VertexElementFormat.Float1); VertexLayoutDescription vertexLayoutPerInstance = new VertexLayoutDescription( stride: 4, instanceStepRate: 1, elements: new VertexElementDescription[] { vertexElementPerInstance } ); _vertexShader = IO.LoadShader("OffsetXNDCColor", ShaderStages.Vertex, GraphicsDevice); _fragmentShader = IO.LoadShader("Color", ShaderStages.Fragment, GraphicsDevice); GraphicsPipelineDescription pipelineDescription = new GraphicsPipelineDescription() { BlendState = BlendStateDescription.SingleOverrideBlend, DepthStencilState = new DepthStencilStateDescription( depthTestEnabled: true, depthWriteEnabled: true, comparisonKind: ComparisonKind.LessEqual), RasterizerState = new RasterizerStateDescription( cullMode: FaceCullMode.Back, fillMode: PolygonFillMode.Solid, frontFace: FrontFace.Clockwise, depthClipEnabled: true, scissorTestEnabled: false ), PrimitiveTopology = PrimitiveTopology.TriangleStrip, ResourceLayouts = new ResourceLayout[] { _resourceLayout }, ShaderSet = new ShaderSetDescription( vertexLayouts: new VertexLayoutDescription[] { vertexLayout, vertexLayoutPerInstance }, shaders: new Shader[] { _vertexShader, _fragmentShader } ), Outputs = GraphicsDevice.SwapchainFramebuffer.OutputDescription }; _pipeline = _factory.CreateGraphicsPipeline(pipelineDescription); }
private void CreateShadersAndBuffer() { var vertexLayout = new VertexLayoutDescription ( 16, 0, new VertexElementDescription[] { new VertexElementDescription("Position", VertexElementFormat.Float2, VertexElementSemantic.Position), new VertexElementDescription("VTex", VertexElementFormat.Float2, VertexElementSemantic.TextureCoordinate) } ); var uniformDescriptions = new ResourceLayoutElementDescription[6][]; uniformDescriptions[0] = new ResourceLayoutElementDescription[] { new ResourceLayoutElementDescription("MixFactors", ResourceKind.UniformBuffer, ShaderStages.Fragment) }; uniformDescriptions[1] = new ResourceLayoutElementDescription[] { new ResourceLayoutElementDescription("SamplerMix", ResourceKind.TextureReadOnly, ShaderStages.Fragment), new ResourceLayoutElementDescription("TextureMix", ResourceKind.Sampler, ShaderStages.Fragment) }; uniformDescriptions[2] = new ResourceLayoutElementDescription[] { new ResourceLayoutElementDescription("Sampler0", ResourceKind.TextureReadOnly, ShaderStages.Fragment), new ResourceLayoutElementDescription("Texture0", ResourceKind.Sampler, ShaderStages.Fragment) }; uniformDescriptions[3] = new ResourceLayoutElementDescription[] { new ResourceLayoutElementDescription("Sampler1", ResourceKind.TextureReadOnly, ShaderStages.Fragment), new ResourceLayoutElementDescription("Texture1", ResourceKind.Sampler, ShaderStages.Fragment) }; uniformDescriptions[4] = new ResourceLayoutElementDescription[] { new ResourceLayoutElementDescription("Sampler2", ResourceKind.TextureReadOnly, ShaderStages.Fragment), new ResourceLayoutElementDescription("Texture2", ResourceKind.Sampler, ShaderStages.Fragment) }; uniformDescriptions[5] = new ResourceLayoutElementDescription[] { new ResourceLayoutElementDescription("Sampler3", ResourceKind.TextureReadOnly, ShaderStages.Fragment), new ResourceLayoutElementDescription("Texture3", ResourceKind.Sampler, ShaderStages.Fragment) }; _shaderPackage = _shaderLoader.CreateShaderPackage("Vertex2D", AssetSourceEnum.Embedded, "MixFragment", AssetSourceEnum.Embedded, vertexLayout, uniformDescriptions); _mixFactorsBuffer = _systemComponents.Factory.CreateBuffer(new BufferDescription(MixStageFactors.SizeInBytes, BufferUsage.UniformBuffer | BufferUsage.Dynamic)); var resourceLayout = _systemComponents.Factory.CreateResourceLayout( new ResourceLayoutDescription( uniformDescriptions[0] ) ); _mixFactorsResource = _systemComponents.Factory.CreateResourceSet( new ResourceSetDescription(resourceLayout, _mixFactorsBuffer) ); }
override protected void CreateResources() { _cameraProjViewBuffer = _factory.CreateBuffer(new BufferDescription(Camera.SizeInBytes, BufferUsage.UniformBuffer | BufferUsage.Dynamic)); ResourceLayoutElementDescription resourceLayoutElementDescription = new ResourceLayoutElementDescription("projView", ResourceKind.UniformBuffer, ShaderStages.Vertex); ResourceLayoutElementDescription[] resourceLayoutElementDescriptions = { resourceLayoutElementDescription }; ResourceLayoutDescription resourceLayoutDescription = new ResourceLayoutDescription(resourceLayoutElementDescriptions); BindableResource[] bindableResources = new BindableResource[] { _cameraProjViewBuffer }; _resourceLayout = _factory.CreateResourceLayout(resourceLayoutDescription); ResourceSetDescription resourceSetDescription = new ResourceSetDescription(_resourceLayout, bindableResources); _cameraResourceSet = _factory.CreateResourceSet(resourceSetDescription); ImageSharpTexture NameImage = new ImageSharpTexture(Path.Combine(AppContext.BaseDirectory, "Textures", "Name.png")); Texture cubeTexture = NameImage.CreateDeviceTexture(GraphicsDevice, _factory); TextureView cubeTextureView = _factory.CreateTextureView(cubeTexture); ResourceLayout textureLayout = _factory.CreateResourceLayout( new ResourceLayoutDescription( new ResourceLayoutElementDescription("Texture", ResourceKind.TextureReadOnly, ShaderStages.Fragment), new ResourceLayoutElementDescription("Sampler", ResourceKind.Sampler, ShaderStages.Fragment))); _textureResourceSet = _factory.CreateResourceSet(new ResourceSetDescription( textureLayout, cubeTextureView, GraphicsDevice.LinearSampler)); Mesh <VertexPositionTexture> texturedCube = GeometryFactory.GenerateTexturedCube(false); ushort[] cubeIndicies = GeometryFactory.generateCubeIndicies_TriangleList_CW(); // declare (VBO) buffers _vertexBuffer = _factory.CreateBuffer(new BufferDescription(texturedCube.Vertices.LengthUnsigned() * VertexPositionTexture.SizeInBytes, BufferUsage.VertexBuffer)); _indexBuffer = _factory.CreateBuffer(new BufferDescription(cubeIndicies.LengthUnsigned() * sizeof(ushort), BufferUsage.IndexBuffer)); // fill buffers with data GraphicsDevice.UpdateBuffer(_vertexBuffer, 0, texturedCube.Vertices); GraphicsDevice.UpdateBuffer(_indexBuffer, 0, cubeIndicies); //graphicsDevice.UpdateBuffer(_cameraProjViewBuffer,0,camera.ViewMatrix); //graphicsDevice.UpdateBuffer(_cameraProjViewBuffer,64,camera.ProjectionMatrix); VertexLayoutDescription vertexLayout = new VertexLayoutDescription( new VertexElementDescription("Position", VertexElementSemantic.Position, VertexElementFormat.Float3), new VertexElementDescription("UV", VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float2) ); _vertexShader = IO.LoadShader("Texture", ShaderStages.Vertex, GraphicsDevice); _fragmentShader = IO.LoadShader("Texture", ShaderStages.Fragment, GraphicsDevice); GraphicsPipelineDescription pipelineDescription = new GraphicsPipelineDescription() { BlendState = BlendStateDescription.SingleOverrideBlend, DepthStencilState = DepthStencilStateDescription.DepthOnlyLessEqual, RasterizerState = new RasterizerStateDescription( cullMode: FaceCullMode.Back, fillMode: PolygonFillMode.Solid, frontFace: FrontFace.Clockwise, depthClipEnabled: true, scissorTestEnabled: false ), PrimitiveTopology = PrimitiveTopology.TriangleList, ResourceLayouts = new ResourceLayout[] { _resourceLayout, textureLayout }, ShaderSet = new ShaderSetDescription( vertexLayouts: new VertexLayoutDescription[] { vertexLayout }, shaders: new Shader[] { _vertexShader, _fragmentShader } ), Outputs = GraphicsDevice.SwapchainFramebuffer.OutputDescription }; _pipeline = _factory.CreateGraphicsPipeline(pipelineDescription); }