static void TestCap(this ShaderCaps caps, StringBuilder builder, ShaderCaps totest, string defname)
 {
     if ((caps & totest) == totest)
     {
         builder.Append("#define ").Append(defname).AppendLine(" 1\n");
     }
 }
예제 #2
0
        public static ShaderVariables Get(string vs, string fs, ShaderCaps caps = ShaderCaps.None)
        {
            var             k = new Strings2(vs, fs, caps);
            ShaderVariables sh;

            if (!shaders.TryGetValue(k, out sh))
            {
                string prelude;
                if (GLExtensions.Features430)
                {
                    prelude = "#version 430\n#define FEATURES430\n" + caps.GetDefines() + "\n#line 0\n";
                }
                else
                {
                    prelude = "#version 150\n" + caps.GetDefines() + "\n#line 0\n";
                }
                FLLog.Debug("Shader", "Compiling [ " + vs + " , " + fs + " ]");
                sh = new ShaderVariables(
                    new Shader(
                        prelude + "#define VERTEX_SHADER\n" + ProcessIncludes(Resources.LoadString("LibreLancer.Shaders." + vs)),
                        prelude + "#define FRAGMENT_SHADER\n" + ProcessIncludes(Resources.LoadString("LibreLancer.Shaders." + fs)))
                    );
                shaders.Add(k, sh);
            }
            return(sh);
        }
예제 #3
0
        public override void SetSkinningData(Matrix4[] bones, ref Lighting lights)
        {
            ShaderCaps caps = ShaderCaps.None;

            if (VertexLighting)
            {
                caps |= ShaderCaps.VertexLighting;
            }
            if (HasSpotlight(ref lights))
            {
                caps |= ShaderCaps.Spotlight;
            }
            if (EtEnabled)
            {
                caps |= ShaderCaps.EtEnabled;
            }
            if (Fade)
            {
                caps |= ShaderCaps.FadeEnabled;
            }
            var sh = GetShader(new Utf.Dfm.DfmVertex(), caps);

            sh.SetSkinningEnabled(true);
            var loc = sh.Shader.GetLocation("Bones");

            if (loc != -1)
            {
                for (int i = 0; i < bones.Length; i++)
                {
                    sh.Shader.SetMatrix(loc + i, ref bones[i]);
                }
            }
        }
예제 #4
0
        public static string GetDefines(this ShaderCaps caps)
        {
            var builder = new StringBuilder();

            caps.TestCap(builder, ShaderCaps.AlphaTestEnabled, "ALPHATEST_ENABLED");
            caps.TestCap(builder, ShaderCaps.EtEnabled, "ET_ENABLED");
            caps.TestCap(builder, ShaderCaps.FadeEnabled, "FADE_ENABLED");
            caps.TestCap(builder, ShaderCaps.Spotlight, "SPOTLIGHT");
            return(builder.ToString());
        }
