[Mesa-dev] [PATCH 1/2] osmesa: add new OSMesaCreateContextAttribs function

Jose Fonseca jfonseca at vmware.com
Wed Dec 16 14:01:50 PST 2015


On 16/12/15 00:59, Brian Paul wrote:
> This allows specifying a GL profile and version so one can get a core-
> profile context.
> ---
>   docs/relnotes/11.2.0.html        |   2 +
>   include/GL/osmesa.h              |  45 ++++++++++++++++-
>   src/mesa/drivers/osmesa/osmesa.c | 104 ++++++++++++++++++++++++++++++++++++++-
>   3 files changed, 148 insertions(+), 3 deletions(-)
>
> diff --git a/docs/relnotes/11.2.0.html b/docs/relnotes/11.2.0.html
> index 12e0f07..e382856 100644
> --- a/docs/relnotes/11.2.0.html
> +++ b/docs/relnotes/11.2.0.html
> @@ -56,6 +56,8 @@ Note: some of the new features are only available with certain drivers.
>   <li>GL_ARB_vertex_type_10f_11f_11f_rev on freedreno/a4xx</li>
>   <li>GL_KHR_texture_compression_astc_ldr on freedreno/a4xx</li>
>   <li>GL_AMD_performance_monitor on radeonsi (CIK+ only)</li>
> +<li>New OSMesaCreateContextAttribs() function (for creating core profile
> +    contexts)</li>
>   </ul>
>
>   <h2>Bug fixes</h2>
> diff --git a/include/GL/osmesa.h b/include/GL/osmesa.h
> index ca0d167..39cd54e 100644
> --- a/include/GL/osmesa.h
> +++ b/include/GL/osmesa.h
> @@ -58,8 +58,8 @@ extern "C" {
>   #include <GL/gl.h>
>
>
> -#define OSMESA_MAJOR_VERSION 10
> -#define OSMESA_MINOR_VERSION 0
> +#define OSMESA_MAJOR_VERSION 11
> +#define OSMESA_MINOR_VERSION 2
>   #define OSMESA_PATCH_VERSION 0
>
>
> @@ -95,6 +95,18 @@ extern "C" {
>   #define OSMESA_MAX_WIDTH	0x24  /* new in 4.0 */
>   #define OSMESA_MAX_HEIGHT	0x25  /* new in 4.0 */
>
> +/*
> + * Accepted in OSMesaCreateContextAttrib's attribute list.
> + */
> +#define OSMESA_DEPTH_BITS            0x30
> +#define OSMESA_STENCIL_BITS          0x31
> +#define OSMESA_ACCUM_BITS            0x32
> +#define OSMESA_PROFILE               0x33
> +#define OSMESA_CORE_PROFILE          0x34
> +#define OSMESA_COMPAT_PROFILE        0x35
> +#define OSMESA_CONTEXT_MAJOR_VERSION 0x36
> +#define OSMESA_CONTEXT_MINOR_VERSION 0x37
> +
>
>   typedef struct osmesa_context *OSMesaContext;
>
> @@ -128,6 +140,35 @@ OSMesaCreateContextExt( GLenum format, GLint depthBits, GLint stencilBits,
>
>
>   /*
> + * Create an Off-Screen Mesa rendering context with attribute list.
> + * The list is composed of (attribute, value) pairs and terminated with
> + * attribute==0.  Supported Attributes:
> + *
> + * Attributes                    Values
> + * --------------------------------------------------------------------------
> + * OSMESA_FORMAT                 OSMESA_RGBA*, OSMESA_BGRA, OSMESA_ARGB, etc.
> + * OSMESA_DEPTH_BITS             0*, 16, 24, 32
> + * OSMESA_STENCIL_BITS           0*, 8
> + * OSMESA_ACCUM_BITS             0*, 16
> + * OSMESA_PROFILE                OSMESA_COMPAT_PROFILE*, OSMESA_CORE_PROFILE
> + * OSMESA_CONTEXT_MAJOR_VERSION  1*, 2, 3
> + * OSMESA_CONTEXT_MINOR_VERSION  0+
> + *
> + * Note: * = default value
> + *
> + * We return a context version >= what's specified by OSMESA_CONTEXT_MAJOR/
> + * MINOR_VERSION for the given profile.  For example, if you request a GL 1.4
> + * compat profile, you might get a GL 3.0 compat profile.
> + * Otherwise, null is returned if the version/profile is not supported.
> + *
> + * New in Mesa 11.2
> + */
> +GLAPI OSMesaContext GLAPIENTRY
> +OSMesaCreateContextAttribs( const int *attribList, OSMesaContext sharelist );
> +
> +
> +
> +/*
>    * Destroy an Off-Screen Mesa rendering context.
>    *
>    * Input:  ctx - the context to destroy
> diff --git a/src/mesa/drivers/osmesa/osmesa.c b/src/mesa/drivers/osmesa/osmesa.c
> index 5c7dcac..8f14dfd 100644
> --- a/src/mesa/drivers/osmesa/osmesa.c
> +++ b/src/mesa/drivers/osmesa/osmesa.c
> @@ -645,10 +645,104 @@ GLAPI OSMesaContext GLAPIENTRY
>   OSMesaCreateContextExt( GLenum format, GLint depthBits, GLint stencilBits,
>                           GLint accumBits, OSMesaContext sharelist )
>   {
> +   int attribs[100], n = 0;
> +
> +   attribs[n++] = OSMESA_FORMAT;
> +   attribs[n++] = format;
> +   attribs[n++] = OSMESA_DEPTH_BITS;
> +   attribs[n++] = depthBits;
> +   attribs[n++] = OSMESA_STENCIL_BITS;
> +   attribs[n++] = stencilBits;
> +   attribs[n++] = OSMESA_ACCUM_BITS;
> +   attribs[n++] = accumBits;
> +   attribs[n++] = 0;
> +
> +   return OSMesaCreateContextAttribs(attribs, sharelist);
> +}
> +
> +
> +/**
> + * New in Mesa 11.2
> + *
> + * Create context with attribute list.
> + */
> +GLAPI OSMesaContext GLAPIENTRY
> +OSMesaCreateContextAttribs(const int *attribList, OSMesaContext sharelist)
> +{
>      OSMesaContext osmesa;
>      struct dd_function_table functions;
>      GLint rind, gind, bind, aind;
>      GLint redBits = 0, greenBits = 0, blueBits = 0, alphaBits =0;
> +   GLenum format = OSMESA_RGBA;
> +   GLint depthBits = 0, stencilBits = 0, accumBits = 0;
> +   int profile = OSMESA_COMPAT_PROFILE, version_major = 1, version_minor = 0;
> +   gl_api api_profile = API_OPENGL_COMPAT;
> +   int i;
> +
> +   osmesa = (OSMesaContext) CALLOC_STRUCT(osmesa_context);
> +   if (!osmesa)
> +      return NULL;
>

It might be worth defer the calloc call till after the for loop so you 
don't need a free(osmesa) before every return NULL stmt.


> +   for (i = 0; attribList[i]; i += 2) {
> +      switch (attribList[i]) {
> +      case OSMESA_FORMAT:
> +         format = attribList[i+1];
> +         switch (format) {
> +         case OSMESA_COLOR_INDEX:
> +         case OSMESA_RGBA:
> +         case OSMESA_BGRA:
> +         case OSMESA_ARGB:
> +         case OSMESA_RGB:
> +         case OSMESA_BGR:
> +         case OSMESA_RGB_565:
> +            /* legal */
> +            break;
> +         default:
> +            return NULL;
> +         }
> +         break;
> +      case OSMESA_DEPTH_BITS:
> +         depthBits = attribList[i+1];
> +         if (depthBits < 0)
> +            return NULL;
> +         break;
> +      case OSMESA_STENCIL_BITS:
> +         stencilBits = attribList[i+1];
> +         if (stencilBits < 0)
> +            return NULL;
> +         break;
> +      case OSMESA_ACCUM_BITS:
> +         accumBits = attribList[i+1];
> +         if (accumBits < 0)
> +            return NULL;
> +         break;
> +      case OSMESA_PROFILE:
> +         profile = attribList[i+1];
> +         if (profile == OSMESA_COMPAT_PROFILE)
> +            api_profile = API_OPENGL_COMPAT;
> +         else if (profile == OSMESA_CORE_PROFILE)
> +            api_profile = API_OPENGL_CORE;
> +         else
> +            return NULL;
> +         break;
> +      case OSMESA_CONTEXT_MAJOR_VERSION:
> +         version_major = attribList[i+1];
> +         if (version_major < 1)
> +            return NULL;
> +         break;
> +      case OSMESA_CONTEXT_MINOR_VERSION:
> +         version_minor = attribList[i+1];
> +         if (version_minor < 0)
> +            return NULL;
> +         break;
> +      case 0:
> +         /* end of list */
> +         break;
> +      default:
> +         fprintf(stderr, "Bad attribute in OSMesaCreateContextAttribs()\n");
> +         return NULL;
> +      }
> +   }
>
>      rind = gind = bind = aind = 0;
>      if (format==OSMESA_RGBA) {
> @@ -742,7 +836,7 @@ OSMesaCreateContextExt( GLenum format, GLint depthBits, GLint stencilBits,
>         functions.UpdateState = osmesa_update_state;
>
>         if (!_mesa_initialize_context(&osmesa->mesa,
> -                                    API_OPENGL_COMPAT,
> +                                    api_profile,
>                                       osmesa->gl_visual,
>                                       sharelist ? &sharelist->mesa
>                                                 : (struct gl_context *) NULL,
> @@ -819,6 +913,13 @@ OSMesaCreateContextExt( GLenum format, GLint depthBits, GLint stencilBits,
>
>            _mesa_compute_version(ctx);
>
> +         if (ctx->Version < version_major * 10 + version_minor) {
> +            _mesa_destroy_visual(osmesa->gl_visual);
> +            _mesa_free_context_data(ctx);
> +            free(osmesa);
> +            return NULL;
> +         }
> +
>            /* Exec table initialization requires the version to be computed */
>            _mesa_initialize_dispatch_tables(ctx);
>            _mesa_initialize_vbo_vtxfmt(ctx);
> @@ -1121,6 +1222,7 @@ struct name_function
>   static struct name_function functions[] = {
>      { "OSMesaCreateContext", (OSMESAproc) OSMesaCreateContext },
>      { "OSMesaCreateContextExt", (OSMESAproc) OSMesaCreateContextExt },
> +   { "OSMesaCreateContextAttribs", (OSMESAproc) OSMesaCreateContextAttribs },
>      { "OSMesaDestroyContext", (OSMESAproc) OSMesaDestroyContext },
>      { "OSMesaMakeCurrent", (OSMESAproc) OSMesaMakeCurrent },
>      { "OSMesaGetCurrentContext", (OSMESAproc) OSMesaGetCurrentContext },
>

Otherwise series looks good AFAIT.

Reviewed-by: Jose Fonseca <jfonseca at vmware.com>




More information about the mesa-dev mailing list