internal void CreateResources(DeviceTexture blurTarget) { ThrowIfDisposed(); //Dispose of the resources targetB?.Dispose(); samplerHor?.Dispose(); samplerVer?.Dispose(); //Create a temp target (same format and size as the blur-target) so we can ping-point //between the original target and this temp target for each blur step targetB = DeviceTexture.CreateColorTarget(blurTarget.Size, blurTarget.Format, scene); //Create samplers samplerHor = new DeviceSampler(scene.LogicalDevice, blurTarget, disposeTexture: false); samplerVer = new DeviceSampler(scene.LogicalDevice, targetB, disposeTexture: false); //Bind the inputs rendererHor.BindGlobalInputs(new IShaderInput[] { samplerHor }); rendererVer.BindGlobalInputs(new IShaderInput[] { samplerVer }); //Bind the targets rendererHor.BindTargets(new [] { targetB }); rendererVer.BindTargets(new [] { blurTarget }); //Tell the renderers to allocate their resources based on the data we've provided rendererHor.CreateResources(); rendererVer.CreateResources(); }
internal void CreateResources(Int2 swapchainSize, IShaderInput sceneData) { ThrowIfDisposed(); //Dispose of the old target depthTarget?.Dispose(); //Dispose of the old sampler depthSampler?.Dispose(); //Create the new render target depthTarget = DeviceTexture.CreateDepthTarget((targetSize, targetSize), depthFormat, scene); //Create sampler depthSampler = new DeviceSampler(scene.LogicalDevice, depthTarget, disposeTexture: false); //Bind inputs to the renderer renderer.BindGlobalInputs(new IShaderInput[] { sceneData, cameraBuffer }); //Bind the targets to the renderer renderer.BindTargets(new [] { depthTarget }); //Tell the renderer to allocate its resources based on the data we've provided renderer.CreateResources(); //Store the aspect of the swapchain, we need it later to calculate the shadow frustum swapchainAspect = (float)swapchainSize.X / swapchainSize.Y; }
internal void CreateResources(Int2 swapchainSize) { ThrowIfDisposed(); //Dispose of the old target bloomTarget?.Dispose(); //Dispose of the old sampler bloomSampler?.Dispose(); //Create the new render target bloomTarget = DeviceTexture.CreateColorTarget( size: (swapchainSize * targetSizeMultiplier).RoundToInt(), bloomFormat, scene); //Create sampler bloomSampler = new DeviceSampler(scene.LogicalDevice, bloomTarget, disposeTexture: false); //Bind inputs to the renderer renderer.BindGlobalInputs(new IShaderInput[] { gbufferTechnique.ColorOutput }); //Bind the targets to the renderer renderer.BindTargets(new [] { bloomTarget }); //Tell the renderer to allocate its resources based on the data we've provided renderer.CreateResources(); //Initialize the blurTechnique, point it to the bloom-target blurTechnique.CreateResources(bloomTarget); }
public override void CreateDeviceObjects(RenderContext rc) { ResourceFactory factory = rc.ResourceFactory; _vb = factory.CreateVertexBuffer( new VertexPosition[] { new VertexPosition(new Vector3(-1000, 0, -1000)), new VertexPosition(new Vector3(+1000, 0, -1000)), new VertexPosition(new Vector3(+1000, 0, +1000)), new VertexPosition(new Vector3(-1000, 0, +1000)), }, new VertexDescriptor(VertexPosition.SizeInBytes, VertexPosition.ElementCount), false); _ib = factory.CreateIndexBuffer(new ushort[] { 0, 1, 2, 0, 2, 3 }, false); GridSetInfo.CreateAll( factory, ShaderHelper.LoadBytecode(factory, "Grid", ShaderStages.Vertex), ShaderHelper.LoadBytecode(factory, "Grid", ShaderStages.Fragment), out _shaderSet, out _resourceBindings); const int gridSize = 64; RgbaByte borderColor = new RgbaByte(255, 255, 255, 150); RgbaByte[] pixels = CreateGridTexturePixels(gridSize, 1, borderColor, new RgbaByte()); _gridTexture = factory.CreateTexture(pixels, gridSize, gridSize, PixelFormat.R8_G8_B8_A8_UInt); _textureBinding = factory.CreateShaderTextureBinding(_gridTexture); _rasterizerState = factory.CreateRasterizerState(FaceCullingMode.None, TriangleFillMode.Solid, true, true); }
internal void CreateResources(Int2 swapchainSize) { ThrowIfDisposed(); //Dispose of the old target aoTarget?.Dispose(); //Create the new render target aoTarget = DeviceTexture.CreateColorTarget( size: (swapchainSize * targetSizeMultiplier).RoundToInt(), aoFormat, scene); //Bind inputs to the renderer renderer.BindGlobalInputs(new IShaderInput[] { gbufferTechnique.CameraOutput, gbufferTechnique.DepthOutput, gbufferTechnique.NormalOutput, sampleKernelBuffer, rotationNoiseSampler }); //Bind the targets to the renderer renderer.BindTargets(new [] { aoTarget }); //Tell the renderer to allocate its resources based on the data we've provided renderer.CreateResources(); //Initialize the blurTechnique, point it to the ao-target blurTechnique.CreateResources(aoTarget); }
/// <summary> /// Wraps an OpenGL texture. /// </summary> /// <param name="texture">The OpenGL texture</param> /// <returns>The texture</returns> public static Texture FromDeviceTexture(DeviceTexture texture) { return(new Texture { deviceTexture = texture }); }
private void InitializeContextObjects(RenderContext rc, MaterialCache materialCache, BufferCache bufferCache) { ResourceFactory factory = rc.ResourceFactory; _instanceDataVB = factory.CreateVertexBuffer(InstanceData.SizeInBytes * 10, true); _ib = factory.CreateIndexBuffer(new[] { 0 }, false); if (_texture == null) { _texture = RawTextureDataArray <RgbaFloat> .FromSingleColor(RgbaFloat.Pink); } _deviceTexture = _texture.CreateDeviceTexture(factory); _textureBinding = factory.CreateShaderTextureBinding(_deviceTexture); _material = materialCache.GetMaterial(rc, "passthrough-vertex", "billboard-geometry", "particle-fragment", s_vertexInputs, s_globalInputs, s_perObjectInputs, s_textureInputs); _depthStencilState = factory.CreateDepthStencilState(true, DepthComparison.LessEqual, true); #if DEBUG_PARTICLE_BOUNDS var briwr = new BoundsRenderItemWireframeRenderer(this, rc); _gs.AddRenderItem(briwr, Transform); #endif _initialized = true; }
internal void CreateResources(DeviceTexture blurTarget) { ThrowIfDisposed(); //Dispose of the resources outputTarget?.Dispose(); inputSampler?.Dispose(); outputSampler?.Dispose(); //Create a output target (same format and size as the blur-target) outputTarget = DeviceTexture.CreateColorTarget(blurTarget.Size, blurTarget.Format, scene); //Create samplers inputSampler = new DeviceSampler(scene.LogicalDevice, blurTarget, disposeTexture: false); outputSampler = new DeviceSampler(scene.LogicalDevice, outputTarget, disposeTexture: false); //Bind the input renderer.BindGlobalInputs(new IShaderInput[] { inputSampler }); //Bind the target renderer.BindTargets(new [] { outputTarget }); //Tell the renderer to allocate its resources based on the data we've provided renderer.CreateResources(); }
public override ShaderTextureBinding CreateShaderTextureBinding(DeviceTexture texture) { D3DTexture d3dTexture = (D3DTexture)texture; ShaderResourceViewDescription srvd = d3dTexture.GetShaderResourceViewDescription(); ShaderResourceView srv = new ShaderResourceView(_device, d3dTexture.DeviceTexture, srvd); return(new D3DTextureBinding(srv, d3dTexture)); }
private void RecreateTexture() { _deviceTexture.Dispose(); _textureBinding.Dispose(); _texture = _textureRef.Get(_ad); _deviceTexture = _texture.CreateDeviceTexture(_gs.Context.ResourceFactory); _textureBinding = _gs.Context.ResourceFactory.CreateShaderTextureBinding(_deviceTexture); }
public override ShaderTextureBinding CreateShaderTextureBinding(DeviceTexture texture) { if (texture is OpenGLTexture2D) { return(new OpenGLTextureBinding((OpenGLTexture2D)texture)); } else { return(new OpenGLTextureBinding((OpenGLCubemapTexture)texture)); } }
private void InitializeContextObjects(RenderContext rc) { ResourceFactory factory = rc.ResourceFactory; _vb = factory.CreateVertexBuffer(1024, true); _ib = factory.CreateIndexBuffer(1024, true, IndexFormat.UInt16); _material = CreateWireframeMaterial(rc); _texture = _textureData.CreateDeviceTexture(factory); _textureBinding = factory.CreateShaderTextureBinding(_texture); _wireframeState = factory.CreateRasterizerState(FaceCullingMode.None, TriangleFillMode.Wireframe, true, true); }
public AxesRenderer(RenderContext rc, GraphicsSystem gs) { _gs = gs; _vb = rc.ResourceFactory.CreateVertexBuffer(6 * VertexPositionColor.SizeInBytes, false); const float opacity = 0.56f; RgbaFloat red = new RgbaFloat(1, 0, 0, opacity); RgbaFloat green = new RgbaFloat(0, 1, 0, opacity); RgbaFloat blue = new RgbaFloat(0, 0, 1, opacity); SetPlaneVertices( new Vector3(PlaneLength, 0, PlaneLength), new Vector3(PlaneLength, PlaneLength, 0), new Vector3(0, PlaneLength, PlaneLength)); _ib = rc.ResourceFactory.CreateIndexBuffer(6 * 4, false); _ib.SetIndices( new int[] { 0, 1, 2, 3, 4, 5, // Lines // Planes 6, 7, 8, 6, 8, 9, 10, 11, 12, 10, 12, 13, 14, 15, 16, 14, 16, 17, // Solid plane borders 18, 19, 19, 20, 20, 21, 21, 18, 22, 23, 23, 24, 24, 25, 25, 22, 26, 27, 27, 28, 28, 29, 29, 26 }, 0, 0); _lineIndicesCount = 6; _material = CreateMaterial(rc); _pointerVB = ArrowPointerModel.MeshData.CreateVertexBuffer(rc.ResourceFactory); _pointerIB = ArrowPointerModel.MeshData.CreateIndexBuffer(rc.ResourceFactory, out _pointerIndexCount); _pointerMaterial = CreatePointerMaterial(rc); _redTexture = RawTextureDataArray <RgbaFloat> .FromSingleColor(RgbaFloat.Red).CreateDeviceTexture(rc.ResourceFactory); _greenTexture = RawTextureDataArray <RgbaFloat> .FromSingleColor(RgbaFloat.Green).CreateDeviceTexture(rc.ResourceFactory); _blueTexture = RawTextureDataArray <RgbaFloat> .FromSingleColor(RgbaFloat.Blue).CreateDeviceTexture(rc.ResourceFactory); _redBinding = rc.ResourceFactory.CreateShaderTextureBinding(_redTexture); _greenBinding = rc.ResourceFactory.CreateShaderTextureBinding(_greenTexture); _blueBinding = rc.ResourceFactory.CreateShaderTextureBinding(_blueTexture); _dss = rc.ResourceFactory.CreateDepthStencilState(false, DepthComparison.Always); _rs = rc.ResourceFactory.CreateRasterizerState(FaceCullingMode.None, TriangleFillMode.Solid, true, true); }
public RenderTexture(int width, int height) { view = new View(new Viewport(0, height, width, 0), new Vector2(0, 0), new Vector2(width, height)); depthBuffer = new DepthBuffer(width, height); framebuffer = new Framebuffer(); deviceTexture = new DeviceTexture(DefaultShader.DEFFAULT_TEXTURE_UNIFORM_NAME, width, height, false); deviceTexture.Bind(); framebuffer.Bind(); deviceTexture.CopyRawData(IntPtr.Zero, 0); framebuffer.AttachDepthBuffer(depthBuffer); framebuffer.AttachTexture(deviceTexture); deviceTexture.Unbind(); framebuffer.Use(); framebuffer.Check(); texture = Texture.FromDeviceTexture(deviceTexture); framebuffer.Unbind(); }
internal void CreateResources(Int2 swapchainSize, IShaderInput sceneData) { ThrowIfDisposed(); //Dispose of the old targets colorTarget?.Dispose(); normalTarget?.Dispose(); attributeTarget?.Dispose(); depthTarget?.Dispose(); //Dispose of the old samplers colorSampler?.Dispose(); normalSampler?.Dispose(); attributeSampler?.Dispose(); depthSampler?.Dispose(); //Create the new render targets colorTarget = DeviceTexture.CreateColorTarget(swapchainSize, colorFormat, scene); normalTarget = DeviceTexture.CreateColorTarget(swapchainSize, normalFormat, scene); attributeTarget = DeviceTexture.CreateColorTarget(swapchainSize, attributeFormat, scene); depthTarget = DeviceTexture.CreateDepthTarget(swapchainSize, depthFormat, scene); //Create samplers for the targets colorSampler = new DeviceSampler(scene.LogicalDevice, colorTarget, disposeTexture: false); normalSampler = new DeviceSampler(scene.LogicalDevice, normalTarget, disposeTexture: false); attributeSampler = new DeviceSampler(scene.LogicalDevice, attributeTarget, disposeTexture: false); depthSampler = new DeviceSampler(scene.LogicalDevice, depthTarget, disposeTexture: false); //Bind inputs to the renderer renderer.BindGlobalInputs(new IShaderInput[] { sceneData, cameraBuffer }); //Bind the targets to the renderer renderer.BindTargets(new [] { colorTarget, normalTarget, attributeTarget, depthTarget }); //Tell the renderer to allocate its resources based on the data we've provided renderer.CreateResources(); }
protected void Setup() { var vs = DefaultShader.FromType(typeof(VertexPositionColorTexture), PlainCore.Graphics.Core.ShaderType.Vertex); var fs = DefaultShader.FromType(typeof(VertexPositionColorTexture), PlainCore.Graphics.Core.ShaderType.Fragment); pipeline = new ShaderPipeline(vs, fs); buffer = new VertexArrayBuffer <VertexPositionColorTexture>(32, OpenGL.BufferUsage.StaticDraw); indexBuffer = new IndexBuffer <VertexPositionColorTexture>(OpenGL.BufferUsage.StaticDraw); vao = new VertexArrayObject <VertexPositionColorTexture>(buffer, pipeline, DefaultVertexDefinition.FromType(typeof(VertexPositionColorTexture))); texture = new DeviceTexture(DefaultShader.DEFFAULT_TEXTURE_UNIFORM_NAME, 100, 100, true); defaultFramebuffer = Framebuffer.GetDefault(); var imageData = Image.Load("Example.png").SavePixelData(); texture.Bind(); texture.CopyData(imageData); buffer.Bind(); buffer.CopyData(_ArrayPosition); buffer.Unbind(); indexBuffer.Bind(); indexBuffer.CopyData(indexArray); indexBuffer.Unbind(); worldMatrix = new Matrix4fUniform(DefaultShader.MVP_UNIFORM_NAME); }
private async void InitializeContextObjects(RenderContext context, MaterialCache materialCache, BufferCache bufferCache) { ResourceFactory factory = context.ResourceFactory; Debug.Assert(_vb == null); Debug.Assert(_ib == null); Debug.Assert(_deviceTexture == null); Debug.Assert(_textureBinding == null); _vb = bufferCache.GetVertexBuffer(_mesh); CreateIndexBuffer(wasTransparent: false); if (s_regularGlobalInputs == null) { s_regularGlobalInputs = new MaterialInputs <MaterialGlobalInputElement>( new MaterialGlobalInputElement[] { new MaterialGlobalInputElement("ProjectionMatrixBuffer", MaterialInputType.Matrix4x4, "ProjectionMatrix"), new MaterialGlobalInputElement("ViewMatrixBuffer", MaterialInputType.Matrix4x4, "ViewMatrix"), new MaterialGlobalInputElement("LightProjectionMatrixBuffer", MaterialInputType.Matrix4x4, "LightProjMatrix"), new MaterialGlobalInputElement("LightViewMatrixBuffer", MaterialInputType.Matrix4x4, "LightViewMatrix"), new MaterialGlobalInputElement("LightInfoBuffer", MaterialInputType.Custom, "LightBuffer"), new MaterialGlobalInputElement("CameraInfoBuffer", MaterialInputType.Custom, "CameraInfo"), new MaterialGlobalInputElement("PointLightsBuffer", MaterialInputType.Custom, "PointLights") }); } _regularPassMaterial = materialCache.GetMaterial( context, RegularPassVertexShaderSource, RegularPassFragmentShaderSource, s_vertexInputs, s_regularGlobalInputs, s_perObjectInputs, s_textureInputs); _regularPassTransparentMaterial = materialCache.GetMaterial( context, RegularPassTransparentVertexShaderSource, RegularPassTransparentFragmentShaderSource, s_vertexInputs, s_regularGlobalInputs, s_transparentPerObjectInputs, s_transparentTextureInputs); if (_texture == null) { _texture = RawTextureDataArray <RgbaFloat> .FromSingleColor(RgbaFloat.Pink); } _deviceTexture = await _gs.ExecuteOnMainThread(() => _texture.CreateDeviceTexture(factory)); _textureBinding = await _gs.ExecuteOnMainThread(() => factory.CreateShaderTextureBinding(_deviceTexture)); if (s_shadowmapGlobalInputs == null) { s_shadowmapGlobalInputs = new MaterialInputs <MaterialGlobalInputElement>( new MaterialGlobalInputElement[] { new MaterialGlobalInputElement("ProjectionMatrixBuffer", MaterialInputType.Matrix4x4, "LightProjMatrix"), new MaterialGlobalInputElement("ViewMatrixBuffer", MaterialInputType.Matrix4x4, "LightViewMatrix") }); } _shadowPassMaterial = materialCache.GetMaterial( context, ShadowMapPassVertexShaderSource, ShadowMapPassFragmentShaderSource, s_vertexInputs, s_shadowmapGlobalInputs, s_shadowmapPerObjectInputs, MaterialTextureInputs.Empty); if (s_wireframeRS == null) { s_wireframeRS = factory.CreateRasterizerState(FaceCullingMode.None, TriangleFillMode.Wireframe, true, true); } if (s_noCullRS == null) { s_noCullRS = factory.CreateRasterizerState(FaceCullingMode.None, TriangleFillMode.Solid, true, true); } _initialized = true; }
/// <summary> /// Gets or creates a handle for a texture to be drawn with ImGui. /// Pass the returned handle to Image() or ImageButton(). /// </summary> public static IntPtr GetOrCreateImGuiBinding(RenderContext rc, DeviceTexture texture) { var deviceBinding = rc.ResourceFactory.CreateShaderTextureBinding(texture); return(GetOrCreateImGuiBinding(rc, deviceBinding)); }
internal AmbientOcclusionTechnique( GBufferTechnique gbufferTechnique, ShaderProgram postVertProg, ShaderProgram aoFragProg, ShaderProgram boxBlurFragProg, RenderScene scene, Logger logger = null) { if (gbufferTechnique == null) { throw new NullReferenceException(nameof(gbufferTechnique)); } if (postVertProg == null) { throw new ArgumentNullException(nameof(postVertProg)); } if (aoFragProg == null) { throw new ArgumentNullException(nameof(aoFragProg)); } if (boxBlurFragProg == null) { throw new ArgumentNullException(nameof(boxBlurFragProg)); } if (scene == null) { throw new NullReferenceException(nameof(scene)); } this.gbufferTechnique = gbufferTechnique; this.scene = scene; this.logger = logger; //Create renderer for rendering the ao texture renderer = new Renderer(scene, logger); renderer.AddSpecialization(scene.SwapchainCount); renderer.AddSpecialization(sampleKernelSize); renderer.AddSpecialization(sampleRadius); renderer.AddSpecialization(sampleBias); swapchainIndexPushDataBinding = renderer.AddPushData <int>(); targetSizePushBinding = renderer.AddPushData <Int2>(); //Create random for generating the kernel and noise (using a arbitrary fixed seed) IRandom random = new ShiftRandom(seed: 1234); //Create the sample kernel Span <Float4> sampleKernel = stackalloc Float4[sampleKernelSize]; GenerateSampleKernel(random, sampleKernel); sampleKernelBuffer = DeviceBuffer.UploadData <Float4>( sampleKernel, scene, BufferUsages.UniformBuffer); //Create the rotation noise texture Float4Texture rotationNoiseTexture = TextureUtils.CreateRandomFloatTexture( random, min: (-1f, -1f, 0f, 0f), max: (1f, 1f, 0f, 0f), size: (noiseSize, noiseSize)); rotationNoiseSampler = new DeviceSampler(scene.LogicalDevice, texture: DeviceTexture.UploadTexture(rotationNoiseTexture, scene), repeat: true, pointFilter: true); //Add full-screen object for drawing the composition renderObject = new AttributelessObject(scene, vertexCount: 3, new TextureInfo[0]); renderer.AddObject(renderObject, postVertProg, aoFragProg, debugName: "fullscreen"); //Create a BlurTechnique for blurring the ao texture (to hide the ao sample pattern) blurTechnique = new BoxBlurTechnique( postVertProg, boxBlurFragProg, blurSampleRange, sampleScale: 1f, scene, logger); }
protected Texture(int width, int height, byte[] data, bool repeated = false) { deviceTexture = new DeviceTexture(DefaultShader.DEFFAULT_TEXTURE_UNIFORM_NAME, width, height, true, repeated); deviceTexture.Bind(); deviceTexture.CopyData(data); }
public void Dispose() { DeviceTexture.Dispose(); }
/// <summary> /// Creates a new <see cref="ShaderTextureBinding"/> for the given <see cref="DeviceTexture"/>. /// </summary> /// <param name="texture">The <see cref="DeviceTexture"/> to associate with the binding.</param> /// <returns>A new <see cref="ShaderTextureBinding"/>.</returns> public abstract ShaderTextureBinding CreateShaderTextureBinding(DeviceTexture texture);
public override ShaderTextureBinding CreateShaderTextureBinding(DeviceTexture texture) { return(new VkShaderTextureBinding(_device, (VkDeviceTexture)texture)); }