| 1 | package com.mojang.renderpearl.backend.opengl;
|
| 2 |
|
| 3 | import com.mojang.blaze3d.systems.RenderSystem;
|
| 4 | import com.mojang.blaze3d.systems.ScissorState;
|
| 5 | import com.mojang.logging.LogUtils;
|
| 6 | import com.mojang.renderpearl.api.GpuFormat;
|
| 7 | import com.mojang.renderpearl.api.buffers.GpuBuffer;
|
| 8 | import com.mojang.renderpearl.api.buffers.GpuBufferSlice;
|
| 9 | import com.mojang.renderpearl.api.buffers.TransientMemory;
|
| 10 | import com.mojang.renderpearl.api.commands.GpuFence;
|
| 11 | import com.mojang.renderpearl.api.commands.GpuQueryPool;
|
| 12 | import com.mojang.renderpearl.api.commands.RenderPass;
|
| 13 | import com.mojang.renderpearl.api.commands.RenderPassDescriptor;
|
| 14 | import com.mojang.renderpearl.api.pipeline.IndexType;
|
| 15 | import com.mojang.renderpearl.api.textures.GpuTexture;
|
| 16 | import com.mojang.renderpearl.api.textures.GpuTextureView;
|
| 17 | import com.mojang.renderpearl.backend.api.CommandEncoderBackend;
|
| 18 | import com.mojang.renderpearl.backend.api.RenderPassBackend;
|
| 19 | import com.mojang.renderpearl.util.TextureViewAndSampler;
|
| 20 | import com.mojang.renderpearl.util.UncheckedAutoCloseable;
|
| 21 | import java.nio.ByteBuffer;
|
| 22 | import java.nio.IntBuffer;
|
| 23 | import java.util.ArrayList;
|
| 24 | import java.util.Collections;
|
| 25 | import java.util.List;
|
| 26 | import java.util.Optional;
|
| 27 | import java.util.OptionalDouble;
|
| 28 | import net.fabricmc.api.EnvType;
|
| 29 | import net.fabricmc.api.Environment;
|
| 30 | import org.joml.Vector4fc;
|
| 31 | import org.jspecify.annotations.Nullable;
|
| 32 | import org.lwjgl.PointerBuffer;
|
| 33 | import org.lwjgl.opengl.ARBBaseInstance;
|
| 34 | import org.lwjgl.opengl.ARBDrawIndirect;
|
| 35 | import org.lwjgl.opengl.ARBMultiDrawIndirect;
|
| 36 | import org.lwjgl.opengl.GL33C;
|
| 37 | import org.lwjgl.system.MemoryUtil;
|
| 38 | import org.slf4j.Logger;
|
| 39 |
|
| 40 | @Environment(EnvType.CLIENT)
|
| 41 | class GlCommandEncoder implements CommandEncoderBackend, UncheckedAutoCloseable {
|
| 42 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 43 | public static final int MAX_SUBMITS_IN_FLIGHT = 2;
|
| 44 | private static final long NO_FENCE = 0L;
|
| 45 | private final GlDevice device;
|
| 46 | private final GlTransientMemory transientMemory;
|
| 47 | private final long[] fences = new long[2];
|
| 48 | private long currentSubmitIndex = 2L;
|
| 49 | private final int readFbo;
|
| 50 | private final int drawFbo;
|
| 51 | private @Nullable GlRenderPipeline lastPipeline;
|
| 52 | private @Nullable GlProgram lastProgram;
|
| 53 | private final List<@Nullable FrameBufferAttachment> renderPassColorTextures = new ArrayList<>();
|
| 54 |
|
| 55 | protected GlCommandEncoder(final GlDevice device) {
|
| 56 | this.device = device;
|
| 57 | this.transientMemory = device.getDeviceInfo().features().persistentMapping()
|
| 58 | ? new GlTransientMemory.PersistentMapping(device, this)
|
| 59 | : new GlTransientMemory.Fallback(device, this);
|
| 60 | this.readFbo = device.directStateAccess().createFrameBufferObject();
|
| 61 | this.drawFbo = device.directStateAccess().createFrameBufferObject();
|
| 62 | }
|
| 63 |
|
| 64 | @Override
|
| 65 | public void close() {
|
| 66 | this.transientMemory.close();
|
| 67 | }
|
| 68 |
|
| 69 | public long currentSubmitIndex() {
|
| 70 | return this.currentSubmitIndex;
|
| 71 | }
|
| 72 |
|
| 73 | public int currentSubmitSlot() {
|
| 74 | return (int)(this.currentSubmitIndex % 2L);
|
| 75 | }
|
| 76 |
|
| 77 | @Override
|
| 78 | public void submit() {
|
| 79 | this.fences[this.currentSubmitSlot()] = GL33C.glFenceSync(37143, 0);
|
| 80 | this.currentSubmitIndex++;
|
| 81 | if (!this.awaitSubmit(this.currentSubmitIndex - 2L, -1L)) {
|
| 82 | throw new IllegalStateException("Failed to wait for frame completion");
|
| 83 | }
|
| 84 |
|
| 85 | this.transientMemory.rotate();
|
| 86 | }
|
| 87 |
|
| 88 | public boolean awaitSubmit(final long index, final long timeoutNS) {
|
| 89 | if (this.currentSubmitIndex > index + 2L) {
|
| 90 | return true;
|
| 91 | }
|
| 92 |
|
| 93 | if (index == this.currentSubmitIndex) {
|
| 94 | if (timeoutNS == 0L) {
|
| 95 | return false;
|
| 96 | } else {
|
| 97 | throw new IllegalStateException("Cannot wait on a fence for the current submit");
|
| 98 | }
|
| 99 | } else {
|
| 100 | int submitSlot = (int)(index % 2L);
|
| 101 | long fence = this.fences[submitSlot];
|
| 102 | if (fence == 0L) {
|
| 103 | return true;
|
| 104 | }
|
| 105 |
|
| 106 | int result = GlStateManager._glClientWaitSync(fence, 1, timeoutNS);
|
| 107 | if (result == 37147) {
|
| 108 | return false;
|
| 109 | }
|
| 110 |
|
| 111 | if (result == 37149) {
|
| 112 | throw new IllegalStateException("Failed to complete GPU fence: " + GlStateManager._getError());
|
| 113 | }
|
| 114 |
|
| 115 | GL33C.glDeleteSync(this.fences[submitSlot]);
|
| 116 | this.fences[submitSlot] = 0L;
|
| 117 | return true;
|
| 118 | }
|
| 119 | }
|
| 120 |
|
| 121 | @Override
|
| 122 | public TransientMemory transientMemory() {
|
| 123 | return this.transientMemory;
|
| 124 | }
|
| 125 |
|
| 126 | @Override
|
| 127 | public RenderPassBackend createRenderPass(final RenderPassDescriptor descriptor) {
|
| 128 | this.device.debugLabels().pushDebugGroup(descriptor.label());
|
| 129 | List<RenderPassDescriptor.Attachment<Optional<Vector4fc>>> colorAttachments = descriptor.colorAttachments();
|
| 130 | this.renderPassColorTextures.clear();
|
| 131 |
|
| 132 | for (RenderPassDescriptor.Attachment<Optional<Vector4fc>> colorAttachment : colorAttachments) {
|
| 133 | this.renderPassColorTextures.add(colorAttachment != null ? (GlTextureView)colorAttachment.textureView() : null);
|
| 134 | }
|
| 135 |
|
| 136 | RenderPassDescriptor.Attachment<OptionalDouble> depthAttachment = descriptor.depthAttachment();
|
| 137 | int fbo = this.device
|
| 138 | .frameBufferCache()
|
| 139 | .getFbo(this.device.directStateAccess(), this.renderPassColorTextures, depthAttachment == null ? null : (GlTextureView)depthAttachment.textureView());
|
| 140 | GlStateManager._glBindFramebuffer(36160, fbo);
|
| 141 | int width = 0;
|
| 142 | int height = 0;
|
| 143 | if (!colorAttachments.isEmpty()) {
|
| 144 | for (RenderPassDescriptor.Attachment<Optional<Vector4fc>> colorAttachment : colorAttachments) {
|
| 145 | if (colorAttachment != null) {
|
| 146 | GpuTextureView colorTexture = colorAttachment.textureView();
|
| 147 | width = colorTexture.getWidth(0);
|
| 148 | height = colorTexture.getHeight(0);
|
| 149 | }
|
| 150 | }
|
| 151 | } else if (depthAttachment != null) {
|
| 152 | width = depthAttachment.textureView().getWidth(0);
|
| 153 | height = depthAttachment.textureView().getHeight(0);
|
| 154 | }
|
| 155 |
|
| 156 | RenderPass.RenderArea renderArea = descriptor.renderArea();
|
| 157 | GlStateManager._enableScissorTest();
|
| 158 | GlStateManager._scissorBox(renderArea.x(), renderArea.y(), renderArea.width(), renderArea.height());
|
| 159 |
|
| 160 | for (int i = 0; i < colorAttachments.size(); i++) {
|
| 161 | RenderPassDescriptor.Attachment<Optional<Vector4fc>> attachment = colorAttachments.get(i);
|
| 162 | if (attachment != null) {
|
| 163 | Optional<Vector4fc> clearValue = attachment.clearValue();
|
| 164 | if (clearValue.isPresent()) {
|
| 165 | GlStateManager._colorMask(i, 15);
|
| 166 | GlStateManager._clearBuffer(i, clearValue.get());
|
| 167 | }
|
| 168 | }
|
| 169 | }
|
| 170 |
|
| 171 | if (depthAttachment != null) {
|
| 172 | OptionalDouble clearValue = depthAttachment.clearValue();
|
| 173 | if (clearValue.isPresent()) {
|
| 174 | GlStateManager._depthMask(true);
|
| 175 | GlStateManager._clearBuffer(clearValue.getAsDouble());
|
| 176 | }
|
| 177 | }
|
| 178 |
|
| 179 | GlStateManager._viewport(0, 0, width, height);
|
| 180 | int[] drawBuffers = new int[this.renderPassColorTextures.size()];
|
| 181 |
|
| 182 | for (int i = 0; i < this.renderPassColorTextures.size(); i++) {
|
| 183 | if (colorAttachments.get(i) != null) {
|
| 184 | drawBuffers[i] = 36064 + i;
|
| 185 | } else {
|
| 186 | drawBuffers[i] = 0;
|
| 187 | }
|
| 188 | }
|
| 189 |
|
| 190 | GL33C.glDrawBuffers(drawBuffers);
|
| 191 | this.lastPipeline = null;
|
| 192 | ScissorState scissorState = new ScissorState();
|
| 193 | scissorState.enable(renderArea.x(), renderArea.y(), renderArea.width(), renderArea.height());
|
| 194 | return new GlRenderPass(this, this.device, this.renderPassColorTextures.size(), scissorState);
|
| 195 | }
|
| 196 |
|
| 197 | @Override
|
| 198 | public void clearColorTexture(final GpuTexture colorTexture, final Vector4fc clearColor) {
|
| 199 | GL33C.glClearColor(clearColor.x(), clearColor.y(), clearColor.z(), clearColor.w());
|
| 200 | GlStateManager._disableScissorTest();
|
| 201 | GlStateManager._colorMask(15);
|
| 202 |
|
| 203 | for (int i = 0; i < colorTexture.getMipLevels(); i++) {
|
| 204 | this.device.directStateAccess().bindFrameBufferTextures(this.drawFbo, ((GlTexture)colorTexture).id, 0, i, 36160);
|
| 205 | GlStateManager._clear(16384);
|
| 206 | }
|
| 207 |
|
| 208 | GlStateManager._glFramebufferTexture2D(36160, 36064, 3553, 0, 0);
|
| 209 | GlStateManager._glBindFramebuffer(36160, 0);
|
| 210 | }
|
| 211 |
|
| 212 | @Override
|
| 213 | public void clearColorAndDepthTextures(final GpuTexture colorTexture, final Vector4fc clearColor, final GpuTexture depthTexture, final double clearDepth) {
|
| 214 | GlStateManager._disableScissorTest();
|
| 215 | GL33C.glClearDepth(clearDepth);
|
| 216 | GL33C.glClearColor(clearColor.x(), clearColor.y(), clearColor.z(), clearColor.w());
|
| 217 | GlStateManager._depthMask(true);
|
| 218 | GlStateManager._colorMask(15);
|
| 219 |
|
| 220 | for (int i = 0; i < colorTexture.getMipLevels(); i++) {
|
| 221 | int fbo = this.device
|
| 222 | .frameBufferCache()
|
| 223 | .getFbo(this.device.directStateAccess(), Collections.singletonList((GlTexture)colorTexture), (GlTexture)depthTexture, i);
|
| 224 | GlStateManager._glBindFramebuffer(36160, fbo);
|
| 225 | GlStateManager._clear(16640);
|
| 226 | }
|
| 227 |
|
| 228 | GlStateManager._glBindFramebuffer(36160, 0);
|
| 229 | }
|
| 230 |
|
| 231 | @Override
|
| 232 | public void clearColorAndDepthTextures(
|
| 233 | final GpuTexture colorTexture,
|
| 234 | final Vector4fc clearColor,
|
| 235 | final GpuTexture depthTexture,
|
| 236 | final double clearDepth,
|
| 237 | final int regionX,
|
| 238 | final int regionY,
|
| 239 | final int regionWidth,
|
| 240 | final int regionHeight,
|
| 241 | final int mipLevel
|
| 242 | ) {
|
| 243 | GlStateManager._scissorBox(regionX, regionY, regionWidth, regionHeight);
|
| 244 | GlStateManager._enableScissorTest();
|
| 245 | GL33C.glClearDepth(clearDepth);
|
| 246 | GL33C.glClearColor(clearColor.x(), clearColor.y(), clearColor.z(), clearColor.w());
|
| 247 | GlStateManager._depthMask(true);
|
| 248 | GlStateManager._colorMask(15);
|
| 249 | int fbo = this.device
|
| 250 | .frameBufferCache()
|
| 251 | .getFbo(this.device.directStateAccess(), Collections.singletonList((GlTexture)colorTexture), (GlTexture)depthTexture, mipLevel);
|
| 252 | GlStateManager._glBindFramebuffer(36160, fbo);
|
| 253 | GlStateManager._clear(16640);
|
| 254 | GlStateManager._glBindFramebuffer(36160, 0);
|
| 255 | }
|
| 256 |
|
| 257 | @Override
|
| 258 | public void clearDepthTexture(final GpuTexture depthTexture, final double clearDepth) {
|
| 259 | GL33C.glClearDepth(clearDepth);
|
| 260 | GlStateManager._depthMask(true);
|
| 261 | GlStateManager._disableScissorTest();
|
| 262 |
|
| 263 | for (int i = 0; i < depthTexture.getMipLevels(); i++) {
|
| 264 | this.device.directStateAccess().bindFrameBufferTextures(this.drawFbo, 0, ((GlTexture)depthTexture).id, i, 36160);
|
| 265 | GL33C.glDrawBuffer(0);
|
| 266 | GlStateManager._clear(256);
|
| 267 | }
|
| 268 |
|
| 269 | GL33C.glDrawBuffer(36064);
|
| 270 | GlStateManager._glFramebufferTexture2D(36160, 36096, 3553, 0, 0);
|
| 271 | GlStateManager._glBindFramebuffer(36160, 0);
|
| 272 | }
|
| 273 |
|
| 274 | @Override
|
| 275 | public void writeToBuffer(final GpuBufferSlice slice, final ByteBuffer data) {
|
| 276 | GlBuffer buffer = (GlBuffer)slice.buffer();
|
| 277 | buffer.checkCanBeUsed();
|
| 278 | this.device.directStateAccess().bufferSubData(buffer.handle(), slice.offset(), data, buffer.usage());
|
| 279 | }
|
| 280 |
|
| 281 | @Override
|
| 282 | public void copyToBuffer(final GpuBufferSlice source, final GpuBufferSlice target) {
|
| 283 | GlBuffer sourceBuffer = (GlBuffer)source.buffer();
|
| 284 | GlBuffer targetBuffer = (GlBuffer)target.buffer();
|
| 285 | sourceBuffer.checkCanBeUsed();
|
| 286 | targetBuffer.checkCanBeUsed();
|
| 287 | this.device.directStateAccess().copyBufferSubData(sourceBuffer.handle(), targetBuffer.handle(), source.offset(), target.offset(), source.length());
|
| 288 | }
|
| 289 |
|
| 290 | @Override
|
| 291 | public void writeToTexture(
|
| 292 | final GpuTexture destination,
|
| 293 | final ByteBuffer source,
|
| 294 | final int mipLevel,
|
| 295 | final int depthOrLayer,
|
| 296 | final int destX,
|
| 297 | final int destY,
|
| 298 | final int width,
|
| 299 | final int height
|
| 300 | ) {
|
| 301 | int target;
|
| 302 | if ((destination.usage() & 16) != 0) {
|
| 303 | target = GlConst.CUBEMAP_TARGETS[depthOrLayer % 6];
|
| 304 | GL33C.glBindTexture(34067, ((GlTexture)destination).id);
|
| 305 | } else {
|
| 306 | target = 3553;
|
| 307 | GlStateManager._bindTexture(((GlTexture)destination).id);
|
| 308 | }
|
| 309 |
|
| 310 | GlStateManager._pixelStore(3314, width);
|
| 311 | GlStateManager._pixelStore(3316, 0);
|
| 312 | GlStateManager._pixelStore(3315, 0);
|
| 313 | GlStateManager._pixelStore(3317, destination.getFormat().byteAlignment());
|
| 314 | GlStateManager._texSubImage2D(
|
| 315 | target, mipLevel, destX, destY, width, height, GlConst.toGlExternalId(destination.getFormat()), GlConst.toGlType(destination.getFormat()), source
|
| 316 | );
|
| 317 | }
|
| 318 |
|
| 319 | @Override
|
| 320 | public void copyBufferToTexture(
|
| 321 | final GpuBufferSlice source,
|
| 322 | final int sourceX,
|
| 323 | final int sourceY,
|
| 324 | final int sourceWidth,
|
| 325 | final int sourceHeight,
|
| 326 | final GpuTexture destination,
|
| 327 | final int destinationX,
|
| 328 | final int destinationY,
|
| 329 | final int copyWidth,
|
| 330 | final int copyHeight,
|
| 331 | final int mipLevel,
|
| 332 | final int arrayLayer
|
| 333 | ) {
|
| 334 | int target;
|
| 335 | if ((destination.usage() & 16) != 0) {
|
| 336 | target = GlConst.CUBEMAP_TARGETS[arrayLayer % 6];
|
| 337 | GL33C.glBindTexture(34067, ((GlTexture)destination).id);
|
| 338 | } else {
|
| 339 | target = 3553;
|
| 340 | GlStateManager._bindTexture(((GlTexture)destination).id);
|
| 341 | }
|
| 342 |
|
| 343 | int texelSize = destination.getFormat().blockSize();
|
| 344 | long skipTexels = sourceX + (long)sourceY * sourceWidth;
|
| 345 | long skipBytes = skipTexels * texelSize;
|
| 346 | GlBuffer sourceGlBuffer = (GlBuffer)source.buffer();
|
| 347 | GlStateManager._glBindBuffer(35052, sourceGlBuffer.handle());
|
| 348 | GlStateManager._pixelStore(3314, sourceWidth);
|
| 349 | GlStateManager._pixelStore(32878, sourceHeight);
|
| 350 | GlStateManager._pixelStore(3316, 0);
|
| 351 | GlStateManager._pixelStore(3315, 0);
|
| 352 | GlStateManager._pixelStore(3317, destination.getFormat().byteAlignment());
|
| 353 | GlStateManager._texSubImage2D(
|
| 354 | target,
|
| 355 | mipLevel,
|
| 356 | destinationX,
|
| 357 | destinationY,
|
| 358 | copyWidth,
|
| 359 | copyHeight,
|
| 360 | GlConst.toGlExternalId(destination.getFormat()),
|
| 361 | GlConst.toGlType(destination.getFormat()),
|
| 362 | source.offset() + skipBytes
|
| 363 | );
|
| 364 | GlStateManager._glBindBuffer(35052, 0);
|
| 365 | }
|
| 366 |
|
| 367 | @Override
|
| 368 | public void copyTextureToBuffer(final GpuTexture source, final GpuBuffer destination, final long offset, final Runnable callback, final int mipLevel) {
|
| 369 | this.copyTextureToBuffer(source, destination, offset, callback, mipLevel, 0, 0, source.getWidth(mipLevel), source.getHeight(mipLevel));
|
| 370 | }
|
| 371 |
|
| 372 | @Override
|
| 373 | public void copyTextureToBuffer(
|
| 374 | final GpuTexture source,
|
| 375 | final GpuBuffer destination,
|
| 376 | final long offset,
|
| 377 | final Runnable callback,
|
| 378 | final int mipLevel,
|
| 379 | final int x,
|
| 380 | final int y,
|
| 381 | final int width,
|
| 382 | final int height
|
| 383 | ) {
|
| 384 | ((GlBuffer)destination).checkCanBeUsed();
|
| 385 | GlStateManager.clearGlErrors();
|
| 386 | boolean isDepth = source.getFormat().hasDepthAspect();
|
| 387 | int textureId = ((GlTexture)source).glId();
|
| 388 | this.device.directStateAccess().bindFrameBufferTextures(this.readFbo, !isDepth ? textureId : 0, isDepth ? textureId : 0, mipLevel, 36008);
|
| 389 | GlStateManager._glBindBuffer(35051, ((GlBuffer)destination).handle());
|
| 390 | GlStateManager._pixelStore(3333, source.getFormat().byteAlignment());
|
| 391 | GlStateManager._pixelStore(3330, width);
|
| 392 | if (isDepth) {
|
| 393 | GlStateManager._glReadBuffer(0);
|
| 394 | }
|
| 395 |
|
| 396 | GlStateManager._readPixels(x, y, width, height, GlConst.toGlExternalId(source.getFormat()), GlConst.toGlType(source.getFormat()), offset);
|
| 397 | RenderSystem.queueFencedTask(callback);
|
| 398 | GlStateManager._glFramebufferTexture2D(36008, isDepth ? 36096 : 36064, 3553, 0, mipLevel);
|
| 399 | GlStateManager._glBindFramebuffer(36008, 0);
|
| 400 | GlStateManager._glBindBuffer(35051, 0);
|
| 401 | int error = GlStateManager._getError();
|
| 402 | if (error != 0) {
|
| 403 | throw new IllegalStateException("Couldn't perform copyTobuffer for texture " + source.getLabel() + ": GL error " + error);
|
| 404 | }
|
| 405 | }
|
| 406 |
|
| 407 | @Override
|
| 408 | public void copyTextureToTexture(
|
| 409 | final GpuTexture source,
|
| 410 | final GpuTexture destination,
|
| 411 | final int mipLevel,
|
| 412 | final int destX,
|
| 413 | final int destY,
|
| 414 | final int sourceX,
|
| 415 | final int sourceY,
|
| 416 | final int width,
|
| 417 | final int height
|
| 418 | ) {
|
| 419 | GlStateManager.clearGlErrors();
|
| 420 | GlStateManager._disableScissorTest();
|
| 421 | boolean isDepth = source.getFormat().hasDepthAspect();
|
| 422 | int sourceId = ((GlTexture)source).glId();
|
| 423 | int destId = ((GlTexture)destination).glId();
|
| 424 | this.device.directStateAccess().bindFrameBufferTextures(this.readFbo, isDepth ? 0 : sourceId, isDepth ? sourceId : 0, mipLevel, 0);
|
| 425 | this.device.directStateAccess().bindFrameBufferTextures(this.drawFbo, isDepth ? 0 : destId, isDepth ? destId : 0, mipLevel, 0);
|
| 426 | this.device
|
| 427 | .directStateAccess()
|
| 428 | .blitFrameBuffers(this.readFbo, this.drawFbo, sourceX, sourceY, width, height, destX, destY, width, height, isDepth ? 256 : 16384, 9728);
|
| 429 | int error = GlStateManager._getError();
|
| 430 | if (error != 0) {
|
| 431 | throw new IllegalStateException("Couldn't perform copyToTexture for texture " + source.getLabel() + " to " + destination.getLabel() + ": GL error " + error);
|
| 432 | }
|
| 433 | }
|
| 434 |
|
| 435 | void presentTexture(final long windowHandle, final GpuTextureView textureView, final int swapchainWidth, final int swapchainHeight) {
|
| 436 | this.device.makeCurrent(windowHandle);
|
| 437 | int destY = Math.max(0, swapchainHeight - textureView.getHeight(0));
|
| 438 | int copyWidth = Math.min(swapchainWidth, textureView.getWidth(0));
|
| 439 | int copyHeight = Math.min(swapchainHeight, textureView.getHeight(0));
|
| 440 | GlStateManager._disableScissorTest();
|
| 441 | GlStateManager._viewport(0, 0, textureView.getWidth(0), textureView.getHeight(0));
|
| 442 | GlStateManager._depthMask(true);
|
| 443 | GlStateManager._colorMask(15);
|
| 444 | this.device.directStateAccess().bindFrameBufferTextures(this.drawFbo, ((GlTexture)textureView.texture()).glId(), 0, 0, 0);
|
| 445 | this.device.directStateAccess().blitFrameBuffers(this.drawFbo, 0, 0, 0, copyWidth, copyHeight, 0, destY, copyWidth, copyHeight + destY, 16384, 9728);
|
| 446 | }
|
| 447 |
|
| 448 | @Override
|
| 449 | public GpuFence createFence() {
|
| 450 | return new GlFence(this);
|
| 451 | }
|
| 452 |
|
| 453 | protected void executeDraw(
|
| 454 | final GlRenderPass renderPass,
|
| 455 | final int baseVertex,
|
| 456 | final int firstIndex,
|
| 457 | final int drawCount,
|
| 458 | final @Nullable IndexType indexType,
|
| 459 | final int instanceCount,
|
| 460 | final int firstInstance
|
| 461 | ) {
|
| 462 | this.setupDraw(renderPass);
|
| 463 | if (indexType != null) {
|
| 464 | if (firstInstance > 0) {
|
| 465 | ARBBaseInstance.glDrawElementsInstancedBaseVertexBaseInstance(
|
| 466 | renderPass.pipeline.primitiveTopology(), drawCount, GlConst.toGl(indexType), (long)firstIndex * indexType.bytes, instanceCount, baseVertex, firstInstance
|
| 467 | );
|
| 468 | } else {
|
| 469 | GL33C.glDrawElementsInstancedBaseVertex(
|
| 470 | renderPass.pipeline.primitiveTopology(), drawCount, GlConst.toGl(indexType), (long)firstIndex * indexType.bytes, instanceCount, baseVertex
|
| 471 | );
|
| 472 | }
|
| 473 | } else if (firstInstance > 0) {
|
| 474 | ARBBaseInstance.glDrawArraysInstancedBaseInstance(renderPass.pipeline.primitiveTopology(), baseVertex, drawCount, instanceCount, firstInstance);
|
| 475 | } else {
|
| 476 | GL33C.glDrawArraysInstanced(renderPass.pipeline.primitiveTopology(), baseVertex, drawCount, instanceCount);
|
| 477 | }
|
| 478 | }
|
| 479 |
|
| 480 | public void executeDraws(
|
| 481 | final GlRenderPass renderPass,
|
| 482 | final @Nullable IndexType indexType,
|
| 483 | final @Nullable PointerBuffer firstIndexOffsets,
|
| 484 | final IntBuffer indexCounts,
|
| 485 | final IntBuffer vertexOffsets,
|
| 486 | final int drawCount
|
| 487 | ) {
|
| 488 | this.setupDraw(renderPass);
|
| 489 | if (indexType == null) {
|
| 490 | GL33C.nglMultiDrawArrays(renderPass.pipeline.primitiveTopology(), MemoryUtil.memAddress(vertexOffsets), MemoryUtil.memAddress(indexCounts), drawCount);
|
| 491 | } else {
|
| 492 | assert firstIndexOffsets != null;
|
| 493 | GL33C.nglMultiDrawElementsBaseVertex(
|
| 494 | renderPass.pipeline.primitiveTopology(),
|
| 495 | MemoryUtil.memAddress(indexCounts),
|
| 496 | GlConst.toGl(indexType),
|
| 497 | MemoryUtil.memAddress(firstIndexOffsets),
|
| 498 | drawCount,
|
| 499 | MemoryUtil.memAddress(vertexOffsets)
|
| 500 | );
|
| 501 | }
|
| 502 | }
|
| 503 |
|
| 504 | protected void executeDrawIndirect(
|
| 505 | final GlRenderPass renderPass, final @Nullable IndexType indexType, final GlBuffer commands, final long offset, final int drawCount
|
| 506 | ) {
|
| 507 | this.setupDraw(renderPass);
|
| 508 | GlStateManager._glBindBuffer(36671, commands.handle());
|
| 509 | if (indexType == null) {
|
| 510 | if (drawCount > 1) {
|
| 511 | ARBMultiDrawIndirect.glMultiDrawArraysIndirect(renderPass.pipeline.primitiveTopology(), offset, drawCount, 0);
|
| 512 | } else {
|
| 513 | ARBDrawIndirect.glDrawArraysIndirect(renderPass.pipeline.primitiveTopology(), offset);
|
| 514 | }
|
| 515 | } else if (drawCount > 1) {
|
| 516 | ARBMultiDrawIndirect.glMultiDrawElementsIndirect(renderPass.pipeline.primitiveTopology(), GlConst.toGl(indexType), offset, drawCount, 0);
|
| 517 | } else {
|
| 518 | ARBDrawIndirect.glDrawElementsIndirect(renderPass.pipeline.primitiveTopology(), GlConst.toGl(indexType), offset);
|
| 519 | }
|
| 520 | }
|
| 521 |
|
| 522 | private void setupDraw(final GlRenderPass renderPass) {
|
| 523 | if (renderPass.vertexBufferDirty) {
|
| 524 | renderPass.pipeline.vertexArray().bind(renderPass.vertexBuffers);
|
| 525 | renderPass.vertexBufferDirty = false;
|
| 526 | }
|
| 527 |
|
| 528 | if (renderPass.indexBufferDirty) {
|
| 529 | renderPass.indexBufferDirty = false;
|
| 530 | GlStateManager._glBindBuffer(34963, ((GlBuffer)renderPass.indexBuffer).handle());
|
| 531 | }
|
| 532 |
|
| 533 | if (this.lastPipeline != renderPass.pipeline) {
|
| 534 | renderPass.pipeline.bind();
|
| 535 | this.lastPipeline = renderPass.pipeline;
|
| 536 | }
|
| 537 |
|
| 538 | GlProgram glProgram = renderPass.pipeline.program();
|
| 539 | if (renderPass.pushConstantsDirty) {
|
| 540 | renderPass.pushConstantsDirty = false;
|
| 541 | Uniform.Ubo pushConstantUBO = glProgram.pushConstant();
|
| 542 | if (pushConstantUBO != null) {
|
| 543 | assert renderPass.pushConstants != null;
|
| 544 | GL33C.glBindBufferRange(
|
| 545 | 35345,
|
| 546 | pushConstantUBO.blockBinding(),
|
| 547 | ((GlBuffer)renderPass.pushConstants.buffer()).handle(),
|
| 548 | renderPass.pushConstants.offset(),
|
| 549 | renderPass.pushConstants.length()
|
| 550 | );
|
| 551 | }
|
| 552 | }
|
| 553 |
|
| 554 | if (renderPass.anyUniformDirty) {
|
| 555 | renderPass.anyUniformDirty = false;
|
| 556 |
|
| 557 | for (int i = 0; i < renderPass.dirtyUniforms.size(); i++) {
|
| 558 | if (renderPass.dirtyUniforms.getBoolean(i)) {
|
| 559 | renderPass.dirtyUniforms.set(i, false);
|
| 560 | Uniform dirtyUniform = glProgram.getUniform(i);
|
| 561 | if (dirtyUniform != null) {
|
| 562 | switch (dirtyUniform) {
|
| 563 | case Uniform.Ubo(int blockBinding):
|
| 564 | GpuBufferSlice var26 = (GpuBufferSlice)renderPass.uniforms.get(i);
|
| 565 | GL33C.glBindBufferRange(35345, blockBinding, ((GlBuffer)var26.buffer()).handle(), var26.offset(), var26.length());
|
| 566 | break;
|
| 567 | case Uniform.Utb(int samplerIndex, GpuFormat format, int texture):
|
| 568 | GlStateManager._activeTexture(33984 + samplerIndex);
|
| 569 | GL33C.glBindTexture(35882, texture);
|
| 570 | GpuBufferSlice var30 = (GpuBufferSlice)renderPass.uniforms.get(i);
|
| 571 | GL33C.glTexBuffer(35882, GlConst.toGlInternalId(format), ((GlBuffer)var30.buffer()).handle());
|
| 572 | break;
|
| 573 | case Uniform.Sampler(int samplerIndex):
|
| 574 | TextureViewAndSampler viewAndSampler = (TextureViewAndSampler)renderPass.uniforms.get(i);
|
| 575 | if (viewAndSampler != null) {
|
| 576 | GlTextureView textureView = (GlTextureView)viewAndSampler.view();
|
| 577 | GlStateManager._activeTexture(33984 + samplerIndex);
|
| 578 | GlTexture texture = textureView.texture();
|
| 579 | int target;
|
| 580 | if ((texture.usage() & 16) != 0) {
|
| 581 | target = 34067;
|
| 582 | GL33C.glBindTexture(34067, texture.id);
|
| 583 | } else {
|
| 584 | target = 3553;
|
| 585 | GlStateManager._bindTexture(texture.id);
|
| 586 | }
|
| 587 |
|
| 588 | GL33C.glBindSampler(samplerIndex, ((GlSampler)viewAndSampler.sampler()).getId());
|
| 589 | GlStateManager._texParameter(target, 33084, textureView.baseMipLevel());
|
| 590 | GlStateManager._texParameter(target, 33085, textureView.baseMipLevel() + textureView.mipLevels() - 1);
|
| 591 | }
|
| 592 | break;
|
| 593 | default:
|
| 594 | throw new MatchException(null, null);
|
| 595 | }
|
| 596 | }
|
| 597 | }
|
| 598 | }
|
| 599 | }
|
| 600 |
|
| 601 | if (renderPass.scissorStateDirty) {
|
| 602 | renderPass.scissorStateDirty = false;
|
| 603 | if (renderPass.isScissorEnabled()) {
|
| 604 | GlStateManager._enableScissorTest();
|
| 605 | GlStateManager._scissorBox(renderPass.getScissorX(), renderPass.getScissorY(), renderPass.getScissorWidth(), renderPass.getScissorHeight());
|
| 606 | } else {
|
| 607 | GlStateManager._disableScissorTest();
|
| 608 | }
|
| 609 | }
|
| 610 | }
|
| 611 |
|
| 612 | @Override
|
| 613 | public void submitRenderPass() {
|
| 614 | GlStateManager._glBindFramebuffer(36160, 0);
|
| 615 | this.device.debugLabels().popDebugGroup();
|
| 616 | }
|
| 617 |
|
| 618 | @Override
|
| 619 | public void writeTimestamp(final GpuQueryPool pool, final int index) {
|
| 620 | ((GlQueryPool)pool).writeTimestamp(index);
|
| 621 | }
|
| 622 | }
|
| 623 |
|