-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Metal] update custom_shaders_uniform example
- Add metal shaders to the custom_shaders_uniforms example - Set reflection pair hint for Metal pixel shaders to ensure params are set Select materials shader language depending on graphics API - Add a GraphicsAPI property to the private data of Ogre2Material - Switch the language used for the GPU program depending on the API - Set the default API to OpenGL. - To do: missing a method to set the graphics API when creating a material Signed-off-by: Rhys Mainwaring <[email protected]>
- Loading branch information
1 parent
1e711f6
commit cf94549
Showing
4 changed files
with
165 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
examples/custom_shaders_uniforms/media/fragment_shader.metal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* Copyright (C) 2021 Open Source Robotics Foundation | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
#include <metal_stdlib> | ||
using namespace metal; | ||
|
||
#define M_PI 3.1415926535897932384626433832795 | ||
|
||
struct PS_INPUT | ||
{ | ||
float4 interpolatedPosition; | ||
}; | ||
|
||
struct Params | ||
{ | ||
int u_seed; | ||
float2 u_resolution; | ||
float3 u_color; | ||
float4x4 u_adjustments; | ||
}; | ||
|
||
float random(float2 uv, float seed) { | ||
return fract(sin(fmod(dot(uv, float2(12.9898, 78.233)) | ||
+ 1113.1 * seed, M_PI)) * 43758.5453); | ||
} | ||
|
||
fragment float4 main_metal | ||
( | ||
PS_INPUT inPs [[stage_in]], | ||
constant Params &p [[buffer(PARAMETER_SLOT)]] | ||
) | ||
{ | ||
// Metal matrices are row major: a[row][col] | ||
float a0 = p.u_adjustments[0][0]; | ||
float a1 = p.u_adjustments[0][1]; | ||
float a2 = p.u_adjustments[0][2]; | ||
float a3 = p.u_adjustments[0][3]; | ||
|
||
float3 a = float3(a0, a1, a2); | ||
float2 b = float2(distance(float3(inPs.interpolatedPosition.xyw), a)) * a3; | ||
float2 normalizedFragCoord = b / p.u_resolution; | ||
|
||
float color = random(normalizedFragCoord, float(p.u_seed)); | ||
return float4(color * p.u_color, 1.0); | ||
} |
49 changes: 49 additions & 0 deletions
49
examples/custom_shaders_uniforms/media/vertex_shader.metal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright (C) 2021 Open Source Robotics Foundation | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
#include <metal_stdlib> | ||
using namespace metal; | ||
|
||
struct VS_INPUT | ||
{ | ||
float4 position [[attribute(VES_POSITION)]]; | ||
}; | ||
|
||
struct PS_INPUT | ||
{ | ||
float4 gl_Position [[position]]; | ||
float4 interpolatedPosition; | ||
}; | ||
|
||
struct Params | ||
{ | ||
float4x4 worldviewproj_matrix; | ||
}; | ||
|
||
vertex PS_INPUT main_metal | ||
( | ||
VS_INPUT input [[stage_in]], | ||
constant Params &p [[buffer(PARAMETER_SLOT)]] | ||
) | ||
{ | ||
PS_INPUT outVs; | ||
|
||
outVs.gl_Position = p.worldviewproj_matrix * input.position; | ||
outVs.interpolatedPosition = outVs.gl_Position; | ||
|
||
return outVs; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters