mesa: Add missing include guards
[mesa.git] / src / mesa / main / formatquery.c
index ac29bc80385354be88cb949b86ae16517f505cff..84b5f512ba524cb28f28867aad09498548e3ccea 100644 (file)
@@ -216,6 +216,8 @@ _legal_parameters(struct gl_context *ctx, GLenum target, GLenum internalformat,
    case GL_CLEAR_BUFFER:
    case GL_TEXTURE_VIEW:
    case GL_VIEW_COMPATIBILITY_CLASS:
+   case GL_NUM_TILING_TYPES_EXT:
+   case GL_TILING_TYPES_EXT:
       /* The ARB_internalformat_query spec says:
        *
        *     "If the <pname> parameter to GetInternalformativ is not SAMPLES
@@ -284,6 +286,7 @@ _set_default_response(GLenum pname, GLint buffer[16])
     */
    switch(pname) {
    case GL_SAMPLES:
+   case GL_TILING_TYPES_EXT:
       break;
 
    case GL_MAX_COMBINED_DIMENSIONS:
@@ -309,6 +312,7 @@ _set_default_response(GLenum pname, GLint buffer[16])
    case GL_TEXTURE_COMPRESSED_BLOCK_WIDTH:
    case GL_TEXTURE_COMPRESSED_BLOCK_HEIGHT:
    case GL_TEXTURE_COMPRESSED_BLOCK_SIZE:
+   case GL_NUM_TILING_TYPES_EXT:
       buffer[0] = 0;
       break;
 
@@ -387,29 +391,27 @@ _is_target_supported(struct gl_context *ctx, GLenum target)
     *     "if a particular type of <target> is not supported by the
     *     implementation the "unsupported" answer should be given.
     *     This is not an error."
+    *
+    * Note that legality of targets has already been verified.
     */
    switch(target){
+   case GL_TEXTURE_1D:
    case GL_TEXTURE_2D:
    case GL_TEXTURE_3D:
       break;
 
-   case GL_TEXTURE_1D:
-      if (!_mesa_is_desktop_gl(ctx))
-         return false;
-      break;
-
    case GL_TEXTURE_1D_ARRAY:
       if (!_mesa_has_EXT_texture_array(ctx))
          return false;
       break;
 
    case GL_TEXTURE_2D_ARRAY:
-      if (!(_mesa_has_EXT_texture_array(ctx) || _mesa_is_gles3(ctx)))
+      if (!_mesa_has_EXT_texture_array(ctx))
          return false;
       break;
 
    case GL_TEXTURE_CUBE_MAP:
-      if (!_mesa_has_ARB_texture_cube_map(ctx))
+      if (ctx->API != API_OPENGL_CORE && !_mesa_has_ARB_texture_cube_map(ctx))
          return false;
       break;
 
@@ -419,7 +421,7 @@ _is_target_supported(struct gl_context *ctx, GLenum target)
       break;
 
    case GL_TEXTURE_RECTANGLE:
-      if (!_mesa_has_NV_texture_rectangle(ctx))
+      if (!_mesa_has_ARB_texture_rectangle(ctx))
           return false;
       break;
 
@@ -499,8 +501,7 @@ _is_resource_supported(struct gl_context *ctx, GLenum target,
 
       /* additional checks for compressed textures */
       if (_mesa_is_compressed_format(ctx, internalformat) &&
-          (!_mesa_target_can_be_compressed(ctx, target, internalformat, NULL) ||
-           _mesa_format_no_online_compression(ctx, internalformat)))
+          !_mesa_target_can_be_compressed(ctx, target, internalformat, NULL))
          return false;
 
       break;
@@ -556,15 +557,29 @@ _is_internalformat_supported(struct gl_context *ctx, GLenum target,
     *         implementation accepts it for any texture specification commands, and
     *         - unsized or base internal format, if the implementation accepts
     *         it for texture or image specification.
+    *
+    * But also:
+    * "If the particualar <target> and <internalformat> combination do not make
+    * sense, or if a particular type of <target> is not supported by the
+    * implementation the "unsupported" answer should be given. This is not an
+    * error.
     */
    GLint buffer[1];
 
-   /* At this point a internalformat is valid if it is valid as a texture or
-    * as a renderbuffer format. The checks are different because those methods
-    * return different values when passing non supported internalformats */
-   if (_mesa_base_tex_format(ctx, internalformat) < 0 &&
-       _mesa_base_fbo_format(ctx, internalformat) == 0)
-      return false;
+   if (target == GL_RENDERBUFFER) {
+      if (_mesa_base_fbo_format(ctx, internalformat) == 0) {
+         return false;
+      }
+   } else if (target == GL_TEXTURE_BUFFER) {
+      if (_mesa_validate_texbuffer_format(ctx, internalformat) ==
+          MESA_FORMAT_NONE) {
+         return false;
+      }
+   } else {
+      if (_mesa_base_tex_format(ctx, internalformat) < 0) {
+         return false;
+      }
+   }
 
    /* Let the driver have the final word */
    ctx->Driver.QueryInternalFormat(ctx, target, internalformat,
@@ -573,6 +588,34 @@ _is_internalformat_supported(struct gl_context *ctx, GLenum target,
    return (buffer[0] == GL_TRUE);
 }
 
+static bool
+_legal_target_for_framebuffer_texture_layer(struct gl_context *ctx,
+                                            GLenum target)
+{
+   switch (target) {
+   case GL_TEXTURE_3D:
+   case GL_TEXTURE_1D_ARRAY:
+   case GL_TEXTURE_2D_ARRAY:
+   case GL_TEXTURE_CUBE_MAP_ARRAY:
+   case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
+   case GL_TEXTURE_CUBE_MAP:
+      return true;
+   default:
+      return false;
+   }
+}
+
+static GLenum
+_mesa_generic_type_for_internal_format(GLenum internalFormat)
+{
+   if (_mesa_is_enum_format_unsigned_int(internalFormat))
+      return GL_UNSIGNED_BYTE;
+   else if (_mesa_is_enum_format_signed_int(internalFormat))
+      return GL_BYTE;
+   else
+      return GL_FLOAT;
+}
+
 /* default implementation of QueryInternalFormat driverfunc, for
  * drivers not implementing ARB_internalformat_query2.
  */
@@ -581,9 +624,7 @@ _mesa_query_internal_format_default(struct gl_context *ctx, GLenum target,
                                     GLenum internalFormat, GLenum pname,
                                     GLint *params)
 {
-   (void) ctx;
    (void) target;
-   (void) internalFormat;
 
    switch (pname) {
    case GL_SAMPLES:
@@ -599,6 +640,52 @@ _mesa_query_internal_format_default(struct gl_context *ctx, GLenum target,
       params[0] = internalFormat;
       break;
 
+   case GL_READ_PIXELS_FORMAT: {
+      GLenum base_format = _mesa_base_tex_format(ctx, internalFormat);
+      switch (base_format) {
+      case GL_STENCIL_INDEX:
+      case GL_DEPTH_COMPONENT:
+      case GL_DEPTH_STENCIL:
+      case GL_RED:
+      case GL_RGB:
+      case GL_BGR:
+      case GL_RGBA:
+      case GL_BGRA:
+         params[0] = base_format;
+         break;
+      default:
+         params[0] = GL_NONE;
+         break;
+      }
+      break;
+   }
+
+   case GL_READ_PIXELS_TYPE:
+   case GL_TEXTURE_IMAGE_TYPE:
+   case GL_GET_TEXTURE_IMAGE_TYPE: {
+      GLenum base_format = _mesa_base_tex_format(ctx, internalFormat);
+      if (base_format > 0)
+         params[0] = _mesa_generic_type_for_internal_format(internalFormat);
+      else
+         params[0] = GL_NONE;
+      break;
+   }
+
+   case GL_TEXTURE_IMAGE_FORMAT:
+   case GL_GET_TEXTURE_IMAGE_FORMAT: {
+      GLenum format = GL_NONE;
+      GLenum base_format = _mesa_base_tex_format(ctx, internalFormat);
+      if (base_format > 0) {
+         if (_mesa_is_enum_format_integer(internalFormat))
+           format = _mesa_base_format_to_integer_format(base_format);
+         else
+           format = base_format;
+      }
+
+      params[0] = format;
+      break;
+   }
+
    case GL_MANUAL_GENERATE_MIPMAP:
    case GL_AUTO_GENERATE_MIPMAP:
    case GL_SRGB_READ:
@@ -619,8 +706,28 @@ _mesa_query_internal_format_default(struct gl_context *ctx, GLenum target,
    case GL_SIMULTANEOUS_TEXTURE_AND_STENCIL_WRITE:
    case GL_CLEAR_BUFFER:
    case GL_TEXTURE_VIEW:
+   case GL_TEXTURE_SHADOW:
+   case GL_TEXTURE_GATHER:
+   case GL_TEXTURE_GATHER_SHADOW:
+   case GL_FRAMEBUFFER_RENDERABLE:
+   case GL_FRAMEBUFFER_RENDERABLE_LAYERED:
+   case GL_FRAMEBUFFER_BLEND:
+   case GL_FILTER:
+      /*
+       * TODO seems a tad optimistic just saying yes to everything here.
+       * Even for combinations which make no sense...
+       * And things like TESS_CONTROL_TEXTURE should definitely default to
+       * NONE if the driver doesn't even support tessellation...
+       */
       params[0] = GL_FULL_SUPPORT;
       break;
+   case GL_NUM_TILING_TYPES_EXT:
+      params[0] = 2;
+      break;
+   case GL_TILING_TYPES_EXT:
+      params[0] = GL_OPTIMAL_TILING_EXT;
+      params[1] = GL_LINEAR_TILING_EXT;
+      break;
 
    default:
       _set_default_response(pname, params);
@@ -639,8 +746,8 @@ _mesa_query_internal_format_default(struct gl_context *ctx, GLenum target,
  * arb_internalformat_query2 spec.
  */
 static GLenum
-equivalentSizePname(GLenum target,
-                    GLenum pname)
+_equivalent_size_pname(GLenum target,
+                       GLenum pname)
 {
    switch (target) {
    case GL_TEXTURE_1D:
@@ -684,7 +791,7 @@ equivalentSizePname(GLenum target,
  * per-se, so we can't just call _mesa_get_texture_dimension directly.
  */
 static GLint
-get_target_dimensions(GLenum target)
+_get_target_dimensions(GLenum target)
 {
    switch(target) {
    case GL_TEXTURE_BUFFER:
@@ -709,7 +816,7 @@ get_target_dimensions(GLenum target)
  *  <skip>."
  */
 static GLint
-get_min_dimensions(GLenum pname)
+_get_min_dimensions(GLenum pname)
 {
    switch(pname) {
    case GL_MAX_WIDTH:
@@ -728,7 +835,7 @@ get_min_dimensions(GLenum pname)
  * dimensions.
  */
 static bool
-is_multisample_target(GLenum target)
+_is_multisample_target(GLenum target)
 {
    switch(target) {
    case GL_TEXTURE_2D_MULTISAMPLE:
@@ -798,6 +905,9 @@ _mesa_GetInternalformativ(GLenum target, GLenum internalformat, GLenum pname,
        *     "Since multisampling is not supported for signed and unsigned
        *     integer internal formats, the value of NUM_SAMPLE_COUNTS will be
        *     zero for such formats.
+       *
+       * Since OpenGL ES 3.1 adds support for multisampled integer formats, we
+       * have to check the version for 30 exactly.
        */
       if (pname == GL_NUM_SAMPLE_COUNTS && ctx->API == API_OPENGLES2 &&
           ctx->Version == 30 && _mesa_is_enum_format_integer(internalformat)) {
@@ -823,7 +933,10 @@ _mesa_GetInternalformativ(GLenum target, GLenum internalformat, GLenum pname,
        *     format for representing resources of the specified <internalformat> is
        *     returned in <params>.
        *
-       * Therefore, we let the driver answer.
+       * Therefore, we let the driver answer. Note that if we reach this
+       * point, it means that the internalformat is supported, so the driver
+       * is called just to try to get a preferred format. If not supported,
+       * GL_NONE was already returned and the driver is not called.
        */
       ctx->Driver.QueryInternalFormat(ctx, target, internalformat, pname,
                                       buffer);
@@ -846,9 +959,6 @@ _mesa_GetInternalformativ(GLenum target, GLenum internalformat, GLenum pname,
       mesa_format texformat;
 
       if (target != GL_RENDERBUFFER) {
-         if (!_mesa_legal_get_tex_level_parameter_target(ctx, target, true))
-            goto end;
-
          baseformat = _mesa_base_tex_format(ctx, internalformat);
       } else {
          baseformat = _mesa_base_fbo_format(ctx, internalformat);
@@ -869,10 +979,7 @@ _mesa_GetInternalformativ(GLenum target, GLenum internalformat, GLenum pname,
        * and glGetRenderbufferParameteriv functions.
        */
       if (pname == GL_INTERNALFORMAT_SHARED_SIZE) {
-         if (_mesa_has_EXT_texture_shared_exponent(ctx) &&
-             target != GL_TEXTURE_BUFFER &&
-             target != GL_RENDERBUFFER &&
-             texformat == MESA_FORMAT_R9G9B9E5_FLOAT) {
+         if (texformat == MESA_FORMAT_R9G9B9E5_FLOAT) {
             buffer[0] = 5;
          }
          goto end;
@@ -883,7 +990,8 @@ _mesa_GetInternalformativ(GLenum target, GLenum internalformat, GLenum pname,
 
       switch (pname) {
       case GL_INTERNALFORMAT_DEPTH_SIZE:
-         if (!_mesa_has_ARB_depth_texture(ctx) &&
+         if (ctx->API != API_OPENGL_CORE &&
+             !_mesa_has_ARB_depth_texture(ctx) &&
              target != GL_RENDERBUFFER &&
              target != GL_TEXTURE_BUFFER)
             goto end;
@@ -930,12 +1038,12 @@ _mesa_GetInternalformativ(GLenum target, GLenum internalformat, GLenum pname,
        * "If the resource does not have at least two dimensions, or if the
        * resource is unsupported, zero is returned."
        */
-      dimensions = get_target_dimensions(target);
-      min_dimensions = get_min_dimensions(pname);
+      dimensions = _get_target_dimensions(target);
+      min_dimensions = _get_min_dimensions(pname);
       if (dimensions < min_dimensions)
          goto end;
 
-      get_pname = equivalentSizePname(target, pname);
+      get_pname = _equivalent_size_pname(target, pname);
       if (get_pname == 0)
          goto end;
 
@@ -969,7 +1077,7 @@ _mesa_GetInternalformativ(GLenum target, GLenum internalformat, GLenum pname,
        * returned as MAX_HEIGHT or MAX_DEPTH */
       for (i = 0; i < 4; i++) {
          if (max_dimensions_pnames[i] == GL_SAMPLES &&
-             !is_multisample_target(target))
+             !_is_multisample_target(target))
             continue;
 
          _mesa_GetInternalformativ(target, internalformat,
@@ -1047,44 +1155,37 @@ _mesa_GetInternalformativ(GLenum target, GLenum internalformat, GLenum pname,
       buffer[0] = GL_TRUE;
       break;
 
+   case GL_FRAMEBUFFER_RENDERABLE_LAYERED:
+      if (!_mesa_has_EXT_texture_array(ctx) ||
+          _legal_target_for_framebuffer_texture_layer(ctx, target))
+         goto end;
+      /* fallthrough */
    case GL_FRAMEBUFFER_RENDERABLE:
-      /* @TODO */
-      break;
+   case GL_FRAMEBUFFER_BLEND:
+      if (!_mesa_has_ARB_framebuffer_object(ctx))
+         goto end;
 
-   case GL_FRAMEBUFFER_RENDERABLE_LAYERED:
-      /* @TODO */
-      break;
+      if (target == GL_TEXTURE_BUFFER ||
+          !_is_renderable(ctx, internalformat))
+         goto end;
 
-   case GL_FRAMEBUFFER_BLEND:
-      /* @TODO */
+      ctx->Driver.QueryInternalFormat(ctx, target, internalformat, pname,
+                                      buffer);
       break;
 
    case GL_READ_PIXELS:
-      /* @TODO */
-      break;
-
    case GL_READ_PIXELS_FORMAT:
-      /* @TODO */
-      break;
-
    case GL_READ_PIXELS_TYPE:
-      /* @TODO */
+      ctx->Driver.QueryInternalFormat(ctx, target, internalformat, pname,
+                                      buffer);
       break;
 
    case GL_TEXTURE_IMAGE_FORMAT:
-      /* @TODO */
-      break;
-
-   case GL_TEXTURE_IMAGE_TYPE:
-      /* @TODO */
-      break;
-
    case GL_GET_TEXTURE_IMAGE_FORMAT:
-      /* @TODO */
-      break;
-
+   case GL_TEXTURE_IMAGE_TYPE:
    case GL_GET_TEXTURE_IMAGE_TYPE:
-      /* @TODO */
+      ctx->Driver.QueryInternalFormat(ctx, target, internalformat, pname,
+                                      buffer);
       break;
 
    case GL_MIPMAP:
@@ -1162,7 +1263,24 @@ _mesa_GetInternalformativ(GLenum target, GLenum internalformat, GLenum pname,
       break;
 
    case GL_FILTER:
-      /* @TODO */
+      /* If it doesn't allow to set sampler parameters then it would not allow
+       * to set a filter different to GL_NEAREST. In practice, this method
+       * only filters out MULTISAMPLE/MULTISAMPLE_ARRAY */
+      if (!_mesa_target_allows_setting_sampler_parameters(target))
+         goto end;
+
+      if (_mesa_is_enum_format_integer(internalformat))
+         goto end;
+
+      if (target == GL_TEXTURE_BUFFER)
+         goto end;
+
+      /* At this point we know that multi-texel filtering is supported. We
+       * need to call the driver to know if it is CAVEAT_SUPPORT or
+       * FULL_SUPPORT.
+       */
+      ctx->Driver.QueryInternalFormat(ctx, target, internalformat, pname,
+                                      buffer);
       break;
 
    case GL_VERTEX_TEXTURE:
@@ -1189,16 +1307,42 @@ _mesa_GetInternalformativ(GLenum target, GLenum internalformat, GLenum pname,
                                       buffer);
       break;
 
+   case GL_TEXTURE_GATHER:
+   case GL_TEXTURE_GATHER_SHADOW:
+      if (!_mesa_has_ARB_texture_gather(ctx))
+         goto end;
+
+      /* fallthrough */
    case GL_TEXTURE_SHADOW:
-      /* @TODO */
-      break;
+      /* Only depth or depth-stencil image formats make sense in shadow
+         samplers */
+      if (pname != GL_TEXTURE_GATHER &&
+          !_mesa_is_depth_format(internalformat) &&
+          !_mesa_is_depthstencil_format(internalformat))
+         goto end;
 
-   case GL_TEXTURE_GATHER:
-      /* @TODO */
-      break;
+      /* Validate the target for shadow and gather operations */
+      switch (target) {
+      case GL_TEXTURE_2D:
+      case GL_TEXTURE_2D_ARRAY:
+      case GL_TEXTURE_CUBE_MAP:
+      case GL_TEXTURE_CUBE_MAP_ARRAY:
+      case GL_TEXTURE_RECTANGLE:
+         break;
 
-   case GL_TEXTURE_GATHER_SHADOW:
-      /* @TODO */
+      case GL_TEXTURE_1D:
+      case GL_TEXTURE_1D_ARRAY:
+         /* 1D and 1DArray textures are not admitted in gather operations */
+         if (pname != GL_TEXTURE_SHADOW)
+            goto end;
+         break;
+
+      default:
+         goto end;
+      }
+
+      ctx->Driver.QueryInternalFormat(ctx, target, internalformat, pname,
+                                      buffer);
       break;
 
    case GL_SHADER_IMAGE_LOAD:
@@ -1297,7 +1441,13 @@ _mesa_GetInternalformativ(GLenum target, GLenum internalformat, GLenum pname,
       if (!_mesa_has_ARB_shader_image_load_store(ctx))
          goto end;
 
-      if (!_mesa_legal_get_tex_level_parameter_target(ctx, target, true))
+      /* As pointed by the spec quote below, this pname query should return
+       * the same value that GetTexParameter. So if the target is not valid
+       * for GetTexParameter we return the unsupported value. The check below
+       * is the same target check used by GetTexParameter.
+       */
+      int targetIndex = _mesa_tex_target_to_index(ctx, target);
+      if (targetIndex < 0 || targetIndex == TEXTURE_BUFFER_INDEX)
          goto end;
 
       /* From spec: "Equivalent to calling GetTexParameter with <value> set
@@ -1397,6 +1547,12 @@ _mesa_GetInternalformativ(GLenum target, GLenum internalformat, GLenum pname,
       }
       break;
 
+   case GL_NUM_TILING_TYPES_EXT:
+   case GL_TILING_TYPES_EXT:
+      ctx->Driver.QueryInternalFormat(ctx, target, internalformat, pname,
+                                      buffer);
+      break;
+
    default:
       unreachable("bad param");
    }
@@ -1442,7 +1598,8 @@ _mesa_GetInternalformati64v(GLenum target, GLenum internalformat,
     * no pname can return a negative value, we fill params32 with negative
     * values as reference values, that can be used to know what copy-back to
     * params */
-   memset(params32, -1, 16);
+   for (i = 0; i < realSize; i++)
+      params32[i] = -1;
 
    /* For GL_MAX_COMBINED_DIMENSIONS we need to get back 2 32-bit integers,
     * and at the same time we only need 2. So for that pname, we call the