i965: Enable OpenGL 4.5 on Haswell.
[mesa.git] / src / mesa / drivers / dri / i965 / gen6_scissor_state.c
1 /*
2 * Copyright © 2009 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 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 *
26 */
27
28 #include "brw_context.h"
29 #include "brw_state.h"
30 #include "brw_defines.h"
31 #include "intel_batchbuffer.h"
32 #include "main/fbobject.h"
33 #include "main/framebuffer.h"
34
35 static void
36 gen6_upload_scissor_state(struct brw_context *brw)
37 {
38 struct gl_context *ctx = &brw->ctx;
39 const bool render_to_fbo = _mesa_is_user_fbo(ctx->DrawBuffer);
40 struct gen6_scissor_rect *scissor;
41 uint32_t scissor_state_offset;
42 const unsigned int fb_width= _mesa_geometric_width(ctx->DrawBuffer);
43 const unsigned int fb_height = _mesa_geometric_height(ctx->DrawBuffer);
44
45 /* BRW_NEW_VIEWPORT_COUNT */
46 const unsigned viewport_count = brw->clip.viewport_count;
47
48 scissor = brw_state_batch(brw, AUB_TRACE_SCISSOR_STATE,
49 sizeof(*scissor) * viewport_count, 32,
50 &scissor_state_offset);
51
52 /* _NEW_SCISSOR | _NEW_BUFFERS | _NEW_VIEWPORT */
53
54 /* The scissor only needs to handle the intersection of drawable and
55 * scissor rect. Clipping to the boundaries of static shared buffers
56 * for front/back/depth is covered by looping over cliprects in brw_draw.c.
57 *
58 * Note that the hardware's coordinates are inclusive, while Mesa's min is
59 * inclusive but max is exclusive.
60 */
61 for (unsigned i = 0; i < viewport_count; i++) {
62 int bbox[4];
63
64 bbox[0] = MAX2(ctx->ViewportArray[i].X, 0);
65 bbox[1] = MIN2(bbox[0] + ctx->ViewportArray[i].Width, fb_width);
66 bbox[2] = MAX2(ctx->ViewportArray[i].Y, 0);
67 bbox[3] = MIN2(bbox[2] + ctx->ViewportArray[i].Height, fb_height);
68 _mesa_intersect_scissor_bounding_box(ctx, i, bbox);
69
70 if (bbox[0] == bbox[1] || bbox[2] == bbox[3]) {
71 /* If the scissor was out of bounds and got clamped to 0 width/height
72 * at the bounds, the subtraction of 1 from maximums could produce a
73 * negative number and thus not clip anything. Instead, just provide
74 * a min > max scissor inside the bounds, which produces the expected
75 * no rendering.
76 */
77 scissor[i].xmin = 1;
78 scissor[i].xmax = 0;
79 scissor[i].ymin = 1;
80 scissor[i].ymax = 0;
81 } else if (render_to_fbo) {
82 /* texmemory: Y=0=bottom */
83 scissor[i].xmin = bbox[0];
84 scissor[i].xmax = bbox[1] - 1;
85 scissor[i].ymin = bbox[2];
86 scissor[i].ymax = bbox[3] - 1;
87 }
88 else {
89 /* memory: Y=0=top */
90 scissor[i].xmin = bbox[0];
91 scissor[i].xmax = bbox[1] - 1;
92 scissor[i].ymin = fb_height - bbox[3];
93 scissor[i].ymax = fb_height - bbox[2] - 1;
94 }
95 }
96 BEGIN_BATCH(2);
97 OUT_BATCH(_3DSTATE_SCISSOR_STATE_POINTERS << 16 | (2 - 2));
98 OUT_BATCH(scissor_state_offset);
99 ADVANCE_BATCH();
100 }
101
102 const struct brw_tracked_state gen6_scissor_state = {
103 .dirty = {
104 .mesa = _NEW_BUFFERS |
105 _NEW_SCISSOR |
106 _NEW_VIEWPORT,
107 .brw = BRW_NEW_BATCH |
108 BRW_NEW_BLORP |
109 BRW_NEW_VIEWPORT_COUNT,
110 },
111 .emit = gen6_upload_scissor_state,
112 };