コード例 #1
0
 public UniformBlock()
 {
     GL.GetInteger(GetPName.MaxUniformBufferBindings, out maxUniformIndex); MyGL.Check();
     engineUBO = new UniformBufferObject <EngineUniformStruct>(nextUniformIndex++, () => engine);
     modelUBO  = new UniformBufferObject <ModelUniformStruct>(nextUniformIndex++, () => model);
     lightUBO  = new UniformBufferObject <LightUniformStruct>(nextUniformIndex++, () => light);
 }
コード例 #2
0
        public void Clear()
        {
            //GL.Clear(ClearBufferMask.DepthBufferBit);
            float clearDepth = 1.0f;

            GL.ClearBuffer(ClearBuffer.Depth, 0, ref clearDepth); MyGL.Check();
        }
コード例 #3
0
 public void CreateBuffer()
 {
     if (VboHandle == -1)
     {
         VboHandle = GL.GenBuffer(); MyGL.Check();
     }
 }
コード例 #4
0
        void DebugDrawTexture(Texture2D texture, Vector4 positionScale, Vector4 positionOffset, float valueScale = 1, float valueOffset = 0)
        {
            var debugDrawTextureShader = Factory.GetShader("internal/debugDrawTexture.shader");

            GL.Disable(EnableCap.DepthTest); MyGL.Check();
            GL.Disable(EnableCap.CullFace); MyGL.Check();
            GL.Disable(EnableCap.Blend); MyGL.Check();

            GL.BindFramebuffer(FramebufferTarget.DrawFramebuffer, 0); MyGL.Check();
            GL.Viewport(0, 0, Width, Height); MyGL.Check();
            //GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit | ClearBufferMask.StencilBufferBit);

            debugDrawTextureShader.Uniforms.Set("debugDrawTexture", texture);
            debugDrawTextureShader.Uniforms.Set("debugDrawTexturePositionScale", positionScale);
            debugDrawTextureShader.Uniforms.Set("debugDrawTexturePositionOffset", positionOffset);
            debugDrawTextureShader.Uniforms.Set("debugDrawTextureScale", valueScale);
            debugDrawTextureShader.Uniforms.Set("debugDrawTextureOffset", valueOffset);
            debugDrawTextureShader.Uniforms.Set("debugDrawTextureGamma", 0.1f);

            debugDrawTextureShader.Bind();

            var quadMesh = Factory.GetMesh("internal/quad.obj");

            quadMesh.Draw();
        }
コード例 #5
0
        /// <summary>
        /// Reloads the shader if marked to reload, binds the shader, uploads all changed uniforms;
        /// </summary>
        public bool Bind()
        {
            if (LoadState == State.NotLoaded)
            {
                Load();
            }
            else if (ShouldReload)
            {
                Log.Info("Reloading " + file.VirtualPath);
                Dispose();
                Load();
                ShouldReload = false;
            }

            if (LoadState == State.LoadedError)
            {
                return(false);
            }

            if (lastBindedShaderHandle != ShaderProgramHandle)
            {
                GL.UseProgram(ShaderProgramHandle); MyGL.Check();
                lastBindedShaderHandle = ShaderProgramHandle;
            }
            Uniforms.UploadChangedUniforms(this);
            return(true);
        }
コード例 #6
0
 public void DeleteBuffer()
 {
     if (VboHandle != -1)
     {
         GL.DeleteBuffer(VboHandle); MyGL.Check();
     }
 }
コード例 #7
0
        void DrawBufferToQuarterOfScreen(ReadBufferMode buffer, int x, int y)
        {
            int qh = Height / 4;
            int qw = Width / 4;

            GL.ReadBuffer(buffer); MyGL.Check();
            GL.BlitFramebuffer(0, 0, Width, Height, x, y, x + qw, y + qh, ClearBufferMask.ColorBufferBit, BlitFramebufferFilter.Linear); MyGL.Check();
        }
コード例 #8
0
        public void GenerateMipMaps()
        {
            GL.BindTexture(TextureTarget.Texture2D, GetNativeTextureID()); MyGL.Check();

            GL.GenerateMipmap(GenerateMipmapTarget.Texture2D); MyGL.Check();

            GL.BindTexture(TextureTarget.Texture2D, 0); MyGL.Check();
        }
