mirror of
https://github.com/Xaymar/obs-StreamFX
synced 2024-11-10 22:05:06 +00:00
gfx/shader: Inlining, std::string_view and optimizations
This commit is contained in:
parent
3a40a63832
commit
a2fd4dd2f6
6 changed files with 183 additions and 73 deletions
|
@ -845,6 +845,9 @@ if(REQUIRE_SHADER_CODE)
|
|||
"source/gfx/shader/gfx-shader-param-texture.hpp"
|
||||
"source/gfx/shader/gfx-shader-param-texture.cpp"
|
||||
)
|
||||
list(APPEND PROJECT_DATA
|
||||
"data/examples/shaders/feature-test.effect"
|
||||
)
|
||||
endif()
|
||||
|
||||
# Combine it all
|
||||
|
|
113
data/examples/shaders/feature-test.effect
Normal file
113
data/examples/shaders/feature-test.effect
Normal file
|
@ -0,0 +1,113 @@
|
|||
// --------------------------------------------------------------------------------
|
||||
// Uniforms set by libobs
|
||||
uniform float4x4 ViewProj<
|
||||
bool automatic = true;
|
||||
>;
|
||||
|
||||
// --------------------------------------------------------------------------------
|
||||
// Uniforms set by StreamFX
|
||||
uniform float4 Time<
|
||||
bool automatic = true;
|
||||
>;
|
||||
uniform float4 ViewSize<
|
||||
bool automatic = true;
|
||||
>;
|
||||
|
||||
// Filters, Transitions
|
||||
uniform texture2d InputA<
|
||||
bool automatic = true;
|
||||
>;
|
||||
|
||||
// Transitions
|
||||
uniform texture2d InputB<
|
||||
bool automatic = true;
|
||||
>;
|
||||
uniform float TransitionTime<
|
||||
bool automatic = true;
|
||||
>;
|
||||
|
||||
// --------------------------------------------------------------------------------
|
||||
// Parameters
|
||||
uniform bool BoolParameter = false;
|
||||
uniform float FloatParameter<
|
||||
string name = "Float Parameter";
|
||||
string description = "This is a 32-bit floating point value.";
|
||||
string field_type = "input";
|
||||
string suffix = " %";
|
||||
float minimum = -100.;
|
||||
float maximum = 100.;
|
||||
float step = .5;
|
||||
float scale = .01;
|
||||
> = 0.;
|
||||
uniform float2 Float2Parameter<
|
||||
string name = "Float2 Parameter";
|
||||
string description = "This is a 32-bit floating point value.";
|
||||
string field_type = "input";
|
||||
string suffix = " %";
|
||||
float2 minimum = {-100., -100.};
|
||||
float2 maximum = {100., 100.};
|
||||
float2 step = {.5, .5};
|
||||
float2 scale = {.01, .01};
|
||||
> = {0., 0.};
|
||||
uniform float3 Float3Parameter = {0., 0., 0.};
|
||||
uniform float4 Float4Parameter = {0., 0., 0., 0.};
|
||||
uniform int IntParameter<
|
||||
string name = "Enum Parameter";
|
||||
string description = "This parameter is an enumeration";
|
||||
string field_type = "enum";
|
||||
// Enumeration
|
||||
int enum = 4;
|
||||
int enum_0 = 0;
|
||||
string enum_0_name = "Null";
|
||||
int enum_1 = 1;
|
||||
string enum_1_name = "One";
|
||||
int enum_2 = 2;
|
||||
string enum_2_name = "Two";
|
||||
int enum_3 = 4;
|
||||
string enum_3_name = "Four";
|
||||
> = 0;
|
||||
uniform int2 Int2Parameter = {0, 0};
|
||||
uniform int3 Int3Parameter = {0, 0, 0};
|
||||
uniform int4 Int4Parameter = {0, 0, 0, 0};
|
||||
|
||||
// --------------------------------------------------------------------------------
|
||||
// Vertex Processing
|
||||
struct VertexData {
|
||||
float4 pos : POSITION;
|
||||
float2 uv : TEXCOORD0;
|
||||
};
|
||||
|
||||
VertexData VSDefault(VertexData vtx) {
|
||||
vtx.pos = mul(float4(vtx.pos.xyz, 1.), ViewProj);
|
||||
return vtx;
|
||||
};
|
||||
|
||||
// --------------------------------------------------------------------------------
|
||||
// Default Technique
|
||||
float4 PSDefault(VertexData vtx) : TARGET {
|
||||
if (BoolParameter)
|
||||
vtx.uv += 1.;
|
||||
vtx.uv += FloatParameter;
|
||||
vtx.uv += Float2Parameter;
|
||||
vtx.uv += Float3Parameter.xy * Float3Parameter.zz;
|
||||
vtx.uv += Float4Parameter.xy * Float4Parameter.zz;
|
||||
|
||||
if (IntParameter == 1) {
|
||||
return float4(1., 0., 0., 1.);
|
||||
} else if (IntParameter == 2) {
|
||||
return float4(0., 1., 0., 1.);
|
||||
} else if (IntParameter == 4) {
|
||||
return float4(0., 0., 1., 1.);
|
||||
}
|
||||
|
||||
return float4(0., 0., 0., 1.);
|
||||
}
|
||||
|
||||
technique Draw
|
||||
{
|
||||
pass
|
||||
{
|
||||
vertex_shader = VSDefault(vtx);
|
||||
pixel_shader = PSDefault(vtx);
|
||||
}
|
||||
}
|
|
@ -175,61 +175,6 @@ void gfx::shader::parameter::update(obs_data_t* settings) {}
|
|||
|
||||
void gfx::shader::parameter::assign() {}
|
||||
|
||||
gs::effect_parameter gfx::shader::parameter::get_parameter()
|
||||
{
|
||||
return _param;
|
||||
}
|
||||
|
||||
gfx::shader::parameter_type gfx::shader::parameter::get_type()
|
||||
{
|
||||
return _type;
|
||||
}
|
||||
|
||||
std::size_t gfx::shader::parameter::get_size()
|
||||
{
|
||||
return _size;
|
||||
}
|
||||
|
||||
int32_t gfx::shader::parameter::get_order()
|
||||
{
|
||||
return _order;
|
||||
}
|
||||
|
||||
const std::string& gfx::shader::parameter::get_key()
|
||||
{
|
||||
return _key;
|
||||
}
|
||||
|
||||
bool gfx::shader::parameter::is_visible()
|
||||
{
|
||||
return _visible && !is_automatic();
|
||||
}
|
||||
|
||||
bool gfx::shader::parameter::is_automatic()
|
||||
{
|
||||
return _automatic;
|
||||
}
|
||||
|
||||
bool gfx::shader::parameter::has_name()
|
||||
{
|
||||
return _name.length() > 0;
|
||||
}
|
||||
|
||||
const std::string& gfx::shader::parameter::get_name()
|
||||
{
|
||||
return _name;
|
||||
}
|
||||
|
||||
bool gfx::shader::parameter::has_description()
|
||||
{
|
||||
return _description.length() > 0;
|
||||
}
|
||||
|
||||
const std::string& gfx::shader::parameter::get_description()
|
||||
{
|
||||
return _description;
|
||||
}
|
||||
|
||||
std::shared_ptr<gfx::shader::parameter> gfx::shader::parameter::make_parameter(gs::effect_parameter param,
|
||||
std::string prefix)
|
||||
{
|
||||
|
|
|
@ -81,27 +81,60 @@ namespace gfx {
|
|||
virtual void assign();
|
||||
|
||||
public:
|
||||
gs::effect_parameter get_parameter();
|
||||
inline gs::effect_parameter get_parameter()
|
||||
{
|
||||
return _param;
|
||||
}
|
||||
|
||||
parameter_type get_type();
|
||||
inline parameter_type get_type()
|
||||
{
|
||||
return _type;
|
||||
}
|
||||
|
||||
std::size_t get_size();
|
||||
inline std::size_t get_size()
|
||||
{
|
||||
return _size;
|
||||
}
|
||||
|
||||
int32_t get_order();
|
||||
inline int32_t get_order()
|
||||
{
|
||||
return _order;
|
||||
}
|
||||
|
||||
const std::string& get_key();
|
||||
inline std::string_view get_key()
|
||||
{
|
||||
return _key;
|
||||
}
|
||||
|
||||
bool is_visible();
|
||||
inline bool is_visible()
|
||||
{
|
||||
return _visible && !_automatic;
|
||||
}
|
||||
|
||||
bool is_automatic();
|
||||
inline bool is_automatic()
|
||||
{
|
||||
return _automatic;
|
||||
}
|
||||
|
||||
bool has_name();
|
||||
inline bool has_name()
|
||||
{
|
||||
return _name.length() > 0;
|
||||
}
|
||||
|
||||
const std::string& get_name();
|
||||
inline std::string_view get_name()
|
||||
{
|
||||
return _name;
|
||||
}
|
||||
|
||||
bool has_description();
|
||||
inline bool has_description()
|
||||
{
|
||||
return _description.length() > 0;
|
||||
}
|
||||
|
||||
const std::string& get_description();
|
||||
inline std::string_view get_description()
|
||||
{
|
||||
return _description;
|
||||
}
|
||||
|
||||
public:
|
||||
static std::shared_ptr<parameter> make_parameter(gs::effect_parameter param, std::string prefix);
|
||||
|
|
|
@ -509,9 +509,17 @@ void gfx::shader::shader::set_input_a(std::shared_ptr<gs::texture> tex)
|
|||
if (!_shader)
|
||||
return;
|
||||
|
||||
if (gs::effect_parameter el = _shader.get_parameter("InputA"); el != nullptr) {
|
||||
if (el.get_type() == gs::effect_parameter::type::Texture) {
|
||||
el.set_texture(tex);
|
||||
std::string_view params[] = {
|
||||
"InputA",
|
||||
"image",
|
||||
"tex_a",
|
||||
};
|
||||
for (auto& name : params) {
|
||||
if (gs::effect_parameter el = _shader.get_parameter(name.data()); el != nullptr) {
|
||||
if (el.get_type() == gs::effect_parameter::type::Texture) {
|
||||
el.set_texture(tex);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -521,9 +529,17 @@ void gfx::shader::shader::set_input_b(std::shared_ptr<gs::texture> tex)
|
|||
if (!_shader)
|
||||
return;
|
||||
|
||||
if (gs::effect_parameter el = _shader.get_parameter("InputB"); el != nullptr) {
|
||||
if (el.get_type() == gs::effect_parameter::type::Texture) {
|
||||
el.set_texture(tex);
|
||||
std::string_view params[] = {
|
||||
"InputB",
|
||||
"image2",
|
||||
"tex_b",
|
||||
};
|
||||
for (auto& name : params) {
|
||||
if (gs::effect_parameter el = _shader.get_parameter(name.data()); el != nullptr) {
|
||||
if (el.get_type() == gs::effect_parameter::type::Texture) {
|
||||
el.set_texture(tex);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -38,7 +38,7 @@ namespace gfx {
|
|||
Transition,
|
||||
};
|
||||
|
||||
typedef std::map<std::string, std::shared_ptr<parameter>> shader_param_map_t;
|
||||
typedef std::map<std::string_view, std::shared_ptr<parameter>> shader_param_map_t;
|
||||
|
||||
class shader {
|
||||
obs_source_t* _self;
|
||||
|
|
Loading…
Reference in a new issue