Skip to content

Commit b53b1fc

Browse files
authored
Merge pull request #54 from GPUOpen-LibrariesAndSDKs/more_samples_2
More samples 2
2 parents f01802b + 4880352 commit b53b1fc

File tree

13 files changed

+1002
-117
lines changed

13 files changed

+1002
-117
lines changed

Resources/Textures/flower.jpg

170 KB
Loading

tutorials/29_ocio/compare.jpg

1.14 KB
Loading

tutorials/29_ocio/main.cpp

Lines changed: 23 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,6 @@
4242
// simple garbage collector for RPR objects.
4343
RPRGarbageCollector g_gc;
4444

45-
// vertices data
46-
struct vertexP3N3T2
47-
{
48-
rpr_float pos[3];
49-
rpr_float norm[3];
50-
rpr_float tex[2];
51-
};
52-
5345

5446
// Convert Color Space :
5547
// From Linear sRGB to ACEScg
@@ -79,83 +71,6 @@ RadeonProRender::float4 sRGB_to_ACEScg(const RadeonProRender::float4& srgb)
7971
}
8072

8173

82-
// create a simple quad shape
83-
rpr_shape CreateQuad(rpr_context context, rpr_scene scene, vertexP3N3T2* meshVertices, unsigned int meshVertices_nbOfElement )
84-
{
85-
rpr_int indices[] = { 3,2,1,0, };
86-
rpr_int num_face_vertices[] = { 4, };
87-
88-
const unsigned int num_face_vertices_nbOfElement = sizeof(num_face_vertices)/sizeof(num_face_vertices[0]);
89-
90-
rpr_shape mesh = nullptr;
91-
92-
CHECK( rprContextCreateMesh(context,
93-
(rpr_float const*)&meshVertices[0], meshVertices_nbOfElement , sizeof(vertexP3N3T2),
94-
(rpr_float const*)((char*)&meshVertices[0] + sizeof(rpr_float)*3), meshVertices_nbOfElement, sizeof(vertexP3N3T2),
95-
(rpr_float const*)((char*)&meshVertices[0] + sizeof(rpr_float)*6), meshVertices_nbOfElement, sizeof(vertexP3N3T2),
96-
(rpr_int const*)indices, sizeof(rpr_int),
97-
(rpr_int const*)indices, sizeof(rpr_int),
98-
(rpr_int const*)indices, sizeof(rpr_int),
99-
num_face_vertices, num_face_vertices_nbOfElement, &mesh) );
100-
101-
if ( scene ) { CHECK( rprSceneAttachShape(scene, mesh) ); }
102-
103-
return mesh;
104-
}
105-
106-
// Create a Quad shape on the YZ plane
107-
rpr_shape CreateQuad_YZ(rpr_context context, rpr_scene scene, float ax, float ay, float bx, float by, float X)
108-
{
109-
vertexP3N3T2 meshVertices[] =
110-
{
111-
{ X, ax, by, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f },
112-
{ X, bx, by, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f },
113-
{ X, bx, ay, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f },
114-
{ X, ax, ay, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f },
115-
};
116-
117-
const unsigned int meshVertices_nbOfElement = sizeof(meshVertices)/sizeof(meshVertices[0]);
118-
119-
rpr_shape mesh = CreateQuad(context, scene, meshVertices, meshVertices_nbOfElement);
120-
return mesh;
121-
}
122-
123-
// Create a Quad shape on the XZ plane
124-
rpr_shape CreateQuad_XZ(rpr_context context, rpr_scene scene, float ax, float ay, float bx, float by, float Y, float normal)
125-
{
126-
vertexP3N3T2 meshVertices[] =
127-
{
128-
{ ax, Y, by, 0.0f, normal, 0.0f, 0.0f, 0.0f },
129-
{ bx, Y, by, 0.0f, normal, 0.0f, 1.0f, 0.0f },
130-
{ bx, Y, ay, 0.0f, normal, 0.0f, 1.0f, 1.0f },
131-
{ ax, Y, ay, 0.0f, normal, 0.0f, 0.0f, 1.0f },
132-
};
133-
134-
const unsigned int meshVertices_nbOfElement = sizeof(meshVertices)/sizeof(meshVertices[0]);
135-
136-
rpr_shape mesh = CreateQuad(context, scene, meshVertices, meshVertices_nbOfElement);
137-
return mesh;
138-
}
139-
140-
// Create a Quad shape on the XY plane
141-
rpr_shape CreateQuad_XY(rpr_context context, rpr_scene scene, float ax, float ay, float bx, float by, float Z)
142-
{
143-
vertexP3N3T2 meshVertices[] =
144-
{
145-
{ ax, by, Z, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f },
146-
{ bx, by, Z, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f },
147-
{ bx, ay, Z, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f },
148-
{ ax, ay, Z, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f },
149-
};
150-
151-
const unsigned int meshVertices_nbOfElement = sizeof(meshVertices)/sizeof(meshVertices[0]);
152-
153-
rpr_shape mesh = CreateQuad(context, scene, meshVertices, meshVertices_nbOfElement);
154-
return mesh;
155-
}
156-
157-
158-
15974

