Blender V5.3
hydra/display_driver.cpp
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2022 NVIDIA Corporation
2 * SPDX-FileCopyrightText: 2022 Blender Foundation
3 *
4 * SPDX-License-Identifier: Apache-2.0 */
5
6#ifdef _WIN32
7// Include first to avoid "NOGDI" definition set in Cycles headers
8# ifndef WIN32_LEAN_AND_MEAN
9# define WIN32_LEAN_AND_MEAN
10# endif
11# include <Windows.h>
12#endif
13
15#include "hydra/render_buffer.h"
16#include "hydra/session.h"
17
18#include <epoxy/gl.h>
19#include <pxr/imaging/hgiGL/texture.h>
20
22
24 : _renderParam(renderParam), _hgi(hgi)
25{
26}
27
29{
30 if (texture_) {
31 _hgi->DestroyTexture(&texture_);
32 }
33
34 if (gl_pbo_id_) {
35 glDeleteBuffers(1, &gl_pbo_id_);
36 }
37
38 gl_context_dispose();
39}
40
41void HdCyclesDisplayDriver::gl_context_create()
42{
43#ifdef _WIN32
44 if (!gl_context_) {
45 hdc_ = GetDC(CreateWindowA("STATIC",
46 "HdCycles",
47 WS_OVERLAPPEDWINDOW | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
48 0,
49 0,
50 64,
51 64,
52 nullptr,
53 nullptr,
54 GetModuleHandle(nullptr),
55 nullptr));
56
57 int pixelFormat = GetPixelFormat(wglGetCurrentDC());
58 PIXELFORMATDESCRIPTOR pfd = {sizeof(pfd)};
59 DescribePixelFormat((HDC)hdc_, pixelFormat, sizeof(pfd), &pfd);
60 SetPixelFormat((HDC)hdc_, pixelFormat, &pfd);
61
62 TF_VERIFY(gl_context_ = wglCreateContext((HDC)hdc_));
63 TF_VERIFY(wglShareLists(wglGetCurrentContext(), (HGLRC)gl_context_));
64 }
65 if (!gl_context_) {
66 return;
67 }
68#endif
69
70 if (!gl_pbo_id_) {
71 glGenBuffers(1, &gl_pbo_id_);
72 graphics_interop_buffer_.clear();
73 }
74}
75
76bool HdCyclesDisplayDriver::gl_context_enable()
77{
78#ifdef _WIN32
79 if (!hdc_ || !gl_context_) {
80 return false;
81 }
82
83 mutex_.lock();
84
85 // Do not change context if this is called in the main thread
86 if (wglGetCurrentContext() == nullptr) {
87 if (!TF_VERIFY(wglMakeCurrent((HDC)hdc_, (HGLRC)gl_context_))) {
88 mutex_.unlock();
89 return false;
90 }
91 }
92
93 return true;
94#else
95 return false;
96#endif
97}
98
99void HdCyclesDisplayDriver::gl_context_disable()
100{
101#ifdef _WIN32
102 if (wglGetCurrentContext() == gl_context_) {
103 TF_VERIFY(wglMakeCurrent(nullptr, nullptr));
104 }
105
106 mutex_.unlock();
107#endif
108}
109
110void HdCyclesDisplayDriver::gl_context_dispose()
111{
112#ifdef _WIN32
113 if (gl_context_) {
114 TF_VERIFY(wglDeleteContext((HGLRC)gl_context_));
115 DestroyWindow(WindowFromDC((HDC)hdc_));
116 }
117#endif
118}
119
120void HdCyclesDisplayDriver::next_tile_begin() {}
121
122bool HdCyclesDisplayDriver::update_begin(const Params &params,
123 int /*texture_width*/,
124 int /*texture_height*/)
125{
126 if (!gl_context_enable()) {
127 return false;
128 }
129
130 if (gl_render_sync_) {
131 glWaitSync((GLsync)gl_render_sync_, 0, GL_TIMEOUT_IGNORED);
132 }
133
134 if (pbo_size_.x != params.full_size.x || pbo_size_.y != params.full_size.y) {
135 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, gl_pbo_id_);
136 glBufferData(GL_PIXEL_UNPACK_BUFFER,
137 sizeof(half4) * params.full_size.x * params.full_size.y,
138 nullptr,
139 GL_DYNAMIC_DRAW);
140 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
141
142 pbo_size_ = params.full_size;
143 graphics_interop_buffer_.clear();
144 }
145
146 need_update_ = true;
147
148 return true;
149}
150
151void HdCyclesDisplayDriver::update_end()
152{
153 gl_upload_sync_ = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
154 glFlush();
155
156 gl_context_disable();
157}
158
159void HdCyclesDisplayDriver::flush()
160{
161 gl_context_enable();
162
163 if (gl_upload_sync_) {
164 glWaitSync((GLsync)gl_upload_sync_, 0, GL_TIMEOUT_IGNORED);
165 }
166
167 if (gl_render_sync_) {
168 glWaitSync((GLsync)gl_render_sync_, 0, GL_TIMEOUT_IGNORED);
169 }
170
171 gl_context_disable();
172}
173
174half4 *HdCyclesDisplayDriver::map_texture_buffer()
175{
176 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, gl_pbo_id_);
177
178 auto *const mapped_rgba_pixels = static_cast<half4 *>(
179 glMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_WRITE_ONLY));
180
181 if (need_zero_ && mapped_rgba_pixels) {
182 memset(mapped_rgba_pixels, 0, sizeof(half4) * pbo_size_.x * pbo_size_.y);
183 need_zero_ = false;
184 }
185
186 return mapped_rgba_pixels;
187}
188
189void HdCyclesDisplayDriver::unmap_texture_buffer()
190{
191 glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER);
192
193 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
194}
195
196GraphicsInteropDevice HdCyclesDisplayDriver::graphics_interop_get_device()
197{
198 GraphicsInteropDevice interop_device;
199 interop_device.type = GraphicsInteropDevice::OPENGL;
200 return interop_device;
201}
202
203void HdCyclesDisplayDriver::graphics_interop_update_buffer()
204{
205 if (graphics_interop_buffer_.is_empty()) {
206 graphics_interop_buffer_.assign(
207 GraphicsInteropDevice::OPENGL, gl_pbo_id_, pbo_size_.x * pbo_size_.y * sizeof(half4));
208 }
209
210 if (need_zero_) {
211 graphics_interop_buffer_.zero();
212 need_zero_ = false;
213 }
214}
215
216void HdCyclesDisplayDriver::graphics_interop_activate()
217{
218 gl_context_enable();
219}
220
221void HdCyclesDisplayDriver::graphics_interop_deactivate()
222{
223 gl_context_disable();
224}
225
226void HdCyclesDisplayDriver::zero()
227{
228 need_zero_ = true;
229}
230
231void HdCyclesDisplayDriver::draw(const Params &params)
232{
233 auto *const renderBuffer = static_cast<HdCyclesRenderBuffer *>(
234 _renderParam->GetDisplayAovBinding().renderBuffer);
235 if (!renderBuffer || // Ensure this render buffer matches the texture dimensions
236 (renderBuffer->GetWidth() != params.size.x || renderBuffer->GetHeight() != params.size.y))
237 {
238 return;
239 }
240
241 if (!renderBuffer->IsResourceUsed()) {
242 return;
243 }
244
245 gl_context_create();
246
247 // Cycles 'DisplayDriver' only supports 'half4' format
248 TF_VERIFY(renderBuffer->GetFormat() == HdFormatFloat16Vec4);
249
250 const thread_scoped_lock lock(mutex_);
251
252 const GfVec3i dimensions(params.size.x, params.size.y, 1);
253 if (!texture_ || texture_->GetDescriptor().dimensions != dimensions) {
254 if (texture_) {
255 _hgi->DestroyTexture(&texture_);
256 }
257
258 HgiTextureDesc texDesc;
259 texDesc.usage = 0;
260 texDesc.format = HgiFormatFloat16Vec4;
261 texDesc.type = HgiTextureType2D;
262 texDesc.dimensions = dimensions;
263 texDesc.sampleCount = HgiSampleCount1;
264
265 texture_ = _hgi->CreateTexture(texDesc);
266
267 renderBuffer->SetResource(VtValue(texture_));
268 }
269
270 HgiGLTexture *const texture = dynamic_cast<HgiGLTexture *>(texture_.Get());
271 if (!texture || !need_update_ || pbo_size_.x != params.size.x || pbo_size_.y != params.size.y) {
272 return;
273 }
274
275 if (gl_upload_sync_) {
276 glWaitSync((GLsync)gl_upload_sync_, 0, GL_TIMEOUT_IGNORED);
277 }
278
279 glBindTexture(GL_TEXTURE_2D, texture->GetTextureId());
280 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, gl_pbo_id_);
281 glTexSubImage2D(
282 GL_TEXTURE_2D, 0, 0, 0, pbo_size_.x, pbo_size_.y, GL_RGBA, GL_HALF_FLOAT, nullptr);
283 glBindTexture(GL_TEXTURE_2D, 0);
284 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
285
286 gl_render_sync_ = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
287 glFlush();
288
289 need_update_ = false;
290}
291
volatile int lock
HdCyclesDisplayDriver(HdCyclesSession *renderParam, Hgi *hgi)
TEX_TEMPLATE DataVec texture(T, FltCoord, float=0.0f) RET
VecBase< half, 4 > half4
#define HDCYCLES_NAMESPACE_CLOSE_SCOPE
Definition half.h:53
std::unique_lock< std::mutex > thread_scoped_lock
Definition thread.h:28