static boolean match_inst(const char **pcur, unsigned *saturate, unsigned *precise, const struct tgsi_opcode_info *info) { const char *cur = *pcur; const char *mnemonic = tgsi_get_opcode_name(info->opcode); /* simple case: the whole string matches the instruction name */ if (str_match_nocase_whole(&cur, mnemonic)) { *pcur = cur; *saturate = 0; *precise = 0; return TRUE; } if (str_match_no_case(&cur, mnemonic)) { /* the instruction has a suffix, figure it out */ if (str_match_no_case(&cur, "_SAT")) { *pcur = cur; *saturate = 1; } if (str_match_no_case(&cur, "_PRECISE")) { *pcur = cur; *precise = 1; } if (!is_digit_alpha_underscore(cur)) return TRUE; } return FALSE; }
/* Parse shader header. * Return TRUE for one of the following headers. * FRAG * GEOM * VERT */ static boolean parse_header( struct translate_ctx *ctx ) { uint processor; if (str_match_no_case( &ctx->cur, "FRAG" )) processor = TGSI_PROCESSOR_FRAGMENT; else if (str_match_no_case( &ctx->cur, "VERT" )) processor = TGSI_PROCESSOR_VERTEX; else if (str_match_no_case( &ctx->cur, "GEOM" )) processor = TGSI_PROCESSOR_GEOMETRY; else { report_error( ctx, "Unknown header" ); return FALSE; } if (ctx->tokens_cur >= ctx->tokens_end) return FALSE; ctx->header = (struct tgsi_header *) ctx->tokens_cur++; *ctx->header = tgsi_build_header(); if (ctx->tokens_cur >= ctx->tokens_end) return FALSE; *(struct tgsi_processor *) ctx->tokens_cur++ = tgsi_build_processor( processor, ctx->header ); ctx->processor = processor; return TRUE; }
static boolean match_inst(const char **pcur, unsigned *saturate, const struct tgsi_opcode_info *info) { const char *cur = *pcur; /* simple case: the whole string matches the instruction name */ if (str_match_nocase_whole(&cur, info->mnemonic)) { *pcur = cur; *saturate = 0; return TRUE; } if (str_match_no_case(&cur, info->mnemonic)) { /* the instruction has a suffix, figure it out */ if (str_match_nocase_whole(&cur, "_SAT")) { *pcur = cur; *saturate = 1; return TRUE; } } return FALSE; }
static boolean match_inst_mnemonic(const char **pcur, const struct tgsi_opcode_info *info) { if (str_match_no_case(pcur, info->mnemonic)) { return TRUE; } return FALSE; }
/* Return TRUE if both strings match. * The first string is be terminated by a non-digit non-letter non-underscore * character, the second string is terminated by zero. * The pointer to the first string is moved at end of the read word * on success. */ static boolean str_match_nocase_whole( const char **pcur, const char *str ) { const char *cur = *pcur; if (str_match_no_case(&cur, str) && !is_digit_alpha_underscore(cur)) { *pcur = cur; return TRUE; } return FALSE; }
static boolean translate( struct translate_ctx *ctx ) { eat_opt_white( &ctx->cur ); if (!parse_header( ctx )) return FALSE; while (*ctx->cur != '\0') { uint label_val = 0; if (!eat_white( &ctx->cur )) { report_error( ctx, "Syntax error" ); return FALSE; } if (*ctx->cur == '\0') break; if (parse_label( ctx, &label_val )) { if (!parse_instruction( ctx, TRUE )) return FALSE; } else if (str_match_no_case( &ctx->cur, "DCL" )) { if (!parse_declaration( ctx )) return FALSE; } else if (str_match_no_case( &ctx->cur, "IMM" )) { if (!parse_immediate( ctx )) return FALSE; } else if (str_match_no_case( &ctx->cur, "PROPERTY" )) { if (!parse_property( ctx )) return FALSE; } else if (!parse_instruction( ctx, FALSE )) { return FALSE; } } return TRUE; }
static boolean parse_fs_coord_pixel_center( const char **pcur, uint *fs_coord_pixel_center ) { uint i; for (i = 0; i < Elements(tgsi_fs_coord_pixel_center_names); i++) { const char *cur = *pcur; if (str_match_no_case( &cur, tgsi_fs_coord_pixel_center_names[i])) { *fs_coord_pixel_center = i; *pcur = cur; return TRUE; } } return FALSE; }
static boolean parse_primitive( const char **pcur, uint *primitive ) { uint i; for (i = 0; i < PIPE_PRIM_MAX; i++) { const char *cur = *pcur; if (str_match_no_case( &cur, tgsi_primitive_names[i])) { *primitive = i; *pcur = cur; return TRUE; } } return FALSE; }
static boolean parse_file( const char **pcur, uint *file ) { uint i; for (i = 0; i < TGSI_FILE_COUNT; i++) { const char *cur = *pcur; if (str_match_no_case( &cur, file_names[i] )) { if (!is_digit_alpha_underscore( cur )) { *pcur = cur; *file = i; return TRUE; } } } return FALSE; }
static boolean parse_immediate( struct translate_ctx *ctx ) { struct tgsi_full_immediate imm; float values[4]; uint advance; if (!eat_white( &ctx->cur )) { report_error( ctx, "Syntax error" ); return FALSE; } if (!str_match_no_case( &ctx->cur, "FLT32" ) || is_digit_alpha_underscore( ctx->cur )) { report_error( ctx, "Expected `FLT32'" ); return FALSE; } parse_immediate_data(ctx, values); imm = tgsi_default_full_immediate(); imm.Immediate.NrTokens += 4; imm.Immediate.DataType = TGSI_IMM_FLOAT32; imm.u[0].Float = values[0]; imm.u[1].Float = values[1]; imm.u[2].Float = values[2]; imm.u[3].Float = values[3]; advance = tgsi_build_full_immediate( &imm, ctx->tokens_cur, ctx->header, (uint) (ctx->tokens_end - ctx->tokens_cur) ); if (advance == 0) return FALSE; ctx->tokens_cur += advance; return TRUE; }
static boolean parse_instruction( struct translate_ctx *ctx, boolean has_label ) { uint i; uint saturate = TGSI_SAT_NONE; const struct tgsi_opcode_info *info; struct tgsi_full_instruction inst; uint advance; inst = tgsi_default_full_instruction(); /* Parse predicate. */ eat_opt_white( &ctx->cur ); if (*ctx->cur == '(') { uint file; int index; uint swizzle[4]; boolean parsed_swizzle; inst.Instruction.Predicate = 1; ctx->cur++; if (*ctx->cur == '!') { ctx->cur++; inst.Predicate.Negate = 1; } if (!parse_register_1d( ctx, &file, &index )) return FALSE; if (parse_optional_swizzle( ctx, swizzle, &parsed_swizzle )) { if (parsed_swizzle) { inst.Predicate.SwizzleX = swizzle[0]; inst.Predicate.SwizzleY = swizzle[1]; inst.Predicate.SwizzleZ = swizzle[2]; inst.Predicate.SwizzleW = swizzle[3]; } } if (*ctx->cur != ')') { report_error( ctx, "Expected `)'" ); return FALSE; } ctx->cur++; } /* Parse instruction name. */ eat_opt_white( &ctx->cur ); for (i = 0; i < TGSI_OPCODE_LAST; i++) { const char *cur = ctx->cur; info = tgsi_get_opcode_info( i ); if (match_inst_mnemonic(&cur, info)) { if (str_match_no_case( &cur, "_SATNV" )) saturate = TGSI_SAT_MINUS_PLUS_ONE; else if (str_match_no_case( &cur, "_SAT" )) saturate = TGSI_SAT_ZERO_ONE; if (info->num_dst + info->num_src + info->is_tex == 0) { if (!is_digit_alpha_underscore( cur )) { ctx->cur = cur; break; } } else if (*cur == '\0' || eat_white( &cur )) { ctx->cur = cur; break; } } } if (i == TGSI_OPCODE_LAST) { if (has_label) report_error( ctx, "Unknown opcode" ); else report_error( ctx, "Expected `DCL', `IMM' or a label" ); return FALSE; } inst.Instruction.Opcode = i; inst.Instruction.Saturate = saturate; inst.Instruction.NumDstRegs = info->num_dst; inst.Instruction.NumSrcRegs = info->num_src; /* Parse instruction operands. */ for (i = 0; i < info->num_dst + info->num_src + info->is_tex; i++) { if (i > 0) { eat_opt_white( &ctx->cur ); if (*ctx->cur != ',') { report_error( ctx, "Expected `,'" ); return FALSE; } ctx->cur++; eat_opt_white( &ctx->cur ); } if (i < info->num_dst) { if (!parse_dst_operand( ctx, &inst.Dst[i] )) return FALSE; } else if (i < info->num_dst + info->num_src) { if (!parse_src_operand( ctx, &inst.Src[i - info->num_dst] )) return FALSE; } else { uint j; for (j = 0; j < TGSI_TEXTURE_COUNT; j++) { if (str_match_no_case( &ctx->cur, texture_names[j] )) { if (!is_digit_alpha_underscore( ctx->cur )) { inst.Instruction.Texture = 1; inst.Texture.Texture = j; break; } } } if (j == TGSI_TEXTURE_COUNT) { report_error( ctx, "Expected texture target" ); return FALSE; } } } if (info->is_branch) { uint target; eat_opt_white( &ctx->cur ); if (*ctx->cur != ':') { report_error( ctx, "Expected `:'" ); return FALSE; } ctx->cur++; eat_opt_white( &ctx->cur ); if (!parse_uint( &ctx->cur, &target )) { report_error( ctx, "Expected a label" ); return FALSE; } inst.Instruction.Label = 1; inst.Label.Label = target; } advance = tgsi_build_full_instruction( &inst, ctx->tokens_cur, ctx->header, (uint) (ctx->tokens_end - ctx->tokens_cur) ); if (advance == 0) return FALSE; ctx->tokens_cur += advance; return TRUE; }
static boolean parse_declaration( struct translate_ctx *ctx ) { struct tgsi_full_declaration decl; uint file; struct parsed_dcl_bracket brackets[2]; int num_brackets; uint writemask; const char *cur; uint advance; boolean is_vs_input; boolean is_imm_array; assert(Elements(semantic_names) == TGSI_SEMANTIC_COUNT); assert(Elements(interpolate_names) == TGSI_INTERPOLATE_COUNT); if (!eat_white( &ctx->cur )) { report_error( ctx, "Syntax error" ); return FALSE; } if (!parse_register_dcl( ctx, &file, brackets, &num_brackets)) return FALSE; if (!parse_opt_writemask( ctx, &writemask )) return FALSE; decl = tgsi_default_full_declaration(); decl.Declaration.File = file; decl.Declaration.UsageMask = writemask; if (num_brackets == 1) { decl.Range.First = brackets[0].first; decl.Range.Last = brackets[0].last; } else { decl.Range.First = brackets[1].first; decl.Range.Last = brackets[1].last; decl.Declaration.Dimension = 1; decl.Dim.Index2D = brackets[0].first; } is_vs_input = (file == TGSI_FILE_INPUT && ctx->processor == TGSI_PROCESSOR_VERTEX); is_imm_array = (file == TGSI_FILE_IMMEDIATE_ARRAY); cur = ctx->cur; eat_opt_white( &cur ); if (*cur == ',' && !is_vs_input) { uint i, j; cur++; eat_opt_white( &cur ); if (file == TGSI_FILE_RESOURCE) { for (i = 0; i < TGSI_TEXTURE_COUNT; i++) { if (str_match_no_case(&cur, texture_names[i])) { if (!is_digit_alpha_underscore(cur)) { decl.Resource.Resource = i; break; } } } if (i == TGSI_TEXTURE_COUNT) { report_error(ctx, "Expected texture target"); return FALSE; } eat_opt_white( &cur ); if (*cur != ',') { report_error( ctx, "Expected `,'" ); return FALSE; } ++cur; eat_opt_white( &cur ); for (j = 0; j < 4; ++j) { for (i = 0; i < PIPE_TYPE_COUNT; ++i) { if (str_match_no_case(&cur, type_names[i])) { if (!is_digit_alpha_underscore(cur)) { switch (j) { case 0: decl.Resource.ReturnTypeX = i; break; case 1: decl.Resource.ReturnTypeY = i; break; case 2: decl.Resource.ReturnTypeZ = i; break; case 3: decl.Resource.ReturnTypeW = i; break; default: assert(0); } break; } } } if (i == PIPE_TYPE_COUNT) { if (j == 0 || j > 2) { report_error(ctx, "Expected type name"); return FALSE; } break; } else { const char *cur2 = cur; eat_opt_white( &cur2 ); if (*cur2 == ',') { cur2++; eat_opt_white( &cur2 ); cur = cur2; continue; } else break; } } if (j < 4) { decl.Resource.ReturnTypeY = decl.Resource.ReturnTypeZ = decl.Resource.ReturnTypeW = decl.Resource.ReturnTypeX; } ctx->cur = cur; } else { for (i = 0; i < TGSI_SEMANTIC_COUNT; i++) { if (str_match_no_case( &cur, semantic_names[i] )) { const char *cur2 = cur; uint index; if (is_digit_alpha_underscore( cur )) continue; eat_opt_white( &cur2 ); if (*cur2 == '[') { cur2++; eat_opt_white( &cur2 ); if (!parse_uint( &cur2, &index )) { report_error( ctx, "Expected literal integer" ); return FALSE; } eat_opt_white( &cur2 ); if (*cur2 != ']') { report_error( ctx, "Expected `]'" ); return FALSE; } cur2++; decl.Semantic.Index = index; cur = cur2; } decl.Declaration.Semantic = 1; decl.Semantic.Name = i; ctx->cur = cur; break; } } } } else if (is_imm_array) { unsigned i; float *vals_itr; /* we have our immediate data */ if (*cur != '{') { report_error( ctx, "Immediate array without data" ); return FALSE; } ++cur; ctx->cur = cur; decl.ImmediateData.u = MALLOC(sizeof(union tgsi_immediate_data) * 4 * (decl.Range.Last + 1)); vals_itr = (float*)decl.ImmediateData.u; for (i = 0; i <= decl.Range.Last; ++i) { if (!parse_immediate_data(ctx, vals_itr)) { FREE(decl.ImmediateData.u); return FALSE; } vals_itr += 4; eat_opt_white( &ctx->cur ); if (*ctx->cur != ',') { if (i != decl.Range.Last) { report_error( ctx, "Not enough data in immediate array!" ); FREE(decl.ImmediateData.u); return FALSE; } } else ++ctx->cur; } eat_opt_white( &ctx->cur ); if (*ctx->cur != '}') { FREE(decl.ImmediateData.u); report_error( ctx, "Immediate array data missing closing '}'" ); return FALSE; } ++ctx->cur; } cur = ctx->cur; eat_opt_white( &cur ); if (*cur == ',' && !is_vs_input) { uint i; cur++; eat_opt_white( &cur ); for (i = 0; i < TGSI_INTERPOLATE_COUNT; i++) { if (str_match_no_case( &cur, interpolate_names[i] )) { if (is_digit_alpha_underscore( cur )) continue; decl.Declaration.Interpolate = i; ctx->cur = cur; break; } } if (i == TGSI_INTERPOLATE_COUNT) { report_error( ctx, "Expected semantic or interpolate attribute" ); return FALSE; } } advance = tgsi_build_full_declaration( &decl, ctx->tokens_cur, ctx->header, (uint) (ctx->tokens_end - ctx->tokens_cur) ); if (is_imm_array) FREE(decl.ImmediateData.u); if (advance == 0) return FALSE; ctx->tokens_cur += advance; return TRUE; }