Skip to content

Commit d70d2a0

Browse files
committed
nv2a: Fix material color source ambient and emissive
1 parent 39fef7e commit d70d2a0

File tree

1 file changed

+37
-17
lines changed

1 file changed

+37
-17
lines changed

hw/xbox/nv2a/pgraph/glsl/vsh-ff.c

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -249,21 +249,20 @@ GLSL_DEFINE(materialEmissionColor, GLSL_LTCTXA(NV_IGRAPH_XF_LTCTXA_CM_COL) ".xyz
249249
alpha_source = alpha_source_specular;
250250
}
251251

252-
if (state->ambient_src == MATERIAL_COLOR_SRC_MATERIAL) {
253-
mstring_append_fmt(body, "oD0 = vec4(sceneAmbientColor, %s);\n", alpha_source);
254-
} else if (state->ambient_src == MATERIAL_COLOR_SRC_DIFFUSE) {
255-
mstring_append_fmt(body, "oD0 = vec4(diffuse.rgb, %s);\n", alpha_source);
256-
} else if (state->ambient_src == MATERIAL_COLOR_SRC_SPECULAR) {
257-
mstring_append_fmt(body, "oD0 = vec4(specular.rgb, %s);\n", alpha_source);
258-
}
259-
260-
mstring_append(body, "oD0.rgb *= materialEmissionColor.rgb;\n");
261-
if (state->emission_src == MATERIAL_COLOR_SRC_MATERIAL) {
262-
mstring_append(body, "oD0.rgb += sceneAmbientColor;\n");
263-
} else if (state->emission_src == MATERIAL_COLOR_SRC_DIFFUSE) {
264-
mstring_append(body, "oD0.rgb += diffuse.rgb;\n");
265-
} else if (state->emission_src == MATERIAL_COLOR_SRC_SPECULAR) {
266-
mstring_append(body, "oD0.rgb += specular.rgb;\n");
252+
// If emission and ambient sources are taken from a vertex color, the scene ambient color is reconstructed from
253+
// the vertex colors.
254+
bool use_scene_ambient =
255+
state->emission_src == MATERIAL_COLOR_SRC_MATERIAL || state->ambient_src == MATERIAL_COLOR_SRC_MATERIAL;
256+
mstring_append_fmt(body,
257+
"oD0 = vec4(%s, %s);\n",
258+
use_scene_ambient ? "sceneAmbientColor" : "0, 0, 0",
259+
alpha_source
260+
);
261+
262+
if (state->emission_src == MATERIAL_COLOR_SRC_DIFFUSE || state->ambient_src == MATERIAL_COLOR_SRC_DIFFUSE) {
263+
mstring_append(body, "oD0.rgb += diffuse.rgb * materialEmissionColor;\n");
264+
} else if (state->emission_src == MATERIAL_COLOR_SRC_SPECULAR || state->ambient_src == MATERIAL_COLOR_SRC_SPECULAR) {
265+
mstring_append(body, "oD0.rgb += specular.rgb * materialEmissionColor;\n");
267266
}
268267

269268
mstring_append(body, "oD1 = vec4(0.0, 0.0, 0.0, specular.a);\n");
@@ -368,8 +367,29 @@ GLSL_DEFINE(materialEmissionColor, GLSL_LTCTXA(NV_IGRAPH_XF_LTCTXA_CM_COL) ".xyz
368367
" vec3 lightSpecular = lightSpecularColor(%d) * attenuation * pf;\n",
369368
state->specular_power, i, i, i);
370369

371-
mstring_append(body,
372-
" oD0.xyz += lightAmbient;\n");
370+
if (use_scene_ambient) {
371+
switch (state->ambient_src) {
372+
case MATERIAL_COLOR_SRC_MATERIAL:
373+
mstring_append(body,
374+
" oD0.rgb += lightAmbient;\n");
375+
break;
376+
case MATERIAL_COLOR_SRC_DIFFUSE:
377+
mstring_append(body,
378+
" oD0.rgb += diffuse.rgb * lightAmbient;\n");
379+
break;
380+
case MATERIAL_COLOR_SRC_SPECULAR:
381+
mstring_append(body,
382+
" oD0.rgb += specular.rgb * lightAmbient;\n");
383+
break;
384+
}
385+
} else {
386+
mstring_append_fmt(
387+
body,
388+
" oD0.rgb += %s.rgb * lightAmbient + %s.rgb;\n",
389+
state->emission_src == MATERIAL_COLOR_SRC_DIFFUSE ? "diffuse" : "specular",
390+
state->ambient_src == MATERIAL_COLOR_SRC_DIFFUSE ? "diffuse" : "specular"
391+
);
392+
}
373393

374394
switch (state->diffuse_src) {
375395
case MATERIAL_COLOR_SRC_MATERIAL:

0 commit comments

Comments
 (0)