/// <summary> /// Initialize predefined textures<para/> /// Инициализация предзаданных текстур /// </summary> public static void Init() { // Checking for extensions // Проверка расширений CompressionSupported = GLExtensions.Supported("GL_EXT_Texture_Compression_S3TC"); FrameBufferSupported = GLExtensions.Supported("GL_ARB_Framebuffer_Object"); // Load DAT-defined texture dictionaries // Загрузка предопределенных архивов текстур foreach (string TXD in FileManager.TextureFiles) { string name = Path.GetFileNameWithoutExtension(TXD).ToLower(); string path = PathManager.GetAbsolute(TXD); TextureFile f = new TextureFile(path, true); CachedFiles.TryAdd(name, f); TextureDictionary td = new TextureDictionary(f, true, true); Cached.TryAdd(name, td); } // Starting TextureFile thread // Запуск потока чтения TextureFile fileReaderThread = new Thread(FileLoaderProcess); fileReaderThread.IsBackground = true; fileReaderThread.Priority = ThreadPriority.BelowNormal; fileReaderThread.Start(); // Starting model builder thread // Запуск потока постройки Model textureBuilderThread = new Thread(TextureBuilderProcess); textureBuilderThread.IsBackground = true; textureBuilderThread.Priority = ThreadPriority.BelowNormal; textureBuilderThread.Start(); }
/// <summary> /// Get a valid image size according to hardware support. /// </summary> /// <param name="size">The size to convert.</param> /// <returns>Valid nearest size (greater than or equal to specified size).</returns> internal static int GetValidSize(int size) { if (GLExtensions.IsAvailable("GL_ARB_texture_non_power_of_two")) { return(size); } else { // If hardware doesn't support NPOT textures, we calculate the nearest power of two // However, we just need to return the size if the size is POT to prevent performance issue if (size % 2 == 0) { return(size); } int powerOfTwo = 1; while (powerOfTwo < size) { powerOfTwo *= 2; } return(powerOfTwo); } }
/// <summary> /// Returns the currently used GPU memory in bytes. Might not work in all platforms! /// </summary> private int GetGPUMemory() { if (GLExtensions.DeviceSupportsOpenGLExtension("GL_NVX_gpu_memory_info")) { // this returns in Kb, Nvidia only extension int dedicated; Gl.Get(Gl.GPU_MEMORY_INFO_DEDICATED_VIDMEM_NVX, out dedicated); int available; Gl.Get(Gl.GPU_MEMORY_INFO_CURRENT_AVAILABLE_VIDMEM_NVX, out available); return((dedicated - available) / 1024); } return(0); }
/// <summary> /// Generates a MipMap using the current <see cref="Texture"/> data. /// </summary> /// <returns></returns> public void GenerateMipMap() { if (!GLExtensions.IsAvailable("GL_EXT_framebuffer_object")) { return; } GLChecker.Check(() => GL.BindTexture(TextureTarget.Texture2D, _textureId)); GLChecker.Check(() => GL.GenerateMipmap(GenerateMipmapTarget.Texture2D)); GLChecker.Check(() => GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)(_isSmooth ? TextureMinFilter.LinearMipmapLinear : TextureMinFilter.NearestMipmapLinear))); _hasMipmap = true; return; }
private void ApplyBlendMode(BlendMode mode) { if (GLExtensions.IsAvailable("GL_EXT_blend_func_separate")) { GLChecker.Check(() => GL.BlendFuncSeparate((BlendingFactorSrc)FactorToGL(mode.ColorSrcFactor), (BlendingFactorDest)FactorToGL(mode.ColorDstFactor), (BlendingFactorSrc)FactorToGL(mode.AlphaSrcFactor), (BlendingFactorDest)FactorToGL(mode.AlphaDstFactor)) ); } else { var src = (BlendingFactorSrc)FactorToGL(mode.ColorSrcFactor); var dst = (BlendingFactorDest)FactorToGL(mode.ColorDstFactor); GLChecker.Check(() => GL.BlendFunc(src, dst) ); } if (GLExtensions.IsAvailable("GL_EXT_blend_minmax") && GLExtensions.IsAvailable("GL_EXT_blend_subtract")) { if (GLExtensions.IsAvailable("GL_EXT_blend_equation_separate")) { GLChecker.Check(() => GL.Ext.BlendEquationSeparate( (BlendEquationModeExt)EquationToGL(mode.ColorEquation), (BlendEquationModeExt)EquationToGL(mode.AlphaEquation)) ); } else { GLChecker.Check(() => GL.Ext.BlendEquation(EquationToGL(mode.ColorEquation)) ); } } else if ((mode.ColorEquation != BlendMode.Equation.Add) || (mode.AlphaEquation != BlendMode.Equation.Add)) { if (!_isBlendingWarned) { Logger.Warning("OpenGL extension EXT_blend_minmax and / or EXT_blend_subtract unavailable.\n" + "Selecting a blend equation is not possible\n" + "Ensure that hardware acceleration is enabled if available."); _isBlendingWarned = true; } } Cache.LastBlendMode = mode; }
/// <summary> /// Renders the connector /// </summary> /// <param name="offset">Node editor pan offset</param> public virtual void Draw(Vector2 offset) { ForEachConnection(offset, (node1, node1Rect, node2, Node2Rect) => { Color color = Color.white; if (IsSelected()) { color = NodeEditor.DefaultSelectionColor; } GLExtensions.DrawGUILineZDepthOff(node1Rect.center, Node2Rect.center, color); return(true); }); }
private void Create(int width, int height, bool srgb) { // Check whether the specified size is valid if (width == 0 || height == 0) { throw new ArgumentException("Failed to create texture." + "invalid size (" + width.ToString() + "x" + height.ToString() + ")"); } // Compute the internal texture dimensions depending on NPOT textures support var actualSize = new Size(GetValidSize(width), GetValidSize(height)); // Check the maximum texture size int maxSize = GetMaximumSize(); if (actualSize.Width > maxSize || actualSize.Height >= maxSize) { throw new ArgumentException("Failed to create texture.\n" + "Texture size (" + width.ToString() + "x" + height.ToString() + ") " + "is exceeding the allowed size (" + maxSize.ToString() + "x" + maxSize.ToString() + ")."); } // All the validity checks passed, store the new texture settings _size = new Size(width, height); _actualSize = actualSize; _sRgb = srgb; _pixelsFlipped = false; _isFboAttachment = false; // Generate Texture Name (if not exist) if (_textureId <= 0) { GLChecker.Check(() => GL.GenTextures(1, out _textureId)); // Check whether the texture name has been generated properly. if (_textureId <= 0) { throw new AccessViolationException("Failed to create texture.\n" + "Check whether the graphic context is valid."); } } if (!_textureEdgeClampChecked) { _textureEdgeClamp = GLExtensions.IsAvailable("GL_EXT_texture_edge_clamp") || GLExtensions.IsAvailable("GL_texture_edge_clamp"); _textureEdgeClampChecked = true; if (!_isRepeated && !_textureEdgeClamp) { Logger.Warning("OpenGL extension SGIS_texture_edge_clamp unavailable\n" + "Artifacts may occur along texture edges\n" + "Ensure that hardware acceleration is enabled if available."); } } if (!_textureSrgbChecked) { _textureSrgb = GLExtensions.IsAvailable("GL_EXT_texture_sRGB"); } if (_sRgb && !_textureSrgb) { if (!_textureSrgbWarned) { _textureSrgbWarned = true; Logger.Warning("OpenGL extension EXT_texture_sRGB unavailable\n" + "Automatic sRGB to linear conversion disable."); } _sRgb = false; } // Initialize the texture GLChecker.Check(() => GL.BindTexture(TextureTarget.Texture2D, _textureId)); GLChecker.Check(() => GL.TexImage2D(TextureTarget.Texture2D, 0, (_sRgb ? PixelInternalFormat.Srgb8Alpha8 : PixelInternalFormat.Rgba), _actualSize.Width, _actualSize.Height, 0, OpenTK.Graphics.OpenGL.PixelFormat.Rgba, PixelType.UnsignedByte, IntPtr.Zero)); GLChecker.Check(() => GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)(_isRepeated ? TextureWrapMode.Repeat : (_textureEdgeClamp ? TextureWrapMode.ClampToEdge : TextureWrapMode.Clamp)))); GLChecker.Check(() => GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)(_isRepeated ? TextureWrapMode.Repeat : (_textureEdgeClamp ? TextureWrapMode.ClampToEdge : TextureWrapMode.Clamp)))); GLChecker.Check(() => GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)(_isSmooth ? TextureMagFilter.Linear : TextureMagFilter.Nearest))); GLChecker.Check(() => GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)(_isSmooth ? TextureMinFilter.Linear : TextureMinFilter.Nearest))); _cacheId = GetUniqueId(); _hasMipmap = false; }