示例#1
0
        internal protected AbstractConstantBuffer(LightDevice device, IntPtr buffer)
        {
            _device = device;
            device.AddComponent(this);

            _buffer = buffer;
        }
示例#2
0
 private RenderTargetObject(LightDevice device)
 {
     _device = device;
     device.AddComponent(this);
     device.ReleaseRenderTargets += ReleaseView;
     device.RebuildRenderTargets += RebuildView;
 }
示例#3
0
        internal AbstractShaderResourceBuffer(LightDevice device, IntPtr pBuffer, IntPtr pView)
        {
            _device = device;
            device.AddComponent(this);

            _pBuffer = pBuffer;
            _pView   = pView;
        }
示例#4
0
 public VertexDataProcessorGroup(LightDevice device, Type[] vertexTypes, object[] processors, IntPtr layout)
 {
     _device     = device;
     VertexTypes = vertexTypes;
     Processors  = processors;
     _layout     = layout;
     _offsets    = new uint[vertexTypes.Length];
 }
示例#5
0
        //TODO get pipeline object

        internal VertexDataProcessor(LightDevice device, IntPtr layout)
        {
            _device = device;
            device.AddComponent(this);

            _inputLayout  = layout;
            _bufferUpdate = new BufferUpdate(device);
        }
示例#6
0
        internal static RenderTargetObject CreateSwapchainTarget(LightDevice device)
        {
            var ret = new RenderTargetObject(device);

            ret._type           = RenderTargetObjectType.SwapchainTarget;
            ret._isDepthStencil = false;
            ret.RebuildView();
            return(ret);
        }
示例#7
0
        internal IndexBuffer(LightDevice device, IntPtr ptr, int bitWidth, int size)
        {
            _device = device;
            device.AddComponent(this);

            _ptr      = ptr;
            _bitWidth = bitWidth;
            _size     = size;
        }
示例#8
0
        internal static RenderTargetObject CreateTextureTarget(LightDevice device, int format)
        {
            var ret = new RenderTargetObject(device);

            ret._type           = RenderTargetObjectType.TextureTarget;
            ret._isDepthStencil = false;
            ret._formatTexture  = format;
            ret._formatTarget   = format;
            ret._formatResource = format;
            ret._textureObj     = new Texture2D(device, IntPtr.Zero, IntPtr.Zero, 0, 0, false);
            ret.RebuildView();
            return(ret);
        }
示例#9
0
        internal Texture2D(LightDevice device, IntPtr tex, IntPtr view, int w, int h, bool holdPtr = true)
        {
            _device = device;
            device.AddComponent(this);

            _holdPointer = holdPtr;

            _texture = tex;
            _view    = view;

            Width  = w;
            Height = h;
        }
示例#10
0
        internal VertexBuffer(LightDevice device, IBufferUpdate update, IntPtr buffer, IntPtr layout,
                              int stride, int vertexCount, bool isDynamic)
        {
            _device = device;
            device.AddComponent(this);

            _update      = update;
            _buffer      = buffer;
            _layout      = layout;
            _stride      = (uint)stride;
            _vertexCount = vertexCount;
            _isDynamic   = isDynamic;
        }
示例#11
0
        internal static RenderTargetObject CreateDepthStencilTarget(LightDevice device,
                                                                    int formatTexture, int formatTarget, int formatResource)
        {
            var ret = new RenderTargetObject(device);

            ret._type           = RenderTargetObjectType.TextureTarget;
            ret._isDepthStencil = true;
            ret._formatTexture  = formatTexture;
            ret._formatTarget   = formatTarget;
            ret._formatResource = formatResource;
            ret.RebuildView();
            return(ret);
        }
示例#12
0
文件: Pipeline.cs 项目: acaly/LightDx
        internal Pipeline(LightDevice device, IntPtr v, IntPtr g, IntPtr p, IntPtr sign, InputTopology topology)
        {
            _device = device;
            device.AddComponent(this);
            _device.ResolutionChanged += DeviceBufferResized;

            _vertex        = v;
            _geometry      = g;
            _pixel         = p;
            _signatureBlob = sign;

            _viewport = new Viewport
            {
                Width    = _device.ScreenWidth,
                Height   = _device.ScreenHeight,
                MaxDepth = 1.0f,
            };
            _topology = topology;
        }
