486e3082533144a9df615641de7969c2f1609316
[mesa.git] / src / mesa / drivers / dri / i965 / intel_pixel_copy.c
1 /**************************************************************************
2 *
3 * Copyright 2003 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 VMWARE 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 #include "main/glheader.h"
29 #include "main/image.h"
30 #include "main/state.h"
31 #include "main/mtypes.h"
32 #include "main/condrender.h"
33 #include "main/fbobject.h"
34 #include "drivers/common/meta.h"
35
36 #include "brw_context.h"
37 #include "intel_buffers.h"
38 #include "intel_mipmap_tree.h"
39 #include "intel_regions.h"
40 #include "intel_pixel.h"
41 #include "intel_fbo.h"
42 #include "intel_blit.h"
43 #include "intel_batchbuffer.h"
44
45 #define FILE_DEBUG_FLAG DEBUG_PIXEL
46
47 /**
48 * CopyPixels with the blitter. Don't support zooming, pixel transfer, etc.
49 */
50 static bool
51 do_blit_copypixels(struct gl_context * ctx,
52 GLint srcx, GLint srcy,
53 GLsizei width, GLsizei height,
54 GLint dstx, GLint dsty, GLenum type)
55 {
56 struct brw_context *brw = brw_context(ctx);
57 struct gl_framebuffer *fb = ctx->DrawBuffer;
58 struct gl_framebuffer *read_fb = ctx->ReadBuffer;
59 GLint orig_dstx;
60 GLint orig_dsty;
61 GLint orig_srcx;
62 GLint orig_srcy;
63 struct intel_renderbuffer *draw_irb = NULL;
64 struct intel_renderbuffer *read_irb = NULL;
65
66 /* Update draw buffer bounds */
67 _mesa_update_state(ctx);
68
69 intel_prepare_render(brw);
70
71 switch (type) {
72 case GL_COLOR:
73 if (fb->_NumColorDrawBuffers != 1) {
74 perf_debug("glCopyPixels() fallback: MRT\n");
75 return false;
76 }
77
78 draw_irb = intel_renderbuffer(fb->_ColorDrawBuffers[0]);
79 read_irb = intel_renderbuffer(read_fb->_ColorReadBuffer);
80 break;
81 case GL_DEPTH_STENCIL_EXT:
82 draw_irb = intel_renderbuffer(fb->Attachment[BUFFER_DEPTH].Renderbuffer);
83 read_irb =
84 intel_renderbuffer(read_fb->Attachment[BUFFER_DEPTH].Renderbuffer);
85 break;
86 case GL_DEPTH:
87 perf_debug("glCopyPixels() fallback: GL_DEPTH\n");
88 return false;
89 case GL_STENCIL:
90 perf_debug("glCopyPixels() fallback: GL_STENCIL\n");
91 return false;
92 default:
93 perf_debug("glCopyPixels(): Unknown type\n");
94 return false;
95 }
96
97 if (!draw_irb) {
98 perf_debug("glCopyPixels() fallback: missing draw buffer\n");
99 return false;
100 }
101
102 if (!read_irb) {
103 perf_debug("glCopyPixels() fallback: missing read buffer\n");
104 return false;
105 }
106
107 if (draw_irb->mt->num_samples > 1 || read_irb->mt->num_samples > 1) {
108 perf_debug("glCopyPixels() fallback: multisampled buffers\n");
109 return false;
110 }
111
112 if (ctx->_ImageTransferState) {
113 perf_debug("glCopyPixels(): Unsupported image transfer state\n");
114 return false;
115 }
116
117 if (ctx->Depth.Test) {
118 perf_debug("glCopyPixels(): Unsupported depth test state\n");
119 return false;
120 }
121
122 if (ctx->Stencil._Enabled) {
123 perf_debug("glCopyPixels(): Unsupported stencil test state\n");
124 return false;
125 }
126
127 if (ctx->Fog.Enabled ||
128 ctx->Texture._EnabledUnits ||
129 ctx->FragmentProgram._Enabled) {
130 perf_debug("glCopyPixels(): Unsupported fragment shader state\n");
131 return false;
132 }
133
134 if (ctx->Color.AlphaEnabled ||
135 ctx->Color.BlendEnabled) {
136 perf_debug("glCopyPixels(): Unsupported blend state\n");
137 return false;
138 }
139
140 if (!ctx->Color.ColorMask[0][0] ||
141 !ctx->Color.ColorMask[0][1] ||
142 !ctx->Color.ColorMask[0][2] ||
143 !ctx->Color.ColorMask[0][3]) {
144 perf_debug("glCopyPixels(): Unsupported color mask state\n");
145 return false;
146 }
147
148 if (ctx->Pixel.ZoomX != 1.0F || ctx->Pixel.ZoomY != 1.0F) {
149 perf_debug("glCopyPixles(): Unsupported pixel zoom\n");
150 return false;
151 }
152
153 intel_batchbuffer_flush(brw);
154
155 /* Clip to destination buffer. */
156 orig_dstx = dstx;
157 orig_dsty = dsty;
158 if (!_mesa_clip_to_region(fb->_Xmin, fb->_Ymin,
159 fb->_Xmax, fb->_Ymax,
160 &dstx, &dsty, &width, &height))
161 goto out;
162 /* Adjust src coords for our post-clipped destination origin */
163 srcx += dstx - orig_dstx;
164 srcy += dsty - orig_dsty;
165
166 /* Clip to source buffer. */
167 orig_srcx = srcx;
168 orig_srcy = srcy;
169 if (!_mesa_clip_to_region(0, 0,
170 read_fb->Width, read_fb->Height,
171 &srcx, &srcy, &width, &height))
172 goto out;
173 /* Adjust dst coords for our post-clipped source origin */
174 dstx += srcx - orig_srcx;
175 dsty += srcy - orig_srcy;
176
177 if (!intel_miptree_blit(brw,
178 read_irb->mt, read_irb->mt_level, read_irb->mt_layer,
179 srcx, srcy, _mesa_is_winsys_fbo(read_fb),
180 draw_irb->mt, draw_irb->mt_level, draw_irb->mt_layer,
181 dstx, dsty, _mesa_is_winsys_fbo(fb),
182 width, height,
183 (ctx->Color.ColorLogicOpEnabled ?
184 ctx->Color.LogicOp : GL_COPY))) {
185 DBG("%s: blit failure\n", __FUNCTION__);
186 return false;
187 }
188
189 if (ctx->Query.CurrentOcclusionObject)
190 ctx->Query.CurrentOcclusionObject->Result += width * height;
191
192 out:
193 intel_check_front_buffer_rendering(brw);
194
195 DBG("%s: success\n", __FUNCTION__);
196 return true;
197 }
198
199
200 void
201 intelCopyPixels(struct gl_context * ctx,
202 GLint srcx, GLint srcy,
203 GLsizei width, GLsizei height,
204 GLint destx, GLint desty, GLenum type)
205 {
206 DBG("%s\n", __FUNCTION__);
207
208 if (!_mesa_check_conditional_render(ctx))
209 return;
210
211 if (do_blit_copypixels(ctx, srcx, srcy, width, height, destx, desty, type))
212 return;
213
214 /* this will use swrast if needed */
215 _mesa_meta_CopyPixels(ctx, srcx, srcy, width, height, destx, desty, type);
216 }