Skip to content

Commit a6142ae

Browse files
authored
RPRUN-134 Material support for Hybrid context (#49)
PURPOSE Improve handling of materials EFFECT OF CHANGE Better applying of UE simple materials in case of Hybrid context
1 parent 030842f commit a6142ae

File tree

11 files changed

+355
-364
lines changed

11 files changed

+355
-364
lines changed

Plugins/RPRPlugin/Source/RPRCore/Private/Material/RPRXMaterialLibrary.cpp

Lines changed: 22 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -226,9 +226,9 @@ RPR::FRPRXMaterialNodePtr FRPRXMaterialLibrary::createMaterial(FString name, uns
226226
return materialPtr;
227227
}
228228

229-
RPR::RPRXVirtualNode* FRPRXMaterialLibrary::createVirtualNode(FString materialNode, RPR::RPRXVirtualNode::VNType nodeType)
229+
RPR::VirtualNode* FRPRXMaterialLibrary::createVirtualNode(FString materialNode, RPR::EVirtualNode nodeType)
230230
{
231-
m_virtualNodes.Emplace(materialNode, MakeUnique<RPR::RPRXVirtualNode>(materialNode, nodeType));
231+
m_virtualNodes.Emplace(materialNode, MakeUnique<RPR::VirtualNode>(materialNode, nodeType));
232232
return m_virtualNodes.Find(materialNode)->Get();
233233
}
234234

@@ -258,7 +258,7 @@ void FRPRXMaterialLibrary::ReleaseCache()
258258
m_materialNodes.Empty();
259259
}
260260

261-
RPR::RPRXVirtualNode* FRPRXMaterialLibrary::getVirtualNode(FString materialNode)
261+
RPR::VirtualNode* FRPRXMaterialLibrary::getVirtualNode(FString materialNode)
262262
{
263263
return m_virtualNodes.Contains(materialNode) ?
264264
m_virtualNodes.Find(materialNode)->Get() : nullptr;
@@ -270,50 +270,25 @@ RPR::FMaterialNode FRPRXMaterialLibrary::getNode(FString materialNode)
270270
return ptr ? *ptr : nullptr;
271271
}
272272

273-
RPR::RPRXVirtualNode* FRPRXMaterialLibrary::getOrCreateVirtualIfNotExists(FString materialNode, RPR::EMaterialNodeType type)
273+
RPR::VirtualNode* FRPRXMaterialLibrary::getOrCreateVirtualIfNotExists(FString materialNode, RPR::EMaterialNodeType rprNodeType, RPR::EVirtualNode vNodeType)
274274
{
275-
RPR::FMaterialNode realNode = nullptr;
276-
RPR::RPRXVirtualNode::VNType vType = RPR::RPRXVirtualNode::VNType::DEFAULT;
275+
RPR::VirtualNode* node = getVirtualNode(materialNode);
277276

278-
switch (type)
279-
{
280-
case RPR::EMaterialNodeType::ImageTexture:
281-
vType = RPR::RPRXVirtualNode::VNType::IMAGE;
282-
break;
283-
case RPR::EMaterialNodeType::SelectX:
284-
case RPR::EMaterialNodeType::SelectY:
285-
case RPR::EMaterialNodeType::SelectZ:
286-
case RPR::EMaterialNodeType::SelectW:
287-
vType = RPR::RPRXVirtualNode::VNType::TEXTURE_CHANNEL;
288-
break;
289-
case RPR::EMaterialNodeType::Arithmetic:
290-
vType = RPR::RPRXVirtualNode::VNType::ARITHMETIC_2_OPERANDS;
291-
realNode = getOrCreateIfNotExists(materialNode, type);
292-
break;
293-
default:
294-
break;
295-
}
277+
if (node)
278+
return node;
296279

297-
RPR::RPRXVirtualNode* node = getVirtualNode(materialNode);
298-
if (!node) {
299-
node = createVirtualNode(materialNode, vType);
300-
if (!node)
301-
return nullptr;
302-
else
303-
node->realNode = realNode;
304-
}
280+
node = createVirtualNode(materialNode, vNodeType);
305281

306-
return node;
307-
}
282+
switch (vNodeType)
283+
{
284+
case RPR::EVirtualNode::TEXTURE: // For texture image data we create RPR Node separately
285+
case RPR::EVirtualNode::CONSTANT: // There is no need to create RPR Node, holds only float values
286+
return node;
287+
}
308288

