draw_pt_fetch.c revision 7400bc4b6fb0c20a935cd108afa92814eeafec6d
1a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell/**************************************************************************
2a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell *
3a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
4a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell * All Rights Reserved.
5a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell *
6a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell * Permission is hereby granted, free of charge, to any person obtaining a
7a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell * copy of this software and associated documentation files (the
8a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell * "Software"), to deal in the Software without restriction, including
9a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell * without limitation the rights to use, copy, modify, merge, publish,
10a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell * distribute, sub license, and/or sell copies of the Software, and to
11a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell * permit persons to whom the Software is furnished to do so, subject to
12a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell * the following conditions:
13a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell *
14a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell * The above copyright notice and this permission notice (including the
15a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell * next paragraph) shall be included in all copies or substantial portions
16a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell * of the Software.
17a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell *
18a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell *
26a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell **************************************************************************/
27a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell
28a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell#include "pipe/p_util.h"
29a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell#include "draw/draw_context.h"
30a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell#include "draw/draw_private.h"
31a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell#include "draw/draw_vbuf.h"
32a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell#include "draw/draw_vertex.h"
33a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell#include "draw/draw_pt.h"
34a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell#include "translate/translate.h"
35a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell
36a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell
37a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwellstruct pt_fetch {
38a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell   struct draw_context *draw;
39a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell
40a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell   struct translate *translate;
41a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell
42a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell   unsigned vertex_size;
43a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell};
44a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell
45a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell
46a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell
47a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell/* Perform the fetch from API vertex elements & vertex buffers, to a
48a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell * contiguous set of float[4] attributes as required for the
49a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell * vertex_shader->run_linear() method.
50a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell *
51a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell * This is used in all cases except pure passthrough
52a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell * (draw_pt_fetch_emit.c) which has its own version to translate
53a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell * directly to hw vertices.
54a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell *
55a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell */
56a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwellvoid draw_pt_fetch_prepare( struct pt_fetch *fetch,
57a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell			    boolean emit_header,
58a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell			    unsigned vertex_size )
59a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell{
60a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell   struct draw_context *draw = fetch->draw;
61a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell   unsigned i, nr = 0;
62a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell   unsigned dst_offset = 0;
63a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell   struct translate_key key;
64a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell
65a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell   fetch->vertex_size = vertex_size;
66a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell
67a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell   memset(&key, 0, sizeof(key));
68a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell
69a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell   /* If PT_SHADE is not set, then we are creating post-shader
70a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell    * vertices, meaning that we need to emit/leave space for a vertex
71a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell    * header.
72a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell    *
73a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell    * It's worth considering whether the vertex headers should contain
74a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell    * a pointer to the 'data', rather than having it inline.
75a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell    * Something to look at after we've fully switched over to the pt
76a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell    * paths.
77a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell    */
78a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell   if (emit_header)
79a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell   {
80a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell      /* Need to set header->vertex_id = 0xffff somehow.
81a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell       */
82a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell      key.element[nr].input_format = PIPE_FORMAT_R32_FLOAT;
83a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell      key.element[nr].input_buffer = draw->nr_vertex_buffers;
84a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell      key.element[nr].input_offset = 0;
85a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell      key.element[nr].output_format = PIPE_FORMAT_R32_FLOAT;
86a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell      key.element[nr].output_offset = dst_offset;
87a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell      dst_offset += 1 * sizeof(float);
88a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell      nr++;
89a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell
90a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell
91a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell      /* Just leave the clip[] array untouched.
92a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell       */
93a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell      dst_offset += 4 * sizeof(float);
94a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell   }
95a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell
96a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell
97a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell   for (i = 0; i < draw->nr_vertex_elements; i++) {
98a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell      key.element[nr].input_format = draw->vertex_element[i].src_format;
99a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell      key.element[nr].input_buffer = draw->vertex_element[i].vertex_buffer_index;
100a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell      key.element[nr].input_offset = draw->vertex_element[i].src_offset;
101a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell      key.element[nr].output_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
102a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell      key.element[nr].output_offset = dst_offset;
103a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell
104a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell      dst_offset += 4 * sizeof(float);
105a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell      nr++;
106a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell   }
107a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell
108a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell   assert(dst_offset <= vertex_size);
109a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell
110a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell   key.nr_elements = nr;
111a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell   key.output_stride = vertex_size;
112a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell
113a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell
114a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell   /* Don't bother with caching at this stage:
115a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell    */
116a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell   if (!fetch->translate ||
117a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell       memcmp(&fetch->translate->key, &key, sizeof(key)) != 0)
118a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell   {
119a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell      if (fetch->translate)
120a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell	 fetch->translate->release(fetch->translate);
121a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell
1227400bc4b6fb0c20a935cd108afa92814eeafec6dKeith Whitwell      fetch->translate = translate_create( &key );
123a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell
124a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell      if (emit_header) {
125a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell	 static struct vertex_header vh = { 0, 0, 0, 0xffff };
126a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell	 fetch->translate->set_buffer(fetch->translate,
127a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell				      draw->nr_vertex_buffers,
128a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell				      &vh,
129a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell				      0);
130a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell      }
131a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell   }
132a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell}
133a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell
134a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell
135a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell
136a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell
137a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwellvoid draw_pt_fetch_run( struct pt_fetch *fetch,
138a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell			const unsigned *elts,
139a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell			unsigned count,
140a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell			char *verts )
141a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell{
142a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell   struct draw_context *draw = fetch->draw;
143a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell   struct translate *translate = fetch->translate;
144a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell   unsigned i;
145a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell
146a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell   for (i = 0; i < draw->nr_vertex_buffers; i++) {
147a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell      translate->set_buffer(translate,
148a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell			    i,
149a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell			    ((char *)draw->user.vbuffer[i] +
150a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell			     draw->vertex_buffer[i].buffer_offset),
151a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell			    draw->vertex_buffer[i].pitch );
152a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell   }
153a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell
154a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell   translate->run_elts( translate,
155a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell			elts,
156a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell			count,
157a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell			verts );
158a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell}
159a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell
160a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell
161a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwellstruct pt_fetch *draw_pt_fetch_create( struct draw_context *draw )
162a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell{
163a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell   struct pt_fetch *fetch = CALLOC_STRUCT(pt_fetch);
164a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell   if (!fetch)
165a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell      return NULL;
166a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell
167a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell   fetch->draw = draw;
168a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell   return fetch;
169a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell}
170a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell
171a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwellvoid draw_pt_fetch_destroy( struct pt_fetch *fetch )
172a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell{
173a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell   FREE(fetch);
174a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell}
175a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell
176