示例#1
0
        private void InitializeContextObjects(RenderContext rc)
        {
            ResourceFactory factory = rc.ResourceFactory;

            _vertexBuffer = factory.CreateVertexBuffer(500, false);
            _indexBuffer  = factory.CreateIndexBuffer(100, false);
            _blendState   = factory.CreateCustomBlendState(
                true,
                Blend.InverseSourceAlpha, Blend.Zero, BlendFunction.Add,
                Blend.SourceAlpha, Blend.InverseSourceAlpha, BlendFunction.Add);
            _depthDisabledState = factory.CreateDepthStencilState(false, DepthComparison.Always);
            _rasterizerState    = factory.CreateRasterizerState(FaceCullingMode.None, TriangleFillMode.Solid, true, true);
            _material           = factory.CreateMaterial(
                rc,
                "imgui-vertex", "imgui-frag",
                new MaterialVertexInput(20, new MaterialVertexInputElement[]
            {
                new MaterialVertexInputElement("in_position", VertexSemanticType.Position, VertexElementFormat.Float2),
                new MaterialVertexInputElement("in_texcoord", VertexSemanticType.TextureCoordinate, VertexElementFormat.Float2),
                new MaterialVertexInputElement("in_color", VertexSemanticType.Color, VertexElementFormat.Byte4)
            }),
                new MaterialInputs <MaterialGlobalInputElement>(new MaterialGlobalInputElement[]
            {
                new MaterialGlobalInputElement("ProjectionMatrixBuffer", MaterialInputType.Matrix4x4, _projectionMatrixProvider)
            }),
                MaterialInputs <MaterialPerObjectInputElement> .Empty,
                new MaterialTextureInputs(new MaterialTextureInputElement[]
            {
                new TextureDataInputElement("surfaceTexture", _fontTexture)
            }));

            var deviceTexture = rc.ResourceFactory.CreateTexture(_fontTexture.PixelData, _textureData.Width, _textureData.Height, _textureData.BytesPerPixel, PixelFormat.R8_G8_B8_A8);

            _fontTextureBinding = rc.ResourceFactory.CreateShaderTextureBinding(deviceTexture);
        }
示例#2
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);
        }
示例#3
0
        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;
        }
示例#4
0
        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);
        }
示例#5
0
        private void SetPreupscaleQuality(float value)
        {
            Debug.Assert(value > 0 && value <= 1);
            _preUpscaleQuality = value;
            int width  = (int)(Context.Window.Width * value);
            int height = (int)(width * ((float)Context.Window.Height / Context.Window.Width));

            _upscaleSource?.ColorTexture?.Dispose();
            _upscaleSource?.DepthTexture?.Dispose();
            _upscaleSource?.Dispose();
            _upscaleDepthView?.Dispose();
            _upscaleSource = Context.ResourceFactory.CreateFramebuffer(width, height);

            if (_alphaBlendFramebuffer == null)
            {
                _alphaBlendFramebuffer = Context.ResourceFactory.CreateFramebuffer();
            }
            _alphaBlendFramebuffer.ColorTexture = _upscaleSource.ColorTexture;

            _upscaleDepthView           = Context.ResourceFactory.CreateShaderTextureBinding(_upscaleSource.DepthTexture);
            _upscaleStage.SourceTexture = _upscaleSource.ColorTexture;
            _upscaleStage.Enabled       = true;

            SetOverrideFramebuffers();
        }