예제 #5
0
        static ShaderVariables GetShader(IVertexType vertextype, ShaderCaps caps)
        {
            var i = caps.GetIndex();

            if (vertextype is Utf.Dfm.DfmVertex)
            {
                if (sh_dfm[i] == null)
                {
                    sh_dfm[i] = ShaderCache.Get(
                        "Basic_Skinned.vs",
                        "Basic_Fragment.frag",
                        caps);
                    sh_dfm[i].SetSkinningEnabled(false);
                }
                return(sh_dfm[i]);
            }
            if (vertextype is VertexPositionNormalTexture ||
                vertextype is VertexPositionNormal)
            {
                if (sh_posNormalTexture[i] == null)
                {
                    sh_posNormalTexture[i] = ShaderCache.Get(
                        "Basic_PositionNormalTexture.vs",
                        "Basic_Fragment.frag",
                        caps
                        );
                }
                return(sh_posNormalTexture[i]);
            }
            if (vertextype is VertexPositionNormalTextureTwo)
            {
                if (sh_posNormalTextureTwo[i] == null)
                {
                    sh_posNormalTextureTwo[i] = ShaderCache.Get(
                        "Basic_PositionNormalTextureTwo.vs",
                        "Basic_Fragment.frag",
                        caps
                        );
                }
                return(sh_posNormalTextureTwo[i]);
            }
            if (vertextype is VertexPositionNormalDiffuseTexture)
            {
                if (sh_posNormalColorTexture[i] == null)
                {
                    sh_posNormalColorTexture[i] = ShaderCache.Get(
                        "Basic_PositionNormalColorTexture.vs",
                        "Basic_Fragment.frag",
                        caps
                        );
                }
                return(sh_posNormalColorTexture[i]);
            }
            if (vertextype is VertexPositionTexture)
            {
                if (sh_posTexture[i] == null)
                {
                    sh_posTexture[i] = ShaderCache.Get(
                        "Basic_PositionTexture.vs",
                        "Basic_Fragment.frag",
                        caps
                        );
                }
                return(sh_posTexture[i]);
            }
            if (vertextype is VertexPosition)
            {
                if (sh_pos[i] == null)
                {
                    sh_pos[i] = ShaderCache.Get(
                        "Basic_PositionTexture.vs",
                        "Basic_Fragment.frag",
                        caps
                        );
                }
                return(sh_pos[i]);
            }
            if (vertextype is VertexPositionColor)
            {
                if (sh_posColor[i] == null)
                {
                    sh_posColor[i] = ShaderCache.Get(
                        "Basic_PositionColor.vs",
                        "Basic_Fragment.frag",
                        caps
                        );
                }
                return(sh_posColor[i]);
            }
            throw new NotImplementedException(vertextype.GetType().Name);
        }
예제 #6
0
        public override void Use(RenderState rstate, IVertexType vertextype, ref Lighting lights)
        {
            if (Camera == null)
            {
                return;
            }
            ShaderCaps caps = ShaderCaps.None;

            if (VertexLighting)
            {
                caps |= ShaderCaps.VertexLighting;
            }
            if (HasSpotlight(ref lights))
            {
                caps |= ShaderCaps.Spotlight;
            }
            if (EtEnabled)
            {
                caps |= ShaderCaps.EtEnabled;
            }
            if (Fade)
            {
                caps |= ShaderCaps.FadeEnabled;
            }
            var dxt1 = GetDxt1();

            if (dxt1)
            {
                caps |= ShaderCaps.AlphaTestEnabled;
                //Shitty way of dealing with alpha_mask
                //FL has a lot of DXT1 textures that aren't part of alpha_mask
                //so this brings overall performance down.
                //Don't change any of this stuff unless you can verify it works
                //in all places! (Check Li01 shipyards, Bw10 tradelanes)
            }
            var shader = GetShader(vertextype, caps);

            lastShader = shader;
            shader.SetWorld(World);
            shader.SetView(Camera);
            shader.SetViewProjection(Camera);
            //Dt
            shader.SetDtSampler(0);
            BindTexture(rstate, 0, DtSampler, 0, DtFlags, ResourceManager.WhiteTextureName);
            //Dc
            shader.SetDc(Dc);
            //Oc
            shader.SetOc(Oc);
            if (AlphaEnabled || Fade || OcEnabled || dxt1)
            {
                rstate.BlendMode = BlendMode.Normal;
            }
            else
            {
                rstate.BlendMode = BlendMode.Opaque; //TODO: Maybe I can just leave this out?
            }
            //Fade
            if (Fade)
            {
                shader.SetFadeRange(new Vector2(FadeNear, FadeFar));
            }
            //MaterialAnim
            if (MaterialAnim != null)
            {
                shader.SetMaterialAnim(new Vector4(
                                           MaterialAnim.UOffset,
                                           MaterialAnim.VOffset,
                                           MaterialAnim.UScale,
                                           MaterialAnim.VScale
                                           ));
            }
            else
            {
                shader.SetMaterialAnim(new Vector4(0, 0, 1, 1));
            }
            shader.SetFlipNormal(FlipNormals);
            if (Bones != null && vertextype is DfmVertex)
            {
                shader.Shader.UniformBlockBinding("Bones", 1);
                shader.SetSkinningEnabled(true);
                Bones.BindTo(1, BufferOffset, 200);
            }
            else
            {
                shader.SetSkinningEnabled(false);
            }
            //Ec
            shader.SetEc(Ec);
            //EtSampler
            if (EtEnabled)
            {
                shader.SetEtSampler(1);
                BindTexture(rstate, 1, EtSampler, 1, EtFlags, ResourceManager.NullTextureName);
            }
            //Set lights
            SetLights(shader, ref lights);
            shader.UseProgram();
        }
