Exemplo n.º 1
0
        public UpscaleStage(RenderContext rc, string stageName, DeviceTexture2D sourceTexture, Framebuffer outputBuffer)
        {
            RenderContext      = rc;
            Name               = stageName;
            _outputFramebuffer = outputBuffer;

            ResourceFactory factory = rc.ResourceFactory;

            _quadVB = factory.CreateVertexBuffer(VertexPositionTexture.SizeInBytes * 4, false);
            _quadVB.SetVertexData(
                new VertexPositionTexture[]
            {
                new VertexPositionTexture(new Vector3(-1, 1, 0), new Vector2(0, 0)),
                new VertexPositionTexture(new Vector3(1, 1, 0), new Vector2(1, 0)),
                new VertexPositionTexture(new Vector3(1, -1, 0), new Vector2(1, 1)),
                new VertexPositionTexture(new Vector3(-1, -1, 0), new Vector2(0, 1))
            }, new VertexDescriptor(VertexPositionTexture.SizeInBytes, 2, 0, IntPtr.Zero));
            _quadIB = factory.CreateIndexBuffer(sizeof(int) * 6, false);
            _quadIB.SetIndices(new int[] { 0, 1, 2, 0, 2, 3 });
            _quadMaterial = factory.CreateMaterial(rc, "simple-2d-vertex", "simple-2d-frag",
                                                   new MaterialVertexInput(
                                                       20,
                                                       new MaterialVertexInputElement("in_position", VertexSemanticType.Position, VertexElementFormat.Float3),
                                                       new MaterialVertexInputElement("in_texCoord", VertexSemanticType.TextureCoordinate, VertexElementFormat.Float2)),
                                                   new MaterialInputs <MaterialGlobalInputElement>(
                                                       new MaterialGlobalInputElement("WorldMatrixBuffer", MaterialInputType.Matrix4x4, _identityProvider),
                                                       new MaterialGlobalInputElement("ProjectionMatrixBuffer", MaterialInputType.Matrix4x4, _identityProvider)),
                                                   MaterialInputs <MaterialPerObjectInputElement> .Empty,
                                                   new MaterialTextureInputs(new ManualTextureInput("SurfaceTexture")));

            if (sourceTexture != null)
            {
                _textureBinding = factory.CreateShaderTextureBinding(sourceTexture);
            }
        }
Exemplo n.º 2
0
        public void AttachColorTexture(int index, DeviceTexture2D texture)
        {
            Bind();
            Debug.Assert(texture is OpenGLESTexture2D);
            OpenGLESTexture2D glTex = (OpenGLESTexture2D)texture;

            _colorTextures[index] = glTex;
            GL.ActiveTexture(TextureUnit.Texture0);
            Utilities.CheckLastGLES3Error();
            glTex.Bind();
            GL.FramebufferTexture2D(
                FramebufferTarget.Framebuffer,
                FramebufferAttachment.ColorAttachment0,
                TextureTarget2d.Texture2D,
                glTex.ID,
                0);
            GL.BindTexture(TextureTarget.Texture2D, 0);
            Utilities.CheckLastGLES3Error();
            DrawBufferMode mode = DrawBufferMode.ColorAttachment0 + index;

            GL.DrawBuffers(1, ref mode);
            Utilities.CheckLastGLES3Error();

            Unbind();
        }
Exemplo n.º 3
0
 private void InitializeContextObjects(RenderContext rc)
 {
     _depthTexture         = rc.ResourceFactory.CreateDepthTexture(DepthMapWidth, DepthMapHeight, sizeof(ushort), PixelFormat.Alpha_UInt16);
     _shadowMapFramebuffer = rc.ResourceFactory.CreateFramebuffer();
     _shadowMapFramebuffer.DepthTexture = _depthTexture;
     rc.GetTextureContextBinding(_contextBindingName).Value = _depthTexture;
 }