示例#13
0
        internal unsafe IntPtr CreateBlenderForDevice(LightDevice device)
        {
            if (!_alpha)
            {
                return(IntPtr.Zero);
            }
            BlendDescription d = new BlendDescription();

            d.RenderTarget0.BlendEnable           = 1;  //true
            d.RenderTarget0.SrcBlend              = 5;  //D3D11_BLEND_SRC_ALPHA
            d.RenderTarget0.DestBlend             = 6;  //D3D11_BLEND_INV_SRC_ALPHA
            d.RenderTarget0.BlendOp               = 1;  //D3D11_BLEND_OP_ADD
            d.RenderTarget0.SrcBlendAlpha         = 2;  //D3D11_BLEND_ONE
            d.RenderTarget0.DestBlendAlpha        = 1;  //D3D11_BLEND_ZERO
            d.RenderTarget0.BlendOpAlpha          = 1;  //D3D11_BLEND_OP_ADD
            d.RenderTarget0.RenderTargetWriteMask = 15; //D3D11_COLOR_WRITE_ENABLE_ALL
            Device.CreateBlendState(device.DevicePtr, new IntPtr(&d), out var ret).Check();
            return(ret);
        }
示例#14
0
        public AbstractFontCache(LightDevice device, Font font, PixelFormat format, Color foreground, Color background)
        {
            _device = device;
            device.AddComponent(this);

            Font            = font;
            PixelFormat     = format;
            BackgroundColor = background;
            ForegroundColor = foreground;
            _brush          = new SolidBrush(foreground);
            var height = (int)Math.Ceiling(font.GetHeight());
            int bitmapSize;

            if (height < 85)
            {
                bitmapSize = 256;
            }
            else
            {
                bitmapSize = Round2Power(height * 3);
            }
            BufferWidth        = bitmapSize;
            BufferHeight       = bitmapSize;
            _gdiBitmap         = new Bitmap(BufferWidth, BufferHeight, format);
            _gdiBitmapGraphics = Graphics.FromImage(_gdiBitmap);
            _gdiBitmapGraphics.TextRenderingHint = TextRenderingHint.AntiAlias;

            _formatSingle = new StringFormat();
            _formatSingle.SetMeasurableCharacterRanges(new[] { new CharacterRange(0, 1) });
            _formatDouble = new StringFormat();
            _formatDouble.SetMeasurableCharacterRanges(new[] { new CharacterRange(0, 2) });
            _empty = new CachedRegion
            {
                Bitmap = null,
                X      = 0,
                Y      = 0,
                Width  = 0,
                Height = 0,
            };
        }
示例#15
0
        public RenderTargetList(params RenderTargetObject[] renderTargetObjects)
        {
            if (renderTargetObjects.Length < 1)
            {
                throw new ArgumentException(nameof(renderTargetObjects));
            }
            LightDevice device = renderTargetObjects[0].Device;
            int         renderTarget = 0, depthStencil = 0;

            if (renderTargetObjects[0].IsDepthStencil)
            {
                depthStencil += 1;
            }
            else
            {
                renderTarget += 1;
            }
            for (int i = 1; i < renderTargetObjects.Length; ++i)
            {
                if (renderTargetObjects[i].Device != device)
                {
                    throw new ArgumentException("target not from same device");
                }
                if (renderTargetObjects[i].IsDepthStencil)
                {
                    depthStencil += 1;
                }
                else
                {
                    renderTarget += 1;
                }
            }
            if (renderTarget == 0 || depthStencil > 1)
            {
                throw new ArgumentException("invalid target type");
            }
            _renderTargets = renderTargetObjects.Where(t => !t.IsDepthStencil).ToArray();
            _depthStencil  = renderTargetObjects.FirstOrDefault(t => t.IsDepthStencil);
            _viewPtr       = new IntPtr[_renderTargets.Length];
        }
