1 /**************************************************************************
2  *
3  * Copyright 2010 VMware, Inc.
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sub license, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice (including the
15  * next paragraph) shall be included in all copies or substantial portions
16  * of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21  * IN NO EVENT SHALL THE AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR
22  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25  *
26  **************************************************************************/
27 
28 
29 #include "st_context.h"
30 #include "pipe/p_screen.h"
31 #include "pipe/p_context.h"
32 #include "st_atom.h"
33 #include "st_program.h"
34 #include "st_util.h"
35 
36 #include "cso_cache/cso_context.h"
37 #include "util/u_framebuffer.h"
38 #include "main/framebuffer.h"
39 
40 
41 /**
42  * Update the sample locations
43  */
44 static void
update_sample_locations(struct st_context * st)45 update_sample_locations(struct st_context *st)
46 {
47    struct gl_framebuffer *fb = st->ctx->DrawBuffer;
48 
49    if (!st->ctx->Extensions.ARB_sample_locations)
50       return;
51 
52    if (fb->ProgrammableSampleLocations) {
53       unsigned grid_width, grid_height, size, pixel, sample_index;
54       unsigned samples = st->state.fb_num_samples;
55       bool sample_location_pixel_grid = fb->SampleLocationPixelGrid;
56       uint8_t locations[
57          PIPE_MAX_SAMPLE_LOCATION_GRID_SIZE *
58          PIPE_MAX_SAMPLE_LOCATION_GRID_SIZE * 32];
59 
60       st->screen->get_sample_pixel_grid(st->screen, samples,
61                                         &grid_width, &grid_height);
62       size = grid_width * grid_height * samples;
63 
64       /**
65        * when a dimension is greater than MAX_SAMPLE_LOCATION_GRID_SIZE,
66        * st->ctx->Driver.GetSamplePixelGrid() returns 1 for both dimensions.
67        */
68       if (grid_width > MAX_SAMPLE_LOCATION_GRID_SIZE ||
69           grid_height > MAX_SAMPLE_LOCATION_GRID_SIZE)
70          sample_location_pixel_grid = false;
71 
72       for (pixel = 0; pixel < grid_width * grid_height; pixel++) {
73          for (sample_index = 0; sample_index < samples; sample_index++) {
74             int table_index = sample_index;
75             float x = 0.5f, y = 0.5f;
76             uint8_t loc;
77             if (sample_location_pixel_grid)
78                table_index = pixel * samples + sample_index;
79             if (fb->SampleLocationTable) {
80                x = fb->SampleLocationTable[table_index*2];
81                y = fb->SampleLocationTable[table_index*2+1];
82             }
83             if (st->state.fb_orientation == Y_0_BOTTOM)
84                y = 1.0 - y;
85 
86             loc = roundf(CLAMP(x * 16.0f, 0.0f, 15.0f));
87             loc |= (int)roundf(CLAMP(y * 16.0f, 0.0f, 15.0f)) << 4;
88             locations[pixel * samples + sample_index] = loc;
89          }
90       }
91 
92       util_sample_locations_flip_y(
93          st->screen, st->state.fb_height, samples, locations);
94 
95       if (!st->state.enable_sample_locations ||
96           st->state.sample_locations_samples != samples ||
97           memcmp(locations, st->state.sample_locations, size) != 0) {
98          st->pipe->set_sample_locations( st->pipe, size, locations);
99 
100          st->state.sample_locations_samples = samples;
101          memcpy(st->state.sample_locations, locations, size);
102       }
103    } else if (st->state.enable_sample_locations) {
104       st->pipe->set_sample_locations(st->pipe, 0, NULL);
105    }
106 
107    st->state.enable_sample_locations = fb->ProgrammableSampleLocations;
108 }
109 
110 
111 /* Update the sample mask and locations for MSAA.
112  */
113 void
st_update_sample_state(struct st_context * st)114 st_update_sample_state(struct st_context *st)
115 {
116    unsigned sample_mask = 0xffffffff;
117    unsigned sample_count = st->state.fb_num_samples;
118 
119    if (_mesa_is_multisample_enabled(st->ctx) && sample_count > 1) {
120       /* unlike in gallium/d3d10 the mask is only active if msaa is enabled */
121       if (st->ctx->Multisample.SampleCoverage) {
122          unsigned nr_bits = (unsigned)
123             (st->ctx->Multisample.SampleCoverageValue * (float) sample_count);
124          /* there's lot of ways how to do this. We just use first few bits,
125           * since we have no knowledge of sample positions here. When
126           * app-supplied mask though is used too might need to be smarter.
127           * Also, there's an interface restriction here in theory it is
128           * encouraged this mask not be the same at each pixel.
129           */
130          sample_mask = (1 << nr_bits) - 1;
131          if (st->ctx->Multisample.SampleCoverageInvert)
132             sample_mask = ~sample_mask;
133       }
134       if (st->ctx->Multisample.SampleMask)
135          sample_mask &= st->ctx->Multisample.SampleMaskValue;
136    }
137 
138    cso_set_sample_mask(st->cso_context, sample_mask);
139 
140    update_sample_locations(st);
141 }
142 
143 
144 void
st_update_sample_shading(struct st_context * st)145 st_update_sample_shading(struct st_context *st)
146 {
147    if (!st->fp)
148       return;
149 
150    if (!st->ctx->Extensions.ARB_sample_shading)
151       return;
152 
153    cso_set_min_samples(st->cso_context,
154          _mesa_get_min_invocations_per_fragment(st->ctx, &st->fp->Base));
155 }
156