コード例 #9
0
        //public void SetPixel(int x, int y, Color color)
        //{
        //	if (KeepLocalCopyOfTexture == false) throw new Exception("before you can acces texture data you have to set " + nameof(KeepLocalCopyOfTexture) + " to true");
        //	lock (bmp)
        //	{
        //		if (bmp == null) throw new NullReferenceException("texture was intialized only with gpu handle, no data");
        //		if (x < 0 || x >= Width && y < 0 && y >= Height) throw new IndexOutOfRangeException("x or y is out of texture width or height");
        //		bmp.SetPixel(x, y, color);
        //	}
        //	VersionInRam++;
        //}


        //public Color GetPixel(int x, int y)
        //{
        //	if (KeepLocalCopyOfTexture == false) throw new Exception("before you can acces texture data you have to set " + nameof(KeepLocalCopyOfTexture) + " to true");
        //	lock (bmp)
        //	{
        //		if (bmp == null) throw new NullReferenceException("texture was intialized only with gpu handle, no data");
        //		if (x < 0 || x >= Width && y < 0 && y >= Height) throw new IndexOutOfRangeException("x or y is out of texture width or height");
        //		return bmp.GetPixel(x, y);
        //	}
        //}



        public void Dispose()
        {
            if (IsOnGpu)
            {
                GL.DeleteTexture(textureHandle); MyGL.Check();
                IsOnGpu = false;
            }
        }
コード例 #10
0
            public void UploadToGPU()
            {
                T d = getData();

                GL.BindBuffer(BufferTarget.UniformBuffer, bufferUBO); MyGL.Check();
                GL.BufferSubData(BufferTarget.UniformBuffer, (IntPtr)0, size, ref d); MyGL.Check();
                GL.BindBuffer(BufferTarget.UniformBuffer, 0); MyGL.Check(); //unbind
            }
コード例 #11
0
            public void DownloadDataFromGPU()
            {
                GL.BindBuffer(BufferTarget.ShaderStorageBuffer, VboHandle); MyGL.Check();
                //var intPtr = GL.MapBufferRange(BufferTarget.ShaderStorageBuffer, new IntPtr(), Count, BufferAccessMask.MapReadBit); MyGL.Check();
                var intPtr = GL.MapBuffer(BufferTarget.ShaderStorageBuffer, BufferAccess.ReadOnly); MyGL.Check();

                SetData(intPtr, Count);
                GL.UnmapBuffer(BufferTarget.ShaderStorageBuffer); MyGL.Check();
            }
コード例 #12
0
 public void Dispose()
 {
     GL.DeleteFramebuffer(frameBufferObjectHandle); MyGL.Check();
     depthTexture.Dispose();
     foreach (var t in textures)
     {
         t.Dispose();
     }
 }
コード例 #13
0
 public void Draw(bool drawWithTesselationSupport = false)
 {
     if (isOnGPU == false)
     {
         UploadDataToGpu();
     }
     GL.BindVertexArray(VertexArray.VaoHandle); MyGL.Check();
     GL.DrawElements(drawWithTesselationSupport ? PrimitiveType.Patches : PrimitiveType.Triangles, TriangleIndicies.Count, DrawElementsType.UnsignedInt, IntPtr.Zero); MyGL.Check();
     GL.BindVertexArray(0); MyGL.Check();
 }
コード例 #14
0
            public void SetDefaults()
            {
                // set these default values only once
                GL.DepthRange(0, 1); MyGL.Check();
                GL.DepthFunc(DepthFunction.Lequal); MyGL.Check();

                GL.FrontFace(FrontFaceDirection.Ccw); MyGL.Check();
                GL.CullFace(CullFaceMode.Back); MyGL.Check();
                GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Fill); MyGL.Check();
            }
コード例 #15
0
 public void DepthTest(bool enabled)
 {
     if (enabled)
     {
         GL.Enable(EnableCap.DepthTest); MyGL.Check();
     }
     else
     {
         GL.Disable(EnableCap.DepthTest); MyGL.Check();
     }
 }
コード例 #16
0
 public void Blend(bool enabled)
 {
     if (enabled)
     {
         GL.Enable(EnableCap.Blend); MyGL.Check();
     }
     else
     {
         GL.Disable(EnableCap.Blend); MyGL.Check();
     }
 }
