Merge remote branch 'upstream/gallium-0.1' into gallium-0.1
[mesa.git] / src / gallium / drivers / nv10 / nv10_state.c
index 1ff01de1060b0aba001a80b02109098600ed8a24..11664fae2afa51ede43f7bd9024d3a7ef3b9770b 100644 (file)
@@ -1,3 +1,4 @@
+#include "draw/draw_context.h"
 #include "pipe/p_state.h"
 #include "pipe/p_defines.h"
 #include "pipe/p_util.h"
@@ -7,36 +8,6 @@
 #include "nv10_context.h"
 #include "nv10_state.h"
 
-static void nv10_vertex_layout(struct pipe_context* pipe)
-{
-       struct nv10_context *nv10 = nv10_context(pipe);
-       struct nv10_fragment_program *fp = nv10->fragprog.current;
-       uint32_t src = 0;
-       int i;
-       struct vertex_info vinfo;
-
-       memset(&vinfo, 0, sizeof(vinfo));
-
-       for (i = 0; i < fp->info.num_inputs; i++) {
-               switch (fp->info.input_semantic_name[i]) {
-                       case TGSI_SEMANTIC_POSITION:
-                               draw_emit_vertex_attr(&vinfo, EMIT_4F, INTERP_LINEAR, src++);
-                               break;
-                       case TGSI_SEMANTIC_COLOR:
-                               draw_emit_vertex_attr(&vinfo, EMIT_4F, INTERP_LINEAR, src++);
-                               break;
-                       default:
-                       case TGSI_SEMANTIC_GENERIC:
-                               draw_emit_vertex_attr(&vinfo, EMIT_4F, INTERP_PERSPECTIVE, src++);
-                               break;
-                       case TGSI_SEMANTIC_FOG:
-                               draw_emit_vertex_attr(&vinfo, EMIT_4F, INTERP_PERSPECTIVE, src++);
-                               break;
-               }
-       }
-       draw_compute_vertex_size(&vinfo);
-}
-
 static void *
 nv10_blend_state_create(struct pipe_context *pipe,
                        const struct pipe_blend_state *cso)
@@ -62,27 +33,19 @@ nv10_blend_state_create(struct pipe_context *pipe,
 }
 
 static void
-nv10_blend_state_bind(struct pipe_context *pipe, void *hwcso)
+nv10_blend_state_bind(struct pipe_context *pipe, void *blend)
 {
        struct nv10_context *nv10 = nv10_context(pipe);
-       struct nv10_blend_state *cb = hwcso;
-
-       BEGIN_RING(celsius, NV10TCL_DITHER_ENABLE, 1);
-       OUT_RING  (cb->d_enable);
 
-       BEGIN_RING(celsius, NV10TCL_BLEND_FUNC_ENABLE, 3);
-       OUT_RING  (cb->b_enable);
-       OUT_RING  (cb->b_srcfunc);
-       OUT_RING  (cb->b_dstfunc);
+       nv10->blend = (struct nv10_blend_state*)blend;
 
-       BEGIN_RING(celsius, NV10TCL_COLOR_MASK, 1);
-       OUT_RING  (cb->c_mask);
+       nv10->dirty |= NV10_NEW_BLEND;
 }
 
 static void
 nv10_blend_state_delete(struct pipe_context *pipe, void *hwcso)
 {
-       free(hwcso);
+       FREE(hwcso);
 }
 
 
@@ -255,7 +218,7 @@ nv10_sampler_state_bind(struct pipe_context *pipe, unsigned nr, void **sampler)
 static void
 nv10_sampler_state_delete(struct pipe_context *pipe, void *hwcso)
 {
-       free(hwcso);
+       FREE(hwcso);
 }
 
 static void
