Пример #1
0
        private ModelViewControl() : base(new GraphicsMode(32, 24, 0, 4), 3, 3,
                                          GraphicsContextFlags.ForwardCompatible)
        {
            InitializeComponent();
            MakeCurrent();

            mGridShader    = GLShaderProgram.Create("Grid");
            mDefaultShader = GLShaderProgram.Create("Default") ??
                             GLShaderProgram.Create("DefaultBasic");

            if (!CanRender)
            {
                Debug.WriteLine("Shader compilation failed. GL rendering will be disabled.");

                Visible = false;
                return;
            }

            GL.FrontFace(FrontFaceDirection.Ccw);
            GL.CullFace(CullFaceMode.Back);
            GL.Enable(EnableCap.CullFace);
            GL.Enable(EnableCap.DepthTest);
            GL.Enable(EnableCap.FramebufferSrgb);
            GL.Enable(EnableCap.PrimitiveRestart);
            GL.PrimitiveRestartIndex(0xFFFF);
        }
Пример #2
0
        public GLShaderProgram Get(string name)
        {
            if (!mLoadedShaders.TryGetValue(name, out var shader))
            {
                var vs = FileManager.Instance.OpenText($"shaders/{name}.glsl.vs").ReadToEnd();
                var fs = FileManager.Instance.OpenText($"shaders/{name}.glsl.fs").ReadToEnd();

                using (var builder = new GLShaderProgramBuilder())
                {
                    if (!builder.TryAttachShader(ShaderType.VertexShader, vs))
                    {
                        return(null);
                    }

                    if (!builder.TryAttachShader(ShaderType.FragmentShader, fs))
                    {
                        return(null);
                    }

                    if (!builder.TryBuild(out var id))
                    {
                        return(null);
                    }

                    shader = new GLShaderProgram(id);
                }

                mLoadedShaders[name] = shader;
            }

            return(shader);
        }
Пример #3
0
        public static IGLProgram CompileCompute(string shader)
        {
            GLComputeShader cmp = new GLComputeShader(shader);
            string          log = null;

            if (!cmp.Compile(out log))
            {
                cmp.Release();
                Log.Error(log);
                Log.Debug(Environment.StackTrace);
                return(null);
            }

            IGLProgram program = new GLShaderProgram(true);

            program.AttachShader(cmp);

            if (!program.Link(out log))
            {
                program.Release();
                Log.Error(log);
                Log.Debug(Environment.StackTrace);
                return(null);
            }

            return(program);
        }
Пример #4
0
        private ModelViewControl() : base(new GraphicsMode(32, 24, 0, 4), 3, 3,
                                          GraphicsContextFlags.ForwardCompatible)
        {
            InitializeComponent();
            MakeCurrent();

            mGridShader    = GLShaderProgram.Create("Grid");
            mDefaultShader = GLShaderProgram.Create("Default");

            if (!CanRender)
            {
                MessageBox.Show("Shader compilation failed. GL rendering will be disabled.", "Miku Miku Model",
                                MessageBoxButtons.OK, MessageBoxIcon.Error);

                Visible = false;
                return;
            }

            GL.FrontFace(FrontFaceDirection.Ccw);
            GL.CullFace(CullFaceMode.Back);
            GL.Enable(EnableCap.CullFace);
            GL.Enable(EnableCap.DepthTest);
            GL.Enable(EnableCap.PrimitiveRestart);
            GL.PrimitiveRestartIndex(0xFFFF);
        }
Пример #5
0
        public virtual void DrawForDepth()
        {
            if (Mat != null && Mat.Shader != null)
            {
                GLShaderProgram shader = Mat.Shader;

                //use shader
                shader.Use();

                Matrix4 proj = Projection;
                Matrix4 view = View;
                Matrix4 m    = Model;
                Matrix4 norm = Matrix4.Invert(m);
                norm.Transpose();

                shader.SetUniformMatrix4("projectionMatrix", ref proj);
                shader.SetUniformMatrix4("viewMatrix", ref view);
                shader.SetUniformMatrix4("modelMatrix", ref m);
                shader.SetUniformMatrix4("normalMatrix", ref norm);

                Vector2 tiling = Tiling;

                shader.SetUniform2("tiling", ref tiling);

                ///draw
                vao.Bind();
                GL.DrawElements(BeginMode.Triangles, indicesCount, DrawElementsType.UnsignedInt, 0);
                GLVertexArray.Unbind();
            }
        }
