Skip to content

Commit

Permalink
Samples: InstancedViewports - refactor to work with Cameras
Browse files Browse the repository at this point in the history
  • Loading branch information
paroj committed Feb 27, 2025
1 parent 0baa453 commit 63ba6a3
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 251 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,36 +29,22 @@ furnished to do so, subject to the following conditions:
// Transform the output position to the current "monitor"
//-----------------------------------------------------------------------------

#ifdef OGRE_VERTEX_SHADER
void SGX_InstancedViewportsTransform(
in vec4 i_position,
in mat4 i_worldViewMatrix,
in mat4 i_projectionMatrix,
in vec4 i_viewportOffsetMatrixR0,
in vec4 i_viewportOffsetMatrixR1,
in vec4 i_viewportOffsetMatrixR2,
in vec4 i_viewportOffsetMatrixR3,
in vec2 i_monitorsCount,
in mat4 viewportOffsetArray[NUM_MONITORS],
in vec4 i_monitorIndex,
out vec4 o_position)
{
o_position = mul(i_worldViewMatrix, i_position);
mat4 viewportOffset = mtxFromRows(i_viewportOffsetMatrixR0,
i_viewportOffsetMatrixR1,
i_viewportOffsetMatrixR2,
i_viewportOffsetMatrixR3);

o_position = mul(viewportOffset, o_position);
o_position = mul(i_projectionMatrix, o_position);

vec2 monitorIndexNorm = i_monitorIndex.xy - ((i_monitorsCount - 1.0)/2.0);
o_position.xy =
(o_position.xy + (o_position.w * monitorIndexNorm)*2.0) / i_monitorsCount;
mat4 viewportOffset = viewportOffsetArray[int(i_monitorIndex.z)];
o_position = mul(viewportOffset, i_position);
}
#endif

//-----------------------------------------------------------------------------
// Discard any pixel that is outside the bounds of the current "monitor"
//-----------------------------------------------------------------------------

#ifdef OGRE_FRAGMENT_SHADER
void SGX_InstancedViewportsDiscardOutOfBounds(
in vec2 i_monitorsCount,
in vec4 i_monitorIndex,
Expand All @@ -72,8 +58,8 @@ void SGX_InstancedViewportsDiscardOutOfBounds(
float maxM = max(boxedXY.x,boxedXY.y);
if (maxM >= 0.5)
{
#ifdef OGRE_FRAGMENT_SHADER

discard;
#endif
}
}
#endif
58 changes: 7 additions & 51 deletions Samples/ShaderSystem/include/OgreShaderExInstancedViewports.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,66 +82,22 @@ class ShaderExInstancedViewports : public SubRenderState

/** Set the monitors count. */
void setMonitorsCount (const Vector2 monitorsCount);

/** Return the monitors count. */
Vector2 getMonitorsCount () const { return mMonitorsCount; }

void setParameter(const String& name, const Any& value) override;

static String Type;

// Protected methods.
protected:

bool createCpuSubPrograms(ProgramSet* programSet) override;

/**
@see SubRenderState::resolveParameters.
*/
bool resolveParameters (ProgramSet* programSet) override;
UniformParameterPtr mPSInMonitorsCount;
UniformParameterPtr mVSInMatrixArray;

/**
@see SubRenderState::resolveDependencies.
*/
bool resolveDependencies (ProgramSet* programSet) override;

/**
@see SubRenderState::addFunctionInvocations.
*/
bool addFunctionInvocations (ProgramSet* programSet) override;

/**
Internal method that adds related vertex shader functions invocations.
*/
bool addVSInvocations (Function* vsMain, const int groupOrder);


/**
Internal method that adds related pixel shader functions invocations.
*/
bool addPSInvocations (Function* psMain, const int groupOrder);


// Attributes.
protected:
ParameterPtr mVSInPosition; // Vertex shader original input position in projective space.
ParameterPtr mVSOriginalOutPositionProjectiveSpace; // Vertex shader original output position in projective space.
ParameterPtr mVSOutPositionProjectiveSpace; // Vertex shader output texcord position in projective space.
ParameterPtr mPSInPositionProjectiveSpace; // Pixel shader input position in projective space.
UniformParameterPtr mVSInMonitorsCount; // Vertex shader uniform monitors count.
UniformParameterPtr mPSInMonitorsCount; // Pixel shader uniform monitors count.
ParameterPtr mVSInMonitorIndex; // Vertex shader uniform monitor index.
ParameterPtr mVSOutMonitorIndex; // Vertex shader output monitor index.
ParameterPtr mPSInMonitorIndex; // Pixel shader input monitor index.

ParameterPtr mVSInViewportOffsetMatrixR0;
ParameterPtr mVSInViewportOffsetMatrixR1;
ParameterPtr mVSInViewportOffsetMatrixR2;
ParameterPtr mVSInViewportOffsetMatrixR3;

UniformParameterPtr mWorldViewMatrix; // world & view parameter.
UniformParameterPtr mProjectionMatrix; // projection parameter.

Vector2 mMonitorsCount;
Vector2 mViewportGrid;
bool mMonitorsCountChanged;

std::vector<Camera*> mCameras;
};


Expand Down
Loading

0 comments on commit 63ba6a3

Please sign in to comment.