@@ -288,6 +251,8 @@ nv10_rasterizer_state_create(struct pipe_context *pipe,
         */
        rs = malloc(sizeof(struct nv10_rasterizer_state));
 
+       rs->templ = cso;
+       
        rs->shade_model = cso->flatshade ? 0x1d00 : 0x1d01;
 
        rs->line_width = (unsigned char)(cso->line_width * 8.0) & 0xff;
@@ -347,43 +312,21 @@ nv10_rasterizer_state_create(struct pipe_context *pipe,
 }
 
 static void
-nv10_rasterizer_state_bind(struct pipe_context *pipe, void *hwcso)
+nv10_rasterizer_state_bind(struct pipe_context *pipe, void *rast)
 {
        struct nv10_context *nv10 = nv10_context(pipe);
-       struct nv10_rasterizer_state *rs = hwcso;
-
-       BEGIN_RING(celsius, NV10TCL_SHADE_MODEL, 2);
-       OUT_RING  (rs->shade_model);
-       OUT_RING  (rs->line_width);
-
 
-       BEGIN_RING(celsius, NV10TCL_POINT_SIZE, 1);
-       OUT_RING  (rs->point_size);
+       nv10->rast = (struct nv10_rasterizer_state*)rast;
 
-       BEGIN_RING(celsius, NV10TCL_POLYGON_MODE_FRONT, 2);
-       OUT_RING  (rs->poly_mode_front);
-       OUT_RING  (rs->poly_mode_back);
+       draw_set_rasterizer_state(nv10->draw, (nv10->rast ? nv10->rast->templ : NULL));
 
-
-       BEGIN_RING(celsius, NV10TCL_CULL_FACE, 2);
-       OUT_RING  (rs->cull_face);
-       OUT_RING  (rs->front_face);
-
-       BEGIN_RING(celsius, NV10TCL_LINE_SMOOTH_ENABLE, 2);
-       OUT_RING  (rs->line_smooth_en);
-       OUT_RING  (rs->poly_smooth_en);
-
-       BEGIN_RING(celsius, NV10TCL_CULL_FACE_ENABLE, 1);
-       OUT_RING  (rs->cull_face_en);
-
-/*     BEGIN_RING(celsius, NV10TCL_POINT_SPRITE, 1);
-       OUT_RING  (rs->point_sprite);*/
+       nv10->dirty |= NV10_NEW_RAST;
 }
 
 static void
 nv10_rasterizer_state_delete(struct pipe_context *pipe, void *hwcso)
 {
-       free(hwcso);
+       FREE(hwcso);
 }
 
 static void *
@@ -415,57 +358,46 @@ nv10_depth_stencil_alpha_state_create(struct pipe_context *pipe,
 }
 
 static void
-nv10_depth_stencil_alpha_state_bind(struct pipe_context *pipe, void *hwcso)
+nv10_depth_stencil_alpha_state_bind(struct pipe_context *pipe, void *dsa)
 {
        struct nv10_context *nv10 = nv10_context(pipe);
-       struct nv10_depth_stencil_alpha_state *hw = hwcso;
-
-       BEGIN_RING(celsius, NV10TCL_DEPTH_FUNC, 3);
-       OUT_RINGp ((uint32_t *)&hw->depth, 3);
-       BEGIN_RING(celsius, NV10TCL_STENCIL_ENABLE, 1);
-       OUT_RING (hw->stencil.enable);
-       BEGIN_RING(celsius, NV10TCL_STENCIL_MASK, 7);
-       OUT_RINGp ((uint32_t *)&(hw->stencil.wmask), 7);
-       BEGIN_RING(celsius, NV10TCL_ALPHA_FUNC_ENABLE, 3);
-       OUT_RINGp ((uint32_t *)&hw->alpha.enabled, 3);
+
+       nv10->dsa = (struct nv10_depth_stencil_alpha_state*)dsa;
+
+       nv10->dirty |= NV10_NEW_DSA;
 }
 
 static void
 nv10_depth_stencil_alpha_state_delete(struct pipe_context *pipe, void *hwcso)
 {
-       free(hwcso);
+       FREE(hwcso);
 }
 
 static void *
 nv10_vp_state_create(struct pipe_context *pipe,
-                    const struct pipe_shader_state *cso)
+                    const struct pipe_shader_state *templ)
 {
-       struct nv10_vertex_program *vp;
-
-       vp = CALLOC(1, sizeof(struct nv10_vertex_program));
-       vp->pipe = cso;
+       struct nv10_context *nv10 = nv10_context(pipe);
 
-       return (void *)vp;
+       return draw_create_vertex_shader(nv10->draw, templ);
 }
 
 static void
