Instead of linked program pairs, keep a list of vertex programs translated for each fragment program.

This commit is contained in:
Brian
2007-09-28 15:39:09 -06:00
parent f14ece2d2c
commit 636480cc9c
5 changed files with 157 additions and 246 deletions

View File

@@ -51,105 +51,56 @@
#include "st_atom_shader.h" #include "st_atom_shader.h"
/** /**
* Structure to describe a (vertex program, fragment program) pair * This represents a vertex program, especially translated to match
* which is linked together (used together to render something). This * the inputs of a particular fragment shader.
* linkage basically servers the same purpose as the OpenGL Shading
* Language linker, but also applies to ARB programs and Mesa's
* fixed-function-generated programs.
*
* More background:
*
* The translation from Mesa programs to TGSI programs depends on the
* linkage between the vertex program and the fragment program. This is
* because we tightly pack the inputs and outputs of shaders into
* consecutive "slots".
*
* Suppose an app uses one vertex program "VP" (outputting pos, color and tex0)
* and two fragment programs:
* FP1: uses tex0 input only (input slot 0)
* FP2: uses color input only (input slot 0)
*
* When VP is used with FP1 we want VP.output[2] to match FP1.input[0], but
* when VP is used with FP2 we want VP.output[1] to match FP1.input[0].
*
* We don't want to re-translate the vertex and/or fragment programs
* each time the VP/FP bindings/linkings change. The solution is this
* structure which stores the translated TGSI shaders on a per-linkage
* basis.
*
*/ */
struct linked_program_pair struct translated_vertex_program
{ {
struct st_vertex_program *vprog; /**< never changes */ /** The fragment shader "signature" this vertex shader is meant for: */
struct st_fragment_program *fprog; /**< never changes */ GLbitfield frag_inputs;
struct tgsi_token vs_tokens[ST_FP_MAX_TOKENS]; /** Compared against master vertex program's serialNo: */
struct tgsi_token fs_tokens[ST_FP_MAX_TOKENS]; GLuint serialNo;
/** Maps VERT_RESULT_x to slot */
GLuint output_to_slot[VERT_RESULT_MAX];
/** The program in TGSI format */
struct tgsi_token tokens[ST_MAX_SHADER_TOKENS];
/** Pointer to the translated, cached vertex shader */
const struct cso_vertex_shader *vs; const struct cso_vertex_shader *vs;
const struct cso_fragment_shader *fs;
GLuint vertSerialNo, fragSerialNo; struct translated_vertex_program *next; /**< next in linked list */
/** maps a Mesa VERT_ATTRIB_x to a packed TGSI input index */
GLuint vp_input_to_index[MAX_VERTEX_PROGRAM_ATTRIBS];
/** maps a TGSI input index back to a Mesa VERT_ATTRIB_x */
GLuint vp_index_to_input[MAX_VERTEX_PROGRAM_ATTRIBS];
GLuint vp_result_to_slot[VERT_RESULT_MAX];
struct linked_program_pair *next;
}; };
/** XXX temporary - use some kind of hash table instead */
static struct linked_program_pair *Pairs = NULL;
static void
find_and_remove(struct gl_program *prog)
{
struct linked_program_pair *pair, *prev = NULL, *next;
for (pair = Pairs; pair; pair = next) {
next = pair->next;
if (pair->vprog == (struct st_vertex_program *) prog ||
pair->fprog == (struct st_fragment_program *) prog) {
/* unlink */
if (prev)
prev->next = next;
else
Pairs = next;
/* delete pair->vs */
/* delete pair->fs */
free(pair);
}
else {
prev = pair;
}
}
}
/** /**
* Delete any known program pairs that use the given vertex program. * Free data hanging off the st vert prog.
*/ */
void void
st_remove_vertex_program(struct st_context *st, struct st_vertex_program *stvp) st_remove_vertex_program(struct st_context *st, struct st_vertex_program *stvp)
{ {
find_and_remove(&stvp->Base.Base); /* no-op, for now? */
} }
/** /**
* Delete any known program pairs that use the given fragment program. * Free data hanging off the st frag prog.
*/ */
void void
st_remove_fragment_program(struct st_context *st, st_remove_fragment_program(struct st_context *st,
struct st_fragment_program *stfp) struct st_fragment_program *stfp)
{ {
find_and_remove(&stfp->Base.Base); struct translated_vertex_program *xvp, *next;
for (xvp = stfp->vertex_programs; xvp; xvp = next) {
next = xvp->next;
/* XXX free xvp->vs */
free(xvp);
}
} }
@@ -187,182 +138,122 @@ vp_out_to_fp_in(GLuint vertResult)
/** /**
* Examine the outputs written by a vertex program and the inputs read * Find a translated vertex program that corresponds to stvp and
* by a fragment program to determine which match up and where they * has outputs matched to stfp's inputs.
* should be mapped into the generic shader output/input slots. * This performs vertex and fragment translation (to TGSI) when needed.
* \param vert_output_map returns the vertex output register mapping
* \param frag_input_map returns the fragment input register mapping
*/ */
static GLuint static struct translated_vertex_program *
link_outputs_to_inputs(GLbitfield outputsWritten, find_translated_vp(struct st_context *st,
GLbitfield inputsRead, struct st_vertex_program *stvp,
GLuint vert_output_map[], struct st_fragment_program *stfp)
GLuint frag_input_map[])
{ {
static const GLuint UNUSED = ~0; static const GLuint UNUSED = ~0;
GLint vert_slot_to_attr[50], frag_slot_to_attr[50]; struct translated_vertex_program *xvp;
GLuint outAttr, inAttr; const GLbitfield fragInputsRead
GLuint numIn = 0, dummySlot; = stfp->Base.Base.InputsRead | FRAG_BIT_WPOS;
/*
* Translate fragment program if needed.
*/
if (!stfp->fs) {
GLuint inAttr, numIn = 0;
for (inAttr = 0; inAttr < FRAG_ATTRIB_MAX; inAttr++) { for (inAttr = 0; inAttr < FRAG_ATTRIB_MAX; inAttr++) {
if (inputsRead & (1 << inAttr)) { if (fragInputsRead & (1 << inAttr)) {
frag_input_map[inAttr] = numIn; stfp->input_to_slot[inAttr] = numIn;
frag_slot_to_attr[numIn] = inAttr;
numIn++; numIn++;
} }
else { else {
frag_input_map[inAttr] = UNUSED; stfp->input_to_slot[inAttr] = UNUSED;
} }
} }
for (outAttr = 0; outAttr < VERT_RESULT_MAX; outAttr++) { stfp->num_input_slots = numIn;
if (outputsWritten & (1 << outAttr)) {
/* see if the frag prog wants this vert output */
GLint fpIn = vp_out_to_fp_in(outAttr);
if (fpIn >= 0) { (void) st_translate_fragment_program(st, stfp,
GLuint frag_slot = frag_input_map[fpIn]; stfp->input_to_slot,
vert_output_map[outAttr] = frag_slot; stfp->tokens,
vert_slot_to_attr[frag_slot] = outAttr; ST_MAX_SHADER_TOKENS);
} assert(stfp->fs);
else {
vert_output_map[outAttr] = UNUSED;
}
}
else {
vert_output_map[outAttr] = UNUSED;
}
} }
/*
* We'll map all unused vertex program outputs to this slot. /* See if we've got a translated vertex program whose outputs match
* We'll also map all undefined fragment program inputs to this slot. * the fragment program's inputs.
* XXX This could be a hash lookup, using InputsRead as the key.
*/ */
dummySlot = numIn; for (xvp = stfp->vertex_programs; xvp; xvp = xvp->next) {
if (xvp->frag_inputs == stfp->Base.Base.InputsRead) {
/* Map vert program outputs that aren't used to the dummy slot */
for (outAttr = 0; outAttr < VERT_RESULT_MAX; outAttr++) {
if (outputsWritten & (1 << outAttr)) {
if (vert_output_map[outAttr] == UNUSED)
vert_output_map[outAttr] = dummySlot;
}
}
/* Map frag program inputs that aren't defined to the dummy slot */
for (inAttr = 0; inAttr < FRAG_ATTRIB_MAX; inAttr++) {
if (inputsRead & (1 << inAttr)) {
if (frag_input_map[inAttr] == UNUSED)
frag_input_map[inAttr] = dummySlot;
}
}
#if 0
printf("vOut W slot\n");
for (outAttr = 0; outAttr < VERT_RESULT_MAX; outAttr++) {
printf("%4d %c %4d\n", outAttr,
" *"[(outputsWritten >> outAttr) & 1],
vert_output_map[outAttr]);
}
printf("vIn R slot\n");
for (inAttr = 0; inAttr < FRAG_ATTRIB_MAX; inAttr++) {
printf("%3d %c %4d\n", inAttr,
" *"[(inputsRead >> inAttr) & 1],
frag_input_map[inAttr]);
}
#endif
return numIn;
}
static struct linked_program_pair *
lookup_program_pair(struct st_context *st,
struct st_vertex_program *vprog,
struct st_fragment_program *fprog)
{
struct linked_program_pair *pair;
/* search */
for (pair = Pairs; pair; pair = pair->next) {
if (pair->vprog == vprog && pair->fprog == fprog) {
/* found it */
break; break;
} }
} }
/* /* No? Allocate translated vp object now */
* Examine the outputs of the vertex shader and the inputs of the if (!xvp) {
* fragment shader to determine how to match both to a common set xvp = CALLOC_STRUCT(translated_vertex_program);
* of slots. xvp->frag_inputs = fragInputsRead;
xvp->next = stfp->vertex_programs;
stfp->vertex_programs = xvp;
}
/* See if we need to translate vertex program to TGSI form */
if (xvp->serialNo != stvp->serialNo) {
GLuint outAttr, dummySlot;
const GLbitfield outputsWritten = stvp->Base.Base.OutputsWritten;
/* Compute mapping of vertex program outputs to slots, which depends
* on the fragment program's input->slot mapping.
*/ */
if (!pair) { for (outAttr = 0; outAttr < VERT_RESULT_MAX; outAttr++) {
pair = CALLOC_STRUCT(linked_program_pair); /* set default: */
if (pair) { xvp->output_to_slot[outAttr] = UNUSED;
pair->vprog = vprog;
pair->fprog = fprog; if (outputsWritten & (1 << outAttr)) {
/* see if the frag prog wants this vert output */
GLint fpIn = vp_out_to_fp_in(outAttr);
if (fpIn >= 0) {
xvp->output_to_slot[outAttr] = stfp->input_to_slot[fpIn];
}
} }
} }
return pair; /* Unneeded vertex program outputs will go to this slot.
} * We could use this info to do dead code elimination in the
* vertex program.
*/
dummySlot = stfp->num_input_slots;
/* Map vert program outputs that aren't used to the dummy slot */
static void for (outAttr = 0; outAttr < VERT_RESULT_MAX; outAttr++) {
link_shaders(struct st_context *st, struct linked_program_pair *pair) if (outputsWritten & (1 << outAttr)) {
{ if (xvp->output_to_slot[outAttr] == UNUSED)
struct st_vertex_program *vprog = pair->vprog; xvp->output_to_slot[outAttr] = dummySlot;
struct st_fragment_program *fprog = pair->fprog;
assert(vprog);
assert(fprog);
if (pair->vertSerialNo != vprog->serialNo ||
pair->fragSerialNo != fprog->serialNo) {
/* re-link and re-translate */
GLuint vert_output_mapping[VERT_RESULT_MAX];
GLuint frag_input_mapping[FRAG_ATTRIB_MAX];
link_outputs_to_inputs(vprog->Base.Base.OutputsWritten,
fprog->Base.Base.InputsRead | FRAG_BIT_WPOS,
vert_output_mapping,
frag_input_mapping);
/* xlate vp to vs + vs tokens */
st_translate_vertex_program(st, vprog,
vert_output_mapping,
pair->vs_tokens, ST_FP_MAX_TOKENS);
pair->vprog = vprog;
/* temp hacks */
pair->vs = vprog->vs;
vprog->vs = NULL;
/* xlate fp to fs + fs tokens */
st_translate_fragment_program(st, fprog,
frag_input_mapping,
pair->fs_tokens, ST_FP_MAX_TOKENS);
pair->fprog = fprog;
/* temp hacks */
pair->fs = fprog->fs;
fprog->fs = NULL;
/* save pair */
pair->next = Pairs;
Pairs = pair;
pair->vertSerialNo = vprog->serialNo;
pair->fragSerialNo = fprog->serialNo;
} }
}
xvp->vs = st_translate_vertex_program(st, stvp,
xvp->output_to_slot,
xvp->tokens,
ST_MAX_SHADER_TOKENS);
assert(xvp->vs);
stvp->vs = NULL; /* don't want to use this */
/* translated VP is up to date now */
xvp->serialNo = stvp->serialNo;
}
return xvp;
} }
static void static void
update_linkage( struct st_context *st ) update_linkage( struct st_context *st )
{ {
struct linked_program_pair *pair;
struct st_vertex_program *stvp; struct st_vertex_program *stvp;
struct st_fragment_program *stfp; struct st_fragment_program *stfp;
struct translated_vertex_program *xvp;
/* find active shader and params -- Should be covered by /* find active shader and params -- Should be covered by
* ST_NEW_VERTEX_PROGRAM * ST_NEW_VERTEX_PROGRAM
@@ -392,23 +283,17 @@ update_linkage( struct st_context *st )
stfp = st_fragment_program(st->ctx->FragmentProgram._Current); stfp = st_fragment_program(st->ctx->FragmentProgram._Current);
} }
xvp = find_translated_vp(st, stvp, stfp);
pair = lookup_program_pair(st, stvp, stfp);
assert(pair);
link_shaders(st, pair);
/* Bind the vertex program and TGSI shader */
st->vp = stvp; st->vp = stvp;
st->state.vs = pair->vs; st->state.vs = xvp->vs;
st->pipe->bind_vs_state(st->pipe, st->state.vs->data); st->pipe->bind_vs_state(st->pipe, st->state.vs->data);
/* Bind the fragment program and TGSI shader */
st->fp = stfp; st->fp = stfp;
st->state.fs = pair->fs; st->state.fs = stfp->fs;
st->pipe->bind_fs_state(st->pipe, st->state.fs->data); st->pipe->bind_fs_state(st->pipe, st->state.fs->data);
st->vertex_result_to_slot = pair->vp_result_to_slot; st->vertex_result_to_slot = xvp->output_to_slot;
} }

View File

@@ -156,7 +156,7 @@ make_frag_shader(struct st_context *st)
stfp = (struct st_fragment_program *) p; stfp = (struct st_fragment_program *) p;
st_translate_fragment_program(st, stfp, NULL, st_translate_fragment_program(st, stfp, NULL,
stfp->tokens, ST_FP_MAX_TOKENS); stfp->tokens, ST_MAX_SHADER_TOKENS);
return stfp; return stfp;
} }
@@ -205,7 +205,7 @@ make_vertex_shader(struct st_context *st)
stvp = (struct st_vertex_program *) p; stvp = (struct st_vertex_program *) p;
st_translate_vertex_program(st, stvp, NULL, st_translate_vertex_program(st, stvp, NULL,
stvp->tokens, ST_FP_MAX_TOKENS); stvp->tokens, ST_MAX_SHADER_TOKENS);
assert(stvp->vs); assert(stvp->vs);
return stvp; return stvp;

