1/*
2 * Copyright © 2015 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24/**
25 * \name mesa_formats.cpp
26 *
27 * Verify that all mesa formats are handled in certain functions and that
28 * the format info table is sane.
29 *
30 */
31
32#include <gtest/gtest.h>
33
34#include "main/formats.h"
35#include "main/glformats.h"
36
37/**
38 * Debug/test: check that all uncompressed formats are handled in the
39 * _mesa_uncompressed_format_to_type_and_comps() function. When new pixel
40 * formats are added to Mesa, that function needs to be updated.
41 */
42TEST(MesaFormatsTest, FormatTypeAndComps)
43{
44   for (int fi = MESA_FORMAT_NONE + 1; fi < MESA_FORMAT_COUNT; ++fi) {
45      mesa_format f = (mesa_format) fi;
46      SCOPED_TRACE(_mesa_get_format_name(f));
47
48      /* This function will emit a problem/warning if the format is
49       * not handled.
50       */
51      if (!_mesa_is_format_compressed(f)) {
52         GLenum datatype = 0;
53         GLenum error = 0;
54         GLuint comps = 0;
55
56         /* If the datatype is zero, the format was not handled */
57         _mesa_uncompressed_format_to_type_and_comps(f, &datatype, &comps);
58         EXPECT_NE(datatype, (GLenum)0);
59
60         /* If the error isn't NO_ERROR, the format was not handled.
61          * Use an arbitrary GLenum format. */
62         _mesa_format_matches_format_and_type(f, GL_RG, datatype,
63                                              GL_FALSE, &error);
64         EXPECT_EQ((GLenum)GL_NO_ERROR, error);
65      }
66
67   }
68}
69
70/**
71 * Do sanity checking of the format info table.
72 */
73TEST(MesaFormatsTest, FormatSanity)
74{
75   for (int fi = 0; fi < MESA_FORMAT_COUNT; ++fi) {
76      mesa_format f = (mesa_format) fi;
77      SCOPED_TRACE(_mesa_get_format_name(f));
78      GLenum datatype = _mesa_get_format_datatype(f);
79      GLint r = _mesa_get_format_bits(f, GL_RED_BITS);
80      GLint g = _mesa_get_format_bits(f, GL_GREEN_BITS);
81      GLint b = _mesa_get_format_bits(f, GL_BLUE_BITS);
82      GLint a = _mesa_get_format_bits(f, GL_ALPHA_BITS);
83      GLint l = _mesa_get_format_bits(f, GL_TEXTURE_LUMINANCE_SIZE);
84      GLint i = _mesa_get_format_bits(f, GL_TEXTURE_INTENSITY_SIZE);
85
86      /* Note: Z32_FLOAT_X24S8 has datatype of GL_NONE */
87      EXPECT_TRUE(datatype == GL_NONE ||
88                  datatype == GL_UNSIGNED_NORMALIZED ||
89                  datatype == GL_SIGNED_NORMALIZED ||
90                  datatype == GL_UNSIGNED_INT ||
91                  datatype == GL_INT ||
92                  datatype == GL_FLOAT);
93
94      if (r > 0 && !_mesa_is_format_compressed(f)) {
95         GLint bytes = _mesa_get_format_bytes(f);
96         EXPECT_LE((r+g+b+a) / 8, bytes);
97      }
98
99      /* Determines if the base format has a channel [rgba] or property [li].
100      * >  indicates existance
101      * == indicates non-existance
102      */
103      #define HAS_PROP(rop,gop,bop,aop,lop,iop) \
104         do { \
105            EXPECT_TRUE(r rop 0); \
106            EXPECT_TRUE(g gop 0); \
107            EXPECT_TRUE(b bop 0); \
108            EXPECT_TRUE(a aop 0); \
109            EXPECT_TRUE(l lop 0); \
110            EXPECT_TRUE(i iop 0); \
111         } while(0)
112
113      switch (_mesa_get_format_base_format(f)) {
114      case GL_RGBA:
115         HAS_PROP(>,>,>,>,==,==);
116         break;
117      case GL_RGB:
118         HAS_PROP(>,>,>,==,==,==);
119         break;
120      case GL_RG:
121         HAS_PROP(>,>,==,==,==,==);
122         break;
123      case GL_RED:
124         HAS_PROP(>,==,==,==,==,==);
125         break;
126      case GL_LUMINANCE:
127         HAS_PROP(==,==,==,==,>,==);
128         break;
129      case GL_INTENSITY:
130         HAS_PROP(==,==,==,==,==,>);
131         break;
132      default:
133         break;
134      }
135
136      #undef HAS_PROP
137
138   }
139}
140