Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/scene/resources/blit_material.cpp
20934 views
1
/**************************************************************************/
2
/* blit_material.cpp */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#include "blit_material.h"
32
33
#include "core/version.h"
34
35
void BlitMaterial::_update_shader(BlendMode p_blend) {
36
MutexLock shader_lock(shader_mutex);
37
int index = int(p_blend);
38
if (shader_cache[p_blend].is_null()) {
39
shader_cache[p_blend] = RS::get_singleton()->shader_create();
40
String code = "// NOTE: Shader automatically converted from " GODOT_VERSION_NAME " " GODOT_VERSION_FULL_CONFIG "'s BlitMaterial.\n\n";
41
42
code += "shader_type texture_blit;\nrender_mode ";
43
switch (p_blend) {
44
case BLEND_MODE_MIX:
45
code += "blend_mix";
46
break;
47
case BLEND_MODE_ADD:
48
code += "blend_add";
49
break;
50
case BLEND_MODE_SUB:
51
code += "blend_sub";
52
break;
53
case BLEND_MODE_MUL:
54
code += "blend_mul";
55
break;
56
case BLEND_MODE_DISABLED:
57
code += "blend_disabled";
58
break;
59
default:
60
code += "blend_mix";
61
break;
62
}
63
code += ";\n\n";
64
65
code += "uniform sampler2D source_texture0 : hint_blit_source0;\n";
66
code += "uniform sampler2D source_texture1 : hint_blit_source1;\n";
67
code += "uniform sampler2D source_texture2 : hint_blit_source2;\n";
68
code += "uniform sampler2D source_texture3 : hint_blit_source3;\n\n";
69
70
code += "void blit() {\n";
71
code += " // Copies from each whole source texture to a rect on each output texture.\n";
72
code += " COLOR0 = texture(source_texture0, UV) * MODULATE;\n";
73
code += " COLOR1 = texture(source_texture1, UV) * MODULATE;\n";
74
code += " COLOR2 = texture(source_texture2, UV) * MODULATE;\n";
75
code += " COLOR3 = texture(source_texture3, UV) * MODULATE;\n}";
76
RS::get_singleton()->shader_set_code(shader_cache[index], code);
77
}
78
}
79
80
void BlitMaterial::set_blend_mode(BlendMode p_blend_mode) {
81
blend_mode = p_blend_mode;
82
_update_shader(blend_mode);
83
if (shader_set) {
84
RS::get_singleton()->material_set_shader(_get_material(), shader_cache[int(blend_mode)]);
85
}
86
}
87
88
BlitMaterial::BlendMode BlitMaterial::get_blend_mode() const {
89
return blend_mode;
90
}
91
92
RID BlitMaterial::get_shader_rid() const {
93
_update_shader(blend_mode);
94
return shader_cache[int(blend_mode)];
95
}
96
97
Shader::Mode BlitMaterial::get_shader_mode() const {
98
return Shader::MODE_TEXTURE_BLIT;
99
}
100
101
RID BlitMaterial::get_rid() const {
102
_update_shader(blend_mode);
103
if (!shader_set) {
104
RS::get_singleton()->material_set_shader(_get_material(), shader_cache[int(blend_mode)]);
105
shader_set = true;
106
}
107
return _get_material();
108
}
109
110
Mutex BlitMaterial::shader_mutex;
111
RID BlitMaterial::shader_cache[5];
112
113
void BlitMaterial::cleanup_shader() {
114
for (int i = 0; i < 5; i++) {
115
if (shader_cache[i].is_valid()) {
116
RS::get_singleton()->free_rid(shader_cache[i]);
117
}
118
}
119
}
120
121
void BlitMaterial::_bind_methods() {
122
ClassDB::bind_method(D_METHOD("set_blend_mode", "blend_mode"), &BlitMaterial::set_blend_mode);
123
ClassDB::bind_method(D_METHOD("get_blend_mode"), &BlitMaterial::get_blend_mode);
124
125
ADD_PROPERTY(PropertyInfo(Variant::INT, "blend_mode", PROPERTY_HINT_ENUM, "Mix,Add,Subtract,Multiply,Disabled"), "set_blend_mode", "get_blend_mode");
126
127
BIND_ENUM_CONSTANT(BLEND_MODE_MIX);
128
BIND_ENUM_CONSTANT(BLEND_MODE_ADD);
129
BIND_ENUM_CONSTANT(BLEND_MODE_SUB);
130
BIND_ENUM_CONSTANT(BLEND_MODE_MUL);
131
BIND_ENUM_CONSTANT(BLEND_MODE_DISABLED);
132
}
133
134
BlitMaterial::BlitMaterial() {
135
_set_material(RS::get_singleton()->material_create());
136
set_blend_mode(BLEND_MODE_MIX);
137
}
138
139
BlitMaterial::~BlitMaterial() {
140
}
141
142