11# ifndef WIN32_LEAN_AND_MEAN
12# define WIN32_LEAN_AND_MEAN
22#include <pxr/imaging/hgiGL/texture.h>
27 : _renderParam(renderParam), _hgi(hgi)
34 _hgi->DestroyTexture(&texture_);
38 glDeleteBuffers(1, &gl_pbo_id_);
44void HdCyclesDisplayDriver::gl_context_create()
48 hdc_ = GetDC(CreateWindowA(
"STATIC",
50 WS_OVERLAPPEDWINDOW | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
57 GetModuleHandle(
NULL),
60 int pixelFormat = GetPixelFormat(wglGetCurrentDC());
61 PIXELFORMATDESCRIPTOR pfd = {
sizeof(pfd)};
62 DescribePixelFormat((HDC)hdc_, pixelFormat,
sizeof(pfd), &pfd);
63 SetPixelFormat((HDC)hdc_, pixelFormat, &pfd);
65 TF_VERIFY(gl_context_ = wglCreateContext((HDC)hdc_));
66 TF_VERIFY(wglShareLists(wglGetCurrentContext(), (HGLRC)gl_context_));
74 glGenBuffers(1, &gl_pbo_id_);
78bool HdCyclesDisplayDriver::gl_context_enable()
81 if (!hdc_ || !gl_context_) {
88 if (wglGetCurrentContext() ==
nullptr) {
89 if (!TF_VERIFY(wglMakeCurrent((HDC)hdc_, (HGLRC)gl_context_))) {
101void HdCyclesDisplayDriver::gl_context_disable()
104 if (wglGetCurrentContext() == gl_context_) {
105 TF_VERIFY(wglMakeCurrent(
nullptr,
nullptr));
112void HdCyclesDisplayDriver::gl_context_dispose()
116 TF_VERIFY(wglDeleteContext((HGLRC)gl_context_));
117 DestroyWindow(WindowFromDC((HDC)hdc_));
122void HdCyclesDisplayDriver::next_tile_begin() {}
124bool HdCyclesDisplayDriver::update_begin(
const Params &
params,
128 if (!gl_context_enable()) {
132 if (gl_render_sync_) {
133 glWaitSync((GLsync)gl_render_sync_, 0, GL_TIMEOUT_IGNORED);
136 if (pbo_size_.x !=
params.full_size.x || pbo_size_.y !=
params.full_size.y) {
137 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, gl_pbo_id_);
138 glBufferData(GL_PIXEL_UNPACK_BUFFER,
142 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
144 pbo_size_ =
params.full_size;
152void HdCyclesDisplayDriver::update_end()
154 gl_upload_sync_ = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
157 gl_context_disable();
160void HdCyclesDisplayDriver::flush()
164 if (gl_upload_sync_) {
165 glWaitSync((GLsync)gl_upload_sync_, 0, GL_TIMEOUT_IGNORED);
168 if (gl_render_sync_) {
169 glWaitSync((GLsync)gl_render_sync_, 0, GL_TIMEOUT_IGNORED);
172 gl_context_disable();
175half4 *HdCyclesDisplayDriver::map_texture_buffer()
177 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, gl_pbo_id_);
179 const auto mapped_rgba_pixels =
static_cast<half4 *
>(
180 glMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_WRITE_ONLY));
182 if (need_clear_ && mapped_rgba_pixels) {
183 memset(mapped_rgba_pixels, 0,
sizeof(
half4) * pbo_size_.x * pbo_size_.y);
187 return mapped_rgba_pixels;
190void HdCyclesDisplayDriver::unmap_texture_buffer()
192 glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER);
194 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
199 GraphicsInterop interop_dst;
200 interop_dst.buffer_width = pbo_size_.x;
201 interop_dst.buffer_height = pbo_size_.y;
202 interop_dst.opengl_pbo_id = gl_pbo_id_;
204 interop_dst.need_clear = need_clear_;
210void HdCyclesDisplayDriver::graphics_interop_activate()
215void HdCyclesDisplayDriver::graphics_interop_deactivate()
217 gl_context_disable();
220void HdCyclesDisplayDriver::clear()
225void HdCyclesDisplayDriver::draw(
const Params &
params)
230 (renderBuffer->GetWidth() !=
params.size.x || renderBuffer->GetHeight() !=
params.size.y))
235 if (!renderBuffer->IsResourceUsed()) {
242 TF_VERIFY(renderBuffer->GetFormat() == HdFormatFloat16Vec4);
246 const GfVec3i dimensions(
params.size.x,
params.size.y, 1);
247 if (!texture_ || texture_->GetDescriptor().dimensions != dimensions) {
249 _hgi->DestroyTexture(&texture_);
252 HgiTextureDesc texDesc;
254 texDesc.format = HgiFormatFloat16Vec4;
255 texDesc.type = HgiTextureType2D;
256 texDesc.dimensions = dimensions;
257 texDesc.sampleCount = HgiSampleCount1;
259 texture_ = _hgi->CreateTexture(texDesc);
261 renderBuffer->SetResource(VtValue(texture_));
264 HgiGLTexture *
const texture =
dynamic_cast<HgiGLTexture *
>(texture_.Get());
265 if (!texture || !need_update_ || pbo_size_.x !=
params.size.x || pbo_size_.y !=
params.size.y) {
269 if (gl_upload_sync_) {
270 glWaitSync((GLsync)gl_upload_sync_, 0, GL_TIMEOUT_IGNORED);
273 glBindTexture(GL_TEXTURE_2D, texture->GetTextureId());
274 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, gl_pbo_id_);
275 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, pbo_size_.x, pbo_size_.y, GL_RGBA, GL_HALF_FLOAT, 0);
276 glBindTexture(GL_TEXTURE_2D, 0);
277 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
279 gl_render_sync_ = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
282 need_update_ =
false;
HdCyclesDisplayDriver(HdCyclesSession *renderParam, Hgi *hgi)
PXR_NS::HdRenderPassAovBinding GetDisplayAovBinding() const
#define HDCYCLES_NAMESPACE_CLOSE_SCOPE
std::unique_lock< std::mutex > thread_scoped_lock