示例#16
0
        public Sprite(LightDevice device)
        {
            _device = device;
            device.AddComponent(this);

            _pipeline = device.CompilePipeline(InputTopology.Triangle,
                                               ShaderSource.FromString(PipelineCode, ShaderType.Vertex | ShaderType.Pixel));
            _pipeline.SetBlender(Blender.AlphaBlender);

            _vertexProcessor = _pipeline.CreateVertexDataProcessor <Vertex>();
            _array           = new[] {
                new Vertex {
                    TexCoord = new Vector4(0, 0, 0, 0), Position = new Vector4(0, 0, 0, 0)
                },
                new Vertex {
                    TexCoord = new Vector4(1, 0, 0, 0), Position = new Vector4(1, 0, 0, 0)
                },
                new Vertex {
                    TexCoord = new Vector4(0, 1, 0, 0), Position = new Vector4(0, 1, 0, 0)
                },

                new Vertex {
                    TexCoord = new Vector4(0, 1, 0, 0), Position = new Vector4(0, 1, 0, 0)
                },
                new Vertex {
                    TexCoord = new Vector4(1, 0, 0, 0), Position = new Vector4(1, 0, 0, 0)
                },
                new Vertex {
                    TexCoord = new Vector4(1, 1, 0, 0), Position = new Vector4(1, 1, 0, 0)
                },
            };
            _buffer = _vertexProcessor.CreateDynamicBuffer(6);

            _constant = _pipeline.CreateConstantBuffer <VSConstant>();
            _pipeline.SetConstant(ShaderType.Vertex, 0, _constant);
        }
示例#17
0
        public static Texture2D Load(LightDevice device, Stream stream)
        {
            var header1 = Read <DdsHeader>(stream);

            Check(header1.Magic == MagicDDS);
            Check(header1.Size == 124);
            Check((header1.Flags & 7) == 7); //caps, height, width
            Check(header1.PixelFormat.Size == 32);
            uint format = 0;
            uint pitch  = 0;

            if ((header1.PixelFormat.Flags & 0x40) == 0x40)  //DDPF_RGB
            {
                Check((header1.PixelFormat.Flags & 1) == 1); //Contains alpha
                Check(header1.PixelFormat.ABitMask == 0xFF000000);
                Check(header1.PixelFormat.RBitMask == 0x00FF0000);
                Check(header1.PixelFormat.GBitMask == 0x0000FF00);
                Check(header1.PixelFormat.BBitMask == 0x000000FF);
                format = 87; //DXGI_FORMAT_B8G8R8A8_UNORM

                //There must be at least one way to calculate pitch (providing RGBBitCount or PitchOrLinearSize)
                if ((header1.PixelFormat.Flags & 0x40) == 0x40) //DDPF_RGB
                {
                    pitch = (header1.Width * header1.PixelFormat.RGBBitCount + 7) / 8;
                }
                else
                {
                    Check((header1.Flags & 8) == 8); //pitch
                }
            }
            else
            {
                Check((header1.PixelFormat.Flags & 0x4) == 0x4); //DDPF_FOURCC
                switch (header1.PixelFormat.FourCC)
                {
                case MagicDXT1:
                    format = 71;     //DXGI_FORMAT_BC1_UNORM
                    pitch  = (header1.Width + 3) / 4 * 8;
                    break;

                case MagicDXT3:
                    format = 74;     //DXGI_FORMAT_BC2_UNORM
                    pitch  = (header1.Width + 3) / 4 * 16;
                    break;

                case MagicDXT5:
                    format = 77;     //DXGI_FORMAT_BC3_UNORM
                    pitch  = (header1.Width + 3) / 4 * 16;
                    break;

                case MagicDX10:
                {
                    var header2 = Read <HeaderDx10>(stream);
                    Check(header2.ResourceDimension == 3 /*DDS_DIMENSION_TEXTURE2D*/);
                    Check(header2.ArraySize == 1);
                    format = header2.DXGIFormat;
                }
                break;

                default:
                    Check(false);
                    break;
                }
            }
            if ((header1.Flags & 8) == 8)
            {
                pitch = header1.PitchOrLinearSize;
            }
            var data = new byte[stream.Length - stream.Position];

            Check(stream.Read(data, 0, data.Length) == data.Length);
            fixed(byte *pData = data)
            {
                return(device.CreateTexture2D((int)header1.Width, (int)header1.Height, (int)format,
                                              new IntPtr(pData), (int)pitch, false));
            }
        }
