Skip to content

Commit

Permalink
Traktor: Write to velocity buffer in Ocean shader.
Browse files Browse the repository at this point in the history
  • Loading branch information
apistol78 committed May 28, 2024
1 parent ec851c4 commit 7daf294
Show file tree
Hide file tree
Showing 6 changed files with 1,274 additions and 906 deletions.
36 changes: 30 additions & 6 deletions code/Terrain/OceanComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "Terrain/TerrainComponent.h"
#include "World/Entity.h"
#include "World/IWorldRenderPass.h"
#include "World/WorldHandles.h"
#include "World/WorldRenderView.h"
#include "World/WorldSetupContext.h"

Expand All @@ -40,6 +41,7 @@ const render::Handle s_handleTerrain_WorldOrigin(L"Terrain_WorldOrigin");
const render::Handle s_handleTerrain_WorldExtent(L"Terrain_WorldExtent");
const render::Handle s_handleOcean_HaveTerrain(L"Ocean_HaveTerrain");
const render::Handle s_handleOcean_Eye(L"Ocean_Eye");
const render::Handle s_handleOcean_LastEye(L"Ocean_LastEye");
const render::Handle s_handleOcean_ShallowTint(L"Ocean_ShallowTint");
const render::Handle s_handleOcean_DeepColor(L"Ocean_DeepColor");
const render::Handle s_handleOcean_Opacity(L"Ocean_Opacity");
Expand Down Expand Up @@ -85,6 +87,8 @@ bool OceanComponent::create(resource::IResourceManager* resourceManager, render:
m_spectrumTexture = renderSystem->createSimpleTexture(stcd, T_FILE_LINE_W);
m_evolvedSpectrumTextures[0] = renderSystem->createSimpleTexture(stcd, T_FILE_LINE_W);
m_evolvedSpectrumTextures[1] = renderSystem->createSimpleTexture(stcd, T_FILE_LINE_W);
m_evolvedSpectrumTextures[2] = renderSystem->createSimpleTexture(stcd, T_FILE_LINE_W);
m_evolvedSpectrumTextures[3] = renderSystem->createSimpleTexture(stcd, T_FILE_LINE_W);
m_foamTexture = renderSystem->createSimpleTexture(stcd, T_FILE_LINE_W);

AlignedVector< render::VertexElement > vertexElements;
Expand Down Expand Up @@ -176,6 +180,8 @@ void OceanComponent::destroy()
safeDestroy(m_spectrumTexture);
safeDestroy(m_evolvedSpectrumTextures[0]);
safeDestroy(m_evolvedSpectrumTextures[1]);
safeDestroy(m_evolvedSpectrumTextures[2]);
safeDestroy(m_evolvedSpectrumTextures[3]);
safeDestroy(m_foamTexture);
m_shader.clear();
}
Expand Down Expand Up @@ -266,6 +272,12 @@ void OceanComponent::setup(
m_spectrumDirty = false;
}

// Swap textures so we have last and current.
{
std::swap(m_evolvedSpectrumTextures[0], m_evolvedSpectrumTextures[2]);
std::swap(m_evolvedSpectrumTextures[1], m_evolvedSpectrumTextures[3]);
}

