BradLarson / GPUImage2

GPUImage 2 is a BSD-licensed Swift framework for GPU-accelerated video and image processing.
BSD 3-Clause "New" or "Revised" License
4.88k stars 611 forks source link

Crash while compile shaders for GaussianBlur #325

Open khorbushko opened 3 years ago

khorbushko commented 3 years ago

When create a simple effect like

    let testImage = UIImage(named:"cat.jpg")!
    let unsharpMask = UnsharpMask()
    unsharpMask.blurRadiusInPixels = 5
    unsharpMask.intensity = 2.5
    let filteredImage = testImage.filterWithOperation(unsharpMask)

within Xcode 12.4 and swift5 I got an fatalError with shader compilation error:

Compile log: ERROR: 0:2: 'float' : declaration must include a precision qualifier for type

ERROR: 0:3: 'float' : declaration must include a precision qualifier for type

ERROR: 0:5: 'vec2[7]' : declaration must include a precision qualifier for type

ERROR: 0:9: 'vec4' : declaration must include a precision qualifier for type

ERROR: 0:10: Use of undeclared identifier 'sum'

ERROR: 0:10: Use of undeclared identifier 'blurCoordinates'

ERROR: 0:11: Use of undeclared identifier 'sum'

ERROR: 0:11: Use of undeclared identifier 'blurCoordinates'

ERROR: 0:12: Use of undeclared identifier 'sum'

ERROR: 0:12: Use of undeclared identifier 'blurCoordinates'

ERROR: 0:13: Use of undeclared identifier 'sum'

ERROR: 0:13: Use of undeclared identifier 'blurCoordinates'

ERROR: 0:14: Use of undeclared identifier 'sum'

ERROR: 0:14: Use of undeclared identifier 'blurCoordinates'

ERROR: 0:15: Use of undeclared identifier 'sum'

ERROR: 0:15: Use of undeclared identifier 'blurCoordinates'

ERROR: 0:16: Use of undeclared identifier 'sum'

ERROR: 0:16: Use of undeclared identifier 'blurCoordinates'

ERROR: 0:17: Use of undeclared identifier 'sum'

Shader deallocated --> ShaderProgram.swift: deinit: 64

ERROR: GaussianBlur compilation failed with error: ShaderCompileError(compileLog: "Fragment shader compile error:")

Fatal error: Aborting execution.: file GPUImage/ShaderProgram.swift, line 253

2021-04-14 14:09:41.961013+0300 gpuImage2tests[94267:692420] Fatal error: Aborting execution.: file GPUImage/ShaderProgram.swift, line 253

looks like shaders needs to be modified a bit by adding required precision. I added highp for some values and looks like this solve the issue, but better if @BradLarson u can check this issue.

example of modified shader:

func vertexShaderForStandardGaussianBlurOfRadius(_ radius:UInt, sigma:Double) -> String {
    guard (radius > 0) else { return OneInputVertexShader }

    let numberOfBlurCoordinates = radius * 2 + 1
    var shaderString = "#version 310 es \n attribute highp vec4 position;\n attribute highp vec4 inputTextureCoordinate;\n \n uniform  highp float texelWidth;\n uniform highp float texelHeight;\n \n varying highp vec2 blurCoordinates[\(numberOfBlurCoordinates)];\n \n void main()\n {\n gl_Position = position;\n \n highp vec2 singleStepOffset = vec2(texelWidth, texelHeight);\n"
    for currentBlurCoordinateIndex in 0..<numberOfBlurCoordinates {
        let offsetFromCenter = Int(currentBlurCoordinateIndex) - Int(radius)
        if (offsetFromCenter < 0) {
            shaderString += "blurCoordinates[\(currentBlurCoordinateIndex)] = inputTextureCoordinate.xy - singleStepOffset * \(Float(-offsetFromCenter));\n"
        } else if (offsetFromCenter > 0) {
            shaderString += "blurCoordinates[\(currentBlurCoordinateIndex)] = inputTextureCoordinate.xy + singleStepOffset * \(Float(offsetFromCenter));\n"
        } else {
            shaderString += "blurCoordinates[\(currentBlurCoordinateIndex)] = inputTextureCoordinate.xy;\n"
        }
    }

    shaderString += "}\n"
    return shaderString
}