How to apply shader to layer?

Is there any way to apply a blur shader to a layer so that all its children are blurred. Im using this code atm, but it’s very slow and applying the shader directly to the layer doesn’t work. So I would like to know if there is an easier and more efficient way to achieve my goal.

void blurLayer(Layer* layer)
{
	RenderTexture* renderTex = RenderTexture::create(layer->getContentSize().width, layer->getContentSize().height);
	renderTex->begin();
	layer->visit();
	renderTex->end();

	Sprite* sprite = renderTex->getSprite();
	sprite->setAnchorPoint(Vec2(0, 0));

	GLchar* fragSource = (GLchar*)String::createWithContentsOfFile(
								FileUtils::getInstance()->fullPathForFilename("shaders/blur.fsh").c_str())->getCString();
	auto program = GLProgram::createWithByteArrays(ccPositionTextureColor_noMVP_vert, fragSource);

	auto glProgramState = GLProgramState::getOrCreateWithGLProgram(program);
	sprite->setGLProgramState(glProgramState);

	auto size = sprite->getTexture()->getContentSizeInPixels();
	sprite->getGLProgramState()->setUniformVec2("resolution", size);
	sprite->getGLProgramState()->setUniformFloat("blurRadius", 8);
	sprite->getGLProgramState()->setUniformFloat("sampleNum", 10.0f);

	layer->addChild(sprite, 1000);
}

I made a class to do just that, and it’s now that slow (it depends on the fillrate of your target device).
But I have an unresolved issue if using on a non-fullscreen layer, see here: Projection matrix not correctly reset

I tried yours, but its just not suitable for realtime

Well, I do use it on realtime for many shaders.
Maybe your shader is not optimized, or too demanding.

I don’t know :smiley: , I’m just using the blur shader from cpp-tests