View File

@@ -131,7 +131,7 @@ make_fragment_shader(struct st_context *st, GLboolean bitmapMode)
stfp = (struct st_fragment_program *) p; stfp = (struct st_fragment_program *) p;
st_translate_fragment_program(st, stfp, NULL, st_translate_fragment_program(st, stfp, NULL,
stfp->tokens, ST_FP_MAX_TOKENS); stfp->tokens, ST_MAX_SHADER_TOKENS);
return stfp; return stfp;
} }
@@ -203,7 +203,7 @@ make_vertex_shader(struct st_context *st, GLboolean passColor)
stvp = (struct st_vertex_program *) p; stvp = (struct st_vertex_program *) p;
st_translate_vertex_program(st, stvp, NULL, st_translate_vertex_program(st, stvp, NULL,
stvp->tokens, ST_FP_MAX_TOKENS); stvp->tokens, ST_MAX_SHADER_TOKENS);
return stvp; return stvp;
} }

View File

@@ -165,6 +165,12 @@ static void st_program_string_notify( GLcontext *ctx,
stfp->serialNo++; stfp->serialNo++;
if (stfp->fs) {
/* free the TGSI code */
// cso_delete(stfp->vs);
stfp->fs = NULL;
}
stfp->param_state = stfp->Base.Base.Parameters->StateFlags; stfp->param_state = stfp->Base.Base.Parameters->StateFlags;
} }
else if (target == GL_VERTEX_PROGRAM_ARB) { else if (target == GL_VERTEX_PROGRAM_ARB) {
@@ -172,6 +178,12 @@ static void st_program_string_notify( GLcontext *ctx,
stvp->serialNo++; stvp->serialNo++;
if (stvp->vs) {
/* free the TGSI code */
// cso_delete(stfp->vs);
stvp->vs = NULL;
}
stvp->param_state = stvp->Base.Base.Parameters->StateFlags; stvp->param_state = stvp->Base.Base.Parameters->StateFlags;
/* Also tell tnl about it: /* Also tell tnl about it:

View File

@@ -38,20 +38,28 @@
#include "pipe/tgsi/exec/tgsi_token.h" #include "pipe/tgsi/exec/tgsi_token.h"
#include "x86/rtasm/x86sse.h" #include "x86/rtasm/x86sse.h"
#define ST_FP_MAX_TOKENS 1024
#define ST_MAX_SHADER_TOKENS 1024
struct cso_fragment_shader; struct cso_fragment_shader;
struct cso_vertex_shader; struct cso_vertex_shader;
struct translated_vertex_program;
/**
* Derived from Mesa gl_fragment_program:
*/
struct st_fragment_program struct st_fragment_program
{ {
struct gl_fragment_program Base; struct gl_fragment_program Base;
GLboolean error; /* If program is malformed for any reason. */
GLuint serialNo; GLuint serialNo;
GLuint input_to_slot[FRAG_ATTRIB_MAX]; /**< Maps FRAG_ATTRIB_x to slot */
GLuint num_input_slots;
/** The program in TGSI format */ /** The program in TGSI format */
struct tgsi_token tokens[ST_FP_MAX_TOKENS]; struct tgsi_token tokens[ST_MAX_SHADER_TOKENS];
#if defined(__i386__) || defined(__386__) #if defined(__i386__) || defined(__386__)
struct x86_function sse2_program; struct x86_function sse2_program;
@@ -61,23 +69,29 @@ struct st_fragment_program
const struct cso_fragment_shader *fs; const struct cso_fragment_shader *fs;
GLuint param_state; GLuint param_state;
/** List of vertex programs which have been translated such that their
* outputs match this fragment program's inputs.
*/
struct translated_vertex_program *vertex_programs;
}; };
/**
* Derived from Mesa gl_fragment_program:
*/
struct st_vertex_program struct st_vertex_program
{ {
struct gl_vertex_program Base; /**< The Mesa vertex program */ struct gl_vertex_program Base; /**< The Mesa vertex program */
GLboolean error; /**< Set if program is malformed for any reason. */
GLuint serialNo; GLuint serialNo;
/** maps a Mesa VERT_ATTRIB_x to a packed TGSI input index */ /** maps a Mesa VERT_ATTRIB_x to a packed TGSI input index */
GLuint input_to_index[MAX_VERTEX_PROGRAM_ATTRIBS]; GLuint input_to_index[MAX_VERTEX_PROGRAM_ATTRIBS];
/** maps a TGSI input index back to a Mesa VERT_ATTRIB_x */ /** maps a TGSI input index back to a Mesa VERT_ATTRIB_x */
GLuint index_to_input[MAX_VERTEX_PROGRAM_ATTRIBS]; GLuint index_to_input[PIPE_MAX_SHADER_INPUTS];
/** The program in TGSI format */ /** The program in TGSI format */
struct tgsi_token tokens[ST_FP_MAX_TOKENS]; struct tgsi_token tokens[ST_MAX_SHADER_TOKENS];
/** Pointer to the corresponding cached shader */ /** Pointer to the corresponding cached shader */
const struct cso_vertex_shader *vs; const struct cso_vertex_shader *vs;