Photon Engine 2.0.0-beta
A physically based renderer.
Loading...
Searching...
No Matches
binary_texture_operators.h
Go to the documentation of this file.
1#pragma once
2
5#include "Utility/traits.h"
8
9#include <Common/assertion.h>
10
11#include <cstddef>
12#include <type_traits>
13#include <memory>
14#include <utility>
15#include <concepts>
16#include <array>
17#include <cmath>
18
19namespace ph
20{
21
22namespace texfunc
23{
24
25template<typename OperatorType, typename InputTypeA, typename InputTypeB, typename OutputType>
26concept CBinaryOperator = requires (OperatorType op, InputTypeA inputA, InputTypeB inputB)
27{
28 { op(inputA, inputB) } -> std::same_as<OutputType>;
29};
30
31template<typename InputTypeA, typename InputTypeB, typename OutputType>
32class TAdd final
33{
34public:
35 OutputType operator () (const InputTypeA& inputValueA, const InputTypeB& inputValueB) const
36 {
38 "Must have addition operator for `OutputType` = `InputTypeA` + `InputTypeB`");
39
40 return inputValueA + inputValueB;
41 }
42};
43
44template<typename InputTypeA, typename InputTypeB, typename OutputType>
45class TSubtract final
46{
47public:
48 OutputType operator () (const InputTypeA& inputValueA, const InputTypeB& inputValueB) const
49 {
51 "Must have subtraction operator for `OutputType` = `InputTypeA` - `InputTypeB`");
52
53 return inputValueA - inputValueB;
54 }
55};
56
57template<typename InputTypeA, typename InputTypeB, typename OutputType>
58class TMultiply final
59{
60public:
61 OutputType operator () (const InputTypeA& inputValueA, const InputTypeB& inputValueB) const
62 {
64 "Must have multiplication operator for `OutputType` = `InputTypeA` * `InputTypeB`");
65
66 return inputValueA * inputValueB;
67 }
68};
69
70template<typename InputTypeA, typename InputTypeB, typename OutputType>
71class TDivide final
72{
73public:
74 OutputType operator () (const InputTypeA& inputValueA, const InputTypeB& inputValueB) const
75 {
77 "Must have multiplication operator for `OutputType` = `InputTypeA` / `InputTypeB`");
78
79 return inputValueA / inputValueB;
80 }
81};
82
83template<typename InputTypeA, typename InputTypeB, typename OutputType>
84class TPower final
85{
86public:
87 OutputType operator () (const InputTypeA& inputValueA, const InputTypeB& inputValueB) const
88 {
89 constexpr bool canCallPowMethod = requires (InputTypeA a, InputTypeB b)
90 {
91 { a.pow(b) } -> std::convertible_to<OutputType>;
92 };
93
94 constexpr bool canCallStdPow = requires (InputTypeA a, InputTypeB b)
95 {
96 { std::pow(a, b) } -> std::convertible_to<OutputType>;
97 };
98
99 if constexpr(canCallPowMethod)
100 {
101 return inputValueA.pow(inputValueB);
102 }
103 else if constexpr(canCallStdPow)
104 {
105 return std::pow(inputValueA, inputValueB);
106 }
107 else
108 {
109 PH_STATIC_ASSERT_DEPENDENT_FALSE(OutputType,
110 "Cannot perform power operation for the specified types.");
111 }
112 }
113};
114
120
121}// end namespace texfunc
122
123template
124<
125 typename InputTypeA,
126 typename InputTypeB,
127 typename OutputType,
129>
130class TBinaryTextureOperator : public TTexture<OutputType>
131{
132public:
133 using InputTexResA = std::shared_ptr<TTexture<InputTypeA>>;
134 using InputTexResB = std::shared_ptr<TTexture<InputTypeB>>;
135
136 TBinaryTextureOperator(InputTexResA inputA, InputTexResB inputB) requires std::default_initializable<OperatorType>
137 : TBinaryTextureOperator(std::move(inputA), std::move(inputB), OperatorType{})
138 {}
139
140 TBinaryTextureOperator(InputTexResA inputA, InputTexResB inputB, OperatorType op)
141 : m_inputA (std::move(inputA))
142 , m_inputB (std::move(inputB))
143 , m_operator(std::move(op))
144 {}
145
146 void sample(const SampleLocation& sampleLocation, OutputType* const out_value) const override
147 {
148 PH_ASSERT(m_inputA);
149 PH_ASSERT(m_inputB);
150 PH_ASSERT(out_value);
151
152 InputTypeA inputValueA;
153 m_inputA->sample(sampleLocation, &inputValueA);
154
155 InputTypeB inputValueB;
156 m_inputB->sample(sampleLocation, &inputValueB);
157
158 *out_value = m_operator(inputValueA, inputValueB);
159 }
160
161private:
162 InputTexResA m_inputA;
163 InputTexResB m_inputB;
164 OperatorType m_operator;
165};
166
167}// end namespace ph
Definition SampleLocation.h:22
Definition binary_texture_operators.h:131
TBinaryTextureOperator(InputTexResA inputA, InputTexResB inputB, OperatorType op)
Definition binary_texture_operators.h:140
std::shared_ptr< TTexture< InputTypeA > > InputTexResA
Definition binary_texture_operators.h:133
std::shared_ptr< TTexture< InputTypeB > > InputTexResB
Definition binary_texture_operators.h:134
void sample(const SampleLocation &sampleLocation, OutputType *const out_value) const override
Definition binary_texture_operators.h:146
TBinaryTextureOperator(InputTexResA inputA, InputTexResB inputB)
Definition binary_texture_operators.h:136
Definition TTexture.h:12
Definition binary_texture_operators.h:33
OutputType operator()(const InputTypeA &inputValueA, const InputTypeB &inputValueB) const
Definition binary_texture_operators.h:35
Definition binary_texture_operators.h:72
OutputType operator()(const InputTypeA &inputValueA, const InputTypeB &inputValueB) const
Definition binary_texture_operators.h:74
Definition binary_texture_operators.h:59
OutputType operator()(const InputTypeA &inputValueA, const InputTypeB &inputValueB) const
Definition binary_texture_operators.h:61
Definition binary_texture_operators.h:85
OutputType operator()(const InputTypeA &inputValueA, const InputTypeB &inputValueB) const
Definition binary_texture_operators.h:87
Definition binary_texture_operators.h:46
OutputType operator()(const InputTypeA &inputValueA, const InputTypeB &inputValueB) const
Definition binary_texture_operators.h:48
Check if instances of types can be added together.
Definition traits.h:19
Check if instances of types can be divided.
Definition traits.h:55
Check if instances of types can be multiplied together.
Definition traits.h:43
Check if instances of types can be subtracted.
Definition traits.h:31
Definition binary_texture_operators.h:26
The root for all renderer implementations.
Definition EEngineProject.h:6
Definition TAABB2D.h:96