draw_pt_fetch.c revision e406ad5912985920a0d796f1ada58b40316590ed
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			    unsigned vertex_size )
58a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell{
59a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell   struct draw_context *draw = fetch->draw;
60a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell   unsigned i, nr = 0;
61a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell   unsigned dst_offset = 0;
62a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell   struct translate_key key;
63a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell
64a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell   fetch->vertex_size = vertex_size;
65a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell
66a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell   memset(&key, 0, sizeof(key));
67a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell
68c898eae27221bd23b11327553c215a94369eeb99Keith Whitwell   /* Always emit/leave space for a vertex header.
69a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell    *
70a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell    * It's worth considering whether the vertex headers should contain
71a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell    * a pointer to the 'data', rather than having it inline.
72a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell    * Something to look at after we've fully switched over to the pt
73a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell    * paths.
74a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell    */
75a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell   {
76a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell      /* Need to set header->vertex_id = 0xffff somehow.
77a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell       */
78a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell      key.element[nr].input_format = PIPE_FORMAT_R32_FLOAT;
797d72607e142c0412b88183b849fd701e698b8f79Keith Whitwell      key.element[nr].input_buffer = draw->pt.nr_vertex_buffers;
80a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell      key.element[nr].input_offset = 0;
81a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell      key.element[nr].output_format = PIPE_FORMAT_R32_FLOAT;
82a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell      key.element[nr].output_offset = dst_offset;
83a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell      dst_offset += 1 * sizeof(float);
84a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell      nr++;
85a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell
86a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell
87a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell      /* Just leave the clip[] array untouched.
88a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell       */
89a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell      dst_offset += 4 * sizeof(float);
90a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell   }
91a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell
92a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell
937d72607e142c0412b88183b849fd701e698b8f79Keith Whitwell   for (i = 0; i < draw->pt.nr_vertex_elements; i++) {
947d72607e142c0412b88183b849fd701e698b8f79Keith Whitwell      key.element[nr].input_format = draw->pt.vertex_element[i].src_format;
957d72607e142c0412b88183b849fd701e698b8f79Keith Whitwell      key.element[nr].input_buffer = draw->pt.vertex_element[i].vertex_buffer_index;
967d72607e142c0412b88183b849fd701e698b8f79Keith Whitwell      key.element[nr].input_offset = draw->pt.vertex_element[i].src_offset;
97a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell      key.element[nr].output_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
98a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell      key.element[nr].output_offset = dst_offset;
99a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell
100a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell      dst_offset += 4 * sizeof(float);
101a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell      nr++;
102a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell   }
103a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell
104a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell   assert(dst_offset <= vertex_size);
105a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell
106a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell   key.nr_elements = nr;
107a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell   key.output_stride = vertex_size;
108a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell
109a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell
110a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell   /* Don't bother with caching at this stage:
111a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell    */
112a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell   if (!fetch->translate ||
113a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell       memcmp(&fetch->translate->key, &key, sizeof(key)) != 0)
114a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell   {
115a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell      if (fetch->translate)
116a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell	 fetch->translate->release(fetch->translate);
117a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell
1187400bc4b6fb0c20a935cd108afa92814eeafec6dKeith Whitwell      fetch->translate = translate_create( &key );
119a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell
120c898eae27221bd23b11327553c215a94369eeb99Keith Whitwell      {
121a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell	 static struct vertex_header vh = { 0, 0, 0, 0xffff };
122a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell	 fetch->translate->set_buffer(fetch->translate,
1237d72607e142c0412b88183b849fd701e698b8f79Keith Whitwell				      draw->pt.nr_vertex_buffers,
124a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell				      &vh,
125a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell				      0);
126a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell      }
127a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell   }
128a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell}
129a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell
130a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell
131a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell
132a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell
133a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwellvoid draw_pt_fetch_run( struct pt_fetch *fetch,
134a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell			const unsigned *elts,
135a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell			unsigned count,
136a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell			char *verts )
137a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell{
138a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell   struct draw_context *draw = fetch->draw;
139a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell   struct translate *translate = fetch->translate;
140a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell   unsigned i;
141a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell
1427d72607e142c0412b88183b849fd701e698b8f79Keith Whitwell   for (i = 0; i < draw->pt.nr_vertex_buffers; i++) {
143a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell      translate->set_buffer(translate,
144a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell			    i,
1457d72607e142c0412b88183b849fd701e698b8f79Keith Whitwell			    ((char *)draw->pt.user.vbuffer[i] +
1467d72607e142c0412b88183b849fd701e698b8f79Keith Whitwell			     draw->pt.vertex_buffer[i].buffer_offset),
1477d72607e142c0412b88183b849fd701e698b8f79Keith Whitwell			    draw->pt.vertex_buffer[i].pitch );
148a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell   }
149a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell
150a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell   translate->run_elts( translate,
151a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell			elts,
152a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell			count,
153a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell			verts );
154a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell}
155a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell
156a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell
157a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwellstruct pt_fetch *draw_pt_fetch_create( struct draw_context *draw )
158a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell{
159a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell   struct pt_fetch *fetch = CALLOC_STRUCT(pt_fetch);
160a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell   if (!fetch)
161a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell      return NULL;
162a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell
163a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell   fetch->draw = draw;
164a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell   return fetch;
165a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell}
166a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell
167a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwellvoid draw_pt_fetch_destroy( struct pt_fetch *fetch )
168a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell{
169e406ad5912985920a0d796f1ada58b40316590edKeith Whitwell   if (fetch->translate)
170e406ad5912985920a0d796f1ada58b40316590edKeith Whitwell      fetch->translate->release( fetch->translate );
171e406ad5912985920a0d796f1ada58b40316590edKeith Whitwell
172a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell   FREE(fetch);
173a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell}
174a773f06e969a3992451dd7fe6fd55ea96b2774faKeith Whitwell
175