mesa: Use u_math.h from macros.h
[mesa.git] / src / mesa / drivers / dri / nouveau / nouveau_driver.c
1 /*
2 * Copyright (C) 2009 Francisco Jerez.
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial
15 * portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 *
25 */
26
27 #include "main/mtypes.h"
28 #include "main/fbobject.h"
29
30 #include "nouveau_driver.h"
31 #include "nouveau_context.h"
32 #include "nouveau_fbo.h"
33 #include "nouveau_util.h"
34
35 #include "drivers/common/meta.h"
36
37 const char const *nouveau_vendor_string = "Nouveau";
38
39 const char *
40 nouveau_get_renderer_string(unsigned chipset)
41 {
42 char hardware_name[32];
43 static char buffer[128];
44
45 snprintf(hardware_name, sizeof(hardware_name), "nv%02X", chipset);
46 driGetRendererString(buffer, hardware_name, 0);
47
48 return buffer;
49 }
50
51 static const GLubyte *
52 nouveau_get_string(struct gl_context *ctx, GLenum name)
53 {
54 switch (name) {
55 case GL_VENDOR:
56 return (GLubyte *)nouveau_vendor_string;
57
58 case GL_RENDERER:
59 return (GLubyte *)nouveau_get_renderer_string(context_chipset(ctx));
60 default:
61 return NULL;
62 }
63 }
64
65 static void
66 nouveau_flush(struct gl_context *ctx)
67 {
68 struct nouveau_context *nctx = to_nouveau_context(ctx);
69 struct nouveau_pushbuf *push = context_push(ctx);
70
71 PUSH_KICK(push);
72
73 if (_mesa_is_winsys_fbo(ctx->DrawBuffer) &&
74 ctx->DrawBuffer->_ColorDrawBufferIndexes[0] == BUFFER_FRONT_LEFT) {
75 __DRIscreen *screen = nctx->screen->dri_screen;
76 const __DRIdri2LoaderExtension *dri2 = screen->dri2.loader;
77 __DRIdrawable *drawable = nctx->dri_context->driDrawablePriv;
78
79 if (drawable && drawable->loaderPrivate)
80 dri2->flushFrontBuffer(drawable, drawable->loaderPrivate);
81 }
82 }
83
84 static void
85 nouveau_finish(struct gl_context *ctx)
86 {
87 struct nouveau_context *nctx = to_nouveau_context(ctx);
88 struct nouveau_pushbuf *push = context_push(ctx);
89 struct nouveau_pushbuf_refn refn =
90 { nctx->fence, NOUVEAU_BO_VRAM | NOUVEAU_BO_RDWR };
91
92 nouveau_flush(ctx);
93
94 if (!nouveau_pushbuf_space(push, 16, 0, 0) &&
95 !nouveau_pushbuf_refn(push, &refn, 1)) {
96 PUSH_DATA(push, 0);
97 PUSH_KICK(push);
98 }
99
100 nouveau_bo_wait(nctx->fence, NOUVEAU_BO_RDWR, context_client(ctx));
101 }
102
103 void
104 nouveau_clear(struct gl_context *ctx, GLbitfield buffers)
105 {
106 struct gl_framebuffer *fb = ctx->DrawBuffer;
107 int x, y, w, h;
108 int i, buf;
109
110 nouveau_validate_framebuffer(ctx);
111 get_scissors(fb, &x, &y, &w, &h);
112
113 for (i = 0; i < BUFFER_COUNT; i++) {
114 struct nouveau_surface *s;
115 unsigned mask, value;
116
117 buf = buffers & (1 << i);
118 if (!buf)
119 continue;
120
121 s = &to_nouveau_renderbuffer(
122 fb->Attachment[i].Renderbuffer)->surface;
123
124 if (buf & BUFFER_BITS_COLOR) {
125 const float *color = ctx->Color.ClearColor.f;
126
127 if (fb->Attachment[i].Renderbuffer->_BaseFormat ==
128 GL_LUMINANCE_ALPHA)
129 value = pack_la_clamp_f(
130 s->format, color[0], color[3]);
131 else
132 value = pack_rgba_clamp_f(s->format, color);
133
134 mask = pack_rgba_i(s->format, ctx->Color.ColorMask[0]);
135
136 if (mask)
137 context_drv(ctx)->surface_fill(
138 ctx, s, mask, value, x, y, w, h);
139
140 buffers &= ~buf;
141
142 } else if (buf & (BUFFER_BIT_DEPTH | BUFFER_BIT_STENCIL)) {
143 mask = pack_zs_i(s->format,
144 (buffers & BUFFER_BIT_DEPTH &&
145 ctx->Depth.Mask) ? ~0 : 0,
146 (buffers & BUFFER_BIT_STENCIL ?
147 ctx->Stencil.WriteMask[0] : 0));
148 value = pack_zs_f(s->format,
149 ctx->Depth.Clear,
150 ctx->Stencil.Clear);
151
152 if (mask)
153 context_drv(ctx)->surface_fill(
154 ctx, s, mask, value, x, y, w, h);
155
156 buffers &= ~(BUFFER_BIT_DEPTH | BUFFER_BIT_STENCIL);
157 }
158 }
159
160 if (buffers)
161 _mesa_meta_Clear(ctx, buffers);
162 }
163
164 void
165 nouveau_driver_functions_init(struct dd_function_table *functions)
166 {
167 functions->GetString = nouveau_get_string;
168 functions->Flush = nouveau_flush;
169 functions->Finish = nouveau_finish;
170 functions->Clear = nouveau_clear;
171 functions->DrawPixels = _mesa_meta_DrawPixels;
172 functions->CopyPixels = _mesa_meta_CopyPixels;
173 functions->Bitmap = _mesa_meta_Bitmap;
174 functions->BlitFramebuffer = _mesa_meta_and_swrast_BlitFramebuffer;
175 }