| 1 | package com.mojang.renderpearl.backend.opengl;
|
| 2 |
|
| 3 | import com.mojang.renderpearl.api.pipeline.BlendFunction;
|
| 4 | import com.mojang.renderpearl.api.pipeline.ColorTargetState;
|
| 5 | import com.mojang.renderpearl.backend.api.BackendRenderPipeline;
|
| 6 | import java.util.Objects;
|
| 7 | import java.util.Optional;
|
| 8 | import net.fabricmc.api.EnvType;
|
| 9 | import net.fabricmc.api.Environment;
|
| 10 |
|
| 11 | @Environment(EnvType.CLIENT)
|
| 12 | public final class GlRenderPipeline implements BackendRenderPipeline {
|
| 13 | private final GlDevice device;
|
| 14 | private final GlProgram program;
|
| 15 | private final VertexArray vertexArray;
|
| 16 | private final int primitiveTopology;
|
| 17 | private final boolean cull;
|
| 18 | private final int polygonMode;
|
| 19 | private final boolean depthEnabled;
|
| 20 | private final int depthFunc;
|
| 21 | private final boolean writeDepth;
|
| 22 | private final float depthBiasConstant;
|
| 23 | private final float depthBiasScaleFactor;
|
| 24 | private final @ColorTargetState.WriteMask int[] writeMasks;
|
| 25 | private final boolean[] blendEnabled;
|
| 26 | private final int srcRgb;
|
| 27 | private final int dstRgb;
|
| 28 | private final int modeRgb;
|
| 29 | private final int srcAlpha;
|
| 30 | private final int dstAlpha;
|
| 31 | private final int modeAlpha;
|
| 32 | private boolean closed = false;
|
| 33 |
|
| 34 | GlRenderPipeline(final GlDevice device, final BackendRenderPipeline.CreateInfo createInfo, final GlProgram program, final VertexArray vertexArray) {
|
| 35 | this.device = device;
|
| 36 | this.program = program;
|
| 37 | this.vertexArray = vertexArray;
|
| 38 | this.primitiveTopology = GlConst.toGl(createInfo.primitiveTopology());
|
| 39 | this.cull = createInfo.cull();
|
| 40 | this.polygonMode = GlConst.toGl(createInfo.polygonMode());
|
| 41 | this.depthEnabled = createInfo.depthStencilState() != null;
|
| 42 | if (this.depthEnabled) {
|
| 43 | this.depthFunc = GlConst.toGl(createInfo.depthStencilState().depthTest());
|
| 44 | this.writeDepth = createInfo.depthStencilState().writeDepth();
|
| 45 | this.depthBiasConstant = createInfo.depthStencilState().depthBiasConstant();
|
| 46 | this.depthBiasScaleFactor = createInfo.depthStencilState().depthBiasScaleFactor();
|
| 47 | } else {
|
| 48 | this.depthFunc = 1280;
|
| 49 | this.writeDepth = false;
|
| 50 | this.depthBiasConstant = 0.0F;
|
| 51 | this.depthBiasScaleFactor = 0.0F;
|
| 52 | }
|
| 53 |
|
| 54 | this.writeMasks = new int[createInfo.colorTargetStates().size()];
|
| 55 | this.blendEnabled = new boolean[createInfo.colorTargetStates().size()];
|
| 56 |
|
| 57 | for (int i = 0; i < createInfo.colorTargetStates().size(); i++) {
|
| 58 | ColorTargetState state = createInfo.colorTargetStates().get(i);
|
| 59 | if (state != null) {
|
| 60 | this.writeMasks[i] = state.writeMask() & 15;
|
| 61 | this.blendEnabled[i] = state.blendFunction().isPresent();
|
| 62 | } else {
|
| 63 | this.writeMasks[i] = -1;
|
| 64 | this.blendEnabled[i] = false;
|
| 65 | }
|
| 66 | }
|
| 67 |
|
| 68 | Optional<BlendFunction> representativeBlendState = createInfo.colorTargetStates()
|
| 69 | .stream()
|
| 70 | .filter(Objects::nonNull)
|
| 71 | .map(ColorTargetState::blendFunction)
|
| 72 | .filter(Optional::isPresent)
|
| 73 | .map(Optional::get)
|
| 74 | .findFirst();
|
| 75 | if (representativeBlendState.isPresent()) {
|
| 76 | BlendFunction blendFunction = representativeBlendState.get();
|
| 77 | this.srcRgb = GlConst.toGl(blendFunction.color().sourceFactor());
|
| 78 | this.dstRgb = GlConst.toGl(blendFunction.color().destFactor());
|
| 79 | this.modeRgb = GlConst.toGl(blendFunction.color().op());
|
| 80 | this.srcAlpha = GlConst.toGl(blendFunction.alpha().sourceFactor());
|
| 81 | this.dstAlpha = GlConst.toGl(blendFunction.alpha().destFactor());
|
| 82 | this.modeAlpha = GlConst.toGl(blendFunction.alpha().op());
|
| 83 | } else {
|
| 84 | this.srcRgb = 1280;
|
| 85 | this.dstRgb = 1280;
|
| 86 | this.modeRgb = 1280;
|
| 87 | this.srcAlpha = 1280;
|
| 88 | this.dstAlpha = 1280;
|
| 89 | this.modeAlpha = 1280;
|
| 90 | }
|
| 91 | }
|
| 92 |
|
| 93 | @Override
|
| 94 | public boolean isClosed() {
|
| 95 | return this.closed;
|
| 96 | }
|
| 97 |
|
| 98 | @Override
|
| 99 | public void close() {
|
| 100 | if (!this.closed) {
|
| 101 | this.closed = true;
|
| 102 | this.program.close();
|
| 103 | this.vertexArray.close();
|
| 104 | this.device.markAmdShaderCompilerAngry();
|
| 105 | }
|
| 106 | }
|
| 107 |
|
| 108 | public GlProgram program() {
|
| 109 | return this.program;
|
| 110 | }
|
| 111 |
|
| 112 | public VertexArray vertexArray() {
|
| 113 | return this.vertexArray;
|
| 114 | }
|
| 115 |
|
| 116 | public void bind() {
|
| 117 | GlStateManager._glUseProgram(this.program.getProgramId());
|
| 118 | if (this.depthEnabled) {
|
| 119 | GlStateManager._enableDepthTest();
|
| 120 | GlStateManager._depthFunc(this.depthFunc);
|
| 121 | GlStateManager._depthMask(this.writeDepth);
|
| 122 | if (this.depthBiasConstant == 0.0F && this.depthBiasScaleFactor == 0.0F) {
|
| 123 | GlStateManager._disablePolygonOffset();
|
| 124 | } else {
|
| 125 | GlStateManager._polygonOffset(this.depthBiasScaleFactor, this.depthBiasConstant);
|
| 126 | GlStateManager._enablePolygonOffset();
|
| 127 | }
|
| 128 | } else {
|
| 129 | GlStateManager._disableDepthTest();
|
| 130 | GlStateManager._depthMask(false);
|
| 131 | GlStateManager._disablePolygonOffset();
|
| 132 | }
|
| 133 |
|
| 134 | if (this.cull) {
|
| 135 | GlStateManager._enableCull();
|
| 136 | } else {
|
| 137 | GlStateManager._disableCull();
|
| 138 | }
|
| 139 |
|
| 140 | for (int i = 0; i < this.writeMasks.length; i++) {
|
| 141 | if (this.writeMasks[i] != -1) {
|
| 142 | GlStateManager._colorMask(i, this.writeMasks[i]);
|
| 143 | } else {
|
| 144 | GlStateManager._colorMask(i, 0);
|
| 145 | }
|
| 146 |
|
| 147 | if (this.blendEnabled[i]) {
|
| 148 | GlStateManager._enableBlend(i);
|
| 149 | } else {
|
| 150 | GlStateManager._disableBlend(i);
|
| 151 | }
|
| 152 | }
|
| 153 |
|
| 154 | if (this.srcRgb != 1280) {
|
| 155 | GlStateManager._blendFuncSeparate(this.srcRgb, this.dstRgb, this.srcAlpha, this.dstAlpha);
|
| 156 | GlStateManager._blendEquationSeparate(this.modeRgb, this.modeAlpha);
|
| 157 | }
|
| 158 |
|
| 159 | GlStateManager._polygonMode(1032, this.polygonMode);
|
| 160 | }
|
| 161 |
|
| 162 | public int primitiveTopology() {
|
| 163 | return this.primitiveTopology;
|
| 164 | }
|
| 165 | }
|
| 166 |
|