Пример #6
0
        public override void Create()
        {
            VAO = new("Cubemap VAO");
            VBO = new("Cubemap VBO");

            VAO.Bind();

            VBO.Bind(BufferTarget.ArrayBuffer);

            VBO.Data(skyboxVertices, BufferUsageHint.DynamicDraw);

            var VS = File.ReadAllText("EngineResources/Shaders/Cubemap/cubemap.vert");
            var FS = File.ReadAllText("EngineResources/Shaders/Cubemap/cubemap.frag");

            var VP = new GLShader(VS, GLShaderType.Vertex);
            var FP = new GLShader(FS, GLShaderType.Fragment);

            CubemapShader = new GLShaderProgram()
                            .Label("Cubemap Shader")
                            .Attach(VP)
                            .Attach(FP)
                            .Link();

            GL.EnableVertexAttribArray(0);
            GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, 3 * sizeof(float), 0);

            GL.BindVertexArray(0);
        }
Пример #7
0
        public static GLShaderProgram GetShader(string vertFile, string fragFile)
        {
            string path       = AppDomain.CurrentDomain.BaseDirectory;
            string vertexPath = Path.Combine(path, "Shaders", "Vertex", vertFile);
            string fragPath   = Path.Combine(path, "Shaders", "Frag", fragFile);

            GLShaderProgram shader = null;

            if (Shaders.TryGetValue(vertexPath + fragPath, out shader))
            {
                return(shader);
            }

            if (!File.Exists(vertexPath))
            {
                return(null);
            }
            if (!File.Exists(fragPath))
            {
                return(null);
            }

            string vertexData = File.ReadAllText(vertexPath);
            string fragdata   = File.ReadAllText(fragPath);

            GLFragmentShader frag = new GLFragmentShader(fragdata);
            string           log  = null;

            if (!frag.Compile(out log))
            {
                frag.Release();
                Console.WriteLine(log);
                return(null);
            }

            GLVertexShader vert = new GLVertexShader(vertexData);

            if (!vert.Compile(out log))
            {
                vert.Release();
                Console.WriteLine(log);
                return(null);
            }

            shader = new GLShaderProgram(true);
            shader.AttachShader(vert);
            shader.AttachShader(frag);

            if (!shader.Link(out log))
            {
                shader.Release();
                Console.WriteLine(log);
                return(null);
            }

            Shaders[vertexPath + fragPath] = shader;

            return(shader);
        }
Пример #8
0
 public CubismAvaloniaShaderManager(GlInterface gl)
 {
     MaskDrawingShader         = new SetupMaskShaderProgram(gl);
     UnmaskedMeshDrawingShader = new UnmaskedShaderProgram(gl);
     MaskedMeshDrawingShader   = new MaskedShaderProgram(gl);
     UnmaskedPremultipliedAlphaMeshDrawingShader = new UnmaskedPremultipliedAlphaShaderProgram(gl);
     MaskedPremultipliedAlphaMeshDrawingShader   = new MaskedPremultipliedAlphaShaderProgram(gl);
 }
Пример #9
0
 public CubismOpenTKShaderManager()
 {
     MaskDrawingShader         = new SetupMaskShaderProgram();
     UnmaskedMeshDrawingShader = new UnmaskedShaderProgram();
     MaskedMeshDrawingShader   = new MaskedShaderProgram();
     UnmaskedPremultipliedAlphaMeshDrawingShader = new UnmaskedPremultipliedAlphaShaderProgram();
     MaskedPremultipliedAlphaMeshDrawingShader   = new MaskedPremultipliedAlphaShaderProgram();
 }
Пример #10
0
 public GLDrawCall(GLShaderProgram program, GLTextureBind[] textureBinds, GLVertexArrayObject vertexArrayObject,
                   UniformBind[] uniformBinds, Action drawAction, GLState state = null)
 {
     Program           = program;
     TextureBinds      = textureBinds;
     VertexArrayObject = vertexArrayObject;
     UniformBinds      = uniformBinds;
     DrawAction        = drawAction;
     State             = state ?? GLState.Default;
 }
Пример #11
0
        protected Shader()
        {
            ShaderProgram = GLShaderProgram.Create(GLShader.CreateVertex(VertexShaderSource), GLShader.CreateFragment(FragmentShaderSource), GLShader.CreateGeometry(GeometryShaderSource));

            if (ShaderProgram is not null)
            {
                InitializeUniforms();
                InitializeAttributes();
            }
        }
        private void LoadLightingShaders()
        {
            var depthVert = ReadEmbeddedShader("shadow-depth.vert");
            var depthFrag = ReadEmbeddedShader("shadow-depth.frag");

            _fovCalculationProgram = _compileProgram(depthVert, depthFrag, "Shadow Depth Program");

            var debugShader = _resourceCache.GetResource <ShaderSourceResource>("/Shaders/Internal/depth-debug.swsl");

            _fovDebugShaderInstance = (ClydeShaderInstance)InstanceShader(debugShader.ClydeHandle);
        }