309-
RPR::RPRXVirtualNode* FRPRXMaterialLibrary::getOrCreateVirtualIfNotExists(FString materialNode, RPR::RPRXVirtualNode::VNType type)
310-
{
311-
RPR::RPRXVirtualNode* node;
289+
if (rprNodeType != RPR::EMaterialNodeType::None)
290+
node->rprNode = getOrCreateIfNotExists(materialNode, rprNodeType);
312291

313-
node = getVirtualNode(materialNode);
314-
if (!node) {
315-
node = createVirtualNode(materialNode, type);
316-
}
317292
return node;
318293
}
319294

@@ -349,25 +324,17 @@ void FRPRXMaterialLibrary::setNodeUInt(RPR::FMaterialNode materialNode, unsigned
349324

350325
}
351326

352-
void FRPRXMaterialLibrary::setNodeConnection(RPR::RPRXVirtualNode* vNode, const unsigned int parameter, RPR::RPRXVirtualNode* otherNode)
327+
void FRPRXMaterialLibrary::setNodeConnection(RPR::VirtualNode* vNode, const unsigned int parameter, const RPR::VirtualNode* otherNode)
353328
{
354329
if (!otherNode) {
355-
setNodeConnection(vNode->realNode, parameter, GetDummyMaterial());
330+
setNodeConnection(vNode->rprNode, parameter, GetDummyMaterial());
356331
return;
357332
}
358333

359-
switch (otherNode->type)
360-
{
361-
case RPR::RPRXVirtualNode::VNType::COLOR:
362-
setNodeFloat(vNode->realNode, parameter, otherNode->data.RGBA[0], otherNode->data.RGBA[1], otherNode->data.RGBA[2], otherNode->data.RGBA[3]);
363-
break;
364-
365-
case RPR::RPRXVirtualNode::VNType::ARITHMETIC_2_OPERANDS: /* true for ADD, SUB, MUL, DIV */
366-
case RPR::RPRXVirtualNode::VNType::TEXTURE_CHANNEL:
367-
case RPR::RPRXVirtualNode::VNType::IMAGE:
368-
default:
369-
setNodeConnection(vNode->realNode, parameter, otherNode->realNode);
370-
}
334+
if (otherNode->type == RPR::EVirtualNode::CONSTANT)
335+
setNodeFloat(vNode->rprNode, parameter, otherNode->constant.R, otherNode->constant.G, otherNode->constant.B, otherNode->constant.A);
336+
else
337+
setNodeConnection(vNode->rprNode, parameter, otherNode->rprNode);
371338
}
372339

373340
void FRPRXMaterialLibrary::setNodeConnection(RPR::FMaterialNode materialNode, const unsigned int parameter, RPR::FMaterialNode otherNode)

Plugins/RPRPlugin/Source/RPRCore/Private/Material/Tools/MaterialCacheMaker/ParameterSetters/MaterialMap/MaterialMapParameterSetter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ namespace RPRX
203203
RPR::FMaterialNode uvScaledNode;
204204
status = RPR::FMaterialHelpers::FArithmeticNode::CreateArithmeticNode(
205205
materialContext.MaterialSystem,
206-
RPR::EMaterialNodeArithmeticOperation::Multiply,
206+
RPR::EMaterialNodeArithmeticOperation::Mul,
207207
TEXT("Arithmetic for UV scale - Multiply"),
208208
uvScaledNode);
209209
check(status == 0);

Plugins/RPRPlugin/Source/RPRCore/Private/RPRCoreSystemResources.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "Helpers/RPRHelpers.h"
2222
#include "RPRSettings.h"
2323
#include "RPRCoreErrorHelper.h"
24+
#include "RadeonProRender_Baikal.h"
2425
#define RIF_STATIC_LIBRARY 0
2526
#include "RadeonImageFilters.h"
2627