示例#6
0
            public PreviewModel(
                VertexBuffer vb,
                IndexBuffer ib,
                int elementCount,
                Material regularMaterial,
                Material shadowmapMaterial,
                ConstantBuffer worldBuffer,
                DynamicDataProvider <Matrix4x4> worldProvider,
                ConstantBuffer inverseTransposeWorldBuffer,
                Dictionary <string, ConstantBuffer> buffersDict,
                ShaderTextureBinding surfaceTextureBinding
                )
            {
                _vb                   = vb;
                _ib                   = ib;
                _elementCount         = elementCount;
                _regularMaterial      = regularMaterial;
                _shadowmapMaterial    = shadowmapMaterial;
                _worldProvider        = worldProvider;
                _inverseWorldProvider = new DependantDataProvider <Matrix4x4>(worldProvider, Utilities.CalculateInverseTranspose);
                _textureBinding       = surfaceTextureBinding;

                _worldBuffer = worldBuffer;
                _inverseTransposeWorldBuffer = inverseTransposeWorldBuffer;
                _buffersDict = buffersDict;
            }
示例#7
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);
            }
        }
示例#8
0
文件: TextureAtlas.cs 项目: zhuowp/ge
 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));
 }
        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);
        }
示例#10
0
        public void SetTexture(int slot, ShaderTextureBinding textureBinding)
        {
            if (_textureBindingSlots == null)
            {
                throw new InvalidOperationException("Cannot call SetTexture when TextureBindingSlots has not been set.");
            }

            PlatformSetTexture(slot, textureBinding);
        }
示例#11
0
        private void RecreateTexture()
        {
            _deviceTexture.Dispose();
            _textureBinding.Dispose();

            _texture        = _textureRef.Get(_ad);
            _deviceTexture  = _texture.CreateDeviceTexture(_gs.Context.ResourceFactory);
            _textureBinding = _gs.Context.ResourceFactory.CreateShaderTextureBinding(_deviceTexture);
        }
示例#12
0
        public void SetTexture(int slot, ShaderTextureBinding textureBinding)
        {
            if (_resourceBindingSlots == null)
            {
                throw new VeldridException("Cannot call SetTexture when TextureBindingSlots has not been set.");
            }

            _boundTexturesBySlot[slot] = textureBinding.BoundTexture;

            PlatformSetTexture(slot, textureBinding);
        }
示例#13
0
        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);
        }
示例#14
0
        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);
        }
示例#15
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);
 }
示例#16
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);
        }
示例#17
0
文件: AxesRenderer.cs 项目: zhuowp/ge
        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);
        }
示例#18
0
        /// <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, ShaderTextureBinding stb)
        {
            ShaderTextureBindingInfo stbi;

            if (!s_textureBindings.TryGetValue(stb.BoundTexture, out stbi))
            {
                var imGuiBinding = new IntPtr(++s_lastAssigned);
                stbi = new ShaderTextureBindingInfo(imGuiBinding, stb);

                s_textureBindings.Add(stb.BoundTexture, stbi);
                s_bindings.Add(imGuiBinding, stbi);
            }

            return(stbi.ImGuiBinding);
        }
示例#19
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);
        }
示例#20
0
 public PreviewModel(
     VertexBuffer vb,
     IndexBuffer ib,
     int elementCount,
     Material regularMaterial,
     Material shadowmapMaterial,
     DynamicDataProvider <Matrix4x4> worldProvider,
     ShaderTextureBinding surfaceTextureBinding = null)
 {
     _vb                   = vb;
     _ib                   = ib;
     _elementCount         = elementCount;
     _regularMaterial      = regularMaterial;
     _shadowmapMaterial    = shadowmapMaterial;
     _worldProvider        = worldProvider;
     _inverseWorldProvider = new DependantDataProvider <Matrix4x4>(worldProvider, Utilities.CalculateInverseTranspose);
     _perObjectInputs      = new ConstantBufferDataProvider[] { _worldProvider, _inverseWorldProvider };
     _textureBinding       = surfaceTextureBinding;
 }
示例#21
0
        public unsafe override void CreateDeviceObjects(RenderContext rc)
        {
            ResourceFactory 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);

            SkyboxSetInfo.CreateAll(
                factory,
                ShaderHelper.LoadBytecode(factory, "Skybox", ShaderStages.Vertex),
                ShaderHelper.LoadBytecode(factory, "Skybox", ShaderStages.Fragment),
                out _shaderSet,
                out _resourceSlots);

            _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())
            {
                _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);
        }
