internal static void Draw()
        {
            // flush inner sprites batcher
            if (m_currentInnerBatch.texture != null)
            {
                m_currentInnerBatch.Commit();
            }
            m_currentInnerBatch = new MySpritesBatch();

            if (m_inputLayout == null)
            {
                var spritesInput = MyVertexInput.Empty()
                                   .Append(MyVertexInputComponentType.CUSTOM_HALF4_0, 0, MyVertexInputComponentFreq.PER_INSTANCE)
                                   .Append(MyVertexInputComponentType.CUSTOM_HALF4_1, 0, MyVertexInputComponentFreq.PER_INSTANCE)
                                   .Append(MyVertexInputComponentType.COLOR4, 0, MyVertexInputComponentFreq.PER_INSTANCE);

                m_inputLayout = MyVertexInput.CreateLayout(spritesInput.Hash, MyShaderCache.CompileFromFile("sprite.hlsl", "vs", MyShaderProfile.VS_5_0).Bytecode);
            }

            //

            var context = MyRender.Context;

            context.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleStrip;

            context.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(m_spritesInstanceBuffer.Buffer, MyVertexFormatSpritePositionTextureColor.STRIDE, 0));
            context.InputAssembler.InputLayout = m_inputLayout;

            context.Rasterizer.SetViewport(0, 0, MyRender.ViewportResolution.X, MyRender.ViewportResolution.Y);

            context.VertexShader.Set(m_vertexShader.VertexShader);

            context.PixelShader.Set(m_pixelShader.PixelShader);
            context.PixelShader.SetSamplers(0, MyRender.StandardSamplers);

            context.OutputMerger.SetTargets(MyRender.Backbuffer.RenderTarget);
            context.OutputMerger.SetBlendState(MyRender.BlendStateGui);

            DataStream stream;

            context.MapSubresource(m_spritesInstanceBuffer.Buffer, MapMode.WriteDiscard, MapFlags.None, out stream);
            for (int i = 0; i < m_spriteInstanceList.Count; i++)
            {
                stream.Write(m_spriteInstanceList[i]);
            }
            context.UnmapSubresource(m_spritesInstanceBuffer.Buffer, 0);
            stream.Dispose();

            for (int b = 0; b < m_spriteBatches.Count; b++)
            {
                context.PixelShader.SetShaderResource(0, m_spriteBatches[b].texture);
                context.DrawInstanced(4, m_spriteBatches[b].instanceCount, 0, m_spriteBatches[b].startInstance);
            }

            m_spriteInstanceList.Clear();
            m_spriteBatches.Clear();
        }
        internal static void Draw()
        {
            if (m_inputLayout == null)
            {
                var input = MyVertexInput.Empty()
                            .Append(MyVertexInputComponentType.POSITION4H)
                            .Append(MyVertexInputComponentType.COLOR4);

                m_inputLayout = MyVertexInput.CreateLayout(input.Hash, MyShaderCache.CompileFromFile("primitive.hlsl", "vs", MyShaderProfile.VS_5_0).Bytecode);
            }

            var context = MyRender.Context;

            context.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList;
            context.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(m_vertexBuffer.Buffer, MyVertexFormatPositionColor.STRIDE, 0));
            context.InputAssembler.InputLayout = m_inputLayout;

            context.Rasterizer.State = MyRender.m_nocullRasterizerState;

            context.Rasterizer.SetViewport(0, 0, MyRender.ViewportResolution.X, MyRender.ViewportResolution.Y);

            context.VertexShader.Set(m_vertexShader.VertexShader);
            context.VertexShader.SetConstantBuffer(MyCommon.ProjectionSlot, MyCommon.ProjectionConstants.Buffer);

            context.PixelShader.Set(m_pixelShader.PixelShader);

            context.OutputMerger.BlendState = MyRender.m_transparentBlendState;
            context.OutputMerger.ResetTargets();
            context.OutputMerger.SetTargets(MyRender.MainGbuffer.DepthBuffer.DepthStencil, MyRender.Backbuffer.RenderTarget);

            //SortTransparent();

            DataStream stream;

            context.MapSubresource(m_vertexBuffer.Buffer, MapMode.WriteDiscard, MapFlags.None, out stream);
            for (int i = 0; i < m_vertexList.Count; i++)
            {
                stream.Write(m_vertexList[i]);
            }
            context.UnmapSubresource(m_vertexBuffer.Buffer, 0);
            stream.Dispose();

            context.Draw(m_vertexList.Count, 0);

            m_vertexList.Clear();
            m_postSortVertexList.Clear();
            m_triangleSortDistance.Clear();
            m_sortedIndices.Clear();
        }