コード例 #17
0
        public void BindForTransparentPass(Shader shader)
        {
            GL.BindFramebuffer(FramebufferTarget.DrawFramebuffer, frameBufferObjectHandle); MyGL.Check();
            GL.DrawBuffer(DrawBufferMode.ColorAttachment4); MyGL.Check();

            shader.Uniforms.Set("gBufferUniform.depth", depthTexture);
            for (int i = 0; i < textures.Length - 2; i++)
            {
                shader.Uniforms.Set("gBufferUniform." + ((GBufferTextures)i).ToString().ToLower(), textures[i]);
            }
        }
コード例 #18
0
        void CheckError(GetProgramParameterName n)
        {
            int statusCode = 0;

            GL.GetProgram(ShaderProgramHandle, n, out statusCode); MyGL.Check();
            if (statusCode != 1)
            {
                var infoLog = GL.GetProgramInfoLog(ShaderProgramHandle); MyGL.Check();
                Log.Error(n + "\n" + infoLog);
            }
        }
コード例 #19
0
 /// <summary>
 /// Delete buffers
 /// </summary>
 public void Dispose()
 {
     if (VaoHandle != -1)
     {
         GL.DeleteVertexArray(VaoHandle); MyGL.Check();
         VaoHandle = -1;
     }
     foreach (var kvp in nameToVbo)
     {
         kvp.Value.DeleteBuffer();
     }
 }
コード例 #20
0
            public UniformBufferObject(int index, Func <T> getData)
            {
                bufferIndex  = index;
                this.getData = getData;
                size         = System.Runtime.InteropServices.Marshal.SizeOf(typeof(T));

                bufferUBO = GL.GenBuffer(); MyGL.Check();                                                                            // Generate the buffer
                GL.BindBuffer(BufferTarget.UniformBuffer, bufferUBO); MyGL.Check();                                                  // Bind the buffer for writing
                GL.BufferData(BufferTarget.UniformBuffer, (IntPtr)(size), (IntPtr)(null), BufferUsageHint.StreamDraw); MyGL.Check(); // Request the memory to be allocated
                GL.BindBufferRange(BufferRangeTarget.UniformBuffer, bufferIndex, bufferUBO, (IntPtr)0, size); MyGL.Check();
                GL.BindBuffer(BufferTarget.UniformBuffer, 0); MyGL.Check();                                                          //unbind
            }
コード例 #21
0
        public bool SetUniformBlockBufferIndex(string name, int uniformBufferIndex)
        {
            var location = GL.GetUniformBlockIndex(ShaderProgramHandle, name); MyGL.Check();

            if (location == -1)
            {
                Log.Warn(file + ", uniform block index " + name + " not found ");
                return(false);
            }
            GL.UniformBlockBinding(ShaderProgramHandle, location, uniformBufferIndex); MyGL.Check();
            return(true);
        }
コード例 #22
0
 public void BindBufferToVAO()
 {
     if (UsesLayoutIndex)
     {
         GL.EnableVertexAttribArray(LayoutIndex); MyGL.Check();
     }
     GL.BindBuffer(GL_BufferTarget, VboHandle); MyGL.Check();
     if (UsesLayoutIndex)
     {
         //GL.VertexAttribPointer(Shader.positionLocation, 3, VertexAttribPointerType.Float, false, Vector3.SizeInBytes, 0); My.Check();
         GL.VertexAttribPointer(LayoutIndex, DataStrideInElementsNumber, GL_PointerType, false, DataSizeOfOneElementInBytes, offset); MyGL.Check();
     }
 }
コード例 #23
0
            public void UploadDataToGPU()
            {
                CreateBuffer();
                int sizeFromGpu;

                GL.BindBuffer(GL_BufferTarget, VboHandle); MyGL.Check();
                var arr  = this.ToArray();
                var size = NumberOfElements * DataSizeOfOneElementInBytes;

                GL.BufferData(GL_BufferTarget, size, arr, BufferUsageHint.DynamicRead); MyGL.Check();                 // BufferUsageHint explained: http://www.informit.com/articles/article.aspx?p=1377833&seqNum=7
                GL.GetBufferParameter(GL_BufferTarget, BufferParameterName.BufferSize, out sizeFromGpu); MyGL.Check();
                // if (size != sizeFromGpu) Log.Error(myName + " size mismatch size=" + GL_BufferTarget + " sizeFromGpu=" + sizeFromGpu);
                GL.BindBuffer(GL_BufferTarget, 0); MyGL.Check();
            }
