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); }
public void Clear() { //GL.Clear(ClearBufferMask.DepthBufferBit); float clearDepth = 1.0f; GL.ClearBuffer(ClearBuffer.Depth, 0, ref clearDepth); MyGL.Check(); }
public void CreateBuffer() { if (VboHandle == -1) { VboHandle = GL.GenBuffer(); MyGL.Check(); } }
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(); }
/// <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); }
public void DeleteBuffer() { if (VboHandle != -1) { GL.DeleteBuffer(VboHandle); MyGL.Check(); } }
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(); }
public void GenerateMipMaps() { GL.BindTexture(TextureTarget.Texture2D, GetNativeTextureID()); MyGL.Check(); GL.GenerateMipmap(GenerateMipmapTarget.Texture2D); MyGL.Check(); GL.BindTexture(TextureTarget.Texture2D, 0); MyGL.Check(); }
//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; } }
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 }
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(); }
public void Dispose() { GL.DeleteFramebuffer(frameBufferObjectHandle); MyGL.Check(); depthTexture.Dispose(); foreach (var t in textures) { t.Dispose(); } }
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(); }
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(); }
public void DepthTest(bool enabled) { if (enabled) { GL.Enable(EnableCap.DepthTest); MyGL.Check(); } else { GL.Disable(EnableCap.DepthTest); MyGL.Check(); } }
public void Blend(bool enabled) { if (enabled) { GL.Enable(EnableCap.Blend); MyGL.Check(); } else { GL.Disable(EnableCap.Blend); MyGL.Check(); } }
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]); } }
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); } }
/// <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(); } }
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 }
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); }
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(); } }
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(); }
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); }
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 }
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]); } }
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)); }
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; }
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(); }
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(); } } }