void glControl_Load(object sender, EventArgs e) { try { GL.ShadeModel(ShadingModel.Smooth); GL.ClearColor(0f, 0f, 0f, 0f); //GL.LightModel(LightModelParameter.LightModelAmbient, new float[] { 0.5f, 0.5f, 0.5f, 1.0f }); GL.Enable(EnableCap.Lighting); GL.Enable(EnableCap.Light0); GL.Light(LightName.Light0, LightParameter.Ambient, new float[] { 0.5f, 0.5f, 0.5f, 1f }); GL.Light(LightName.Light0, LightParameter.Diffuse, new float[] { 0.3f, 0.3f, 0.3f, 1f }); GL.Light(LightName.Light0, LightParameter.Specular, new float[] { 0.8f, 0.8f, 0.8f, 1.0f }); GL.Light(LightName.Light0, LightParameter.Position, lightPos); GL.ClearDepth(1.0d); GL.Enable(EnableCap.DepthTest); GL.Enable(EnableCap.ColorMaterial); GL.Enable(EnableCap.CullFace); GL.CullFace(CullFaceMode.Back); GL.ColorMaterial(MaterialFace.Front, ColorMaterialParameter.AmbientAndDiffuse); GL.ColorMaterial(MaterialFace.Front, ColorMaterialParameter.Specular); GL.DepthMask(true); GL.DepthFunc(DepthFunction.Lequal); GL.Hint(HintTarget.PerspectiveCorrectionHint, HintMode.Nicest); GL.MatrixMode(MatrixMode.Projection); GL.Enable(EnableCap.Blend); GL.AlphaFunc(AlphaFunction.Greater, 0.5f); GL.BlendFunc(BlendingFactor.SrcAlpha, BlendingFactor.OneMinusSrcAlpha); #region Compatibility checks OpenTK.Graphics.IGraphicsContextInternal context = glControl.Context as OpenTK.Graphics.IGraphicsContextInternal; string glExtensions = GL.GetString(StringName.Extensions); // VBO RenderSettings.ARBVBOPresent = context.GetAddress("glGenBuffersARB") != IntPtr.Zero; RenderSettings.CoreVBOPresent = context.GetAddress("glGenBuffers") != IntPtr.Zero; RenderSettings.UseVBO = (RenderSettings.ARBVBOPresent || RenderSettings.CoreVBOPresent) && instance.GlobalSettings["rendering_use_vbo"]; // Occlusion Query RenderSettings.ARBQuerySupported = context.GetAddress("glGetQueryObjectivARB") != IntPtr.Zero; RenderSettings.CoreQuerySupported = context.GetAddress("glGetQueryObjectiv") != IntPtr.Zero; RenderSettings.OcclusionCullingEnabled = (RenderSettings.CoreQuerySupported || RenderSettings.ARBQuerySupported) && instance.GlobalSettings["rendering_occlusion_culling_enabled2"]; // Mipmap RenderSettings.HasMipmap = context.GetAddress("glGenerateMipmap") != IntPtr.Zero; // Shader support RenderSettings.HasShaders = glExtensions.Contains("vertex_shader") && glExtensions.Contains("fragment_shader"); // Multi texture RenderSettings.HasMultiTexturing = context.GetAddress("glMultiTexCoord2f") != IntPtr.Zero; RenderSettings.WaterReflections = instance.GlobalSettings["water_reflections"]; if (!RenderSettings.HasMultiTexturing || !RenderSettings.HasShaders) { RenderSettings.WaterReflections = false; } // Do textures have to have dimensions that are powers of two RenderSettings.TextureNonPowerOfTwoSupported = glExtensions.Contains("texture_non_power_of_two"); // Occlusion culling RenderSettings.OcclusionCullingEnabled = Instance.GlobalSettings["rendering_occlusion_culling_enabled2"] && (RenderSettings.ARBQuerySupported || RenderSettings.CoreQuerySupported); // Shiny RenderSettings.EnableShiny = Instance.GlobalSettings["scene_viewer_shiny"]; #endregion Compatibility checks RenderingEnabled = true; // Call the resizing function which sets up the GL drawing window // and will also invalidate the GL control glControl_Resize(null, null); var textureThread = new Thread(TextureThread) { IsBackground = true, Name = "TextureLoadingThread" }; textureThread.Start(); } catch (Exception ex) { RenderingEnabled = false; Logger.Log("Failed to initialize OpenGL control", Helpers.LogLevel.Warning, Client, ex); } }
internal OpenCLFractalEngine(OpenTK.Graphics.IGraphicsContext graphicsContext, Platform platform, Device device) { if (graphicsContext == null || !(graphicsContext is OpenTK.Graphics.IGraphicsContextInternal)) { throw new ArgumentException("Invalid graphics context for OpenCLFractalEngine.", "graphicsContext"); } if (platform == null) { throw new ArgumentException("Invalid platform for OpenCLFractalEngine", "platform"); } if (device == null) { throw new ArgumentException("Invalid device for OpenCLFractalEngine", "device"); } this.platform = platform; this.device = device; useLowProfile = !device.ImageSupport; IntPtr curDC = wglGetCurrentDC(); OpenTK.Graphics.IGraphicsContextInternal gCtx = (OpenTK.Graphics.IGraphicsContextInternal)graphicsContext; context = OpenCL.Context.Create(new Device[] { device }, new ContextParam(ContextProperties.Platform, platform), new ContextParam(ContextProperties.GlContext, gCtx.Context.Handle), new ContextParam(ContextProperties.WglHdc, curDC)); iterBlockCount = Util.Clamp((int)device.MaxComputeUnits * 2, 2, 64); string source = null; if (useLowProfile) { source = Kernels.KernelResources.kernels_low_cl; } else { source = Kernels.KernelResources.kernels_cl; } string opts = ""; try{ program = OpenCL.Program.CreateFromSource(context, new string[] { source }); program.Build(new Device[] { device }, opts); } catch (OpenCLCallFailedException ex) { if (ex.ErrorCode == OpenCL.ErrorCode.BuildProgramFailure) { ex.Data.Add("build_log", program.GetBuildLog(device)); } throw ex; } initIteratorsKernel = Kernel.Create(program, "init_iterators_kernel"); resetIteratorsKernel = Kernel.Create(program, "reset_iterators_kernel"); iterateKernel = Kernel.Create(program, "iterate_kernel"); updateStatsKernel = Kernel.Create(program, "update_stats_kernel"); resetOutputKernel = Kernel.Create(program, "reset_output_kernel"); updateOutputKernel = Kernel.Create(program, "update_output_kernel"); glOutputBufferID = 0; queue = CommandQueue.Create(context, device, CommandQueueFlags.ProfilingEnable); fractalBuffer = context.CreateBuffer(MemFlags.ReadOnly, Marshal.SizeOf(typeof(NativeFractal))); branchBuffer = context.CreateBuffer(MemFlags.ReadOnly, Marshal.SizeOf(typeof(NativeBranch)) * NativeFractal.MaxBranches); variWeightBuffer = context.CreateBuffer(MemFlags.ReadOnly, 4 * NativeFractal.MaxBranches * NativeFractal.MaxVariations); iterPosStateBuffer = context.CreateBuffer(MemFlags.ReadWrite, (8 * IteratorCount)); iterColorStateBuffer = context.CreateBuffer(MemFlags.ReadWrite, (8 * IteratorCount)); iterStatBuffer = context.CreateBuffer(MemFlags.ReadWrite, Marshal.SizeOf(typeof(NativeIterStatEntry)) * IteratorCount); globalStatBuffer = context.CreateBuffer(MemFlags.ReadWrite, Marshal.SizeOf(typeof(NativeGlobalStatEntry))); entropyXBuffer = context.CreateBuffer(MemFlags.ReadWrite, (16 * IteratorCount)); entropyCBuffer = context.CreateBuffer(MemFlags.ReadWrite, (4 * IteratorCount)); entropySeedBuffer = context.CreateBuffer(MemFlags.ReadWrite, (4 * IteratorCount)); uint[] seeds = new uint[IteratorCount]; for (int i = 0; i < IteratorCount; i++) { seeds[i] = (uint)rand.Next(65536); } entropySeedBuffer.Write(queue, seeds); paletteSize = new Size(0, 0); paletteImage = null; paletteBuffer = null; if (!useLowProfile) { paletteSampler = Sampler.Create(context, true, AddressingMode.ClampToEdge, FilterMode.Linear); } initIteratorsKernel.SetArgs(entropyXBuffer, entropyCBuffer, entropySeedBuffer); Event initEvt; initIteratorsKernel.EnqueueLaunch(queue, IteratorCount, IterGroupSize, null, out initEvt); initEvt.Wait(); disposeAndNullify(ref initEvt); queue.Finish(); }