Skip to content

Commit

Permalink
Support vrb ProgramFactory (#2773)
Browse files Browse the repository at this point in the history
* Support vrb ProgramFactory

* Update vrb

Co-authored-by: Imanol Fernandez <mortimergoro@gmail.com>
  • Loading branch information
bluemarvin and MortimerGoro committed Feb 12, 2020
1 parent 97bb737 commit 37ab021
Show file tree
Hide file tree
Showing 14 changed files with 97 additions and 11 deletions.
2 changes: 2 additions & 0 deletions app/src/main/cpp/BrowserWorld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#include "vrb/NodeFactoryObj.h"
#include "vrb/ParserObj.h"
#include "vrb/PerformanceMonitor.h"
#include "vrb/ProgramFactory.h"
#include "vrb/RenderContext.h"
#include "vrb/RenderState.h"
#include "vrb/SurfaceTextureFactory.h"
Expand Down Expand Up @@ -194,6 +195,7 @@ struct BrowserWorld::State {
context = RenderContext::Create();
create = context->GetRenderThreadCreationContext();
loader = ModelLoaderAndroid::Create(context);
context->GetProgramFactory()->SetLoaderThread(loader);
rootOpaque = Transform::Create(create);
rootTransparent = Transform::Create(create);
rootController = Group::Create(create);
Expand Down
6 changes: 5 additions & 1 deletion app/src/main/cpp/ControllerContainer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@

#include "vrb/ConcreteClass.h"
#include "vrb/Color.h"
#include "vrb/CreationContext.h"
#include "vrb/Geometry.h"
#include "vrb/Group.h"
#include "vrb/Matrix.h"
#include "vrb/ModelLoaderAndroid.h"
#include "vrb/Program.h"
#include "vrb/ProgramFactory.h"
#include "vrb/RenderState.h"
#include "vrb/Toggle.h"
#include "vrb/Transform.h"
Expand Down Expand Up @@ -107,8 +110,9 @@ ControllerContainer::InitializeBeam() {
array->AppendNormal(Vector(-1.0f, 1.0f, 0.0f).Normalize()); // Top left
array->AppendNormal(Vector(0.0f, 0.0f, -1.0f).Normalize()); // in to the screen


ProgramPtr program = create->GetProgramFactory()->CreateProgram(create, 0);
RenderStatePtr state = RenderState::Create(create);
state->SetProgram(program);
state->SetMaterial(Color(1.0f, 1.0f, 1.0f), Color(1.0f, 1.0f, 1.0f), Color(0.0f, 0.0f, 0.0f), 0.0f);
state->SetLightsEnabled(false);
GeometryPtr geometry = Geometry::Create(create);
Expand Down
34 changes: 31 additions & 3 deletions app/src/main/cpp/Cylinder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#include "vrb/CreationContext.h"
#include "vrb/Matrix.h"
#include "vrb/Geometry.h"
#include "vrb/Program.h"
#include "vrb/ProgramFactory.h"
#include "vrb/RenderState.h"
#include "vrb/SurfaceTextureFactory.h"
#include "vrb/TextureSurface.h"
Expand Down Expand Up @@ -163,9 +165,6 @@ struct Cylinder::State {

vrb::RenderStatePtr state = vrb::RenderState::Create(create);
state->SetLightsEnabled(false);
state->SetVertexColorEnabled(border > 0.0f);
state->SetFragmentPrecision(GL_HIGH_FLOAT);
state->SetUVTransformEnabled(true);
geometry->SetRenderState(state);

return geometry;
Expand Down Expand Up @@ -237,6 +236,35 @@ Cylinder::Create(vrb::CreationContextPtr aContext, const VRLayerCylinderPtr& aLa
return result;
}

void
Cylinder::UpdateProgram(const std::string& aCustomFragmentShader) {
if (!m.geometry) {
return;
}
vrb::CreationContextPtr create = m.context.lock();
if (!create) {
return;
}
uint32_t features = vrb::FeatureHighPrecision | vrb::FeatureUVTransform;
if (m.border > 0) {
features |= vrb::FeatureVertexColor;
}

vrb::TexturePtr texture = m.geometry->GetRenderState()->GetTexture();
if (texture) {
if (texture->GetTarget() == GL_TEXTURE_CUBE_MAP) {
features |= vrb::FeatureCubeTexture;
} else if (dynamic_cast<vrb::TextureSurface*>(texture.get()) != nullptr) {
features |= vrb::FeatureSurfaceTexture;
} else {
features |= vrb::FeatureTexture;
}
}

vrb::ProgramPtr program = create->GetProgramFactory()->CreateProgram(create, features, aCustomFragmentShader);
m.geometry->GetRenderState()->SetProgram(program);
}

void
Cylinder::GetTextureSize(int32_t& aWidth, int32_t& aHeight) const {
aWidth = (int32_t)(m.textureWidth * m.textureScaleX);
Expand Down
1 change: 1 addition & 0 deletions app/src/main/cpp/Cylinder.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class Cylinder {
static CylinderPtr Create(vrb::CreationContextPtr aContext, const VRLayerCylinderPtr& aLayer = nullptr);
static CylinderPtr Create(vrb::CreationContextPtr aContext, const Cylinder& aCylinder);
static float kWorldDensityRatio;
void UpdateProgram(const std::string& aCustomFragmentShader);
void GetTextureSize(int32_t& aWidth, int32_t& aHeight) const;
void SetTextureSize(int32_t aWidth, int32_t aHeight);
void SetTexture(const vrb::TexturePtr& aTexture, int32_t aWidth, int32_t aHeight);
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/cpp/Pointer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#include "vrb/Geometry.h"
#include "vrb/Matrix.h"
#include "vrb/ModelLoaderAndroid.h"
#include "vrb/Program.h"
#include "vrb/ProgramFactory.h"
#include "vrb/RenderState.h"
#include "vrb/RenderContext.h"
#include "vrb/TextureCubeMap.h"
Expand Down Expand Up @@ -110,10 +112,13 @@ struct Pointer::State {
geometry = createCircle(kResolution, kInnerRadius, kOffset);
vrb::GeometryPtr geometryOuter = createCircle(kResolution, kOuterRadius, kOffset);

vrb::ProgramPtr program = create->GetProgramFactory()->CreateProgram(create, 0);
vrb::RenderStatePtr state = vrb::RenderState::Create(create);
state->SetProgram(program);
state->SetMaterial(pointerColor, pointerColor, vrb::Color(0.0f, 0.0f, 0.0f), 0.0f);
geometry->SetRenderState(state);
vrb::RenderStatePtr stateOuter = vrb::RenderState::Create(create);
stateOuter->SetProgram(program);
stateOuter->SetMaterial(POINTER_COLOR_OUTER, POINTER_COLOR_OUTER, vrb::Color(0.0f, 0.0f, 0.0f), 0.0f);
geometryOuter->SetRenderState(stateOuter);
pointerScale->AddNode(geometry);
Expand Down
28 changes: 28 additions & 0 deletions app/src/main/cpp/Quad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#include "vrb/CreationContext.h"
#include "vrb/Matrix.h"
#include "vrb/Geometry.h"
#include "vrb/Program.h"
#include "vrb/ProgramFactory.h"
#include "vrb/RenderState.h"
#include "vrb/SurfaceTextureFactory.h"
#include "vrb/TextureSurface.h"
Expand Down Expand Up @@ -239,6 +241,32 @@ Quad::CreateGeometry(vrb::CreationContextPtr aContext, const float aWorldWidth,
return Quad::CreateGeometry(std::move(aContext), -max, max);
}

void
Quad::UpdateProgram(const std::string& aCustomFragmentShader) {
if (!m.geometry) {
return;
}
vrb::CreationContextPtr create = m.context.lock();
if (!create) {
return;
}
uint32_t features = vrb::FeatureHighPrecision | vrb::FeatureUVTransform;

vrb::TexturePtr texture = m.geometry->GetRenderState()->GetTexture();
if (texture) {
if (texture->GetTarget() == GL_TEXTURE_CUBE_MAP) {
features |= vrb::FeatureCubeTexture;
} else if (dynamic_cast<vrb::TextureSurface*>(texture.get()) != nullptr) {
features |= vrb::FeatureSurfaceTexture;
} else {
features |= vrb::FeatureTexture;
}
}

vrb::ProgramPtr program = create->GetProgramFactory()->CreateProgram(create, features, aCustomFragmentShader);
m.geometry->GetRenderState()->SetProgram(program);
}

void
Quad::SetTexture(const vrb::TexturePtr& aTexture, int32_t aWidth, int32_t aHeight) {
m.textureWidth = aWidth;
Expand Down
1 change: 1 addition & 0 deletions app/src/main/cpp/Quad.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class Quad {
static vrb::GeometryPtr CreateGeometry(vrb::CreationContextPtr aContext, const vrb::Vector& aMin, const vrb::Vector& aMax);
static vrb::GeometryPtr CreateGeometry(vrb::CreationContextPtr aContext, const float aWorldWidth, const float aWorldHeight);
static vrb::GeometryPtr CreateGeometry(vrb::CreationContextPtr aContext, const vrb::Vector& aMin, const vrb::Vector& aMax, const device::EyeRect& aRect);
void UpdateProgram(const std::string& aCustomFragmentShader);
void SetTexture(const vrb::TexturePtr& aTexture, int32_t aWidth, int32_t aHeight);
void SetMaterial(const vrb::Color& aAmbient, const vrb::Color& aDiffuse, const vrb::Color& aSpecular, const float aSpecularExponent);
void SetScaleMode(ScaleMode aScaleMode);
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/cpp/Skybox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#include "vrb/Geometry.h"
#include "vrb/Matrix.h"
#include "vrb/ModelLoaderAndroid.h"
#include "vrb/Program.h"
#include "vrb/ProgramFactory.h"
#include "vrb/RenderState.h"
#include "vrb/RenderContext.h"
#include "vrb/TextureCubeMap.h"
Expand Down Expand Up @@ -130,7 +132,9 @@ struct Skybox::State {
cubeIndices[i + 2] + 1, cubeIndices[i + 3] + 1};
geometry->AddFace(indices, indices, {});
}
ProgramPtr program = aContext->GetProgramFactory()->CreateProgram(aContext, FeatureCubeTexture);
RenderStatePtr state = RenderState::Create(aContext);
state->SetProgram(program);
geometry->SetRenderState(state);

texture = LoadTextureCube(aContext, basePath, extension);
Expand Down
1 change: 1 addition & 0 deletions app/src/main/cpp/SplashAnimation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ SplashAnimation::Load(vrb::RenderContextPtr& aContext, const DeviceDelegatePtr&
m.logo = Quad::Create(create, w, w / aspect, m.layer);
if (!m.layer) {
m.logo->SetTexture(texture, texture->GetWidth(), texture->GetHeight());
m.logo->UpdateProgram("");
}
m.root->AddNode(m.logo->GetRoot());
}
Expand Down
7 changes: 6 additions & 1 deletion app/src/main/cpp/VRVideo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#include "vrb/Geometry.h"
#include "vrb/Matrix.h"
#include "vrb/ModelLoaderAndroid.h"
#include "vrb/Program.h"
#include "vrb/ProgramFactory.h"
#include "vrb/RenderState.h"
#include "vrb/RenderContext.h"
#include "vrb/TextureGL.h"
Expand Down Expand Up @@ -155,9 +157,10 @@ struct VRVideo::State {

std::vector<int> indices;

vrb::ProgramPtr program = create->GetProgramFactory()->CreateProgram(create, vrb::FeatureSurfaceTexture | vrb::FeatureHighPrecision);
vrb::RenderStatePtr state = vrb::RenderState::Create(create);
state->SetProgram(program);
state->SetLightsEnabled(false);
state->SetFragmentPrecision(GL_HIGH_FLOAT);
vrb::TexturePtr texture = std::dynamic_pointer_cast<vrb::Texture>(window->GetSurfaceTexture());
state->SetTexture(texture);
vrb::GeometryPtr geometry = vrb::Geometry::Create(create);
Expand Down Expand Up @@ -308,7 +311,9 @@ struct VRVideo::State {
vrb::Vector min, max;
window->GetWidgetMinAndMax(min, max);
vrb::GeometryPtr geometry = Quad::CreateGeometry(create, min, max, aUVRect);
vrb::ProgramPtr program = create->GetProgramFactory()->CreateProgram(create, vrb::FeatureSurfaceTexture);
vrb::RenderStatePtr state = vrb::RenderState::Create(create);
state->SetProgram(program);
state->SetLightsEnabled(false);
vrb::TexturePtr texture = std::dynamic_pointer_cast<vrb::Texture>(window->GetSurfaceTexture());
state->SetTexture(texture);
Expand Down
6 changes: 4 additions & 2 deletions app/src/main/cpp/Widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,13 @@ struct Widget::State {
if (quad) {
quad->SetTexture(surface, aTextureWidth, aTextureHeight);
quad->SetMaterial(vrb::Color(0.4f, 0.4f, 0.4f), vrb::Color(1.0f, 1.0f, 1.0f), vrb::Color(0.0f, 0.0f, 0.0f), 0.0f);
quad->GetRenderState()->SetCustomFragmentShader(customFragment);
quad->GetRenderState()->SetTintColor(tintColor);
quad->UpdateProgram(customFragment);
} else if (cylinder) {
cylinder->SetTexture(surface, aTextureWidth, aTextureHeight);
cylinder->SetMaterial(vrb::Color(0.4f, 0.4f, 0.4f), vrb::Color(1.0f, 1.0f, 1.0f), vrb::Color(0.0f, 0.0f, 0.0f), 0.0f);
cylinder->GetRenderState()->SetCustomFragmentShader(customFragment);
cylinder->GetRenderState()->SetTintColor(tintColor);
cylinder->UpdateProgram(customFragment);
}
}
}
Expand Down Expand Up @@ -617,10 +617,12 @@ Widget::SetProxifyLayer(const bool aValue) {
proxy->SetCylinderTheta(m.cylinder->GetCylinderTheta());
proxy->SetTexture(proxySurface, textureWidth, textureHeight);
proxy->SetTransform(m.cylinder->GetTransformNode()->GetTransform());
proxy->UpdateProgram("");
m.layerProxy->AddNode(proxy->GetRoot());
} else {
QuadPtr proxy = Quad::Create(create, *m.quad);
proxy->SetTexture(proxySurface, textureWidth, textureHeight);
proxy->UpdateProgram("");
m.layerProxy->AddNode(proxy->GetRoot());
}
}
Expand Down
6 changes: 4 additions & 2 deletions app/src/main/cpp/WidgetBorder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "vrb/VertexArray.h"
#include "WidgetBorder.h"
#include <vector>
#include <vrb/include/vrb/ProgramFactory.h>

namespace crow {

Expand Down Expand Up @@ -89,7 +90,6 @@ struct WidgetBorder::State {
array->AppendNormal(normal);

vrb::RenderStatePtr state = vrb::RenderState::Create(aContext);
state->SetVertexColorEnabled(true);
vrb::GeometryPtr geometry = vrb::Geometry::Create(aContext);
geometry->SetVertexArray(array);
geometry->SetRenderState(state);
Expand Down Expand Up @@ -142,11 +142,13 @@ WidgetBorderPtr WidgetBorder::Create(vrb::CreationContextPtr& aContext, const vr
const std::string customFragment =
#include "shaders/clear_color.fs"
;
result->m.cylinder->GetRenderState()->SetCustomFragmentShader(customFragment);
result->m.cylinder->UpdateProgram(customFragment);
result->m.transform->AddNode(result->m.cylinder->GetRoot());
} else {
result->m.geometry = result->m.CreateGeometry(aContext, -max, max, aBorderRect);
result->m.geometry->GetRenderState()->SetLightsEnabled(false);
vrb::ProgramPtr program = aContext->GetProgramFactory()->CreateProgram(aContext, vrb::FeatureVertexColor);
result->m.geometry->GetRenderState()->SetProgram(program);
result->m.transform->AddNode(result->m.geometry);
}

Expand Down
5 changes: 4 additions & 1 deletion app/src/main/cpp/WidgetResizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
#include "vrb/Matrix.h"
#include "vrb/GLError.h"
#include "vrb/Geometry.h"
#include "vrb/Program.h"
#include "vrb/ProgramFactory.h"
#include "vrb/RenderState.h"
#include "vrb/SurfaceTextureFactory.h"
#include "vrb/TextureGL.h"
Expand Down Expand Up @@ -177,9 +179,10 @@ struct ResizeHandle {
normalIndices.push_back(1);

vrb::GeometryPtr geometry = vrb::Geometry::Create(aContext);
vrb::ProgramPtr program = aContext->GetProgramFactory()->CreateProgram(aContext, vrb::FeatureVertexColor);
vrb::RenderStatePtr state = vrb::RenderState::Create(aContext);
state->SetProgram(program);
state->SetLightsEnabled(false);
state->SetVertexColorEnabled(true);
geometry->SetVertexArray(array);
geometry->SetRenderState(state);
geometry->AddFace(indices, indices, normalIndices);
Expand Down

0 comments on commit 37ab021

Please sign in to comment.