Merge pull request #51676 from Calinou/shader-add-hint-transparent-texture-3.x

Add `hint_transparent` to use a transparent black placeholder texture (3.x)
This commit is contained in:
Rémi Verschelde 2022-08-05 22:34:05 +02:00 committed by GitHub
commit 144f3ec94e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 66 additions and 12 deletions

View file

@ -1682,6 +1682,9 @@ void RasterizerCanvasGLES2::_legacy_canvas_render_item(Item *p_ci, RenderItemSta
case ShaderLanguage::ShaderNode::Uniform::HINT_BLACK: {
glBindTexture(GL_TEXTURE_2D, storage->resources.black_tex);
} break;
case ShaderLanguage::ShaderNode::Uniform::HINT_TRANSPARENT: {
glBindTexture(GL_TEXTURE_2D, storage->resources.transparent_tex);
} break;
case ShaderLanguage::ShaderNode::Uniform::HINT_ANISO: {
glBindTexture(GL_TEXTURE_2D, storage->resources.aniso_tex);
} break;
@ -2044,6 +2047,9 @@ void RasterizerCanvasGLES2::render_joined_item(const BItemJoined &p_bij, RenderI
case ShaderLanguage::ShaderNode::Uniform::HINT_BLACK: {
glBindTexture(GL_TEXTURE_2D, storage->resources.black_tex);
} break;
case ShaderLanguage::ShaderNode::Uniform::HINT_TRANSPARENT: {
glBindTexture(GL_TEXTURE_2D, storage->resources.transparent_tex);
} break;
case ShaderLanguage::ShaderNode::Uniform::HINT_ANISO: {
glBindTexture(GL_TEXTURE_2D, storage->resources.aniso_tex);
} break;

View file

@ -1420,6 +1420,9 @@ bool RasterizerSceneGLES2::_setup_material(RasterizerStorageGLES2::Material *p_m
case ShaderLanguage::ShaderNode::Uniform::HINT_BLACK: {
glBindTexture(GL_TEXTURE_2D, storage->resources.black_tex);
} break;
case ShaderLanguage::ShaderNode::Uniform::HINT_TRANSPARENT: {
glBindTexture(GL_TEXTURE_2D, storage->resources.transparent_tex);
} break;
case ShaderLanguage::ShaderNode::Uniform::HINT_ANISO: {
glBindTexture(GL_TEXTURE_2D, storage->resources.aniso_tex);
} break;

View file

@ -6456,32 +6456,45 @@ void RasterizerStorageGLES2::initialize() {
}
{
//default textures
// Generate default textures.
// Opaque white color.
glGenTextures(1, &resources.white_tex);
unsigned char whitetexdata[8 * 8 * 3];
for (int i = 0; i < 8 * 8 * 3; i++) {
whitetexdata[i] = 255;
}
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, resources.white_tex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 8, 8, 0, GL_RGB, GL_UNSIGNED_BYTE, whitetexdata);
glGenerateMipmap(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, 0);
// Opaque black color.
glGenTextures(1, &resources.black_tex);
unsigned char blacktexdata[8 * 8 * 3];
for (int i = 0; i < 8 * 8 * 3; i++) {
blacktexdata[i] = 0;
}
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, resources.black_tex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 8, 8, 0, GL_RGB, GL_UNSIGNED_BYTE, blacktexdata);
glGenerateMipmap(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, 0);
// Transparent black color.
glGenTextures(1, &resources.transparent_tex);
unsigned char transparenttexdata[8 * 8 * 4];
for (int i = 0; i < 8 * 8 * 4; i++) {
transparenttexdata[i] = 0;
}
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, resources.transparent_tex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 8, 8, 0, GL_RGBA, GL_UNSIGNED_BYTE, transparenttexdata);
glGenerateMipmap(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, 0);
// Opaque "flat" normal map color.
glGenTextures(1, &resources.normal_tex);
unsigned char normaltexdata[8 * 8 * 3];
for (int i = 0; i < 8 * 8 * 3; i += 3) {
@ -6489,13 +6502,13 @@ void RasterizerStorageGLES2::initialize() {
normaltexdata[i + 1] = 128;
normaltexdata[i + 2] = 255;
}
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, resources.normal_tex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 8, 8, 0, GL_RGB, GL_UNSIGNED_BYTE, normaltexdata);
glGenerateMipmap(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, 0);
// Opaque "flat" flowmap color.
glGenTextures(1, &resources.aniso_tex);
unsigned char anisotexdata[8 * 8 * 3];
for (int i = 0; i < 8 * 8 * 3; i += 3) {
@ -6503,7 +6516,6 @@ void RasterizerStorageGLES2::initialize() {
anisotexdata[i + 1] = 128;
anisotexdata[i + 2] = 0;
}
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, resources.aniso_tex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 8, 8, 0, GL_RGB, GL_UNSIGNED_BYTE, anisotexdata);

View file

@ -111,6 +111,7 @@ public:
struct Resources {
GLuint white_tex;
GLuint black_tex;
GLuint transparent_tex;
GLuint normal_tex;
GLuint aniso_tex;

View file

@ -195,6 +195,9 @@ void RasterizerCanvasGLES3::_legacy_canvas_render_item(Item *p_ci, RenderItemSta
case ShaderLanguage::ShaderNode::Uniform::HINT_BLACK: {
glBindTexture(GL_TEXTURE_2D, storage->resources.black_tex);
} break;
case ShaderLanguage::ShaderNode::Uniform::HINT_TRANSPARENT: {
glBindTexture(GL_TEXTURE_2D, storage->resources.transparent_tex);
} break;
case ShaderLanguage::ShaderNode::Uniform::HINT_ANISO: {
glBindTexture(GL_TEXTURE_2D, storage->resources.aniso_tex);
} break;
@ -1275,6 +1278,9 @@ void RasterizerCanvasGLES3::render_joined_item(const BItemJoined &p_bij, RenderI
case ShaderLanguage::ShaderNode::Uniform::HINT_BLACK: {
glBindTexture(GL_TEXTURE_2D, storage->resources.black_tex);
} break;
case ShaderLanguage::ShaderNode::Uniform::HINT_TRANSPARENT: {
glBindTexture(GL_TEXTURE_2D, storage->resources.transparent_tex);
} break;
case ShaderLanguage::ShaderNode::Uniform::HINT_ANISO: {
glBindTexture(GL_TEXTURE_2D, storage->resources.aniso_tex);
} break;

View file

@ -1195,6 +1195,9 @@ bool RasterizerSceneGLES3::_setup_material(RasterizerStorageGLES3::Material *p_m
case ShaderLanguage::ShaderNode::Uniform::HINT_BLACK: {
tex = storage->resources.black_tex;
} break;
case ShaderLanguage::ShaderNode::Uniform::HINT_TRANSPARENT: {
tex = storage->resources.transparent_tex;
} break;
case ShaderLanguage::ShaderNode::Uniform::HINT_ANISO: {
tex = storage->resources.aniso_tex;
} break;

View file

@ -6596,6 +6596,9 @@ void RasterizerStorageGLES3::update_particles() {
case ShaderLanguage::ShaderNode::Uniform::HINT_BLACK: {
tex = resources.black_tex;
} break;
case ShaderLanguage::ShaderNode::Uniform::HINT_TRANSPARENT: {
tex = resources.transparent_tex;
} break;
case ShaderLanguage::ShaderNode::Uniform::HINT_ANISO: {
tex = resources.aniso_tex;
} break;
@ -8202,32 +8205,45 @@ void RasterizerStorageGLES3::initialize() {
shaders.copy.init();
{
//default textures
// Generate default textures.
// Opaque white color.
glGenTextures(1, &resources.white_tex);
unsigned char whitetexdata[8 * 8 * 3];
for (int i = 0; i < 8 * 8 * 3; i++) {
whitetexdata[i] = 255;
}
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, resources.white_tex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 8, 8, 0, GL_RGB, GL_UNSIGNED_BYTE, whitetexdata);
glGenerateMipmap(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, 0);
// Opaque black color.
glGenTextures(1, &resources.black_tex);
unsigned char blacktexdata[8 * 8 * 3];
for (int i = 0; i < 8 * 8 * 3; i++) {
blacktexdata[i] = 0;
}
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, resources.black_tex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 8, 8, 0, GL_RGB, GL_UNSIGNED_BYTE, blacktexdata);
glGenerateMipmap(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, 0);
// Transparent black color.
glGenTextures(1, &resources.transparent_tex);
unsigned char transparenttexdata[8 * 8 * 4];
for (int i = 0; i < 8 * 8 * 4; i++) {
transparenttexdata[i] = 0;
}
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, resources.transparent_tex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 8, 8, 0, GL_RGBA, GL_UNSIGNED_BYTE, transparenttexdata);
glGenerateMipmap(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, 0);
// Opaque "flat" normal map color.
glGenTextures(1, &resources.normal_tex);
unsigned char normaltexdata[8 * 8 * 3];
for (int i = 0; i < 8 * 8 * 3; i += 3) {
@ -8235,13 +8251,13 @@ void RasterizerStorageGLES3::initialize() {
normaltexdata[i + 1] = 128;
normaltexdata[i + 2] = 255;
}
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, resources.normal_tex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 8, 8, 0, GL_RGB, GL_UNSIGNED_BYTE, normaltexdata);
glGenerateMipmap(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, 0);
// Opaque "flat" flowmap color.
glGenTextures(1, &resources.aniso_tex);
unsigned char anisotexdata[8 * 8 * 3];
for (int i = 0; i < 8 * 8 * 3; i += 3) {
@ -8249,7 +8265,6 @@ void RasterizerStorageGLES3::initialize() {
anisotexdata[i + 1] = 128;
anisotexdata[i + 2] = 0;
}
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, resources.aniso_tex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 8, 8, 0, GL_RGB, GL_UNSIGNED_BYTE, anisotexdata);
@ -8264,15 +8279,15 @@ void RasterizerStorageGLES3::initialize() {
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT16, 8, 8, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT, depthtexdata);
glBindTexture(GL_TEXTURE_2D, 0);
// Opaque white color for 3D texture.
glGenTextures(1, &resources.white_tex_3d);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_3D, resources.white_tex_3d);
glTexImage3D(GL_TEXTURE_3D, 0, GL_RGB, 2, 2, 2, 0, GL_RGB, GL_UNSIGNED_BYTE, whitetexdata);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_BASE_LEVEL, 0);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAX_LEVEL, 0);
// Opaque white color for texture array.
glGenTextures(1, &resources.white_tex_array);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D_ARRAY, resources.white_tex_array);
@ -8411,6 +8426,7 @@ void RasterizerStorageGLES3::initialize() {
void RasterizerStorageGLES3::finalize() {
glDeleteTextures(1, &resources.white_tex);
glDeleteTextures(1, &resources.black_tex);
glDeleteTextures(1, &resources.transparent_tex);
glDeleteTextures(1, &resources.normal_tex);
glDeleteTextures(1, &resources.depth_tex);
}

View file

@ -140,6 +140,7 @@ public:
struct Resources {
GLuint white_tex;
GLuint black_tex;
GLuint transparent_tex;
GLuint normal_tex;
GLuint aniso_tex;
GLuint depth_tex;

View file

@ -198,6 +198,7 @@ const char *ShaderLanguage::token_names[TK_MAX] = {
"RENDER_MODE",
"HINT_WHITE_TEXTURE",
"HINT_BLACK_TEXTURE",
"HINT_TRANSPARENT_TEXTURE",
"HINT_NORMAL_TEXTURE",
"HINT_ANISO_TEXTURE",
"HINT_ALBEDO_TEXTURE",
@ -295,6 +296,7 @@ const ShaderLanguage::KeyWord ShaderLanguage::keyword_list[] = {
{ TK_RENDER_MODE, "render_mode" },
{ TK_HINT_WHITE_TEXTURE, "hint_white" },
{ TK_HINT_BLACK_TEXTURE, "hint_black" },
{ TK_HINT_TRANSPARENT_TEXTURE, "hint_transparent" },
{ TK_HINT_NORMAL_TEXTURE, "hint_normal" },
{ TK_HINT_ANISO_TEXTURE, "hint_aniso" },
{ TK_HINT_ALBEDO_TEXTURE, "hint_albedo" },
@ -5977,6 +5979,8 @@ Error ShaderLanguage::_parse_shader(const Map<StringName, FunctionInfo> &p_funct
uniform2.hint = ShaderNode::Uniform::HINT_WHITE;
} else if (tk.type == TK_HINT_BLACK_TEXTURE) {
uniform2.hint = ShaderNode::Uniform::HINT_BLACK;
} else if (tk.type == TK_HINT_TRANSPARENT_TEXTURE) {
uniform2.hint = ShaderNode::Uniform::HINT_TRANSPARENT;
} else if (tk.type == TK_HINT_NORMAL_TEXTURE) {
uniform2.hint = ShaderNode::Uniform::HINT_NORMAL;
} else if (tk.type == TK_HINT_ANISO_TEXTURE) {

View file

@ -157,6 +157,7 @@ public:
TK_RENDER_MODE,
TK_HINT_WHITE_TEXTURE,
TK_HINT_BLACK_TEXTURE,
TK_HINT_TRANSPARENT_TEXTURE,
TK_HINT_NORMAL_TEXTURE,
TK_HINT_ANISO_TEXTURE,
TK_HINT_ALBEDO_TEXTURE,
@ -643,6 +644,7 @@ public:
HINT_NORMAL,
HINT_BLACK,
HINT_WHITE,
HINT_TRANSPARENT,
HINT_ANISO,
HINT_MAX
};