Skip to content

Commit 838267a

Browse files
authored
Merge pull request #25 from GPUOpen-LibrariesAndSDKs/demos
Update OpenGL demo and readme
2 parents 5ca51cc + 767d581 commit 838267a

File tree

3 files changed

+115
-25
lines changed

3 files changed

+115
-25
lines changed

readme.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,19 @@ On Visual Studio:
1818
then open tutorials/Tutorials.sln
1919
```
2020

21-
On Linux:
21+
On Linux:
22+
Dependencies on Linux for the tutorials: GLEW, GLUT, Pthread.
2223
```
2324
> cd tutorials
25+
> sudo chmod +x ../premake5/linux64/premake5
2426
> ../premake5/linux64/premake5 gmake
2527
> make -j config=release_x64
2628
```
2729

2830
On MacOS:
2931
```
3032
> cd tutorials
33+
> sudo chmod +x ../premake5/osx/premake5
3134
> ../premake5/osx/premake5 gmake
3235
> make -j config=release_x64
3336
```

tutorials/32_gl_interop/main.cpp

Lines changed: 110 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
/*****************************************************************************\
22
*
3-
* Module Name simple_render.cpp
3+
* Module Name gl_interop.cpp
44
* Project Radeon ProRender SDK rendering tutorial
55
*
66
* Description Radeon ProRender SDK tutorials
7+
* Demo of an OpenGL window rendering RPR.
78
*
89
* Copyright 2011 - 2020 Advanced Micro Devices, Inc.
910
*
@@ -38,10 +39,44 @@
3839

3940
#include <cassert>
4041
#include <iostream>
42+
#include <thread>
4143

4244
#define WINDOW_WIDTH 800
4345
#define WINDOW_HEIGHT 600
4446

47+
48+
class GuiRenderImpl
49+
{
50+
public:
51+
struct Update
52+
{
53+
Update()
54+
{
55+
clear();
56+
m_progress = 0.0f;
57+
}
58+
59+
volatile int m_hasUpdate;
60+
volatile int m_done;
61+
volatile int m_aborted;
62+
int m_camUpdated;
63+
float m_progress;
64+
65+
void clear()
66+
{
67+
m_hasUpdate = m_done = m_aborted = m_camUpdated = 0;
68+
}
69+
};
70+
static
71+
void notifyUpdate( float x, void* userData )
72+
{
73+
Update* demo = (Update*)userData;
74+
demo->m_hasUpdate = 1;
75+
demo->m_progress = x;
76+
}
77+
};
78+
79+
4580
GLuint g_vertex_buffer_id = NULL;
4681
GLuint g_index_buffer_id = NULL;
4782
GLuint g_texture = NULL;
@@ -61,11 +96,67 @@ float g_camera_posX = 0.0;
6196
float g_camera_posY = 5.0;
6297
int g_lastMouseDownUpdateX = -1;
6398
int g_lastMouseDownUpdateY = -1;
99+
GuiRenderImpl::Update g_update;
100+
101+
102+
// thread rendering 1 iteration
103+
void renderJob( rpr_context ctxt, GuiRenderImpl::Update* update )
104+
{
105+
CHECK( rprContextRender( ctxt ) );
106+
update->m_done = 1;
107+
return;
108+
}
109+
110+
64111

65112
void Update()
66113
{
67-
// Send update event
68-
glutPostRedisplay();
114+
// clear state
115+
g_update.clear();
116+
117+
// start the rendering thread
118+
std::thread t( &renderJob, g_context, &g_update );
119+
120+
// wait the rendering thread
121+
while( !g_update.m_done )
122+
{
123+
// at each update of the rendering thread
124+
if( g_update.m_hasUpdate )
125+
{
126+
// Read the frame buffer from RPR
127+
// Note that rprContextResolveFrameBuffer and rprFrameBufferGetInfo(fb,RPR_FRAMEBUFFER_DATA) can be called asynchronous while rprContextRender is running.
128+
129+
CHECK( rprContextResolveFrameBuffer( g_context, g_frame_buffer, g_frame_buffer_2, false ) );
130+
size_t frame_buffer_dataSize = 0;
131+
CHECK( rprFrameBufferGetInfo( g_frame_buffer_2, RPR_FRAMEBUFFER_DATA, 0 , NULL , &frame_buffer_dataSize ) );
132+
133+
// check that the size fits with original buffer alloc
134+
if ( frame_buffer_dataSize != WINDOW_WIDTH * WINDOW_HEIGHT * 4 * sizeof(float) )
135+
{
136+
CHECK(RPR_ERROR_INTERNAL_ERROR)
137+
}
138+
139+
CHECK( rprFrameBufferGetInfo( g_frame_buffer_2, RPR_FRAMEBUFFER_DATA, frame_buffer_dataSize , g_fbdata , NULL ) );
140+
141+
// update the OpenGL texture with the new image from RPR
142+
glBindTexture(GL_TEXTURE_2D, g_texture);
143+
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT, GL_RGBA, GL_FLOAT, static_cast<const GLvoid*>(g_fbdata));
144+
glBindTexture(GL_TEXTURE_2D, 0);
145+
146+
// clear the update flag
147+
g_update.m_hasUpdate = false;
148+
}
149+
150+
// Request a new Display call.
151+
glutPostRedisplay();
152+
153+
}
154+
155+
// wait the end of the rendering thread
156+
t.join();
157+
158+
159+
return;
69160
}
70161

71162
void MoveCamera()
@@ -143,9 +234,6 @@ void OnKeyboardEvent(unsigned char key, int xmouse, int ymouse)
143234

144235
void Display()
145236
{
146-
// Render FR image into the GL texture
147-
rprContextRender(g_context);
148-
rprContextResolveFrameBuffer(g_context, g_frame_buffer, g_frame_buffer_2, true);
149237

150238
// Clear backbuffer
151239
glClear(GL_COLOR_BUFFER_BIT);
@@ -166,16 +254,7 @@ void Display()
166254

167255
glActiveTexture(GL_TEXTURE0);
168256
glBindTexture(GL_TEXTURE_2D, g_texture);
169-
170-
171-
172-
CHECK(rprFrameBufferGetInfo(g_frame_buffer_2, RPR_FRAMEBUFFER_DATA, WINDOW_WIDTH*WINDOW_HEIGHT*sizeof(float)*4, g_fbdata, NULL));
173-
174-
//glBindTexture(GL_TEXTURE_2D, g_texture);
175-
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT, GL_RGBA, GL_FLOAT, static_cast<const GLvoid*>(g_fbdata));
176-
//glBindTexture(GL_TEXTURE_2D, 0);
177-
178-
257+
//glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT, GL_RGBA, GL_FLOAT, static_cast<const GLvoid*>(g_fbdata));
179258

180259
GLuint position_attr_id = glGetAttribLocation(program, "inPosition");
181260
GLuint texcoord_attr_id = glGetAttribLocation(program, "inTexcoord");
@@ -277,9 +356,9 @@ void OnExit()
277356

278357
int main(int argc, char** argv)
279358
{
280-
// enable firerender API trace
359+
// enable RPR API trace
281360
// set this before any RPR API calls
282-
// frContextSetParameter1u(0,RPR_CONTEXT_TRACING_ENABLED,1);
361+
// rprContextSetParameter1u(0,RPR_CONTEXT_TRACING_ENABLED,1);
283362

284363
//GL setup
285364
{
@@ -299,12 +378,11 @@ int main(int argc, char** argv)
299378
InitGraphics();
300379
}
301380

302-
std::cout << "FireRender SDK simple rendering tutorial.\n";
303-
// Indicates whether the last operation has suceeded or not
381+
std::cout << "RPR SDK simple rendering tutorial.\n";
382+
304383
rpr_int status = RPR_SUCCESS;
305-
// Create OpenCL context using a single GPU
306384

307-
// Register Tahoe ray tracing plugin.
385+
// Register the plugin.
308386
rpr_int tahoePluginID = rprRegisterPlugin(RPR_PLUGIN_FILE_NAME);
309387
CHECK_NE(tahoePluginID , -1)
310388
rpr_int plugins[] = { tahoePluginID };
@@ -411,13 +489,21 @@ int main(int argc, char** argv)
411489
// Set framebuffer for the context
412490
CHECK(rprContextSetAOV(g_context, RPR_AOV_COLOR, g_frame_buffer));
413491

414-
CHECK( rprContextSetParameterByKey1u(g_context,RPR_CONTEXT_PREVIEW, 1u ) );
492+
// this line can be added for faster RPR rendering.
493+
// the higher RPR_CONTEXT_PREVIEW, the faster RPR rendering, ( but more pixelated )
494+
//CHECK( rprContextSetParameterByKey1u(g_context,RPR_CONTEXT_PREVIEW, 1u ) );
415495

416496
// Set framebuffer for the context
417497
CHECK(rprContextSetAOV(g_context, RPR_AOV_COLOR, g_frame_buffer));
418498

419-
g_fbdata = new float[WINDOW_WIDTH * WINDOW_HEIGHT * 4];
499+
// Define the update callback.
500+
// During the rprContextRender execution, RPR will call it regularly
501+
// The 'CALLBACK_DATA' : 'g_update' is not used by RPR. it can be any data structure that the API user wants.
502+
CHECK(rprContextSetParameterByKeyPtr(g_context, RPR_CONTEXT_RENDER_UPDATE_CALLBACK_FUNC, (void*)GuiRenderImpl::notifyUpdate));
503+
CHECK(rprContextSetParameterByKeyPtr(g_context, RPR_CONTEXT_RENDER_UPDATE_CALLBACK_DATA, &g_update));
420504

505+
// allocate the data that will be used the read RPR framebuffer, and give it to OpenGL.
506+
g_fbdata = new float[WINDOW_WIDTH * WINDOW_HEIGHT * 4];
421507

422508
std::cout << "Press W or S to move the camera.\n";
423509

tutorials/32_gl_interop/premake4.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ project "32_gl_interop"
2323
if os.istarget("linux") then
2424
links {"glut"}
2525
links {"GL"}
26+
links {"pthread"}
2627
end
2728
if os.istarget("macosx") then
2829
linkoptions{"-framework OpenGL", "-framework GLUT"}

0 commit comments

Comments
 (0)