2023-01-10 15:26:54 +01:00
|
|
|
/**************************************************************************/
|
|
|
|
/* shader_gles2.h */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/**************************************************************************/
|
2018-09-11 18:13:45 +02:00
|
|
|
|
2017-12-04 13:41:34 +01:00
|
|
|
#ifndef SHADER_GLES2_H
|
|
|
|
#define SHADER_GLES2_H
|
|
|
|
|
2018-09-11 18:13:45 +02:00
|
|
|
// This must come first to avoid windows.h mess
|
2017-12-04 13:41:34 +01:00
|
|
|
#include "platform_config.h"
|
|
|
|
#ifndef GLES2_INCLUDE_H
|
|
|
|
#include <GLES2/gl2.h>
|
|
|
|
#else
|
|
|
|
#include GLES2_INCLUDE_H
|
|
|
|
#endif
|
|
|
|
|
2018-09-11 18:13:45 +02:00
|
|
|
#include "core/hash_map.h"
|
|
|
|
#include "core/map.h"
|
|
|
|
#include "core/math/camera_matrix.h"
|
2018-02-24 14:48:22 +01:00
|
|
|
#include "core/pair.h"
|
2018-09-11 18:13:45 +02:00
|
|
|
#include "core/variant.h"
|
2018-02-24 14:48:22 +01:00
|
|
|
#include "servers/visual/shader_language.h"
|
|
|
|
|
2018-09-11 18:13:45 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2018-02-24 14:48:22 +01:00
|
|
|
class RasterizerStorageGLES2;
|
|
|
|
|
2017-12-04 13:41:34 +01:00
|
|
|
class ShaderGLES2 {
|
2022-11-27 09:34:07 +01:00
|
|
|
static String _mkid(const String &p_id);
|
|
|
|
|
2017-12-04 13:41:34 +01:00
|
|
|
protected:
|
|
|
|
struct Enum {
|
|
|
|
uint64_t mask;
|
|
|
|
uint64_t shift;
|
|
|
|
const char *defines[16];
|
|
|
|
};
|
|
|
|
|
|
|
|
struct EnumValue {
|
|
|
|
uint64_t set_mask;
|
|
|
|
uint64_t clear_mask;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct AttributePair {
|
|
|
|
const char *name;
|
|
|
|
int index;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct UniformPair {
|
|
|
|
const char *name;
|
|
|
|
Variant::Type type_hint;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct TexUnitPair {
|
|
|
|
const char *name;
|
|
|
|
int index;
|
|
|
|
};
|
|
|
|
|
|
|
|
bool uniforms_dirty;
|
|
|
|
|
|
|
|
private:
|
|
|
|
//@TODO Optimize to a fixed set of shader pools and use a LRU
|
|
|
|
int uniform_count;
|
|
|
|
int texunit_pair_count;
|
|
|
|
int conditional_count;
|
|
|
|
int vertex_code_start;
|
|
|
|
int fragment_code_start;
|
|
|
|
int attribute_pair_count;
|
|
|
|
|
|
|
|
struct CustomCode {
|
|
|
|
String vertex;
|
|
|
|
String vertex_globals;
|
|
|
|
String fragment;
|
|
|
|
String fragment_globals;
|
|
|
|
String light;
|
|
|
|
uint32_t version;
|
|
|
|
Vector<StringName> texture_uniforms;
|
|
|
|
Vector<StringName> custom_uniforms;
|
|
|
|
Vector<CharString> custom_defines;
|
2021-03-08 18:58:13 +01:00
|
|
|
Set<uint64_t> versions;
|
2017-12-04 13:41:34 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
struct Version {
|
|
|
|
GLuint id;
|
|
|
|
GLuint vert_id;
|
|
|
|
GLuint frag_id;
|
|
|
|
GLint *uniform_location;
|
|
|
|
Vector<GLint> texture_uniform_locations;
|
2018-02-24 14:48:22 +01:00
|
|
|
Map<StringName, GLint> custom_uniform_locations;
|
2017-12-04 13:41:34 +01:00
|
|
|
uint32_t code_version;
|
|
|
|
bool ok;
|
|
|
|
Version() {
|
2019-12-05 09:15:31 +01:00
|
|
|
id = 0;
|
|
|
|
vert_id = 0;
|
2019-11-20 16:22:16 +01:00
|
|
|
frag_id = 0;
|
2021-05-04 16:00:45 +02:00
|
|
|
uniform_location = nullptr;
|
2019-12-05 09:15:31 +01:00
|
|
|
code_version = 0;
|
|
|
|
ok = false;
|
2017-12-04 13:41:34 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Version *version;
|
|
|
|
|
|
|
|
union VersionKey {
|
|
|
|
struct {
|
2021-03-08 18:58:13 +01:00
|
|
|
uint64_t version;
|
2017-12-04 13:41:34 +01:00
|
|
|
uint32_t code_version;
|
|
|
|
};
|
2021-03-08 18:58:13 +01:00
|
|
|
unsigned char key[12];
|
|
|
|
bool operator==(const VersionKey &p_key) const { return version == p_key.version && code_version == p_key.code_version; }
|
|
|
|
bool operator<(const VersionKey &p_key) const { return version < p_key.version || (version == p_key.version && code_version < p_key.code_version); }
|
2017-12-04 13:41:34 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
struct VersionKeyHash {
|
2021-03-08 18:58:13 +01:00
|
|
|
static _FORCE_INLINE_ uint32_t hash(const VersionKey &p_key) { return hash_djb2_buffer(p_key.key, sizeof(p_key.key)); }
|
2017-12-04 13:41:34 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
//this should use a way more cachefriendly version..
|
|
|
|
HashMap<VersionKey, Version, VersionKeyHash> version_map;
|
|
|
|
|
|
|
|
HashMap<uint32_t, CustomCode> custom_code_map;
|
|
|
|
uint32_t last_custom_code;
|
|
|
|
|
|
|
|
VersionKey conditional_version;
|
|
|
|
VersionKey new_conditional_version;
|
|
|
|
|
|
|
|
virtual String get_shader_name() const = 0;
|
|
|
|
|
|
|
|
const char **conditional_defines;
|
|
|
|
const char **uniform_names;
|
|
|
|
const AttributePair *attribute_pairs;
|
|
|
|
const TexUnitPair *texunit_pairs;
|
|
|
|
const char *vertex_code;
|
|
|
|
const char *fragment_code;
|
|
|
|
CharString fragment_code0;
|
|
|
|
CharString fragment_code1;
|
|
|
|
CharString fragment_code2;
|
|
|
|
CharString fragment_code3;
|
|
|
|
|
|
|
|
CharString vertex_code0;
|
|
|
|
CharString vertex_code1;
|
|
|
|
CharString vertex_code2;
|
|
|
|
|
|
|
|
Vector<CharString> custom_defines;
|
|
|
|
|
|
|
|
Version *get_current_version();
|
|
|
|
|
|
|
|
static ShaderGLES2 *active;
|
|
|
|
|
|
|
|
int max_image_units;
|
|
|
|
|
2021-05-04 14:20:36 +02:00
|
|
|
Map<StringName, Pair<ShaderLanguage::DataType, Vector<ShaderLanguage::ConstantNode::Value>>> uniform_values;
|
2018-02-24 14:48:22 +01:00
|
|
|
|
|
|
|
protected:
|
|
|
|
_FORCE_INLINE_ int _get_uniform(int p_which) const;
|
|
|
|
_FORCE_INLINE_ void _set_conditional(int p_which, bool p_value);
|
|
|
|
|
|
|
|
void setup(const char **p_conditional_defines,
|
|
|
|
int p_conditional_count,
|
|
|
|
const char **p_uniform_names,
|
|
|
|
int p_uniform_count,
|
|
|
|
const AttributePair *p_attribute_pairs,
|
|
|
|
int p_attribute_count,
|
|
|
|
const TexUnitPair *p_texunit_pairs,
|
|
|
|
int p_texunit_pair_count,
|
|
|
|
const char *p_vertex_code,
|
|
|
|
const char *p_fragment_code,
|
|
|
|
int p_vertex_code_start,
|
|
|
|
int p_fragment_code_start);
|
|
|
|
|
|
|
|
ShaderGLES2();
|
|
|
|
|
|
|
|
public:
|
|
|
|
enum {
|
|
|
|
CUSTOM_SHADER_DISABLED = 0
|
|
|
|
};
|
|
|
|
|
|
|
|
GLint get_uniform_location(const String &p_name) const;
|
|
|
|
GLint get_uniform_location(int p_index) const;
|
|
|
|
|
|
|
|
static _FORCE_INLINE_ ShaderGLES2 *get_active() { return active; }
|
|
|
|
bool bind();
|
|
|
|
void unbind();
|
|
|
|
|
|
|
|
inline GLuint get_program() const { return version ? version->id : 0; }
|
|
|
|
|
|
|
|
void clear_caches();
|
|
|
|
|
2017-12-04 13:41:34 +01:00
|
|
|
uint32_t create_custom_shader();
|
|
|
|
void set_custom_shader_code(uint32_t p_code_id,
|
|
|
|
const String &p_vertex,
|
|
|
|
const String &p_vertex_globals,
|
|
|
|
const String &p_fragment,
|
|
|
|
const String &p_light,
|
|
|
|
const String &p_fragment_globals,
|
|
|
|
const Vector<StringName> &p_uniforms,
|
|
|
|
const Vector<StringName> &p_texture_uniforms,
|
|
|
|
const Vector<CharString> &p_custom_defines);
|
|
|
|
|
|
|
|
void set_custom_shader(uint32_t p_code_id);
|
|
|
|
void free_custom_shader(uint32_t p_code_id);
|
|
|
|
|
2021-03-08 18:58:13 +01:00
|
|
|
uint64_t get_version_key() const { return conditional_version.version; }
|
2019-01-24 21:00:33 +01:00
|
|
|
|
2018-02-24 14:48:22 +01:00
|
|
|
// this void* is actually a RasterizerStorageGLES2::Material, but C++ doesn't
|
|
|
|
// like forward declared nested classes.
|
2018-08-06 18:26:17 +02:00
|
|
|
void use_material(void *p_material);
|
2018-02-24 14:48:22 +01:00
|
|
|
|
2021-03-08 18:58:13 +01:00
|
|
|
_FORCE_INLINE_ uint64_t get_version() const { return new_conditional_version.version; }
|
2018-09-23 17:12:30 +02:00
|
|
|
_FORCE_INLINE_ bool is_version_valid() const { return version && version->ok; }
|
2017-12-04 13:41:34 +01:00
|
|
|
|
|
|
|
virtual void init() = 0;
|
|
|
|
void finish();
|
|
|
|
|
|
|
|
void add_custom_define(const String &p_define) {
|
|
|
|
custom_defines.push_back(p_define.utf8());
|
|
|
|
}
|
|
|
|
|
2020-01-19 08:14:10 +01:00
|
|
|
void get_custom_defines(Vector<String> *p_defines) {
|
|
|
|
for (int i = 0; i < custom_defines.size(); i++) {
|
|
|
|
p_defines->push_back(custom_defines[i].get_data());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-07 06:31:09 +02:00
|
|
|
void remove_custom_define(const String &p_define) {
|
|
|
|
custom_defines.erase(p_define.utf8());
|
2020-01-19 08:14:10 +01:00
|
|
|
}
|
|
|
|
|
2017-12-04 13:41:34 +01:00
|
|
|
virtual ~ShaderGLES2();
|
|
|
|
};
|
|
|
|
|
|
|
|
// called a lot, made inline
|
|
|
|
|
|
|
|
int ShaderGLES2::_get_uniform(int p_which) const {
|
|
|
|
ERR_FAIL_INDEX_V(p_which, uniform_count, -1);
|
|
|
|
ERR_FAIL_COND_V(!version, -1);
|
|
|
|
return version->uniform_location[p_which];
|
|
|
|
}
|
|
|
|
|
|
|
|
void ShaderGLES2::_set_conditional(int p_which, bool p_value) {
|
|
|
|
ERR_FAIL_INDEX(p_which, conditional_count);
|
2021-09-29 15:23:12 +02:00
|
|
|
ERR_FAIL_INDEX(p_which, (int)sizeof(new_conditional_version.version) * 8);
|
2021-03-08 18:58:13 +01:00
|
|
|
|
2021-05-05 12:44:11 +02:00
|
|
|
if (p_value) {
|
2021-03-08 18:58:13 +01:00
|
|
|
new_conditional_version.version |= (uint64_t(1) << p_which);
|
2021-05-05 12:44:11 +02:00
|
|
|
} else {
|
2021-03-08 18:58:13 +01:00
|
|
|
new_conditional_version.version &= ~(uint64_t(1) << p_which);
|
2021-05-05 12:44:11 +02:00
|
|
|
}
|
2017-12-04 13:41:34 +01:00
|
|
|
}
|
|
|
|
|
2022-07-25 12:33:41 +02:00
|
|
|
#endif // SHADER_GLES2_H
|