示例#22
0
        public 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, 0, IntPtr.Zero));

            _ib = factory.CreateIndexBuffer(s_indices.Length * sizeof(int), false);
            _ib.SetIndices(s_indices);

            _material = ad.LoadAsset <MaterialAsset>("MaterialAsset/Skybox.json").Create(ad, rc);

            var viewProvider = (ConstantBufferDataProvider <Matrix4x4>)((ChangeableProvider)rc.GetNamedGlobalBufferProviderPair("ViewMatrix").DataProvider).DataProvider;

            _perObjectInput = new DependantDataProvider <Matrix4x4>(
                viewProvider,
                Utilities.ConvertToMatrix3x3);

            using (var frontPin = _front.Pixels.Pin())
                using (var backPin = _back.Pixels.Pin())
                    using (var leftPin = _left.Pixels.Pin())
                        using (var rightPin = _right.Pixels.Pin())
                            using (var topPin = _top.Pixels.Pin())
                                using (var bottomPin = _bottom.Pixels.Pin())
                                {
                                    var cubemapTexture = factory.CreateCubemapTexture(
                                        frontPin.Ptr,
                                        backPin.Ptr,
                                        leftPin.Ptr,
                                        rightPin.Ptr,
                                        topPin.Ptr,
                                        bottomPin.Ptr,
                                        _front.Width,
                                        _front.Height,
                                        _front.PixelSizeInBytes,
                                        _front.Format);
                                    _cubemapBinding = factory.CreateShaderTextureBinding(cubemapTexture);
                                }

            _rasterizerState = factory.CreateRasterizerState(FaceCullingMode.None, TriangleFillMode.Solid, false, false);
        }
示例#23
0
        /// <summary>
        /// Recreates the device texture used to render text.
        /// </summary>
        public unsafe void RecreateFontDeviceTexture(RenderContext rc)
        {
            var io = ImGui.GetIO();
            // Build
            var textureData = io.FontAtlas.GetTexDataAsRGBA32();

            // Store our identifier
            io.FontAtlas.SetTexID(_fontAtlasID);

            var deviceTexture = rc.ResourceFactory.CreateTexture(1, textureData.Width, textureData.Height, PixelFormat.R8_G8_B8_A8_UInt);

            deviceTexture.SetTextureData(
                0,
                0, 0,
                textureData.Width,
                textureData.Height,
                (IntPtr)textureData.Pixels,
                textureData.BytesPerPixel * textureData.Width * textureData.Height);
            _fontTextureBinding = rc.ResourceFactory.CreateShaderTextureBinding(deviceTexture);

            io.FontAtlas.ClearTexData();
        }
示例#24
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);
            }
        }
示例#25
0
        public unsafe void RecreateFontDeviceTexture(RenderContext rc)
        {
            var io = ImGui.GetIO();

            // Build
            _textureData = io.FontAtlas.GetTexDataAsRGBA32();
            int[] pixels = new int[_textureData.Width * _textureData.Height];
            for (int i = 0; i < pixels.Length; i++)
            {
                pixels[i] = ((int *)_textureData.Pixels)[i];
            }

            _fontTexture = new RawTextureDataArray <int>(pixels, _textureData.Width, _textureData.Height, _textureData.BytesPerPixel, PixelFormat.R8_G8_B8_A8);

            // Store our identifier
            io.FontAtlas.SetTexID(_fontAtlasID);

            var deviceTexture = rc.ResourceFactory.CreateTexture(_fontTexture.PixelData, _textureData.Width, _textureData.Height, _textureData.BytesPerPixel, PixelFormat.R8_G8_B8_A8);

            _fontTextureBinding = rc.ResourceFactory.CreateShaderTextureBinding(deviceTexture);

            io.FontAtlas.ClearTexData();
        }
示例#26
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));
        }