示例#18
0
 public TextureFontCache(LightDevice device, Font font)
     : base(device, font, PixelFormat.Format32bppArgb, Color.Black, Color.Transparent)
 {
 }
示例#19
0
 //TODO unused?
 internal VertexDataProcessor(LightDevice device)
     : this(device, IntPtr.Zero)
 {
 }
示例#20
0
 internal ConstantBuffer(LightDevice device, IntPtr buffer)
     : base(device, buffer)
 {
 }
示例#21
0
 public BufferUpdate(LightDevice device)
 {
     _device = device;
 }
示例#22
0
 public TextureFontCache(LightDevice device, Font font, Color foreground, Color background)
     : base(device, font, PixelFormat.Format32bppArgb, foreground, background)
 {
 }
示例#23
0
 public TextureFontCache(LightDevice device, Font font)
     : this(device, font, Color.Black, Color.Transparent)
 {
 }
示例#24
0
        public unsafe static LightDevice Create(Control ctrl, int initWidth = -1, int initHeight = -1)
        {
            var ret = new LightDevice();

            //initialize size
            {
                var width  = initWidth == -1 ? ctrl.ClientSize.Width : initWidth;
                var height = initHeight == -1 ? ctrl.ClientSize.Height : initHeight;

                ret._ctrl   = ctrl;
                ret._form   = ctrl.FindForm();
                ret._width  = width;
                ret._height = height;
                ret._dpi    = GetDpiForWindow(ret._form.Handle);
            }

            try
            {
                using (var adapter = new ComScopeGuard())
                {
                    //Find the adapter
                    adapter.Ptr = GetAdapter();

                    //create core objects
                    IntPtr swapChain, device, immediateContext;
                    {
                        var d = new SwapChainDescription(ctrl.Handle, ret._width, ret._height);

                        Native.D3D11CreateDeviceAndSwapChain(
                            adapter.Ptr, adapter.Ptr == IntPtr.Zero ? 1u : 0u,
                            IntPtr.Zero, 0, IntPtr.Zero, 0, 7, ref d,
                            out swapChain, out device, out var featureLevel, out immediateContext).Check();

                        ret._device    = device;
                        ret._swapchain = swapChain;
                        ret._context   = immediateContext;
                    }

                    //get default render target
                    IntPtr renderView;
                    {
                        using (var backBuffer = new ComScopeGuard())
                        {
                            SwapChain.GetBuffer(swapChain, 0, Guids.Texture2D, out backBuffer.Ptr).Check();
                            Device.CreateRenderTargetView(device, backBuffer.Ptr, null, out renderView).Check();
                        }
                        ret._defaultRenderView = renderView;
                    }

                    //get DXGI.Output
                    {
                        var i = Adapter.EnumOutputs(adapter.Ptr, 0, out var output);
                        //Sometimes this can fail, but it should not affect our other functions.
                        //TODO Actually we should think of supporting multiple outputs.
                        if (i != 0x887A0002)
                        {
                            i.Check();
                        }
                        ret._output = output;
                    }

                    ret._defaultRenderTarget = RenderTargetObject.CreateSwapchainTarget(ret);
                    ret.AddEventHandlers();
                }
            }
            catch (NativeException e)
            {
                ret.Dispose(true);
                throw e;
            }
            return(ret);
        }
示例#25
0
 public static Matrix4x4 CreatePerspectiveFieldOfView(this LightDevice device, float fov, float nearPlane = 0.1f, float farPlane = 1000f)
 {
     return(CreatePerspectiveFieldOfView(fov, device.ScreenWidth / (float)device.ScreenHeight, nearPlane, farPlane));
 }
示例#26
0
 internal ShaderResourceBuffer(LightDevice device, IntPtr pBuffer, IntPtr pView)
     : base(device, pBuffer, pView)
 {
 }