@@ -63,9 +64,6 @@ FRPRCoreSystemResources::FRPRCoreSystemResources()
6364

6465
bool FRPRCoreSystemResources::Initialize()
6566
{
66-
if (CurrentContextType == RPR::GetSettings()->CurrentRenderType)
67-
return true;
68-
6967
CurrentContextType = RPR::GetSettings()->CurrentRenderType;
7068

7169
if (!bIsInitialized)

Plugins/RPRPlugin/Source/RPRCore/Private/RPRXVirtualNode.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,20 @@
33
namespace RPR
44
{
55

6-
RPRXVirtualNode::~RPRXVirtualNode()
6+
VirtualNode::VirtualNode(FString aID, EVirtualNode aType)
7+
: id(aID)
8+
, type(aType)
9+
, rprNode(nullptr)
10+
, constant{}
11+
, texture(false)
12+
{}
13+
14+
void VirtualNode::SetData(float r, float g, float b, float a)
715
{
16+
constant.R = r;
17+
constant.G = g;
18+
constant.B = b;
19+
constant.A = a;
820
}
921

1022
}//namespace RPR

Plugins/RPRPlugin/Source/RPRCore/Public/Material/RPRXMaterialLibrary.h

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -52,39 +52,38 @@ class RPRCORE_API FRPRXMaterialLibrary : public IObjectScopedLockable
5252
bool TryGetMaterial(const URPRMaterial* MaterialKey, RPR::FRPRXMaterialPtr& OutRPRXMaterial) const;
5353
void ClearCache();
5454

55-
RPR::FMaterialNode GetDummyMaterial() const;
56-
RPR::FRPRXMaterialPtr GetMaterial(const URPRMaterial* MaterialKey);
55+
RPR::FMaterialNode GetDummyMaterial() const;
56+
RPR::FRPRXMaterialPtr GetMaterial(const URPRMaterial* MaterialKey);
5757

58-
virtual FCriticalSection& GetCriticalSection() override;
58+
virtual FCriticalSection& GetCriticalSection() override;
5959

6060
RPR::FRPRXMaterialNodePtr createMaterial(FString name, unsigned int type = RPR_MATERIAL_NODE_UBERV2);
6161
bool hasMaterial(FString materialName) const;
62-
RPR::FRPRXMaterialNodePtr getMaterial(FString materialName);
62+
RPR::FRPRXMaterialNodePtr getMaterial(FString materialName);
6363

6464
RPR::FMaterialNode createNode(FString materialNode, RPR::EMaterialNodeType type = RPR::EMaterialNodeType::Diffuse);
65-
RPR::RPRXVirtualNode* createVirtualNode(FString materialNode, RPR::RPRXVirtualNode::VNType nodeType);
65+
RPR::VirtualNode* createVirtualNode(FString materialNode, RPR::EVirtualNode nodeType);
6666
bool hasNode(FString materialNode) const;
6767
RPR::FMaterialNode getNode(FString materialNode);
68-
RPR::RPRXVirtualNode* getVirtualNode(FString materialNode);
68+
RPR::VirtualNode* getVirtualNode(FString materialNode);
6969
RPR::FMaterialNode getOrCreateIfNotExists(FString materialNode, RPR::EMaterialNodeType type = RPR::EMaterialNodeType::Diffuse);
70-
RPR::RPRXVirtualNode* getOrCreateVirtualIfNotExists(FString materialNode, RPR::EMaterialNodeType type);
71-
RPR::RPRXVirtualNode* getOrCreateVirtualIfNotExists(FString materialNode, RPR::RPRXVirtualNode::VNType type);
70+
RPR::VirtualNode* getOrCreateVirtualIfNotExists(FString materialNode, RPR::EMaterialNodeType rprNodeType = RPR::EMaterialNodeType::None, RPR::EVirtualNode type = RPR::EVirtualNode::OTHER);
7271
void setNodeFloat(RPR::FMaterialNode materialNode, const unsigned int parameter, float r, float g, float b, float a);
7372
void setNodeUInt(RPR::FMaterialNode materialNode, const unsigned int parameter, unsigned int value);
74-
void setNodeConnection(RPR::RPRXVirtualNode* materialNode, const unsigned int parameter, RPR::RPRXVirtualNode* otherNode);
73+
void setNodeConnection(RPR::VirtualNode* materialNode, const unsigned int parameter, const RPR::VirtualNode* otherNode);
7574
void setNodeConnection(RPR::FMaterialNode MaterialNode, const unsigned int ParameterName, RPR::FMaterialNode InMaterialNode);
7675
void ReleaseCache();
7776

7877
RPR::FMaterialNode createImage(UTexture2D* texture);
7978
RPR::FMaterialNode createImageNodeFromImageData(const FString& nodeId, RPR::FImagePtr imagePtr);
8079

8180
private:
82-
const RPR::FRPRXMaterialPtr FindMaterialCache(const URPRMaterial* MaterialKey) const;
83-
RPR::FRPRXMaterialPtr FindMaterialCache(const URPRMaterial* MaterialKey);
81+
const RPR::FRPRXMaterialPtr FindMaterialCache(const URPRMaterial* MaterialKey) const;
82+
RPR::FRPRXMaterialPtr FindMaterialCache(const URPRMaterial* MaterialKey);
8483

8584
void InitializeDummyMaterial();
8685
void DestroyDummyMaterial();
87-
void DestroyMaterialGraph();
86+
void DestroyMaterialGraph();
8887

8988
RPR::FRPRXMaterialPtr CacheMaterial(URPRMaterial* InMaterial);
9089
RPR::FMaterialContext CreateMaterialContext() const;
@@ -96,14 +95,14 @@ class RPRCORE_API FRPRXMaterialLibrary : public IObjectScopedLockable
9695
// 1. Unassign root materials from Meshes
9796
// 2. Unlink all mat. nodes from each other
9897
// 3. Destroy nodes
99-
TMap<FString, RPR::FRPRXMaterialNodePtr> m_materials;
100-
TMap<FString, RPR::FMaterialNode> m_materialNodes;
101-
TMap<FString, TUniquePtr<RPR::RPRXVirtualNode>> m_virtualNodes;
102-
103-
bool bIsInitialized;
104-
FCriticalSection CriticalSection;
105-
RPR::FMaterialNode DummyMaterial;
106-
RPR::FMaterialNode TestMaterial;
98+
TMap<FString, RPR::FRPRXMaterialNodePtr> m_materials;
99+
TMap<FString, RPR::FMaterialNode> m_materialNodes;
100+
TMap<FString, TUniquePtr<RPR::VirtualNode>> m_virtualNodes;
101+
102+
bool bIsInitialized;
103+
FCriticalSection CriticalSection;
104+
RPR::FMaterialNode DummyMaterial;
105+
RPR::FMaterialNode TestMaterial;
107106
};
108107