Esempio n. 3
0
        internal static byte[] Compile(string filepath, ShaderMacro[] macros, MyShaderProfile profile, string sourceDescriptor, bool optimize, bool invalidateCache, out bool wasCached, out string compileLog)
        {
            ProfilerShort.Begin("MyShaders.Compile");

            var globalMacros = GlobalShaderMacros;

            if (globalMacros.Length != 0)
            {
                var macroList = new List <ShaderMacro>();
                macroList.AddRange(globalMacros);
                macroList.AddRange(macros);
                macros = macroList.ToArray();
            }

            string function    = ProfileEntryPoint(profile);
            string profileName = ProfileToString(profile);

            wasCached  = false;
            compileLog = null;

            ProfilerShort.Begin("MyShaders.Preprocess");
            string errors;
            string preprocessedSource = PreprocessShader(filepath, macros, out errors);

            if (preprocessedSource == null)
            {
                compileLog = errors;
                return(null);
            }

            // modify preprocessor to be readable for NSight
            if (MyCompilationSymbols.EnableShaderDebuggingInNSight)
            {
                preprocessedSource = Regex.Replace(preprocessedSource, "#line [^\n]*\n", "");
            }

            MyShaderIdentity identity = null;

            if (!invalidateCache && !MyCompilationSymbols.EnableShaderDebuggingInNSight)
            {
                identity = MyShaderCache.ComputeShaderIdentity(preprocessedSource, profile);
                byte[] cached;
                if (MyShaderCache.TryFetch(identity, out cached))
                {
                    wasCached = true;
                    ProfilerShort.End();
                    ProfilerShort.End();
                    return(cached);
                }
            }
            ProfilerShort.End();

            try
            {
                string            descriptor = sourceDescriptor + " " + profile + " " + macros.GetString();
                CompilationResult compilationResult;
                if (MyCompilationSymbols.EnableShaderDebuggingInNSight)
                {
                    if (MyCompilationSymbols.EnableShaderPreprocessorInNSight)
                    {
                        compilationResult = ShaderBytecode.Compile(preprocessedSource, function, profileName, 0, 0, macros, new MyIncludeProcessor(filepath));
                    }
                    else
                    {
                        compilationResult = ShaderBytecode.CompileFromFile(filepath, function, profileName, 0, 0, macros, new MyIncludeProcessor(filepath));
                    }
                }
                else
                {
                    compilationResult = ShaderBytecode.Compile(preprocessedSource, function, profileName,
                                                               optimize ? ShaderFlags.OptimizationLevel3 : ShaderFlags.None,
                                                               EffectFlags.None, filepath);
                }

                if (DUMP_CODE)
                {
                    var disassembly = compilationResult.Bytecode.Disassemble(DisassemblyFlags.EnableColorCode |
                                                                             DisassemblyFlags.EnableInstructionNumbering);
                    string asmPath;
                    if (MyRender11.DebugMode)
                    {
                        asmPath = Path.GetFileName(descriptor + "__DEBUG.html");
                    }
                    else
                    {
                        asmPath = Path.GetFileName(descriptor + "__O3.html");
                    }

                    using (var writer = new StreamWriter(Path.Combine(MyFileSystem.ContentPath, "ShaderOutput", asmPath)))
                    {
                        writer.Write(disassembly);
                    }
                }

                if (compilationResult.Message != null)
                {
                    compileLog = compilationResult.Message;
                }

                if (!MyCompilationSymbols.EnableShaderDebuggingInNSight && compilationResult.Bytecode != null &&
                    compilationResult.Bytecode.Data.Length > 0)
                {
                    MyShaderCache.Store(identity, compilationResult.Bytecode.Data);
                }

                return(compilationResult.Bytecode != null ? compilationResult.Bytecode.Data : null);
            }
            catch (Exception e)
            {
                Debug.WriteLine(preprocessedSource);
                compileLog = e.Message;
            }
            finally
            {
                ProfilerShort.End();
            }
            return(null);
        }