16075
int main()
16176
{
@@ -350,35 +265,21 @@ int main()
350265
const float lightX1 = lightX0 + 0.8f;
351266
const float lightX2 = 4.0f - 0.4f - 0.8f;
352267
const float lightX3 = lightX2 + 0.8f;
353-
rpr_shape roof0 = CreateQuad_XZ(context, scene, -4.0f, -2.0f , lightX0, +2.0f , roofY, -1.0f);
354-
rpr_shape roof1 = CreateQuad_XZ(context, scene, lightX0, -2.0f , lightX1 , -2.0f+0.8*2.0f , roofY, -1.0f);
355-
rpr_shape roof2 = CreateQuad_XZ(context, scene, lightX0, -2.0f+0.8*3.0f , lightX1 , +2.0f , roofY, -1.0f);
356-
rpr_shape roofLightLeft = CreateQuad_XZ(context, scene, lightX0, -2.0f+0.8*2.0f , lightX1 , -2.0f+0.8*3.0f , roofY, -1.0f);
357-
rpr_shape roof3 = CreateQuad_XZ(context, scene, lightX1, -2.0f , lightX2, +2.0f , roofY, -1.0f);
358-
rpr_shape roof4 = CreateQuad_XZ(context, scene, lightX2, -2.0f , lightX3 , -2.0f+0.8*2.0f , roofY, -1.0f);
359-
rpr_shape roof5 = CreateQuad_XZ(context, scene, lightX2, -2.0f+0.8*3.0f , lightX3 , +2.0f , roofY, -1.0f);
360-
rpr_shape roofLightRight = CreateQuad_XZ(context, scene, lightX2, -2.0f+0.8*2.0f , lightX3 , -2.0f+0.8*3.0f , roofY, -1.0f);
361-
rpr_shape roof6 = CreateQuad_XZ(context, scene, lightX3, -2.0f , 4.0f, +2.0f , roofY, -1.0f);
362-
rpr_shape floor = CreateQuad_XZ(context, scene, -4.0f, -2.0f , +4.0f, +2.0f , 0.0f, +1.0f);
363-
rpr_shape shapeWallLeft = CreateQuad_YZ(context, scene, 0.0, -2.0f , roofY, +2.0f , -4.0f);
364-
rpr_shape shapeWallRight = CreateQuad_YZ(context, scene, 0.0, -2.0f , roofY, +2.0f , +4.0f);
365-
rpr_shape shapeWallBack = CreateQuad_XY(context, scene, -4.0, 0.0, +4.0f, roofY , -2.0f);
366-
rpr_shape pictureShape = CreateQuad_XY(context, scene, 0.0, 0.3, +3.5f, roofY-0.3f , -1.9f);
367-
g_gc.GCAdd(roof0);
368-
g_gc.GCAdd(roof1);
369-
g_gc.GCAdd(roof2);
370-
g_gc.GCAdd(roofLightLeft);
371-
g_gc.GCAdd(roof3);
372-
g_gc.GCAdd(roof4);
373-
g_gc.GCAdd(roof5);
374-
g_gc.GCAdd(roofLightRight);
375-
g_gc.GCAdd(roof6);
376-
g_gc.GCAdd(floor);
377-
g_gc.GCAdd(shapeWallLeft);
378-
g_gc.GCAdd(shapeWallRight);
379-
g_gc.GCAdd(shapeWallBack);
380-
g_gc.GCAdd(pictureShape);
381-
268+
rpr_shape roof0 = CreateQuad_XZ(g_gc, context, scene, -4.0f, -2.0f , lightX0, +2.0f , roofY, -1.0f);
269+
rpr_shape roof1 = CreateQuad_XZ(g_gc, context, scene, lightX0, -2.0f , lightX1 , -2.0f+0.8*2.0f , roofY, -1.0f);
270+
rpr_shape roof2 = CreateQuad_XZ(g_gc, context, scene, lightX0, -2.0f+0.8*3.0f , lightX1 , +2.0f , roofY, -1.0f);
271+
rpr_shape roofLightLeft = CreateQuad_XZ(g_gc, context, scene, lightX0, -2.0f+0.8*2.0f , lightX1 , -2.0f+0.8*3.0f , roofY, -1.0f);
272+
rpr_shape roof3 = CreateQuad_XZ(g_gc, context, scene, lightX1, -2.0f , lightX2, +2.0f , roofY, -1.0f);
273+
rpr_shape roof4 = CreateQuad_XZ(g_gc, context, scene, lightX2, -2.0f , lightX3 , -2.0f+0.8*2.0f , roofY, -1.0f);
274+
rpr_shape roof5 = CreateQuad_XZ(g_gc, context, scene, lightX2, -2.0f+0.8*3.0f , lightX3 , +2.0f , roofY, -1.0f);
275+
rpr_shape roofLightRight = CreateQuad_XZ(g_gc, context, scene, lightX2, -2.0f+0.8*2.0f , lightX3 , -2.0f+0.8*3.0f , roofY, -1.0f);
276+
rpr_shape roof6 = CreateQuad_XZ(g_gc, context, scene, lightX3, -2.0f , 4.0f, +2.0f , roofY, -1.0f);
277+
rpr_shape floor = CreateQuad_XZ(g_gc, context, scene, -4.0f, -2.0f , +4.0f, +2.0f , 0.0f, +1.0f);
278+
rpr_shape shapeWallLeft = CreateQuad_YZ(g_gc, context, scene, 0.0, -2.0f , roofY, +2.0f , -4.0f, 1.0f);
279+
rpr_shape shapeWallRight = CreateQuad_YZ(g_gc, context, scene, 0.0, -2.0f , roofY, +2.0f , +4.0f, 1.0f);
280+
rpr_shape shapeWallBack = CreateQuad_XY(g_gc, context, scene, -4.0, 0.0, +4.0f, roofY , -2.0f, 1.0f);
281+
rpr_shape pictureShape = CreateQuad_XY(g_gc, context, scene, 0.0, 0.3, +3.5f, roofY-0.3f , -1.9f, 1.0f);
282+
382283

383284
// create material for the left wall
384285
{
@@ -415,6 +316,14 @@ int main()
415316
// This means the texture is using the sRGB color space with a gamma correction.
416317
CHECK( rprImageSetOcioColorspace(img , "Utility - sRGB - Texture") );
417318
}
319+
else
320+
{
321+
// art.jpg image is stored in the non-linear sRGB space.
322+
// if we are not using the OCIO workflow, then we need to set the gamma for non-linear images ( like most of .png, .jpg... representing color )
323+
// This gamma value is used to do a basic IDT operation, transforming the image from non-linear "Texture Colorspace" to a linear "Render Colorspace".
324+
// In our case: from "non-linear sRGB" to "linear sRGB"
325+
CHECK( rprImageSetGamma(img, 2.2) );
326+
}
418327

419328
CHECK( rprMaterialSystemCreateNode(matsys, RPR_MATERIAL_NODE_IMAGE_TEXTURE, &imgSampler));
420329
g_gc.GCAdd(imgSampler);
Lines changed: 222 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
1+
/*****************************************************************************\
2+
*
3+
* Module Name Per Face Material Demo
4+
* Project Radeon ProRender rendering tutorial
5+
*
6+
* Description Radeon ProRender SDK tutorials
7+
*
8+
* Copyright(C) 2011-2022 Advanced Micro Devices, Inc. All rights reserved.
9+
*
10+
\*****************************************************************************/
11+
12+
#include "RadeonProRender.h"
13+
#include "Math/mathutils.h"
14+
#include "../common/common.h"
15+
#include <cassert>
16+
#include <iostream>
17+
18+
//
19+
// Demo of the rprShapeSetMaterialFaces API. This allows to set materials for specific faces of the shape.
20+
//
21+
22+
// garbage collector of this demo
23+
RPRGarbageCollector g_gc;
24+
25+
int main()
26+
{
27+
// for Debugging you can enable Radeon ProRender API trace
28+
// set this before any RPR API calls
29+
// rprContextSetParameterByKey1u(0,RPR_CONTEXT_TRACING_ENABLED,1);
30+
31+
// the RPR context object.
32+
rpr_context context = nullptr;
33+
34+
// Register the RPR DLL
35+
rpr_int tahoePluginID = rprRegisterPlugin(RPR_PLUGIN_FILE_NAME);
36+
CHECK_NE(tahoePluginID , -1);
37+
rpr_int plugins[] = { tahoePluginID };
38+
size_t pluginCount = sizeof(plugins) / sizeof(plugins[0]);
39+
40+
// Create context
41+
CHECK( rprCreateContext(RPR_API_VERSION, plugins, pluginCount, g_ContextCreationFlags, NULL, NULL, &context) );
42+
43+
// Set the active plugin.
44+
CHECK( rprContextSetActivePlugin(context, plugins[0]) );
45+
46+
std::cout << "RPR Context creation succeeded." << std::endl;
47+
48+
// Create the scene.
49+
rpr_scene scene = nullptr;
50+
CHECK( rprContextCreateScene(context, &scene) );
51+
CHECK( rprContextSetScene(context, scene) );
52+
53+
// Create the camera
54+
rpr_camera camera = nullptr;
55+
CHECK( rprContextCreateCamera(context, &camera) );
56+
CHECK( rprCameraLookAt(camera, 0, 10, 12, 0, -2, 0, 0, 1, 0) );
57+
CHECK( rprSceneSetCamera(scene, camera) );
58+
59+
// Create an environment light
60+
CHECK( CreateNatureEnvLight(context, scene, g_gc, 1.0f) );
61+
62+
// create the material system
63+
rpr_material_system matsys = nullptr;
64+
CHECK( rprContextCreateMaterialSystem(context, 0, &matsys) );
65+
66+
67+
68+
// create the shape plane
69+
rpr_shape plane = NULL;
70+
{
71+
72+
const int gridSize = 4; // create a 3x3 grid (4*4 vertices)
73+
vertex plane_data[gridSize*gridSize];
74+
75+
// grid dimensions
76+
const float minGrid = -5.0f;
77+
const float maxGrid = +5.0f;
78+
79+
// create grid
80+
for(int y=0; y<gridSize; y++)
81+
{
82+
for(int x=0; x<gridSize; x++)
83+
{
84+
plane_data[y*gridSize+x].pos[0] = minGrid + (maxGrid-minGrid) / ((float)gridSize-1.0f) * ((float)x);
85+
plane_data[y*gridSize+x].pos[1] = 0.0f;
86+
plane_data[y*gridSize+x].pos[2] = minGrid + (maxGrid-minGrid) / ((float)gridSize-1.0f) * ((float)y);
87+
88+
plane_data[y*gridSize+x].norm[0] = 0.0f;
89+
plane_data[y*gridSize+x].norm[1] = 1.0f;
90+
plane_data[y*gridSize+x].norm[2] = 0.0f;
91+
92+
plane_data[y*gridSize+x].tex[0] = (plane_data[y*gridSize+x].pos[0] - minGrid) / (maxGrid - minGrid);
93+
plane_data[y*gridSize+x].tex[1] = 1.0f - (plane_data[y*gridSize+x].pos[2] - minGrid) / (maxGrid - minGrid);
94+
}
95+
}
96+
97+
// create indices of the grid
98+
rpr_int indices[(gridSize-1)*(gridSize-1)*4];
99+
for(int y=0; y<gridSize-1; y++)
100+
{
101+
for(int x=0; x<gridSize-1; x++)
102+
{
103+
indices[ (x+y*(gridSize-1))*4 + 0 ] = y*gridSize+x ;
104+
indices[ (x+y*(gridSize-1))*4 + 1 ] = y*gridSize+(x+1) ;
105+
indices[ (x+y*(gridSize-1))*4 + 3 ] = (y+1)*gridSize+x ;
106+
indices[ (x+y*(gridSize-1))*4 + 2 ] = (y+1)*gridSize+(x+1) ;
107+
}
108+
}
109+
110+
// create face list
111+
rpr_int num_face_vertices[(gridSize-1)*(gridSize-1)];
112+
for(int y=0; y<gridSize-1; y++)
113+
{
114+
for(int x=0; x<gridSize-1; x++)
115+
{
116+
num_face_vertices[ (x+y*(gridSize-1)) ] = 4; // use quad face: 4 vertices per face.
117+
}
118+
}
119+
120+
CHECK( rprContextCreateMesh(context,
121+
(rpr_float const*)&plane_data[0], gridSize*gridSize, sizeof(vertex),
122+
(rpr_float const*)((char*)&plane_data[0] + sizeof(rpr_float)*3), gridSize*gridSize, sizeof(vertex),
123+
(rpr_float const*)((char*)&plane_data[0] + sizeof(rpr_float)*6), gridSize*gridSize, sizeof(vertex),
124+
(rpr_int const*)indices, sizeof(rpr_int),
125+
(rpr_int const*)indices, sizeof(rpr_int),
126+
(rpr_int const*)indices, sizeof(rpr_int),
127+
num_face_vertices, (gridSize-1)*(gridSize-1), &plane) );
128+
g_gc.GCAdd(plane);
129+
130+
// attach the plane to the scene.
131+
CHECK( rprSceneAttachShape(scene, plane));
132+
133+
}
134+
135+
136+
// create the base material for the plane shape
137+
{
138+
rpr_material_node diffuseMaterial = nullptr;
139+
rpr_image image2 = nullptr;
140+
rpr_material_node materialImage2 = nullptr;
141+
142+
const std::string pathImageFileA = "../../Resources/Textures/flower.jpg";
143+
rpr_status status = rprContextCreateImageFromFile(context, pathImageFileA.c_str(), &image2);
144+
g_gc.GCAdd(image2);
145+
if (status == RPR_ERROR_IO_ERROR)
146+
{
147+
std::cout << "Error : " << pathImageFileA << " not found.\n";
148+
return status;
149+
}
150+
CHECK(status);
151+
152+
// This texture is using the classic 2.2 gamma compression.
153+
CHECK( rprImageSetGamma(image2, 2.2f) );
154+
155+
CHECK(rprMaterialSystemCreateNode(matsys, RPR_MATERIAL_NODE_IMAGE_TEXTURE, &materialImage2));
156+
g_gc.GCAdd(materialImage2);
157+
CHECK(rprMaterialNodeSetInputImageDataByKey(materialImage2, RPR_MATERIAL_INPUT_DATA, image2)); // Set image data
158+
159+
CHECK(rprMaterialSystemCreateNode(matsys, RPR_MATERIAL_NODE_DIFFUSE, &diffuseMaterial));
160+
g_gc.GCAdd(diffuseMaterial);
161+
CHECK(rprMaterialNodeSetInputNByKey(diffuseMaterial, RPR_MATERIAL_INPUT_COLOR, materialImage2)); // set image sampler as the color input of diffuse material
162+
163+
164+
// assign the material on the shape
165+
CHECK(rprShapeSetMaterial(plane, diffuseMaterial));
166+
167+
}
168+
169+
// create a simple Red diffuse material
170+
rpr_material_node diffuseRed = NULL;
171+
CHECK( rprMaterialSystemCreateNode(matsys, RPR_MATERIAL_NODE_DIFFUSE,&diffuseRed) );
172+
g_gc.GCAdd(diffuseRed);
173+
CHECK( rprMaterialNodeSetInputFByKey(diffuseRed, RPR_MATERIAL_INPUT_COLOR, 1, 0.1, 0.05f, 1) );
174+
175+
// create a simple Blue diffuse material
176+
rpr_material_node diffuseBlue = NULL;
177+
CHECK( rprMaterialSystemCreateNode(matsys, RPR_MATERIAL_NODE_DIFFUSE,&diffuseBlue) );
178+
g_gc.GCAdd(diffuseBlue);
179+
CHECK( rprMaterialNodeSetInputFByKey(diffuseBlue, RPR_MATERIAL_INPUT_COLOR, 0.05f, 0.1f, 1, 1) );
180+
181+
// assign the Red material to faces '0' and '2'
182+
rpr_int indicesList1[] = { 0, 2 };
183+
CHECK( rprShapeSetMaterialFaces(plane, diffuseRed, indicesList1, sizeof(indicesList1) / sizeof(indicesList1[0]) ) );
184+
185+
// assign the Blue material to faces '5', '6' and '7'
186+
rpr_int indicesList2[] = { 5, 6, 7 };
187+
CHECK( rprShapeSetMaterialFaces(plane, diffuseBlue, indicesList2, sizeof(indicesList2) / sizeof(indicesList2[0]) ) );
188+
189+
// Create framebuffer
190+
rpr_framebuffer_desc desc = { 800 , 600 };
191+
rpr_framebuffer_format fmt = {4, RPR_COMPONENT_TYPE_FLOAT32};
192+
rpr_framebuffer frame_buffer = nullptr;
193+
rpr_framebuffer frame_buffer_resolved = nullptr;
194+
CHECK( rprContextCreateFrameBuffer(context, fmt, &desc, &frame_buffer) );
195+
CHECK( rprContextCreateFrameBuffer(context, fmt, &desc, &frame_buffer_resolved) );
196+
CHECK( rprContextSetAOV(context, RPR_AOV_COLOR, frame_buffer) );
197+
198+
// set rendering gamma
199+
CHECK( rprContextSetParameterByKey1f(context, RPR_CONTEXT_DISPLAY_GAMMA , 2.2f ) );
200+
201+
// Render the scene
202+
CHECK(rprContextSetParameterByKey1u(context,RPR_CONTEXT_ITERATIONS, 200));
203+
CHECK( rprContextRender(context) );
204+
CHECK(rprContextResolveFrameBuffer(context,frame_buffer,frame_buffer_resolved,false));
205+
CHECK(rprFrameBufferSaveToFile(frame_buffer_resolved,"34.png"));
206+
207+
208+
// Release the stuff we created
209+
CHECK(rprObjectDelete(camera)); camera=nullptr;
210+
CHECK(rprObjectDelete(frame_buffer)); frame_buffer=nullptr;
211+
CHECK(rprObjectDelete(frame_buffer_resolved)); frame_buffer_resolved=nullptr;
212+
g_gc.GCClean();
213+
CHECK(rprObjectDelete(scene)); scene=nullptr;
214+
CHECK(rprObjectDelete(matsys)); matsys=nullptr;
215+
CheckNoLeak(context);
216+
CHECK(rprObjectDelete(context)); context=nullptr;
217+
return 0;
218+
}
219+
220+
221+
222+

0 commit comments

Comments
 (0)