f28e88022c6def3bbf94f60c5b4530336e5e0f27
[mesa.git] / src / mesa / drivers / dri / i965 / gen6_multisample_state.c
1 /*
2 * Copyright © 2012 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 "intel_batchbuffer.h"
25
26 #include "brw_context.h"
27 #include "brw_defines.h"
28 #include "brw_multisample_state.h"
29
30 void
31 gen6_get_sample_position(struct gl_context *ctx,
32 struct gl_framebuffer *fb,
33 GLuint index, GLfloat *result)
34 {
35 switch (fb->Visual.samples) {
36 case 1:
37 result[0] = result[1] = 0.5f;
38 break;
39 case 4: {
40 uint8_t val = (uint8_t)(brw_multisample_positions_4x[0] >> (8*index));
41 result[0] = ((val >> 4) & 0xf) / 16.0f;
42 result[1] = (val & 0xf) / 16.0f;
43 break;
44 }
45 case 8: {
46 uint8_t val = (uint8_t)(brw_multisample_positions_8x[index>>2] >> (8*(index & 3)));
47 result[0] = ((val >> 4) & 0xf) / 16.0f;
48 result[1] = (val & 0xf) / 16.0f;
49 break;
50 }
51 default:
52 assert(!"Not implemented");
53 }
54 }
55
56 /**
57 * 3DSTATE_MULTISAMPLE
58 */
59 void
60 gen6_emit_3dstate_multisample(struct brw_context *brw,
61 unsigned num_samples)
62 {
63 uint32_t number_of_multisamples = 0;
64 uint32_t sample_positions_3210 = 0;
65 uint32_t sample_positions_7654 = 0;
66
67 assert(brw->gen < 8);
68
69 switch (num_samples) {
70 case 0:
71 case 1:
72 number_of_multisamples = MS_NUMSAMPLES_1;
73 break;
74 case 4:
75 number_of_multisamples = MS_NUMSAMPLES_4;
76 sample_positions_3210 = brw_multisample_positions_4x[0];
77 break;
78 case 8:
79 number_of_multisamples = MS_NUMSAMPLES_8;
80 sample_positions_3210 = brw_multisample_positions_8x[0];
81 sample_positions_7654 = brw_multisample_positions_8x[1];
82 break;
83 default:
84 assert(!"Unrecognized num_samples in gen6_emit_3dstate_multisample");
85 break;
86 }
87
88 /* 3DSTATE_MULTISAMPLE is nonpipelined. */
89 intel_emit_post_sync_nonzero_flush(brw);
90
91 int len = brw->gen >= 7 ? 4 : 3;
92 BEGIN_BATCH(len);
93 OUT_BATCH(_3DSTATE_MULTISAMPLE << 16 | (len - 2));
94 OUT_BATCH(MS_PIXEL_LOCATION_CENTER | number_of_multisamples);
95 OUT_BATCH(sample_positions_3210);
96 if (brw->gen >= 7)
97 OUT_BATCH(sample_positions_7654);
98 ADVANCE_BATCH();
99 }
100
101
102 unsigned
103 gen6_determine_sample_mask(struct brw_context *brw)
104 {
105 struct gl_context *ctx = &brw->ctx;
106 float coverage = 1.0;
107 float coverage_invert = false;
108 unsigned sample_mask = ~0u;
109
110 unsigned num_samples = ctx->DrawBuffer->Visual.samples;
111
112 if (ctx->Multisample._Enabled) {
113 if (ctx->Multisample.SampleCoverage) {
114 coverage = ctx->Multisample.SampleCoverageValue;
115 coverage_invert = ctx->Multisample.SampleCoverageInvert;
116 }
117 if (ctx->Multisample.SampleMask) {
118 sample_mask = ctx->Multisample.SampleMaskValue;
119 }
120 }
121
122 if (num_samples > 1) {
123 int coverage_int = (int) (num_samples * coverage + 0.5);
124 uint32_t coverage_bits = (1 << coverage_int) - 1;
125 if (coverage_invert)
126 coverage_bits ^= (1 << num_samples) - 1;
127 return coverage_bits & sample_mask;
128 } else {
129 return 1;
130 }
131 }
132
133
134 /**
135 * 3DSTATE_SAMPLE_MASK
136 */
137 void
138 gen6_emit_3dstate_sample_mask(struct brw_context *brw, unsigned mask)
139 {
140 BEGIN_BATCH(2);
141 OUT_BATCH(_3DSTATE_SAMPLE_MASK << 16 | (2 - 2));
142 OUT_BATCH(mask);
143 ADVANCE_BATCH();
144 }
145
146
147 static void upload_multisample_state(struct brw_context *brw)
148 {
149 struct gl_context *ctx = &brw->ctx;
150
151 /* _NEW_BUFFERS, _NEW_MULTISAMPLE */
152 unsigned num_samples = ctx->DrawBuffer->Visual.samples;
153
154 gen6_emit_3dstate_multisample(brw, num_samples);
155 gen6_emit_3dstate_sample_mask(brw, gen6_determine_sample_mask(brw));
156 }
157
158
159 const struct brw_tracked_state gen6_multisample_state = {
160 .dirty = {
161 .mesa = _NEW_BUFFERS |
162 _NEW_MULTISAMPLE,
163 .brw = BRW_NEW_CONTEXT,
164 .cache = 0
165 },
166 .emit = upload_multisample_state
167 };