Пример #13
0
        public static IGLProgram CompileFragWithVert(string vertFile, string fragData)
        {
            string path       = AppDomain.CurrentDomain.BaseDirectory;
            string vertexPath = Path.Combine(path, "Shaders", "Vertex", vertFile);

            IGLProgram shader = null;

            if (!File.Exists(vertexPath))
            {
                return(null);
            }

            string vertexData = File.ReadAllText(vertexPath);

            GLFragmentShader frag = new GLFragmentShader(fragData);
            string           log  = null;

            if (!frag.Compile(out log))
            {
                frag.Release();
                Log.Error(log);
                Log.Debug(Environment.StackTrace);
                Log.Debug("Shader failure for: " + vertFile);
                return(null);
            }

            GLVertexShader vert = new GLVertexShader(vertexData);

            if (!vert.Compile(out log))
            {
                vert.Release();
                Log.Error(log);
                Log.Debug(Environment.StackTrace);
                Log.Debug("Shader failure for: " + vertFile);
                return(null);
            }

            shader = new GLShaderProgram(true);
            shader.AttachShader(vert);
            shader.AttachShader(frag);

            if (!shader.Link(out log))
            {
                shader.Release();
                Log.Error(log);
                Log.Debug(Environment.StackTrace);
                Log.Debug("Shader failure for: " + vertFile);
                return(null);
            }

            return(shader);
        }
Пример #14
0
        private void InitializeOnce()
        {
            if (!sInitialized)
            {
                sVertexBuffer = new VertexBufferBuilder()
                                .AddAttributeBuffer(sVertices, BufferUsageHint.StaticDraw, new VertexAttributeDescriptor(VertexAttributeType.Position, 3, VertexAttributeDataType.Float, 0, 0))
                                .AddAttributeBuffer(sNormals, BufferUsageHint.StaticDraw, new VertexAttributeDescriptor(VertexAttributeType.Normal, 3, VertexAttributeDataType.Float, 0, 0))
                                .Build();

                sShader      = ShaderStore.Instance.Get("shaded_uniform_color");
                sInitialized = true;
            }
        }
Пример #15
0
        private GLShaderProgram _compileProgram(string vertexSource, string fragmentSource, string name = null)
        {
            GLShader vertexShader   = null;
            GLShader fragmentShader = null;

            try
            {
                try
                {
                    vertexShader = new GLShader(this, ShaderType.VertexShader, vertexSource, name == null ? $"{name}-vertex" : null);
                }
                catch (ShaderCompilationException e)
                {
                    throw new ShaderCompilationException(
                              "Failed to compile vertex shader, see inner for details.", e);
                }

                try
                {
                    fragmentShader = new GLShader(this, ShaderType.FragmentShader, fragmentSource, name == null ? $"{name}-fragment" : null);
                }
                catch (ShaderCompilationException e)
                {
                    throw new ShaderCompilationException(
                              "Failed to compile fragment shader, see inner for details.", e);
                }

                var program = new GLShaderProgram(this, name);
                program.Add(vertexShader);
                program.Add(fragmentShader);

                try
                {
                    program.Link();
                }
                catch (ShaderCompilationException e)
                {
                    program.Delete();

                    throw new ShaderCompilationException("Failed to link shaders. See inner for details.", e);
                }

                return(program);
            }
            finally
            {
                vertexShader?.Delete();
                fragmentShader?.Delete();
            }
        }
Пример #16
0
        /// <summary>
        ///   This must be called on the GPU thread.
        /// </summary>
        /// <param name="shaderProgram"></param>
        public void SetIfPresent(GLShaderProgram shaderProgram)
        {
            Debug.Assert(shaderProgram.GLHandle != -1);
            Debug.Assert(Thread.CurrentThread.Name == RenderContext.GpuThreadName);
            // Get location of attribute from shader program
            var index = shaderProgram.GetAttributeLocation(_name);

            if (index < 0)
            {
                return;
            }
            // Enable and set attribute
            GL.EnableVertexAttribArray(index);
            GL.VertexAttribPointer(index, _size, _type, _normalize, _stride, _offset);
        }
