00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #include "util/u_memory.h"
00034 #include "util/u_math.h"
00035 #include "draw/draw_context.h"
00036 #include "draw/draw_private.h"
00037 #include "draw/draw_vbuf.h"
00038 #include "draw/draw_vertex.h"
00039 #include "draw/draw_vs.h"
00040 #include "translate/translate.h"
00041 #include "translate/translate_cache.h"
00042
00043
00044
00045 struct draw_vs_varient_generic {
00046 struct draw_vs_varient base;
00047
00048 struct draw_vertex_shader *shader;
00049 struct draw_context *draw;
00050
00051
00052
00053
00054
00055
00056
00057 struct translate *fetch;
00058 struct translate *emit;
00059
00060 unsigned temp_vertex_stride;
00061 };
00062
00063
00064
00065
00066
00067 static void vsvg_set_buffer( struct draw_vs_varient *varient,
00068 unsigned buffer,
00069 const void *ptr,
00070 unsigned stride )
00071 {
00072 struct draw_vs_varient_generic *vsvg = (struct draw_vs_varient_generic *)varient;
00073
00074 vsvg->fetch->set_buffer(vsvg->fetch,
00075 buffer,
00076 ptr,
00077 stride);
00078 }
00079
00080
00081
00082
00083 static void do_rhw_viewport( struct draw_vs_varient_generic *vsvg,
00084 unsigned count,
00085 void *output_buffer )
00086 {
00087 char *ptr = (char *)output_buffer;
00088 const float *scale = vsvg->base.vs->draw->viewport.scale;
00089 const float *trans = vsvg->base.vs->draw->viewport.translate;
00090 unsigned stride = vsvg->temp_vertex_stride;
00091 unsigned j;
00092
00093 ptr += vsvg->base.vs->position_output * 4 * sizeof(float);
00094
00095 for (j = 0; j < count; j++, ptr += stride) {
00096 float *data = (float *)ptr;
00097 float w = 1.0f / data[3];
00098
00099 data[0] = data[0] * w * scale[0] + trans[0];
00100 data[1] = data[1] * w * scale[1] + trans[1];
00101 data[2] = data[2] * w * scale[2] + trans[2];
00102 data[3] = w;
00103 }
00104 }
00105
00106 static void do_viewport( struct draw_vs_varient_generic *vsvg,
00107 unsigned count,
00108 void *output_buffer )
00109 {
00110 char *ptr = (char *)output_buffer;
00111 const float *scale = vsvg->base.vs->draw->viewport.scale;
00112 const float *trans = vsvg->base.vs->draw->viewport.translate;
00113 unsigned stride = vsvg->temp_vertex_stride;
00114 unsigned j;
00115
00116 ptr += vsvg->base.vs->position_output * 4 * sizeof(float);
00117
00118 for (j = 0; j < count; j++, ptr += stride) {
00119 float *data = (float *)ptr;
00120
00121 data[0] = data[0] * scale[0] + trans[0];
00122 data[1] = data[1] * scale[1] + trans[1];
00123 data[2] = data[2] * scale[2] + trans[2];
00124 }
00125 }
00126
00127
00128 static void PIPE_CDECL vsvg_run_elts( struct draw_vs_varient *varient,
00129 const unsigned *elts,
00130 unsigned count,
00131 void *output_buffer)
00132 {
00133 struct draw_vs_varient_generic *vsvg = (struct draw_vs_varient_generic *)varient;
00134 unsigned temp_vertex_stride = vsvg->temp_vertex_stride;
00135 void *temp_buffer = MALLOC( align(count,4) * temp_vertex_stride );
00136
00137 if (0) debug_printf("%s %d \n", __FUNCTION__, count);
00138
00139
00140
00141
00142 vsvg->fetch->run_elts( vsvg->fetch,
00143 elts,
00144 count,
00145 temp_buffer );
00146
00147 vsvg->base.vs->run_linear( vsvg->base.vs,
00148 temp_buffer,
00149 temp_buffer,
00150 (const float (*)[4])vsvg->base.vs->draw->pt.user.constants,
00151 count,
00152 temp_vertex_stride,
00153 temp_vertex_stride);
00154
00155
00156 if (vsvg->base.key.clip) {
00157
00158
00159
00160 do_rhw_viewport( vsvg,
00161 count,
00162 temp_buffer );
00163 }
00164 else if (vsvg->base.key.viewport) {
00165 do_viewport( vsvg,
00166 count,
00167 temp_buffer );
00168 }
00169
00170
00171 vsvg->emit->set_buffer( vsvg->emit,
00172 0,
00173 temp_buffer,
00174 temp_vertex_stride );
00175
00176 vsvg->emit->set_buffer( vsvg->emit,
00177 1,
00178 &vsvg->draw->rasterizer->point_size,
00179 0);
00180
00181 vsvg->emit->run( vsvg->emit,
00182 0, count,
00183 output_buffer );
00184
00185 FREE(temp_buffer);
00186 }
00187
00188
00189 static void PIPE_CDECL vsvg_run_linear( struct draw_vs_varient *varient,
00190 unsigned start,
00191 unsigned count,
00192 void *output_buffer )
00193 {
00194 struct draw_vs_varient_generic *vsvg = (struct draw_vs_varient_generic *)varient;
00195 unsigned temp_vertex_stride = vsvg->temp_vertex_stride;
00196 void *temp_buffer = MALLOC( align(count,4) * temp_vertex_stride );
00197
00198 if (0) debug_printf("%s %d %d (sz %d, %d)\n", __FUNCTION__, start, count,
00199 vsvg->base.key.output_stride,
00200 temp_vertex_stride);
00201
00202 vsvg->fetch->run( vsvg->fetch,
00203 start,
00204 count,
00205 temp_buffer );
00206
00207 vsvg->base.vs->run_linear( vsvg->base.vs,
00208 temp_buffer,
00209 temp_buffer,
00210 (const float (*)[4])vsvg->base.vs->draw->pt.user.constants,
00211 count,
00212 temp_vertex_stride,
00213 temp_vertex_stride);
00214
00215 if (vsvg->base.key.clip) {
00216
00217
00218
00219 do_rhw_viewport( vsvg,
00220 count,
00221 temp_buffer );
00222 }
00223 else if (vsvg->base.key.viewport) {
00224 do_viewport( vsvg,
00225 count,
00226 temp_buffer );
00227 }
00228
00229 vsvg->emit->set_buffer( vsvg->emit,
00230 0,
00231 temp_buffer,
00232 temp_vertex_stride );
00233
00234 vsvg->emit->set_buffer( vsvg->emit,
00235 1,
00236 &vsvg->draw->rasterizer->point_size,
00237 0);
00238
00239 vsvg->emit->run( vsvg->emit,
00240 0, count,
00241 output_buffer );
00242
00243 FREE(temp_buffer);
00244 }
00245
00246
00247
00248
00249
00250 static void vsvg_destroy( struct draw_vs_varient *varient )
00251 {
00252 FREE(varient);
00253 }
00254
00255
00256 struct draw_vs_varient *draw_vs_varient_generic( struct draw_vertex_shader *vs,
00257 const struct draw_vs_varient_key *key )
00258 {
00259 unsigned i;
00260 struct translate_key fetch, emit;
00261
00262 struct draw_vs_varient_generic *vsvg = CALLOC_STRUCT( draw_vs_varient_generic );
00263 if (vsvg == NULL)
00264 return NULL;
00265
00266 vsvg->base.key = *key;
00267 vsvg->base.vs = vs;
00268 vsvg->base.set_buffer = vsvg_set_buffer;
00269 vsvg->base.run_elts = vsvg_run_elts;
00270 vsvg->base.run_linear = vsvg_run_linear;
00271 vsvg->base.destroy = vsvg_destroy;
00272
00273 vsvg->draw = vs->draw;
00274
00275 vsvg->temp_vertex_stride = MAX2(key->nr_inputs,
00276 vsvg->base.vs->info.num_outputs) * 4 * sizeof(float);
00277
00278
00279
00280 fetch.nr_elements = key->nr_inputs;
00281 fetch.output_stride = vsvg->temp_vertex_stride;
00282 for (i = 0; i < key->nr_inputs; i++) {
00283 fetch.element[i].input_format = key->element[i].in.format;
00284 fetch.element[i].input_buffer = key->element[i].in.buffer;
00285 fetch.element[i].input_offset = key->element[i].in.offset;
00286 fetch.element[i].output_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
00287 fetch.element[i].output_offset = i * 4 * sizeof(float);
00288 assert(fetch.element[i].output_offset < fetch.output_stride);
00289 }
00290
00291
00292 emit.nr_elements = key->nr_outputs;
00293 emit.output_stride = key->output_stride;
00294 for (i = 0; i < key->nr_outputs; i++) {
00295 if (key->element[i].out.format != EMIT_1F_PSIZE)
00296 {
00297 emit.element[i].input_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
00298 emit.element[i].input_buffer = 0;
00299 emit.element[i].input_offset = key->element[i].out.vs_output * 4 * sizeof(float);
00300 emit.element[i].output_format = draw_translate_vinfo_format(key->element[i].out.format);
00301 emit.element[i].output_offset = key->element[i].out.offset;
00302 assert(emit.element[i].input_offset <= fetch.output_stride);
00303 }
00304 else {
00305 emit.element[i].input_format = PIPE_FORMAT_R32_FLOAT;
00306 emit.element[i].input_buffer = 1;
00307 emit.element[i].input_offset = 0;
00308 emit.element[i].output_format = PIPE_FORMAT_R32_FLOAT;
00309 emit.element[i].output_offset = key->element[i].out.offset;
00310 }
00311 }
00312
00313 vsvg->fetch = draw_vs_get_fetch( vs->draw, &fetch );
00314 vsvg->emit = draw_vs_get_emit( vs->draw, &emit );
00315
00316 return &vsvg->base;
00317 }
00318
00319
00320
00321
00322