draw_pt_emit.c revision cb9f0a589623397c3437911aeef39f189213527a
1c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell/**************************************************************************
2c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell *
3c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
4c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell * All Rights Reserved.
5c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell *
6c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell * Permission is hereby granted, free of charge, to any person obtaining a
7c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell * copy of this software and associated documentation files (the
8c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell * "Software"), to deal in the Software without restriction, including
9c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell * without limitation the rights to use, copy, modify, merge, publish,
10c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell * distribute, sub license, and/or sell copies of the Software, and to
11c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell * permit persons to whom the Software is furnished to do so, subject to
12c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell * the following conditions:
13c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell *
14c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell * The above copyright notice and this permission notice (including the
15c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell * next paragraph) shall be included in all copies or substantial portions
16c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell * of the Software.
17c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell *
18c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell *
26c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell **************************************************************************/
27c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell
28c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell#include "pipe/p_util.h"
29c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell#include "draw/draw_context.h"
30c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell#include "draw/draw_private.h"
31c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell#include "draw/draw_vbuf.h"
32c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell#include "draw/draw_vertex.h"
33c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell#include "draw/draw_pt.h"
34c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell#include "translate/translate.h"
35c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell
36c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell
37c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwellstruct pt_emit {
38c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell   struct draw_context *draw;
39c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell
40c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell   struct translate *translate;
41c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell};
42c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell
43c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell
44c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwellvoid draw_pt_emit_prepare( struct pt_emit *emit,
45a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell			   unsigned prim )
46c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell{
47c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell   struct draw_context *draw = emit->draw;
48c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell   const struct vertex_info *vinfo;
49c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell   unsigned dst_offset;
50c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell   struct translate_key hw_key;
51c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell   unsigned i;
52c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell   boolean ok;
53c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell
54c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell   ok = draw->render->set_primitive(draw->render, prim);
55c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell   if (!ok) {
56c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell      assert(0);
57c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell      return;
58c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell   }
59c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell
60c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell   /* Must do this after set_primitive() above:
61c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell    */
62c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell   vinfo = draw->render->get_vertex_info(draw->render);
63c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell
64c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell
65cb9f0a589623397c3437911aeef39f189213527aKeith Whitwell   /* Translate from pipeline vertices to hw vertices.
66c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell    */
67c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell   dst_offset = 0;
68c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell   for (i = 0; i < vinfo->num_attribs; i++) {
69c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell      unsigned emit_sz = 0;
70c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell      unsigned src_buffer = 0;
71c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell      unsigned output_format;
72a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell      unsigned src_offset = (vinfo->src_index[i] * 4 * sizeof(float) );
73c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell
74c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell
75c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell
76c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell      switch (vinfo->emit[i]) {
77c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell      case EMIT_4F:
78c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell	 output_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
79c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell	 emit_sz = 4 * sizeof(float);
80c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell	 break;
81c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell      case EMIT_3F:
82c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell	 output_format = PIPE_FORMAT_R32G32B32_FLOAT;
83c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell	 emit_sz = 3 * sizeof(float);
84c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell	 break;
85c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell      case EMIT_2F:
86c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell	 output_format = PIPE_FORMAT_R32G32_FLOAT;
87c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell	 emit_sz = 2 * sizeof(float);
88c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell	 break;
89c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell      case EMIT_1F:
90c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell	 output_format = PIPE_FORMAT_R32_FLOAT;
91c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell	 emit_sz = 1 * sizeof(float);
92c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell	 break;
93c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell      case EMIT_1F_PSIZE:
94c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell	 output_format = PIPE_FORMAT_R32_FLOAT;
95c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell	 emit_sz = 1 * sizeof(float);
96c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell	 src_buffer = 1;
97c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell	 src_offset = 0;
98c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell	 break;
99c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell      case EMIT_4UB:
100c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell	 output_format = PIPE_FORMAT_B8G8R8A8_UNORM;
101c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell	 emit_sz = 4 * sizeof(ubyte);
102c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell      default:
103c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell	 assert(0);
104c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell	 output_format = PIPE_FORMAT_NONE;
105c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell	 emit_sz = 0;
106c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell	 break;
107c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell      }
108c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell
109c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell      hw_key.element[i].input_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
110c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell      hw_key.element[i].input_buffer = src_buffer;
111c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell      hw_key.element[i].input_offset = src_offset;
112c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell      hw_key.element[i].output_format = output_format;
113c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell      hw_key.element[i].output_offset = dst_offset;
114c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell
115c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell      dst_offset += emit_sz;
116c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell   }
117c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell
118c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell   hw_key.nr_elements = vinfo->num_attribs;
119c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell   hw_key.output_stride = vinfo->size * 4;
120c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell
121c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell   /* Don't bother with caching at this stage:
122c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell    */
123c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell   if (!emit->translate ||
124c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell       memcmp(&emit->translate->key, &hw_key, sizeof(hw_key)) != 0)
125c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell   {
126c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell      if (emit->translate)
127c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell	 emit->translate->release(emit->translate);
128c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell
1297400bc4b6fb0c20a935cd108afa92814eeafec6dKeith Whitwell      emit->translate = translate_create( &hw_key );
130c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell   }
131c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell}
132c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell
133c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell
134c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwellvoid draw_pt_emit( struct pt_emit *emit,
135a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell		   const float (*vertex_data)[4],
136c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell		   unsigned vertex_count,
137a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell		   unsigned stride,
138c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell		   const ushort *elts,
139c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell		   unsigned count )
140c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell{
141c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell   struct draw_context *draw = emit->draw;
142c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell   struct translate *translate = emit->translate;
143c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell   struct vbuf_render *render = draw->render;
144c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell   void *hw_verts;
145c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell
146c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell   /* XXX: need to flush to get prim_vbuf.c to release its allocation??
147c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell    */
148c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell   draw_do_flush( draw, DRAW_FLUSH_BACKEND );
149c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell
150c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell   hw_verts = render->allocate_vertices(render,
151c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell					(ushort)translate->key.output_stride,
152c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell					(ushort)count);
153c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell   if (!hw_verts) {
154c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell      assert(0);
155c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell      return;
156c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell   }
157c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell
158c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell   translate->set_buffer(translate,
159c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell			 0,
160a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell			 vertex_data,
161c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell			 stride );
162c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell
163c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell   translate->set_buffer(translate,
164c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell			 1,
165c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell			 &draw->rasterizer->point_size,
166c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell			 0);
167c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell
168c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell   translate->run( translate,
169c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell		   0,
170c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell		   vertex_count,
171c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell		   hw_verts );
172c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell
173c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell   render->draw(render,
174c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell		elts,
175c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell		count);
176c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell
177c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell   render->release_vertices(render,
178c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell			    hw_verts,
179c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell			    translate->key.output_stride,
180c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell			    vertex_count);
181c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell}
182c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell
183c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell
184c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwellstruct pt_emit *draw_pt_emit_create( struct draw_context *draw )
185c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell{
186c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell   struct pt_emit *emit = CALLOC_STRUCT(pt_emit);
187c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell   if (!emit)
188c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell      return NULL;
189c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell
190c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell   emit->draw = draw;
191c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell
192c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell   return emit;
193c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell}
194c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell
195c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwellvoid draw_pt_emit_destroy( struct pt_emit *emit )
196c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell{
197c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell   if (emit->translate)
198c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell      emit->translate->release( emit->translate );
199c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell
200c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell   FREE(emit);
201c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell}
202