Пример #17
0
        private void LoadStockShaders()
        {
            _shaderWrapCodeDefaultFrag = ReadEmbeddedShader("base-default.frag");
            _shaderWrapCodeDefaultVert = ReadEmbeddedShader("base-default.vert");

            _shaderWrapCodeRawVert = ReadEmbeddedShader("base-raw.vert");
            _shaderWrapCodeRawFrag = ReadEmbeddedShader("base-raw.frag");

            var defaultLoadedShader = _resourceCache
                                      .GetResource <ShaderSourceResource>("/Shaders/Internal/default-sprite.swsl").ClydeHandle;

            _defaultShader = (ClydeShaderInstance)InstanceShader(defaultLoadedShader);

            _queuedShader = _defaultShader.Handle;

            var lightVert = ReadEmbeddedShader("light.vert");
            var lightFrag = ReadEmbeddedShader("light.frag");

            _lightShader = _compileProgram(lightVert, lightFrag, "_lightShader");
        }
        private void LoadLightingShaders()
        {
            var depthVert = ReadEmbeddedShader("shadow-depth.vert");
            var depthFrag = ReadEmbeddedShader("shadow-depth.frag");

            _fovCalculationProgram = _compileProgram(depthVert, depthFrag, "Shadow Depth Program");

            var debugShader = _resourceCache.GetResource <ShaderSourceResource>("/Shaders/Internal/depth-debug.swsl");

            _fovDebugShaderInstance = (ClydeShaderInstance)InstanceShader(debugShader.ClydeHandle);

            ClydeHandle LoadShaderHandle(string path)
            {
                try
                {
                    var shaderSource = _resourceCache.GetResource <ShaderSourceResource>(path);
                    return(shaderSource.ClydeHandle);
                }
                catch (Exception ex)
                {
                    Logger.Warning($"Can't load shader {path}\n{ex.GetType().Name}: {ex.Message}");
                    return(default);
Пример #19
0
        private static void initShaders()
        {
            var fragmentShader = GetShader(gl, "shader-fs");
            var vertexShader   = GetShader(gl, "shader-vs");

            shaderProgram = gl.CreateProgram();
            gl.AttachShader(shaderProgram, vertexShader);
            gl.AttachShader(shaderProgram, fragmentShader);
            gl.LinkProgram(shaderProgram);

            if (!gl.GetProgramParameter(shaderProgram, gl.LinkStatus))
            {
                throw new Exception("Could not initialize shaders");
            }

            gl.UseProgram(shaderProgram);

            vertexPositionAttribute = gl.GetAttributeLocation(shaderProgram, "aVertexPosition");
            gl.EnableVertexAttributeArray(vertexPositionAttribute);

            pMatrixUniform  = gl.GetUniformLocation(shaderProgram, "uPMatrix");
            mvMatrixUniform = gl.GetUniformLocation(shaderProgram, "uMVMatrix");
        }
Пример #20
0
        public GridPrimitive(int gridSize, int gridSpacing)
        {
            // thanks Skyth
            var vertices = new List <Vector3>();

            for (int i = -gridSize; i <= gridSize; i += gridSpacing)
            {
                vertices.Add(new Vector3(i, 0, -gridSize));
                vertices.Add(new Vector3(i, 0, gridSize));
                vertices.Add(new Vector3(-gridSize, 0, i));
                vertices.Add(new Vector3(gridSize, 0, i));
            }

            mMinZ        = vertices.Min(x => x.Z);
            mVertexArray = GL.GenVertexArray();
            GL.BindVertexArray(mVertexArray);

            mVertexBuffer = new GLBuffer <Vector3>(BufferTarget.ArrayBuffer, vertices.ToArray());

            GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, mVertexBuffer.Stride, 0);
            GL.EnableVertexAttribArray(0);

            mShader = ShaderStore.Instance.Get("line");
        }
Пример #21
0
        private void LoadLightingShaders()
        {
            var depthVert = ReadEmbeddedShader("shadow-depth.vert");
            var depthFrag = ReadEmbeddedShader("shadow-depth.frag");

            _fovCalculationProgram = _compileProgram(depthVert, depthFrag, "Shadow Depth Program");

            var debugShader = _resourceCache.GetResource <ShaderSourceResource>("/Shaders/Internal/depth-debug.swsl");

            _fovDebugShaderInstance = (ClydeShaderInstance)InstanceShader(debugShader.ClydeHandle);

            ClydeHandle LoadShaderHandle(string path)
            {
                var shaderSource = _resourceCache.GetResource <ShaderSourceResource>(path);

                return(shaderSource.ClydeHandle);
            }

            _lightShaderHandle          = LoadShaderHandle("/Shaders/Internal/light.swsl");
            _fovShaderHandle            = LoadShaderHandle("/Shaders/Internal/fov.swsl");
            _fovLightShaderHandle       = LoadShaderHandle("/Shaders/Internal/fov-lighting.swsl");
            _wallBleedBlurShaderHandle  = LoadShaderHandle("/Shaders/Internal/wall-bleed-blur.swsl");
            _mergeWallLayerShaderHandle = LoadShaderHandle("/Shaders/Internal/wall-merge.swsl");
        }
Пример #22
0
 public InvertProcessor() : base()
 {
     shader = GetShader("image.glsl", "invert.glsl");
 }
Пример #23
0
 public EmbossProcessor() : base()
 {
     shader = GetShader("image.glsl", "emboss.glsl");
 }
Пример #24
0
 public BlurProcessor() : base()
 {
     shader = GetShader("image.glsl", "blur.glsl");
 }
Пример #25
0
 public GrayscaleConvProcessor() : base()
 {
     shader = GetShader("image.glsl", "grayscaleconv.glsl");
 }
Пример #26
0
 public MeshDepthProcessor() : base()
 {
     shader = GetShader("image.glsl", "image-basic.glsl");
 }
Пример #27
0
 public UniformColorProcessor() : base()
 {
     shader = GetShader("image.glsl", "uniformcolor.glsl");
 }
Пример #28
0
        public override void Draw()
        {
            if (Mat != null && Mat.Shader != null)
            {
                GLShaderProgram shader = Mat.Shader;

                GL.ActiveTexture(TextureUnit.Texture0);
                if (Mat.Albedo != null)
                {
                    Mat.Albedo.Bind();
                }

                GL.ActiveTexture(TextureUnit.Texture1);
                if (Mat.Metallic != null)
                {
                    Mat.Metallic.Bind();
                }

                GL.ActiveTexture(TextureUnit.Texture2);
                if (Mat.Roughness != null)
                {
                    Mat.Roughness.Bind();
                }

                GL.ActiveTexture(TextureUnit.Texture3);
                if (Mat.Occlusion != null)
                {
                    Mat.Occlusion.Bind();
                }

                GL.ActiveTexture(TextureUnit.Texture4);
                if (Mat.Normal != null)
                {
                    Mat.Normal.Bind();
                }

                GL.ActiveTexture(TextureUnit.Texture5);
                if (Mat.Height != null)
                {
                    Mat.Height.Bind();
                }

                GL.ActiveTexture(TextureUnit.Texture6);
                PBRMaterial.BRDFLut.Bind();

                GL.ActiveTexture(TextureUnit.Texture7);
                if (IrradianceMap != null)
                {
                    IrradianceMap.Bind();
                }

                GL.ActiveTexture(TextureUnit.Texture8);
                if (PrefilterMap != null)
                {
                    PrefilterMap.Bind();
                }

                //use shader
                shader.Use();

                //set texture bind points
                shader.SetUniform("albedo", 0);
                shader.SetUniform("metallicMap", 1);
                shader.SetUniform("roughnessMap", 2);
                shader.SetUniform("occlusionMap", 3);
                shader.SetUniform("normalMap", 4);
                shader.SetUniform("heightMap", 5);
                shader.SetUniform("brdfLUT", 6);
                shader.SetUniform("irradianceMap", 7);
                shader.SetUniform("prefilterMap", 8);

                Vector3 lpos = LightPosition;
                Vector3 lc   = LightColor;

                Vector3 cam = CameraPosition;

                //set camera and light stuff
                shader.SetUniform3("cameraPosition", ref cam);
                shader.SetUniform3("lightPosition", ref lpos);
                shader.SetUniform3("lightColor", ref lc);

                //setup MVP and N matrices
                Matrix4 proj = Projection;
                Matrix4 view = View;
                Matrix4 m    = Model;
                Matrix4 norm = Matrix4.Invert(m);
                norm.Transpose();

                shader.SetUniformMatrix4("projectionMatrix", ref proj);
                shader.SetUniformMatrix4("viewMatrix", ref view);
                shader.SetUniformMatrix4("modelMatrix", ref m);
                shader.SetUniformMatrix4("normalMatrix", ref norm);

                Vector2 tiling = Tiling;

                shader.SetUniform2("tiling", ref tiling);

                ///draw
                vao.Bind();
                GL.DrawElements(BeginMode.Triangles, indicesCount, DrawElementsType.UnsignedInt, 0);
                GLVertexArray.Unbind();
                GLTextuer2D.Unbind();
            }
        }
Пример #29
0
 public BasicImageRenderer() : base()
 {
     shader = GetShader("image.glsl", "image-basic.glsl");
 }
Пример #30
0
 public BlendProcessor() : base()
 {
     shader = GetShader("image.glsl", "blend.glsl");
 }