minimize token checks

This commit is contained in:
tigrazone
2017-12-08 21:53:36 +02:00
parent 5d7f6bf539
commit c016910317

View File

@@ -946,6 +946,7 @@ static inline texture_type_t parseTextureType(
const char **token, texture_type_t default_value = TEXTURE_TYPE_NONE) { const char **token, texture_type_t default_value = TEXTURE_TYPE_NONE) {
(*token) += strspn((*token), " \t"); (*token) += strspn((*token), " \t");
const char *end = (*token) + strcspn((*token), " \t\r"); const char *end = (*token) + strcspn((*token), " \t\r");
texture_type_t ty = default_value; texture_type_t ty = default_value;
@@ -957,42 +958,47 @@ static inline texture_type_t parseTextureType(
a_tok = token2tok(*token); a_tok = token2tok(*token);
//tigra: dont check if wrong token
if(a_tok>=TOK_cube_top && a_tok<=TOK_sphere)
{
//if ((0 == strncmp((*token), "cube_top", strlen("cube_top")))) //if ((0 == strncmp((*token), "cube_top", strlen("cube_top"))))
if (a_tok == TOK_cube_top) if (a_tok == TOK_cube_top)
{
ty = TEXTURE_TYPE_CUBE_TOP;
} else
//if ((0 == strncmp((*token), "cube_bottom", strlen("cube_bottom"))))
if (a_tok == TOK_cube_bottom)
{ {
ty = TEXTURE_TYPE_CUBE_BOTTOM; ty = TEXTURE_TYPE_CUBE_TOP;
} else } else
//if ((0 == strncmp((*token), "cube_left", strlen("cube_left")))) //if ((0 == strncmp((*token), "cube_bottom", strlen("cube_bottom"))))
if (a_tok == TOK_cube_left) if (a_tok == TOK_cube_bottom)
{ {
ty = TEXTURE_TYPE_CUBE_LEFT; ty = TEXTURE_TYPE_CUBE_BOTTOM;
} else } else
//if ((0 == strncmp((*token), "cube_right", strlen("cube_right")))) //if ((0 == strncmp((*token), "cube_left", strlen("cube_left"))))
if (a_tok == TOK_cube_right) if (a_tok == TOK_cube_left)
{ {
ty = TEXTURE_TYPE_CUBE_RIGHT; ty = TEXTURE_TYPE_CUBE_LEFT;
} else } else
//if ((0 == strncmp((*token), "cube_front", strlen("cube_front")))) //if ((0 == strncmp((*token), "cube_right", strlen("cube_right"))))
if (a_tok == TOK_cube_front) if (a_tok == TOK_cube_right)
{ {
ty = TEXTURE_TYPE_CUBE_FRONT; ty = TEXTURE_TYPE_CUBE_RIGHT;
} else } else
//if ((0 == strncmp((*token), "cube_back", strlen("cube_back")))) //if ((0 == strncmp((*token), "cube_front", strlen("cube_front"))))
if (a_tok == TOK_cube_back) if (a_tok == TOK_cube_front)
{ {
ty = TEXTURE_TYPE_CUBE_BACK; ty = TEXTURE_TYPE_CUBE_FRONT;
} else } else
//if ((0 == strncmp((*token), "sphere", strlen("sphere")))) //if ((0 == strncmp((*token), "cube_back", strlen("cube_back"))))
if (a_tok == TOK_sphere) if (a_tok == TOK_cube_back)
{ {
ty = TEXTURE_TYPE_SPHERE; ty = TEXTURE_TYPE_CUBE_BACK;
} } else
//if ((0 == strncmp((*token), "sphere", strlen("sphere"))))
if (a_tok == TOK_sphere)
{
ty = TEXTURE_TYPE_SPHERE;
}
}
(*token) = end; (*token) = end;
return ty; return ty;
@@ -1155,82 +1161,86 @@ static bool ParseTextureNameAndOption(std::string *texname,
token += strspn(token, " \t"); // skip space token += strspn(token, " \t"); // skip space
a_tok = token2tok(token); a_tok = token2tok(token);
//if ((0 == strncmp(token, "-blendu", 7)) && IS_SPACE((token[7]))) //tigra: minimize checks
if (a_tok == TOK_blendu) if(a_tok>=TOK_blendu && a_tok<=TOK_mm)
{ {
token += 8; //if ((0 == strncmp(token, "-blendu", 7)) && IS_SPACE((token[7])))
texopt->blendu = parseOnOff(&token, /* default */ true); if (a_tok == TOK_blendu)
} else {
//if ((0 == strncmp(token, "-blendv", 7)) && IS_SPACE((token[7]))) token += 8;
if (a_tok == TOK_blendv) texopt->blendu = parseOnOff(&token, /* default */ true);
{ } else
token += 8; //if ((0 == strncmp(token, "-blendv", 7)) && IS_SPACE((token[7])))
texopt->blendv = parseOnOff(&token, /* default */ true); if (a_tok == TOK_blendv)
} else {
//if ((0 == strncmp(token, "-clamp", 6)) && IS_SPACE((token[6]))) token += 8;
if (a_tok == TOK_clamp) texopt->blendv = parseOnOff(&token, /* default */ true);
{ } else
token += 7; //if ((0 == strncmp(token, "-clamp", 6)) && IS_SPACE((token[6])))
texopt->clamp = parseOnOff(&token, /* default */ true); if (a_tok == TOK_clamp)
} else {
//if ((0 == strncmp(token, "-boost", 6)) && IS_SPACE((token[6]))) token += 7;
if (a_tok == TOK_boost) texopt->clamp = parseOnOff(&token, /* default */ true);
{ } else
token += 7; //if ((0 == strncmp(token, "-boost", 6)) && IS_SPACE((token[6])))
texopt->sharpness = parseReal(&token, 1.0); if (a_tok == TOK_boost)
} else {
//if ((0 == strncmp(token, "-bm", 3)) && IS_SPACE((token[3]))) token += 7;
if (a_tok == TOK_bm) texopt->sharpness = parseReal(&token, 1.0);
{ } else
token += 4; //if ((0 == strncmp(token, "-bm", 3)) && IS_SPACE((token[3])))
texopt->bump_multiplier = parseReal(&token, 1.0); if (a_tok == TOK_bm)
} else {
//if ((0 == strncmp(token, "-o", 2)) && IS_SPACE((token[2]))) token += 4;
if (a_tok == TOK_o) texopt->bump_multiplier = parseReal(&token, 1.0);
{ } else
token += 3; //if ((0 == strncmp(token, "-o", 2)) && IS_SPACE((token[2])))
parseReal3(&(texopt->origin_offset[0]), &(texopt->origin_offset[1]), if (a_tok == TOK_o)
&(texopt->origin_offset[2]), &token); {
} else token += 3;
//if ((0 == strncmp(token, "-s", 2)) && IS_SPACE((token[2]))) parseReal3(&(texopt->origin_offset[0]), &(texopt->origin_offset[1]),
if (a_tok == TOK_s) &(texopt->origin_offset[2]), &token);
{ } else
token += 3; //if ((0 == strncmp(token, "-s", 2)) && IS_SPACE((token[2])))
parseReal3(&(texopt->scale[0]), &(texopt->scale[1]), &(texopt->scale[2]), if (a_tok == TOK_s)
&token, 1.0, 1.0, 1.0); {
} else token += 3;
//if ((0 == strncmp(token, "-t", 2)) && IS_SPACE((token[2]))) parseReal3(&(texopt->scale[0]), &(texopt->scale[1]), &(texopt->scale[2]),
if (a_tok == TOK_t) &token, 1.0, 1.0, 1.0);
{ } else
token += 3; //if ((0 == strncmp(token, "-t", 2)) && IS_SPACE((token[2])))
parseReal3(&(texopt->turbulence[0]), &(texopt->turbulence[1]), if (a_tok == TOK_t)
&(texopt->turbulence[2]), &token); {
} else token += 3;
//if ((0 == strncmp(token, "-type", 5)) && IS_SPACE((token[5]))) parseReal3(&(texopt->turbulence[0]), &(texopt->turbulence[1]),
if (a_tok == TOK_type) &(texopt->turbulence[2]), &token);
{ } else
token += 5; //if ((0 == strncmp(token, "-type", 5)) && IS_SPACE((token[5])))
texopt->type = parseTextureType((&token), TEXTURE_TYPE_NONE); if (a_tok == TOK_type)
} else {
//if ((0 == strncmp(token, "-imfchan", 8)) && IS_SPACE((token[8]))) token += 5;
if (a_tok == TOK_imfchan) texopt->type = parseTextureType((&token), TEXTURE_TYPE_NONE);
{ } else
token += 9; //if ((0 == strncmp(token, "-imfchan", 8)) && IS_SPACE((token[8])))
token += strspn(token, " \t"); if (a_tok == TOK_imfchan)
const char *end = token + strcspn(token, " \t\r"); {
if ((end - token) == 1) { // Assume one char for -imfchan token += 9;
texopt->imfchan = (*token); token += strspn(token, " \t");
} const char *end = token + strcspn(token, " \t\r");
token = end; if ((end - token) == 1) { // Assume one char for -imfchan
} else texopt->imfchan = (*token);
//if ((0 == strncmp(token, "-mm", 3)) && IS_SPACE((token[3]))) }
if (a_tok == TOK_mm) token = end;
{ } else
token += 4; //if ((0 == strncmp(token, "-mm", 3)) && IS_SPACE((token[3])))
parseReal2(&(texopt->brightness), &(texopt->contrast), &token, 0.0, 1.0); if (a_tok == TOK_mm)
} else { {
token += 4;
parseReal2(&(texopt->brightness), &(texopt->contrast), &token, 0.0, 1.0);
}
}
else {
// Assume texture filename // Assume texture filename
#if 0 #if 0
size_t len = strcspn(token, " \t\r"); // untile next space size_t len = strcspn(token, " \t\r"); // untile next space
@@ -1545,240 +1555,244 @@ void LoadMtl(std::map<std::string, int> *material_map,
} }
a_tok = token2tok(token); a_tok = token2tok(token);
//tigra: minimize checks
//tigra: refactoring for new speedup release if(a_tok>=TOK_newmtl && a_tok<=TOK_norm)
// new mtl
//if ((0 == strncmp(token, "newmtl", 6)) && IS_SPACE((token[6])))
if (a_tok == TOK_newmtl)
{ {
// flush previous material.
if (!material.name.empty()) { //tigra: refactoring for new speedup release
material_map->insert(std::pair<std::string, int>( // new mtl
material.name, static_cast<int>(materials->size()))); //if ((0 == strncmp(token, "newmtl", 6)) && IS_SPACE((token[6])))
materials->push_back(material); if (a_tok == TOK_newmtl)
} {
// flush previous material.
if (!material.name.empty()) {
material_map->insert(std::pair<std::string, int>(
material.name, static_cast<int>(materials->size())));
materials->push_back(material);
}
// initial temporary material // initial temporary material
InitMaterial(&material); InitMaterial(&material);
has_d = false; has_d = false;
has_tr = false; has_tr = false;
// set new mtl name // set new mtl name
token += 7; token += 7;
{ {
std::stringstream sstr; std::stringstream sstr;
sstr << token; sstr << token;
material.name = sstr.str(); material.name = sstr.str();
} }
continue; continue;
} }
// illum model // illum model
//if (0 == strncmp(token, "illum", 5) && IS_SPACE(token[5])) //if (0 == strncmp(token, "illum", 5) && IS_SPACE(token[5]))
if (a_tok == TOK_illum) if (a_tok == TOK_illum)
{ {
token += 6; token += 6;
material.illum = parseInt(&token); material.illum = parseInt(&token);
continue; continue;
} }
// PBR: clearcoat roughness // PBR: clearcoat roughness
//if ((0 == strncmp(token, "Pcr", 3)) && IS_SPACE(token[3])) //if ((0 == strncmp(token, "Pcr", 3)) && IS_SPACE(token[3]))
if (a_tok == TOK_Pcr) if (a_tok == TOK_Pcr)
{ {
token += 4; token += 4;
material.clearcoat_roughness = parseReal(&token); material.clearcoat_roughness = parseReal(&token);
continue; continue;
} }
// PBR: anisotropy // PBR: anisotropy
//if ((0 == strncmp(token, "aniso", 5)) && IS_SPACE(token[5])) //if ((0 == strncmp(token, "aniso", 5)) && IS_SPACE(token[5]))
if (a_tok == TOK_aniso) if (a_tok == TOK_aniso)
{ {
token += 6; token += 6;
material.anisotropy = parseReal(&token); material.anisotropy = parseReal(&token);
continue; continue;
} }
// PBR: anisotropy rotation // PBR: anisotropy rotation
//if ((0 == strncmp(token, "anisor", 6)) && IS_SPACE(token[6])) //if ((0 == strncmp(token, "anisor", 6)) && IS_SPACE(token[6]))
if (a_tok == TOK_anisor) if (a_tok == TOK_anisor)
{ {
token += 7; token += 7;
material.anisotropy_rotation = parseReal(&token); material.anisotropy_rotation = parseReal(&token);
continue; continue;
} }
// ambient texture // ambient texture
//if ((0 == strncmp(token, "map_Ka", 6)) && IS_SPACE(token[6])) //if ((0 == strncmp(token, "map_Ka", 6)) && IS_SPACE(token[6]))
if (a_tok == TOK_map_Ka) if (a_tok == TOK_map_Ka)
{ {
token += 7; token += 7;
ParseTextureNameAndOption(&(material.ambient_texname), ParseTextureNameAndOption(&(material.ambient_texname),
&(material.ambient_texopt), token, &(material.ambient_texopt), token,
/* is_bump */ false); /* is_bump */ false);
continue; continue;
} }
// diffuse texture // diffuse texture
//if ((0 == strncmp(token, "map_Kd", 6)) && IS_SPACE(token[6])) //if ((0 == strncmp(token, "map_Kd", 6)) && IS_SPACE(token[6]))
if (a_tok == TOK_map_Kd) if (a_tok == TOK_map_Kd)
{ {
token += 7; token += 7;
ParseTextureNameAndOption(&(material.diffuse_texname), ParseTextureNameAndOption(&(material.diffuse_texname),
&(material.diffuse_texopt), token, &(material.diffuse_texopt), token,
/* is_bump */ false); /* is_bump */ false);
continue; continue;
} }
// specular texture // specular texture
//if ((0 == strncmp(token, "map_Ks", 6)) && IS_SPACE(token[6])) //if ((0 == strncmp(token, "map_Ks", 6)) && IS_SPACE(token[6]))
if (a_tok == TOK_map_Ks) if (a_tok == TOK_map_Ks)
{ {
token += 7; token += 7;
ParseTextureNameAndOption(&(material.specular_texname), ParseTextureNameAndOption(&(material.specular_texname),
&(material.specular_texopt), token, &(material.specular_texopt), token,
/* is_bump */ false); /* is_bump */ false);
continue; continue;
} }
// specular highlight texture // specular highlight texture
//if ((0 == strncmp(token, "map_Ns", 6)) && IS_SPACE(token[6])) //if ((0 == strncmp(token, "map_Ns", 6)) && IS_SPACE(token[6]))
if (a_tok == TOK_map_Ns) if (a_tok == TOK_map_Ns)
{ {
token += 7; token += 7;
ParseTextureNameAndOption(&(material.specular_highlight_texname), ParseTextureNameAndOption(&(material.specular_highlight_texname),
&(material.specular_highlight_texopt), token, &(material.specular_highlight_texopt), token,
/* is_bump */ false); /* is_bump */ false);
continue; continue;
} }
// bump texture // bump texture
//if ((0 == strncmp(token, "map_bump", 8)) && IS_SPACE(token[8])) //if ((0 == strncmp(token, "map_bump", 8)) && IS_SPACE(token[8]))
if (a_tok == TOK_map_bump) if (a_tok == TOK_map_bump)
{ {
token += 9; token += 9;
ParseTextureNameAndOption(&(material.bump_texname), ParseTextureNameAndOption(&(material.bump_texname),
&(material.bump_texopt), token, &(material.bump_texopt), token,
/* is_bump */ true); /* is_bump */ true);
continue; continue;
} }
// bump texture // bump texture
//if ((0 == strncmp(token, "map_Bump", 8)) && IS_SPACE(token[8])) //if ((0 == strncmp(token, "map_Bump", 8)) && IS_SPACE(token[8]))
if (a_tok == TOK_map_Bump) if (a_tok == TOK_map_Bump)
{ {
token += 9; token += 9;
ParseTextureNameAndOption(&(material.bump_texname), ParseTextureNameAndOption(&(material.bump_texname),
&(material.bump_texopt), token, &(material.bump_texopt), token,
/* is_bump */ true); /* is_bump */ true);
continue; continue;
} }
// bump texture // bump texture
//if ((0 == strncmp(token, "bump", 4)) && IS_SPACE(token[4])) //if ((0 == strncmp(token, "bump", 4)) && IS_SPACE(token[4]))
if (a_tok == TOK_bump) if (a_tok == TOK_bump)
{ {
token += 5; token += 5;
ParseTextureNameAndOption(&(material.bump_texname), ParseTextureNameAndOption(&(material.bump_texname),
&(material.bump_texopt), token, &(material.bump_texopt), token,
/* is_bump */ true); /* is_bump */ true);
continue; continue;
} }
// alpha texture // alpha texture
//if ((0 == strncmp(token, "map_d", 5)) && IS_SPACE(token[5])) //if ((0 == strncmp(token, "map_d", 5)) && IS_SPACE(token[5]))
if (a_tok == TOK_map_d) if (a_tok == TOK_map_d)
{ {
token += 6; token += 6;
material.alpha_texname = token; material.alpha_texname = token;
ParseTextureNameAndOption(&(material.alpha_texname), ParseTextureNameAndOption(&(material.alpha_texname),
&(material.alpha_texopt), token, &(material.alpha_texopt), token,
/* is_bump */ false); /* is_bump */ false);
continue; continue;
} }
// displacement texture // displacement texture
//if ((0 == strncmp(token, "disp", 4)) && IS_SPACE(token[4])) //if ((0 == strncmp(token, "disp", 4)) && IS_SPACE(token[4]))
if (a_tok == TOK_disp) if (a_tok == TOK_disp)
{ {
token += 5; token += 5;
ParseTextureNameAndOption(&(material.displacement_texname), ParseTextureNameAndOption(&(material.displacement_texname),
&(material.displacement_texopt), token, &(material.displacement_texopt), token,
/* is_bump */ false); /* is_bump */ false);
continue; continue;
} }
// reflection map // reflection map
//if ((0 == strncmp(token, "refl", 4)) && IS_SPACE(token[4])) //if ((0 == strncmp(token, "refl", 4)) && IS_SPACE(token[4]))
if (a_tok == TOK_refl) if (a_tok == TOK_refl)
{ {
token += 5; token += 5;
ParseTextureNameAndOption(&(material.reflection_texname), ParseTextureNameAndOption(&(material.reflection_texname),
&(material.reflection_texopt), token, &(material.reflection_texopt), token,
/* is_bump */ false); /* is_bump */ false);
continue; continue;
} }
// PBR: roughness texture // PBR: roughness texture
//if ((0 == strncmp(token, "map_Pr", 6)) && IS_SPACE(token[6])) //if ((0 == strncmp(token, "map_Pr", 6)) && IS_SPACE(token[6]))
if (a_tok == TOK_map_Pr) if (a_tok == TOK_map_Pr)
{ {
token += 7; token += 7;
ParseTextureNameAndOption(&(material.roughness_texname), ParseTextureNameAndOption(&(material.roughness_texname),
&(material.roughness_texopt), token, &(material.roughness_texopt), token,
/* is_bump */ false); /* is_bump */ false);
continue; continue;
} }
// PBR: metallic texture // PBR: metallic texture
//if ((0 == strncmp(token, "map_Pm", 6)) && IS_SPACE(token[6])) //if ((0 == strncmp(token, "map_Pm", 6)) && IS_SPACE(token[6]))
if (a_tok == TOK_map_Pm) if (a_tok == TOK_map_Pm)
{ {
token += 7; token += 7;
ParseTextureNameAndOption(&(material.metallic_texname), ParseTextureNameAndOption(&(material.metallic_texname),
&(material.metallic_texopt), token, &(material.metallic_texopt), token,
/* is_bump */ false); /* is_bump */ false);
continue; continue;
} }
// PBR: sheen texture // PBR: sheen texture
//if ((0 == strncmp(token, "map_Ps", 6)) && IS_SPACE(token[6])) //if ((0 == strncmp(token, "map_Ps", 6)) && IS_SPACE(token[6]))
if (a_tok == TOK_map_Ps) if (a_tok == TOK_map_Ps)
{ {
token += 7; token += 7;
ParseTextureNameAndOption(&(material.sheen_texname), ParseTextureNameAndOption(&(material.sheen_texname),
&(material.sheen_texopt), token, &(material.sheen_texopt), token,
/* is_bump */ false); /* is_bump */ false);
continue; continue;
} }
// PBR: emissive texture // PBR: emissive texture
//if ((0 == strncmp(token, "map_Ke", 6)) && IS_SPACE(token[6])) //if ((0 == strncmp(token, "map_Ke", 6)) && IS_SPACE(token[6]))
if (a_tok == TOK_map_Ke) if (a_tok == TOK_map_Ke)
{ {
token += 7; token += 7;
ParseTextureNameAndOption(&(material.emissive_texname), ParseTextureNameAndOption(&(material.emissive_texname),
&(material.emissive_texopt), token, &(material.emissive_texopt), token,
/* is_bump */ false); /* is_bump */ false);
continue; continue;
} }
// PBR: normal map texture // PBR: normal map texture
//if ((0 == strncmp(token, "norm", 4)) && IS_SPACE(token[4])) //if ((0 == strncmp(token, "norm", 4)) && IS_SPACE(token[4]))
if (a_tok == TOK_norm) if (a_tok == TOK_norm)
{ {
token += 5; token += 5;
ParseTextureNameAndOption( ParseTextureNameAndOption(
&(material.normal_texname), &(material.normal_texopt), token, &(material.normal_texname), &(material.normal_texopt), token,
/* is_bump */ false); // @fixme { is_bump will be true? } /* is_bump */ false); // @fixme { is_bump will be true? }
continue; continue;
} }
}
// unknown parameter // unknown parameter
const char *_space = strchr(token, ' '); const char *_space = strchr(token, ' ');