draw_pt_emit.c revision 95f8f8863a80ce1e584160d4d085213a9bbaef12
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"
355fcd84ab39318a371253b1a7285bc657fb82efedZack Rusin#include "translate/translate_cache.h"
36c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell
37c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwellstruct pt_emit {
38c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell   struct draw_context *draw;
39c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell
40c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell   struct translate *translate;
41f088b53769aacbee20135d912c33d688b6002011Zack Rusin
425fcd84ab39318a371253b1a7285bc657fb82efedZack Rusin   struct translate_cache *cache;
43c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell};
44c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell
45c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwellvoid draw_pt_emit_prepare( struct pt_emit *emit,
46a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell			   unsigned prim )
47c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell{
48c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell   struct draw_context *draw = emit->draw;
49c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell   const struct vertex_info *vinfo;
50c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell   unsigned dst_offset;
51c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell   struct translate_key hw_key;
52c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell   unsigned i;
53c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell   boolean ok;
54c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell
55c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell   ok = draw->render->set_primitive(draw->render, prim);
56c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell   if (!ok) {
57c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell      assert(0);
58c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell      return;
59c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell   }
60c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell
61c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell   /* Must do this after set_primitive() above:
62c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell    */
63c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell   vinfo = draw->render->get_vertex_info(draw->render);
64c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell
65c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell
66cb9f0a589623397c3437911aeef39f189213527aKeith Whitwell   /* Translate from pipeline vertices to hw vertices.
67c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell    */
68c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell   dst_offset = 0;
69c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell   for (i = 0; i < vinfo->num_attribs; i++) {
70c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell      unsigned emit_sz = 0;
71c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell      unsigned src_buffer = 0;
72c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell      unsigned output_format;
73a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell      unsigned src_offset = (vinfo->src_index[i] * 4 * sizeof(float) );
74c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell
75c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell
76c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell
77c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell      switch (vinfo->emit[i]) {
78c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell      case EMIT_4F:
79c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell	 output_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
80c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell	 emit_sz = 4 * sizeof(float);
81c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell	 break;
82c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell      case EMIT_3F:
83c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell	 output_format = PIPE_FORMAT_R32G32B32_FLOAT;
84c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell	 emit_sz = 3 * sizeof(float);
85c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell	 break;
86c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell      case EMIT_2F:
87c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell	 output_format = PIPE_FORMAT_R32G32_FLOAT;
88c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell	 emit_sz = 2 * sizeof(float);
89c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell	 break;
90c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell      case EMIT_1F:
91c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell	 output_format = PIPE_FORMAT_R32_FLOAT;
92c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell	 emit_sz = 1 * sizeof(float);
93c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell	 break;
94c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell      case EMIT_1F_PSIZE:
95c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell	 output_format = PIPE_FORMAT_R32_FLOAT;
96c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell	 emit_sz = 1 * sizeof(float);
97c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell	 src_buffer = 1;
98c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell	 src_offset = 0;
99c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell	 break;
100c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell      case EMIT_4UB:
101c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell	 output_format = PIPE_FORMAT_B8G8R8A8_UNORM;
102c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell	 emit_sz = 4 * sizeof(ubyte);
103c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell      default:
104c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell	 assert(0);
105c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell	 output_format = PIPE_FORMAT_NONE;
106c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell	 emit_sz = 0;
107c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell	 break;
108c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell      }
109c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell
110c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell      hw_key.element[i].input_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
111c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell      hw_key.element[i].input_buffer = src_buffer;
112c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell      hw_key.element[i].input_offset = src_offset;
113c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell      hw_key.element[i].output_format = output_format;
114c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell      hw_key.element[i].output_offset = dst_offset;
115c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell
116c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell      dst_offset += emit_sz;
117c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell   }
118c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell
119c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell   hw_key.nr_elements = vinfo->num_attribs;
120c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell   hw_key.output_stride = vinfo->size * 4;
121c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell
122c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell   if (!emit->translate ||
1235fcd84ab39318a371253b1a7285bc657fb82efedZack Rusin       memcmp(&emit->translate->key, &hw_key, sizeof(hw_key)) != 0)
124c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell   {
1255fcd84ab39318a371253b1a7285bc657fb82efedZack Rusin      emit->translate = translate_cache_find(emit->cache, &hw_key);
126c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell   }
127c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell}
128c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell
129c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell
130c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwellvoid draw_pt_emit( struct pt_emit *emit,
131a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell		   const float (*vertex_data)[4],
132c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell		   unsigned vertex_count,
133a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell		   unsigned stride,
134c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell		   const ushort *elts,
135c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell		   unsigned count )
136c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell{
137c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell   struct draw_context *draw = emit->draw;
138c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell   struct translate *translate = emit->translate;
139c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell   struct vbuf_render *render = draw->render;
140c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell   void *hw_verts;
141c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell
142c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell   /* XXX: need to flush to get prim_vbuf.c to release its allocation??
143c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell    */
144c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell   draw_do_flush( draw, DRAW_FLUSH_BACKEND );
145c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell
146c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell   hw_verts = render->allocate_vertices(render,
147c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell					(ushort)translate->key.output_stride,
148c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell					(ushort)count);
149c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell   if (!hw_verts) {
150c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell      assert(0);
151c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell      return;
152c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell   }
153c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell
154c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell   translate->set_buffer(translate,
155c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell			 0,
156a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell			 vertex_data,
157c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell			 stride );
158c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell
159c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell   translate->set_buffer(translate,
160c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell			 1,
161c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell			 &draw->rasterizer->point_size,
162c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell			 0);
163c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell
164c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell   translate->run( translate,
165c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell		   0,
166c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell		   vertex_count,
167c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell		   hw_verts );
168c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell
169c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell   render->draw(render,
170c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell		elts,
171c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell		count);
172c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell
173c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell   render->release_vertices(render,
174c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell			    hw_verts,
175c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell			    translate->key.output_stride,
176c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell			    vertex_count);
177c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell}
178c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell
179c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell
180c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwellstruct pt_emit *draw_pt_emit_create( struct draw_context *draw )
181c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell{
182c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell   struct pt_emit *emit = CALLOC_STRUCT(pt_emit);
183c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell   if (!emit)
184c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell      return NULL;
185c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell
186c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell   emit->draw = draw;
1875fcd84ab39318a371253b1a7285bc657fb82efedZack Rusin   emit->cache = translate_cache_create();
18895f8f8863a80ce1e584160d4d085213a9bbaef12Zack Rusin   if (!emit->cache) {
18995f8f8863a80ce1e584160d4d085213a9bbaef12Zack Rusin      FREE(emit);
19095f8f8863a80ce1e584160d4d085213a9bbaef12Zack Rusin      return NULL;
19195f8f8863a80ce1e584160d4d085213a9bbaef12Zack Rusin   }
192c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell
193c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell   return emit;
194c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell}
195c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell
196c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwellvoid draw_pt_emit_destroy( struct pt_emit *emit )
197c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell{
1985fcd84ab39318a371253b1a7285bc657fb82efedZack Rusin   translate_cache_destroy(emit->cache);
199c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell
200c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell   FREE(emit);
201c503e55d74cf84f87f82b3dab3cb4d38b201d47aKeith Whitwell}
202