예제 #7
0
        public override void Use(RenderState rstate, IVertexType vertextype, Lighting lights)
        {
            if (Camera == null)
            {
                return;
            }
            ShaderCaps caps = ShaderCaps.None;

            if (HasSpotlight(ref lights))
            {
                caps |= ShaderCaps.Spotlight;
            }
            if (EtEnabled)
            {
                caps |= ShaderCaps.EtEnabled;
            }
            if (Fade)
            {
                caps |= ShaderCaps.FadeEnabled;
            }
            if (GetTexture(0, DtSampler).Dxt1)
            {
                caps |= ShaderCaps.AlphaTestEnabled;                 //Shitty way of dealing with alpha_mask
            }
            var shader = GetShader(vertextype, caps);

            shader.SetWorld(ref World);
            shader.SetView(Camera);
            shader.SetViewProjection(Camera);
            //Dt
            shader.SetDtSampler(0);
            BindTexture(rstate, 0, DtSampler, 0, DtFlags, ResourceManager.WhiteTextureName);
            //Dc
            shader.SetDc(Dc);
            //Oc
            shader.SetOc(Oc);
            if (AlphaEnabled || Fade || OcEnabled)
            {
                rstate.BlendMode = BlendMode.Normal;
            }
            else
            {
                rstate.BlendMode = BlendMode.Opaque;
            }
            //Fade
            if (Fade)
            {
                shader.SetFadeRange(new Vector2(FadeNear, FadeFar));
            }
            //MaterialAnim
            if (MaterialAnim != null)
            {
                shader.SetMaterialAnim(new Vector4(
                                           MaterialAnim.UOffset,
                                           MaterialAnim.VOffset,
                                           MaterialAnim.UScale,
                                           MaterialAnim.VScale
                                           ));
            }
            else
            {
                shader.SetMaterialAnim(new Vector4(0, 0, 1, 1));
            }
            shader.SetFlipNormal(FlipNormals);
            //Ec
            shader.SetEc(Ec);
            //EtSampler
            if (EtEnabled)
            {
                shader.SetEtSampler(1);
                BindTexture(rstate, 1, EtSampler, 1, EtFlags, ResourceManager.NullTextureName);
            }
            //Set lights
            SetLights(shader, lights);
            var normalMatrix = World;

            normalMatrix.Invert();
            normalMatrix.Transpose();
            shader.SetNormalMatrix(ref normalMatrix);
            shader.UseProgram();
        }
        public static int GetIndex(this ShaderCaps caps)
        {
            int b = 0;

            if ((caps & ShaderCaps.VertexLighting) == ShaderCaps.VertexLighting)
            {
                b = 16;
            }
            caps &= ~ShaderCaps.VertexLighting;
            if (caps == ShaderCaps.None)
            {
                return(b + 0);
            }
            if (caps == ShaderCaps.AlphaTestEnabled)
            {
                return(b + 1);
            }
            if (caps == ShaderCaps.FadeEnabled)
            {
                return(b + 2);
            }
            if (caps == ShaderCaps.EtEnabled)
            {
                return(b + 3);
            }
            if (caps == (ShaderCaps.EtEnabled | ShaderCaps.AlphaTestEnabled))
            {
                return(b + 4);
            }
            if (caps == (ShaderCaps.FadeEnabled | ShaderCaps.AlphaTestEnabled))
            {
                return(b + 5);
            }
            if (caps == (ShaderCaps.EtEnabled | ShaderCaps.FadeEnabled))
            {
                return(b + 6);
            }
            if (caps == (ShaderCaps.EtEnabled | ShaderCaps.FadeEnabled | ShaderCaps.AlphaTestEnabled))
            {
                return(b + 7);
            }
            if (caps == (ShaderCaps.Spotlight))
            {
                return(b + 8);
            }
            if (caps == (ShaderCaps.AlphaTestEnabled | ShaderCaps.Spotlight))
            {
                return(b + 9);
            }
            if (caps == (ShaderCaps.FadeEnabled | ShaderCaps.Spotlight))
            {
                return(b + 10);
            }
            if (caps == (ShaderCaps.EtEnabled | ShaderCaps.Spotlight))
            {
                return(b + 11);
            }
            if (caps == (ShaderCaps.EtEnabled | ShaderCaps.AlphaTestEnabled | ShaderCaps.Spotlight))
            {
                return(b + 12);
            }
            if (caps == (ShaderCaps.FadeEnabled | ShaderCaps.AlphaTestEnabled | ShaderCaps.Spotlight))
            {
                return(b + 13);
            }
            if (caps == (ShaderCaps.EtEnabled | ShaderCaps.FadeEnabled | ShaderCaps.Spotlight))
            {
                return(b + 14);
            }
            if (caps == (ShaderCaps.EtEnabled | ShaderCaps.FadeEnabled | ShaderCaps.AlphaTestEnabled | ShaderCaps.Spotlight))
            {
                return(b + 15);
            }
            throw new NotImplementedException(caps.ToString());
        }
