| 1 | package net.minecraft.client.shader;
|
| 2 |
|
| 3 | import com.google.common.base.Charsets;
|
| 4 | import com.google.common.collect.Lists;
|
| 5 | import com.google.common.collect.Maps;
|
| 6 | import com.google.gson.JsonArray;
|
| 7 | import com.google.gson.JsonElement;
|
| 8 | import com.google.gson.JsonObject;
|
| 9 | import com.google.gson.JsonParser;
|
| 10 | import com.google.gson.JsonSyntaxException;
|
| 11 | import java.io.FileNotFoundException;
|
| 12 | import java.io.IOException;
|
| 13 | import java.io.InputStream;
|
| 14 | import java.util.List;
|
| 15 | import java.util.Map;
|
| 16 | import net.minecraft.client.renderer.texture.ITextureObject;
|
| 17 | import net.minecraft.client.renderer.texture.TextureManager;
|
| 18 | import net.minecraft.client.resources.IResource;
|
| 19 | import net.minecraft.client.resources.IResourceManager;
|
| 20 | import net.minecraft.client.util.JsonException;
|
| 21 | import net.minecraft.util.JsonUtils;
|
| 22 | import net.minecraft.util.ResourceLocation;
|
| 23 | import org.apache.commons.io.IOUtils;
|
| 24 | import org.lwjgl.opengl.GL11;
|
| 25 | import org.lwjgl.util.vector.Matrix4f;
|
| 26 |
|
| 27 | public class ShaderGroup
|
| 28 | {
|
| 29 | private Framebuffer mainFramebuffer;
|
| 30 | private IResourceManager resourceManager;
|
| 31 | private String shaderGroupName;
|
| 32 | private final List<Shader> listShaders = Lists.<Shader>newArrayList();
|
| 33 | private final Map<String, Framebuffer> mapFramebuffers = Maps.<String, Framebuffer>newHashMap();
|
| 34 | private final List<Framebuffer> listFramebuffers = Lists.<Framebuffer>newArrayList();
|
| 35 | private Matrix4f projectionMatrix;
|
| 36 | private int mainFramebufferWidth;
|
| 37 | private int mainFramebufferHeight;
|
| 38 | private float field_148036_j;
|
| 39 | private float field_148037_k;
|
| 40 |
|
| 41 | public ShaderGroup(TextureManager p_i1050_1_, IResourceManager p_i1050_2_, Framebuffer p_i1050_3_, ResourceLocation p_i1050_4_) throws JsonException, IOException, JsonSyntaxException
|
| 42 | {
|
| 43 | this.resourceManager = p_i1050_2_;
|
| 44 | this.mainFramebuffer = p_i1050_3_;
|
| 45 | this.field_148036_j = 0.0F;
|
| 46 | this.field_148037_k = 0.0F;
|
| 47 | this.mainFramebufferWidth = p_i1050_3_.framebufferWidth;
|
| 48 | this.mainFramebufferHeight = p_i1050_3_.framebufferHeight;
|
| 49 | this.shaderGroupName = p_i1050_4_.toString();
|
| 50 | this.resetProjectionMatrix();
|
| 51 | this.parseGroup(p_i1050_1_, p_i1050_4_);
|
| 52 | }
|
| 53 |
|
| 54 | public void parseGroup(TextureManager p_152765_1_, ResourceLocation p_152765_2_) throws JsonException, IOException, JsonSyntaxException
|
| 55 | {
|
| 56 | JsonParser jsonparser = new JsonParser();
|
| 57 | InputStream inputstream = null;
|
| 58 |
|
| 59 | try
|
| 60 | {
|
| 61 | IResource iresource = this.resourceManager.getResource(p_152765_2_);
|
| 62 | inputstream = iresource.getInputStream();
|
| 63 | JsonObject jsonobject = jsonparser.parse(IOUtils.toString(inputstream, Charsets.UTF_8)).getAsJsonObject();
|
| 64 |
|
| 65 | if (JsonUtils.isJsonArray(jsonobject, "targets"))
|
| 66 | {
|
| 67 | JsonArray jsonarray = jsonobject.getAsJsonArray("targets");
|
| 68 | int i = 0;
|
| 69 |
|
| 70 | for (JsonElement jsonelement : jsonarray)
|
| 71 | {
|
| 72 | try
|
| 73 | {
|
| 74 | this.initTarget(jsonelement);
|
| 75 | }
|
| 76 | catch (Exception exception1)
|
| 77 | {
|
| 78 | JsonException jsonexception1 = JsonException.func_151379_a(exception1);
|
| 79 | jsonexception1.func_151380_a("targets[" + i + "]");
|
| 80 | throw jsonexception1;
|
| 81 | }
|
| 82 |
|
| 83 | ++i;
|
| 84 | }
|
| 85 | }
|
| 86 |
|
| 87 | if (JsonUtils.isJsonArray(jsonobject, "passes"))
|
| 88 | {
|
| 89 | JsonArray jsonarray1 = jsonobject.getAsJsonArray("passes");
|
| 90 | int j = 0;
|
| 91 |
|
| 92 | for (JsonElement jsonelement1 : jsonarray1)
|
| 93 | {
|
| 94 | try
|
| 95 | {
|
| 96 | this.parsePass(p_152765_1_, jsonelement1);
|
| 97 | }
|
| 98 | catch (Exception exception)
|
| 99 | {
|
| 100 | JsonException jsonexception2 = JsonException.func_151379_a(exception);
|
| 101 | jsonexception2.func_151380_a("passes[" + j + "]");
|
| 102 | throw jsonexception2;
|
| 103 | }
|
| 104 |
|
| 105 | ++j;
|
| 106 | }
|
| 107 | }
|
| 108 | }
|
| 109 | catch (Exception exception2)
|
| 110 | {
|
| 111 | JsonException jsonexception = JsonException.func_151379_a(exception2);
|
| 112 | jsonexception.func_151381_b(p_152765_2_.getResourcePath());
|
| 113 | throw jsonexception;
|
| 114 | }
|
| 115 | finally
|
| 116 | {
|
| 117 | IOUtils.closeQuietly(inputstream);
|
| 118 | }
|
| 119 | }
|
| 120 |
|
| 121 | private void initTarget(JsonElement p_148027_1_) throws JsonException
|
| 122 | {
|
| 123 | if (JsonUtils.isString(p_148027_1_))
|
| 124 | {
|
| 125 | this.addFramebuffer(p_148027_1_.getAsString(), this.mainFramebufferWidth, this.mainFramebufferHeight);
|
| 126 | }
|
| 127 | else
|
| 128 | {
|
| 129 | JsonObject jsonobject = JsonUtils.getJsonObject(p_148027_1_, "target");
|
| 130 | String s = JsonUtils.getString(jsonobject, "name");
|
| 131 | int i = JsonUtils.getInt(jsonobject, "width", this.mainFramebufferWidth);
|
| 132 | int j = JsonUtils.getInt(jsonobject, "height", this.mainFramebufferHeight);
|
| 133 |
|
| 134 | if (this.mapFramebuffers.containsKey(s))
|
| 135 | {
|
| 136 | throw new JsonException(s + " is already defined");
|
| 137 | }
|
| 138 |
|
| 139 | this.addFramebuffer(s, i, j);
|
| 140 | }
|
| 141 | }
|
| 142 |
|
| 143 | private void parsePass(TextureManager p_152764_1_, JsonElement p_152764_2_) throws JsonException, IOException
|
| 144 | {
|
| 145 | JsonObject jsonobject = JsonUtils.getJsonObject(p_152764_2_, "pass");
|
| 146 | String s = JsonUtils.getString(jsonobject, "name");
|
| 147 | String s1 = JsonUtils.getString(jsonobject, "intarget");
|
| 148 | String s2 = JsonUtils.getString(jsonobject, "outtarget");
|
| 149 | Framebuffer framebuffer = this.getFramebuffer(s1);
|
| 150 | Framebuffer framebuffer1 = this.getFramebuffer(s2);
|
| 151 |
|
| 152 | if (framebuffer == null)
|
| 153 | {
|
| 154 | throw new JsonException("Input target \'" + s1 + "\' does not exist");
|
| 155 | }
|
| 156 | else if (framebuffer1 == null)
|
| 157 | {
|
| 158 | throw new JsonException("Output target \'" + s2 + "\' does not exist");
|
| 159 | }
|
| 160 | else
|
| 161 | {
|
| 162 | Shader shader = this.addShader(s, framebuffer, framebuffer1);
|
| 163 | JsonArray jsonarray = JsonUtils.getJsonArray(jsonobject, "auxtargets", (JsonArray)null);
|
| 164 |
|
| 165 | if (jsonarray != null)
|
| 166 | {
|
| 167 | int i = 0;
|
| 168 |
|
| 169 | for (JsonElement jsonelement : jsonarray)
|
| 170 | {
|
| 171 | try
|
| 172 | {
|
| 173 | JsonObject jsonobject1 = JsonUtils.getJsonObject(jsonelement, "auxtarget");
|
| 174 | String s4 = JsonUtils.getString(jsonobject1, "name");
|
| 175 | String s3 = JsonUtils.getString(jsonobject1, "id");
|
| 176 | Framebuffer framebuffer2 = this.getFramebuffer(s3);
|
| 177 |
|
| 178 | if (framebuffer2 == null)
|
| 179 | {
|
| 180 | ResourceLocation resourcelocation = new ResourceLocation("textures/effect/" + s3 + ".png");
|
| 181 |
|
| 182 | try
|
| 183 | {
|
| 184 | this.resourceManager.getResource(resourcelocation);
|
| 185 | }
|
| 186 | catch (FileNotFoundException var24)
|
| 187 | {
|
| 188 | throw new JsonException("Render target or texture \'" + s3 + "\' does not exist");
|
| 189 | }
|
| 190 |
|
| 191 | p_152764_1_.bindTexture(resourcelocation);
|
| 192 | ITextureObject itextureobject = p_152764_1_.getTexture(resourcelocation);
|
| 193 | int j = JsonUtils.getInt(jsonobject1, "width");
|
| 194 | int k = JsonUtils.getInt(jsonobject1, "height");
|
| 195 | boolean flag = JsonUtils.getBoolean(jsonobject1, "bilinear");
|
| 196 |
|
| 197 | if (flag)
|
| 198 | {
|
| 199 | GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
|
| 200 | GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
|
| 201 | }
|
| 202 | else
|
| 203 | {
|
| 204 | GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST);
|
| 205 | GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
|
| 206 | }
|
| 207 |
|
| 208 | shader.addAuxFramebuffer(s4, Integer.valueOf(itextureobject.getGlTextureId()), j, k);
|
| 209 | }
|
| 210 | else
|
| 211 | {
|
| 212 | shader.addAuxFramebuffer(s4, framebuffer2, framebuffer2.framebufferTextureWidth, framebuffer2.framebufferTextureHeight);
|
| 213 | }
|
| 214 | }
|
| 215 | catch (Exception exception1)
|
| 216 | {
|
| 217 | JsonException jsonexception = JsonException.func_151379_a(exception1);
|
| 218 | jsonexception.func_151380_a("auxtargets[" + i + "]");
|
| 219 | throw jsonexception;
|
| 220 | }
|
| 221 |
|
| 222 | ++i;
|
| 223 | }
|
| 224 | }
|
| 225 |
|
| 226 | JsonArray jsonarray1 = JsonUtils.getJsonArray(jsonobject, "uniforms", (JsonArray)null);
|
| 227 |
|
| 228 | if (jsonarray1 != null)
|
| 229 | {
|
| 230 | int l = 0;
|
| 231 |
|
| 232 | for (JsonElement jsonelement1 : jsonarray1)
|
| 233 | {
|
| 234 | try
|
| 235 | {
|
| 236 | this.initUniform(jsonelement1);
|
| 237 | }
|
| 238 | catch (Exception exception)
|
| 239 | {
|
| 240 | JsonException jsonexception1 = JsonException.func_151379_a(exception);
|
| 241 | jsonexception1.func_151380_a("uniforms[" + l + "]");
|
| 242 | throw jsonexception1;
|
| 243 | }
|
| 244 |
|
| 245 | ++l;
|
| 246 | }
|
| 247 | }
|
| 248 | }
|
| 249 | }
|
| 250 |
|
| 251 | private void initUniform(JsonElement p_148028_1_) throws JsonException
|
| 252 | {
|
| 253 | JsonObject jsonobject = JsonUtils.getJsonObject(p_148028_1_, "uniform");
|
| 254 | String s = JsonUtils.getString(jsonobject, "name");
|
| 255 | ShaderUniform shaderuniform = ((Shader)this.listShaders.get(this.listShaders.size() - 1)).getShaderManager().getShaderUniform(s);
|
| 256 |
|
| 257 | if (shaderuniform == null)
|
| 258 | {
|
| 259 | throw new JsonException("Uniform \'" + s + "\' does not exist");
|
| 260 | }
|
| 261 | else
|
| 262 | {
|
| 263 | float[] afloat = new float[4];
|
| 264 | int i = 0;
|
| 265 |
|
| 266 | for (JsonElement jsonelement : JsonUtils.getJsonArray(jsonobject, "values"))
|
| 267 | {
|
| 268 | try
|
| 269 | {
|
| 270 | afloat[i] = JsonUtils.getFloat(jsonelement, "value");
|
| 271 | }
|
| 272 | catch (Exception exception)
|
| 273 | {
|
| 274 | JsonException jsonexception = JsonException.func_151379_a(exception);
|
| 275 | jsonexception.func_151380_a("values[" + i + "]");
|
| 276 | throw jsonexception;
|
| 277 | }
|
| 278 |
|
| 279 | ++i;
|
| 280 | }
|
| 281 |
|
| 282 | switch (i)
|
| 283 | {
|
| 284 | case 0:
|
| 285 | default:
|
| 286 | break;
|
| 287 |
|
| 288 | case 1:
|
| 289 | shaderuniform.set(afloat[0]);
|
| 290 | break;
|
| 291 |
|
| 292 | case 2:
|
| 293 | shaderuniform.set(afloat[0], afloat[1]);
|
| 294 | break;
|
| 295 |
|
| 296 | case 3:
|
| 297 | shaderuniform.set(afloat[0], afloat[1], afloat[2]);
|
| 298 | break;
|
| 299 |
|
| 300 | case 4:
|
| 301 | shaderuniform.set(afloat[0], afloat[1], afloat[2], afloat[3]);
|
| 302 | }
|
| 303 | }
|
| 304 | }
|
| 305 |
|
| 306 | public Framebuffer getFramebufferRaw(String p_177066_1_)
|
| 307 | {
|
| 308 | return (Framebuffer)this.mapFramebuffers.get(p_177066_1_);
|
| 309 | }
|
| 310 |
|
| 311 | public void addFramebuffer(String p_148020_1_, int p_148020_2_, int p_148020_3_)
|
| 312 | {
|
| 313 | Framebuffer framebuffer = new Framebuffer(p_148020_2_, p_148020_3_, true);
|
| 314 | framebuffer.setFramebufferColor(0.0F, 0.0F, 0.0F, 0.0F);
|
| 315 | this.mapFramebuffers.put(p_148020_1_, framebuffer);
|
| 316 |
|
| 317 | if (p_148020_2_ == this.mainFramebufferWidth && p_148020_3_ == this.mainFramebufferHeight)
|
| 318 | {
|
| 319 | this.listFramebuffers.add(framebuffer);
|
| 320 | }
|
| 321 | }
|
| 322 |
|
| 323 | public void deleteShaderGroup()
|
| 324 | {
|
| 325 | for (Framebuffer framebuffer : this.mapFramebuffers.values())
|
| 326 | {
|
| 327 | framebuffer.deleteFramebuffer();
|
| 328 | }
|
| 329 |
|
| 330 | for (Shader shader : this.listShaders)
|
| 331 | {
|
| 332 | shader.deleteShader();
|
| 333 | }
|
| 334 |
|
| 335 | this.listShaders.clear();
|
| 336 | }
|
| 337 |
|
| 338 | public Shader addShader(String p_148023_1_, Framebuffer p_148023_2_, Framebuffer p_148023_3_) throws JsonException, IOException
|
| 339 | {
|
| 340 | Shader shader = new Shader(this.resourceManager, p_148023_1_, p_148023_2_, p_148023_3_);
|
| 341 | this.listShaders.add(this.listShaders.size(), shader);
|
| 342 | return shader;
|
| 343 | }
|
| 344 |
|
| 345 | private void resetProjectionMatrix()
|
| 346 | {
|
| 347 | this.projectionMatrix = new Matrix4f();
|
| 348 | this.projectionMatrix.setIdentity();
|
| 349 | this.projectionMatrix.m00 = 2.0F / (float)this.mainFramebuffer.framebufferTextureWidth;
|
| 350 | this.projectionMatrix.m11 = 2.0F / (float)(-this.mainFramebuffer.framebufferTextureHeight);
|
| 351 | this.projectionMatrix.m22 = -0.0020001999F;
|
| 352 | this.projectionMatrix.m33 = 1.0F;
|
| 353 | this.projectionMatrix.m03 = -1.0F;
|
| 354 | this.projectionMatrix.m13 = 1.0F;
|
| 355 | this.projectionMatrix.m23 = -1.0001999F;
|
| 356 | }
|
| 357 |
|
| 358 | public void createBindFramebuffers(int width, int height)
|
| 359 | {
|
| 360 | this.mainFramebufferWidth = this.mainFramebuffer.framebufferTextureWidth;
|
| 361 | this.mainFramebufferHeight = this.mainFramebuffer.framebufferTextureHeight;
|
| 362 | this.resetProjectionMatrix();
|
| 363 |
|
| 364 | for (Shader shader : this.listShaders)
|
| 365 | {
|
| 366 | shader.setProjectionMatrix(this.projectionMatrix);
|
| 367 | }
|
| 368 |
|
| 369 | for (Framebuffer framebuffer : this.listFramebuffers)
|
| 370 | {
|
| 371 | framebuffer.createBindFramebuffer(width, height);
|
| 372 | }
|
| 373 | }
|
| 374 |
|
| 375 | public void loadShaderGroup(float partialTicks)
|
| 376 | {
|
| 377 | if (partialTicks < this.field_148037_k)
|
| 378 | {
|
| 379 | this.field_148036_j += 1.0F - this.field_148037_k;
|
| 380 | this.field_148036_j += partialTicks;
|
| 381 | }
|
| 382 | else
|
| 383 | {
|
| 384 | this.field_148036_j += partialTicks - this.field_148037_k;
|
| 385 | }
|
| 386 |
|
| 387 | for (this.field_148037_k = partialTicks; this.field_148036_j > 20.0F; this.field_148036_j -= 20.0F)
|
| 388 | {
|
| 389 | ;
|
| 390 | }
|
| 391 |
|
| 392 | for (Shader shader : this.listShaders)
|
| 393 | {
|
| 394 | shader.loadShader(this.field_148036_j / 20.0F);
|
| 395 | }
|
| 396 | }
|
| 397 |
|
| 398 | public final String getShaderGroupName()
|
| 399 | {
|
| 400 | return this.shaderGroupName;
|
| 401 | }
|
| 402 |
|
| 403 | private Framebuffer getFramebuffer(String p_148017_1_)
|
| 404 | {
|
| 405 | return p_148017_1_ == null ? null : (p_148017_1_.equals("minecraft:main") ? this.mainFramebuffer : (Framebuffer)this.mapFramebuffers.get(p_148017_1_));
|
| 406 | }
|
| 407 | }
|
| 408 |
|