コード例 #24
0
        public int GetUniformLocation(string name)
        {
            int location = -1;

            if (cachedUniformLocations.TryGetValue(name, out location) == false)
            {
                location = GL.GetUniformLocation(ShaderProgramHandle, name); MyGL.Check();
                if (location == -1)
                {
                    Log.Warn(file + ", uniform " + name + " not found ");
                }
                cachedUniformLocations[name] = location;
            }
            return(location);
        }
コード例 #25
0
        public ShadowMap(Light light, int width, int height)
        {
            this.light = light;

            ShadowViewCamera = light.Entity.AddComponent <Camera>();
            ShadowViewCamera.SetSize(width, height);
            ShadowViewCamera.IsOrthographic   = true;
            ShadowViewCamera.OrthographicSize = 50;

            this.Width  = width;
            this.Height = height;

            // create frame buffer object
            FrameBufferObjectHandle = GL.GenFramebuffer(); MyGL.Check();
            GL.BindFramebuffer(FramebufferTarget.Framebuffer, FrameBufferObjectHandle); MyGL.Check();

            DepthMap = new Texture2D(GL.GenTexture());

            GL.BindTexture(TextureTarget.Texture2D, DepthMap.GetNativeTextureID()); MyGL.Check();

            GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.DepthComponent32f, width, height, 0, PixelFormat.DepthComponent, PixelType.Float, new IntPtr(0)); MyGL.Check();

            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear); MyGL.Check();
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear); MyGL.Check();
            //GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Nearest);
            //GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Nearest);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.Clamp); MyGL.Check();
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.Clamp); MyGL.Check();

            // breaks it, but should enable hardware 4 pcf sampling
            //GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureCompareMode, (int)TextureCompareMode.CompareRToTexture);
            //GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureCompareFunc, (int)All.Lequal);
            //GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.DepthTextureMode, (int)All.Intensity);

            GL.FramebufferTexture(FramebufferTarget.Framebuffer, FramebufferAttachment.DepthAttachment, DepthMap.GetNativeTextureID(), 0); MyGL.Check();
            GL.DrawBuffer(DrawBufferMode.None); MyGL.Check();
            var status = GL.CheckFramebufferStatus(FramebufferTarget.Framebuffer); MyGL.Check();

            if (status != FramebufferErrorCode.FramebufferComplete)
            {
                throw new GLError(status);
            }

            GL.ReadBuffer(ReadBufferMode.None); MyGL.Check();
            GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0); MyGL.Check();             //restore default FBO
        }
コード例 #26
0
        public void BindForLightPass(Shader shader)
        {
            readFirstFinalTexture = true;

            GL.Disable(EnableCap.DepthTest); MyGL.Check();
            GL.DepthMask(false); MyGL.Check();
            GL.CullFace(CullFaceMode.Back); MyGL.Check();

            GL.BindFramebuffer(FramebufferTarget.DrawFramebuffer, frameBufferObjectHandle); MyGL.Check();
            GL.DrawBuffer(DrawBufferMode.ColorAttachment4); MyGL.Check();

            shader.Uniforms.Set("gBufferUniform.depth", depthTexture);
            for (int i = 0; i < textures.Length - 2; i++)
            {
                shader.Uniforms.Set("gBufferUniform." + ((GBufferTextures)i).ToString().ToLower(), textures[i]);
            }
        }
コード例 #27
0
        void FinalizeInit()
        {
            /*
             * GL.BindAttribLocation(shaderProgramHandle, Shader.positionLocation, "in_position"); My.Check();
             * GL.BindAttribLocation(shaderProgramHandle, Shader.normalLocation, "in_normal"); My.Check();
             * GL.BindAttribLocation(shaderProgramHandle, Shader.tangentLocation, "in_tangent"); My.Check();
             * GL.BindAttribLocation(shaderProgramHandle, Shader.uvLocation, "in_uv"); My.Check();
             */
            GL.LinkProgram(ShaderProgramHandle); MyGL.Check();
            CheckError(GetProgramParameterName.LinkStatus);

            GL.ValidateProgram(ShaderProgramHandle); MyGL.Check();
            CheckError(GetProgramParameterName.ValidateStatus);

            EngineMain.ubo.SetUniformBuffers(this);

            //Debug.WriteLine(GL.GetProgramInfoLog(shaderProgramHandle));
        }