109108
using FRPRXMaterialLibrarySL = FObjectScopedLocked<FRPRXMaterialLibrary>;
Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,35 @@
11
#pragma once
22

33
#include "Typedefs/RPRTypedefs.h"
4+
#include "Color.h"
45

56
/**
6-
* In case of a color node, we don't create a real node and use a virtual node to hold the data.
7+
* In case of a constant nodes, we don't create a RPR real node and use a virtual node to hold the data.
78
*/
89
namespace RPR
910
{
1011

11-
class RPRCORE_API RPRXVirtualNode
12+
enum class EVirtualNode
1213
{
13-
FString name;
14-
public:
15-
RPR::FMaterialNode realNode;
16-
enum VNType {
17-
COLOR,
18-
IMAGE,
19-
TEXTURE_CHANNEL,
20-
ARITHMETIC_2_OPERANDS,
21-
DEFAULT
22-
} type;
23-
24-
union Data {
25-
float RGBA[4];
26-
} data;
14+
OTHER,
15+
TEXTURE,
16+
CONSTANT
17+
};
2718

19+
class RPRCORE_API VirtualNode
20+
{
2821
public:
29-
RPRXVirtualNode(FString name = "", VNType t = VNType::DEFAULT) : name(name), realNode(nullptr), type(t) {
30-
data.RGBA[0] = 0.0f; data.RGBA[1] = 0.0f; data.RGBA[2] = 0.0f; data.RGBA[3] = 0.0f;
31-
}
32-
33-
void SetData(float r, float g, float b, float a) {
34-
data.RGBA[0] = r; data.RGBA[1] = g; data.RGBA[2] = b; data.RGBA[3] = a;
35-
}
22+
FString id;
23+
EVirtualNode type;
24+
RPR::FMaterialNode rprNode;
25+
FLinearColor constant;
26+
bool texture;
3627

37-
RPRXVirtualNode(const RPRXVirtualNode&) = delete;
38-
RPRXVirtualNode& operator=(const RPRXVirtualNode&) = delete;
28+
public:
29+
VirtualNode(FString aID = "", EVirtualNode aType = EVirtualNode::OTHER);
30+
void SetData(float r, float g, float b, float a);
3931

40-
~RPRXVirtualNode();
32+
VirtualNode(const VirtualNode&) = delete;
33+
VirtualNode& operator=(const VirtualNode&) = delete;
4134
};
42-
}//namespace RPR
35+
}//namespace RPR