-nv10_vp_state_bind(struct pipe_context *pipe, void *hwcso)
+nv10_vp_state_bind(struct pipe_context *pipe, void *shader)
 {
        struct nv10_context *nv10 = nv10_context(pipe);
-       struct nv10_vertex_program *vp = hwcso;
 
-       nv10->vertprog.current = vp;
+       draw_bind_vertex_shader(nv10->draw, (struct draw_vertex_shader *) shader);
+
        nv10->dirty |= NV10_NEW_VERTPROG;
 }
 
 static void
-nv10_vp_state_delete(struct pipe_context *pipe, void *hwcso)
+nv10_vp_state_delete(struct pipe_context *pipe, void *shader)
 {
        struct nv10_context *nv10 = nv10_context(pipe);
-       struct nv10_vertex_program *vp = hwcso;
 
-       //nv10_vertprog_destroy(nv10, vp);
-       free(vp);
+       draw_delete_vertex_shader(nv10->draw, (struct draw_vertex_shader *) shader);
 }
 
 static void *
@@ -499,7 +431,7 @@ nv10_fp_state_delete(struct pipe_context *pipe, void *hwcso)
        struct nv10_fragment_program *fp = hwcso;
 
        nv10_fragprog_destroy(nv10, fp);
-       free(fp);
+       FREE(fp);
 }
 
 static void