コード例 #28
0
        public void BindForPostProcessEffects(IPostProcessEffect postProcess)
        {
            var shader          = postProcess.Shader;
            var generateMipMaps = postProcess.RequiresGBufferMipMaps;

            // generate mip maps for final texture, so it can be used in post processing effects, many post processing effects require blurred texture
            if (generateMipMaps)
            {
                /*GL.ActiveTexture(TextureUnit.Texture0);
                 * GL.BindTexture(TextureTarget.Texture2D, finalTextureToWriteTo.GetNativeTextureID()); My.Check();
                 * GL.GenerateMipmap(GenerateMipmapTarget.Texture2D); My.Check();*/

                GL.ActiveTexture(TextureUnit.Texture0); MyGL.Check();
                GL.BindTexture(TextureTarget.Texture2D, FinalTextureToRead.GetNativeTextureID()); MyGL.Check();
                GL.GenerateMipmap(GenerateMipmapTarget.Texture2D); MyGL.Check();
            }

            GL.Disable(EnableCap.DepthTest); MyGL.Check();
            GL.DepthMask(false); MyGL.Check();
            GL.CullFace(CullFaceMode.Back); MyGL.Check();

            GL.BindFramebuffer(FramebufferTarget.DrawFramebuffer, frameBufferObjectHandle); MyGL.Check();

            // draw to the one we are not reading
            if (readFirstFinalTexture == false)
            {
                GL.DrawBuffer(DrawBufferMode.ColorAttachment4); MyGL.Check();
            }
            else
            {
                GL.DrawBuffer(DrawBufferMode.ColorAttachment5); MyGL.Check();
            }

            shader.Uniforms.Set("gBufferUniform.depth", depthTexture);
            shader.Uniforms.Set("gBufferUniform.final", FinalTextureToRead);

            for (int i = 0; i < textures.Length - 2; i++)
            {
                shader.Uniforms.Set("gBufferUniform." + ((GBufferTextures)i).ToString().ToLower(), textures[i]);
            }

            readFirstFinalTexture = !readFirstFinalTexture;
        }
コード例 #29
0
        void Load()
        {
            ShaderProgramHandle = GL.CreateProgram(); MyGL.Check();

            var builder = new ShaderBuilder(file.FileSystem);
            var success = true;

            builder.LoadAndParse(file);

            if (builder.buildResults.Count == 0)
            {
                Log.Error("no shader parts were found, possible part markers are: " + Enum.GetNames(typeof(ShaderType)).Select(s => "[" + s + "]").Join(" "));
            }

            foreach (var r in builder.buildResults)
            {
                success &= AttachShader(builder, r.shaderContents, r.shaderType, r.filePath);
            }

            FinalizeInit();

            if (success)
            {
                Log.Info(typeof(Shader) + " " + file + " loaded successfully");
                LoadState    = State.LoadedSuccess;
                VersionOnGpu = VersionInFile;
            }
            else
            {
                LoadState = State.LoadedError;
                Log.Error("fix the error then press any key to reload ...");
                Console.ReadKey();
                Log.Error("reloading ...");
                Load();
                return;
            }

            file.OnFileChanged(NotifyFileChanged);
            builder.includedFiles.ForEach(f => f.OnFileChanged(NotifyFileChanged));

            Uniforms.MarkAllUniformsAsChanged();
            cachedUniformLocations.Clear();
        }
コード例 #30
0
 public void DrawLinesOnly(bool linesOnly, bool drawFrontOnly = true)
 {
     if (linesOnly)
     {
         GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Line); MyGL.Check();
         GL.Disable(EnableCap.CullFace); MyGL.Check();
     }
     else
     {
         if (drawFrontOnly)
         {
             GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Fill); MyGL.Check();
             GL.Enable(EnableCap.CullFace); MyGL.Check();
         }
         else
         {
             GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Fill); MyGL.Check();
             GL.Disable(EnableCap.CullFace); MyGL.Check();
         }
     }
 }