1135817fe9a9da450f4659b91ccc820e5e37b66c
[mesa.git] / src / mesa / drivers / dri / nouveau / nouveau_driver.c
1 /**************************************************************************
2
3 Copyright 2006 Stephane Marchesin
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 "Software"),
8 to deal in the Software without restriction, including without limitation
9 on the rights to use, copy, modify, merge, publish, distribute, sub
10 license, and/or sell copies of the Software, and to permit persons to whom
11 the Software is furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice (including the next
14 paragraph) shall be included in all copies or substantial portions of the
15 Software.
16
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
20 ERIC ANHOLT OR SILICON INTEGRATED SYSTEMS CORP BE LIABLE FOR ANY CLAIM,
21 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
22 OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
23 USE OR OTHER DEALINGS IN THE SOFTWARE.
24
25 **************************************************************************/
26
27 #include "nouveau_context.h"
28 //#include "nouveau_state.h"
29 #include "nouveau_lock.h"
30 #include "nouveau_fifo.h"
31 #include "nouveau_driver.h"
32 #include "swrast/swrast.h"
33
34 #include "context.h"
35 #include "framebuffer.h"
36
37 #include "utils.h"
38 #include "colormac.h"
39
40 /* Wrapper for DRM_NOUVEAU_GETPARAM ioctl */
41 GLboolean nouveauDRMGetParam(nouveauContextPtr nmesa,
42 unsigned int param,
43 uint64_t* value)
44 {
45 struct drm_nouveau_getparam getp;
46
47 getp.param = param;
48 if (!value || drmCommandWriteRead(nmesa->driFd, DRM_NOUVEAU_GETPARAM,
49 &getp, sizeof(getp)))
50 return GL_FALSE;
51 *value = getp.value;
52 return GL_TRUE;
53 }
54
55 /* Wrapper for DRM_NOUVEAU_GETPARAM ioctl */
56 GLboolean nouveauDRMSetParam(nouveauContextPtr nmesa,
57 unsigned int param,
58 uint64_t value)
59 {
60 struct drm_nouveau_setparam setp;
61
62 setp.param = param;
63 setp.value = value;
64 if (drmCommandWrite(nmesa->driFd, DRM_NOUVEAU_SETPARAM, &setp,
65 sizeof(setp)))
66 return GL_FALSE;
67 return GL_TRUE;
68 }
69
70 /* Return the width and height of the current color buffer */
71 static void nouveauGetBufferSize( GLframebuffer *buffer,
72 GLuint *width, GLuint *height )
73 {
74 GET_CURRENT_CONTEXT(ctx);
75 nouveauContextPtr nmesa = NOUVEAU_CONTEXT(ctx);
76
77 LOCK_HARDWARE( nmesa );
78 *width = nmesa->driDrawable->w;
79 *height = nmesa->driDrawable->h;
80 UNLOCK_HARDWARE( nmesa );
81 }
82
83 /* glGetString */
84 static const GLubyte *nouveauGetString( GLcontext *ctx, GLenum name )
85 {
86 nouveauContextPtr nmesa = NOUVEAU_CONTEXT(ctx);
87 static char buffer[128];
88 const char * card_name = "Unknown";
89 GLuint agp_mode = 0;
90
91 switch ( name ) {
92 case GL_VENDOR:
93 return (GLubyte *)DRIVER_AUTHOR;
94
95 case GL_RENDERER:
96 card_name=nmesa->screen->card->name;
97
98 switch(nmesa->screen->bus_type)
99 {
100 case NV_PCI:
101 case NV_PCIE:
102 default:
103 agp_mode=0;
104 break;
105 case NV_AGP:
106 agp_mode=nmesa->screen->agp_mode;
107 break;
108 }
109 driGetRendererString( buffer, card_name, DRIVER_DATE,
110 agp_mode );
111 return (GLubyte *)buffer;
112 default:
113 return NULL;
114 }
115 }
116
117 /* glFlush */
118 static void nouveauFlush( GLcontext *ctx )
119 {
120 nouveauContextPtr nmesa = NOUVEAU_CONTEXT(ctx);
121
122 if (ctx->DrawBuffer->_ColorDrawBufferMask[0] == BUFFER_BIT_FRONT_LEFT)
123 nouveauDoSwapBuffers(nmesa, nmesa->driDrawable);
124 FIRE_RING();
125 }
126
127 /* glFinish */
128 static void nouveauFinish( GLcontext *ctx )
129 {
130 nouveauContextPtr nmesa = NOUVEAU_CONTEXT(ctx);
131
132 nouveauFlush( ctx );
133 nouveauWaitForIdle( nmesa );
134 }
135
136 /* glClear */
137 static void nouveauClear( GLcontext *ctx, GLbitfield mask )
138 {
139 uint32_t clear_value;
140 nouveauContextPtr nmesa = NOUVEAU_CONTEXT(ctx);
141
142 /* FIXME: should we clear front buffer, even if asked to do it? */
143 if (mask & (BUFFER_BIT_FRONT_LEFT|BUFFER_BIT_BACK_LEFT)) {
144 GLubyte c[4];
145 int color_bits = 32;
146 int color_mask = 0xffffffff;
147
148 UNCLAMPED_FLOAT_TO_RGBA_CHAN(c,ctx->Color.ClearColor);
149 clear_value = PACK_COLOR_8888(c[3],c[0],c[1],c[2]);
150
151 if (ctx->DrawBuffer) {
152 /* FIXME: find correct color buffer, instead of [0] */
153 if (ctx->DrawBuffer->_ColorDrawBuffers[0]) {
154 color_bits = ctx->DrawBuffer->_ColorDrawBuffers[0]->RedBits;
155 color_bits += ctx->DrawBuffer->_ColorDrawBuffers[0]->GreenBits;
156 color_bits += ctx->DrawBuffer->_ColorDrawBuffers[0]->BlueBits;
157 color_bits += ctx->DrawBuffer->_ColorDrawBuffers[0]->AlphaBits;
158 }
159 }
160
161 if (color_bits<24) {
162 clear_value = PACK_COLOR_565(c[0],c[1],c[2]);
163 color_mask = 0xffff;
164 }
165
166 nouveauClearBuffer(ctx, nmesa->color_buffer,
167 clear_value, color_mask);
168 }
169
170 if (mask & (BUFFER_BIT_DEPTH)) {
171 int depth_bits = 24;
172 int depth_mask;
173 if (ctx->DrawBuffer) {
174 if (ctx->DrawBuffer->_DepthBuffer) {
175 depth_bits = ctx->DrawBuffer->_DepthBuffer->DepthBits;
176 }
177 }
178
179 switch(depth_bits) {
180 case 16:
181 clear_value = (uint32_t) (ctx->Depth.Clear * 32767.0);
182 depth_mask = 0xffff;
183 break;
184 default:
185 clear_value = ((uint32_t) (ctx->Depth.Clear * 16777215.0)) << 8;
186 depth_mask = 0xffffff00;
187 break;
188 }
189
190 nouveauClearBuffer(ctx, nmesa->depth_buffer,
191 clear_value, depth_mask);
192 }
193
194 if (mask & (BUFFER_BIT_STENCIL)) {
195 int stencil_bits = 0;
196 if (ctx->DrawBuffer) {
197 if (ctx->DrawBuffer->_StencilBuffer) {
198 stencil_bits = ctx->DrawBuffer->_StencilBuffer->StencilBits;
199 }
200 }
201
202 if (stencil_bits>0) {
203 nouveauClearBuffer(ctx, nmesa->depth_buffer,
204 ctx->Stencil.Clear, (1<<stencil_bits)-1);
205 }
206 }
207 }
208
209 void nouveauDriverInitFunctions( struct dd_function_table *functions )
210 {
211 functions->GetBufferSize = nouveauGetBufferSize;
212 functions->ResizeBuffers = _mesa_resize_framebuffer;
213 functions->GetString = nouveauGetString;
214 functions->Flush = nouveauFlush;
215 functions->Finish = nouveauFinish;
216 functions->Clear = nouveauClear;
217 }
218