private void InitializeContextObjects(AssetDatabase ad, RenderContext rc) { ResourceFactory factory = rc.ResourceFactory; _vb = factory.CreateVertexBuffer(1024, true); _ib = factory.CreateIndexBuffer(1024, true); Shader vs = factory.CreateShader(ShaderStages.Vertex, ShaderHelper.LoadShaderCode("wireframe-vertex", ShaderStages.Vertex, rc.ResourceFactory)); Shader fs = factory.CreateShader(ShaderStages.Fragment, ShaderHelper.LoadShaderCode("wireframe-frag", ShaderStages.Fragment, rc.ResourceFactory)); VertexInputLayout inputLayout = factory.CreateInputLayout( new VertexInputElement("in_position", VertexSemanticType.Position, VertexElementFormat.Float3), new VertexInputElement("in_color", VertexSemanticType.Color, VertexElementFormat.Byte4)); ShaderSet shaderSet = factory.CreateShaderSet(inputLayout, vs, fs); ShaderResourceBindingSlots cbs = factory.CreateShaderResourceBindingSlots( shaderSet, new ShaderResourceDescription("ProjectionMatrixBuffer", ShaderConstantType.Matrix4x4), new ShaderResourceDescription("ViewMatrixBuffer", ShaderConstantType.Matrix4x4), new ShaderResourceDescription("WorldMatrixBuffer", ShaderConstantType.Matrix4x4)); _material = new Material(shaderSet, cbs); _worldBuffer = factory.CreateConstantBuffer(ShaderConstantType.Matrix4x4); Matrix4x4 identity = Matrix4x4.Identity; _worldBuffer.SetData(ref identity, 64); _wireframeState = factory.CreateRasterizerState(FaceCullingMode.None, TriangleFillMode.Solid, true, true); }
public static Material CreateRegularPassMaterial(ResourceFactory factory) { Shader vs = factory.CreateShader(ShaderStages.Vertex, ShaderHelper.LoadShaderCode("shadow-vertex", ShaderStages.Vertex, factory)); Shader fs = factory.CreateShader(ShaderStages.Fragment, ShaderHelper.LoadShaderCode("shadow-frag", ShaderStages.Fragment, factory)); VertexInputLayout inputLayout = factory.CreateInputLayout( new VertexInputDescription( 32, new VertexInputElement("in_position", VertexSemanticType.Position, VertexElementFormat.Float3), new VertexInputElement("in_normal", VertexSemanticType.Normal, VertexElementFormat.Float3), new VertexInputElement("in_texCoord", VertexSemanticType.TextureCoordinate, VertexElementFormat.Float2))); ShaderSet shaderSet = factory.CreateShaderSet(inputLayout, vs, fs); ShaderResourceBindingSlots constantSlots = factory.CreateShaderResourceBindingSlots( shaderSet, new ShaderResourceDescription("ProjectionMatrixBuffer", ShaderConstantType.Matrix4x4), new ShaderResourceDescription("ViewMatrixBuffer", ShaderConstantType.Matrix4x4), new ShaderResourceDescription("LightProjectionMatrixBuffer", ShaderConstantType.Matrix4x4), new ShaderResourceDescription("LightViewMatrixBuffer", ShaderConstantType.Matrix4x4), new ShaderResourceDescription("LightInfoBuffer", ShaderConstantType.Float4), new ShaderResourceDescription("WorldMatrixBuffer", ShaderConstantType.Matrix4x4), new ShaderResourceDescription("InverseTransposeWorldMatrixBuffer", ShaderConstantType.Matrix4x4), new ShaderResourceDescription("SurfaceTexture", ShaderResourceType.Texture), new ShaderResourceDescription("SurfaceTexture", ShaderResourceType.Sampler), new ShaderResourceDescription("ShadowMap", ShaderResourceType.Texture), new ShaderResourceDescription("ShadowMap", ShaderResourceType.Sampler)); return(new Material(shaderSet, constantSlots)); }
public Material( ShaderSet shaderSet, ShaderResourceBindingSlots resourceBindings) { ShaderSet = shaderSet; ResourceBindings = resourceBindings; }
public static void Main(string[] args) { WindowCreateInfo windowCI = new WindowCreateInfo() { X = 100, Y = 100, WindowWidth = 960, WindowHeight = 540, WindowTitle = "Veldrid TinyDemo", }; RenderContextCreateInfo contextCI = new RenderContextCreateInfo(); VeldridStartup.CreateWindowAndRenderContext(ref windowCI, ref contextCI, out var window, out RenderContext rc); VertexBuffer vb = rc.ResourceFactory.CreateVertexBuffer(Cube.Vertices, new VertexDescriptor(VertexPositionColor.SizeInBytes, 2), false); IndexBuffer ib = rc.ResourceFactory.CreateIndexBuffer(Cube.Indices, false); string folder = rc.BackendType == GraphicsBackend.Direct3D11 ? "HLSL" : "GLSL"; string extension = rc.BackendType == GraphicsBackend.Direct3D11 ? "hlsl" : "glsl"; VertexInputLayout inputLayout = rc.ResourceFactory.CreateInputLayout(new VertexInputDescription[] { new VertexInputDescription( new VertexInputElement("Position", VertexSemanticType.Position, VertexElementFormat.Float3), new VertexInputElement("Color", VertexSemanticType.Color, VertexElementFormat.Float4)) }); string vsPath = Path.Combine(AppContext.BaseDirectory, folder, $"vertex.{extension}"); string fsPath = Path.Combine(AppContext.BaseDirectory, folder, $"fragment.{extension}"); Shader vs = rc.ResourceFactory.CreateShader(ShaderStages.Vertex, File.ReadAllText(vsPath)); Shader fs = rc.ResourceFactory.CreateShader(ShaderStages.Fragment, File.ReadAllText(fsPath)); ShaderSet shaderSet = rc.ResourceFactory.CreateShaderSet(inputLayout, vs, fs); ShaderResourceBindingSlots bindingSlots = rc.ResourceFactory.CreateShaderResourceBindingSlots( shaderSet, new ShaderResourceDescription("ViewProjectionMatrix", ShaderConstantType.Matrix4x4)); ConstantBuffer viewProjectionBuffer = rc.ResourceFactory.CreateConstantBuffer(ShaderConstantType.Matrix4x4); while (window.Exists) { InputSnapshot snapshot = window.PumpEvents(); rc.ClearBuffer(); rc.SetViewport(0, 0, window.Width, window.Height); float timeFactor = Environment.TickCount / 1000f; viewProjectionBuffer.SetData( Matrix4x4.CreateLookAt( new Vector3(2 * (float)Math.Sin(timeFactor), (float)Math.Sin(timeFactor), 2 * (float)Math.Cos(timeFactor)), Vector3.Zero, Vector3.UnitY) * Matrix4x4.CreatePerspectiveFieldOfView(1.05f, (float)window.Width / window.Height, .5f, 10f)); rc.SetVertexBuffer(0, vb); rc.IndexBuffer = ib; rc.ShaderSet = shaderSet; rc.ShaderResourceBindingSlots = bindingSlots; rc.SetConstantBuffer(0, viewProjectionBuffer); rc.DrawIndexedPrimitives(Cube.Indices.Length); rc.SwapBuffers(); } }
protected override void PlatformSetSamplerState(int slot, SamplerState samplerState) { OpenGLSamplerState glSampler = (OpenGLSamplerState)samplerState; OpenGLTextureBindingSlotInfo info = ShaderResourceBindingSlots.GetSamplerBindingInfo(slot); _textureSamplerManager.SetSampler(info.RelativeIndex, glSampler); }
protected override void PlatformSetTexture(int slot, ShaderTextureBinding textureBinding) { OpenGLTextureBinding glTextureBinding = (OpenGLTextureBinding)textureBinding; OpenGLTextureBindingSlotInfo info = ShaderResourceBindingSlots.GetTextureBindingInfo(slot); _textureSamplerManager.SetTexture(info.RelativeIndex, glTextureBinding); ShaderSet.UpdateTextureUniform(info.UniformLocation, info.RelativeIndex); }
private void NullInputs() { for (int i = 0; i < MaxVertexBuffers; i++) { _vertexBuffers[i] = null; } _indexBuffer = null; _resourceBindingSlots = null; }
public unsafe void ChangeRenderContext(AssetDatabase ad, RenderContext rc) { var factory = rc.ResourceFactory; _vb = factory.CreateVertexBuffer(s_vertices.Length * VertexPosition.SizeInBytes, false); _vb.SetVertexData(s_vertices, new VertexDescriptor(VertexPosition.SizeInBytes, 1, IntPtr.Zero)); _ib = factory.CreateIndexBuffer(s_indices.Length * sizeof(int), false); _ib.SetIndices(s_indices); Shader vs = factory.CreateShader(ShaderStages.Vertex, ShaderHelper.LoadShaderCode("skybox-vertex", ShaderStages.Vertex, rc.ResourceFactory)); Shader fs = factory.CreateShader(ShaderStages.Fragment, ShaderHelper.LoadShaderCode("skybox-frag", ShaderStages.Fragment, rc.ResourceFactory)); VertexInputLayout inputLayout = factory.CreateInputLayout( new VertexInputDescription( 12, new VertexInputElement("position", VertexSemanticType.Position, VertexElementFormat.Float3))); ShaderSet shaderSet = factory.CreateShaderSet(inputLayout, vs, fs); ShaderResourceBindingSlots constantSlots = factory.CreateShaderResourceBindingSlots( shaderSet, new ShaderResourceDescription("ProjectionMatrixBuffer", ShaderConstantType.Matrix4x4), new ShaderResourceDescription("ViewMatrixBuffer", ShaderConstantType.Matrix4x4), new ShaderResourceDescription("Skybox", ShaderResourceType.Texture), new ShaderResourceDescription("Skybox", ShaderResourceType.Sampler)); _material = new Material(shaderSet, constantSlots); _viewMatrixBuffer = factory.CreateConstantBuffer(ShaderConstantType.Matrix4x4); fixed(Rgba32 *frontPin = &_front.ISImage.DangerousGetPinnableReferenceToPixelBuffer()) fixed(Rgba32 * backPin = &_back.ISImage.DangerousGetPinnableReferenceToPixelBuffer()) fixed(Rgba32 * leftPin = &_left.ISImage.DangerousGetPinnableReferenceToPixelBuffer()) fixed(Rgba32 * rightPin = &_right.ISImage.DangerousGetPinnableReferenceToPixelBuffer()) fixed(Rgba32 * topPin = &_top.ISImage.DangerousGetPinnableReferenceToPixelBuffer()) fixed(Rgba32 * bottomPin = &_bottom.ISImage.DangerousGetPinnableReferenceToPixelBuffer()) { var cubemapTexture = factory.CreateCubemapTexture( (IntPtr)frontPin, (IntPtr)backPin, (IntPtr)leftPin, (IntPtr)rightPin, (IntPtr)topPin, (IntPtr)bottomPin, _front.Width, _front.Height, _front.PixelSizeInBytes, _front.Format); _cubemapBinding = factory.CreateShaderTextureBinding(cubemapTexture); } _rasterizerState = factory.CreateRasterizerState(FaceCullingMode.None, TriangleFillMode.Solid, false, false); _viewProvider = new DependantDataProvider <Matrix4x4>(SharedDataProviders.GetProvider <Matrix4x4>("ViewMatrix"), Utilities.ConvertToMatrix3x3); }
private void InitializeContextObjects(AssetDatabase ad, RenderContext rc) { ResourceFactory factory = rc.ResourceFactory; MeshData sphere = ad.LoadAsset <ObjFile>(new AssetID("Models/Sphere.obj")).GetFirstMesh(); Vector3[] spherePositions = sphere.GetVertexPositions(); _sphereGeometryVB = factory.CreateVertexBuffer(spherePositions.Length * 12, false); _sphereGeometryVB.SetVertexData(spherePositions, new VertexDescriptor(12, 1)); _ib = sphere.CreateIndexBuffer(factory, out _indexCount); Random r = new Random(); int width = InstanceRows; InstanceData[] instanceData = new InstanceData[width * width * width]; for (int z = 0; z < width; z++) { for (int y = 0; y < width; y++) { for (int x = 0; x < width; x++) { instanceData[z * width * width + y * width + x] = new InstanceData( new Vector3(x * 10, y * 10, z * 10), new RgbaFloat((float)r.NextDouble(), (float)r.NextDouble(), (float)r.NextDouble(), (float)r.NextDouble())); } } } _instanceVB = factory.CreateVertexBuffer(instanceData.Length * InstanceData.SizeInBytes, false); _instanceVB.SetVertexData(instanceData, new VertexDescriptor(InstanceData.SizeInBytes, 2, 0, IntPtr.Zero)); Shader vs = factory.CreateShader(ShaderStages.Vertex, ShaderHelper.LoadShaderCode("instanced-simple-vertex", ShaderStages.Vertex, rc.ResourceFactory)); Shader fs = factory.CreateShader(ShaderStages.Fragment, ShaderHelper.LoadShaderCode("instanced-simple-frag", ShaderStages.Fragment, rc.ResourceFactory)); VertexInputLayout inputLayout = factory.CreateInputLayout( new VertexInputDescription(VertexPosition.SizeInBytes, new VertexInputElement("in_position", VertexSemanticType.Position, VertexElementFormat.Float3)), new VertexInputDescription( InstanceData.SizeInBytes, new VertexInputElement("in_offset", VertexSemanticType.TextureCoordinate, VertexElementFormat.Float3, VertexElementInputClass.PerInstance, 1), new VertexInputElement("in_color", VertexSemanticType.Color, VertexElementFormat.Float4, VertexElementInputClass.PerInstance, 1))); ShaderSet shaderSet = factory.CreateShaderSet(inputLayout, vs, fs); ShaderResourceBindingSlots constantBindings = factory.CreateShaderResourceBindingSlots( shaderSet, new ShaderResourceDescription("ProjectionMatrixBuffer", ShaderConstantType.Matrix4x4), new ShaderResourceDescription("ViewMatrixBuffer", ShaderConstantType.Matrix4x4), new ShaderResourceDescription("WorldMatrixBuffer", ShaderConstantType.Matrix4x4)); _material = new Material(shaderSet, constantBindings); _worldBuffer = factory.CreateConstantBuffer(ShaderConstantType.Matrix4x4); Matrix4x4 identity = Matrix4x4.Identity; _worldBuffer.SetData(ref identity, 64); }
protected override void PlatformSetConstantBuffer(int slot, ConstantBuffer cb) { OpenGLUniformBinding binding = ShaderResourceBindingSlots.GetUniformBindingForSlot(slot); if (binding.BlockLocation != -1) { BindUniformBlock(ShaderSet, slot, binding.BlockLocation, (OpenGLConstantBuffer)cb); } else { Debug.Assert(binding.StorageAdapter != null); SetUniformLocationDataSlow((OpenGLConstantBuffer)cb, binding.StorageAdapter); } }
protected override void PlatformSetConstantBuffer(int slot, ConstantBuffer cb) { OpenGLESUniformBinding binding = ShaderResourceBindingSlots.GetUniformBindingForSlot(slot); ((OpenGLESConstantBuffer)cb).BindToBlock(ShaderSet.ProgramID, binding.BlockLocation, ((OpenGLESConstantBuffer)cb).BufferSize, slot); //if (binding.BlockLocation != -1) //{ // BindUniformBlock(ShaderSet, slot, binding.BlockLocation, (OpenGLESConstantBuffer)cb); //} //else //{ // Debug.Assert(binding.StorageAdapter != null); // SetUniformLocationDataSlow((OpenGLESConstantBuffer)cb, binding.StorageAdapter); //} }
public static Material CreateShadowPassMaterial(ResourceFactory factory) { Shader vs = factory.CreateShader(ShaderStages.Vertex, ShaderHelper.LoadShaderCode("shadowmap-vertex", ShaderStages.Vertex, factory)); Shader fs = factory.CreateShader(ShaderStages.Fragment, ShaderHelper.LoadShaderCode("shadowmap-frag", ShaderStages.Fragment, factory)); VertexInputLayout inputLayout = factory.CreateInputLayout( new VertexInputDescription( 12, new VertexInputElement("in_position", VertexSemanticType.Position, VertexElementFormat.Float3))); ShaderSet shaderSet = factory.CreateShaderSet(inputLayout, vs, fs); ShaderResourceBindingSlots constantSlots = factory.CreateShaderResourceBindingSlots( shaderSet, new ShaderResourceDescription("ProjectionMatrixBuffer", ShaderConstantType.Matrix4x4), // Light Projection new ShaderResourceDescription("ViewMatrixBuffer", ShaderConstantType.Matrix4x4), // Light View new ShaderResourceDescription("WorldMatrixBuffer", ShaderConstantType.Matrix4x4)); return(new Material(shaderSet, constantSlots)); }
public static Material CreateMaterial( this ResourceFactory factory, RenderContext rc, string vertexShaderName, string fragmentShaderName, VertexInputDescription[] vertexInputs, ShaderResourceDescription[] resources) { Shader vs = factory.CreateShader(ShaderStages.Vertex, ShaderHelper.LoadShaderCode(vertexShaderName, ShaderStages.Vertex, rc.ResourceFactory)); Shader fs = factory.CreateShader(ShaderStages.Fragment, ShaderHelper.LoadShaderCode(fragmentShaderName, ShaderStages.Fragment, rc.ResourceFactory)); VertexInputLayout inputLayout = factory.CreateInputLayout(vertexInputs); ShaderSet shaderSet = factory.CreateShaderSet(inputLayout, vs, fs); ShaderResourceBindingSlots resourceBindings = factory.CreateShaderResourceBindingSlots(shaderSet, resources); return(new Material(shaderSet, resourceBindings)); }
private void InitializeContextObjects(AssetDatabase ad, RenderContext rc) { ResourceFactory factory = rc.ResourceFactory; _vb = factory.CreateVertexBuffer(new[] { new VertexPosition(Vector3.Zero) }, new VertexDescriptor(12, 1), false); _ib = factory.CreateIndexBuffer(new ushort[] { 0 }, false); Shader vertexShader = factory.CreateShader(ShaderStages.Vertex, ShaderHelper.LoadShaderCode("geometry-vertex", ShaderStages.Vertex, rc.ResourceFactory)); Shader geometryShader = factory.CreateShader(ShaderStages.Geometry, ShaderHelper.LoadShaderCode(_geometryShaderName, ShaderStages.Geometry, rc.ResourceFactory)); Shader fragmentShader = factory.CreateShader(ShaderStages.Fragment, ShaderHelper.LoadShaderCode("geometry-frag", ShaderStages.Fragment, rc.ResourceFactory)); VertexInputLayout inputLayout = factory.CreateInputLayout( new VertexInputDescription(12, new VertexInputElement("in_position", VertexSemanticType.Position, VertexElementFormat.Float3))); ShaderSet shaderSet = factory.CreateShaderSet(inputLayout, vertexShader, geometryShader, fragmentShader); ShaderResourceBindingSlots constantBindings = factory.CreateShaderResourceBindingSlots( shaderSet, new ShaderResourceDescription("ProjectionMatrixBuffer", ShaderConstantType.Matrix4x4), // Global new ShaderResourceDescription("ViewMatrixBuffer", ShaderConstantType.Matrix4x4), // Global new ShaderResourceDescription("CameraInfoBuffer", Unsafe.SizeOf <Camera.Info>()), // Global new ShaderResourceDescription("WorldMatrixBuffer", ShaderConstantType.Matrix4x4)); // Local _material = new Material(shaderSet, constantBindings); _worldMatrixBuffer = factory.CreateConstantBuffer(ShaderConstantType.Matrix4x4); }
public void CreateDeviceResources(RenderContext rc) { _rc = rc; ResourceFactory factory = rc.ResourceFactory; _vertexBuffer = factory.CreateVertexBuffer(1000, true); _indexBuffer = factory.CreateIndexBuffer(500, true); _blendState = factory.CreateCustomBlendState( true, Blend.InverseSourceAlpha, Blend.Zero, BlendFunction.Add, Blend.SourceAlpha, Blend.InverseSourceAlpha, BlendFunction.Add, RgbaFloat.Black); _depthDisabledState = factory.CreateDepthStencilState(false, DepthComparison.Always); _rasterizerState = factory.CreateRasterizerState(FaceCullingMode.None, TriangleFillMode.Solid, true, true); RecreateFontDeviceTexture(rc); var vertexShaderCode = LoadEmbeddedShaderCode(rc.ResourceFactory, "imgui-vertex", ShaderStages.Vertex); var fragmentShaderCode = LoadEmbeddedShaderCode(rc.ResourceFactory, "imgui-frag", ShaderStages.Fragment); Shader vertexShader = factory.CreateShader(ShaderStages.Vertex, vertexShaderCode); Shader fragmentShader = factory.CreateShader(ShaderStages.Fragment, fragmentShaderCode); VertexInputLayout inputLayout = factory.CreateInputLayout( new VertexInputDescription(20, new VertexInputElement[] { new VertexInputElement("in_position", VertexSemanticType.Position, VertexElementFormat.Float2), new VertexInputElement("in_texCoord", VertexSemanticType.TextureCoordinate, VertexElementFormat.Float2), new VertexInputElement("in_color", VertexSemanticType.Color, VertexElementFormat.Byte4) })); _shaderSet = factory.CreateShaderSet(inputLayout, vertexShader, fragmentShader); _resourceBindings = factory.CreateShaderResourceBindingSlots( _shaderSet, new ShaderResourceDescription("ProjectionMatrixBuffer", ShaderConstantType.Matrix4x4), new ShaderResourceDescription("FontTexture", ShaderResourceType.Texture), new ShaderResourceDescription("FontSampler", ShaderResourceType.Sampler)); _projMatrixBuffer = factory.CreateConstantBuffer(ShaderConstantType.Matrix4x4); }
public BasicDemoApp(Sdl2Window window, RenderContext rc) { _window = window; _rc = rc; _window.Closed += () => _running = false; _window.Resized += () => _windowResized = true; ResourceFactory factory = _rc.ResourceFactory; _vb = factory.CreateVertexBuffer(s_cubeVertices, new VertexDescriptor(VertexPositionTexture.SizeInBytes, VertexPositionTexture.ElementCount), false); _ib = factory.CreateIndexBuffer(s_cubeIndices, false); Shader vertexShader = factory.CreateShader(ShaderStages.Vertex, factory.LoadProcessedShader(GetShaderBytecode(factory.BackendType, true))); Shader fragmentShader = factory.CreateShader(ShaderStages.Fragment, factory.LoadProcessedShader(GetShaderBytecode(factory.BackendType, false))); VertexInputLayout inputLayout = factory.CreateInputLayout( new VertexInputElement("vsin_position", VertexSemanticType.Position, VertexElementFormat.Float3), new VertexInputElement("vsin_texCoord", VertexSemanticType.TextureCoordinate, VertexElementFormat.Float2)); _shaderSet = factory.CreateShaderSet(inputLayout, vertexShader, fragmentShader); _resourceBindings = factory.CreateShaderResourceBindingSlots( _shaderSet, new ShaderResourceDescription("WorldViewProjectionBuffer", ShaderConstantType.Matrix4x4), new ShaderResourceDescription("SurfaceTexture", ShaderResourceType.Texture, ShaderStages.Fragment), new ShaderResourceDescription("Sampler", ShaderResourceType.Sampler, ShaderStages.Fragment)); _worldBuffer = factory.CreateConstantBuffer(ShaderConstantType.Matrix4x4); _viewBuffer = factory.CreateConstantBuffer(ShaderConstantType.Matrix4x4); _projectionBuffer = factory.CreateConstantBuffer(ShaderConstantType.Matrix4x4); TextureData textureData = new ImageSharpMipmapChain( Path.Combine(AppContext.BaseDirectory, "Textures", "Sponza_Bricks.png")); _deviceTexture = textureData.CreateDeviceTexture(factory); _textureBinding = factory.CreateShaderTextureBinding(_deviceTexture); _worldBuffer.SetData(Matrix4x4.Identity); _viewBuffer.SetData(Matrix4x4.CreateLookAt(new Vector3(0, 0, -5), Vector3.Zero, Vector3.UnitY)); }
protected abstract void PlatformSetShaderResourceBindingSlots(ShaderResourceBindingSlots shaderConstantBindings);
protected override void PlatformSetShaderResourceBindingSlots(ShaderResourceBindingSlots shaderConstantBindings) { }