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 DEALINGS
21  * IN THE SOFTWARE.
22  */
23 
24 #include "brw_context.h"
25 #include "brw_state.h"
26 #include "main/context.h"
27 #include "main/formatquery.h"
28 #include "main/glformats.h"
29 
30 static size_t
brw_query_samples_for_format(struct gl_context * ctx,GLenum target,GLenum internalFormat,int samples[16])31 brw_query_samples_for_format(struct gl_context *ctx, GLenum target,
32                              GLenum internalFormat, int samples[16])
33 {
34    struct brw_context *brw = brw_context(ctx);
35    const struct gen_device_info *devinfo = &brw->screen->devinfo;
36 
37    (void) target;
38    (void) internalFormat;
39 
40    switch (devinfo->gen) {
41    case 10:
42    case 9:
43       samples[0] = 16;
44       samples[1] = 8;
45       samples[2] = 4;
46       samples[3] = 2;
47       return 4;
48 
49    case 8:
50       samples[0] = 8;
51       samples[1] = 4;
52       samples[2] = 2;
53       return 3;
54 
55    case 7:
56       if (internalFormat == GL_RGBA32F && _mesa_is_gles(ctx)) {
57          /* For GLES, we are allowed to return a smaller number of samples for
58           * GL_RGBA32F. See OpenGLES 3.2 spec, section 20.3.1 Internal Format
59           * Query Parameters, under SAMPLES:
60           *
61           * "A value less than or equal to the value of MAX_SAMPLES, if
62           *  internalformat is RGBA16F, R32F, RG32F, or RGBA32F."
63           *
64           * In brw_render_target_supported, we prevent formats with a size
65           * greater than 8 bytes from using 8x MSAA on gen7.
66           */
67          samples[0] = 4;
68          return 1;
69       } else {
70          samples[0] = 8;
71          samples[1] = 4;
72          return 2;
73       }
74 
75    case 6:
76       samples[0] = 4;
77       return 1;
78 
79    default:
80       assert(devinfo->gen < 6);
81       samples[0] = 1;
82       return 1;
83    }
84 }
85 
86 void
brw_query_internal_format(struct gl_context * ctx,GLenum target,GLenum internalFormat,GLenum pname,GLint * params)87 brw_query_internal_format(struct gl_context *ctx, GLenum target,
88                           GLenum internalFormat, GLenum pname, GLint *params)
89 {
90    /* The Mesa layer gives us a temporary params buffer that is guaranteed
91     * to be non-NULL, and have at least 16 elements.
92     */
93    assert(params != NULL);
94 
95    switch (pname) {
96    case GL_SAMPLES:
97       brw_query_samples_for_format(ctx, target, internalFormat, params);
98       break;
99 
100    case GL_NUM_SAMPLE_COUNTS: {
101       size_t num_samples;
102       GLint dummy_buffer[16];
103 
104       num_samples = brw_query_samples_for_format(ctx, target, internalFormat,
105                                                  dummy_buffer);
106       params[0] = (GLint) num_samples;
107       break;
108    }
109 
110    default:
111       /* By default, we call the driver hook's fallback function from the frontend,
112        * which has generic implementation for all pnames.
113        */
114       _mesa_query_internal_format_default(ctx, target, internalFormat, pname,
115                                           params);
116       break;
117    }
118 }
119