// Evolve spectrum over time.
{
Ref< render::RenderPass > rp = new render::RenderPass(L"Ocean compute spectrum evolve");
Expand Down Expand Up @@ -388,6 +400,7 @@ void OceanComponent::build(
if (!m_owner || worldRenderView.getSnapshot())
return;

const bool writeVelocity = (worldRenderPass.getTechnique() == world::s_techniqueVelocityWrite);
bool haveTerrain = false;

// Get terrain from owner.
Expand All @@ -399,9 +412,8 @@ void OceanComponent::build(
}

const Transform transform = m_owner->getTransform() * Transform(Vector4(0.0f, m_elevation, 0.0f, 0.0f));
const Matrix44& view = worldRenderView.getView();
const Matrix44 viewInv = view.inverse();
const Vector4 eye = viewInv.translation().xyz1();
const Vector4 lastEye = worldRenderView.getLastView().inverse().translation().xyz1();
const Vector4 eye = worldRenderView.getView().inverse().translation().xyz1();

// Render ocean geometry.
auto perm = worldRenderPass.getPermutation(m_shader);
Expand All @@ -423,11 +435,23 @@ void OceanComponent::build(
renderBlock->programParams->beginParameters(renderContext);
renderBlock->programParams->setFloatParameter(s_handleOcean_Opacity, m_opacity);
renderBlock->programParams->setVectorParameter(s_handleOcean_Eye, eye);
renderBlock->programParams->setVectorParameter(s_handleOcean_LastEye, lastEye);
renderBlock->programParams->setVectorParameter(s_handleOcean_ShallowTint, m_shallowTint);
renderBlock->programParams->setVectorParameter(s_handleOcean_DeepColor, m_deepColor);
renderBlock->programParams->setTextureParameter(s_handleOcean_WaveTexture0, m_evolvedSpectrumTextures[0]);
renderBlock->programParams->setTextureParameter(s_handleOcean_WaveTexture1, m_evolvedSpectrumTextures[1]);
renderBlock->programParams->setTextureParameter(s_handleOcean_WaveTexture2, m_foamTexture);

if (!writeVelocity)
{
renderBlock->programParams->setTextureParameter(s_handleOcean_WaveTexture0, m_evolvedSpectrumTextures[0]);
renderBlock->programParams->setTextureParameter(s_handleOcean_WaveTexture1, m_evolvedSpectrumTextures[1]);
renderBlock->programParams->setTextureParameter(s_handleOcean_WaveTexture2, m_foamTexture);
}
else
{
renderBlock->programParams->setTextureParameter(s_handleOcean_WaveTexture0, m_evolvedSpectrumTextures[0]);
renderBlock->programParams->setTextureParameter(s_handleOcean_WaveTexture1, m_evolvedSpectrumTextures[1]);
renderBlock->programParams->setTextureParameter(s_handleOcean_WaveTexture2, m_evolvedSpectrumTextures[2]);
renderBlock->programParams->setTextureParameter(s_handleOcean_WaveTexture3, m_evolvedSpectrumTextures[3]);
}

if (haveTerrain)
{
Expand Down
2 changes: 1 addition & 1 deletion code/Terrain/OceanComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class T_DLLCLASS OceanComponent : public world::IEntityComponent
resource::Proxy< render::Shader > m_shaderWave; //!< Compute shader to generate wave maps.
resource::Proxy< render::Shader > m_shader;
Ref< render::ITexture > m_spectrumTexture;
Ref< render::ITexture > m_evolvedSpectrumTextures[2];
Ref< render::ITexture > m_evolvedSpectrumTextures[4];
Ref< render::ITexture > m_foamTexture;
Ref< const render::IVertexLayout > m_vertexLayout;
Ref< render::Buffer > m_indexBuffer;
Expand Down
1 change: 1 addition & 0 deletions code/World/Shared/WorldRenderPassShared.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ void WorldRenderPassShared::setWorldProgramParameters(render::ProgramParameters*
{
const Matrix44 w0 = lastWorld.toMatrix44();
programParams->setMatrixParameter(s_handleLastWorld, w0);
programParams->setMatrixParameter(s_handleLastView, m_worldRenderView.getLastView());
programParams->setMatrixParameter(s_handleLastWorldView, m_worldRenderView.getLastView() * w0);
}
}
Expand Down
1 change: 1 addition & 0 deletions code/World/WorldHandles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const render::Handle s_handleFogDistanceAndDensity(L"World_FogDistanceAndDensity
const render::Handle s_handleGamma(L"World_Gamma");
const render::Handle s_handleGammaInverse(L"World_GammaInverse");
const render::Handle s_handleLastWorld(L"World_LastWorld");
const render::Handle s_handleLastView(L"World_LastView");
const render::Handle s_handleLastWorldView(L"World_LastWorldView");
const render::Handle s_handleLightDiffuseMap(L"World_LightDiffuseMap");
const render::Handle s_handleLightIndexSBuffer(L"World_LightIndexSBuffer");
Expand Down
1 change: 1 addition & 0 deletions code/World/WorldHandles.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ extern const render::Handle T_DLLCLASS s_handleFogDistanceAndDensity;
extern const render::Handle T_DLLCLASS s_handleGamma;
extern const render::Handle T_DLLCLASS s_handleGammaInverse;
extern const render::Handle T_DLLCLASS s_handleLastWorld;
extern const render::Handle T_DLLCLASS s_handleLastView;
extern const render::Handle T_DLLCLASS s_handleLastWorldView;
extern const render::Handle T_DLLCLASS s_handleLightDiffuseMap;
extern const render::Handle T_DLLCLASS s_handleLightIndexSBuffer;
Expand Down
Loading

0 comments on commit 7daf294

Please sign in to comment.