Exemplo n.º 4
0
        private void InitializeContextObjects(AssetDatabase ad, RenderContext rc)
        {
            ResourceFactory factory = rc.ResourceFactory;

            _vb = factory.CreateVertexBuffer(_meshData.Vertices.Length * VertexPositionNormalTexture.SizeInBytes, false);
            _vb.SetVertexData(
                _meshData.Vertices,
                new VertexDescriptor(
                    VertexPositionNormalTexture.SizeInBytes,
                    VertexPositionNormalTexture.ElementCount,
                    IntPtr.Zero));
            _ib = factory.CreateIndexBuffer(sizeof(int) * _meshData.Indices.Length, false);
            _ib.SetIndices(_meshData.Indices);

            _shadowPassMaterial  = CreateShadowPassMaterial(rc.ResourceFactory);
            _regularPassMaterial = CreateRegularPassMaterial(rc.ResourceFactory);

            _worldBuffer = factory.CreateConstantBuffer(ShaderConstantType.Matrix4x4);
            _inverseTransposeWorldBuffer = factory.CreateConstantBuffer(ShaderConstantType.Matrix4x4);

            _surfaceTexture        = _textureData.CreateDeviceTexture(factory);
            _surfaceTextureBinding = factory.CreateShaderTextureBinding(_surfaceTexture);

            _shadowMapSampler = rc.ResourceFactory.CreateSamplerState(
                SamplerAddressMode.Border,
                SamplerAddressMode.Border,
                SamplerAddressMode.Border,
                SamplerFilter.MinMagMipPoint,
                1,
                RgbaFloat.White,
                DepthComparison.Always,
                0,
                int.MaxValue,
                0);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Creates a new <see cref="DeviceTexture2D"/>.
        /// </summary>
        /// <param name="pixelData">An array of pixel information.</param>
        /// <param name="width">The width of the texture</param>
        /// <param name="height">The height of the texture</param>
        /// <param name="pixelSizeInBytes">The total size in bytes of the pixel data.</param>
        /// <param name="format">The format of pixel information.</param>
        /// <returns>A new <see cref="DeviceTexture2D"/> containing the given pixel data.</returns>
        public DeviceTexture2D CreateTexture(IntPtr pixelData, int width, int height, PixelFormat format)
        {
            int             pixelSizeInBytes = FormatHelpers.GetPixelSizeInBytes(format);
            DeviceTexture2D tex = CreateTexture(1, width, height, format);

            tex.SetTextureData(0, 0, 0, width, height, pixelData, pixelSizeInBytes * width * height);
            return(tex);
        }
Exemplo n.º 6
0
 public TextureAtlas(RenderContext rc, int size)
 {
     _size           = size;
     _rc             = rc;
     _texture        = rc.ResourceFactory.CreateTexture(IntPtr.Zero, size, size, 1, PixelFormat.R8_UInt);
     _textureBinding = rc.ResourceFactory.CreateShaderTextureBinding(_texture);
     _atlasInfo      = new DynamicDataProvider <FontAtlasInfo>(new FontAtlasInfo(size));
 }
Exemplo n.º 7
0
 public void AttachColorTexture(int index, DeviceTexture2D texture)
 {
     _colorTextures[index]     = (D3DTexture2D)texture;
     _renderTargetViews[index] = new RenderTargetView(_device, _colorTextures[index].DeviceTexture);
     if (index == 0)
     {
         _width  = texture.Width;
         _height = texture.Height;
     }
 }
Exemplo n.º 8
0
        /// <summary>
        /// Creates a new <see cref="DeviceTexture2D"/>.
        /// </summary>
        /// <typeparam name="T">The type of pixel data; must be a value type.</typeparam>
        /// <param name="pixelData">An array of pixel information.</param>
        /// <param name="width">The width of the texture</param>
        /// <param name="height">The height of the texture</param>
        /// <param name="pixelSizeInBytes">The total size in bytes of the pixel data.</param>
        /// <param name="format">The format of pixel information.</param>
        /// <returns>A new <see cref="DeviceTexture2D"/> containing the given pixel data.</returns>
        public DeviceTexture2D CreateTexture <T>(T[] pixelData, int width, int height, PixelFormat format) where T : struct
        {
            int             pixelSizeInBytes = FormatHelpers.GetPixelSizeInBytes(format);
            DeviceTexture2D tex    = CreateTexture(1, width, height, format);
            GCHandle        handle = GCHandle.Alloc(pixelData, GCHandleType.Pinned);

            tex.SetTextureData(0, 0, 0, width, height, handle.AddrOfPinnedObject(), pixelSizeInBytes * width * height);
            handle.Free();
            return(tex);
        }
Exemplo n.º 9
0
 private void InitializeContextObjects(RenderContext rc)
 {
     _depthTexture = rc.ResourceFactory.CreateTexture(
         1,
         DepthMapWidth,
         DepthMapHeight,
         PixelFormat.R16_UInt,
         DeviceTextureCreateOptions.DepthStencil);
     _depthTextureBinding  = rc.ResourceFactory.CreateShaderTextureBinding(_depthTexture);
     _shadowMapFramebuffer = rc.ResourceFactory.CreateFramebuffer();
     _shadowMapFramebuffer.DepthTexture = _depthTexture;
     SharedTextures.SetTextureBinding(_contextBindingName, _depthTextureBinding);
 }
Exemplo n.º 10
0
        private void InitializeContextObjects(RenderContext context)
        {
            _currentContext = context;
            ResourceFactory factory = context.ResourceFactory;

            _vb = factory.CreateVertexBuffer(VertexPositionNormalTexture.SizeInBytes * _vertices.Length, false);
            VertexDescriptor desc = new VertexDescriptor(
                VertexPositionNormalTexture.SizeInBytes,
                VertexPositionNormalTexture.ElementCount,
                IntPtr.Zero);

            _vb.SetVertexData(_vertices, desc);

            _ib = factory.CreateIndexBuffer(sizeof(ushort) * _indices.Length, false);
            _ib.SetIndices(_indices);

            VertexInputDescription materialInputs = new VertexInputDescription(
                VertexPositionNormalTexture.SizeInBytes,
                new VertexInputElement[]
            {
                new VertexInputElement("in_position", VertexSemanticType.Position, VertexElementFormat.Float3),
                new VertexInputElement("in_normal", VertexSemanticType.Normal, VertexElementFormat.Float3),
                new VertexInputElement("in_texCoord", VertexSemanticType.TextureCoordinate, VertexElementFormat.Float2)
            });

            ShaderResourceDescription[] resources = new[]
            {
                new ShaderResourceDescription("ProjectionMatrixBuffer", ShaderConstantType.Matrix4x4),
                new ShaderResourceDescription("ViewMatrixBuffer", ShaderConstantType.Matrix4x4),
                new ShaderResourceDescription("LightBuffer", Unsafe.SizeOf <DirectionalLightBuffer>()),
                new ShaderResourceDescription("WorldMatrixBuffer", ShaderConstantType.Matrix4x4),
                new ShaderResourceDescription("InverseTransposeWorldMatrixBuffer", ShaderConstantType.Matrix4x4),
                new ShaderResourceDescription("surfaceTexture", ShaderResourceType.Texture),
                new ShaderResourceDescription("surfaceTexture", ShaderResourceType.Sampler)
            };
            _material = factory.CreateMaterial(
                context,
                VertexShaderSource,
                FragmentShaderSource,
                materialInputs,
                resources);

            _worldBuffer = factory.CreateConstantBuffer(ShaderConstantType.Matrix4x4);
            _inverseTransposeWorldBuffer = factory.CreateConstantBuffer(ShaderConstantType.Matrix4x4);

            DeviceTexture2D texture = _texture.CreateDeviceTexture(factory);

            _textureBinding = factory.CreateShaderTextureBinding(texture);

            s_wireframeRasterizerState = factory.CreateRasterizerState(FaceCullingMode.None, TriangleFillMode.Wireframe, true, true);
        }
Exemplo n.º 11
0
        /// <summary>
        /// Constructs a mipmapped 2D device texture from this mipmap chain.
        /// </summary>
        /// <param name="factory">The ResourceFactory used to create device resources.</param>
        /// <returns>A mpimapped 2D device texture.</returns>
        public unsafe DeviceTexture2D CreateDeviceTexture(ResourceFactory factory)
        {
            DeviceTexture2D tex = factory.CreateTexture(MipLevels, Width, Height, Format);

            for (int level = 0; level < MipLevels; level++)
            {
                Image <Rgba32> image = Images[level];
                fixed(void *pin = &image.DangerousGetPinnableReferenceToPixelBuffer())
                {
                    tex.SetTextureData(level, 0, 0, image.Width, image.Height, (IntPtr)pin, PixelSizeInBytes * Width * Height);
                }
            }

            return(tex);
        }
Exemplo n.º 12
0
        private void InitializeContextObjects(RenderContext rc)
        {
            var factory = rc.ResourceFactory;
            var mesh    = LoadTeapotMesh();

            _vertexBuffer = factory.CreateVertexBuffer(mesh.Vertices.Length * VertexPositionNormalTexture.SizeInBytes, false);
            _vertexBuffer.SetVertexData(mesh.Vertices, new VertexDescriptor(VertexPositionNormalTexture.SizeInBytes, 3, IntPtr.Zero));

            _indexBuffer = factory.CreateIndexBuffer(mesh.Indices.Length * sizeof(int), false);
            _indexBuffer.SetIndices(mesh.Indices);

            VertexInputDescription materialInputs = new VertexInputDescription(
                VertexPositionNormalTexture.SizeInBytes,
                new VertexInputElement[]
            {
                new VertexInputElement("in_position", VertexSemanticType.Position, VertexElementFormat.Float3),
                new VertexInputElement("in_normal", VertexSemanticType.Normal, VertexElementFormat.Float3),
                new VertexInputElement("in_texCoord", VertexSemanticType.TextureCoordinate, VertexElementFormat.Float2)
            });

            ShaderResourceDescription[] resources = new[]
            {
                new ShaderResourceDescription("ProjectionMatrixBuffer", ShaderConstantType.Matrix4x4),
                new ShaderResourceDescription("ViewMatrixBuffer", ShaderConstantType.Matrix4x4),
                new ShaderResourceDescription("LightBuffer", Unsafe.SizeOf <DirectionalLightBuffer>()),
                new ShaderResourceDescription("WorldMatrixBuffer", ShaderConstantType.Matrix4x4),
                new ShaderResourceDescription("InverseTransposeWorldMatrixBuffer", ShaderConstantType.Matrix4x4),
                new ShaderResourceDescription("surfaceTexture", ShaderResourceType.Texture),
                new ShaderResourceDescription("surfaceTexture", ShaderResourceType.Sampler)
            };

            _material = factory.CreateMaterial(
                rc,
                "textured-vertex",
                "lit-frag",
                materialInputs,
                resources);

            _worldBuffer = factory.CreateConstantBuffer(ShaderConstantType.Matrix4x4);
            _inverseTransposeWorldBuffer = factory.CreateConstantBuffer(ShaderConstantType.Matrix4x4);

            DeviceTexture2D deviceTex = s_cubeTexture.CreateDeviceTexture(factory);

            _textureBinding = factory.CreateShaderTextureBinding(deviceTex);
        }
Exemplo n.º 13
0
        public void AttachColorTexture(int index, DeviceTexture2D texture)
        {
            Bind();
            Debug.Assert(texture is OpenGLTexture2D);
            OpenGLTexture2D glTex = (OpenGLTexture2D)texture;

            _colorTextures[index] = glTex;
            GL.ActiveTexture(TextureUnit.Texture0);
            glTex.Bind();
            GL.FramebufferTexture2D(
                FramebufferTarget.Framebuffer,
                FramebufferAttachment.ColorAttachment0,
                TextureTarget.Texture2D,
                glTex.ID,
                0);
            GL.BindTexture(TextureTarget.Texture2D, 0);
            GL.DrawBuffer(DrawBufferMode.ColorAttachment0 + index);
            Unbind();
        }
Exemplo n.º 14
0
        public void AttachColorTexture(int index, DeviceTexture2D texture)
        {
            Bind();
            Debug.Assert(texture is OpenGLTexture2D);
            OpenGLTexture2D glTex = (OpenGLTexture2D)texture;

            _colorTextures[index] = glTex;
            GL.ActiveTexture(TextureUnit.Texture0);
            glTex.Bind();
            GL.FramebufferTexture2D(
                FramebufferTarget.Framebuffer,
                FramebufferAttachment.ColorAttachment0 + index,
                TextureTarget.Texture2D,
                glTex.ID,
                0);
            GL.BindTexture(TextureTarget.Texture2D, 0);
            // TODO: I'm pretty sure this is supposed to be using glDrawBuffers (plural).
            GL.DrawBuffer(DrawBufferMode.ColorAttachment0 + index);
            Unbind();
        }
Exemplo n.º 15
0
        /// <summary>
        /// Creates a new <see cref="Framebuffer"/> with the given dimensions.
        /// </summary>
        /// <param name="width">The width of the color and depth textures.</param>
        /// <param name="height">The height of the color and depth textures.</param>
        /// <returns></returns>
        public Framebuffer CreateFramebuffer(int width, int height)
        {
            Framebuffer     fb           = CreateFramebuffer();
            DeviceTexture2D colorTexture = CreateTexture(
                1,
                width,
                height,
                PixelFormat.R8_G8_B8_A8_UInt,
                DeviceTextureCreateOptions.RenderTarget);
            DeviceTexture2D depthTexture = CreateTexture(
                1,
                width,
                height,
                PixelFormat.R16_UInt,
                DeviceTextureCreateOptions.DepthStencil);

            fb.ColorTexture = colorTexture;
            fb.DepthTexture = depthTexture;

            return(fb);
        }
Exemplo n.º 16
0
        private void InitializeContextObjects(AssetDatabase ad, RenderContext rc)
        {
            ResourceFactory factory = rc.ResourceFactory;

            _vb = factory.CreateVertexBuffer(_vertices.Length * VertexPositionNormalTexture.SizeInBytes, false);
            _vb.SetVertexData(
                _vertices,
                new VertexDescriptor(
                    VertexPositionNormalTexture.SizeInBytes,
                    VertexPositionNormalTexture.ElementCount,
                    0,
                    IntPtr.Zero));
            _ib = factory.CreateIndexBuffer(sizeof(int) * _indices.Length, false);
            _ib.SetIndices(_indices);

            _shadowPassMaterial  = _shadowPassMaterialAsset.Create(ad, rc);
            _regularPassMaterial = _regularPassMaterialAsset.Create(ad, rc);
            if (_overrideTextureData != null)
            {
                _overrideTexture        = _overrideTextureData.CreateDeviceTexture(factory);
                _overrideTextureBinding = factory.CreateShaderTextureBinding(_overrideTexture);
            }
        }
Exemplo n.º 17
0
        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));
        }
Exemplo n.º 18
0
 public void AttachColorTexture(int index, DeviceTexture2D texture)
 {
     throw new NotSupportedException("Cannot set color textures on OpenGLDefaultFramebuffer.");
 }