-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathmain.cpp
More file actions
152 lines (124 loc) · 4.34 KB
/
main.cpp
File metadata and controls
152 lines (124 loc) · 4.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#define GLEW_STATIC
#include <contrib/glew/include/glew/glew.h>
#include <contrib/glfw/include/GLFW/glfw3.h>
#include <Orochi/Orochi.h>
#include <iostream>
#include <assert.h>
inline void checkError( const oroError result )
{
if( result != oroSuccess )
{
const char* errorNamePtr = oroGetErrorName( result );
const std::string_view errorName{ ( errorNamePtr != nullptr ) ? errorNamePtr : "NoErrorName" };
std::cerr << "oroError '" << errorName << "': ";
const char* errorDescriptionPtr = nullptr;
oroGetErrorString( result, &errorDescriptionPtr );
const std::string_view errorDescription{ ( errorDescriptionPtr != nullptr ) ? errorDescriptionPtr : "No description." };
std::cerr << errorDescription << '\n' << std::flush;
std::abort();
}
}
inline void checkError( const orortcResult result )
{
if( result != ORORTC_SUCCESS )
{
const std::string_view errorDescription = orortcGetErrorString( result );
std::cerr << "orortcError: " << errorDescription << '\n' << std::flush;
std::abort();
}
}
#define ERROR_CHECK( e ) checkError( e )
int main( int argc, char** argv )
{
GLFWwindow* window;
if( !glfwInit() ) return 0;
glfwWindowHint( GLFW_RED_BITS, 32 );
glfwWindowHint( GLFW_GREEN_BITS, 32 );
glfwWindowHint( GLFW_BLUE_BITS, 32 );
glfwWindowHint( GLFW_RESIZABLE, GL_FALSE );
window = glfwCreateWindow( 1280, 720, "orochiOglInterop", NULL, NULL );
if( !window )
{
glfwTerminate();
return 0;
}
glfwMakeContextCurrent( window );
if( glewInit() != GLEW_OK )
{
glfwTerminate();
return 0;
}
oroDevice m_device = 0;
oroCtx m_ctx = nullptr;
oroStream m_stream = nullptr;
{
const int deviceIndex = 0;
oroApi api = (oroApi)( ORO_API_CUDA | ORO_API_HIP );
int a = oroInitialize( api, 0 );
assert( a == 0 );
ERROR_CHECK( oroInit( 0 ) );
ERROR_CHECK( oroDeviceGet( &m_device, deviceIndex ) );
ERROR_CHECK( oroCtxCreate( &m_ctx, 0, m_device ) );
ERROR_CHECK( oroCtxSetCurrent( m_ctx ) );
ERROR_CHECK( oroStreamCreate( &m_stream ) );
}
{
const uint32_t imageSize = 64u;
GLuint texture = 0;
oroGraphicsResource_t oroResource = nullptr;
oroArray_t interopArray = nullptr;
oroTextureObject_t interopTexture = nullptr;
// Work around for AMD driver crash when calling `oroGLRegister*` functions
const bool isAmd = oroGetCurAPI( 0 ) == ORO_API_HIP;
if( isAmd )
{
uint32_t deviceCount = 16;
int glDevices[16];
ERROR_CHECK( hipGLGetDevices( &deviceCount, glDevices, deviceCount, hipGLDeviceListAll ) );
}
// Create texture object
glGenTextures( 1, &texture );
glBindTexture( GL_TEXTURE_2D, texture );
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA8, imageSize, imageSize, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0 );
glBindTexture( GL_TEXTURE_2D, 0 );
// Register framebuffer attachment that will be passed to Oro
{
ERROR_CHECK( oroGraphicsGLRegisterImage( &oroResource, texture, GL_TEXTURE_2D, oroGraphicsRegisterFlagsReadOnly ) );
oroDeviceSynchronize();
ERROR_CHECK( oroGetLastError() );
// Map buffer objects to get device pointers
ERROR_CHECK( oroGraphicsMapResources( 1, &oroResource, 0 ) );
oroDeviceSynchronize();
ERROR_CHECK( oroGetLastError() );
ERROR_CHECK( oroGraphicsSubResourceGetMappedArray( &interopArray, oroResource, 0, 0 ) );
oroDeviceSynchronize();
ERROR_CHECK( oroGetLastError() );
// Create texture interop object to be passed to kernel
{
oroChannelFormatDesc desc;
ERROR_CHECK( oroGetChannelDesc( &desc, interopArray ) );
oroDeviceSynchronize();
ERROR_CHECK( oroGetLastError() );
oroResourceDesc texRes;
memset( &texRes, 0, sizeof( oroResourceDesc ) );
texRes.resType = oroResourceTypeArray;
texRes.res.array.array = interopArray;
oroTextureDesc texDescr;
memset( &texDescr, 0, sizeof( oroTextureDesc ) );
texDescr.normalizedCoords = false;
texDescr.filterMode = oroFilterModePoint;
texDescr.addressMode[0] = oroAddressModeWrap;
texDescr.readMode = oroReadModeElementType;
ERROR_CHECK( oroCreateTextureObject( &interopTexture, &texRes, &texDescr, NULL ) );
}
}
ERROR_CHECK( oroGraphicsUnmapResources( 1, &oroResource, 0 ) );
ERROR_CHECK( oroDestroyTextureObject( interopTexture ) );
ERROR_CHECK( oroGraphicsUnregisterResource( oroResource ) );
glDeleteTextures( 1, &texture );
}
ERROR_CHECK( oroStreamDestroy( m_stream ) );
ERROR_CHECK( oroCtxDestroy( m_ctx ) );
std::cout << "Success!\n";
return 0;
}