Skip to content
This repository was archived by the owner on Aug 7, 2023. It is now read-only.

[WIP]glslify support #68

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 57 additions & 2 deletions lib/linter-glsl.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ const os = require('os');
const fs = require('fs');
const path = require('path');
const which = require('which');
const glslify = require('glslify');

const sourceMap = require('source-map');
const convert = require('convert-source-map')

const char1glslRegex = /^(.*(?:\.|_))(v|g|f)(\.glsl)$/;
const char2glslRegex = /^(.*(?:\.|_))(vs|tc|te|gs|fs|cs)(\.glsl)$/;
Expand Down Expand Up @@ -57,14 +61,37 @@ const parseGlslValidatorResponse = (inputs, output) => new Promise((resolve) =>
let compileStarted = false;
const typeName = shader.type.name;

let consumer;
if (shader.sourcemaps) {
consumer = new sourceMap.SourceMapConsumer(shader.sourcemaps);
}

output.split(os.EOL).forEach((line) => {
if (line.endsWith(shader.name)) {
compileStarted = true;
} else if (compileStarted || inputs.length === 1) {
const match = new RegExp(compileRegex).exec(line);
if (match) {
const lineStart = parseInt(match[3], 10);
const colStart = parseInt(match[2], 10);
let lineStart = parseInt(match[3], 10);
let colStart = parseInt(match[2], 10);

if (consumer) {
// glslangValidator doesn't output the column number of the error,
// so we need to find the first column from the sourcemap.
const lineLength = shader.contents.split('\n')[lineStart - 1].length;
for (let i = 0; i < lineLength; i++) {
const errorPos = consumer.originalPositionFor({
line: lineStart,
column: colStart + i,
});
if (errorPos.line != null) {
lineStart = errorPos.line;
colStart = errorPos.column;
break;
}
}
}

const lineEnd = lineStart;
const colEnd = colStart;

Expand Down Expand Up @@ -183,6 +210,12 @@ export default {
default: false,
order: 2,
},
useGlslify: {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This probably should have a description on here describing what this is actually doing.

type: 'boolean',
default: false,
order: 3,
description: 'Transform codes by glslify before running glslangValidator. Enable if your codes depend on glslify.',
},
},

activate() {
Expand All @@ -201,6 +234,12 @@ export default {
}),
);

this.subscriptions.add(
atom.config.observe('linter-glsl.useGlslify', (useGlslify) => {
this.useGlslify = useGlslify;
}),
);

this.subscriptions.add(
atom.config.observe('linter-glsl.glslangValidatorPath', (glslangValidatorPath) => {
this.glslangValidatorPath = module.exports.config.glslangValidatorPath.default;
Expand Down Expand Up @@ -294,6 +333,22 @@ export default {
args = ['-l'];
}
}
if (this.useGlslify) {
filesToValidate.forEach((f) => {
const baseDir = path.dirname(f.fullFilename);
try {
f.originalContents = f.contents;
f.contents = glslify.compile(f.contents, { basedir: baseDir });

const sourcemapsComment = f.contents.match(/sourceMappingURL/);
if (sourcemapsComment) {
f.sourcemaps = convert.fromSource(f.contents).toObject();
}
} catch (e) {
console.error(e);
}
});
}
return helpers.tempFiles(
filesToValidate,
files => helpers.exec(command, args.concat(files), {
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
"atom-linter": "^10.0.0",
"atom-message-panel": "^1.2.4",
"atom-package-deps": "^4.3.1",
"convert-source-map": "^1.5.0",
"glslify": "https://github.com/fand/glslify#sourcemaps",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I really want to use a branch on a fork here, that's just led to outdated and unmaintained code in every previous case.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yea, this is just an experimental fork to get sourcemaps and disable minification.
I'm gonna replace it with glslify if it supports sourcemaps.

"source-map": "^0.5.6",
"which": "^1.2.12"
},
"devDependencies": {
Expand Down