@@ -508,17 +440,18 @@ nv10_set_blend_color(struct pipe_context *pipe,
 {
        struct nv10_context *nv10 = nv10_context(pipe);
 
-       BEGIN_RING(celsius, NV10TCL_BLEND_COLOR, 1);
-       OUT_RING  ((float_to_ubyte(bcol->color[3]) << 24) |
-                  (float_to_ubyte(bcol->color[0]) << 16) |
-                  (float_to_ubyte(bcol->color[1]) <<  8) |
-                  (float_to_ubyte(bcol->color[2]) <<  0));
+       nv10->blend_color = (struct pipe_blend_color*)bcol;
+
+       nv10->dirty |= NV10_NEW_BLENDCOL;
 }
 
 static void
 nv10_set_clip_state(struct pipe_context *pipe,
                    const struct pipe_clip_state *clip)
 {
+       struct nv10_context *nv10 = nv10_context(pipe);
+
+       draw_set_clip_state(nv10->draw, clip);
 }
 
 static void
@@ -526,14 +459,20 @@ nv10_set_constant_buffer(struct pipe_context *pipe, uint shader, uint index,
                         const struct pipe_constant_buffer *buf )
 {
        struct nv10_context *nv10 = nv10_context(pipe);
-
-       if (shader == PIPE_SHADER_VERTEX) {
-               nv10->vertprog.constant_buf = buf->buffer;
-               nv10->dirty |= NV10_NEW_VERTPROG;
-       } else
-       if (shader == PIPE_SHADER_FRAGMENT) {
-               nv10->fragprog.constant_buf = buf->buffer;
-               nv10->dirty |= NV10_NEW_FRAGPROG;
+       struct pipe_winsys *ws = pipe->winsys;
+
+       assert(shader < PIPE_SHADER_TYPES);
+       assert(index == 0);
+
+       if (buf) {
+               void *mapped;
+               if (buf->size && (mapped = ws->buffer_map(ws, buf->buffer, PIPE_BUFFER_USAGE_CPU_READ)))
+               {
+                       memcpy(nv10->constbuf[shader], mapped, buf->size);
+                       nv10->constbuf_nr[shader] =
+                               buf->size / (4 * sizeof(float));
+                       ws->buffer_unmap(ws, buf->buffer);
+               }
        }
 }
 
@@ -542,58 +481,10 @@ nv10_set_framebuffer_state(struct pipe_context *pipe,
                           const struct pipe_framebuffer_state *fb)
 {
        struct nv10_context *nv10 = nv10_context(pipe);
-       struct pipe_surface *rt, *zeta;
-       uint32_t rt_format, w, h;
-       int i, colour_format = 0, zeta_format = 0;
-
-       w = fb->cbufs[0]->width;
-       h = fb->cbufs[0]->height;
-       colour_format = fb->cbufs[0]->format;
-       rt = fb->cbufs[0];
-
-       if (fb->zsbuf) {
-               if (colour_format) {
-                       assert(w == fb->zsbuf->width);
-                       assert(h == fb->zsbuf->height);
-               } else {
-                       w = fb->zsbuf->width;
-                       h = fb->zsbuf->height;
-               }
 
-               zeta_format = fb->zsbuf->format;
-               zeta = fb->zsbuf;
-       }
-
-       rt_format = NV10TCL_RT_FORMAT_TYPE_LINEAR;
+       nv10->framebuffer = (struct pipe_framebuffer_state*)fb;
 
-       switch (colour_format) {
-       case PIPE_FORMAT_A8R8G8B8_UNORM:
-       case 0:
-               rt_format |= NV10TCL_RT_FORMAT_COLOR_A8R8G8B8;
-               break;
-       case PIPE_FORMAT_R5G6B5_UNORM:
-               rt_format |= NV10TCL_RT_FORMAT_COLOR_R5G6B5;
-               break;
-       default:
-               assert(0);
-       }
-
-       BEGIN_RING(celsius, NV10TCL_RT_PITCH, 1);
-       OUT_RING  ( (rt->pitch * rt->cpp) | ( (zeta->pitch * zeta->cpp) << 16) );
-       nv10->rt[0] = rt->buffer;
-
-       if (zeta_format)
-       {
-               nv10->zeta = zeta->buffer;
-       }
-
-       BEGIN_RING(celsius, NV10TCL_RT_HORIZ, 3);
-       OUT_RING  ((w << 16) | 0);
-       OUT_RING  ((h << 16) | 0);
-       OUT_RING  (rt_format);
-       BEGIN_RING(celsius, NV10TCL_VIEWPORT_CLIP_HORIZ(0), 2);
-       OUT_RING  (((w - 1) << 16) | 0);
-       OUT_RING  (((h - 1) << 16) | 0);
+       nv10->dirty |= NV10_NEW_FRAMEBUFFER;
 }
 
 static void
@@ -609,10 +500,9 @@ nv10_set_scissor_state(struct pipe_context *pipe,
 {
        struct nv10_context *nv10 = nv10_context(pipe);
 
-       // XXX
-/*     BEGIN_RING(celsius, NV10TCL_SCISSOR_HORIZ, 2);
-       OUT_RING  (((s->maxx - s->minx) << 16) | s->minx);
-       OUT_RING  (((s->maxy - s->miny) << 16) | s->miny);*/
+       nv10->scissor = (struct pipe_scissor_state*)s;
+
+       nv10->dirty |= NV10_NEW_SCISSOR;
 }
 
 static void
@@ -621,15 +511,11 @@ nv10_set_viewport_state(struct pipe_context *pipe,
 {
        struct nv10_context *nv10 = nv10_context(pipe);
 
-/*     OUT_RINGf (vpt->translate[0]);
-       OUT_RINGf (vpt->translate[1]);
-       OUT_RINGf (vpt->translate[2]);
-       OUT_RINGf (vpt->translate[3]);*/
-       BEGIN_RING(celsius, NV10TCL_VIEWPORT_SCALE_X, 4);
-       OUT_RINGf (vpt->scale[0]);
-       OUT_RINGf (vpt->scale[1]);
-       OUT_RINGf (vpt->scale[2]);
-       OUT_RINGf (vpt->scale[3]);
+       nv10->viewport = (struct pipe_viewport_state*)vpt;
+
+       draw_set_viewport_state(nv10->draw, nv10->viewport);
+
+       nv10->dirty |= NV10_NEW_VIEWPORT;
 }
 
 static void
@@ -639,7 +525,9 @@ nv10_set_vertex_buffers(struct pipe_context *pipe, unsigned count,
        struct nv10_context *nv10 = nv10_context(pipe);
 
        memcpy(nv10->vtxbuf, vb, sizeof(*vb) * count);
-       nv10->dirty |= NV10_NEW_ARRAYS;
+       nv10->dirty |= NV10_NEW_VTXARRAYS;
+
+       draw_set_vertex_buffers(nv10->draw, count, vb);
 }
 
 static void
@@ -649,7 +537,9 @@ nv10_set_vertex_elements(struct pipe_context *pipe, unsigned count,
        struct nv10_context *nv10 = nv10_context(pipe);
 
        memcpy(nv10->vtxelt, ve, sizeof(*ve) * count);
-       nv10->dirty |= NV10_NEW_ARRAYS;
+       nv10->dirty |= NV10_NEW_VTXARRAYS;
+
+       draw_set_vertex_elements(nv10->draw, count, ve);
 }
 
 void