示例#27
0
文件: Skybox.cs 项目: zhuowp/ge
        private void RecreateCubemapTexture()
        {
            AssetDatabase   ad      = _as.Database;
            ResourceFactory factory = _gs.Context.ResourceFactory;

            _cubemapBinding?.BoundTexture.Dispose();
            _cubemapBinding?.Dispose();

            var front  = !_front.HasValue ? _front.Get(ad) : ad.LoadAsset <ImageSharpTexture>(EngineEmbeddedAssets.SkyboxFrontID);
            var back   = !_back.HasValue ? _back.Get(ad) : ad.LoadAsset <ImageSharpTexture>(EngineEmbeddedAssets.SkyboxBackID);
            var left   = !_left.HasValue ? _left.Get(ad) : ad.LoadAsset <ImageSharpTexture>(EngineEmbeddedAssets.SkyboxLeftID);
            var right  = !_right.HasValue ? _right.Get(ad) : ad.LoadAsset <ImageSharpTexture>(EngineEmbeddedAssets.SkyboxRightID);
            var top    = !_top.HasValue ? _top.Get(ad) : ad.LoadAsset <ImageSharpTexture>(EngineEmbeddedAssets.SkyboxTopID);
            var bottom = !_bottom.HasValue ? _bottom.Get(ad) : ad.LoadAsset <ImageSharpTexture>(EngineEmbeddedAssets.SkyboxBottomID);

            using (var frontPin = front.Pixels.Pin())
                using (var backPin = back.Pixels.Pin())
                    using (var leftPin = left.Pixels.Pin())
                        using (var rightPin = right.Pixels.Pin())
                            using (var topPin = top.Pixels.Pin())
                                using (var bottomPin = bottom.Pixels.Pin())
                                {
                                    var cubemapTexture = factory.CreateCubemapTexture(
                                        frontPin.Ptr,
                                        backPin.Ptr,
                                        leftPin.Ptr,
                                        rightPin.Ptr,
                                        topPin.Ptr,
                                        bottomPin.Ptr,
                                        front.Width,
                                        front.Height,
                                        front.PixelSizeInBytes,
                                        front.Format);
                                    _cubemapBinding = factory.CreateShaderTextureBinding(cubemapTexture);
                                }
        }