예제 #9
0
 public Strings2(string a, string b, ShaderCaps c)
 {
     A = a;
     B = b;
     C = c;
 }
예제 #10
0
 public static int GetIndex(this ShaderCaps caps)
 {
     if (caps == ShaderCaps.None)
     {
         return(0);
     }
     if (caps == ShaderCaps.AlphaTestEnabled)
     {
         return(1);
     }
     if (caps == ShaderCaps.FadeEnabled)
     {
         return(2);
     }
     if (caps == ShaderCaps.EtEnabled)
     {
         return(3);
     }
     if (caps == (ShaderCaps.EtEnabled | ShaderCaps.AlphaTestEnabled))
     {
         return(4);
     }
     if (caps == (ShaderCaps.FadeEnabled | ShaderCaps.AlphaTestEnabled))
     {
         return(5);
     }
     if (caps == (ShaderCaps.EtEnabled | ShaderCaps.FadeEnabled))
     {
         return(6);
     }
     if (caps == (ShaderCaps.EtEnabled | ShaderCaps.FadeEnabled | ShaderCaps.AlphaTestEnabled))
     {
         return(7);
     }
     if (caps == (ShaderCaps.Spotlight))
     {
         return(8);
     }
     if (caps == (ShaderCaps.AlphaTestEnabled | ShaderCaps.Spotlight))
     {
         return(9);
     }
     if (caps == (ShaderCaps.FadeEnabled | ShaderCaps.Spotlight))
     {
         return(10);
     }
     if (caps == (ShaderCaps.EtEnabled | ShaderCaps.Spotlight))
     {
         return(11);
     }
     if (caps == (ShaderCaps.EtEnabled | ShaderCaps.AlphaTestEnabled | ShaderCaps.Spotlight))
     {
         return(12);
     }
     if (caps == (ShaderCaps.FadeEnabled | ShaderCaps.AlphaTestEnabled | ShaderCaps.Spotlight))
     {
         return(13);
     }
     if (caps == (ShaderCaps.EtEnabled | ShaderCaps.FadeEnabled | ShaderCaps.Spotlight))
     {
         return(14);
     }
     if (caps == (ShaderCaps.EtEnabled | ShaderCaps.FadeEnabled | ShaderCaps.AlphaTestEnabled | ShaderCaps.Spotlight))
     {
         return(15);
     }
     throw new NotImplementedException(caps.ToString());
 }