Merge branch 'gallium-drm-driver-drescriptor'
[mesa.git] / src / mesa / drivers / dri / common / dri_metaops.c
1 /**************************************************************************
2 *
3 * Copyright 2006 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * Copyright 2009 Intel Corporation.
5 * All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
17 * of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
23 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 *
27 **************************************************************************/
28
29 #include "main/arbprogram.h"
30 #include "main/arrayobj.h"
31 #include "main/bufferobj.h"
32 #include "main/enable.h"
33 #include "main/matrix.h"
34 #include "main/texstate.h"
35 #include "main/varray.h"
36 #include "main/viewport.h"
37 #include "program/program.h"
38 #include "dri_metaops.h"
39
40 void
41 meta_set_passthrough_transform(struct dri_metaops *meta)
42 {
43 GLcontext *ctx = meta->ctx;
44
45 meta->saved_vp_x = ctx->Viewport.X;
46 meta->saved_vp_y = ctx->Viewport.Y;
47 meta->saved_vp_width = ctx->Viewport.Width;
48 meta->saved_vp_height = ctx->Viewport.Height;
49 meta->saved_matrix_mode = ctx->Transform.MatrixMode;
50
51 meta->internal_viewport_call = GL_TRUE;
52 _mesa_Viewport(0, 0, ctx->DrawBuffer->Width, ctx->DrawBuffer->Height);
53 meta->internal_viewport_call = GL_FALSE;
54
55 _mesa_MatrixMode(GL_PROJECTION);
56 _mesa_PushMatrix();
57 _mesa_LoadIdentity();
58 _mesa_Ortho(0, ctx->DrawBuffer->Width, 0, ctx->DrawBuffer->Height, 1, -1);
59
60 _mesa_MatrixMode(GL_MODELVIEW);
61 _mesa_PushMatrix();
62 _mesa_LoadIdentity();
63 }
64
65 void
66 meta_restore_transform(struct dri_metaops *meta)
67 {
68 _mesa_MatrixMode(GL_PROJECTION);
69 _mesa_PopMatrix();
70 _mesa_MatrixMode(GL_MODELVIEW);
71 _mesa_PopMatrix();
72
73 _mesa_MatrixMode(meta->saved_matrix_mode);
74
75 meta->internal_viewport_call = GL_TRUE;
76 _mesa_Viewport(meta->saved_vp_x, meta->saved_vp_y,
77 meta->saved_vp_width, meta->saved_vp_height);
78 meta->internal_viewport_call = GL_FALSE;
79 }
80
81
82 /**
83 * Set up a vertex program to pass through the position and first texcoord
84 * for pixel path.
85 */
86 void
87 meta_set_passthrough_vertex_program(struct dri_metaops *meta)
88 {
89 GLcontext *ctx = meta->ctx;
90 static const char *vp =
91 "!!ARBvp1.0\n"
92 "TEMP vertexClip;\n"
93 "DP4 vertexClip.x, state.matrix.mvp.row[0], vertex.position;\n"
94 "DP4 vertexClip.y, state.matrix.mvp.row[1], vertex.position;\n"
95 "DP4 vertexClip.z, state.matrix.mvp.row[2], vertex.position;\n"
96 "DP4 vertexClip.w, state.matrix.mvp.row[3], vertex.position;\n"
97 "MOV result.position, vertexClip;\n"
98 "MOV result.texcoord[0], vertex.texcoord[0];\n"
99 "MOV result.color, vertex.color;\n"
100 "END\n";
101
102 assert(meta->saved_vp == NULL);
103
104 _mesa_reference_vertprog(ctx, &meta->saved_vp,
105 ctx->VertexProgram.Current);
106 if (meta->passthrough_vp == NULL) {
107 GLuint prog_name;
108 _mesa_GenPrograms(1, &prog_name);
109 _mesa_BindProgram(GL_VERTEX_PROGRAM_ARB, prog_name);
110 _mesa_ProgramStringARB(GL_VERTEX_PROGRAM_ARB,
111 GL_PROGRAM_FORMAT_ASCII_ARB,
112 strlen(vp), (const GLubyte *)vp);
113 _mesa_reference_vertprog(ctx, &meta->passthrough_vp,
114 ctx->VertexProgram.Current);
115 _mesa_DeletePrograms(1, &prog_name);
116 }
117
118 FLUSH_VERTICES(ctx, _NEW_PROGRAM);
119 _mesa_reference_vertprog(ctx, &ctx->VertexProgram.Current,
120 meta->passthrough_vp);
121 ctx->Driver.BindProgram(ctx, GL_VERTEX_PROGRAM_ARB,
122 &meta->passthrough_vp->Base);
123
124 meta->saved_vp_enable = ctx->VertexProgram.Enabled;
125 _mesa_Enable(GL_VERTEX_PROGRAM_ARB);
126 }
127
128 /**
129 * Restores the previous vertex program after
130 * meta_set_passthrough_vertex_program()
131 */
132 void
133 meta_restore_vertex_program(struct dri_metaops *meta)
134 {
135 GLcontext *ctx = meta->ctx;
136
137 FLUSH_VERTICES(ctx, _NEW_PROGRAM);
138 _mesa_reference_vertprog(ctx, &ctx->VertexProgram.Current,
139 meta->saved_vp);
140 _mesa_reference_vertprog(ctx, &meta->saved_vp, NULL);
141 ctx->Driver.BindProgram(ctx, GL_VERTEX_PROGRAM_ARB,
142 &ctx->VertexProgram.Current->Base);
143
144 if (!meta->saved_vp_enable)
145 _mesa_Disable(GL_VERTEX_PROGRAM_ARB);
146 }
147
148 /**
149 * Binds the given program string to GL_FRAGMENT_PROGRAM_ARB, caching the
150 * program object.
151 */
152 void
153 meta_set_fragment_program(struct dri_metaops *meta,
154 struct gl_fragment_program **prog,
155 const char *prog_string)
156 {
157 GLcontext *ctx = meta->ctx;
158 assert(meta->saved_fp == NULL);
159
160 _mesa_reference_fragprog(ctx, &meta->saved_fp,
161 ctx->FragmentProgram.Current);
162 if (*prog == NULL) {
163 GLuint prog_name;
164 _mesa_GenPrograms(1, &prog_name);
165 _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB, prog_name);
166 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB,
167 GL_PROGRAM_FORMAT_ASCII_ARB,
168 strlen(prog_string), (const GLubyte *)prog_string);
169 _mesa_reference_fragprog(ctx, prog, ctx->FragmentProgram.Current);
170 /* Note that DeletePrograms unbinds the program on us */
171 _mesa_DeletePrograms(1, &prog_name);
172 }
173
174 FLUSH_VERTICES(ctx, _NEW_PROGRAM);
175 _mesa_reference_fragprog(ctx, &ctx->FragmentProgram.Current, *prog);
176 ctx->Driver.BindProgram(ctx, GL_FRAGMENT_PROGRAM_ARB, &((*prog)->Base));
177
178 meta->saved_fp_enable = ctx->FragmentProgram.Enabled;
179 _mesa_Enable(GL_FRAGMENT_PROGRAM_ARB);
180 }
181
182 /**
183 * Restores the previous fragment program after
184 * meta_set_fragment_program()
185 */
186 void
187 meta_restore_fragment_program(struct dri_metaops *meta)
188 {
189 GLcontext *ctx = meta->ctx;
190
191 FLUSH_VERTICES(ctx, _NEW_PROGRAM);
192 _mesa_reference_fragprog(ctx, &ctx->FragmentProgram.Current,
193 meta->saved_fp);
194 _mesa_reference_fragprog(ctx, &meta->saved_fp, NULL);
195 ctx->Driver.BindProgram(ctx, GL_FRAGMENT_PROGRAM_ARB,
196 &ctx->FragmentProgram.Current->Base);
197
198 if (!meta->saved_fp_enable)
199 _mesa_Disable(GL_FRAGMENT_PROGRAM_ARB);
200 }
201
202 static const float default_texcoords[4][2] = { { 0.0, 0.0 },
203 { 1.0, 0.0 },
204 { 1.0, 1.0 },
205 { 0.0, 1.0 } };
206
207 void
208 meta_set_default_texrect(struct dri_metaops *meta)
209 {
210 GLcontext *ctx = meta->ctx;
211 struct gl_client_array *old_texcoord_array;
212
213 meta->saved_active_texture = ctx->Texture.CurrentUnit;
214 if (meta->saved_array_vbo == NULL) {
215 _mesa_reference_buffer_object(ctx, &meta->saved_array_vbo,
216 ctx->Array.ArrayBufferObj);
217 }
218
219 old_texcoord_array = &ctx->Array.ArrayObj->TexCoord[0];
220 meta->saved_texcoord_type = old_texcoord_array->Type;
221 meta->saved_texcoord_size = old_texcoord_array->Size;
222 meta->saved_texcoord_stride = old_texcoord_array->Stride;
223 meta->saved_texcoord_enable = old_texcoord_array->Enabled;
224 meta->saved_texcoord_ptr = old_texcoord_array->Ptr;
225 _mesa_reference_buffer_object(ctx, &meta->saved_texcoord_vbo,
226 old_texcoord_array->BufferObj);
227
228 _mesa_ClientActiveTextureARB(GL_TEXTURE0);
229
230 if (meta->texcoord_vbo == NULL) {
231 GLuint vbo_name;
232
233 _mesa_GenBuffersARB(1, &vbo_name);
234 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, vbo_name);
235 _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(default_texcoords),
236 default_texcoords, GL_STATIC_DRAW_ARB);
237 _mesa_reference_buffer_object(ctx, &meta->texcoord_vbo,
238 ctx->Array.ArrayBufferObj);
239 } else {
240 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB,
241 meta->texcoord_vbo->Name);
242 }
243 _mesa_TexCoordPointer(2, GL_FLOAT, 2 * sizeof(GLfloat), NULL);
244
245 _mesa_Enable(GL_TEXTURE_COORD_ARRAY);
246 }
247
248 void
249 meta_restore_texcoords(struct dri_metaops *meta)
250 {
251 GLcontext *ctx = meta->ctx;
252
253 /* Restore the old TexCoordPointer */
254 if (meta->saved_texcoord_vbo) {
255 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB,
256 meta->saved_texcoord_vbo->Name);
257 _mesa_reference_buffer_object(ctx, &meta->saved_texcoord_vbo, NULL);
258 } else {
259 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
260 }
261
262 _mesa_TexCoordPointer(meta->saved_texcoord_size,
263 meta->saved_texcoord_type,
264 meta->saved_texcoord_stride,
265 meta->saved_texcoord_ptr);
266 if (!meta->saved_texcoord_enable)
267 _mesa_Disable(GL_TEXTURE_COORD_ARRAY);
268
269 _mesa_ClientActiveTextureARB(GL_TEXTURE0 +
270 meta->saved_active_texture);
271
272 if (meta->saved_array_vbo) {
273 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB,
274 meta->saved_array_vbo->Name);
275 _mesa_reference_buffer_object(ctx, &meta->saved_array_vbo, NULL);
276 } else {
277 _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
278 }
279 }
280
281
282 void meta_init_metaops(GLcontext *ctx, struct dri_metaops *meta)
283 {
284 meta->ctx = ctx;
285 }
286
287 void meta_destroy_metaops(struct dri_metaops *meta)
288 {
289
290 }