示例#28
0
        private unsafe void RenderImDrawData(DrawData *draw_data, RenderContext rc)
        {
            VertexDescriptor descriptor = new VertexDescriptor((byte)sizeof(DrawVert), 3, IntPtr.Zero);

            int vertexOffsetInVertices = 0;
            int indexOffsetInElements  = 0;

            if (draw_data->CmdListsCount == 0)
            {
                return;
            }

            for (int i = 0; i < draw_data->CmdListsCount; i++)
            {
                NativeDrawList *cmd_list = draw_data->CmdLists[i];

                _vertexBuffer.SetVertexData(new IntPtr(cmd_list->VtxBuffer.Data), descriptor, cmd_list->VtxBuffer.Size, vertexOffsetInVertices);
                _indexBuffer.SetIndices(new IntPtr(cmd_list->IdxBuffer.Data), IndexFormat.UInt16, cmd_list->IdxBuffer.Size, indexOffsetInElements);

                vertexOffsetInVertices += cmd_list->VtxBuffer.Size;
                indexOffsetInElements  += cmd_list->IdxBuffer.Size;
            }

            // Setup orthographic projection matrix into our constant buffer
            {
                var io = ImGui.GetIO();

                Matrix4x4 mvp = Matrix4x4.CreateOrthographicOffCenter(
                    0f,
                    io.DisplaySize.X,
                    io.DisplaySize.Y,
                    0.0f,
                    -1.0f,
                    1.0f);

                _projMatrixBuffer.SetData(ref mvp, sizeof(Matrix4x4));
            }

            BlendState previousBlendState = rc.BlendState;

            rc.SetBlendState(_blendState);
            rc.SetDepthStencilState(_depthDisabledState);
            RasterizerState previousRasterizerState = rc.RasterizerState;

            rc.SetRasterizerState(_rasterizerState);
            rc.VertexBuffer = _vertexBuffer;
            rc.IndexBuffer  = _indexBuffer;

            rc.ShaderSet = _shaderSet;
            rc.ShaderResourceBindingSlots = _resourceBindings;
            rc.SetConstantBuffer(0, _projMatrixBuffer);
            rc.SetSamplerState(2, _rc.PointSampler);

            ImGui.ScaleClipRects(draw_data, ImGui.GetIO().DisplayFramebufferScale);

            // Render command lists
            int vtx_offset = 0;
            int idx_offset = 0;

            for (int n = 0; n < draw_data->CmdListsCount; n++)
            {
                NativeDrawList *cmd_list = draw_data->CmdLists[n];
                for (int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.Size; cmd_i++)
                {
                    DrawCmd *pcmd = &(((DrawCmd *)cmd_list->CmdBuffer.Data)[cmd_i]);
                    if (pcmd->UserCallback != IntPtr.Zero)
                    {
                        throw new NotImplementedException();
                    }
                    else
                    {
                        if (pcmd->TextureId != IntPtr.Zero)
                        {
                            if (pcmd->TextureId == new IntPtr(_fontAtlasID))
                            {
                                _rc.SetTexture(1, _fontTextureBinding);
                            }
                            else
                            {
                                ShaderTextureBinding binding = ImGuiImageHelper.GetShaderTextureBinding(pcmd->TextureId);
                                _rc.SetTexture(1, binding);
                            }
                        }

                        // TODO: This doesn't take into account viewport coordinates.
                        rc.SetScissorRectangle(
                            (int)pcmd->ClipRect.X,
                            (int)pcmd->ClipRect.Y,
                            (int)pcmd->ClipRect.Z,
                            (int)pcmd->ClipRect.W);

                        rc.DrawIndexedPrimitives((int)pcmd->ElemCount, idx_offset, vtx_offset);
                    }

                    idx_offset += (int)pcmd->ElemCount;
                }
                vtx_offset += cmd_list->VtxBuffer.Size;
            }

            rc.ClearScissorRectangle();
            rc.SetBlendState(previousBlendState);
            rc.SetDepthStencilState(rc.DefaultDepthStencilState);
            rc.SetRasterizerState(previousRasterizerState);
        }
示例#29
0
        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;
        }
示例#30
0
            private PreviewModel CreatePreviewModel(VertexPositionNormalTexture[] vertices, int[] indices, ShaderTextureBinding textureBinding = null)
            {
                AssetDatabase lfd = new LooseFileDatabase(Path.Combine(AppContext.BaseDirectory, "Assets"));
                VertexBuffer  vb  = _rc.ResourceFactory.CreateVertexBuffer(vertices.Length * VertexPositionNormalTexture.SizeInBytes, false);

                vb.SetVertexData(
                    vertices,
                    new VertexDescriptor(VertexPositionNormalTexture.SizeInBytes, VertexPositionNormalTexture.ElementCount, 0, IntPtr.Zero));

                IndexBuffer ib = _rc.ResourceFactory.CreateIndexBuffer(indices.Length * sizeof(int), false);

                ib.SetIndices(indices, sizeof(int), 0);

                MaterialAsset shadowmapAsset    = lfd.LoadAsset <MaterialAsset>("MaterialAsset/ShadowCaster_ShadowMap.json");
                MaterialAsset surfaceMaterial   = lfd.LoadAsset <MaterialAsset>("MaterialAsset/ModelPreview.json");
                Material      shadowmapMaterial = shadowmapAsset.Create(lfd, _rc, _sceneProviders);
                Material      regularMaterial   = surfaceMaterial.Create(lfd, _rc, _sceneProviders);

                return(new PreviewModel(
                           vb,
                           ib,
                           indices.Length,
                           regularMaterial,
                           shadowmapMaterial,
                           new DynamicDataProvider <Matrix4x4>(Matrix4x4.Identity),
                           textureBinding));
            }