Switch fragment/vertex shaders to the new caching semantics.
[mesa.git] / src / mesa / state_tracker / st_cb_readpixels.c
1 /**************************************************************************
2 *
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
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 TUNGSTEN GRAPHICS 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 /**
30 * glReadPixels interface to pipe
31 *
32 * \author Brian Paul
33 */
34
35
36 #include "main/imports.h"
37 #include "main/context.h"
38 #include "main/image.h"
39
40 #include "pipe/p_context.h"
41 #include "pipe/p_defines.h"
42 #include "st_context.h"
43 #include "st_cb_readpixels.h"
44 #include "st_cb_fbo.h"
45 #include "st_format.h"
46 #include "st_public.h"
47
48
49 /**
50 * Do glReadPixels by getting rows from the framebuffer surface with
51 * get_tile(). Convert to requested format/type with Mesa image routines.
52 * Image transfer ops are done in software too.
53 */
54 static void
55 st_readpixels(GLcontext *ctx, GLint x, GLint y, GLsizei width, GLsizei height,
56 GLenum format, GLenum type,
57 const struct gl_pixelstore_attrib *pack,
58 GLvoid *dest)
59 {
60 struct pipe_context *pipe = ctx->st->pipe;
61 GLfloat temp[MAX_WIDTH][4];
62 const GLbitfield transferOps = ctx->_ImageTransferState;
63 GLint i, yStep, dfStride;
64 GLfloat *df;
65 struct st_renderbuffer *strb;
66 struct gl_pixelstore_attrib clippedPacking = *pack;
67
68 /* XXX convolution not done yet */
69 assert((transferOps & IMAGE_CONVOLUTION_BIT) == 0);
70
71 /* Do all needed clipping here, so that we can forget about it later */
72 if (!_mesa_clip_readpixels(ctx, &x, &y, &width, &height, &clippedPacking)) {
73 /* The ReadPixels region is totally outside the window bounds */
74 return;
75 }
76
77 /* make sure rendering has completed */
78 pipe->flush(pipe, 0x0);
79
80 if (pack->BufferObj && pack->BufferObj->Name) {
81 /* reading into a PBO */
82
83 }
84 else {
85 /* reading into user memory/buffer */
86
87 }
88
89 strb = st_renderbuffer(ctx->ReadBuffer->_ColorReadBuffer);
90 if (!strb)
91 return;
92
93 pipe->region_map(pipe, strb->surface->region);
94
95 if (format == GL_RGBA && type == GL_FLOAT) {
96 /* write tile(row) directly into user's buffer */
97 df = (GLfloat *) _mesa_image_address2d(&clippedPacking, dest, width,
98 height, format, type, 0, 0);
99 dfStride = width * 4;
100 }
101 else {
102 /* write tile(row) into temp row buffer */
103 df = (GLfloat *) temp;
104 dfStride = 0;
105 }
106
107 /* determine bottom-to-top vs. top-to-bottom order */
108 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
109 y = strb->Base.Height - 1 - y;
110 yStep = -1;
111 }
112 else {
113 yStep = 1;
114 }
115
116 /* Do a row at a time to flip image data vertically */
117 for (i = 0; i < height; i++) {
118 strb->surface->get_tile(strb->surface, x, y, width, 1, df);
119 y += yStep;
120 df += dfStride;
121 if (!dfStride) {
122 /* convert GLfloat to user's format/type */
123 GLvoid *dst = _mesa_image_address2d(&clippedPacking, dest, width,
124 height, format, type, i, 0);
125 _mesa_pack_rgba_span_float(ctx, width, temp, format, type, dst,
126 &clippedPacking, transferOps);
127 }
128 }
129
130 pipe->region_unmap(pipe, strb->surface->region);
131 }
132
133
134 void st_init_readpixels_functions(struct dd_function_table *functions)
135 {
136 functions->ReadPixels = st_readpixels;
137 }