Plugins/RPRPlugin/Source/RPRPlugin/Private/Renderer/RPRRendererWorker.cpp

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -909,18 +909,19 @@ uint32 FRPRRendererWorker::Run()
909909
}
910910
{
911911
SCOPE_CYCLE_COUNTER(STAT_ProRender_Resolve);
912-
if (RPR::Context::ResolveFrameBuffer(m_RprContext, m_RprFrameBuffer, m_RprResolvedFrameBuffer) != RPR_SUCCESS ||
913-
RPR::Context::ResolveFrameBuffer(m_RprContext, m_RprShadingNormalBuffer, m_RprShadingNormalResolvedBuffer) != RPR_SUCCESS ||
914-
RPR::Context::ResolveFrameBuffer(m_RprContext, m_RprAovDepthBuffer, m_RprAovDepthResolvedBuffer) != RPR_SUCCESS ||
915-
RPR::Context::ResolveFrameBuffer(m_RprContext, m_RprDiffuseAlbedoBuffer, m_RprDiffuseAlbedoResolvedBuffer) != RPR_SUCCESS ||
916-
RPR::Context::ResolveFrameBuffer(m_RprContext, m_RprWorldCoordinatesBuffer, m_RprWorldCoordinatesResolvedBuffer) != RPR_SUCCESS ||
917-
RPR::Context::ResolveFrameBuffer(m_RprContext, m_RprAovDepthBuffer, m_RprAovDepthResolvedBuffer) != RPR_SUCCESS ||
918-
RPR::Context::ResolveFrameBuffer(m_RprContext, m_RprDiffuseAlbedoBuffer, m_RprDiffuseAlbedoResolvedBuffer) != RPR_SUCCESS)
919-
{
920-
RPR::Error::LogLastError(m_RprContext);
921-
m_RenderLock.Unlock();
922-
UE_LOG(LogRPRRenderer, Error, TEXT("Couldn't resolve framebuffer at iteration %d, stopping.."), m_CurrentIteration);
923-
}
912+
if (!settings->IsHybrid)
913+
if (RPR::Context::ResolveFrameBuffer(m_RprContext, m_RprFrameBuffer, m_RprResolvedFrameBuffer) != RPR_SUCCESS ||
914+
RPR::Context::ResolveFrameBuffer(m_RprContext, m_RprShadingNormalBuffer, m_RprShadingNormalResolvedBuffer) != RPR_SUCCESS ||
915+
RPR::Context::ResolveFrameBuffer(m_RprContext, m_RprAovDepthBuffer, m_RprAovDepthResolvedBuffer) != RPR_SUCCESS ||
916+
RPR::Context::ResolveFrameBuffer(m_RprContext, m_RprDiffuseAlbedoBuffer, m_RprDiffuseAlbedoResolvedBuffer) != RPR_SUCCESS ||
917+
RPR::Context::ResolveFrameBuffer(m_RprContext, m_RprWorldCoordinatesBuffer, m_RprWorldCoordinatesResolvedBuffer) != RPR_SUCCESS ||
918+
RPR::Context::ResolveFrameBuffer(m_RprContext, m_RprAovDepthBuffer, m_RprAovDepthResolvedBuffer) != RPR_SUCCESS ||
919+
RPR::Context::ResolveFrameBuffer(m_RprContext, m_RprDiffuseAlbedoBuffer, m_RprDiffuseAlbedoResolvedBuffer) != RPR_SUCCESS)
920+
{
921+
RPR::Error::LogLastError(m_RprContext);
922+
m_RenderLock.Unlock();
923+
UE_LOG(LogRPRRenderer, Error, TEXT("Couldn't resolve framebuffer at iteration %d, stopping.."), m_CurrentIteration);
924+
}
924925
}
925926
m_RenderLock.Unlock();
926927

