public void ResetBackbuffer( PresentationParameters presentationParameters, bool renderTargetBound ) { int winWidth, winHeight; GetWindowDimensions( presentationParameters, out winWidth, out winHeight ); bool useFauxBackbuffer = ( winWidth != presentationParameters.BackBufferWidth || winHeight != presentationParameters.BackBufferHeight || presentationParameters.MultiSampleCount > 0 ); if (useFauxBackbuffer) { if (Backbuffer is NullBackbuffer) { if (!supportsFauxBackbuffer) { throw new NoSuitableGraphicsDeviceException( "Your hardware does not support the faux-backbuffer!" + "\n\nKeep the window/backbuffer resolution the same." ); } Backbuffer = new OpenGLBackbuffer( this, presentationParameters.BackBufferWidth, presentationParameters.BackBufferHeight, presentationParameters.DepthStencilFormat, presentationParameters.MultiSampleCount ); } else { Backbuffer.ResetFramebuffer( presentationParameters, renderTargetBound ); } } else { if (Backbuffer is OpenGLBackbuffer) { (Backbuffer as OpenGLBackbuffer).Dispose(); Backbuffer = new NullBackbuffer( presentationParameters.BackBufferWidth, presentationParameters.BackBufferHeight ); } else { Backbuffer.ResetFramebuffer( presentationParameters, renderTargetBound ); } } }
public void ResetBackbuffer( PresentationParameters presentationParameters, bool renderTargetBound ) { Rectangle bounds = FNAPlatform.GetWindowBounds(presentationParameters.DeviceWindowHandle); bool useFauxBackbuffer = ( bounds.Width != presentationParameters.BackBufferWidth || bounds.Height != presentationParameters.BackBufferHeight || presentationParameters.MultiSampleCount > 0 ); if (useFauxBackbuffer) { if (Backbuffer is NullBackbuffer) { Backbuffer = new OpenGLBackbuffer( this, presentationParameters.BackBufferWidth, presentationParameters.BackBufferHeight, presentationParameters.DepthStencilFormat, presentationParameters.MultiSampleCount ); } else { Backbuffer.ResetFramebuffer( presentationParameters, renderTargetBound ); } } else { if (Backbuffer is OpenGLBackbuffer) { (Backbuffer as OpenGLBackbuffer).Dispose(); Backbuffer = new NullBackbuffer( presentationParameters.BackBufferWidth, presentationParameters.BackBufferHeight ); } else { Backbuffer.ResetFramebuffer( presentationParameters, renderTargetBound ); } } }
public OpenGLDevice( PresentationParameters presentationParameters ) { // Create OpenGL context glContext = SDL.SDL_GL_CreateContext( presentationParameters.DeviceWindowHandle ); // Check for a possible ES context int flags; int es2Flag = (int) SDL.SDL_GLprofile.SDL_GL_CONTEXT_PROFILE_ES; SDL.SDL_GL_GetAttribute(SDL.SDL_GLattr.SDL_GL_CONTEXT_PROFILE_MASK, out flags); useES2 = (flags & es2Flag) == es2Flag; // Check for a possible Core context int coreFlag = (int) SDL.SDL_GLprofile.SDL_GL_CONTEXT_PROFILE_CORE; useCoreProfile = (flags & coreFlag) == coreFlag; // Init threaded GL crap where applicable InitThreadedGL( presentationParameters.DeviceWindowHandle ); // Initialize entry points LoadGLEntryPoints(); shaderProfile = MojoShader.MOJOSHADER_glBestProfile( GLGetProcAddress, IntPtr.Zero, null, null, IntPtr.Zero ); shaderContext = MojoShader.MOJOSHADER_glCreateContext( shaderProfile, GLGetProcAddress, IntPtr.Zero, null, null, IntPtr.Zero ); MojoShader.MOJOSHADER_glMakeContextCurrent(shaderContext); // Print GL information FNAPlatform.Log("OpenGL Device: " + glGetString(GLenum.GL_RENDERER)); FNAPlatform.Log("OpenGL Driver: " + glGetString(GLenum.GL_VERSION)); FNAPlatform.Log("OpenGL Vendor: " + glGetString(GLenum.GL_VENDOR)); FNAPlatform.Log("MojoShader Profile: " + shaderProfile); // Load the extension list, initialize extension-dependent components string extensions; if (useCoreProfile) { extensions = string.Empty; int numExtensions; glGetIntegerv(GLenum.GL_NUM_EXTENSIONS, out numExtensions); for (uint i = 0; i < numExtensions; i += 1) { extensions += glGetStringi(GLenum.GL_EXTENSIONS, i) + " "; } } else { extensions = glGetString(GLenum.GL_EXTENSIONS); } SupportsS3tc = ( extensions.Contains("GL_EXT_texture_compression_s3tc") || extensions.Contains("GL_OES_texture_compression_S3TC") || extensions.Contains("GL_EXT_texture_compression_dxt3") || extensions.Contains("GL_EXT_texture_compression_dxt5") ); SupportsDxt1 = ( SupportsS3tc || extensions.Contains("GL_EXT_texture_compression_dxt1") ); /* Check the max multisample count, override parameters if necessary */ int maxSamples = 0; if (supportsMultisampling) { glGetIntegerv(GLenum.GL_MAX_SAMPLES, out maxSamples); } MaxMultiSampleCount = maxSamples; presentationParameters.MultiSampleCount = Math.Min( presentationParameters.MultiSampleCount, MaxMultiSampleCount ); // Initialize the faux-backbuffer int winWidth, winHeight; GetWindowDimensions( presentationParameters, out winWidth, out winHeight ); if ( winWidth != presentationParameters.BackBufferWidth || winHeight != presentationParameters.BackBufferHeight || presentationParameters.MultiSampleCount > 0 ) { if (!supportsFauxBackbuffer) { throw new NoSuitableGraphicsDeviceException( "Your hardware does not support the faux-backbuffer!" + "\n\nKeep the window/backbuffer resolution the same." ); } Backbuffer = new OpenGLBackbuffer( this, presentationParameters.BackBufferWidth, presentationParameters.BackBufferHeight, presentationParameters.DepthStencilFormat, presentationParameters.MultiSampleCount ); } else { Backbuffer = new NullBackbuffer( presentationParameters.BackBufferWidth, presentationParameters.BackBufferHeight ); } // Initialize texture collection array int numSamplers; glGetIntegerv(GLenum.GL_MAX_TEXTURE_IMAGE_UNITS, out numSamplers); Textures = new OpenGLTexture[numSamplers]; for (int i = 0; i < numSamplers; i += 1) { Textures[i] = OpenGLTexture.NullTexture; } MaxTextureSlots = numSamplers; // Initialize vertex attribute state arrays int numAttributes; glGetIntegerv(GLenum.GL_MAX_VERTEX_ATTRIBS, out numAttributes); attributes = new VertexAttribute[numAttributes]; attributeEnabled = new bool[numAttributes]; previousAttributeEnabled = new bool[numAttributes]; attributeDivisor = new int[numAttributes]; previousAttributeDivisor = new int[numAttributes]; for (int i = 0; i < numAttributes; i += 1) { attributes[i] = new VertexAttribute(); attributeEnabled[i] = false; previousAttributeEnabled[i] = false; attributeDivisor[i] = 0; previousAttributeDivisor[i] = 0; } // Initialize render target FBO and state arrays int numAttachments; glGetIntegerv(GLenum.GL_MAX_DRAW_BUFFERS, out numAttachments); currentAttachments = new uint[numAttachments]; currentAttachmentTypes = new GLenum[numAttachments]; drawBuffersArray = new GLenum[numAttachments]; for (int i = 0; i < numAttachments; i += 1) { currentAttachments[i] = 0; currentAttachmentTypes[i] = GLenum.GL_TEXTURE_2D; drawBuffersArray[i] = GLenum.GL_COLOR_ATTACHMENT0 + i; } currentDrawBuffers = 0; currentRenderbuffer = 0; currentDepthStencilFormat = DepthFormat.None; glGenFramebuffers(1, out targetFramebuffer); glGenFramebuffers(1, out resolveFramebufferRead); glGenFramebuffers(1, out resolveFramebufferDraw); // Generate and bind a VAO, to shut Core up if (useCoreProfile) { glGenVertexArrays(1, out vao); glBindVertexArray(vao); } }
public ModernGLDevice( PresentationParameters presentationParameters ) { // Create OpenGL context glContext = SDL.SDL_GL_CreateContext( presentationParameters.DeviceWindowHandle ); // Check for a possible Core context int flags; int coreFlag = (int) SDL.SDL_GLprofile.SDL_GL_CONTEXT_PROFILE_CORE; SDL.SDL_GL_GetAttribute(SDL.SDL_GLattr.SDL_GL_CONTEXT_PROFILE_MASK, out flags); useCoreProfile = (flags & coreFlag) == coreFlag; // Init threaded GL crap where applicable InitThreadedGL( presentationParameters.DeviceWindowHandle ); // Initialize entry points LoadGLEntryPoints(); shaderProfile = MojoShader.MOJOSHADER_glBestProfile( GLGetProcAddress, IntPtr.Zero, null, null, IntPtr.Zero ); shaderContext = MojoShader.MOJOSHADER_glCreateContext( shaderProfile, GLGetProcAddress, IntPtr.Zero, null, null, IntPtr.Zero ); MojoShader.MOJOSHADER_glMakeContextCurrent(shaderContext); // Print GL information FNALoggerEXT.LogInfo("IGLDevice: ModernGLDevice"); FNALoggerEXT.LogInfo("OpenGL Device: " + glGetString(GLenum.GL_RENDERER)); FNALoggerEXT.LogInfo("OpenGL Driver: " + glGetString(GLenum.GL_VERSION)); FNALoggerEXT.LogInfo("OpenGL Vendor: " + glGetString(GLenum.GL_VENDOR)); FNALoggerEXT.LogInfo("MojoShader Profile: " + shaderProfile); // Load the extension list, initialize extension-dependent components string extensions; if (useCoreProfile) { extensions = string.Empty; int numExtensions; glGetIntegerv(GLenum.GL_NUM_EXTENSIONS, out numExtensions); for (uint i = 0; i < numExtensions; i += 1) { extensions += glGetStringi(GLenum.GL_EXTENSIONS, i) + " "; } } else { extensions = glGetString(GLenum.GL_EXTENSIONS); } SupportsS3tc = ( extensions.Contains("GL_EXT_texture_compression_s3tc") || extensions.Contains("GL_OES_texture_compression_S3TC") || extensions.Contains("GL_EXT_texture_compression_dxt3") || extensions.Contains("GL_EXT_texture_compression_dxt5") ); SupportsDxt1 = ( SupportsS3tc || extensions.Contains("GL_EXT_texture_compression_dxt1") ); SupportsHardwareInstancing = true; /* Check the max multisample count, override parameters if necessary */ int maxSamples = 0; glGetIntegerv(GLenum.GL_MAX_SAMPLES, out maxSamples); MaxMultiSampleCount = maxSamples; presentationParameters.MultiSampleCount = Math.Min( presentationParameters.MultiSampleCount, MaxMultiSampleCount ); // Initialize the faux-backbuffer Rectangle bounds = FNAPlatform.GetWindowBounds(presentationParameters.DeviceWindowHandle); if ( bounds.Width != presentationParameters.BackBufferWidth || bounds.Height != presentationParameters.BackBufferHeight || presentationParameters.MultiSampleCount > 0 ) { Backbuffer = new OpenGLBackbuffer( this, presentationParameters.BackBufferWidth, presentationParameters.BackBufferHeight, presentationParameters.DepthStencilFormat, presentationParameters.MultiSampleCount ); } else { Backbuffer = new NullBackbuffer( presentationParameters.BackBufferWidth, presentationParameters.BackBufferHeight ); } // Initialize texture collection array int numSamplers; glGetIntegerv(GLenum.GL_MAX_TEXTURE_IMAGE_UNITS, out numSamplers); Textures = new OpenGLTexture[numSamplers]; Samplers = new uint[numSamplers]; SamplersU = new TextureAddressMode[numSamplers]; SamplersV = new TextureAddressMode[numSamplers]; SamplersW = new TextureAddressMode[numSamplers]; SamplersFilter = new TextureFilter[numSamplers]; SamplersAnisotropy = new float[numSamplers]; SamplersMaxLevel = new int[numSamplers]; SamplersLODBias = new float[numSamplers]; SamplersMipped = new bool[numSamplers]; glCreateSamplers(numSamplers, Samplers); for (int i = 0; i < numSamplers; i += 1) { Textures[i] = OpenGLTexture.NullTexture; SamplersU[i] = TextureAddressMode.Wrap; SamplersV[i] = TextureAddressMode.Wrap; SamplersW[i] = TextureAddressMode.Wrap; SamplersFilter[i] = TextureFilter.Linear; SamplersAnisotropy[i] = 4.0f; SamplersMaxLevel[i] = 0; SamplersLODBias[i] = 0.0f; SamplersMipped[i] = false; glBindSampler(i, Samplers[i]); } MaxTextureSlots = numSamplers; // Initialize vertex attribute state arrays int numAttributes; glGetIntegerv(GLenum.GL_MAX_VERTEX_ATTRIBS, out numAttributes); attributes = new VertexAttribute[numAttributes]; attributeEnabled = new bool[numAttributes]; previousAttributeEnabled = new bool[numAttributes]; attributeDivisor = new int[numAttributes]; previousAttributeDivisor = new int[numAttributes]; for (int i = 0; i < numAttributes; i += 1) { attributes[i] = new VertexAttribute(); attributeEnabled[i] = false; previousAttributeEnabled[i] = false; attributeDivisor[i] = 0; previousAttributeDivisor[i] = 0; } // Initialize render target FBO and state arrays int numAttachments; glGetIntegerv(GLenum.GL_MAX_DRAW_BUFFERS, out numAttachments); currentAttachments = new uint[numAttachments]; currentAttachmentTypes = new GLenum[numAttachments]; drawBuffersArray = new GLenum[numAttachments]; for (int i = 0; i < numAttachments; i += 1) { currentAttachments[i] = 0; currentAttachmentTypes[i] = GLenum.GL_TEXTURE_2D; drawBuffersArray[i] = GLenum.GL_COLOR_ATTACHMENT0 + i; } currentDrawBuffers = 0; currentRenderbuffer = 0; currentDepthStencilFormat = DepthFormat.None; glCreateFramebuffers(1, out targetFramebuffer); glCreateFramebuffers(1, out resolveFramebufferRead); glCreateFramebuffers(1, out resolveFramebufferDraw); // Generate and bind a VAO, to shut Core up if (useCoreProfile) { glGenVertexArrays(1, out vao); glBindVertexArray(vao); } }