|
| 1 | +/*****************************************************************************\ |
| 2 | +* |
| 3 | +* Module Name simple_render.cpp |
| 4 | +* Project Radeon ProRender rendering tutorial |
| 5 | +* |
| 6 | +* Description Radeon ProRender SDK tutorials |
| 7 | +* |
| 8 | +* Copyright 2011 - 2021 Advanced Micro Devices, Inc. |
| 9 | +* |
| 10 | +* All rights reserved. This notice is intended as a precaution against |
| 11 | +* inadvertent publication and does not imply publication or any waiver |
| 12 | +* of confidentiality. The year included in the foregoing notice is the |
| 13 | +* year of creation of the work. |
| 14 | +* |
| 15 | +\*****************************************************************************/ |
| 16 | +#include "RadeonProRender.h" |
| 17 | +#include "Math/mathutils.h" |
| 18 | +#include "../common/common.h" |
| 19 | +#include <cassert> |
| 20 | +#include <iostream> |
| 21 | + |
| 22 | +int main() |
| 23 | +{ |
| 24 | + // enable Radeon ProRender API trace |
| 25 | + // set this before any rpr API calls |
| 26 | + // rprContextSetParameterByKey1u(0,RPR_CONTEXT_TRACING_ENABLED,1); |
| 27 | + |
| 28 | + std::cout << "Radeon ProRender SDK simple rendering tutorial.\n"; |
| 29 | + // Indicates whether the last operation has suceeded or not |
| 30 | + rpr_int status = RPR_SUCCESS; |
| 31 | + // Create OpenCL context using a single GPU |
| 32 | + rpr_context context = NULL; |
| 33 | + |
| 34 | + // Register Tahoe ray tracing plugin. |
| 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 using a single GPU |
| 41 | + CHECK( rprCreateContext(RPR_API_VERSION, plugins, pluginCount, g_ContextCreationFlags, NULL, NULL, &context) ); |
| 42 | + |
| 43 | + // Set active plugin. |
| 44 | + CHECK( rprContextSetActivePlugin(context, plugins[0]) ); |
| 45 | + |
| 46 | + std::cout << "Context successfully created.\n"; |
| 47 | + |
| 48 | + rpr_material_system matsys = nullptr; |
| 49 | + CHECK( rprContextCreateMaterialSystem(context, 0, &matsys) ); |
| 50 | + |
| 51 | + // Create a scene |
| 52 | + rpr_scene scene = nullptr; |
| 53 | + CHECK( rprContextCreateScene(context, &scene) ); |
| 54 | + |
| 55 | + // Create camera |
| 56 | + rpr_camera camera = nullptr; |
| 57 | + { |
| 58 | + CHECK( rprContextCreateCamera(context, &camera) ); |
| 59 | + |
| 60 | + // Position camera in world space: |
| 61 | + CHECK( rprCameraLookAt(camera, 0, 5, 20, 0, 1, 0, 0, 1, 0) ); |
| 62 | + |
| 63 | + CHECK( rprCameraSetFocalLength(camera, 75.f) ); |
| 64 | + |
| 65 | + // Set camera for the scene |
| 66 | + CHECK( rprSceneSetCamera(scene, camera) ); |
| 67 | + } |
| 68 | + // Set scene to render for the context |
| 69 | + CHECK( rprContextSetScene(context, scene) ); |
| 70 | + |
| 71 | + // Create framebuffer to store rendering result |
| 72 | + rpr_framebuffer_desc desc = { 800 , 600 }; |
| 73 | + |
| 74 | + // 4 component 32-bit float value each |
| 75 | + rpr_framebuffer_format fmt = {4, RPR_COMPONENT_TYPE_FLOAT32}; |
| 76 | + rpr_framebuffer frame_buffer = nullptr; |
| 77 | + rpr_framebuffer frame_buffer_resolved = nullptr; |
| 78 | + CHECK( rprContextCreateFrameBuffer(context, fmt, &desc, &frame_buffer) ); |
| 79 | + CHECK( rprContextCreateFrameBuffer(context, fmt, &desc, &frame_buffer_resolved) ); |
| 80 | + |
| 81 | + // Clear framebuffer to black color |
| 82 | + CHECK( rprFrameBufferClear(frame_buffer) ); |
| 83 | + |
| 84 | + // Set framebuffer for the context |
| 85 | + CHECK( rprContextSetAOV(context, RPR_AOV_COLOR, frame_buffer) ); |
| 86 | + |
| 87 | + const float shiftX = 0.3f; |
| 88 | + const float shiftY = 0.3f; |
| 89 | + |
| 90 | + const int cube_NumberOfVertices = 24; |
| 91 | + const int numberOfBlurKeyTime = 2; |
| 92 | + |
| 93 | + // Cube geometry |
| 94 | + vertex cube_data_motionBlur[cube_NumberOfVertices*numberOfBlurKeyTime] = |
| 95 | + { |
| 96 | + // vertices at camera exposure = 0.0 |
| 97 | + { -1.0f, 1.0f, -1.0f, 0.f, 1.f, 0.f, 0.f, 0.f }, |
| 98 | + { 1.0f, 1.0f, -1.0f, 0.f, 1.f, 0.f, 0.f, 0.f }, |
| 99 | + { 1.0f, 1.0f, 1.0f , 0.f, 1.f, 0.f, 0.f, 0.f }, |
| 100 | + { -1.0f, 1.0f, 1.0f , 0.f, 1.f, 0.f, 0.f, 0.f}, |
| 101 | + { -1.0f, -1.0f, -1.0f , 0.f, -1.f, 0.f, 0.f, 0.f }, |
| 102 | + { 1.0f, -1.0f, -1.0f , 0.f, -1.f, 0.f, 0.f, 0.f }, |
| 103 | + { 1.0f, -1.0f, 1.0f , 0.f, -1.f, 0.f, 0.f, 0.f }, |
| 104 | + { -1.0f, -1.0f, 1.0f , 0.f, -1.f, 0.f, 0.f, 0.f }, |
| 105 | + { -1.0f, -1.0f, 1.0f , -1.f, 0.f, 0.f, 0.f, 0.f }, |
| 106 | + { -1.0f, -1.0f, -1.0f , -1.f, 0.f, 0.f, 0.f, 0.f }, |
| 107 | + { -1.0f, 1.0f, -1.0f , -1.f, 0.f, 0.f, 0.f, 0.f }, |
| 108 | + { -1.0f, 1.0f, 1.0f , -1.f, 0.f, 0.f, 0.f, 0.f }, |
| 109 | + { 1.0f, -1.0f, 1.0f , 1.f, 0.f, 0.f, 0.f, 0.f }, |
| 110 | + { 1.0f, -1.0f, -1.0f , 1.f, 0.f, 0.f, 0.f, 0.f }, |
| 111 | + { 1.0f, 1.0f, -1.0f , 1.f, 0.f, 0.f, 0.f, 0.f }, |
| 112 | + { 1.0f, 1.0f, 1.0f , 1.f, 0.f, 0.f, 0.f, 0.f }, |
| 113 | + { -1.0f, -1.0f, -1.0f , 0.f, 0.f, -1.f , 0.f, 0.f }, |
| 114 | + { 1.0f, -1.0f, -1.0f , 0.f, 0.f, -1.f , 0.f, 0.f }, |
| 115 | + { 1.0f, 1.0f, -1.0f , 0.f, 0.f, -1.f, 0.f, 0.f }, |
| 116 | + { -1.0f, 1.0f, -1.0f , 0.f, 0.f, -1.f, 0.f, 0.f }, |
| 117 | + { -1.0f, -1.0f, 1.0f , 0.f, 0.f, 1.f, 0.f, 0.f }, |
| 118 | + { 1.0f, -1.0f, 1.0f , 0.f, 0.f, 1.f, 0.f, 0.f }, |
| 119 | + { 1.0f, 1.0f, 1.0f , 0.f, 0.f, 1.f, 0.f, 0.f }, |
| 120 | + { -1.0f, 1.0f, 1.0f , 0.f, 0.f, 1.f, 0.f, 0.f }, |
| 121 | + |
| 122 | + // vertices at camera exposure = 1.0 : slightly deform the Cube |
| 123 | + { -1.0f+shiftX, 1.0f, -1.0f, 0.f, 1.f, 0.f, 0.f, 0.f }, |
| 124 | + { 1.0f+shiftX, 1.0f+shiftY, -1.0f, 0.f, 1.f, 0.f, 0.f, 0.f }, |
| 125 | + { 1.0f+shiftX, 1.0f+shiftY, 1.0f , 0.f, 1.f, 0.f, 0.f, 0.f }, |
| 126 | + { -1.0f+shiftX, 1.0f, 1.0f , 0.f, 1.f, 0.f, 0.f, 0.f}, |
| 127 | + { -1.0f, -1.0f, -1.0f , 0.f, -1.f, 0.f, 0.f, 0.f }, |
| 128 | + { 1.0f, -1.0f, -1.0f , 0.f, -1.f, 0.f, 0.f, 0.f }, |
| 129 | + { 1.0f, -1.0f, 1.0f , 0.f, -1.f, 0.f, 0.f, 0.f }, |
| 130 | + { -1.0f, -1.0f, 1.0f , 0.f, -1.f, 0.f, 0.f, 0.f }, |
| 131 | + { -1.0f, -1.0f, 1.0f , -1.f, 0.f, 0.f, 0.f, 0.f }, |
| 132 | + { -1.0f, -1.0f, -1.0f , -1.f, 0.f, 0.f, 0.f, 0.f }, |
| 133 | + { -1.0f+shiftX, 1.0f, -1.0f , -1.f, 0.f, 0.f, 0.f, 0.f }, |
| 134 | + { -1.0f+shiftX, 1.0f, 1.0f , -1.f, 0.f, 0.f, 0.f, 0.f }, |
| 135 | + { 1.0f, -1.0f, 1.0f , 1.f, 0.f, 0.f, 0.f, 0.f }, |
| 136 | + { 1.0f, -1.0f, -1.0f , 1.f, 0.f, 0.f, 0.f, 0.f }, |
| 137 | + { 1.0f+shiftX, 1.0f+shiftY, -1.0f , 1.f, 0.f, 0.f, 0.f, 0.f }, |
| 138 | + { 1.0f+shiftX, 1.0f+shiftY, 1.0f , 1.f, 0.f, 0.f, 0.f, 0.f }, |
| 139 | + { -1.0f, -1.0f, -1.0f , 0.f, 0.f, -1.f , 0.f, 0.f }, |
| 140 | + { 1.0f, -1.0f, -1.0f , 0.f, 0.f, -1.f , 0.f, 0.f }, |
| 141 | + { 1.0f+shiftX, 1.0f+shiftY, -1.0f , 0.f, 0.f, -1.f, 0.f, 0.f }, |
| 142 | + { -1.0f+shiftX, 1.0f, -1.0f , 0.f, 0.f, -1.f, 0.f, 0.f }, |
| 143 | + { -1.0f, -1.0f, 1.0f , 0.f, 0.f, 1.f, 0.f, 0.f }, |
| 144 | + { 1.0f, -1.0f, 1.0f , 0.f, 0.f, 1.f, 0.f, 0.f }, |
| 145 | + { 1.0f+shiftX, 1.0f+shiftY, 1.0f , 0.f, 0.f, 1.f, 0.f, 0.f }, |
| 146 | + { -1.0f+shiftX, 1.0f, 1.0f , 0.f, 0.f, 1.f, 0.f, 0.f }, |
| 147 | + |
| 148 | + }; |
| 149 | + |
| 150 | + |
| 151 | + // Create cube mesh |
| 152 | + rpr_shape cube = nullptr; |
| 153 | + { |
| 154 | + // 0-terminated list of mesh extra properties. (key+value) |
| 155 | + rpr_mesh_info mesh_properties[16]; |
| 156 | + |
| 157 | + // first key: specify that we want to use mesh deformation on this rpr_shape. |
| 158 | + mesh_properties[0] = (rpr_mesh_info)RPR_MESH_MOTION_DIMENSION; |
| 159 | + |
| 160 | + // 2 key times are used in cube_data_motionBlur ( exposure = 0.0 and exposure = 1.0 ) |
| 161 | + // More key times can be used if needed, for example, 3 key times would mean : exposure = 0.0; 0.5; 1.0 |
| 162 | + mesh_properties[1] = (rpr_mesh_info)numberOfBlurKeyTime; |
| 163 | + |
| 164 | + mesh_properties[2] = (rpr_mesh_info)0; // key=0 means end of mesh properties. |
| 165 | + |
| 166 | + const rpr_float* texcoords[] = { (rpr_float const*)((char*)&cube_data_motionBlur[0] + sizeof(rpr_float)*6) }; |
| 167 | + size_t num_texcoords[] = { cube_NumberOfVertices*numberOfBlurKeyTime }; |
| 168 | + rpr_int texcoord_stride[] = { sizeof(vertex) }; |
| 169 | + const rpr_int* texcoord_indices_[] = { indices }; |
| 170 | + rpr_int tidx_stride_[] = { sizeof(rpr_int) }; |
| 171 | + |
| 172 | + CHECK(rprContextCreateMeshEx2(context, |
| 173 | + (rpr_float const*)&cube_data_motionBlur[0], cube_NumberOfVertices*numberOfBlurKeyTime, sizeof(vertex), |
| 174 | + (rpr_float const*)((char*)&cube_data_motionBlur[0] + sizeof(rpr_float) * 3), cube_NumberOfVertices*numberOfBlurKeyTime, sizeof(vertex), |
| 175 | + nullptr,0,0, |
| 176 | + 1, // using 1 texture coord layer. |
| 177 | + texcoords, num_texcoords, texcoord_stride, |
| 178 | + (rpr_int const*)indices, sizeof(rpr_int), |
| 179 | + (rpr_int const*)indices, sizeof(rpr_int), |
| 180 | + texcoord_indices_,tidx_stride_, |
| 181 | + num_face_vertices, 12, mesh_properties, &cube)); |
| 182 | + |
| 183 | + // Add cube into the scene |
| 184 | + CHECK(rprSceneAttachShape(scene, cube)); |
| 185 | + |
| 186 | + // Create a transform: -2 unit along X axis and 1 unit up Y axis |
| 187 | + RadeonProRender::matrix m = RadeonProRender::translation(RadeonProRender::float3(-2, 1, 0)); |
| 188 | + |
| 189 | + // Set the transform |
| 190 | + CHECK(rprShapeSetTransform(cube, RPR_TRUE, &m.m00)); |
| 191 | + } |
| 192 | + |
| 193 | + // Create plane mesh |
| 194 | + rpr_shape plane = nullptr; |
| 195 | + { |
| 196 | + CHECK(rprContextCreateMesh(context, |
| 197 | + (rpr_float const*)&plane_data[0], 4, sizeof(vertex), |
| 198 | + (rpr_float const*)((char*)&plane_data[0] + sizeof(rpr_float) * 3), 4, sizeof(vertex), |
| 199 | + (rpr_float const*)((char*)&plane_data[0] + sizeof(rpr_float) * 6), 4, sizeof(vertex), |
| 200 | + (rpr_int const*)indices, sizeof(rpr_int), |
| 201 | + (rpr_int const*)indices, sizeof(rpr_int), |
| 202 | + (rpr_int const*)indices, sizeof(rpr_int), |
| 203 | + num_face_vertices, 2, &plane)); |
| 204 | + |
| 205 | + // Add plane into the scene |
| 206 | + CHECK(rprSceneAttachShape(scene, plane)); |
| 207 | + } |
| 208 | + |
| 209 | + // Create point light |
| 210 | + rpr_light light=nullptr; |
| 211 | + { |
| 212 | + CHECK(rprContextCreatePointLight(context, &light)); |
| 213 | + |
| 214 | + // Create a transform: move 5 units in X axis, 8 units up Y axis, -2 units in Z axis |
| 215 | + RadeonProRender::matrix lightm = RadeonProRender::translation(RadeonProRender::float3(0, 8, 2)); |
| 216 | + |
| 217 | + // Set transform for the light |
| 218 | + CHECK(rprLightSetTransform(light, RPR_TRUE, &lightm.m00)); |
| 219 | + |
| 220 | + // Set light radiant power in Watts |
| 221 | + CHECK(rprPointLightSetRadiantPower3f(light, 100, 100, 100)); |
| 222 | + |
| 223 | + // Attach the light to the scene |
| 224 | + CHECK(rprSceneAttachLight(scene, light)); |
| 225 | + } |
| 226 | + |
| 227 | + // set exposure, for motion blur |
| 228 | + CHECK(rprCameraSetExposure(camera,1.0)); |
| 229 | + |
| 230 | + // Progressively render an image |
| 231 | + CHECK(rprContextSetParameterByKey1u(context,RPR_CONTEXT_ITERATIONS,NUM_ITERATIONS)); |
| 232 | + CHECK( rprContextRender(context) ); |
| 233 | + CHECK(rprContextResolveFrameBuffer(context,frame_buffer,frame_buffer_resolved,true)); |
| 234 | + |
| 235 | + std::cout << "Rendering finished.\n"; |
| 236 | + |
| 237 | + // Save the result to file |
| 238 | + CHECK( rprFrameBufferSaveToFile(frame_buffer_resolved, "04.png") ); |
| 239 | + |
| 240 | + // Release the stuff we created |
| 241 | + CHECK(rprObjectDelete(matsys));matsys=nullptr; |
| 242 | + CHECK(rprObjectDelete(plane));plane=nullptr; |
| 243 | + CHECK(rprObjectDelete(cube));cube=nullptr; |
| 244 | + CHECK(rprObjectDelete(scene));scene=nullptr; |
| 245 | + CHECK(rprObjectDelete(camera));camera=nullptr; |
| 246 | + CHECK(rprObjectDelete(light));light=nullptr; |
| 247 | + CHECK(rprObjectDelete(frame_buffer));frame_buffer=nullptr; |
| 248 | + CHECK(rprObjectDelete(frame_buffer_resolved));frame_buffer_resolved=nullptr; |
| 249 | + CheckNoLeak(context); |
| 250 | + CHECK(rprObjectDelete(context));context=nullptr; // Always delete the RPR Context in last. |
| 251 | + return 0; |
| 252 | +} |
| 253 | + |
| 254 | + |
0 commit comments