Esempio n. 4
0
        internal static byte[] Compile(string source, ShaderMacro[] macros, MyShadersDefines.Profiles profile, string sourceDescriptor, bool optimize, bool invalidateCache, out bool wasCached, out string compileLog)
        {
            ProfilerShort.Begin("MyShaders.Compile");
            string function    = MyShadersDefines.ProfileEntryPoint(profile);
            string profileName = MyShadersDefines.ProfileToString(profile);

            wasCached  = false;
            compileLog = null;

            ProfilerShort.Begin("MyShaders.Preprocess");
            string preprocessedSource = PreprocessShader(source, macros);

            var key = MyShaderCache.CalculateKey(preprocessedSource, function, profileName);

            if (!invalidateCache)
            {
                var cached = MyShaderCache.TryFetch(key);
                if (cached != null)
                {
                    wasCached = true;
                    ProfilerShort.End();
                    ProfilerShort.End();
                    return(cached);
                }
            }
            ProfilerShort.End();

            try
            {
                string            descriptor        = sourceDescriptor + " " + profile + " " + macros.GetString();
                CompilationResult compilationResult = ShaderBytecode.Compile(preprocessedSource, function, profileName, optimize ? ShaderFlags.OptimizationLevel3 : 0, 0, null, null, descriptor);

                if (DUMP_CODE)
                {
                    var disassembly = compilationResult.Bytecode.Disassemble(DisassemblyFlags.EnableColorCode |
                                                                             DisassemblyFlags.EnableInstructionNumbering);
                    string asmPath;
                    if (MyRender11.DebugMode)
                    {
                        asmPath = Path.GetFileName(descriptor + "__DEBUG.html");
                    }
                    else
                    {
                        asmPath = Path.GetFileName(descriptor + "__O3.html");
                    }

                    using (var writer = new StreamWriter(Path.Combine(MyFileSystem.ContentPath, "ShaderOutput", asmPath)))
                    {
                        writer.Write(disassembly);
                    }
                }

                if (compilationResult.Message != null)
                {
                    compileLog = ExtendedErrorMessage(source, compilationResult.Message) + DumpShaderSource(key, preprocessedSource);
                }

                if (compilationResult.Bytecode.Data.Length > 0)
                {
                    MyShaderCache.Store(key.ToString(), compilationResult.Bytecode.Data);
                }

                return(compilationResult.Bytecode.Data);
            }
            catch (CompilationException e)
            {
                Debug.WriteLine(preprocessedSource);
                compileLog = ExtendedErrorMessage(source, e.Message) + DumpShaderSource(key, preprocessedSource);
            }
            finally
            {
                ProfilerShort.End();
            }
            return(null);
        }
Esempio n. 5
0
        internal static unsafe void Draw()
        {
            if (m_inputLayout == null)
            {
                var linesInput = MyVertexInput.Empty()
                                 .Append(MyVertexInputComponentType.POSITION_PACKED)
                                 .Append(MyVertexInputComponentType.COLOR4);

                m_inputLayout = MyVertexInput.CreateLayout(linesInput.Hash, MyShaderCache.CompileFromFile("line.hlsl", "vs", MyShaderProfile.VS_5_0).Bytecode);
            }

            var context = MyRender.Context;

            context.InputAssembler.PrimitiveTopology = PrimitiveTopology.LineList;
            context.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(m_linesVertexBuffer.Buffer, MyVertexFormatPositionColor.STRIDE, 0));
            context.InputAssembler.InputLayout = m_inputLayout;

            context.Rasterizer.SetViewport(0, 0, MyRender.ViewportResolution.X, MyRender.ViewportResolution.Y);
            context.Rasterizer.State = MyRender.m_linesRasterizerState;

            context.VertexShader.Set(m_vertexShader.VertexShader);
            context.VertexShader.SetConstantBuffer(0, MyResources.ProjectionConstants.Buffer);

            context.PixelShader.Set(m_pixelShader.PixelShader);

            context.OutputMerger.ResetTargets();
            context.OutputMerger.SetTargets(MyRender.Backbuffer.RenderTarget);

            DataStream stream;

            context.MapSubresource(m_linesVertexBuffer.Buffer, MapMode.WriteNoOverwrite, MapFlags.None, out stream);
            for (int i = 0; i < m_lineVertexList.Count; i++)
            {
                stream.Write(m_lineVertexList[i]);
            }
            context.UnmapSubresource(m_linesVertexBuffer.Buffer, 0);
            stream.Dispose();

            Matrix viewProjection;

            for (int b = 0; b < m_lineBatches.Count; b++)
            {
                if (m_lineBatches[b].m_customViewProjection.HasValue)
                {
                    viewProjection = m_lineBatches[b].m_customViewProjection.Value;
                }
                else
                {
                    viewProjection = MyEnvironment.CameraView * MyEnvironment.Projection;
                }


                stream = MyMapping.MapDiscard(MyResources.ProjectionConstants.Buffer);
                stream.Write(Matrix.Transpose(viewProjection));
                MyMapping.Unmap(MyResources.ProjectionConstants.Buffer, stream);


                context.Draw(m_lineBatches[b].m_vertexCount, m_lineBatches[b].m_startVertex);
            }

            // cleanup
            m_lineVertexList.Clear();
            m_lineBatches.Clear();
        }