15 #include <pxr/imaging/hgiGL/texture.h>
20 : _renderParam(renderParam), _hgi(hgi)
27 _hgi->DestroyTexture(&texture_);
31 glDeleteBuffers(1, &gl_pbo_id_);
37 void HdCyclesDisplayDriver::gl_context_create()
41 hdc_ = GetDC(CreateWindowA(
"STATIC",
43 WS_OVERLAPPEDWINDOW | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
50 GetModuleHandle(
NULL),
53 int pixelFormat = GetPixelFormat(wglGetCurrentDC());
54 PIXELFORMATDESCRIPTOR pfd = {
sizeof(pfd)};
55 DescribePixelFormat((HDC)hdc_, pixelFormat,
sizeof(pfd), &pfd);
56 SetPixelFormat((HDC)hdc_, pixelFormat, &pfd);
58 TF_VERIFY(gl_context_ = wglCreateContext((HDC)hdc_));
59 TF_VERIFY(wglShareLists(wglGetCurrentContext(), (HGLRC)gl_context_));
67 if (glewInit() != GLEW_OK) {
71 glGenBuffers(1, &gl_pbo_id_);
75 bool HdCyclesDisplayDriver::gl_context_enable()
78 if (!hdc_ || !gl_context_) {
85 if (wglGetCurrentContext() ==
nullptr) {
86 if (!TF_VERIFY(wglMakeCurrent((HDC)hdc_, (HGLRC)gl_context_))) {
98 void HdCyclesDisplayDriver::gl_context_disable()
101 if (wglGetCurrentContext() == gl_context_) {
102 TF_VERIFY(wglMakeCurrent(
nullptr,
nullptr));
109 void HdCyclesDisplayDriver::gl_context_dispose()
113 TF_VERIFY(wglDeleteContext((HGLRC)gl_context_));
114 DestroyWindow(WindowFromDC((HDC)hdc_));
119 void HdCyclesDisplayDriver::next_tile_begin()
123 bool HdCyclesDisplayDriver::update_begin(
const Params &
params,
127 if (!gl_context_enable()) {
131 if (gl_render_sync_) {
132 glWaitSync((GLsync)gl_render_sync_, 0, GL_TIMEOUT_IGNORED);
135 if (pbo_size_.x !=
params.full_size.x || pbo_size_.y !=
params.full_size.y) {
136 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, gl_pbo_id_);
137 glBufferData(GL_PIXEL_UNPACK_BUFFER,
141 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
143 pbo_size_ =
params.full_size;
151 void HdCyclesDisplayDriver::update_end()
153 gl_upload_sync_ = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
156 gl_context_disable();
159 void HdCyclesDisplayDriver::flush()
163 if (gl_upload_sync_) {
164 glWaitSync((GLsync)gl_upload_sync_, 0, GL_TIMEOUT_IGNORED);
167 if (gl_render_sync_) {
168 glWaitSync((GLsync)gl_render_sync_, 0, GL_TIMEOUT_IGNORED);
171 gl_context_disable();
174 half4 *HdCyclesDisplayDriver::map_texture_buffer()
176 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, gl_pbo_id_);
178 const auto mapped_rgba_pixels =
static_cast<half4 *
>(
179 glMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_WRITE_ONLY));
181 if (need_clear_ && mapped_rgba_pixels) {
182 memset(mapped_rgba_pixels, 0,
sizeof(
half4) * pbo_size_.x * pbo_size_.y);
186 return mapped_rgba_pixels;
189 void HdCyclesDisplayDriver::unmap_texture_buffer()
191 glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER);
193 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
198 GraphicsInterop interop_dst;
199 interop_dst.buffer_width = pbo_size_.x;
200 interop_dst.buffer_height = pbo_size_.y;
201 interop_dst.opengl_pbo_id = gl_pbo_id_;
203 interop_dst.need_clear = need_clear_;
209 void HdCyclesDisplayDriver::graphics_interop_activate()
214 void HdCyclesDisplayDriver::graphics_interop_deactivate()
216 gl_context_disable();
219 void HdCyclesDisplayDriver::clear()
224 void HdCyclesDisplayDriver::draw(
const Params &
params)
229 (renderBuffer->GetWidth() !=
params.size.x || renderBuffer->GetHeight() !=
params.size.y)) {
233 if (!renderBuffer->IsResourceUsed()) {
240 TF_VERIFY(renderBuffer->GetFormat() == HdFormatFloat16Vec4);
244 const GfVec3i dimensions(
params.size.x,
params.size.y, 1);
245 if (!texture_ || texture_->GetDescriptor().dimensions != dimensions) {
247 _hgi->DestroyTexture(&texture_);
250 HgiTextureDesc texDesc;
252 texDesc.format = HgiFormatFloat16Vec4;
253 texDesc.type = HgiTextureType2D;
254 texDesc.dimensions = dimensions;
255 texDesc.sampleCount = HgiSampleCount1;
257 texture_ = _hgi->CreateTexture(texDesc);
259 renderBuffer->SetResource(VtValue(texture_));
262 HgiGLTexture *
const texture =
dynamic_cast<HgiGLTexture *
>(texture_.Get());
263 if (!texture || !need_update_ || pbo_size_.x !=
params.size.x || pbo_size_.y !=
params.size.y) {
267 if (gl_upload_sync_) {
268 glWaitSync((GLsync)gl_upload_sync_, 0, GL_TIMEOUT_IGNORED);
271 glBindTexture(GL_TEXTURE_2D, texture->GetTextureId());
272 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, gl_pbo_id_);
273 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, pbo_size_.x, pbo_size_.y, GL_RGBA, GL_HALF_FLOAT, 0);
274 glBindTexture(GL_TEXTURE_2D, 0);
275 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
277 gl_render_sync_ = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
280 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