Plugins/RPRPlugin/Source/RPRPlugin/Private/Scene/RPRStaticMeshComponent.cpp

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -255,10 +255,11 @@ bool URPRStaticMeshComponent::Build()
255255
const FPositionVertexBuffer &srcPositions = FRPRCpStaticMesh::GetPositionVertexBufferConst(lodRes);
256256
const uint32 uvCount = srcVertices.GetNumTexCoords();
257257

258+
auto settings = RPR::GetSettings();
258259
const uint32 sectionCount = lodRes.Sections.Num();
259260
for (uint32 iSection = 0; iSection < sectionCount; ++iSection)
260261
{
261-
const FStaticMeshSection &section = lodRes.Sections[iSection];
262+
const FStaticMeshSection& section = lodRes.Sections[iSection];
262263
const uint32 srcIndexStart = section.FirstIndex;
263264
const uint32 indexCount = section.NumTriangles * 3;
264265

@@ -323,8 +324,11 @@ bool URPRStaticMeshComponent::Build()
323324
Cache[staticMesh].Add(newShape);
324325

325326
// New shape in the cache ? Add it in the scene + make it invisible
326-
status = rprShapeSetVisibility(baseShape, false);
327-
CHECK_ERROR(status, TEXT("Can't set shape visibility to false"));
327+
if (!settings->IsHybrid)
328+
{
329+
status = rprShapeSetVisibility(baseShape, false);
330+
CHECK_ERROR(status, TEXT("Can't set shape visibility to false"));
331+
}
328332

329333
status = RPR::Scene::AttachShape(Scene->m_RprScene, baseShape);
330334
CHECK_ERROR(status, TEXT("Couldn't attach Cached RPR shape to the RPR scene"));
@@ -355,18 +359,19 @@ bool URPRStaticMeshComponent::Build()
355359
rpr_shape shape = m_Shapes[iShape].m_RprShape;
356360
status = SetInstanceTransforms(instancedMeshComponent, &componentMatrix, shape, m_Shapes[iShape].m_InstanceIndex);
357361
CHECK_ERROR(status, TEXT("Can't set shape transform"));
358-
359-
if (!primaryOnly)
360-
{
361-
status = rprShapeSetVisibility(shape, staticMeshComponent->IsVisible());
362-
CHECK_ERROR(status, TEXT("Can't set shape visibility"));
363-
}
364-
else
362+
if (!settings->IsHybrid)
365363
{
366-
status = rprShapeSetVisibility(shape, true);
367-
CHECK_ERROR(status, TEXT("Can't set shape visibility"));
364+
if (!primaryOnly)
365+
{
366+
status = rprShapeSetVisibility(shape, staticMeshComponent->IsVisible());
367+
CHECK_ERROR(status, TEXT("Can't set shape visibility"));
368+
}
369+
else
370+
{
371+
status = rprShapeSetVisibility(shape, true);
372+
CHECK_ERROR(status, TEXT("Can't set shape visibility"));
373+
}
368374
}
369-
370375
//rprShapeSetShadow(shape, staticMeshComponent->bCastStaticShadow) != RPR_SUCCESS ||
371376
status = RPR::Scene::AttachShape(Scene->m_RprScene, shape);
372377
CHECK_ERROR(status, TEXT("Couldn't attach RPR shape to the RPR scene"));

0 commit comments

Comments
 (0)