Example #1
0
        private static void LoadShaders()
        {
            using (var cache = File.OpenRead(RenderSettings.ShaderCache))
                using (var reader = new BinaryReader(cache))
                {
                    var magic = reader.ReadChars(_cacheMagic.Length);

                    if (VisualC.CompareMemory(magic, _cacheMagic, magic.Length) != 0)
                    {
                        throw new InvalidDataException("The shader cache did not match the magic bytes.");
                    }

                    do
                    {
                        var compressedLength   = reader.ReadInt32();
                        var decompressedLength = reader.ReadInt32();
                        var inputBuffer        = reader.ReadBytes(compressedLength);
                        var outputBuffer       = LZ4Codec.Decode(inputBuffer, 0, compressedLength, decompressedLength);

                        using (var shaderStream = new MemoryStream(outputBuffer))
                            using (var shaderReader = new BinaryReader(shaderStream))
                            {
                                var name      = shaderReader.ReadString();
                                var passCount = shaderReader.ReadInt32();
                                var passes    = new List <GfxShader.Pass>(passCount);
                                for (int i = 0; i < passCount; i++)
                                {
                                    var pass = new GfxShader.Pass();
                                    pass.Load(shaderReader);
                                    passes[i] = pass;
                                }
                                var source = shaderReader.ReadString();
                                var shader = new GfxShader(name, passes, source);
                                shader.Passes.ForEach(p => p.Load());
                                _shaders[name] = shader;
                            }
                    } while (cache.Position < cache.Length);
                }
        }
Example #2
0
        internal static void Init()
        {
            Logger.Info("GraphicsManager initializing.");

            // Direct3D11 Init ---
            ResizeNextFrame = true;

            Factory        = new FactoryDXGI();
            ImagingFactory = new FactoryWIC();
            Factory2D      = new FactoryD2D(FactoryType.MultiThreaded,
                                            Engine.IsDebug ? DebugLevel.Error : DebugLevel.None);
            DirectWriteFactory = new FactoryDW(SharpDX.DirectWrite.FactoryType.Shared);

            CreateDevices(RenderSettings.GraphicsAdapter);

            Brushes =
                new MemoizingMRUCache <Colour, SolidColorBrush>(
                    (colour, _) => new SolidColorBrush(Context2D, (Color4)colour)
            {
                Opacity = colour.A
            }, int.MaxValue,
                    brush => brush.Dispose());
            // ------------------------------------

            Engine.HandleSet += hwnd =>
            {
                var camera = Game.Workspace.CurrentCamera;
                camera.RenderHandle = hwnd;
            };

            if (Engine.Handle != IntPtr.Zero)
            {
                var camera = Game.Workspace.CurrentCamera;
                camera.RenderHandle = Engine.Handle;
            }

            SamplerStates.Load();
            Shaders.Init();
            BlendStates.Load();
            DepthStencilStates.Load();
            RasterizerStates.Load();

            //StandardConstants = new ConstantBuffer<StandardConstantData>();

            StandardShader = Shaders.Get("Standard");
            MainPass       = StandardShader.GetPass();
            LightingShader = Shaders.Get("Lighting");
            LightingPass   = LightingShader.GetPass();

            PostProcessShader = Shaders.Get("PostProcess");

            SkyboxShader = Shaders.Get("Skybox");
            SkyboxPass   = SkyboxShader.GetPass();

            AdornShader      = Shaders.Get("Adorn");
            AALinePass       = AdornShader.GetPass("AALine");
            AdornSelfLitPass = AdornShader.GetPass("AdornSelfLit");

            Shadows.Init();

            // ------------------

            IsInitialized = true;
            Initialized?.Invoke();
            Initialized = null;

            //PixHelper.AllowProfiling(Engine.IsDebug);

            Logger.Info("Renderer initialized.");
        }