public void compileShader(GLSLShaderConfig config) { if (config.program_id != -1) { GL.DeleteProgram(config.program_id); } GLShaderHelper.CreateShaders(config); }
private void glControl1_Load(object sender, EventArgs e) { GL.Viewport(0, 0, glControl1.ClientSize.Width, glControl1.ClientSize.Height); GL.ClearColor(System.Drawing.Color.Black); GL.Enable(EnableCap.DepthTest); //glControl1.SwapBuffers(); //glControl1.Invalidate(); Debug.WriteLine("GL Cleared"); Debug.WriteLine(GL.GetError()); this.glloaded = true; //Generate Geometry VBOs GL.GenBuffers(1, out quad_vbo); GL.GenBuffers(1, out quad_ebo); //Bind Geometry Buffers int arraysize = sizeof(float) * 4 * 3; //Upload vertex buffer GL.BindBuffer(BufferTarget.ArrayBuffer, quad_vbo); //Allocate to NULL GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(arraysize), (IntPtr)null, BufferUsageHint.StaticDraw); //Add verts data GL.BufferSubData(BufferTarget.ArrayBuffer, (IntPtr)0, (IntPtr)arraysize, quadverts); //Upload index buffer GL.BindBuffer(BufferTarget.ElementArrayBuffer, quad_ebo); GL.BufferData(BufferTarget.ElementArrayBuffer, (IntPtr)(sizeof(Int32) * 6), quadindices, BufferUsageHint.StaticDraw); //Compile Shaders string vvs, ffs; int vertex_shader_ob, fragment_shader_ob; vvs = GLSL_Preprocessor.Parser("Shaders/Text_VS.glsl"); ffs = GLSL_Preprocessor.Parser("Shaders/Text_FS.glsl"); //Compile Texture Shaders GLShaderHelper.CreateShaders(vvs, ffs, out vertex_shader_ob, out fragment_shader_ob, out shader_program); //Setup default program GL.UseProgram(shader_program); //Load Sample texture //Test BMP Image Class BMPImage bm = new BMPImage("courier.bmp"); sampleTex = bm.GLid; glControl1.Invalidate(); }
public void handleRequests() { if (reqHandler.hasOpenRequests()) { ThreadRequest req = reqHandler.Fetch(); lock (req) { switch (req.type) { case THREAD_REQUEST_TYPE.QUERY_GLCONTROL_STATUS_REQUEST: //At this point the renderer is up and running req.status = THREAD_REQUEST_STATUS.FINISHED; break; case THREAD_REQUEST_TYPE.INIT_RESOURCE_MANAGER: resMgr.Init(); req.status = THREAD_REQUEST_STATUS.FINISHED; break; case THREAD_REQUEST_TYPE.NEW_SCENE_REQUEST: inputPollTimer.Stop(); rt_addRootScene((string)req.arguments[0]); req.status = THREAD_REQUEST_STATUS.FINISHED; inputPollTimer.Start(); break; #if DEBUG case THREAD_REQUEST_TYPE.NEW_TEST_SCENE_REQUEST: inputPollTimer.Stop(); rt_addTestScene((int)req.arguments[0]); req.status = THREAD_REQUEST_STATUS.FINISHED; inputPollTimer.Start(); break; #endif case THREAD_REQUEST_TYPE.CHANGE_MODEL_PARENT_REQUEST: Model source = (Model)req.arguments[0]; Model target = (Model)req.arguments[1]; System.Windows.Application.Current.Dispatcher.Invoke((System.Action)(() => { if (source.parent != null) { source.parent.Children.Remove(source); } //Add to target node source.parent = target; target.Children.Add(source); })); req.status = THREAD_REQUEST_STATUS.FINISHED; break; case THREAD_REQUEST_TYPE.UPDATE_SCENE_REQUEST: Scene req_scn = (Scene)req.arguments[0]; req_scn.update(); req.status = THREAD_REQUEST_STATUS.FINISHED; break; case THREAD_REQUEST_TYPE.GL_COMPILE_ALL_SHADERS_REQUEST: resMgr.compileMainShaders(); req.status = THREAD_REQUEST_STATUS.FINISHED; break; case THREAD_REQUEST_TYPE.MOUSEPOSITION_INFO_REQUEST: Vector4[] t = (Vector4[])req.arguments[2]; renderMgr.getMousePosInfo((int)req.arguments[0], (int)req.arguments[1], ref t); req.status = THREAD_REQUEST_STATUS.FINISHED; break; case THREAD_REQUEST_TYPE.GL_RESIZE_REQUEST: rt_ResizeViewport((int)req.arguments[0], (int)req.arguments[1]); req.status = THREAD_REQUEST_STATUS.FINISHED; break; case THREAD_REQUEST_TYPE.GL_MODIFY_SHADER_REQUEST: GLShaderHelper.modifyShader((GLSLShaderConfig)req.arguments[0], (GLSLShaderText)req.arguments[1]); req.status = THREAD_REQUEST_STATUS.FINISHED; break; case THREAD_REQUEST_TYPE.GIZMO_PICKING_REQUEST: //TODO: Send the nessessary arguments to the render manager and mark the active gizmoparts Gizmo g = (Gizmo)req.arguments[0]; renderMgr.gizmoPick(ref g, (Vector2)req.arguments[1]); req.status = THREAD_REQUEST_STATUS.FINISHED; break; case THREAD_REQUEST_TYPE.TERMINATE_REQUEST: rt_State = EngineRenderingState.EXIT; inputPollTimer.Stop(); req.status = THREAD_REQUEST_STATUS.FINISHED; break; case THREAD_REQUEST_TYPE.GL_PAUSE_RENDER_REQUEST: rt_State = EngineRenderingState.PAUSED; req.status = THREAD_REQUEST_STATUS.FINISHED; break; case THREAD_REQUEST_TYPE.GL_RESUME_RENDER_REQUEST: rt_State = EngineRenderingState.ACTIVE; req.status = THREAD_REQUEST_STATUS.FINISHED; break; case THREAD_REQUEST_TYPE.NULL: break; } } } }
private void compileMaterialShader() { Dictionary <int, GLSLShaderConfig> shaderDict; Dictionary <int, List <GLMeshVao> > meshList; List <string> includes = new List <string>(); List <string> defines = new List <string>(); //Save shader to resource Manager //Check for explicit materials if (Name == "collisionMat" || Name == "jointMat" || Name == "crossMat") { shaderDict = Common.RenderState.activeResMgr.GLDefaultShaderMap; meshList = Common.RenderState.activeResMgr.defaultMeshShaderMap; defines.Add("_D_DEFERRED_RENDERING"); } else if (MaterialFlags.Contains("_F51_DECAL_DIFFUSE") || MaterialFlags.Contains("_F52_DECAL_NORMAL")) { shaderDict = Common.RenderState.activeResMgr.GLDeferredShaderMapDecal; meshList = Common.RenderState.activeResMgr.decalMeshShaderMap; defines.Add("_D_DEFERRED_RENDERING"); } else if (MaterialFlags.Contains("_F09_TRANSPARENT") || MaterialFlags.Contains("_F22_TRANSPARENT_SCALAR") || MaterialFlags.Contains("_F11_ALPHACUTOUT")) { shaderDict = Common.RenderState.activeResMgr.GLForwardShaderMapTransparent; meshList = Common.RenderState.activeResMgr.transparentMeshShaderMap; } else if (MaterialFlags.Contains("_F07_UNLIT")) { shaderDict = Common.RenderState.activeResMgr.GLDeferredUNLITShaderMap; meshList = Common.RenderState.activeResMgr.opaqueMeshShaderMap; defines.Add("_D_DEFERRED_RENDERING"); } else { shaderDict = Common.RenderState.activeResMgr.GLDeferredLITShaderMap; meshList = Common.RenderState.activeResMgr.opaqueMeshShaderMap; defines.Add("_D_DEFERRED_RENDERING"); } for (int i = 0; i < MaterialFlags.Count; i++) { if (supported_flags.Contains(MaterialFlags[i])) { includes.Add(MaterialFlags[i]); } } GLSLShaderConfig shader = GLShaderHelper.compileShader("Shaders/Simple_VS.glsl", "Shaders/Simple_FS.glsl", null, null, null, defines, includes, SHADER_TYPE.MATERIAL_SHADER, ref Common.RenderState.shaderCompilationLog); //Attach UBO binding Points GLShaderHelper.attachUBOToShaderBindingPoint(shader, "_COMMON_PER_FRAME", 0); GLShaderHelper.attachSSBOToShaderBindingPoint(shader, "_COMMON_PER_MESH", 1); //Save shader to the resource Manager shaderDict[shader.shaderHash] = shader; meshList[shader.shaderHash] = new List <GLMeshVao>(); //Init list }
public void init() { //Get MaterialFlags foreach (TkMaterialFlags f in Flags) { material_flags[(int)f.MaterialFlag] = 1.0f; } //Get Uniforms foreach (TkMaterialUniform un in Uniforms) { Uniform my_un = new Uniform("mpCustomPerMaterial.", un); CustomPerMaterialUniforms[my_un.Name] = my_un; } //Get Samplers foreach (TkMaterialSampler sm in Samplers) { Sampler s = new Sampler(sm); s.init(texMgr); PSamplers[s.PName] = s; } //Workaround for Procedurally Generated Samplers //I need to check if the diffuse sampler is procgen and then force the maps //on the other samplers with the appropriate names foreach (Sampler s in PSamplers.Values) { //Check if the first sampler is procgen if (s.isProcGen) { string name = s.Map; //Properly assemble the mask and the normal map names string[] split = name.Split('.'); string pre_ext_name = ""; for (int i = 0; i < split.Length - 1; i++) { pre_ext_name += split[i] + '.'; } if (PSamplers.ContainsKey("mpCustomPerMaterial.gMasksMap")) { string new_name = pre_ext_name + "MASKS.DDS"; PSamplers["mpCustomPerMaterial.gMasksMap"].PMap = new_name; PSamplers["mpCustomPerMaterial.gMasksMap"].tex = PSamplers["mpCustomPerMaterial.gMasksMap"].texMgr.getTexture(new_name); } if (PSamplers.ContainsKey("mpCustomPerMaterial.gNormalMap")) { string new_name = pre_ext_name + "NORMAL.DDS"; PSamplers["mpCustomPerMaterial.gNormalMap"].PMap = new_name; PSamplers["mpCustomPerMaterial.gNormalMap"].tex = PSamplers["mpCustomPerMaterial.gNormalMap"].texMgr.getTexture(new_name); } break; } } //Calculate material hash List <string> includes = new List <string>(); for (int i = 0; i < MaterialFlags.Count; i++) { if (supported_flags.Contains(MaterialFlags[i])) { includes.Add(MaterialFlags[i]); } } shaderHash = GLShaderHelper.calculateShaderHash(includes); if (!Common.RenderState.activeResMgr.shaderExistsForMaterial(this)) { try { compileMaterialShader(); } catch (Exception e) { Common.CallBacks.Log("Error during material shader compilation: " + e.Message); } } }
public void compileMainShaders() { #if (DEBUG) //Query GL Extensions Console.WriteLine("OPENGL AVAILABLE EXTENSIONS:"); string[] ext = GL.GetString(StringNameIndexed.Extensions, 0).Split(' '); foreach (string s in ext) { if (s.Contains("explicit")) { Console.WriteLine(s); } if (s.Contains("texture")) { Console.WriteLine(s); } if (s.Contains("16")) { Console.WriteLine(s); } } //Query maximum buffer sizes Console.WriteLine("MaxUniformBlock Size {0}", GL.GetInteger(GetPName.MaxUniformBlockSize)); #endif //Populate shader list string log = ""; GLSLHelper.GLSLShaderConfig shader_conf; //Geometry Shader //Compile Object Shaders GLSLShaderText geometry_shader_vs = new GLSLShaderText(ShaderType.VertexShader); GLSLShaderText geometry_shader_fs = new GLSLShaderText(ShaderType.FragmentShader); GLSLShaderText geometry_shader_gs = new GLSLShaderText(ShaderType.GeometryShader); geometry_shader_vs.addStringFromFile("Shaders/Simple_VSEmpty.glsl"); geometry_shader_fs.addStringFromFile("Shaders/Simple_FSEmpty.glsl"); geometry_shader_gs.addStringFromFile("Shaders/Simple_GS.glsl"); GLShaderHelper.compileShader(geometry_shader_vs, geometry_shader_fs, geometry_shader_gs, null, null, SHADER_TYPE.DEBUG_MESH_SHADER, ref log); //Compile Object Shaders GLSLShaderText gizmo_shader_vs = new GLSLShaderText(ShaderType.VertexShader); GLSLShaderText gizmo_shader_fs = new GLSLShaderText(ShaderType.FragmentShader); gizmo_shader_vs.addStringFromFile("Shaders/Gizmo_VS.glsl"); gizmo_shader_fs.addStringFromFile("Shaders/Gizmo_FS.glsl"); shader_conf = GLShaderHelper.compileShader(gizmo_shader_vs, gizmo_shader_fs, null, null, null, SHADER_TYPE.GIZMO_SHADER, ref log); //Attach UBO binding Points GLShaderHelper.attachUBOToShaderBindingPoint(shader_conf, "_COMMON_PER_FRAME", 0); GLShaders[SHADER_TYPE.GIZMO_SHADER] = shader_conf; #if DEBUG //Report UBOs GLShaderHelper.reportUBOs(shader_conf); #endif //Picking Shader //Compile Default Shaders //BoundBox Shader GLSLShaderText bbox_shader_vs = new GLSLShaderText(ShaderType.VertexShader); GLSLShaderText bbox_shader_fs = new GLSLShaderText(ShaderType.FragmentShader); bbox_shader_vs.addStringFromFile("Shaders/Bound_VS.glsl"); bbox_shader_fs.addStringFromFile("Shaders/Bound_FS.glsl"); GLShaderHelper.compileShader(bbox_shader_vs, bbox_shader_fs, null, null, null, GLSLHelper.SHADER_TYPE.BBOX_SHADER, ref log); //Texture Mixing Shader GLSLShaderText texture_mixing_shader_vs = new GLSLShaderText(ShaderType.VertexShader); GLSLShaderText texture_mixing_shader_fs = new GLSLShaderText(ShaderType.FragmentShader); texture_mixing_shader_vs.addStringFromFile("Shaders/texture_mixer_VS.glsl"); texture_mixing_shader_fs.addStringFromFile("Shaders/texture_mixer_FS.glsl"); shader_conf = GLShaderHelper.compileShader(texture_mixing_shader_vs, texture_mixing_shader_fs, null, null, null, GLSLHelper.SHADER_TYPE.TEXTURE_MIX_SHADER, ref log); GLShaders[GLSLHelper.SHADER_TYPE.TEXTURE_MIX_SHADER] = shader_conf; //GBuffer Shaders //UNLIT GLSLShaderText gbuffer_shader_vs = new GLSLShaderText(ShaderType.VertexShader); GLSLShaderText gbuffer_shader_fs = new GLSLShaderText(ShaderType.FragmentShader); gbuffer_shader_vs.addStringFromFile("Shaders/Gbuffer_VS.glsl"); gbuffer_shader_fs.addStringFromFile("Shaders/Gbuffer_FS.glsl"); shader_conf = GLShaderHelper.compileShader(gbuffer_shader_vs, gbuffer_shader_fs, null, null, null, GLSLHelper.SHADER_TYPE.GBUFFER_UNLIT_SHADER, ref log); GLShaders[GLSLHelper.SHADER_TYPE.GBUFFER_UNLIT_SHADER] = shader_conf; //LIT gbuffer_shader_vs = new GLSLShaderText(ShaderType.VertexShader); gbuffer_shader_fs = new GLSLShaderText(ShaderType.FragmentShader); gbuffer_shader_vs.addStringFromFile("Shaders/Gbuffer_VS.glsl"); gbuffer_shader_fs.addString("#define _D_LIGHTING"); gbuffer_shader_fs.addStringFromFile("Shaders/Gbuffer_FS.glsl"); shader_conf = GLShaderHelper.compileShader(gbuffer_shader_vs, gbuffer_shader_fs, null, null, null, GLSLHelper.SHADER_TYPE.GBUFFER_LIT_SHADER, ref log); GLShaders[GLSLHelper.SHADER_TYPE.GBUFFER_LIT_SHADER] = shader_conf; //GAUSSIAN HORIZONTAL BLUR SHADER gbuffer_shader_vs = new GLSLShaderText(ShaderType.VertexShader); GLSLShaderText gaussian_blur_shader_fs = new GLSLShaderText(ShaderType.FragmentShader); gbuffer_shader_vs.addStringFromFile("Shaders/Gbuffer_VS.glsl"); gaussian_blur_shader_fs.addStringFromFile("Shaders/gaussian_horizontalBlur_FS.glsl"); shader_conf = GLShaderHelper.compileShader(gbuffer_shader_vs, gaussian_blur_shader_fs, null, null, null, GLSLHelper.SHADER_TYPE.GAUSSIAN_HORIZONTAL_BLUR_SHADER, ref log); GLShaders[GLSLHelper.SHADER_TYPE.GAUSSIAN_HORIZONTAL_BLUR_SHADER] = shader_conf; //GAUSSIAN VERTICAL BLUR SHADER gbuffer_shader_vs = new GLSLShaderText(ShaderType.VertexShader); gaussian_blur_shader_fs = new GLSLShaderText(ShaderType.FragmentShader); gbuffer_shader_vs.addStringFromFile("Shaders/Gbuffer_VS.glsl"); gaussian_blur_shader_fs.addStringFromFile("Shaders/gaussian_verticalBlur_FS.glsl"); shader_conf = GLShaderHelper.compileShader(gbuffer_shader_vs, gaussian_blur_shader_fs, null, null, null, GLSLHelper.SHADER_TYPE.GAUSSIAN_VERTICAL_BLUR_SHADER, ref log); GLShaders[GLSLHelper.SHADER_TYPE.GAUSSIAN_VERTICAL_BLUR_SHADER] = shader_conf; //BRIGHTNESS EXTRACTION SHADER gbuffer_shader_vs = new GLSLShaderText(ShaderType.VertexShader); gbuffer_shader_fs = new GLSLShaderText(ShaderType.FragmentShader); gbuffer_shader_vs.addStringFromFile("Shaders/Gbuffer_VS.glsl"); gbuffer_shader_fs.addStringFromFile("Shaders/brightness_extract_shader_fs.glsl"); shader_conf = GLShaderHelper.compileShader(gbuffer_shader_vs, gbuffer_shader_fs, null, null, null, GLSLHelper.SHADER_TYPE.BRIGHTNESS_EXTRACT_SHADER, ref log); GLShaders[GLSLHelper.SHADER_TYPE.BRIGHTNESS_EXTRACT_SHADER] = shader_conf; //ADDITIVE BLEND gbuffer_shader_vs = new GLSLShaderText(ShaderType.VertexShader); gbuffer_shader_fs = new GLSLShaderText(ShaderType.FragmentShader); gbuffer_shader_vs.addStringFromFile("Shaders/Gbuffer_VS.glsl"); gbuffer_shader_fs.addStringFromFile("Shaders/additive_blend_fs.glsl"); shader_conf = GLShaderHelper.compileShader(gbuffer_shader_vs, gbuffer_shader_fs, null, null, null, GLSLHelper.SHADER_TYPE.ADDITIVE_BLEND_SHADER, ref log); GLShaders[GLSLHelper.SHADER_TYPE.ADDITIVE_BLEND_SHADER] = shader_conf; //FXAA gbuffer_shader_vs = new GLSLShaderText(ShaderType.VertexShader); gbuffer_shader_fs = new GLSLShaderText(ShaderType.FragmentShader); gbuffer_shader_vs.addStringFromFile("Shaders/Gbuffer_VS.glsl"); gbuffer_shader_fs.addStringFromFile("Shaders/fxaa_shader_fs.glsl"); shader_conf = GLShaderHelper.compileShader(gbuffer_shader_vs, gbuffer_shader_fs, null, null, null, GLSLHelper.SHADER_TYPE.FXAA_SHADER, ref log); GLShaders[GLSLHelper.SHADER_TYPE.FXAA_SHADER] = shader_conf; //TONE MAPPING + GAMMA CORRECTION gbuffer_shader_vs = new GLSLShaderText(ShaderType.VertexShader); gbuffer_shader_fs = new GLSLShaderText(ShaderType.FragmentShader); gbuffer_shader_vs.addStringFromFile("Shaders/Gbuffer_VS.glsl"); gbuffer_shader_fs.addStringFromFile("Shaders/tone_mapping_fs.glsl"); shader_conf = GLShaderHelper.compileShader(gbuffer_shader_vs, gbuffer_shader_fs, null, null, null, GLSLHelper.SHADER_TYPE.TONE_MAPPING, ref log); GLShaders[GLSLHelper.SHADER_TYPE.TONE_MAPPING] = shader_conf; //INV TONE MAPPING + GAMMA CORRECTION gbuffer_shader_vs = new GLSLShaderText(ShaderType.VertexShader); gbuffer_shader_fs = new GLSLShaderText(ShaderType.FragmentShader); gbuffer_shader_vs.addStringFromFile("Shaders/Gbuffer_VS.glsl"); gbuffer_shader_fs.addStringFromFile("Shaders/inv_tone_mapping_fs.glsl"); shader_conf = GLShaderHelper.compileShader(gbuffer_shader_vs, gbuffer_shader_fs, null, null, null, SHADER_TYPE.INV_TONE_MAPPING, ref log); GLShaders[SHADER_TYPE.INV_TONE_MAPPING] = shader_conf; //BWOIT SHADER gbuffer_shader_vs = new GLSLShaderText(ShaderType.VertexShader); gbuffer_shader_fs = new GLSLShaderText(ShaderType.FragmentShader); gbuffer_shader_vs.addStringFromFile("Shaders/Gbuffer_VS.glsl"); gbuffer_shader_fs.addStringFromFile("Shaders/bwoit_shader_fs.glsl"); shader_conf = GLShaderHelper.compileShader(gbuffer_shader_vs, gbuffer_shader_fs, null, null, null, SHADER_TYPE.BWOIT_COMPOSITE_SHADER, ref log); GLShaders[SHADER_TYPE.BWOIT_COMPOSITE_SHADER] = shader_conf; //Text Shaders GLSLShaderText text_shader_vs = new GLSLShaderText(ShaderType.VertexShader); GLSLShaderText text_shader_fs = new GLSLShaderText(ShaderType.FragmentShader); text_shader_vs.addStringFromFile("Shaders/Text_VS.glsl"); text_shader_fs.addStringFromFile("Shaders/Text_FS.glsl"); shader_conf = GLShaderHelper.compileShader(text_shader_vs, text_shader_fs, null, null, null, SHADER_TYPE.TEXT_SHADER, ref log); GLShaders[SHADER_TYPE.TEXT_SHADER] = shader_conf; //Camera Shaders //TODO: Add Camera Shaders if required GLShaders[GLSLHelper.SHADER_TYPE.CAMERA_SHADER] = null; //FILTERS - EFFECTS //Pass Shader gbuffer_shader_vs = new GLSLShaderText(ShaderType.VertexShader); GLSLShaderText passthrough_shader_fs = new GLSLShaderText(ShaderType.FragmentShader); gbuffer_shader_vs.addStringFromFile("Shaders/Gbuffer_VS.glsl"); passthrough_shader_fs.addStringFromFile("Shaders/PassThrough_FS.glsl"); shader_conf = GLShaderHelper.compileShader(gbuffer_shader_vs, passthrough_shader_fs, null, null, null, SHADER_TYPE.PASSTHROUGH_SHADER, ref log); GLShaders[SHADER_TYPE.PASSTHROUGH_SHADER] = shader_conf; }