diff options
Diffstat (limited to 'drivers')
149 files changed, 9063 insertions, 7697 deletions
diff --git a/drivers/SCsub b/drivers/SCsub index f9cfa3fb05..320d4dc4bb 100644 --- a/drivers/SCsub +++ b/drivers/SCsub @@ -4,9 +4,6 @@ Import('env') env.drivers_sources = [] -if 'builtin_zlib' in env and env['builtin_zlib']: - SConscript("zlib/SCsub") - # OS drivers SConscript('unix/SCsub') SConscript('windows/SCsub') diff --git a/drivers/alsa/SCsub b/drivers/alsa/SCsub index ee39fd2631..28b315ae66 100644 --- a/drivers/alsa/SCsub +++ b/drivers/alsa/SCsub @@ -3,5 +3,3 @@ Import('env') env.add_source_files(env.drivers_sources, "*.cpp") - -Export('env') diff --git a/drivers/alsa/audio_driver_alsa.cpp b/drivers/alsa/audio_driver_alsa.cpp index a44a11a46d..2e3861f500 100644 --- a/drivers/alsa/audio_driver_alsa.cpp +++ b/drivers/alsa/audio_driver_alsa.cpp @@ -32,8 +32,8 @@ #ifdef ALSA_ENABLED -#include "os/os.h" -#include "project_settings.h" +#include "core/os/os.h" +#include "core/project_settings.h" #include <errno.h> @@ -116,9 +116,7 @@ Error AudioDriverALSA::init_device() { status = snd_pcm_hw_params_set_period_size_near(pcm_handle, hwparams, &period_size, NULL); CHECK_FAIL(status < 0); - if (OS::get_singleton()->is_stdout_verbose()) { - print_line("audio buffer frames: " + itos(period_size) + " calculated latency: " + itos(period_size * 1000 / mix_rate) + "ms"); - } + print_verbose("Audio buffer frames: " + itos(period_size) + " calculated latency: " + itos(period_size * 1000 / mix_rate) + "ms"); status = snd_pcm_hw_params_set_periods_near(pcm_handle, hwparams, &periods, NULL); CHECK_FAIL(status < 0); @@ -161,7 +159,7 @@ Error AudioDriverALSA::init() { } return err; -}; +} void AudioDriverALSA::thread_func(void *p_udata) { @@ -234,25 +232,25 @@ void AudioDriverALSA::thread_func(void *p_udata) { ad->stop_counting_ticks(); ad->unlock(); - }; + } ad->thread_exited = true; -}; +} void AudioDriverALSA::start() { active = true; -}; +} int AudioDriverALSA::get_mix_rate() const { return mix_rate; -}; +} AudioDriver::SpeakerMode AudioDriverALSA::get_speaker_mode() const { return speaker_mode; -}; +} Array AudioDriverALSA::get_device_list() { @@ -304,14 +302,14 @@ void AudioDriverALSA::lock() { if (!thread || !mutex) return; mutex->lock(); -}; +} void AudioDriverALSA::unlock() { if (!thread || !mutex) return; mutex->unlock(); -}; +} void AudioDriverALSA::finish_device() { @@ -339,18 +337,15 @@ void AudioDriverALSA::finish() { finish_device(); } -AudioDriverALSA::AudioDriverALSA() { - - mutex = NULL; - thread = NULL; - pcm_handle = NULL; - - device_name = "Default"; - new_device = "Default"; -}; - -AudioDriverALSA::~AudioDriverALSA(){ +AudioDriverALSA::AudioDriverALSA() : + thread(NULL), + mutex(NULL), + pcm_handle(NULL), + device_name("Default"), + new_device("Default") { +} -}; +AudioDriverALSA::~AudioDriverALSA() { +} #endif diff --git a/drivers/alsamidi/SCsub b/drivers/alsamidi/SCsub index 233593b0f9..4c24925192 100644 --- a/drivers/alsamidi/SCsub +++ b/drivers/alsamidi/SCsub @@ -4,5 +4,3 @@ Import('env') # Driver source files env.add_source_files(env.drivers_sources, "*.cpp") - -Export('env') diff --git a/drivers/alsamidi/alsa_midi.cpp b/drivers/alsamidi/alsa_midi.cpp index 599470d7e0..33ad7e3f17 100644 --- a/drivers/alsamidi/alsa_midi.cpp +++ b/drivers/alsamidi/alsa_midi.cpp @@ -30,11 +30,12 @@ #ifdef ALSAMIDI_ENABLED -#include <errno.h> - #include "alsa_midi.h" -#include "os/os.h" -#include "print_string.h" + +#include "core/os/os.h" +#include "core/print_string.h" + +#include <errno.h> static int get_message_size(uint8_t message) { switch (message & 0xF0) { diff --git a/drivers/alsamidi/alsa_midi.h b/drivers/alsamidi/alsa_midi.h index 90e458a365..5741036166 100644 --- a/drivers/alsamidi/alsa_midi.h +++ b/drivers/alsamidi/alsa_midi.h @@ -33,13 +33,13 @@ #ifndef ALSA_MIDI_H #define ALSA_MIDI_H -#include <alsa/asoundlib.h> -#include <stdio.h> - +#include "core/os/midi_driver.h" #include "core/os/mutex.h" #include "core/os/thread.h" #include "core/vector.h" -#include "os/midi_driver.h" + +#include <alsa/asoundlib.h> +#include <stdio.h> class MIDIDriverALSAMidi : public MIDIDriver { diff --git a/drivers/convex_decomp/SCsub b/drivers/convex_decomp/SCsub index f017e55120..65ba5332b7 100644 --- a/drivers/convex_decomp/SCsub +++ b/drivers/convex_decomp/SCsub @@ -11,6 +11,7 @@ thirdparty_sources = [ "b2Triangle.cpp", ] thirdparty_sources = [thirdparty_dir + file for file in thirdparty_sources] -env.add_source_files(env.drivers_sources, thirdparty_sources) -Export('env') +env_thirdparty = env.Clone() +env_thirdparty.disable_warnings() +env_thirdparty.add_source_files(env.drivers_sources, thirdparty_sources) diff --git a/drivers/convex_decomp/b2d_decompose.h b/drivers/convex_decomp/b2d_decompose.h index 068689d73d..f6b08b957c 100644 --- a/drivers/convex_decomp/b2d_decompose.h +++ b/drivers/convex_decomp/b2d_decompose.h @@ -31,8 +31,9 @@ #ifndef B2D_DECOMPOSE_H #define B2D_DECOMPOSE_H -#include "math_2d.h" -#include "vector.h" +#include "core/math/vector2.h" +#include "core/vector.h" + Vector<Vector<Vector2> > b2d_decompose(const Vector<Vector2> &p_polygon); #endif // B2D_DECOMPOSE_H diff --git a/drivers/coreaudio/SCsub b/drivers/coreaudio/SCsub index 233593b0f9..4c24925192 100644 --- a/drivers/coreaudio/SCsub +++ b/drivers/coreaudio/SCsub @@ -4,5 +4,3 @@ Import('env') # Driver source files env.add_source_files(env.drivers_sources, "*.cpp") - -Export('env') diff --git a/drivers/coreaudio/audio_driver_coreaudio.cpp b/drivers/coreaudio/audio_driver_coreaudio.cpp index e1f47cb8c2..3d093b965a 100644 --- a/drivers/coreaudio/audio_driver_coreaudio.cpp +++ b/drivers/coreaudio/audio_driver_coreaudio.cpp @@ -31,8 +31,9 @@ #ifdef COREAUDIO_ENABLED #include "audio_driver_coreaudio.h" + +#include "core/os/os.h" #include "core/project_settings.h" -#include "os/os.h" #define kOutputBus 0 #define kInputBus 1 @@ -94,11 +95,6 @@ Error AudioDriverCoreAudio::init() { result = AudioObjectAddPropertyListener(kAudioObjectSystemObject, &prop, &output_device_address_cb, this); ERR_FAIL_COND_V(result != noErr, FAILED); - - prop.mSelector = kAudioHardwarePropertyDefaultInputDevice; - - result = AudioObjectAddPropertyListener(kAudioObjectSystemObject, &prop, &input_device_address_cb, this); - ERR_FAIL_COND_V(result != noErr, FAILED); #endif AudioStreamBasicDescription strdesc; @@ -122,26 +118,6 @@ Error AudioDriverCoreAudio::init() { break; } - zeromem(&strdesc, sizeof(strdesc)); - size = sizeof(strdesc); - result = AudioUnitGetProperty(audio_unit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, kInputBus, &strdesc, &size); - ERR_FAIL_COND_V(result != noErr, FAILED); - - switch (strdesc.mChannelsPerFrame) { - case 1: // Mono - capture_channels = 1; - break; - - case 2: // Stereo - capture_channels = 2; - break; - - default: - // Unknown number of channels, default to stereo - capture_channels = 2; - break; - } - mix_rate = GLOBAL_DEF_RST("audio/mix_rate", DEFAULT_MIX_RATE); zeromem(&strdesc, sizeof(strdesc)); @@ -157,11 +133,6 @@ Error AudioDriverCoreAudio::init() { result = AudioUnitSetProperty(audio_unit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, kOutputBus, &strdesc, sizeof(strdesc)); ERR_FAIL_COND_V(result != noErr, FAILED); - strdesc.mChannelsPerFrame = capture_channels; - - result = AudioUnitSetProperty(audio_unit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, kInputBus, &strdesc, sizeof(strdesc)); - ERR_FAIL_COND_V(result != noErr, FAILED); - int latency = GLOBAL_DEF_RST("audio/output_latency", DEFAULT_OUTPUT_LATENCY); // Sample rate is independent of channels (ref: https://stackoverflow.com/questions/11048825/audio-sample-frequency-rely-on-channels) buffer_frames = closest_power_of_2(latency * mix_rate / 1000); @@ -174,14 +145,9 @@ Error AudioDriverCoreAudio::init() { unsigned int buffer_size = buffer_frames * channels; samples_in.resize(buffer_size); input_buf.resize(buffer_size); - input_buffer.resize(buffer_size * 8); - input_position = 0; - input_size = 0; - if (OS::get_singleton()->is_stdout_verbose()) { - print_line("CoreAudio: detected " + itos(channels) + " channels"); - print_line("CoreAudio: audio buffer frames: " + itos(buffer_frames) + " calculated latency: " + itos(buffer_frames * 1000 / mix_rate) + "ms"); - } + print_verbose("CoreAudio: detected " + itos(channels) + " channels"); + print_verbose("CoreAudio: audio buffer frames: " + itos(buffer_frames) + " calculated latency: " + itos(buffer_frames * 1000 / mix_rate) + "ms"); AURenderCallbackStruct callback; zeromem(&callback, sizeof(AURenderCallbackStruct)); @@ -190,15 +156,12 @@ Error AudioDriverCoreAudio::init() { result = AudioUnitSetProperty(audio_unit, kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Input, kOutputBus, &callback, sizeof(callback)); ERR_FAIL_COND_V(result != noErr, FAILED); - zeromem(&callback, sizeof(AURenderCallbackStruct)); - callback.inputProc = &AudioDriverCoreAudio::input_callback; - callback.inputProcRefCon = this; - result = AudioUnitSetProperty(audio_unit, kAudioOutputUnitProperty_SetInputCallback, kAudioUnitScope_Global, 0, &callback, sizeof(callback)); - ERR_FAIL_COND_V(result != noErr, FAILED); - result = AudioUnitInitialize(audio_unit); ERR_FAIL_COND_V(result != noErr, FAILED); + if (GLOBAL_GET("audio/enable_audio_input")) { + return capture_init(); + } return OK; } @@ -266,7 +229,7 @@ OSStatus AudioDriverCoreAudio::input_callback(void *inRefCon, bufferList.mBuffers[0].mNumberChannels = ad->capture_channels; bufferList.mBuffers[0].mDataByteSize = ad->input_buf.size() * sizeof(int16_t); - OSStatus result = AudioUnitRender(ad->audio_unit, ioActionFlags, inTimeStamp, inBusNumber, inNumberFrames, &bufferList); + OSStatus result = AudioUnitRender(ad->input_unit, ioActionFlags, inTimeStamp, inBusNumber, inNumberFrames, &bufferList); if (result == noErr) { for (int i = 0; i < inNumberFrames * ad->capture_channels; i++) { int32_t sample = ad->input_buf[i] << 16; @@ -278,7 +241,7 @@ OSStatus AudioDriverCoreAudio::input_callback(void *inRefCon, } } } else { - ERR_PRINT(("AudioUnitRender failed, code: " + itos(result)).utf8().get_data()); + ERR_PRINTS("AudioUnitRender failed, code: " + itos(result)); } ad->unlock(); @@ -290,7 +253,7 @@ void AudioDriverCoreAudio::start() { if (!active) { OSStatus result = AudioOutputUnitStart(audio_unit); if (result != noErr) { - ERR_PRINT(("AudioOutputUnitStart failed, code: " + itos(result)).utf8().get_data()); + ERR_PRINTS("AudioOutputUnitStart failed, code: " + itos(result)); } else { active = true; } @@ -301,7 +264,7 @@ void AudioDriverCoreAudio::stop() { if (active) { OSStatus result = AudioOutputUnitStop(audio_unit); if (result != noErr) { - ERR_PRINT(("AudioOutputUnitStop failed, code: " + itos(result)).utf8().get_data()); + ERR_PRINTS("AudioOutputUnitStop failed, code: " + itos(result)); } else { active = false; } @@ -333,70 +296,215 @@ bool AudioDriverCoreAudio::try_lock() { } void AudioDriverCoreAudio::finish() { - OSStatus result; + capture_finish(); - lock(); + if (audio_unit) { + OSStatus result; - AURenderCallbackStruct callback; - zeromem(&callback, sizeof(AURenderCallbackStruct)); - result = AudioUnitSetProperty(audio_unit, kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Input, kOutputBus, &callback, sizeof(callback)); - if (result != noErr) { - ERR_PRINT("AudioUnitSetProperty failed"); - } + lock(); - if (active) { - result = AudioOutputUnitStop(audio_unit); + AURenderCallbackStruct callback; + zeromem(&callback, sizeof(AURenderCallbackStruct)); + result = AudioUnitSetProperty(audio_unit, kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Input, kOutputBus, &callback, sizeof(callback)); if (result != noErr) { - ERR_PRINT("AudioOutputUnitStop failed"); + ERR_PRINT("AudioUnitSetProperty failed"); } - active = false; + if (active) { + result = AudioOutputUnitStop(audio_unit); + if (result != noErr) { + ERR_PRINT("AudioOutputUnitStop failed"); + } + + active = false; + } + + result = AudioUnitUninitialize(audio_unit); + if (result != noErr) { + ERR_PRINT("AudioUnitUninitialize failed"); + } + +#ifdef OSX_ENABLED + AudioObjectPropertyAddress prop; + prop.mSelector = kAudioHardwarePropertyDefaultOutputDevice; + prop.mScope = kAudioObjectPropertyScopeGlobal; + prop.mElement = kAudioObjectPropertyElementMaster; + + result = AudioObjectRemovePropertyListener(kAudioObjectSystemObject, &prop, &output_device_address_cb, this); + if (result != noErr) { + ERR_PRINT("AudioObjectRemovePropertyListener failed"); + } +#endif + + result = AudioComponentInstanceDispose(audio_unit); + if (result != noErr) { + ERR_PRINT("AudioComponentInstanceDispose failed"); + } + + audio_unit = NULL; + unlock(); } - result = AudioUnitUninitialize(audio_unit); - if (result != noErr) { - ERR_PRINT("AudioUnitUninitialize failed"); + if (mutex) { + memdelete(mutex); + mutex = NULL; } +} + +Error AudioDriverCoreAudio::capture_init() { + AudioComponentDescription desc; + zeromem(&desc, sizeof(desc)); + desc.componentType = kAudioUnitType_Output; +#ifdef OSX_ENABLED + desc.componentSubType = kAudioUnitSubType_HALOutput; +#else + desc.componentSubType = kAudioUnitSubType_RemoteIO; +#endif + desc.componentManufacturer = kAudioUnitManufacturer_Apple; + + AudioComponent comp = AudioComponentFindNext(NULL, &desc); + ERR_FAIL_COND_V(comp == NULL, FAILED); + + OSStatus result = AudioComponentInstanceNew(comp, &input_unit); + ERR_FAIL_COND_V(result != noErr, FAILED); #ifdef OSX_ENABLED AudioObjectPropertyAddress prop; - prop.mSelector = kAudioHardwarePropertyDefaultOutputDevice; + prop.mSelector = kAudioHardwarePropertyDefaultInputDevice; prop.mScope = kAudioObjectPropertyScopeGlobal; prop.mElement = kAudioObjectPropertyElementMaster; - result = AudioObjectRemovePropertyListener(kAudioObjectSystemObject, &prop, &output_device_address_cb, this); - if (result != noErr) { - ERR_PRINT("AudioObjectRemovePropertyListener failed"); - } + result = AudioObjectAddPropertyListener(kAudioObjectSystemObject, &prop, &input_device_address_cb, this); + ERR_FAIL_COND_V(result != noErr, FAILED); #endif - result = AudioComponentInstanceDispose(audio_unit); - if (result != noErr) { - ERR_PRINT("AudioComponentInstanceDispose failed"); + UInt32 flag = 1; + result = AudioUnitSetProperty(input_unit, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Input, kInputBus, &flag, sizeof(flag)); + ERR_FAIL_COND_V(result != noErr, FAILED); + flag = 0; + result = AudioUnitSetProperty(input_unit, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Output, kOutputBus, &flag, sizeof(flag)); + ERR_FAIL_COND_V(result != noErr, FAILED); + + UInt32 size; +#ifdef OSX_ENABLED + AudioDeviceID deviceId; + size = sizeof(AudioDeviceID); + AudioObjectPropertyAddress property = { kAudioHardwarePropertyDefaultInputDevice, kAudioObjectPropertyScopeGlobal, kAudioObjectPropertyElementMaster }; + + result = AudioObjectGetPropertyData(kAudioObjectSystemObject, &property, 0, NULL, &size, &deviceId); + ERR_FAIL_COND_V(result != noErr, FAILED); + + result = AudioUnitSetProperty(input_unit, kAudioOutputUnitProperty_CurrentDevice, kAudioUnitScope_Global, 0, &deviceId, sizeof(AudioDeviceID)); + ERR_FAIL_COND_V(result != noErr, FAILED); +#endif + + AudioStreamBasicDescription strdesc; + zeromem(&strdesc, sizeof(strdesc)); + size = sizeof(strdesc); + result = AudioUnitGetProperty(input_unit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, kInputBus, &strdesc, &size); + ERR_FAIL_COND_V(result != noErr, FAILED); + + switch (strdesc.mChannelsPerFrame) { + case 1: // Mono + capture_channels = 1; + break; + + case 2: // Stereo + capture_channels = 2; + break; + + default: + // Unknown number of channels, default to stereo + capture_channels = 2; + break; } - unlock(); + mix_rate = GLOBAL_DEF_RST("audio/mix_rate", DEFAULT_MIX_RATE); - if (mutex) { - memdelete(mutex); - mutex = NULL; + zeromem(&strdesc, sizeof(strdesc)); + strdesc.mFormatID = kAudioFormatLinearPCM; + strdesc.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger | kLinearPCMFormatFlagIsPacked; + strdesc.mChannelsPerFrame = capture_channels; + strdesc.mSampleRate = mix_rate; + strdesc.mFramesPerPacket = 1; + strdesc.mBitsPerChannel = 16; + strdesc.mBytesPerFrame = strdesc.mBitsPerChannel * strdesc.mChannelsPerFrame / 8; + strdesc.mBytesPerPacket = strdesc.mBytesPerFrame * strdesc.mFramesPerPacket; + + result = AudioUnitSetProperty(input_unit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, kInputBus, &strdesc, sizeof(strdesc)); + ERR_FAIL_COND_V(result != noErr, FAILED); + + AURenderCallbackStruct callback; + zeromem(&callback, sizeof(AURenderCallbackStruct)); + callback.inputProc = &AudioDriverCoreAudio::input_callback; + callback.inputProcRefCon = this; + result = AudioUnitSetProperty(input_unit, kAudioOutputUnitProperty_SetInputCallback, kAudioUnitScope_Global, kInputBus, &callback, sizeof(callback)); + ERR_FAIL_COND_V(result != noErr, FAILED); + + result = AudioUnitInitialize(input_unit); + ERR_FAIL_COND_V(result != noErr, FAILED); + + return OK; +} + +void AudioDriverCoreAudio::capture_finish() { + if (input_unit) { + lock(); + + AURenderCallbackStruct callback; + zeromem(&callback, sizeof(AURenderCallbackStruct)); + OSStatus result = AudioUnitSetProperty(input_unit, kAudioOutputUnitProperty_SetInputCallback, kAudioUnitScope_Global, 0, &callback, sizeof(callback)); + if (result != noErr) { + ERR_PRINT("AudioUnitSetProperty failed"); + } + + result = AudioUnitUninitialize(input_unit); + if (result != noErr) { + ERR_PRINT("AudioUnitUninitialize failed"); + } + +#ifdef OSX_ENABLED + AudioObjectPropertyAddress prop; + prop.mSelector = kAudioHardwarePropertyDefaultInputDevice; + prop.mScope = kAudioObjectPropertyScopeGlobal; + prop.mElement = kAudioObjectPropertyElementMaster; + + result = AudioObjectRemovePropertyListener(kAudioObjectSystemObject, &prop, &input_device_address_cb, this); + if (result != noErr) { + ERR_PRINT("AudioObjectRemovePropertyListener failed"); + } +#endif + + result = AudioComponentInstanceDispose(input_unit); + if (result != noErr) { + ERR_PRINT("AudioComponentInstanceDispose failed"); + } + + input_unit = NULL; + unlock(); } -}; +} Error AudioDriverCoreAudio::capture_start() { - UInt32 flag = 1; - OSStatus result = AudioUnitSetProperty(audio_unit, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Input, kInputBus, &flag, sizeof(flag)); - ERR_FAIL_COND_V(result != noErr, FAILED); + input_buffer_init(buffer_frames); + + OSStatus result = AudioOutputUnitStart(input_unit); + if (result != noErr) { + ERR_PRINTS("AudioOutputUnitStart failed, code: " + itos(result)); + } return OK; } Error AudioDriverCoreAudio::capture_stop() { - UInt32 flag = 0; - OSStatus result = AudioUnitSetProperty(audio_unit, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Input, kInputBus, &flag, sizeof(flag)); - ERR_FAIL_COND_V(result != noErr, FAILED); + if (input_unit) { + OSStatus result = AudioOutputUnitStop(input_unit); + if (result != noErr) { + ERR_PRINTS("AudioOutputUnitStop failed, code: " + itos(result)); + } + } return OK; } @@ -530,12 +638,14 @@ void AudioDriverCoreAudio::_set_device(const String &device, bool capture) { } if (found) { - OSStatus result = AudioUnitSetProperty(audio_unit, kAudioOutputUnitProperty_CurrentDevice, kAudioUnitScope_Global, capture ? kInputBus : kOutputBus, &deviceId, sizeof(AudioDeviceID)); + OSStatus result = AudioUnitSetProperty(capture ? input_unit : audio_unit, kAudioOutputUnitProperty_CurrentDevice, kAudioUnitScope_Global, 0, &deviceId, sizeof(AudioDeviceID)); ERR_FAIL_COND(result != noErr); - // Reset audio input to keep synchronisation. - input_position = 0; - input_size = 0; + if (capture) { + // Reset audio input to keep synchronisation. + input_position = 0; + input_size = 0; + } } } @@ -577,20 +687,18 @@ String AudioDriverCoreAudio::capture_get_device() { #endif -AudioDriverCoreAudio::AudioDriverCoreAudio() { - active = false; - mutex = NULL; - - mix_rate = 0; - channels = 2; - capture_channels = 2; - - buffer_frames = 0; - +AudioDriverCoreAudio::AudioDriverCoreAudio() : + audio_unit(NULL), + input_unit(NULL), + active(false), + mutex(NULL), + device_name("Default"), + capture_device_name("Default"), + mix_rate(0), + channels(2), + capture_channels(2), + buffer_frames(0) { samples_in.clear(); - - device_name = "Default"; - capture_device_name = "Default"; } AudioDriverCoreAudio::~AudioDriverCoreAudio(){}; diff --git a/drivers/coreaudio/audio_driver_coreaudio.h b/drivers/coreaudio/audio_driver_coreaudio.h index d3f7c8d596..474a9e43ae 100644 --- a/drivers/coreaudio/audio_driver_coreaudio.h +++ b/drivers/coreaudio/audio_driver_coreaudio.h @@ -43,6 +43,7 @@ class AudioDriverCoreAudio : public AudioDriver { AudioComponentInstance audio_unit; + AudioComponentInstance input_unit; bool active; Mutex *mutex; @@ -83,6 +84,9 @@ class AudioDriverCoreAudio : public AudioDriver { UInt32 inBusNumber, UInt32 inNumberFrames, AudioBufferList *ioData); + Error capture_init(); + void capture_finish(); + public: const char *get_name() const { return "CoreAudio"; diff --git a/drivers/coremidi/SCsub b/drivers/coremidi/SCsub index 233593b0f9..4c24925192 100644 --- a/drivers/coremidi/SCsub +++ b/drivers/coremidi/SCsub @@ -4,5 +4,3 @@ Import('env') # Driver source files env.add_source_files(env.drivers_sources, "*.cpp") - -Export('env') diff --git a/drivers/coremidi/core_midi.cpp b/drivers/coremidi/core_midi.cpp index 3619be4a8e..a20aec04b5 100644 --- a/drivers/coremidi/core_midi.cpp +++ b/drivers/coremidi/core_midi.cpp @@ -31,7 +31,8 @@ #ifdef COREMIDI_ENABLED #include "core_midi.h" -#include "print_string.h" + +#include "core/print_string.h" #include <CoreAudio/HostTime.h> #include <CoreServices/CoreServices.h> @@ -50,13 +51,13 @@ Error MIDIDriverCoreMidi::open() { OSStatus result = MIDIClientCreate(name, NULL, NULL, &client); CFRelease(name); if (result != noErr) { - ERR_PRINTS("MIDIClientCreate failed: " + String(GetMacOSStatusErrorString(result))); + ERR_PRINTS("MIDIClientCreate failed, code: " + itos(result)); return ERR_CANT_OPEN; } result = MIDIInputPortCreate(client, CFSTR("Godot Input"), MIDIDriverCoreMidi::read, (void *)this, &port_in); if (result != noErr) { - ERR_PRINTS("MIDIInputPortCreate failed: " + String(GetMacOSStatusErrorString(result))); + ERR_PRINTS("MIDIInputPortCreate failed, code: " + itos(result)); return ERR_CANT_OPEN; } @@ -64,7 +65,7 @@ Error MIDIDriverCoreMidi::open() { for (int i = 0; i < sources; i++) { MIDIEndpointRef source = MIDIGetSource(i); - if (source != NULL) { + if (source) { MIDIPortConnectSource(port_in, source, (void *)this); connected_sources.insert(i, source); } @@ -92,13 +93,30 @@ void MIDIDriverCoreMidi::close() { } } -MIDIDriverCoreMidi::MIDIDriverCoreMidi() { +PoolStringArray MIDIDriverCoreMidi::get_connected_inputs() { + + PoolStringArray list; + + for (int i = 0; i < connected_sources.size(); i++) { + MIDIEndpointRef source = connected_sources[i]; + CFStringRef ref = NULL; + char name[256]; + + MIDIObjectGetStringProperty(source, kMIDIPropertyDisplayName, &ref); + CFStringGetCString(ref, name, sizeof(name), kCFStringEncodingUTF8); + CFRelease(ref); - client = 0; + list.push_back(name); + } + + return list; } -MIDIDriverCoreMidi::~MIDIDriverCoreMidi() { +MIDIDriverCoreMidi::MIDIDriverCoreMidi() : + client(0) { +} +MIDIDriverCoreMidi::~MIDIDriverCoreMidi() { close(); } diff --git a/drivers/coremidi/core_midi.h b/drivers/coremidi/core_midi.h index fd35e12f4b..ea6b0fcb06 100644 --- a/drivers/coremidi/core_midi.h +++ b/drivers/coremidi/core_midi.h @@ -33,12 +33,11 @@ #ifndef CORE_MIDI_H #define CORE_MIDI_H -#include <stdio.h> +#include "core/os/midi_driver.h" +#include "core/vector.h" #include <CoreMIDI/CoreMIDI.h> - -#include "core/vector.h" -#include "os/midi_driver.h" +#include <stdio.h> class MIDIDriverCoreMidi : public MIDIDriver { @@ -53,6 +52,8 @@ public: virtual Error open(); virtual void close(); + PoolStringArray get_connected_inputs(); + MIDIDriverCoreMidi(); virtual ~MIDIDriverCoreMidi(); }; diff --git a/drivers/dummy/rasterizer_dummy.h b/drivers/dummy/rasterizer_dummy.h index e39ec915fc..dfdb3a6bba 100644 --- a/drivers/dummy/rasterizer_dummy.h +++ b/drivers/dummy/rasterizer_dummy.h @@ -27,16 +27,16 @@ /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ + #ifndef RASTERIZER_DUMMY_H #define RASTERIZER_DUMMY_H -#include "camera_matrix.h" +#include "core/math/camera_matrix.h" +#include "core/self_list.h" #include "scene/resources/mesh.h" #include "servers/visual/rasterizer.h" #include "servers/visual_server.h" -#include "self_list.h" - class RasterizerSceneDummy : public RasterizerScene { public: /* SHADOW ATLAS API */ @@ -63,7 +63,8 @@ public: void environment_set_dof_blur_near(RID p_env, bool p_enable, float p_distance, float p_transition, float p_far_amount, VS::EnvironmentDOFBlurQuality p_quality) {} void environment_set_dof_blur_far(RID p_env, bool p_enable, float p_distance, float p_transition, float p_far_amount, VS::EnvironmentDOFBlurQuality p_quality) {} - void environment_set_glow(RID p_env, bool p_enable, int p_level_flags, float p_intensity, float p_strength, float p_bloom_threshold, VS::EnvironmentGlowBlendMode p_blend_mode, float p_hdr_bleed_threshold, float p_hdr_bleed_scale, bool p_bicubic_upscale) {} + void environment_set_glow(RID p_env, bool p_enable, int p_level_flags, float p_intensity, float p_strength, float p_bloom_threshold, VS::EnvironmentGlowBlendMode p_blend_mode, float p_hdr_bleed_threshold, float p_hdr_bleed_scale, float p_hdr_luminance_cap, bool p_bicubic_upscale) {} + void environment_set_fog(RID p_env, bool p_enable, float p_begin, float p_end, RID p_gradient_texture) {} void environment_set_ssr(RID p_env, bool p_enable, int p_max_steps, float p_fade_int, float p_fade_out, float p_depth_tolerance, bool p_roughness) {} @@ -74,7 +75,7 @@ public: void environment_set_adjustment(RID p_env, bool p_enable, float p_brightness, float p_contrast, float p_saturation, RID p_ramp) {} void environment_set_fog(RID p_env, bool p_enable, const Color &p_color, const Color &p_sun_color, float p_sun_amount) {} - void environment_set_fog_depth(RID p_env, bool p_enable, float p_depth_begin, float p_depth_curve, bool p_transmit, float p_transmit_curve) {} + void environment_set_fog_depth(RID p_env, bool p_enable, float p_depth_begin, float p_depth_end, float p_depth_curve, bool p_transmit, float p_transmit_curve) {} void environment_set_fog_height(RID p_env, bool p_enable, float p_min_height, float p_max_height, float p_height_curve) {} bool is_environment(RID p_env) { return false; } @@ -267,6 +268,7 @@ public: void material_set_param(RID p_material, const StringName &p_param, const Variant &p_value) {} Variant material_get_param(RID p_material, const StringName &p_param) const { return Variant(); } + Variant material_get_param_default(RID p_material, const StringName &p_param) const { return Variant(); } void material_set_line_width(RID p_material, float p_width) {} @@ -516,6 +518,7 @@ public: void reflection_probe_set_enable_box_projection(RID p_probe, bool p_enable) {} void reflection_probe_set_enable_shadows(RID p_probe, bool p_enable) {} void reflection_probe_set_cull_mask(RID p_probe, uint32_t p_layers) {} + void reflection_probe_set_resolution(RID p_probe, int p_resolution) {} AABB reflection_probe_get_aabb(RID p_probe) const { return AABB(); } VS::ReflectionProbeUpdateMode reflection_probe_get_update_mode(RID p_probe) const { return VisualServer::REFLECTION_PROBE_UPDATE_ONCE; } @@ -582,22 +585,12 @@ public: SelfList<RasterizerScene::InstanceBase>::List instance_list; - _FORCE_INLINE_ void instance_change_notify() { + _FORCE_INLINE_ void instance_change_notify(bool p_aabb = true, bool p_materials = true) { SelfList<RasterizerScene::InstanceBase> *instances = instance_list.first(); while (instances) { - instances->self()->base_changed(); - instances = instances->next(); - } - } - - _FORCE_INLINE_ void instance_material_change_notify() { - - SelfList<RasterizerScene::InstanceBase> *instances = instance_list.first(); - while (instances) { - - instances->self()->base_material_changed(); + instances->self()->base_changed(p_aabb, p_materials); instances = instances->next(); } } @@ -689,6 +682,8 @@ public: int particles_get_draw_passes(RID p_particles) const { return 0; } RID particles_get_draw_pass_mesh(RID p_particles, int p_pass) const { return RID(); } + virtual bool particles_is_inactive(RID p_particles) const { return false; } + /* RENDER TARGET */ RID render_target_create() { return RID(); } @@ -786,9 +781,14 @@ public: void restore_render_target() {} void clear_render_target(const Color &p_color) {} void blit_render_target_to_screen(RID p_render_target, const Rect2 &p_screen_rect, int p_screen = 0) {} + void output_lens_distorted_to_screen(RID p_render_target, const Rect2 &p_screen_rect, float p_k1, float p_k2, const Vector2 &p_eye_center, float p_oversample) {} void end_frame(bool p_swap_buffers) {} void finalize() {} + static Error is_viable() { + return OK; + } + static Rasterizer *_create_current() { return memnew(RasterizerDummy); } @@ -797,6 +797,8 @@ public: _create_func = _create_current; } + virtual bool is_low_end() const { return true; } + RasterizerDummy() {} ~RasterizerDummy() {} }; diff --git a/drivers/dummy/texture_loader_dummy.cpp b/drivers/dummy/texture_loader_dummy.cpp index b099019d17..8153fbd10b 100644 --- a/drivers/dummy/texture_loader_dummy.cpp +++ b/drivers/dummy/texture_loader_dummy.cpp @@ -29,8 +29,10 @@ /*************************************************************************/ #include "texture_loader_dummy.h" + #include "core/os/file_access.h" -#include "print_string.h" +#include "core/print_string.h" + #include <string.h> RES ResourceFormatDummyTexture::load(const String &p_path, const String &p_original_path, Error *r_error) { diff --git a/drivers/gl_context/SCsub b/drivers/gl_context/SCsub index 4d66a9f9f1..efb26a7908 100644 --- a/drivers/gl_context/SCsub +++ b/drivers/gl_context/SCsub @@ -10,13 +10,14 @@ if (env["platform"] in ["haiku", "osx", "windows", "x11"]): ] thirdparty_sources = [thirdparty_dir + file for file in thirdparty_sources] - env.add_source_files(env.drivers_sources, thirdparty_sources) env.Append(CPPPATH=[thirdparty_dir]) env.Append(CPPFLAGS=['-DGLAD_ENABLED']) env.Append(CPPFLAGS=['-DGLES_OVER_GL']) + env_thirdparty = env.Clone() + env_thirdparty.disable_warnings() + env_thirdparty.add_source_files(env.drivers_sources, thirdparty_sources) + # Godot source files env.add_source_files(env.drivers_sources, "*.cpp") - -Export('env') diff --git a/drivers/gl_context/context_gl.h b/drivers/gl_context/context_gl.h index 60781a3453..37f334454b 100644 --- a/drivers/gl_context/context_gl.h +++ b/drivers/gl_context/context_gl.h @@ -33,7 +33,7 @@ #if defined(OPENGL_ENABLED) || defined(GLES_ENABLED) -#include "typedefs.h" +#include "core/typedefs.h" /** @author Juan Linietsky <reduzio@gmail.com> diff --git a/drivers/gles2/SCsub b/drivers/gles2/SCsub index 2471dd3739..9923e52c73 100644 --- a/drivers/gles2/SCsub +++ b/drivers/gles2/SCsub @@ -2,6 +2,6 @@ Import('env') -env.add_source_files(env.drivers_sources,"*.cpp") +env.add_source_files(env.drivers_sources, "*.cpp") SConscript("shaders/SCsub") diff --git a/drivers/gles2/rasterizer_canvas_gles2.cpp b/drivers/gles2/rasterizer_canvas_gles2.cpp index 3d388c031a..9227c04e71 100644 --- a/drivers/gles2/rasterizer_canvas_gles2.cpp +++ b/drivers/gles2/rasterizer_canvas_gles2.cpp @@ -27,11 +27,14 @@ /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ + #include "rasterizer_canvas_gles2.h" -#include "os/os.h" -#include "project_settings.h" + +#include "core/os/os.h" +#include "core/project_settings.h" #include "rasterizer_scene_gles2.h" #include "servers/visual/visual_server_raster.h" + #ifndef GLES_OVER_GL #define glClearDepth glClearDepthf #endif @@ -75,6 +78,7 @@ void RasterizerCanvasGLES2::canvas_begin() { } if (storage->frame.clear_request) { + glColorMask(true, true, true, true); glClearColor(storage->frame.clear_request_color.r, storage->frame.clear_request_color.g, storage->frame.clear_request_color.b, @@ -151,7 +155,7 @@ RasterizerStorageGLES2::Texture *RasterizerCanvasGLES2::_bind_canvas_texture(con state.current_tex = RID(); state.current_tex_ptr = NULL; - glActiveTexture(GL_TEXTURE0); + glActiveTexture(GL_TEXTURE0 + storage->config.max_texture_image_units - 1); glBindTexture(GL_TEXTURE_2D, storage->resources.white_tex); } else { @@ -166,7 +170,7 @@ RasterizerStorageGLES2::Texture *RasterizerCanvasGLES2::_bind_canvas_texture(con texture->render_target->used_in_frame = true; } - glActiveTexture(GL_TEXTURE0); + glActiveTexture(GL_TEXTURE0 + storage->config.max_texture_image_units - 1); glBindTexture(GL_TEXTURE_2D, texture->tex_id); state.current_tex = p_texture; @@ -178,7 +182,7 @@ RasterizerStorageGLES2::Texture *RasterizerCanvasGLES2::_bind_canvas_texture(con state.current_tex = RID(); state.current_tex_ptr = NULL; - glActiveTexture(GL_TEXTURE0); + glActiveTexture(GL_TEXTURE0 + storage->config.max_texture_image_units - 1); glBindTexture(GL_TEXTURE_2D, storage->resources.white_tex); } @@ -485,7 +489,8 @@ void RasterizerCanvasGLES2::_canvas_item_render_commands(Item *p_item, Item *cur RasterizerStorageGLES2::Texture *tex = _bind_canvas_texture(np->texture, np->normal_map); if (!tex) { - print_line("TODO: ninepatch without texture"); + // FIXME: Handle textureless ninepatch gracefully + WARN_PRINT("NinePatch without texture not supported yet in GLES2 backend, skipping."); continue; } @@ -560,7 +565,7 @@ void RasterizerCanvasGLES2::_canvas_item_render_commands(Item *p_item, Item *cur buffer[(1 * 4 * 4) + 14] = (source.position.x + source.size.x) * texpixel_size.x; buffer[(1 * 4 * 4) + 15] = (source.position.y + np->margin[MARGIN_TOP]) * texpixel_size.y; - // thrid row + // third row buffer[(2 * 4 * 4) + 0] = np->rect.position.x; buffer[(2 * 4 * 4) + 1] = np->rect.position.y + np->rect.size.y - np->margin[MARGIN_BOTTOM]; @@ -611,8 +616,6 @@ void RasterizerCanvasGLES2::_canvas_item_render_commands(Item *p_item, Item *cur buffer[(3 * 4 * 4) + 14] = (source.position.x + source.size.x) * texpixel_size.x; buffer[(3 * 4 * 4) + 15] = (source.position.y + source.size.y) * texpixel_size.y; - - // print_line(String::num((source.position.y + source.size.y) * texpixel_size.y)); } glBindBuffer(GL_ARRAY_BUFFER, data.ninepatch_vertices); @@ -788,7 +791,8 @@ void RasterizerCanvasGLES2::_canvas_item_render_commands(Item *p_item, Item *cur } break; default: { - print_line("other"); + // FIXME: Proper error handling if relevant + //print_line("other"); } break; } } @@ -807,8 +811,6 @@ void RasterizerCanvasGLES2::canvas_render_items(Item *p_item_list, int p_z, cons bool rebind_shader = true; - Size2 rt_size = Size2(storage->frame.current_rt->width, storage->frame.current_rt->height); - state.current_tex = RID(); state.current_tex_ptr = NULL; state.current_normal = RID(); @@ -888,7 +890,7 @@ void RasterizerCanvasGLES2::canvas_render_items(Item *p_item_list, int p_z, cons for (int i = 0; i < tc; i++) { - glActiveTexture(GL_TEXTURE2 + i); + glActiveTexture(GL_TEXTURE0 + i); RasterizerStorageGLES2::Texture *t = storage->texture_owner.getornull(textures[i].second); @@ -936,7 +938,7 @@ void RasterizerCanvasGLES2::canvas_render_items(Item *p_item_list, int p_z, cons } int blend_mode = shader_cache ? shader_cache->canvas_item.blend_mode : RasterizerStorageGLES2::Shader::CanvasItem::BLEND_MODE_MIX; - bool unshaded = true || (shader_cache && blend_mode != RasterizerStorageGLES2::Shader::CanvasItem::BLEND_MODE_MIX); + bool unshaded = (shader_cache && blend_mode != RasterizerStorageGLES2::Shader::CanvasItem::BLEND_MODE_MIX); bool reclip = false; if (last_blend_mode != blend_mode) { @@ -1026,8 +1028,8 @@ void RasterizerCanvasGLES2::reset_canvas() { // keeping this for now as there's nothing else that uses texture unit 2 // TODO ^ if (storage->frame.current_rt) { - glActiveTexture(GL_TEXTURE0 + 2); - glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->copy_screen_effect.color); + // glActiveTexture(GL_TEXTURE0 + 2); + // glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->copy_screen_effect.color); } glBindBuffer(GL_ARRAY_BUFFER, 0); @@ -1047,6 +1049,43 @@ void RasterizerCanvasGLES2::draw_generic_textured_rect(const Rect2 &p_rect, cons glDrawArrays(GL_TRIANGLE_FAN, 0, 4); } +void RasterizerCanvasGLES2::draw_lens_distortion_rect(const Rect2 &p_rect, float p_k1, float p_k2, const Vector2 &p_eye_center, float p_oversample) { + Vector2 half_size; + if (storage->frame.current_rt) { + half_size = Vector2(storage->frame.current_rt->width, storage->frame.current_rt->height); + } else { + half_size = OS::get_singleton()->get_window_size(); + } + half_size *= 0.5; + Vector2 offset((p_rect.position.x - half_size.x) / half_size.x, (p_rect.position.y - half_size.y) / half_size.y); + Vector2 scale(p_rect.size.x / half_size.x, p_rect.size.y / half_size.y); + + float aspect_ratio = p_rect.size.x / p_rect.size.y; + + // setup our lens shader + state.lens_shader.bind(); + state.lens_shader.set_uniform(LensDistortedShaderGLES2::OFFSET, offset); + state.lens_shader.set_uniform(LensDistortedShaderGLES2::SCALE, scale); + state.lens_shader.set_uniform(LensDistortedShaderGLES2::K1, p_k1); + state.lens_shader.set_uniform(LensDistortedShaderGLES2::K2, p_k2); + state.lens_shader.set_uniform(LensDistortedShaderGLES2::EYE_CENTER, p_eye_center); + state.lens_shader.set_uniform(LensDistortedShaderGLES2::UPSCALE, p_oversample); + state.lens_shader.set_uniform(LensDistortedShaderGLES2::ASPECT_RATIO, aspect_ratio); + + // bind our quad buffer + _bind_quad_buffer(); + + // and draw + glDrawArrays(GL_TRIANGLE_FAN, 0, 4); + + // and cleanup + glBindBuffer(GL_ARRAY_BUFFER, 0); + + for (int i = 0; i < VS::ARRAY_MAX; i++) { + glDisableVertexAttribArray(i); + } +} + void RasterizerCanvasGLES2::draw_window_margins(int *black_margin, RID *black_image) { } @@ -1072,6 +1111,7 @@ void RasterizerCanvasGLES2::initialize() { // polygon buffer { uint32_t poly_size = GLOBAL_DEF("rendering/limits/buffers/canvas_polygon_buffer_size_kb", 128); + ProjectSettings::get_singleton()->set_custom_property_info("rendering/limits/buffers/canvas_polygon_buffer_size_kb", PropertyInfo(Variant::INT, "rendering/limits/buffers/canvas_polygon_buffer_size_kb", PROPERTY_HINT_RANGE, "0,256,1,or_greater")); poly_size *= 1024; poly_size = MAX(poly_size, (2 + 2 + 4) * 4 * sizeof(float)); glGenBuffers(1, &data.polygon_buffer); @@ -1083,6 +1123,7 @@ void RasterizerCanvasGLES2::initialize() { glBindBuffer(GL_ARRAY_BUFFER, 0); uint32_t index_size = GLOBAL_DEF("rendering/limits/buffers/canvas_polygon_index_size_kb", 128); + ProjectSettings::get_singleton()->set_custom_property_info("rendering/limits/buffers/canvas_polygon_index_size_kb", PropertyInfo(Variant::INT, "rendering/limits/buffers/canvas_polygon_index_size_kb", PROPERTY_HINT_RANGE, "0,256,1,or_greater")); index_size *= 1024; // kb glGenBuffers(1, &data.polygon_index_buffer); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, data.polygon_index_buffer); @@ -1146,7 +1187,6 @@ void RasterizerCanvasGLES2::initialize() { _EIDX(1, 1), _EIDX(1, 2), _EIDX(2, 2), _EIDX(2, 2), _EIDX(2, 1), _EIDX(1, 1) }; - ; #undef _EIDX glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(elems), elems, GL_STATIC_DRAW); @@ -1159,6 +1199,10 @@ void RasterizerCanvasGLES2::initialize() { state.canvas_shader.set_conditional(CanvasShaderGLES2::USE_TEXTURE_RECT, true); state.canvas_shader.bind(); + + state.lens_shader.init(); + + state.canvas_shader.set_conditional(CanvasShaderGLES2::USE_PIXEL_SNAP, GLOBAL_DEF("rendering/quality/2d/use_pixel_snap", false)); } void RasterizerCanvasGLES2::finalize() { diff --git a/drivers/gles2/rasterizer_canvas_gles2.h b/drivers/gles2/rasterizer_canvas_gles2.h index cda3ec79e7..cf1c239b6e 100644 --- a/drivers/gles2/rasterizer_canvas_gles2.h +++ b/drivers/gles2/rasterizer_canvas_gles2.h @@ -34,6 +34,7 @@ #include "servers/visual/rasterizer.h" #include "shaders/canvas.glsl.gen.h" +#include "shaders/lens_distorted.glsl.gen.h" // #include "shaders/canvas_shadow.glsl.gen.h" @@ -70,6 +71,7 @@ public: bool canvas_texscreen_used; CanvasShaderGLES2 canvas_shader; // CanvasShadowShaderGLES3 canvas_shadow_shader; + LensDistortedShaderGLES2 lens_shader; bool using_texture_rect; bool using_ninepatch; @@ -117,6 +119,7 @@ public: void _bind_quad_buffer(); void draw_generic_textured_rect(const Rect2 &p_rect, const Rect2 &p_src); + void draw_lens_distortion_rect(const Rect2 &p_rect, float p_k1, float p_k2, const Vector2 &p_eye_center, float p_oversample); void initialize(); void finalize(); diff --git a/drivers/gles2/rasterizer_gles2.cpp b/drivers/gles2/rasterizer_gles2.cpp index a1a0b9e2c6..f49dbfeabb 100644 --- a/drivers/gles2/rasterizer_gles2.cpp +++ b/drivers/gles2/rasterizer_gles2.cpp @@ -27,12 +27,12 @@ /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ + #include "rasterizer_gles2.h" -#include "gl_context/context_gl.h" -#include "os/os.h" -#include "project_settings.h" -#include <string.h> +#include "core/os/os.h" +#include "core/project_settings.h" +#include "drivers/gl_context/context_gl.h" #define _EXT_DEBUG_OUTPUT_SYNCHRONOUS_ARB 0x8242 #define _EXT_DEBUG_NEXT_LOGGED_MESSAGE_LENGTH_ARB 0x8243 @@ -58,12 +58,29 @@ #define _EXT_DEBUG_SEVERITY_LOW_ARB 0x9148 #define _EXT_DEBUG_OUTPUT 0x92E0 -#if (defined WINDOWS_ENABLED) && !(defined UWP_ENABLED) +#ifndef GLAPIENTRY +#if defined(WINDOWS_ENABLED) && !defined(UWP_ENABLED) #define GLAPIENTRY APIENTRY #else #define GLAPIENTRY #endif +#endif + +#if !defined(GLES_OVER_GL) && !defined(IPHONE_ENABLED) +// Used for debugging on mobile, but not iOS as EGL is not available +#include <GLES2/gl2.h> +#include <GLES2/gl2ext.h> +#include <GLES2/gl2platform.h> + +#include <EGL/egl.h> +#include <EGL/eglext.h> +#endif + +#if defined(MINGW_ENABLED) || defined(_MSC_VER) +#define strcpy strcpy_s +#endif +#ifndef IPHONE_ENABLED static void GLAPIENTRY _gl_debug_print(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar *message, const GLvoid *userParam) { if (type == _EXT_DEBUG_TYPE_OTHER_ARB) @@ -73,6 +90,7 @@ static void GLAPIENTRY _gl_debug_print(GLenum source, GLenum type, GLuint id, GL return; //these are ultimately annoying, so removing for now char debSource[256], debType[256], debSev[256]; + if (source == _EXT_DEBUG_SOURCE_API_ARB) strcpy(debSource, "OpenGL"); else if (source == _EXT_DEBUG_SOURCE_WINDOW_SYSTEM_ARB) @@ -110,6 +128,7 @@ static void GLAPIENTRY _gl_debug_print(GLenum source, GLenum type, GLuint id, GL ERR_PRINTS(output); } +#endif // IPHONE_ENABLED typedef void (*DEBUGPROCARB)(GLenum source, GLenum type, @@ -136,28 +155,21 @@ RasterizerScene *RasterizerGLES2::get_scene() { return scene; } -void RasterizerGLES2::initialize() { - - if (OS::get_singleton()->is_stdout_verbose()) { - print_line("Using GLES2 video driver"); - } +Error RasterizerGLES2::is_viable() { #ifdef GLAD_ENABLED if (!gladLoadGL()) { ERR_PRINT("Error initializing GLAD"); + return ERR_UNAVAILABLE; } // GLVersion seems to be used for both GL and GL ES, so we need different version checks for them #ifdef OPENGL_ENABLED // OpenGL 2.1 Profile required - if (GLVersion.major < 2) { -#else // OpenGL ES 3.0 + if (GLVersion.major < 2 || (GLVersion.major == 2 && GLVersion.minor < 1)) { +#else // OpenGL ES 2.0 if (GLVersion.major < 2) { #endif - ERR_PRINT("Your system's graphic drivers seem not to support OpenGL 2.1 / OpenGL ES 2.0, sorry :(\n" - "Try a drivers update, buy a new GPU or try software rendering on Linux; Godot will now crash with a segmentation fault."); - OS::get_singleton()->alert("Your system's graphic drivers seem not to support OpenGL 2.1 / OpenGL ES 2.0, sorry :(\n" - "Godot Engine will self-destruct as soon as you acknowledge this error message.", - "Fatal error: Insufficient OpenGL / GLES driver support"); + return ERR_UNAVAILABLE; } #ifdef GLES_OVER_GL @@ -183,15 +195,22 @@ void RasterizerGLES2::initialize() { glGetFramebufferAttachmentParameteriv = glGetFramebufferAttachmentParameterivEXT; glGenerateMipmap = glGenerateMipmapEXT; } else { - ERR_PRINT("Your system's graphic drivers seem not to support GL_ARB(EXT)_framebuffer_object OpenGL extension, sorry :(\n" - "Try a drivers update, buy a new GPU or try software rendering on Linux; Godot will now crash with a segmentation fault."); - OS::get_singleton()->alert("Your system's graphic drivers seem not to support GL_ARB(EXT)_framebuffer_object OpenGL extension, sorry :(\n" - "Godot Engine will self-destruct as soon as you acknowledge this error message.", - "Fatal error: Insufficient OpenGL / GLES driver support"); + return ERR_UNAVAILABLE; } } -#endif - if (true || OS::get_singleton()->is_stdout_verbose()) { +#endif // GLES_OVER_GL + +#endif // GLAD_ENABLED + + return OK; +} + +void RasterizerGLES2::initialize() { + + print_verbose("Using GLES2 video driver"); + +#ifdef GLAD_ENABLED + if (OS::get_singleton()->is_stdout_verbose()) { if (GLAD_GL_ARB_debug_output) { glEnable(_EXT_DEBUG_OUTPUT_SYNCHRONOUS_ARB); glDebugMessageCallbackARB(_gl_debug_print, NULL); @@ -200,12 +219,11 @@ void RasterizerGLES2::initialize() { print_line("OpenGL debugging not supported!"); } } - #endif // GLAD_ENABLED // For debugging #ifdef GLES_OVER_GL - if (GLAD_GL_ARB_debug_output) { + if (OS::get_singleton()->is_stdout_verbose() && GLAD_GL_ARB_debug_output) { glDebugMessageControlARB(_EXT_DEBUG_SOURCE_API_ARB, _EXT_DEBUG_TYPE_ERROR_ARB, _EXT_DEBUG_SEVERITY_HIGH_ARB, 0, NULL, GL_TRUE); glDebugMessageControlARB(_EXT_DEBUG_SOURCE_API_ARB, _EXT_DEBUG_TYPE_DEPRECATED_BEHAVIOR_ARB, _EXT_DEBUG_SEVERITY_HIGH_ARB, 0, NULL, GL_TRUE); glDebugMessageControlARB(_EXT_DEBUG_SOURCE_API_ARB, _EXT_DEBUG_TYPE_UNDEFINED_BEHAVIOR_ARB, _EXT_DEBUG_SEVERITY_HIGH_ARB, 0, NULL, GL_TRUE); @@ -218,7 +236,24 @@ void RasterizerGLES2::initialize() { GL_DEBUG_SEVERITY_HIGH_ARB, 5, "hello"); */ } -#endif +#else +#ifndef IPHONE_ENABLED + if (OS::get_singleton()->is_stdout_verbose()) { + DebugMessageCallbackARB callback = (DebugMessageCallbackARB)eglGetProcAddress("glDebugMessageCallback"); + if (!callback) { + callback = (DebugMessageCallbackARB)eglGetProcAddress("glDebugMessageCallbackKHR"); + } + + if (callback) { + + print_line("godot: ENABLING GL DEBUG"); + glEnable(_EXT_DEBUG_OUTPUT_SYNCHRONOUS_ARB); + callback(_gl_debug_print, NULL); + glEnable(_EXT_DEBUG_OUTPUT); + } + } +#endif // !IPHONE_ENABLED +#endif // GLES_OVER_GL const GLubyte *renderer = glGetString(GL_RENDERER); print_line("OpenGL ES 2.0 Renderer: " + String((const char *)renderer)); @@ -322,12 +357,28 @@ void RasterizerGLES2::set_boot_image(const Ref<Image> &p_image, const Color &p_c Rect2 imgrect(0, 0, p_image->get_width(), p_image->get_height()); Rect2 screenrect; + if (p_scale) { - screenrect = imgrect; - screenrect.position += ((Size2(window_w, window_h) - screenrect.size) / 2.0).floor(); + if (window_w > window_h) { + //scale horizontally + screenrect.size.y = window_h; + screenrect.size.x = imgrect.size.x * window_h / imgrect.size.y; + screenrect.position.x = (window_w - screenrect.size.x) / 2; + + } else { + //scale vertically + screenrect.size.x = window_w; + screenrect.size.y = imgrect.size.y * window_w / imgrect.size.x; + screenrect.position.y = (window_h - screenrect.size.y) / 2; + } + } else { + + screenrect = imgrect; + screenrect.position += ((Size2(window_w, window_h) - screenrect.size) / 2.0).floor(); + } RasterizerStorageGLES2::Texture *t = storage->texture_owner.get(texture); - glActiveTexture(GL_TEXTURE0); + glActiveTexture(GL_TEXTURE0 + storage->config.max_texture_image_units - 1); glBindTexture(GL_TEXTURE_2D, t->tex_id); canvas->draw_generic_textured_rect(screenrect, Rect2(0, 0, 1, 1)); glBindTexture(GL_TEXTURE_2D, 0); @@ -354,7 +405,7 @@ void RasterizerGLES2::blit_render_target_to_screen(RID p_render_target, const Re canvas->canvas_begin(); glDisable(GL_BLEND); glBindFramebuffer(GL_FRAMEBUFFER, RasterizerStorageGLES2::system_fbo); - glActiveTexture(GL_TEXTURE0); + glActiveTexture(GL_TEXTURE0 + storage->config.max_texture_image_units - 1); glBindTexture(GL_TEXTURE_2D, rt->color); // TODO normals @@ -365,6 +416,26 @@ void RasterizerGLES2::blit_render_target_to_screen(RID p_render_target, const Re canvas->canvas_end(); } +void RasterizerGLES2::output_lens_distorted_to_screen(RID p_render_target, const Rect2 &p_screen_rect, float p_k1, float p_k2, const Vector2 &p_eye_center, float p_oversample) { + ERR_FAIL_COND(storage->frame.current_rt); + + RasterizerStorageGLES2::RenderTarget *rt = storage->render_target_owner.getornull(p_render_target); + ERR_FAIL_COND(!rt); + + glDisable(GL_BLEND); + + // render to our framebuffer + glBindFramebuffer(GL_FRAMEBUFFER, RasterizerStorageGLES2::system_fbo); + + // output our texture + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, rt->color); + + canvas->draw_lens_distortion_rect(p_screen_rect, p_k1, p_k2, p_eye_center, p_oversample); + + glBindTexture(GL_TEXTURE_2D, 0); +} + void RasterizerGLES2::end_frame(bool p_swap_buffers) { if (OS::get_singleton()->is_layered_allowed()) { diff --git a/drivers/gles2/rasterizer_gles2.h b/drivers/gles2/rasterizer_gles2.h index f727af39dd..97f8ee7c1c 100644 --- a/drivers/gles2/rasterizer_gles2.h +++ b/drivers/gles2/rasterizer_gles2.h @@ -27,6 +27,7 @@ /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ + #ifndef RASTERIZERGLES2_H #define RASTERIZERGLES2_H @@ -58,12 +59,16 @@ public: virtual void restore_render_target(); virtual void clear_render_target(const Color &p_color); virtual void blit_render_target_to_screen(RID p_render_target, const Rect2 &p_screen_rect, int p_screen = 0); + virtual void output_lens_distorted_to_screen(RID p_render_target, const Rect2 &p_screen_rect, float p_k1, float p_k2, const Vector2 &p_eye_center, float p_oversample); virtual void end_frame(bool p_swap_buffers); virtual void finalize(); + static Error is_viable(); static void make_current(); - static void register_config(); + + virtual bool is_low_end() const { return true; } + RasterizerGLES2(); ~RasterizerGLES2(); }; diff --git a/drivers/gles2/rasterizer_scene_gles2.cpp b/drivers/gles2/rasterizer_scene_gles2.cpp index 5f31bfe209..25d7df8ebc 100644 --- a/drivers/gles2/rasterizer_scene_gles2.cpp +++ b/drivers/gles2/rasterizer_scene_gles2.cpp @@ -27,20 +27,23 @@ /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ + #include "rasterizer_scene_gles2.h" -#include "math/transform.h" -#include "math_funcs.h" -#include "os/os.h" -#include "project_settings.h" + +#include "core/math/math_funcs.h" +#include "core/math/transform.h" +#include "core/os/os.h" +#include "core/project_settings.h" +#include "core/vmap.h" #include "rasterizer_canvas_gles2.h" #include "servers/visual/visual_server_raster.h" -#include "vmap.h" - #ifndef GLES_OVER_GL #define glClearDepth glClearDepthf #endif +#define _DEPTH_COMPONENT24_OES 0x81A6 + static const GLenum _cube_side_enum[6] = { GL_TEXTURE_CUBE_MAP_NEGATIVE_X, @@ -107,10 +110,10 @@ void RasterizerSceneGLES2::shadow_atlas_set_size(RID p_atlas, int p_size) { glActiveTexture(GL_TEXTURE0); glGenTextures(1, &shadow_atlas->depth); glBindTexture(GL_TEXTURE_2D, shadow_atlas->depth); - glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT16, shadow_atlas->size, shadow_atlas->size, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, NULL); + glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, shadow_atlas->size, shadow_atlas->size, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, NULL); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); @@ -436,29 +439,203 @@ void RasterizerSceneGLES2::reflection_atlas_set_subdivision(RID p_ref_atlas, int //////////////////////////////////////////////////// RID RasterizerSceneGLES2::reflection_probe_instance_create(RID p_probe) { - return RID(); + + RasterizerStorageGLES2::ReflectionProbe *probe = storage->reflection_probe_owner.getornull(p_probe); + ERR_FAIL_COND_V(!probe, RID()); + + ReflectionProbeInstance *rpi = memnew(ReflectionProbeInstance); + + rpi->probe_ptr = probe; + rpi->self = reflection_probe_instance_owner.make_rid(rpi); + rpi->probe = p_probe; + rpi->reflection_atlas_index = -1; + rpi->render_step = -1; + rpi->last_pass = 0; + rpi->current_resolution = 0; + rpi->dirty = true; + + rpi->last_pass = 0; + rpi->index = 0; + + for (int i = 0; i < 6; i++) { + glGenFramebuffers(1, &rpi->fbo[i]); + } + + glGenFramebuffers(1, &rpi->fbo_blur); + glGenRenderbuffers(1, &rpi->depth); + rpi->cubemap = 0; + //glGenTextures(1, &rpi->cubemap); + + return rpi->self; } void RasterizerSceneGLES2::reflection_probe_instance_set_transform(RID p_instance, const Transform &p_transform) { + + ReflectionProbeInstance *rpi = reflection_probe_instance_owner.getornull(p_instance); + ERR_FAIL_COND(!rpi); + rpi->transform = p_transform; } void RasterizerSceneGLES2::reflection_probe_release_atlas_index(RID p_instance) { } bool RasterizerSceneGLES2::reflection_probe_instance_needs_redraw(RID p_instance) { - return false; + const ReflectionProbeInstance *rpi = reflection_probe_instance_owner.getornull(p_instance); + ERR_FAIL_COND_V(!rpi, false); + + bool need_redraw = rpi->probe_ptr->resolution != rpi->current_resolution || rpi->dirty || rpi->probe_ptr->update_mode == VS::REFLECTION_PROBE_UPDATE_ALWAYS; + rpi->dirty = false; + return need_redraw; } bool RasterizerSceneGLES2::reflection_probe_instance_has_reflection(RID p_instance) { - return false; + return true; } bool RasterizerSceneGLES2::reflection_probe_instance_begin_render(RID p_instance, RID p_reflection_atlas) { - return false; + + ReflectionProbeInstance *rpi = reflection_probe_instance_owner.getornull(p_instance); + ERR_FAIL_COND_V(!rpi, false); + + rpi->render_step = 0; + + if (rpi->probe_ptr->resolution != rpi->current_resolution) { + + //update cubemap if resolution changed + int size = rpi->probe_ptr->resolution; + rpi->current_resolution = size; + + GLenum internal_format = GL_RGB; + GLenum format = GL_RGB; + GLenum type = GL_UNSIGNED_BYTE; + + glActiveTexture(GL_TEXTURE0); + if (rpi->cubemap != 0) { + glDeleteTextures(1, &rpi->cubemap); + } + glGenTextures(1, &rpi->cubemap); + glBindTexture(GL_TEXTURE_CUBE_MAP, rpi->cubemap); +#if 1 + //Mobile hardware (PowerVR specially) prefers this approach, the other one kills the game + for (int i = 0; i < 6; i++) { + glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, internal_format, size, size, 0, format, type, NULL); + } + + glGenerateMipmap(GL_TEXTURE_CUBE_MAP); + + glBindRenderbuffer(GL_RENDERBUFFER, rpi->depth); //resize depth buffer + glRenderbufferStorage(GL_RENDERBUFFER, _DEPTH_COMPONENT24_OES, size, size); + + for (int i = 0; i < 6; i++) { + glBindFramebuffer(GL_FRAMEBUFFER, rpi->fbo[i]); + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, _cube_side_enum[i], rpi->cubemap, 0); + glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rpi->depth); + } + +#else + int lod = 0; + + //the approach below is fatal for powervr + + // Set the initial (empty) mipmaps, all need to be set for this to work in GLES2, even if later wont be used. + while (size >= 1) { + + for (int i = 0; i < 6; i++) { + glTexImage2D(_cube_side_enum[i], lod, internal_format, size, size, 0, format, type, NULL); + if (size == rpi->current_resolution) { + //adjust framebuffer + glBindFramebuffer(GL_FRAMEBUFFER, rpi->fbo[i]); + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, _cube_side_enum[i], rpi->cubemap, 0); + glBindRenderbuffer(GL_RENDERBUFFER, rpi->depth); + glRenderbufferStorage(GL_RENDERBUFFER, _DEPTH_COMPONENT24_OES, size, size); + glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rpi->depth); + +#ifdef DEBUG_ENABLED + GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER); + ERR_CONTINUE(status != GL_FRAMEBUFFER_COMPLETE); +#endif + } + } + + lod++; + + size >>= 1; + } +#endif + glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); + glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + } + + return true; } bool RasterizerSceneGLES2::reflection_probe_instance_postprocess_step(RID p_instance) { - return false; + + ReflectionProbeInstance *rpi = reflection_probe_instance_owner.getornull(p_instance); + ERR_FAIL_COND_V(!rpi, false); + + int size = rpi->probe_ptr->resolution; + + { + glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); + glDisable(GL_CULL_FACE); + glDisable(GL_DEPTH_TEST); + glDisable(GL_SCISSOR_TEST); + glDisable(GL_BLEND); + glDepthMask(GL_FALSE); + + for (int i = 0; i < VS::ARRAY_MAX - 1; i++) { + glDisableVertexAttribArray(i); + } + } + + //vdc cache + glActiveTexture(GL_TEXTURE1); + glBindTexture(GL_TEXTURE_2D, storage->resources.radical_inverse_vdc_cache_tex); + + glBindFramebuffer(GL_FRAMEBUFFER, rpi->fbo_blur); + // now render to the framebuffer, mipmap level for mipmap level + int lod = 1; + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_CUBE_MAP, rpi->cubemap); + glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR); //use linear, no mipmaps so it does not read from what is being written to + + size >>= 1; + int mipmaps = 6; + + storage->shaders.cubemap_filter.set_conditional(CubemapFilterShaderGLES2::USE_SOURCE_PANORAMA, false); + storage->shaders.cubemap_filter.bind(); + + //blur + while (size >= 1) { + + for (int i = 0; i < 6; i++) { + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, _cube_side_enum[i], rpi->cubemap, lod); + + glViewport(0, 0, size, size); + storage->bind_quad_array(); + storage->shaders.cubemap_filter.set_uniform(CubemapFilterShaderGLES2::FACE_ID, i); + float roughness = CLAMP(lod / (float)(mipmaps - 1), 0, 1); + storage->shaders.cubemap_filter.set_uniform(CubemapFilterShaderGLES2::ROUGHNESS, roughness); + storage->shaders.cubemap_filter.set_uniform(CubemapFilterShaderGLES2::Z_FLIP, false); + + glDrawArrays(GL_TRIANGLE_FAN, 0, 4); + } + + size >>= 1; + + lod++; + } + + // restore ranges + + glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); + glBindFramebuffer(GL_FRAMEBUFFER, 0); + + return true; } /* ENVIRONMENT API */ @@ -531,7 +708,7 @@ void RasterizerSceneGLES2::environment_set_dof_blur_near(RID p_env, bool p_enabl ERR_FAIL_COND(!env); } -void RasterizerSceneGLES2::environment_set_glow(RID p_env, bool p_enable, int p_level_flags, float p_intensity, float p_strength, float p_bloom_threshold, VS::EnvironmentGlowBlendMode p_blend_mode, float p_hdr_bleed_threshold, float p_hdr_bleed_scale, bool p_bicubic_upscale) { +void RasterizerSceneGLES2::environment_set_glow(RID p_env, bool p_enable, int p_level_flags, float p_intensity, float p_strength, float p_bloom_threshold, VS::EnvironmentGlowBlendMode p_blend_mode, float p_hdr_bleed_threshold, float p_hdr_bleed_scale, float p_hdr_luminance_cap, bool p_bicubic_upscale) { Environment *env = environment_owner.getornull(p_env); ERR_FAIL_COND(!env); } @@ -562,20 +739,39 @@ void RasterizerSceneGLES2::environment_set_adjustment(RID p_env, bool p_enable, } void RasterizerSceneGLES2::environment_set_fog(RID p_env, bool p_enable, const Color &p_color, const Color &p_sun_color, float p_sun_amount) { + Environment *env = environment_owner.getornull(p_env); ERR_FAIL_COND(!env); + + env->fog_enabled = p_enable; + env->fog_color = p_color; + env->fog_sun_color = p_sun_color; + env->fog_sun_amount = p_sun_amount; } -void RasterizerSceneGLES2::environment_set_fog_depth(RID p_env, bool p_enable, float p_depth_begin, float p_depth_curve, bool p_transmit, float p_transmit_curve) { +void RasterizerSceneGLES2::environment_set_fog_depth(RID p_env, bool p_enable, float p_depth_begin, float p_depth_end, float p_depth_curve, bool p_transmit, float p_transmit_curve) { + Environment *env = environment_owner.getornull(p_env); ERR_FAIL_COND(!env); + + env->fog_depth_enabled = p_enable; + env->fog_depth_begin = p_depth_begin; + env->fog_depth_end = p_depth_end; + env->fog_depth_curve = p_depth_curve; + env->fog_transmit_enabled = p_transmit; + env->fog_transmit_curve = p_transmit_curve; } void RasterizerSceneGLES2::environment_set_fog_height(RID p_env, bool p_enable, float p_min_height, float p_max_height, float p_height_curve) { + Environment *env = environment_owner.getornull(p_env); ERR_FAIL_COND(!env); -} + env->fog_height_enabled = p_enable; + env->fog_height_min = p_min_height; + env->fog_height_max = p_max_height; + env->fog_height_curve = p_height_curve; +} bool RasterizerSceneGLES2::is_environment(RID p_env) { return environment_owner.owns(p_env); } @@ -603,6 +799,8 @@ RID RasterizerSceneGLES2::light_instance_create(RID p_light) { light_instance->light = p_light; light_instance->light_ptr = storage->light_owner.getornull(p_light); + light_instance->light_index = 0xFFFF; + ERR_FAIL_COND_V(!light_instance->light_ptr, RID()); light_instance->self = light_instance_owner.make_rid(light_instance); @@ -708,9 +906,39 @@ void RasterizerSceneGLES2::_add_geometry_with_material(RasterizerStorageGLES2::G bool has_blend_alpha = p_material->shader->spatial.blend_mode != RasterizerStorageGLES2::Shader::Spatial::BLEND_MODE_MIX; bool has_alpha = has_base_alpha || has_blend_alpha; - // TODO add this stuff - // bool mirror = p_instance->mirror; - // bool no_cull = false; + bool mirror = p_instance->mirror; + + if (p_material->shader->spatial.cull_mode == RasterizerStorageGLES2::Shader::Spatial::CULL_MODE_DISABLED) { + mirror = false; + } else if (p_material->shader->spatial.cull_mode == RasterizerStorageGLES2::Shader::Spatial::CULL_MODE_FRONT) { + mirror = !mirror; + } + + //if (p_material->shader->spatial.uses_sss) { + // state.used_sss = true; + //} + + if (p_material->shader->spatial.uses_screen_texture) { + state.used_screen_texture = true; + } + + if (p_depth_pass) { + + if (has_blend_alpha || p_material->shader->spatial.uses_depth_texture || (has_base_alpha && p_material->shader->spatial.depth_draw_mode != RasterizerStorageGLES2::Shader::Spatial::DEPTH_DRAW_ALPHA_PREPASS)) + return; //bye + + if (!p_material->shader->spatial.uses_alpha_scissor && !p_material->shader->spatial.writes_modelview_or_projection && !p_material->shader->spatial.uses_vertex && !p_material->shader->spatial.uses_discard && p_material->shader->spatial.depth_draw_mode != RasterizerStorageGLES2::Shader::Spatial::DEPTH_DRAW_ALPHA_PREPASS) { + //shader does not use discard and does not write a vertex position, use generic material + if (p_instance->cast_shadows == VS::SHADOW_CASTING_SETTING_DOUBLE_SIDED) { + p_material = storage->material_owner.getptr(!p_shadow_pass && p_material->shader->spatial.uses_world_coordinates ? default_worldcoord_material_twosided : default_material_twosided); + mirror = false; + } else { + p_material = storage->material_owner.getptr(!p_shadow_pass && p_material->shader->spatial.uses_world_coordinates ? default_worldcoord_material : default_material); + } + } + + has_alpha = false; + } RenderList::Element *e = has_alpha ? render_list.add_alpha_element() : render_list.add_element(); @@ -723,46 +951,130 @@ void RasterizerSceneGLES2::_add_geometry_with_material(RasterizerStorageGLES2::G e->instance = p_instance; e->owner = p_owner; e->sort_key = 0; + e->depth_key = 0; + e->use_accum = false; + e->light_index = RenderList::MAX_LIGHTS; + e->use_accum_ptr = &e->use_accum; + e->instancing = (e->instance->base_type == VS::INSTANCE_MULTIMESH) ? 1 : 0; + + if (e->geometry->last_pass != render_pass) { + e->geometry->last_pass = render_pass; + e->geometry->index = current_geometry_index++; + } - // TODO check render pass of geometry - - // TODO check directional light flag + e->geometry_index = e->geometry->index; - if (p_depth_pass) { - // if we are in the depth pass we can sort out a few things to improve performance + if (e->material->last_pass != render_pass) { + e->material->last_pass = render_pass; + e->material->index = current_material_index++; - if (has_blend_alpha || p_material->shader->spatial.uses_depth_texture || (has_base_alpha && p_material->shader->spatial.depth_draw_mode != RasterizerStorageGLES2::Shader::Spatial::DEPTH_DRAW_ALPHA_PREPASS)) { - return; + if (e->material->shader->last_pass != render_pass) { + e->material->shader->index = current_shader_index++; } + } - if (p_material->shader->spatial.uses_alpha_scissor && !p_material->shader->spatial.writes_modelview_or_projection && !p_material->shader->spatial.uses_vertex && !p_material->shader->spatial.uses_discard && p_material->shader->spatial.depth_draw_mode != RasterizerStorageGLES2::Shader::Spatial::DEPTH_DRAW_ALPHA_PREPASS) { + e->material_index = e->material->index; - // shader doesn't use discard or writes a custom vertex position, - // so we can use a stripped down shader instead + e->refprobe_0_index = RenderList::MAX_REFLECTION_PROBES; //refprobe disabled by default + e->refprobe_1_index = RenderList::MAX_REFLECTION_PROBES; //refprobe disabled by default - // TODO twosided and worldcoord stuff + if (!p_depth_pass) { - p_material = storage->material_owner.getptr(default_material_twosided); + e->depth_layer = e->instance->depth_layer; + e->priority = p_material->render_priority; + + int rpsize = e->instance->reflection_probe_instances.size(); + if (rpsize > 0) { + bool first = true; + rpsize = MIN(rpsize, 2); //more than 2 per object are not supported, this keeps it stable + + for (int i = 0; i < rpsize; i++) { + ReflectionProbeInstance *rpi = reflection_probe_instance_owner.getornull(e->instance->reflection_probe_instances[i]); + if (rpi->last_pass != render_pass) { + continue; + } + if (first) { + e->refprobe_0_index = rpi->index; + first = false; + } else { + e->refprobe_1_index = rpi->index; + break; + } + } + + /* if (e->refprobe_0_index > e->refprobe_1_index) { //if both are valid, swap them to keep order as best as possible + uint64_t tmp = e->refprobe_0_index; + e->refprobe_0_index = e->refprobe_1_index; + e->refprobe_1_index = tmp; + }*/ } - has_alpha = false; - } + //add directional lights - e->sort_key |= uint64_t(e->geometry->index) << RenderList::SORT_KEY_GEOMETRY_INDEX_SHIFT; - e->sort_key |= uint64_t(e->instance->base_type) << RenderList::SORT_KEY_GEOMETRY_TYPE_SHIFT; + if (p_material->shader->spatial.unshaded) { + e->light_mode = LIGHTMODE_UNSHADED; + } else { - if (p_material->shader->spatial.unshaded) { - e->sort_key |= SORT_KEY_UNSHADED_FLAG; - } + bool copy = false; - if (!p_depth_pass) { - e->sort_key |= uint64_t(e->material->index) << RenderList::SORT_KEY_MATERIAL_INDEX_SHIFT; + for (int i = 0; i < render_directional_lights; i++) { - e->sort_key |= uint64_t(p_material->render_priority + 128) << RenderList::SORT_KEY_PRIORITY_SHIFT; - } else { - // TODO + if (copy) { + RenderList::Element *e2 = has_alpha ? render_list.add_alpha_element() : render_list.add_element(); + if (!e2) { + break; + } + *e2 = *e; //this includes accum ptr :) + e = e2; + } + + //directional sort key + e->light_type1 = 0; + e->light_type2 = 1; + e->light_index = i; + + copy = true; + } + + //add omni / spots + + for (int i = 0; i < e->instance->light_instances.size(); i++) { + + LightInstance *li = light_instance_owner.getornull(e->instance->light_instances[i]); + + if (li->light_index >= render_light_instance_count) { + continue; // too many + } + + if (copy) { + RenderList::Element *e2 = has_alpha ? render_list.add_alpha_element() : render_list.add_element(); + if (!e2) { + break; + } + *e2 = *e; //this includes accum ptr :) + e = e2; + } + + //directional sort key + e->light_type1 = 1; + e->light_type2 = li->light_ptr->type == VisualServer::LIGHT_OMNI ? 0 : 1; + e->light_index = li->light_index; + + copy = true; + } + + if (e->instance->lightmap.is_valid()) { + e->light_mode = LIGHTMODE_LIGHTMAP; + } else if (!e->instance->lightmap_capture_data.empty()) { + e->light_mode = LIGHTMODE_LIGHTMAP_CAPTURE; + } else { + e->light_mode = LIGHTMODE_NORMAL; + } + } } + // do not add anything here, as lights are duplicated elements.. + if (p_material->shader->spatial.uses_time) { VisualServerRaster::redraw_request(); } @@ -770,6 +1082,13 @@ void RasterizerSceneGLES2::_add_geometry_with_material(RasterizerStorageGLES2::G void RasterizerSceneGLES2::_fill_render_list(InstanceBase **p_cull_result, int p_cull_count, bool p_depth_pass, bool p_shadow_pass) { + render_pass++; + current_material_index = 0; + current_geometry_index = 0; + current_light_index = 0; + current_refprobe_index = 0; + current_shader_index = 0; + for (int i = 0; i < p_cull_count; i++) { InstanceBase *instance = p_cull_result[i]; @@ -812,9 +1131,15 @@ void RasterizerSceneGLES2::_fill_render_list(InstanceBase **p_cull_result, int p } } break; - default: { + case VS::INSTANCE_IMMEDIATE: { + RasterizerStorageGLES2::Immediate *im = storage->immediate_owner.getptr(instance->base); + ERR_CONTINUE(!im); + + _add_geometry(im, instance, NULL, -1, p_depth_pass, p_shadow_pass); } break; + + default: {} } } } @@ -829,13 +1154,13 @@ static const GLenum gl_primitive[] = { GL_TRIANGLE_FAN }; -void RasterizerSceneGLES2::_setup_material(RasterizerStorageGLES2::Material *p_material, bool p_reverse_cull, Size2i p_skeleton_tex_size) { +bool RasterizerSceneGLES2::_setup_material(RasterizerStorageGLES2::Material *p_material, bool p_reverse_cull, bool p_alpha_pass, Size2i p_skeleton_tex_size) { // material parameters state.scene_shader.set_custom_shader(p_material->shader->custom_code_id); - state.scene_shader.bind(); + bool shader_rebind = state.scene_shader.bind(); if (p_material->shader->spatial.no_depth_test) { glDisable(GL_DEPTH_TEST); @@ -843,6 +1168,20 @@ void RasterizerSceneGLES2::_setup_material(RasterizerStorageGLES2::Material *p_m glEnable(GL_DEPTH_TEST); } + switch (p_material->shader->spatial.depth_draw_mode) { + case RasterizerStorageGLES2::Shader::Spatial::DEPTH_DRAW_ALPHA_PREPASS: + case RasterizerStorageGLES2::Shader::Spatial::DEPTH_DRAW_OPAQUE: { + + glDepthMask(!p_alpha_pass); + } break; + case RasterizerStorageGLES2::Shader::Spatial::DEPTH_DRAW_ALWAYS: { + glDepthMask(GL_TRUE); + } break; + case RasterizerStorageGLES2::Shader::Spatial::DEPTH_DRAW_NEVER: { + glDepthMask(GL_FALSE); + } break; + } + // TODO whyyyyy???? p_reverse_cull = true; @@ -868,6 +1207,8 @@ void RasterizerSceneGLES2::_setup_material(RasterizerStorageGLES2::Material *p_m state.scene_shader.set_uniform(SceneShaderGLES2::SKELETON_TEXTURE_SIZE, p_skeleton_tex_size); + state.current_main_tex = 0; + for (int i = 0; i < tc; i++) { glActiveTexture(GL_TEXTURE0 + i); @@ -898,198 +1239,183 @@ void RasterizerSceneGLES2::_setup_material(RasterizerStorageGLES2::Material *p_m t = t->get_ptr(); glBindTexture(t->target, t->tex_id); + if (i == 0) { + state.current_main_tex = t->tex_id; + } } state.scene_shader.use_material((void *)p_material); + + return shader_rebind; } void RasterizerSceneGLES2::_setup_geometry(RenderList::Element *p_element, RasterizerStorageGLES2::Skeleton *p_skeleton) { - state.scene_shader.set_conditional(SceneShaderGLES2::USE_SKELETON, p_skeleton != NULL); - // state.scene_shader.set_conditional(SceneShaderGLES2::USE_SKELETON_SOFTWARE, !storage->config.float_texture_supported); - state.scene_shader.set_conditional(SceneShaderGLES2::USE_SKELETON_SOFTWARE, true); - switch (p_element->instance->base_type) { case VS::INSTANCE_MESH: { RasterizerStorageGLES2::Surface *s = static_cast<RasterizerStorageGLES2::Surface *>(p_element->geometry); - state.scene_shader.set_conditional(SceneShaderGLES2::USE_INSTANCING, false); - state.scene_shader.set_conditional(SceneShaderGLES2::ENABLE_COLOR_INTERP, s->attribs[VS::ARRAY_COLOR].enabled); - state.scene_shader.set_conditional(SceneShaderGLES2::ENABLE_UV_INTERP, s->attribs[VS::ARRAY_TEX_UV].enabled); - state.scene_shader.set_conditional(SceneShaderGLES2::ENABLE_UV2_INTERP, s->attribs[VS::ARRAY_TEX_UV2].enabled); - - } break; - - case VS::INSTANCE_MULTIMESH: { - RasterizerStorageGLES2::MultiMesh *multi_mesh = static_cast<RasterizerStorageGLES2::MultiMesh *>(p_element->owner); - RasterizerStorageGLES2::Surface *s = static_cast<RasterizerStorageGLES2::Surface *>(p_element->geometry); - - state.scene_shader.set_conditional(SceneShaderGLES2::ENABLE_COLOR_INTERP, true); - state.scene_shader.set_conditional(SceneShaderGLES2::USE_INSTANCING, true); - - state.scene_shader.set_conditional(SceneShaderGLES2::ENABLE_UV_INTERP, s->attribs[VS::ARRAY_TEX_UV].enabled); - state.scene_shader.set_conditional(SceneShaderGLES2::ENABLE_UV2_INTERP, s->attribs[VS::ARRAY_TEX_UV2].enabled); - } break; - - default: { - - } break; - } - - if (false && storage->config.float_texture_supported) { - if (p_skeleton) { - glActiveTexture(GL_TEXTURE4); - glBindTexture(GL_TEXTURE_2D, p_skeleton->tex_id); - } - - return; - } - - if (p_skeleton) { - ERR_FAIL_COND(p_skeleton->use_2d); - - PoolVector<float> &transform_buffer = storage->resources.skeleton_transform_cpu_buffer; - - switch (p_element->instance->base_type) { - case VS::INSTANCE_MESH: { - RasterizerStorageGLES2::Surface *s = static_cast<RasterizerStorageGLES2::Surface *>(p_element->geometry); + glBindBuffer(GL_ARRAY_BUFFER, s->vertex_id); - if (!s->attribs[VS::ARRAY_BONES].enabled || !s->attribs[VS::ARRAY_WEIGHTS].enabled) { - break; // the whole instance has a skeleton, but this surface is not affected by it. - } + if (s->index_array_len > 0) { + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, s->index_id); + } - // 3 * vec4 per vertex - if (transform_buffer.size() < s->array_len * 12) { - transform_buffer.resize(s->array_len * 12); + for (int i = 0; i < VS::ARRAY_MAX - 1; i++) { + if (s->attribs[i].enabled) { + glEnableVertexAttribArray(i); + glVertexAttribPointer(s->attribs[i].index, s->attribs[i].size, s->attribs[i].type, s->attribs[i].normalized, s->attribs[i].stride, (uint8_t *)0 + s->attribs[i].offset); + } else { + glDisableVertexAttribArray(i); + switch (i) { + case VS::ARRAY_NORMAL: { + glVertexAttrib4f(VS::ARRAY_NORMAL, 0.0, 0.0, 1, 1); + } break; + case VS::ARRAY_COLOR: { + glVertexAttrib4f(VS::ARRAY_COLOR, 1, 1, 1, 1); + + } break; + default: {} + } } + } - const size_t bones_offset = s->attribs[VS::ARRAY_BONES].offset; - const size_t bones_stride = s->attribs[VS::ARRAY_BONES].stride; - const size_t bone_weight_offset = s->attribs[VS::ARRAY_WEIGHTS].offset; - const size_t bone_weight_stride = s->attribs[VS::ARRAY_WEIGHTS].stride; - - { - PoolVector<float>::Write write = transform_buffer.write(); - float *buffer = write.ptr(); + bool clear_skeleton_buffer = !storage->config.float_texture_supported; - PoolVector<uint8_t>::Read vertex_array_read = s->data.read(); - const uint8_t *vertex_data = vertex_array_read.ptr(); + if (p_skeleton) { - for (int i = 0; i < s->array_len; i++) { + if (storage->config.float_texture_supported) { + //use float texture workflow + glActiveTexture(GL_TEXTURE0 + storage->config.max_texture_image_units - 1); + glBindTexture(GL_TEXTURE_2D, p_skeleton->tex_id); + } else { + //use transform buffer workflow + ERR_FAIL_COND(p_skeleton->use_2d); - // do magic + PoolVector<float> &transform_buffer = storage->resources.skeleton_transform_cpu_buffer; - size_t bones[4]; - float bone_weight[4]; + if (!s->attribs[VS::ARRAY_BONES].enabled || !s->attribs[VS::ARRAY_WEIGHTS].enabled) { + break; // the whole instance has a skeleton, but this surface is not affected by it. + } - if (s->attribs[VS::ARRAY_BONES].type == GL_UNSIGNED_BYTE) { - // read as byte - const uint8_t *bones_ptr = vertex_data + bones_offset + (i * bones_stride); - bones[0] = bones_ptr[0]; - bones[1] = bones_ptr[1]; - bones[2] = bones_ptr[2]; - bones[3] = bones_ptr[3]; - } else { - // read as short - const uint16_t *bones_ptr = (const uint16_t *)(vertex_data + bones_offset + (i * bones_stride)); - bones[0] = bones_ptr[0]; - bones[1] = bones_ptr[1]; - bones[2] = bones_ptr[2]; - bones[3] = bones_ptr[3]; - } + // 3 * vec4 per vertex + if (transform_buffer.size() < s->array_len * 12) { + transform_buffer.resize(s->array_len * 12); + } - if (s->attribs[VS::ARRAY_WEIGHTS].type == GL_FLOAT) { - // read as float - const float *weight_ptr = (const float *)(vertex_data + bone_weight_offset + (i * bone_weight_stride)); - bone_weight[0] = weight_ptr[0]; - bone_weight[1] = weight_ptr[1]; - bone_weight[2] = weight_ptr[2]; - bone_weight[3] = weight_ptr[3]; - } else { - // read as half - const uint16_t *weight_ptr = (const uint16_t *)(vertex_data + bone_weight_offset + (i * bone_weight_stride)); - bone_weight[0] = (weight_ptr[0] / (float)0xFFFF); - bone_weight[1] = (weight_ptr[1] / (float)0xFFFF); - bone_weight[2] = (weight_ptr[2] / (float)0xFFFF); - bone_weight[3] = (weight_ptr[3] / (float)0xFFFF); + const size_t bones_offset = s->attribs[VS::ARRAY_BONES].offset; + const size_t bones_stride = s->attribs[VS::ARRAY_BONES].stride; + const size_t bone_weight_offset = s->attribs[VS::ARRAY_WEIGHTS].offset; + const size_t bone_weight_stride = s->attribs[VS::ARRAY_WEIGHTS].stride; + + { + PoolVector<float>::Write write = transform_buffer.write(); + float *buffer = write.ptr(); + + PoolVector<uint8_t>::Read vertex_array_read = s->data.read(); + const uint8_t *vertex_data = vertex_array_read.ptr(); + + for (int i = 0; i < s->array_len; i++) { + + // do magic + + size_t bones[4]; + float bone_weight[4]; + + if (s->attribs[VS::ARRAY_BONES].type == GL_UNSIGNED_BYTE) { + // read as byte + const uint8_t *bones_ptr = vertex_data + bones_offset + (i * bones_stride); + bones[0] = bones_ptr[0]; + bones[1] = bones_ptr[1]; + bones[2] = bones_ptr[2]; + bones[3] = bones_ptr[3]; + } else { + // read as short + const uint16_t *bones_ptr = (const uint16_t *)(vertex_data + bones_offset + (i * bones_stride)); + bones[0] = bones_ptr[0]; + bones[1] = bones_ptr[1]; + bones[2] = bones_ptr[2]; + bones[3] = bones_ptr[3]; + } + + if (s->attribs[VS::ARRAY_WEIGHTS].type == GL_FLOAT) { + // read as float + const float *weight_ptr = (const float *)(vertex_data + bone_weight_offset + (i * bone_weight_stride)); + bone_weight[0] = weight_ptr[0]; + bone_weight[1] = weight_ptr[1]; + bone_weight[2] = weight_ptr[2]; + bone_weight[3] = weight_ptr[3]; + } else { + // read as half + const uint16_t *weight_ptr = (const uint16_t *)(vertex_data + bone_weight_offset + (i * bone_weight_stride)); + bone_weight[0] = (weight_ptr[0] / (float)0xFFFF); + bone_weight[1] = (weight_ptr[1] / (float)0xFFFF); + bone_weight[2] = (weight_ptr[2] / (float)0xFFFF); + bone_weight[3] = (weight_ptr[3] / (float)0xFFFF); + } + + Transform transform; + + Transform bone_transforms[4] = { + storage->skeleton_bone_get_transform(p_element->instance->skeleton, bones[0]), + storage->skeleton_bone_get_transform(p_element->instance->skeleton, bones[1]), + storage->skeleton_bone_get_transform(p_element->instance->skeleton, bones[2]), + storage->skeleton_bone_get_transform(p_element->instance->skeleton, bones[3]), + }; + + transform.origin = + bone_weight[0] * bone_transforms[0].origin + + bone_weight[1] * bone_transforms[1].origin + + bone_weight[2] * bone_transforms[2].origin + + bone_weight[3] * bone_transforms[3].origin; + + transform.basis = + bone_transforms[0].basis * bone_weight[0] + + bone_transforms[1].basis * bone_weight[1] + + bone_transforms[2].basis * bone_weight[2] + + bone_transforms[3].basis * bone_weight[3]; + + float row[3][4] = { + { transform.basis[0][0], transform.basis[0][1], transform.basis[0][2], transform.origin[0] }, + { transform.basis[1][0], transform.basis[1][1], transform.basis[1][2], transform.origin[1] }, + { transform.basis[2][0], transform.basis[2][1], transform.basis[2][2], transform.origin[2] }, + }; + + size_t transform_buffer_offset = i * 12; + + copymem(&buffer[transform_buffer_offset], row, sizeof(row)); } + } - size_t offset = i * 12; - - Transform transform; - - Transform bone_transforms[4] = { - storage->skeleton_bone_get_transform(p_element->instance->skeleton, bones[0]), - storage->skeleton_bone_get_transform(p_element->instance->skeleton, bones[1]), - storage->skeleton_bone_get_transform(p_element->instance->skeleton, bones[2]), - storage->skeleton_bone_get_transform(p_element->instance->skeleton, bones[3]), - }; - - transform.origin = - bone_weight[0] * bone_transforms[0].origin + - bone_weight[1] * bone_transforms[1].origin + - bone_weight[2] * bone_transforms[2].origin + - bone_weight[3] * bone_transforms[3].origin; + storage->_update_skeleton_transform_buffer(transform_buffer, s->array_len * 12); - transform.basis = - bone_transforms[0].basis * bone_weight[0] + - bone_transforms[1].basis * bone_weight[1] + - bone_transforms[2].basis * bone_weight[2] + - bone_transforms[3].basis * bone_weight[3]; + //enable transform buffer and bind it + glBindBuffer(GL_ARRAY_BUFFER, storage->resources.skeleton_transform_buffer); - float row[3][4] = { - { transform.basis[0][0], transform.basis[0][1], transform.basis[0][2], transform.origin[0] }, - { transform.basis[1][0], transform.basis[1][1], transform.basis[1][2], transform.origin[1] }, - { transform.basis[2][0], transform.basis[2][1], transform.basis[2][2], transform.origin[2] }, - }; + glEnableVertexAttribArray(INSTANCE_BONE_BASE + 0); + glEnableVertexAttribArray(INSTANCE_BONE_BASE + 1); + glEnableVertexAttribArray(INSTANCE_BONE_BASE + 2); - size_t transform_buffer_offset = i * 12; + glVertexAttribPointer(INSTANCE_BONE_BASE + 0, 4, GL_FLOAT, GL_FALSE, sizeof(float) * 12, (const void *)(sizeof(float) * 4 * 0)); + glVertexAttribPointer(INSTANCE_BONE_BASE + 1, 4, GL_FLOAT, GL_FALSE, sizeof(float) * 12, (const void *)(sizeof(float) * 4 * 1)); + glVertexAttribPointer(INSTANCE_BONE_BASE + 2, 4, GL_FLOAT, GL_FALSE, sizeof(float) * 12, (const void *)(sizeof(float) * 4 * 2)); - copymem(&buffer[transform_buffer_offset], row, sizeof(row)); - } + clear_skeleton_buffer = false; } + } - storage->_update_skeleton_transform_buffer(transform_buffer, s->array_len * 12); - } break; - - default: { - - } break; - } - } -} - -void RasterizerSceneGLES2::_render_geometry(RenderList::Element *p_element) { + if (clear_skeleton_buffer) { - switch (p_element->instance->base_type) { + glDisableVertexAttribArray(INSTANCE_BONE_BASE + 0); + glDisableVertexAttribArray(INSTANCE_BONE_BASE + 1); + glDisableVertexAttribArray(INSTANCE_BONE_BASE + 2); + } - case VS::INSTANCE_MESH: { + } break; + case VS::INSTANCE_MULTIMESH: { RasterizerStorageGLES2::Surface *s = static_cast<RasterizerStorageGLES2::Surface *>(p_element->geometry); - // set up - - if (p_element->instance->skeleton.is_valid() && s->attribs[VS::ARRAY_BONES].enabled && s->attribs[VS::ARRAY_WEIGHTS].enabled) { - glBindBuffer(GL_ARRAY_BUFFER, storage->resources.skeleton_transform_buffer); - - glEnableVertexAttribArray(VS::ARRAY_MAX + 0); - glEnableVertexAttribArray(VS::ARRAY_MAX + 1); - glEnableVertexAttribArray(VS::ARRAY_MAX + 2); - - glVertexAttribPointer(VS::ARRAY_MAX + 0, 4, GL_FLOAT, GL_FALSE, sizeof(float) * 12, (const void *)(sizeof(float) * 4 * 0)); - glVertexAttribPointer(VS::ARRAY_MAX + 1, 4, GL_FLOAT, GL_FALSE, sizeof(float) * 12, (const void *)(sizeof(float) * 4 * 1)); - glVertexAttribPointer(VS::ARRAY_MAX + 2, 4, GL_FLOAT, GL_FALSE, sizeof(float) * 12, (const void *)(sizeof(float) * 4 * 2)); - } else { - // just to make sure - glDisableVertexAttribArray(VS::ARRAY_MAX + 0); - glDisableVertexAttribArray(VS::ARRAY_MAX + 1); - glDisableVertexAttribArray(VS::ARRAY_MAX + 2); - - glVertexAttrib4f(VS::ARRAY_MAX + 0, 1, 0, 0, 0); - glVertexAttrib4f(VS::ARRAY_MAX + 1, 0, 1, 0, 0); - glVertexAttrib4f(VS::ARRAY_MAX + 2, 0, 0, 1, 0); - } - glBindBuffer(GL_ARRAY_BUFFER, s->vertex_id); if (s->index_array_len > 0) { @@ -1102,61 +1428,58 @@ void RasterizerSceneGLES2::_render_geometry(RenderList::Element *p_element) { glVertexAttribPointer(s->attribs[i].index, s->attribs[i].size, s->attribs[i].type, s->attribs[i].normalized, s->attribs[i].stride, (uint8_t *)0 + s->attribs[i].offset); } else { glDisableVertexAttribArray(i); + switch (i) { + case VS::ARRAY_NORMAL: { + glVertexAttrib4f(VS::ARRAY_NORMAL, 0.0, 0.0, 1, 1); + } break; + case VS::ARRAY_COLOR: { + glVertexAttrib4f(VS::ARRAY_COLOR, 1, 1, 1, 1); + + } break; + default: {} + } } } - // drawing + // prepare multimesh (disable) + glDisableVertexAttribArray(INSTANCE_ATTRIB_BASE + 0); + glDisableVertexAttribArray(INSTANCE_ATTRIB_BASE + 1); + glDisableVertexAttribArray(INSTANCE_ATTRIB_BASE + 2); + glDisableVertexAttribArray(INSTANCE_ATTRIB_BASE + 3); + glDisableVertexAttribArray(INSTANCE_ATTRIB_BASE + 4); + glDisableVertexAttribArray(INSTANCE_BONE_BASE + 0); + glDisableVertexAttribArray(INSTANCE_BONE_BASE + 1); + glDisableVertexAttribArray(INSTANCE_BONE_BASE + 2); - if (s->index_array_len > 0) { - glDrawElements(gl_primitive[s->primitive], s->index_array_len, (s->array_len >= (1 << 16)) ? GL_UNSIGNED_INT : GL_UNSIGNED_SHORT, 0); - } else { - glDrawArrays(gl_primitive[s->primitive], 0, s->array_len); - } - - // tear down - - for (int i = 0; i < VS::ARRAY_MAX - 1; i++) { - glDisableVertexAttribArray(i); - } - - if (s->index_array_len > 0) { - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); - } + } break; - if (p_element->instance->skeleton.is_valid() && s->attribs[VS::ARRAY_BONES].enabled && s->attribs[VS::ARRAY_WEIGHTS].enabled) { - glBindBuffer(GL_ARRAY_BUFFER, storage->resources.skeleton_transform_buffer); + case VS::INSTANCE_IMMEDIATE: { + } break; - glDisableVertexAttribArray(VS::ARRAY_MAX + 0); - glDisableVertexAttribArray(VS::ARRAY_MAX + 1); - glDisableVertexAttribArray(VS::ARRAY_MAX + 2); - } + default: {} + } +} - glBindBuffer(GL_ARRAY_BUFFER, 0); +void RasterizerSceneGLES2::_render_geometry(RenderList::Element *p_element) { - } break; + switch (p_element->instance->base_type) { - case VS::INSTANCE_MULTIMESH: { + case VS::INSTANCE_MESH: { - RasterizerStorageGLES2::MultiMesh *multi_mesh = static_cast<RasterizerStorageGLES2::MultiMesh *>(p_element->owner); RasterizerStorageGLES2::Surface *s = static_cast<RasterizerStorageGLES2::Surface *>(p_element->geometry); - int amount = MIN(multi_mesh->size, multi_mesh->visible_instances); - if (amount == -1) { - amount = multi_mesh->size; - } + // drawing + if (s->index_array_len > 0) { + glDrawElements(gl_primitive[s->primitive], s->index_array_len, (s->array_len >= (1 << 16)) ? GL_UNSIGNED_INT : GL_UNSIGNED_SHORT, 0); + } else { + glDrawArrays(gl_primitive[s->primitive], 0, s->array_len); + } + /* if (p_element->instance->skeleton.is_valid() && s->attribs[VS::ARRAY_BONES].enabled && s->attribs[VS::ARRAY_WEIGHTS].enabled) { + //clean up after skeleton glBindBuffer(GL_ARRAY_BUFFER, storage->resources.skeleton_transform_buffer); - glEnableVertexAttribArray(VS::ARRAY_MAX + 0); - glEnableVertexAttribArray(VS::ARRAY_MAX + 1); - glEnableVertexAttribArray(VS::ARRAY_MAX + 2); - - glVertexAttribPointer(VS::ARRAY_MAX + 0, 4, GL_FLOAT, GL_FALSE, sizeof(float) * 12, (const void *)(sizeof(float) * 4 * 0)); - glVertexAttribPointer(VS::ARRAY_MAX + 1, 4, GL_FLOAT, GL_FALSE, sizeof(float) * 12, (const void *)(sizeof(float) * 4 * 1)); - glVertexAttribPointer(VS::ARRAY_MAX + 2, 4, GL_FLOAT, GL_FALSE, sizeof(float) * 12, (const void *)(sizeof(float) * 4 * 2)); - } else { - // just to make sure glDisableVertexAttribArray(VS::ARRAY_MAX + 0); glDisableVertexAttribArray(VS::ARRAY_MAX + 1); glDisableVertexAttribArray(VS::ARRAY_MAX + 2); @@ -1165,30 +1488,19 @@ void RasterizerSceneGLES2::_render_geometry(RenderList::Element *p_element) { glVertexAttrib4f(VS::ARRAY_MAX + 1, 0, 1, 0, 0); glVertexAttrib4f(VS::ARRAY_MAX + 2, 0, 0, 1, 0); } +*/ + } break; - glBindBuffer(GL_ARRAY_BUFFER, s->vertex_id); - - if (s->index_array_len > 0) { - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, s->index_id); - } + case VS::INSTANCE_MULTIMESH: { - for (int i = 0; i < VS::ARRAY_MAX - 1; i++) { - if (s->attribs[i].enabled) { - glEnableVertexAttribArray(i); - glVertexAttribPointer(s->attribs[i].index, s->attribs[i].size, s->attribs[i].type, s->attribs[i].normalized, s->attribs[i].stride, (uint8_t *)0 + s->attribs[i].offset); - } else { - glDisableVertexAttribArray(i); - } - } + RasterizerStorageGLES2::MultiMesh *multi_mesh = static_cast<RasterizerStorageGLES2::MultiMesh *>(p_element->owner); + RasterizerStorageGLES2::Surface *s = static_cast<RasterizerStorageGLES2::Surface *>(p_element->geometry); - glDisableVertexAttribArray(12); // transform 0 - glDisableVertexAttribArray(13); // transform 1 - glDisableVertexAttribArray(14); // transform 2 - glDisableVertexAttribArray(15); // color - glDisableVertexAttribArray(8); // custom data + int amount = MIN(multi_mesh->size, multi_mesh->visible_instances); - glVertexAttrib4f(15, 1, 1, 1, 1); - glVertexAttrib4f(8, 0, 0, 0, 0); + if (amount == -1) { + amount = multi_mesh->size; + } int stride = multi_mesh->color_floats + multi_mesh->custom_data_floats + multi_mesh->xform_floats; @@ -1197,44 +1509,34 @@ void RasterizerSceneGLES2::_render_geometry(RenderList::Element *p_element) { // drawing + const float *base_buffer = multi_mesh->data.ptr(); + for (int i = 0; i < amount; i++) { - float *buffer = &multi_mesh->data.write[i * stride]; + const float *buffer = base_buffer + i * stride; { - // inline of multimesh_get_transform since it's such a pain - // to get a RID from here... - Transform transform; - - transform.basis.elements[0][0] = buffer[0]; - transform.basis.elements[0][1] = buffer[1]; - transform.basis.elements[0][2] = buffer[2]; - transform.origin.x = buffer[3]; - transform.basis.elements[1][0] = buffer[4]; - transform.basis.elements[1][1] = buffer[5]; - transform.basis.elements[1][2] = buffer[6]; - transform.origin.y = buffer[7]; - transform.basis.elements[2][0] = buffer[8]; - transform.basis.elements[2][1] = buffer[9]; - transform.basis.elements[2][2] = buffer[10]; - transform.origin.z = buffer[11]; - - float row[3][4] = { - { transform.basis[0][0], transform.basis[0][1], transform.basis[0][2], transform.origin[0] }, - { transform.basis[1][0], transform.basis[1][1], transform.basis[1][2], transform.origin[1] }, - { transform.basis[2][0], transform.basis[2][1], transform.basis[2][2], transform.origin[2] }, - }; - - glVertexAttrib4fv(12, row[0]); - glVertexAttrib4fv(13, row[1]); - glVertexAttrib4fv(14, row[2]); + + glVertexAttrib4fv(INSTANCE_ATTRIB_BASE + 0, &buffer[0]); + glVertexAttrib4fv(INSTANCE_ATTRIB_BASE + 1, &buffer[4]); + glVertexAttrib4fv(INSTANCE_ATTRIB_BASE + 2, &buffer[8]); } if (multi_mesh->color_floats) { - glVertexAttrib4fv(15, buffer + color_ofs); + if (multi_mesh->color_format == VS::MULTIMESH_COLOR_8BIT) { + uint8_t *color_data = (uint8_t *)(buffer + color_ofs); + glVertexAttrib4f(INSTANCE_ATTRIB_BASE + 3, color_data[0] / 255.0, color_data[1] / 255.0, color_data[2] / 255.0, color_data[3] / 255.0); + } else { + glVertexAttrib4fv(INSTANCE_ATTRIB_BASE + 3, buffer + color_ofs); + } } if (multi_mesh->custom_data_floats) { - glVertexAttrib4fv(8, buffer + custom_data_ofs); + if (multi_mesh->custom_data_format == VS::MULTIMESH_CUSTOM_DATA_8BIT) { + uint8_t *custom_data = (uint8_t *)(buffer + custom_data_ofs); + glVertexAttrib4f(INSTANCE_ATTRIB_BASE + 4, custom_data[0] / 255.0, custom_data[1] / 255.0, custom_data[2] / 255.0, custom_data[3] / 255.0); + } else { + glVertexAttrib4fv(INSTANCE_ATTRIB_BASE + 4, buffer + custom_data_ofs); + } } if (s->index_array_len > 0) { @@ -1244,528 +1546,851 @@ void RasterizerSceneGLES2::_render_geometry(RenderList::Element *p_element) { } } - // tear down + } break; - for (int i = 0; i < VS::ARRAY_MAX - 1; i++) { - glDisableVertexAttribArray(i); - } + case VS::INSTANCE_IMMEDIATE: { + const RasterizerStorageGLES2::Immediate *im = static_cast<const RasterizerStorageGLES2::Immediate *>(p_element->geometry); - if (s->index_array_len > 0) { - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); + if (im->building) { + return; } - if (p_element->instance->skeleton.is_valid() && s->attribs[VS::ARRAY_BONES].enabled && s->attribs[VS::ARRAY_WEIGHTS].enabled) { - glBindBuffer(GL_ARRAY_BUFFER, storage->resources.skeleton_transform_buffer); + bool restore_tex = false; - glDisableVertexAttribArray(VS::ARRAY_MAX + 0); - glDisableVertexAttribArray(VS::ARRAY_MAX + 1); - glDisableVertexAttribArray(VS::ARRAY_MAX + 2); - } + glBindBuffer(GL_ARRAY_BUFFER, state.immediate_buffer); - glBindBuffer(GL_ARRAY_BUFFER, 0); - } break; - } -} + for (const List<RasterizerStorageGLES2::Immediate::Chunk>::Element *E = im->chunks.front(); E; E = E->next()) { + const RasterizerStorageGLES2::Immediate::Chunk &c = E->get(); -void RasterizerSceneGLES2::_render_render_list(RenderList::Element **p_elements, int p_element_count, const RID *p_directional_lights, int p_directional_light_count, const Transform &p_view_transform, const CameraMatrix &p_projection, RID p_shadow_atlas, Environment *p_env, GLuint p_base_env, float p_shadow_bias, float p_shadow_normal_bias, bool p_reverse_cull, bool p_alpha_pass, bool p_shadow, bool p_directional_add) { + if (c.vertices.empty()) { + continue; + } - ShadowAtlas *shadow_atlas = shadow_atlas_owner.getornull(p_shadow_atlas); + int vertices = c.vertices.size(); - Vector2 screen_pixel_size; - screen_pixel_size.x = 1.0 / storage->frame.current_rt->width; - screen_pixel_size.y = 1.0 / storage->frame.current_rt->height; + uint32_t buf_ofs = 0; - bool use_radiance_map = false; + storage->info.render.vertices_count += vertices; - VMap<RID, Vector<RenderList::Element *> > lit_objects; + if (c.texture.is_valid() && storage->texture_owner.owns(c.texture)) { + RasterizerStorageGLES2::Texture *t = storage->texture_owner.get(c.texture); - for (int i = 0; i < p_element_count; i++) { - RenderList::Element *e = p_elements[i]; + t = t->get_ptr(); - RasterizerStorageGLES2::Material *material = e->material; + if (t->redraw_if_visible) { + VisualServerRaster::redraw_request(); + } - RasterizerStorageGLES2::Skeleton *skeleton = storage->skeleton_owner.getornull(e->instance->skeleton); +#ifdef TOOLS_ENABLED + if (t->detect_3d) { + t->detect_3d(t->detect_3d_ud); + } +#endif + if (t->render_target) { + t->render_target->used_in_frame = true; + } - if (p_base_env) { - glActiveTexture(GL_TEXTURE0 + storage->config.max_texture_image_units - 2); - glBindTexture(GL_TEXTURE_CUBE_MAP, p_base_env); - use_radiance_map = true; - } - state.scene_shader.set_conditional(SceneShaderGLES2::USE_RADIANCE_MAP, use_radiance_map); + glActiveTexture(GL_TEXTURE0); + glBindTexture(t->target, t->tex_id); + restore_tex = true; + } else if (restore_tex) { - if (material->shader->spatial.unshaded) { - state.scene_shader.set_conditional(SceneShaderGLES2::USE_RADIANCE_MAP, false); - } else { - state.scene_shader.set_conditional(SceneShaderGLES2::USE_RADIANCE_MAP, use_radiance_map); - } + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, state.current_main_tex); + restore_tex = false; + } - // opaque pass + if (!c.normals.empty()) { + glEnableVertexAttribArray(VS::ARRAY_NORMAL); + glBufferSubData(GL_ARRAY_BUFFER, buf_ofs, sizeof(Vector3) * vertices, c.normals.ptr()); + glVertexAttribPointer(VS::ARRAY_NORMAL, 3, GL_FLOAT, GL_FALSE, sizeof(Vector3), ((uint8_t *)NULL) + buf_ofs); + buf_ofs += sizeof(Vector3) * vertices; + } else { + glDisableVertexAttribArray(VS::ARRAY_NORMAL); + } - state.scene_shader.set_conditional(SceneShaderGLES2::LIGHT_PASS, false); + if (!c.tangents.empty()) { + glEnableVertexAttribArray(VS::ARRAY_TANGENT); + glBufferSubData(GL_ARRAY_BUFFER, buf_ofs, sizeof(Plane) * vertices, c.tangents.ptr()); + glVertexAttribPointer(VS::ARRAY_TANGENT, 4, GL_FLOAT, GL_FALSE, sizeof(Plane), ((uint8_t *)NULL) + buf_ofs); + buf_ofs += sizeof(Plane) * vertices; + } else { + glDisableVertexAttribArray(VS::ARRAY_TANGENT); + } - _setup_geometry(e, skeleton); + if (!c.colors.empty()) { + glEnableVertexAttribArray(VS::ARRAY_COLOR); + glBufferSubData(GL_ARRAY_BUFFER, buf_ofs, sizeof(Color) * vertices, c.colors.ptr()); + glVertexAttribPointer(VS::ARRAY_COLOR, 4, GL_FLOAT, GL_FALSE, sizeof(Color), ((uint8_t *)NULL) + buf_ofs); + buf_ofs += sizeof(Color) * vertices; + } else { + glDisableVertexAttribArray(VS::ARRAY_COLOR); + } - _setup_material(material, p_reverse_cull, Size2i(skeleton ? skeleton->size * 3 : 0, 0)); + if (!c.uvs.empty()) { + glEnableVertexAttribArray(VS::ARRAY_TEX_UV); + glBufferSubData(GL_ARRAY_BUFFER, buf_ofs, sizeof(Vector2) * vertices, c.uvs.ptr()); + glVertexAttribPointer(VS::ARRAY_TEX_UV, 2, GL_FLOAT, GL_FALSE, sizeof(Vector2), ((uint8_t *)NULL) + buf_ofs); + buf_ofs += sizeof(Vector2) * vertices; + } else { + glDisableVertexAttribArray(VS::ARRAY_TEX_UV); + } - if (use_radiance_map) { - state.scene_shader.set_uniform(SceneShaderGLES2::RADIANCE_INVERSE_XFORM, p_view_transform); - } + if (!c.uv2s.empty()) { + glEnableVertexAttribArray(VS::ARRAY_TEX_UV2); + glBufferSubData(GL_ARRAY_BUFFER, buf_ofs, sizeof(Vector2) * vertices, c.uv2s.ptr()); + glVertexAttribPointer(VS::ARRAY_TEX_UV2, 2, GL_FLOAT, GL_FALSE, sizeof(Vector2), ((uint8_t *)NULL) + buf_ofs); + buf_ofs += sizeof(Vector2) * vertices; + } else { + glDisableVertexAttribArray(VS::ARRAY_TEX_UV2); + } - if (p_shadow) { - state.scene_shader.set_uniform(SceneShaderGLES2::LIGHT_BIAS, p_shadow_bias); - state.scene_shader.set_uniform(SceneShaderGLES2::LIGHT_NORMAL_BIAS, p_shadow_normal_bias); - } + glEnableVertexAttribArray(VS::ARRAY_VERTEX); + glBufferSubData(GL_ARRAY_BUFFER, buf_ofs, sizeof(Vector3) * vertices, c.vertices.ptr()); + glVertexAttribPointer(VS::ARRAY_VERTEX, 3, GL_FLOAT, GL_FALSE, sizeof(Vector3), ((uint8_t *)NULL) + buf_ofs); - if (p_env) { - state.scene_shader.set_uniform(SceneShaderGLES2::BG_ENERGY, p_env->bg_energy); - state.scene_shader.set_uniform(SceneShaderGLES2::AMBIENT_SKY_CONTRIBUTION, p_env->ambient_sky_contribution); - state.scene_shader.set_uniform(SceneShaderGLES2::AMBIENT_COLOR, p_env->ambient_color); - state.scene_shader.set_uniform(SceneShaderGLES2::AMBIENT_ENERGY, p_env->ambient_energy); + glDrawArrays(gl_primitive[c.primitive], 0, c.vertices.size()); + } - } else { - state.scene_shader.set_uniform(SceneShaderGLES2::BG_ENERGY, 1.0); - state.scene_shader.set_uniform(SceneShaderGLES2::AMBIENT_SKY_CONTRIBUTION, 1.0); - state.scene_shader.set_uniform(SceneShaderGLES2::AMBIENT_COLOR, Color(1.0, 1.0, 1.0, 1.0)); - state.scene_shader.set_uniform(SceneShaderGLES2::AMBIENT_ENERGY, 1.0); - } + if (restore_tex) { + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, state.current_main_tex); + restore_tex = false; + } - glEnable(GL_BLEND); + } break; + default: {} + } +} - if (p_alpha_pass || p_directional_add) { - int desired_blend_mode; - if (p_directional_add) { - desired_blend_mode = RasterizerStorageGLES2::Shader::Spatial::BLEND_MODE_ADD; - } else { - desired_blend_mode = material->shader->spatial.blend_mode; - } +void RasterizerSceneGLES2::_setup_light_type(LightInstance *p_light, ShadowAtlas *shadow_atlas) { - switch (desired_blend_mode) { + //turn off all by default + state.scene_shader.set_conditional(SceneShaderGLES2::USE_LIGHTING, false); + state.scene_shader.set_conditional(SceneShaderGLES2::USE_SHADOW, false); + state.scene_shader.set_conditional(SceneShaderGLES2::SHADOW_MODE_PCF_5, false); + state.scene_shader.set_conditional(SceneShaderGLES2::SHADOW_MODE_PCF_13, false); + state.scene_shader.set_conditional(SceneShaderGLES2::LIGHT_MODE_DIRECTIONAL, false); + state.scene_shader.set_conditional(SceneShaderGLES2::LIGHT_MODE_OMNI, false); + state.scene_shader.set_conditional(SceneShaderGLES2::LIGHT_MODE_SPOT, false); + state.scene_shader.set_conditional(SceneShaderGLES2::LIGHT_USE_PSSM2, false); + state.scene_shader.set_conditional(SceneShaderGLES2::LIGHT_USE_PSSM4, false); + state.scene_shader.set_conditional(SceneShaderGLES2::LIGHT_USE_PSSM_BLEND, false); + state.scene_shader.set_conditional(SceneShaderGLES2::USE_SHADOW, false); - case RasterizerStorageGLES2::Shader::Spatial::BLEND_MODE_MIX: { - glBlendEquation(GL_FUNC_ADD); - if (storage->frame.current_rt && storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT]) { - glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA); - } else { - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - } + if (!p_light) { //no light, return off + return; + } - } break; - case RasterizerStorageGLES2::Shader::Spatial::BLEND_MODE_ADD: { + //turn on lighting + state.scene_shader.set_conditional(SceneShaderGLES2::USE_LIGHTING, true); - glBlendEquation(GL_FUNC_ADD); - glBlendFunc(p_alpha_pass ? GL_SRC_ALPHA : GL_ONE, GL_ONE); + switch (p_light->light_ptr->type) { + case VS::LIGHT_DIRECTIONAL: { + state.scene_shader.set_conditional(SceneShaderGLES2::LIGHT_MODE_DIRECTIONAL, true); + switch (p_light->light_ptr->directional_shadow_mode) { + case VS::LIGHT_DIRECTIONAL_SHADOW_ORTHOGONAL: { + //no need } break; - case RasterizerStorageGLES2::Shader::Spatial::BLEND_MODE_SUB: { + case VS::LIGHT_DIRECTIONAL_SHADOW_PARALLEL_2_SPLITS: { + state.scene_shader.set_conditional(SceneShaderGLES2::LIGHT_USE_PSSM2, true); - glBlendEquation(GL_FUNC_REVERSE_SUBTRACT); - glBlendFunc(GL_SRC_ALPHA, GL_ONE); } break; - case RasterizerStorageGLES2::Shader::Spatial::BLEND_MODE_MUL: { - glBlendEquation(GL_FUNC_ADD); - if (storage->frame.current_rt && storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT]) { - glBlendFuncSeparate(GL_DST_COLOR, GL_ZERO, GL_DST_ALPHA, GL_ZERO); - } else { - glBlendFuncSeparate(GL_DST_COLOR, GL_ZERO, GL_ZERO, GL_ONE); - } - + case VS::LIGHT_DIRECTIONAL_SHADOW_PARALLEL_4_SPLITS: { + state.scene_shader.set_conditional(SceneShaderGLES2::LIGHT_USE_PSSM4, true); } break; } - } else { - // no blend mode given - assume mix - glBlendEquation(GL_FUNC_ADD); - if (storage->frame.current_rt && storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT]) { - glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA); - } else { - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + + state.scene_shader.set_conditional(SceneShaderGLES2::LIGHT_USE_PSSM_BLEND, p_light->light_ptr->directional_blend_splits); + if (!state.render_no_shadows && p_light->light_ptr->shadow) { + state.scene_shader.set_conditional(SceneShaderGLES2::USE_SHADOW, true); + glActiveTexture(GL_TEXTURE0 + storage->config.max_texture_image_units - 3); + glBindTexture(GL_TEXTURE_2D, directional_shadow.depth); + state.scene_shader.set_conditional(SceneShaderGLES2::SHADOW_MODE_PCF_5, shadow_filter_mode == SHADOW_FILTER_PCF5); + state.scene_shader.set_conditional(SceneShaderGLES2::SHADOW_MODE_PCF_13, shadow_filter_mode == SHADOW_FILTER_PCF13); } - } - state.scene_shader.set_uniform(SceneShaderGLES2::CAMERA_MATRIX, p_view_transform.inverse()); - state.scene_shader.set_uniform(SceneShaderGLES2::CAMERA_INVERSE_MATRIX, p_view_transform); - state.scene_shader.set_uniform(SceneShaderGLES2::PROJECTION_MATRIX, p_projection); - state.scene_shader.set_uniform(SceneShaderGLES2::PROJECTION_INVERSE_MATRIX, p_projection.inverse()); + } break; + case VS::LIGHT_OMNI: { + + state.scene_shader.set_conditional(SceneShaderGLES2::LIGHT_MODE_OMNI, true); + if (!state.render_no_shadows && shadow_atlas && p_light->light_ptr->shadow) { + state.scene_shader.set_conditional(SceneShaderGLES2::USE_SHADOW, true); + glActiveTexture(GL_TEXTURE0 + storage->config.max_texture_image_units - 3); + glBindTexture(GL_TEXTURE_2D, shadow_atlas->depth); + state.scene_shader.set_conditional(SceneShaderGLES2::SHADOW_MODE_PCF_5, shadow_filter_mode == SHADOW_FILTER_PCF5); + state.scene_shader.set_conditional(SceneShaderGLES2::SHADOW_MODE_PCF_13, shadow_filter_mode == SHADOW_FILTER_PCF13); + } + } break; + case VS::LIGHT_SPOT: { + + state.scene_shader.set_conditional(SceneShaderGLES2::LIGHT_MODE_SPOT, true); + if (!state.render_no_shadows && shadow_atlas && p_light->light_ptr->shadow) { + state.scene_shader.set_conditional(SceneShaderGLES2::USE_SHADOW, true); + glActiveTexture(GL_TEXTURE0 + storage->config.max_texture_image_units - 3); + glBindTexture(GL_TEXTURE_2D, shadow_atlas->depth); + state.scene_shader.set_conditional(SceneShaderGLES2::SHADOW_MODE_PCF_5, shadow_filter_mode == SHADOW_FILTER_PCF5); + state.scene_shader.set_conditional(SceneShaderGLES2::SHADOW_MODE_PCF_13, shadow_filter_mode == SHADOW_FILTER_PCF13); + } + } break; + } +} - state.scene_shader.set_uniform(SceneShaderGLES2::TIME, storage->frame.time[0]); +void RasterizerSceneGLES2::_setup_light(LightInstance *light, ShadowAtlas *shadow_atlas, const Transform &p_view_transform) { - state.scene_shader.set_uniform(SceneShaderGLES2::SCREEN_PIXEL_SIZE, screen_pixel_size); - state.scene_shader.set_uniform(SceneShaderGLES2::NORMAL_MULT, 1.0); // TODO mirror? - state.scene_shader.set_uniform(SceneShaderGLES2::WORLD_TRANSFORM, e->instance->transform); + RasterizerStorageGLES2::Light *light_ptr = light->light_ptr; - _render_geometry(e); + //common parameters + float energy = light_ptr->param[VS::LIGHT_PARAM_ENERGY]; + float specular = light_ptr->param[VS::LIGHT_PARAM_SPECULAR]; + float sign = light_ptr->negative ? -1 : 1; - if (material->shader->spatial.unshaded) - continue; + state.scene_shader.set_uniform(SceneShaderGLES2::LIGHT_SPECULAR, specular); + Color color = light_ptr->color * sign * energy * Math_PI; + state.scene_shader.set_uniform(SceneShaderGLES2::LIGHT_COLOR, color); - if (p_shadow) - continue; + //specific parameters - for (int light = 0; light < e->instance->light_instances.size(); light++) { + switch (light_ptr->type) { + case VS::LIGHT_DIRECTIONAL: { + //not using inverse for performance, view should be normalized anyway + Vector3 direction = p_view_transform.basis.xform_inv(light->transform.basis.xform(Vector3(0, 0, -1))).normalized(); + state.scene_shader.set_uniform(SceneShaderGLES2::LIGHT_DIRECTION, direction); - RID light_instance = e->instance->light_instances[light]; + CameraMatrix matrices[4]; - lit_objects[light_instance].push_back(e); - } - } + if (!state.render_no_shadows && light_ptr->shadow && directional_shadow.depth) { - if (p_shadow) { - state.scene_shader.set_conditional(SceneShaderGLES2::USE_RADIANCE_MAP, false); - state.scene_shader.set_conditional(SceneShaderGLES2::LIGHT_USE_PSSM4, false); - state.scene_shader.set_conditional(SceneShaderGLES2::LIGHT_USE_PSSM2, false); - state.scene_shader.set_conditional(SceneShaderGLES2::LIGHT_USE_PSSM_BLEND, false); - return; - } + int shadow_count = 0; + Color split_offsets; - state.scene_shader.set_conditional(SceneShaderGLES2::LIGHT_PASS, true); + switch (light_ptr->directional_shadow_mode) { + case VS::LIGHT_DIRECTIONAL_SHADOW_ORTHOGONAL: { + shadow_count = 1; + } break; - glEnable(GL_BLEND); - glBlendEquation(GL_FUNC_ADD); - glBlendFunc(GL_SRC_ALPHA, GL_ONE); + case VS::LIGHT_DIRECTIONAL_SHADOW_PARALLEL_2_SPLITS: { + shadow_count = 2; + } break; - for (int lo = 0; lo < lit_objects.size(); lo++) { + case VS::LIGHT_DIRECTIONAL_SHADOW_PARALLEL_4_SPLITS: { + shadow_count = 4; + } break; + } - RID key = lit_objects.getk(lo); + for (int k = 0; k < shadow_count; k++) { - LightInstance *light = light_instance_owner.getornull(key); - RasterizerStorageGLES2::Light *light_ptr = light->light_ptr; + uint32_t x = light->directional_rect.position.x; + uint32_t y = light->directional_rect.position.y; + uint32_t width = light->directional_rect.size.x; + uint32_t height = light->directional_rect.size.y; - const Vector<RenderList::Element *> &list = lit_objects.getv(lo); + if (light_ptr->directional_shadow_mode == VS::LIGHT_DIRECTIONAL_SHADOW_PARALLEL_4_SPLITS) { - for (int i = 0; i < list.size(); i++) { + width /= 2; + height /= 2; - RenderList::Element *e = list[i]; - RasterizerStorageGLES2::Material *material = e->material; + if (k == 0) { - RasterizerStorageGLES2::Skeleton *skeleton = storage->skeleton_owner.getornull(e->instance->skeleton); + } else if (k == 1) { + x += width; + } else if (k == 2) { + y += height; + } else if (k == 3) { + x += width; + y += height; + } - { - _setup_geometry(e, skeleton); + } else if (light_ptr->directional_shadow_mode == VS::LIGHT_DIRECTIONAL_SHADOW_PARALLEL_2_SPLITS) { - _setup_material(material, p_reverse_cull, Size2i(skeleton ? skeleton->size * 3 : 0, 0)); - if (shadow_atlas != NULL) { - glActiveTexture(GL_TEXTURE0 + storage->config.max_texture_image_units - 4); - glBindTexture(GL_TEXTURE_2D, shadow_atlas->depth); - } + height /= 2; - state.scene_shader.set_uniform(SceneShaderGLES2::CAMERA_MATRIX, p_view_transform.inverse()); - state.scene_shader.set_uniform(SceneShaderGLES2::CAMERA_INVERSE_MATRIX, p_view_transform); - state.scene_shader.set_uniform(SceneShaderGLES2::PROJECTION_MATRIX, p_projection); - state.scene_shader.set_uniform(SceneShaderGLES2::PROJECTION_INVERSE_MATRIX, p_projection.inverse()); + if (k == 0) { - state.scene_shader.set_uniform(SceneShaderGLES2::TIME, storage->frame.time[0]); + } else { + y += height; + } + } - state.scene_shader.set_uniform(SceneShaderGLES2::SCREEN_PIXEL_SIZE, screen_pixel_size); - state.scene_shader.set_uniform(SceneShaderGLES2::NORMAL_MULT, 1.0); // TODO mirror? - state.scene_shader.set_uniform(SceneShaderGLES2::WORLD_TRANSFORM, e->instance->transform); - } + split_offsets[k] = light->shadow_transform[k].split; - switch (light_ptr->type) { - case VS::LIGHT_OMNI: { + Transform modelview = (p_view_transform.inverse() * light->shadow_transform[k].transform).affine_inverse(); - state.scene_shader.set_uniform(SceneShaderGLES2::LIGHT_TYPE, (int)1); + CameraMatrix bias; + bias.set_light_bias(); + CameraMatrix rectm; + Rect2 atlas_rect = Rect2(float(x) / directional_shadow.size, float(y) / directional_shadow.size, float(width) / directional_shadow.size, float(height) / directional_shadow.size); + rectm.set_light_atlas_rect(atlas_rect); - Vector3 position = p_view_transform.inverse().xform(light->transform.origin); + CameraMatrix shadow_mtx = rectm * bias * light->shadow_transform[k].camera * modelview; + matrices[k] = shadow_mtx; - state.scene_shader.set_uniform(SceneShaderGLES2::LIGHT_POSITION, position); + /*Color light_clamp; + light_clamp[0] = atlas_rect.position.x; + light_clamp[1] = atlas_rect.position.y; + light_clamp[2] = atlas_rect.size.x; + light_clamp[3] = atlas_rect.size.y;*/ + } - float range = light_ptr->param[VS::LIGHT_PARAM_RANGE]; - state.scene_shader.set_uniform(SceneShaderGLES2::LIGHT_RANGE, range); + // state.scene_shader.set_uniform(SceneShaderGLES2::LIGHT_CLAMP, light_clamp); + state.scene_shader.set_uniform(SceneShaderGLES2::SHADOW_PIXEL_SIZE, Size2(1.0 / directional_shadow.size, 1.0 / directional_shadow.size)); + state.scene_shader.set_uniform(SceneShaderGLES2::LIGHT_SPLIT_OFFSETS, split_offsets); + state.scene_shader.set_uniform(SceneShaderGLES2::LIGHT_SHADOW_MATRIX, matrices[0]); + state.scene_shader.set_uniform(SceneShaderGLES2::LIGHT_SHADOW_MATRIX2, matrices[1]); + state.scene_shader.set_uniform(SceneShaderGLES2::LIGHT_SHADOW_MATRIX3, matrices[2]); + state.scene_shader.set_uniform(SceneShaderGLES2::LIGHT_SHADOW_MATRIX4, matrices[3]); + } + } break; + case VS::LIGHT_OMNI: { - Color attenuation = Color(0.0, 0.0, 0.0, 0.0); - attenuation.a = light_ptr->param[VS::LIGHT_PARAM_ATTENUATION]; - state.scene_shader.set_uniform(SceneShaderGLES2::LIGHT_ATTENUATION, attenuation); + Vector3 position = p_view_transform.xform_inv(light->transform.origin); - if (light_ptr->shadow && shadow_atlas->shadow_owners.has(light->self)) { + state.scene_shader.set_uniform(SceneShaderGLES2::LIGHT_POSITION, position); - uint32_t key = shadow_atlas->shadow_owners[light->self]; + float range = light_ptr->param[VS::LIGHT_PARAM_RANGE]; + state.scene_shader.set_uniform(SceneShaderGLES2::LIGHT_RANGE, range); - uint32_t quadrant = (key >> ShadowAtlas::QUADRANT_SHIFT) & 0x03; - uint32_t shadow = key & ShadowAtlas::SHADOW_INDEX_MASK; + float attenuation = light_ptr->param[VS::LIGHT_PARAM_ATTENUATION]; + state.scene_shader.set_uniform(SceneShaderGLES2::LIGHT_ATTENUATION, attenuation); - ERR_CONTINUE(shadow >= (uint32_t)shadow_atlas->quadrants[quadrant].shadows.size()); + if (!state.render_no_shadows && light_ptr->shadow && shadow_atlas && shadow_atlas->shadow_owners.has(light->self)) { - uint32_t atlas_size = shadow_atlas->size; - uint32_t quadrant_size = atlas_size >> 1; + uint32_t key = shadow_atlas->shadow_owners[light->self]; - uint32_t x = (quadrant & 1) * quadrant_size; - uint32_t y = (quadrant >> 1) * quadrant_size; + uint32_t quadrant = (key >> ShadowAtlas::QUADRANT_SHIFT) & 0x03; + uint32_t shadow = key & ShadowAtlas::SHADOW_INDEX_MASK; - uint32_t shadow_size = (quadrant_size / shadow_atlas->quadrants[quadrant].subdivision); - x += (shadow % shadow_atlas->quadrants[quadrant].subdivision) * shadow_size; - y += (shadow / shadow_atlas->quadrants[quadrant].subdivision) * shadow_size; + ERR_BREAK(shadow >= (uint32_t)shadow_atlas->quadrants[quadrant].shadows.size()); - uint32_t width = shadow_size; - uint32_t height = shadow_size; + uint32_t atlas_size = shadow_atlas->size; + uint32_t quadrant_size = atlas_size >> 1; - if (light->light_ptr->omni_shadow_detail == VS::LIGHT_OMNI_SHADOW_DETAIL_HORIZONTAL) { - height /= 2; - } else { - width /= 2; - } + uint32_t x = (quadrant & 1) * quadrant_size; + uint32_t y = (quadrant >> 1) * quadrant_size; - Transform proj = (p_view_transform.inverse() * light->transform).inverse(); + uint32_t shadow_size = (quadrant_size / shadow_atlas->quadrants[quadrant].subdivision); + x += (shadow % shadow_atlas->quadrants[quadrant].subdivision) * shadow_size; + y += (shadow / shadow_atlas->quadrants[quadrant].subdivision) * shadow_size; - Color light_clamp; - light_clamp[0] = float(x) / atlas_size; - light_clamp[1] = float(y) / atlas_size; - light_clamp[2] = float(width) / atlas_size; - light_clamp[3] = float(height) / atlas_size; + uint32_t width = shadow_size; + uint32_t height = shadow_size; - state.scene_shader.set_uniform(SceneShaderGLES2::LIGHT_SHADOW_MATRIX, proj); - state.scene_shader.set_uniform(SceneShaderGLES2::LIGHT_CLAMP, light_clamp); + if (light->light_ptr->omni_shadow_detail == VS::LIGHT_OMNI_SHADOW_DETAIL_HORIZONTAL) { + height /= 2; + } else { + width /= 2; + } - state.scene_shader.set_uniform(SceneShaderGLES2::LIGHT_HAS_SHADOW, 1.0); - } else { - state.scene_shader.set_uniform(SceneShaderGLES2::LIGHT_HAS_SHADOW, 0.0); - } - } break; + Transform proj = (p_view_transform.inverse() * light->transform).inverse(); - case VS::LIGHT_SPOT: { - Vector3 position = p_view_transform.inverse().xform(light->transform.origin); - state.scene_shader.set_uniform(SceneShaderGLES2::LIGHT_TYPE, (int)2); - state.scene_shader.set_uniform(SceneShaderGLES2::LIGHT_POSITION, position); + Color light_clamp; + light_clamp[0] = float(x) / atlas_size; + light_clamp[1] = float(y) / atlas_size; + light_clamp[2] = float(width) / atlas_size; + light_clamp[3] = float(height) / atlas_size; - Vector3 direction = p_view_transform.inverse().basis.xform(light->transform.basis.xform(Vector3(0, 0, -1))).normalized(); - state.scene_shader.set_uniform(SceneShaderGLES2::LIGHT_DIRECTION, direction); - Color attenuation = Color(0.0, 0.0, 0.0, 0.0); - attenuation.a = light_ptr->param[VS::LIGHT_PARAM_ATTENUATION]; - float range = light_ptr->param[VS::LIGHT_PARAM_RANGE]; - float spot_attenuation = light_ptr->param[VS::LIGHT_PARAM_SPOT_ATTENUATION]; - float angle = light_ptr->param[VS::LIGHT_PARAM_SPOT_ANGLE]; - angle = Math::cos(Math::deg2rad(angle)); - state.scene_shader.set_uniform(SceneShaderGLES2::LIGHT_ATTENUATION, attenuation); - state.scene_shader.set_uniform(SceneShaderGLES2::LIGHT_SPOT_ATTENUATION, spot_attenuation); - state.scene_shader.set_uniform(SceneShaderGLES2::LIGHT_SPOT_RANGE, spot_attenuation); - state.scene_shader.set_uniform(SceneShaderGLES2::LIGHT_SPOT_ANGLE, angle); - state.scene_shader.set_uniform(SceneShaderGLES2::LIGHT_RANGE, range); + state.scene_shader.set_uniform(SceneShaderGLES2::SHADOW_PIXEL_SIZE, Size2(1.0 / shadow_atlas->size, 1.0 / shadow_atlas->size)); + state.scene_shader.set_uniform(SceneShaderGLES2::LIGHT_SHADOW_MATRIX, proj); + state.scene_shader.set_uniform(SceneShaderGLES2::LIGHT_CLAMP, light_clamp); + } + } break; - if (light->light_ptr->shadow && shadow_atlas && shadow_atlas->shadow_owners.has(light->self)) { - uint32_t key = shadow_atlas->shadow_owners[light->self]; + case VS::LIGHT_SPOT: { - uint32_t quadrant = (key >> ShadowAtlas::QUADRANT_SHIFT) & 0x03; - uint32_t shadow = key & ShadowAtlas::SHADOW_INDEX_MASK; + Vector3 position = p_view_transform.xform_inv(light->transform.origin); - ERR_CONTINUE(shadow >= (uint32_t)shadow_atlas->quadrants[quadrant].shadows.size()); + state.scene_shader.set_uniform(SceneShaderGLES2::LIGHT_POSITION, position); - uint32_t atlas_size = shadow_atlas->size; - uint32_t quadrant_size = atlas_size >> 1; + Vector3 direction = p_view_transform.inverse().basis.xform(light->transform.basis.xform(Vector3(0, 0, -1))).normalized(); + state.scene_shader.set_uniform(SceneShaderGLES2::LIGHT_DIRECTION, direction); + float attenuation = light_ptr->param[VS::LIGHT_PARAM_ATTENUATION]; + float range = light_ptr->param[VS::LIGHT_PARAM_RANGE]; + float spot_attenuation = light_ptr->param[VS::LIGHT_PARAM_SPOT_ATTENUATION]; + float angle = light_ptr->param[VS::LIGHT_PARAM_SPOT_ANGLE]; + angle = Math::cos(Math::deg2rad(angle)); + state.scene_shader.set_uniform(SceneShaderGLES2::LIGHT_ATTENUATION, attenuation); + state.scene_shader.set_uniform(SceneShaderGLES2::LIGHT_SPOT_ATTENUATION, spot_attenuation); + state.scene_shader.set_uniform(SceneShaderGLES2::LIGHT_SPOT_RANGE, spot_attenuation); + state.scene_shader.set_uniform(SceneShaderGLES2::LIGHT_SPOT_ANGLE, angle); + state.scene_shader.set_uniform(SceneShaderGLES2::LIGHT_RANGE, range); - uint32_t x = (quadrant & 1) * quadrant_size; - uint32_t y = (quadrant >> 1) * quadrant_size; + if (!state.render_no_shadows && light->light_ptr->shadow && shadow_atlas && shadow_atlas->shadow_owners.has(light->self)) { + uint32_t key = shadow_atlas->shadow_owners[light->self]; - uint32_t shadow_size = (quadrant_size / shadow_atlas->quadrants[quadrant].subdivision); - x += (shadow % shadow_atlas->quadrants[quadrant].subdivision) * shadow_size; - y += (shadow / shadow_atlas->quadrants[quadrant].subdivision) * shadow_size; + uint32_t quadrant = (key >> ShadowAtlas::QUADRANT_SHIFT) & 0x03; + uint32_t shadow = key & ShadowAtlas::SHADOW_INDEX_MASK; - uint32_t width = shadow_size; - uint32_t height = shadow_size; + ERR_BREAK(shadow >= (uint32_t)shadow_atlas->quadrants[quadrant].shadows.size()); - Rect2 rect(float(x) / atlas_size, float(y) / atlas_size, float(width) / atlas_size, float(height) / atlas_size); + uint32_t atlas_size = shadow_atlas->size; + uint32_t quadrant_size = atlas_size >> 1; - Color light_clamp; - light_clamp[0] = rect.position.x; - light_clamp[1] = rect.position.y; - light_clamp[2] = rect.size.x; - light_clamp[3] = rect.size.y; + uint32_t x = (quadrant & 1) * quadrant_size; + uint32_t y = (quadrant >> 1) * quadrant_size; - Transform modelview = (p_view_transform.inverse() * light->transform).inverse(); + uint32_t shadow_size = (quadrant_size / shadow_atlas->quadrants[quadrant].subdivision); + x += (shadow % shadow_atlas->quadrants[quadrant].subdivision) * shadow_size; + y += (shadow / shadow_atlas->quadrants[quadrant].subdivision) * shadow_size; - CameraMatrix bias; - bias.set_light_bias(); + uint32_t width = shadow_size; + uint32_t height = shadow_size; - CameraMatrix rectm; - rectm.set_light_atlas_rect(rect); + Rect2 rect(float(x) / atlas_size, float(y) / atlas_size, float(width) / atlas_size, float(height) / atlas_size); - CameraMatrix shadow_matrix = rectm * bias * light->shadow_transform[0].camera * modelview; + Color light_clamp; + light_clamp[0] = rect.position.x; + light_clamp[1] = rect.position.y; + light_clamp[2] = rect.size.x; + light_clamp[3] = rect.size.y; - state.scene_shader.set_uniform(SceneShaderGLES2::LIGHT_HAS_SHADOW, 1.0); - state.scene_shader.set_uniform(SceneShaderGLES2::LIGHT_SHADOW_MATRIX, shadow_matrix); - state.scene_shader.set_uniform(SceneShaderGLES2::LIGHT_CLAMP, light_clamp); + Transform modelview = (p_view_transform.inverse() * light->transform).inverse(); - } else { - state.scene_shader.set_uniform(SceneShaderGLES2::LIGHT_HAS_SHADOW, 0.0); - } + CameraMatrix bias; + bias.set_light_bias(); - } break; + CameraMatrix rectm; + rectm.set_light_atlas_rect(rect); - default: break; + CameraMatrix shadow_matrix = rectm * bias * light->shadow_transform[0].camera * modelview; + + state.scene_shader.set_uniform(SceneShaderGLES2::SHADOW_PIXEL_SIZE, Size2(1.0 / shadow_atlas->size, 1.0 / shadow_atlas->size)); + state.scene_shader.set_uniform(SceneShaderGLES2::LIGHT_SHADOW_MATRIX, shadow_matrix); + state.scene_shader.set_uniform(SceneShaderGLES2::LIGHT_CLAMP, light_clamp); } - float energy = light->light_ptr->param[VS::LIGHT_PARAM_ENERGY]; - float specular = light->light_ptr->param[VS::LIGHT_PARAM_SPECULAR]; + } break; + default: {} + } +} + +void RasterizerSceneGLES2::_setup_refprobes(ReflectionProbeInstance *p_refprobe1, ReflectionProbeInstance *p_refprobe2, const Transform &p_view_transform, Environment *p_env) { - state.scene_shader.set_uniform(SceneShaderGLES2::LIGHT_ENERGY, energy); - state.scene_shader.set_uniform(SceneShaderGLES2::LIGHT_COLOR, light->light_ptr->color.to_linear()); - state.scene_shader.set_uniform(SceneShaderGLES2::LIGHT_SPECULAR, specular); + if (p_refprobe1) { + state.scene_shader.set_uniform(SceneShaderGLES2::REFPROBE1_USE_BOX_PROJECT, p_refprobe1->probe_ptr->box_projection); + state.scene_shader.set_uniform(SceneShaderGLES2::REFPROBE1_BOX_EXTENTS, p_refprobe1->probe_ptr->extents); + state.scene_shader.set_uniform(SceneShaderGLES2::REFPROBE1_BOX_OFFSET, p_refprobe1->probe_ptr->origin_offset); + state.scene_shader.set_uniform(SceneShaderGLES2::REFPROBE1_EXTERIOR, !p_refprobe1->probe_ptr->interior); + state.scene_shader.set_uniform(SceneShaderGLES2::REFPROBE1_INTENSITY, p_refprobe1->probe_ptr->intensity); - _render_geometry(e); + Color ambient; + if (p_refprobe1->probe_ptr->interior) { + ambient = p_refprobe1->probe_ptr->interior_ambient * p_refprobe1->probe_ptr->interior_ambient_energy; + ambient.a = p_refprobe1->probe_ptr->interior_ambient_probe_contrib; + } else if (p_env) { + ambient = p_env->ambient_color * p_env->ambient_energy; + ambient.a = p_env->ambient_sky_contribution; } + + state.scene_shader.set_uniform(SceneShaderGLES2::REFPROBE1_AMBIENT, ambient); + + Transform proj = (p_view_transform.inverse() * p_refprobe1->transform).affine_inverse(); + + state.scene_shader.set_uniform(SceneShaderGLES2::REFPROBE1_LOCAL_MATRIX, proj); } - for (int dl = 0; dl < p_directional_light_count; dl++) { - RID light_rid = p_directional_lights[dl]; - LightInstance *light = light_instance_owner.getornull(light_rid); - RasterizerStorageGLES2::Light *light_ptr = light->light_ptr; + if (p_refprobe2) { + state.scene_shader.set_uniform(SceneShaderGLES2::REFPROBE2_USE_BOX_PROJECT, p_refprobe2->probe_ptr->box_projection); + state.scene_shader.set_uniform(SceneShaderGLES2::REFPROBE2_BOX_EXTENTS, p_refprobe2->probe_ptr->extents); + state.scene_shader.set_uniform(SceneShaderGLES2::REFPROBE2_BOX_OFFSET, p_refprobe2->probe_ptr->origin_offset); + state.scene_shader.set_uniform(SceneShaderGLES2::REFPROBE2_EXTERIOR, !p_refprobe2->probe_ptr->interior); + state.scene_shader.set_uniform(SceneShaderGLES2::REFPROBE2_INTENSITY, p_refprobe2->probe_ptr->intensity); + + Color ambient; + if (p_refprobe2->probe_ptr->interior) { + ambient = p_refprobe2->probe_ptr->interior_ambient * p_refprobe2->probe_ptr->interior_ambient_energy; + ambient.a = p_refprobe2->probe_ptr->interior_ambient_probe_contrib; + } else if (p_env) { + ambient = p_env->ambient_color * p_env->ambient_energy; + ambient.a = p_env->ambient_sky_contribution; + } - switch (light_ptr->directional_shadow_mode) { - case VS::LIGHT_DIRECTIONAL_SHADOW_ORTHOGONAL: { - } break; - case VS::LIGHT_DIRECTIONAL_SHADOW_PARALLEL_2_SPLITS: { - state.scene_shader.set_conditional(SceneShaderGLES2::LIGHT_USE_PSSM2, true); - state.scene_shader.set_conditional(SceneShaderGLES2::LIGHT_USE_PSSM_BLEND, light_ptr->directional_blend_splits); - } break; + state.scene_shader.set_uniform(SceneShaderGLES2::REFPROBE2_AMBIENT, ambient); - case VS::LIGHT_DIRECTIONAL_SHADOW_PARALLEL_4_SPLITS: { - state.scene_shader.set_conditional(SceneShaderGLES2::LIGHT_USE_PSSM4, true); - state.scene_shader.set_conditional(SceneShaderGLES2::LIGHT_USE_PSSM_BLEND, light_ptr->directional_blend_splits); - } break; - default: - break; + Transform proj = (p_view_transform.inverse() * p_refprobe2->transform).affine_inverse(); + + state.scene_shader.set_uniform(SceneShaderGLES2::REFPROBE2_LOCAL_MATRIX, proj); + } +} + +void RasterizerSceneGLES2::_render_render_list(RenderList::Element **p_elements, int p_element_count, const Transform &p_view_transform, const CameraMatrix &p_projection, RID p_shadow_atlas, Environment *p_env, GLuint p_base_env, float p_shadow_bias, float p_shadow_normal_bias, bool p_reverse_cull, bool p_alpha_pass, bool p_shadow) { + + ShadowAtlas *shadow_atlas = shadow_atlas_owner.getornull(p_shadow_atlas); + + Vector2 viewport_size = state.viewport_size; + + Vector2 screen_pixel_size = state.screen_pixel_size; + + bool use_radiance_map = false; + if (!p_shadow && p_base_env) { + glActiveTexture(GL_TEXTURE0 + storage->config.max_texture_image_units - 2); + glBindTexture(GL_TEXTURE_CUBE_MAP, p_base_env); + use_radiance_map = true; + state.scene_shader.set_conditional(SceneShaderGLES2::USE_RADIANCE_MAP, true); //since prev unshaded is false, this needs to be true if exists + } + + bool prev_unshaded = false; + bool prev_instancing = false; + state.scene_shader.set_conditional(SceneShaderGLES2::SHADELESS, false); + RasterizerStorageGLES2::Material *prev_material = NULL; + RasterizerStorageGLES2::Geometry *prev_geometry = NULL; + RasterizerStorageGLES2::Skeleton *prev_skeleton = NULL; + RasterizerStorageGLES2::GeometryOwner *prev_owner = NULL; + + Transform view_transform_inverse = p_view_transform.inverse(); + CameraMatrix projection_inverse = p_projection.inverse(); + + bool prev_base_pass = false; + LightInstance *prev_light = NULL; + bool prev_vertex_lit = false; + ReflectionProbeInstance *prev_refprobe_1 = NULL; + ReflectionProbeInstance *prev_refprobe_2 = NULL; + + int prev_blend_mode = -2; //will always catch the first go + + if (p_alpha_pass) { + glEnable(GL_BLEND); + } else { + glDisable(GL_BLEND); + } + + float fog_max_distance = 0; + bool using_fog = false; + if (p_env && !p_shadow && p_env->fog_enabled && (p_env->fog_depth_enabled || p_env->fog_height_enabled)) { + state.scene_shader.set_conditional(SceneShaderGLES2::FOG_DEPTH_ENABLED, p_env->fog_depth_enabled); + state.scene_shader.set_conditional(SceneShaderGLES2::FOG_HEIGHT_ENABLED, p_env->fog_height_enabled); + if (p_env->fog_depth_end > 0) { + fog_max_distance = p_env->fog_depth_end; + } else { + fog_max_distance = p_projection.get_z_far(); } + using_fog = true; + } - for (int i = 0; i < p_element_count; i++) { + RasterizerStorageGLES2::Texture *prev_lightmap = NULL; + float lightmap_energy = 1.0; + bool prev_use_lightmap_capture = false; - RenderList::Element *e = p_elements[i]; - RasterizerStorageGLES2::Material *material = e->material; - RasterizerStorageGLES2::Skeleton *skeleton = storage->skeleton_owner.getornull(e->instance->skeleton); + for (int i = 0; i < p_element_count; i++) { + RenderList::Element *e = p_elements[i]; - { - _setup_material(material, p_reverse_cull, Size2i(skeleton ? skeleton->size * 3 : 0, 0)); + RasterizerStorageGLES2::Material *material = e->material; - if (directional_shadow.depth) { - glActiveTexture(GL_TEXTURE0 + storage->config.max_texture_image_units - 4); // TODO move into base pass - glBindTexture(GL_TEXTURE_2D, directional_shadow.depth); + bool rebind = false; + bool accum_pass = *e->use_accum_ptr; + *e->use_accum_ptr = true; //set to accum for next time this is found + LightInstance *light = NULL; + ReflectionProbeInstance *refprobe_1 = NULL; + ReflectionProbeInstance *refprobe_2 = NULL; + RasterizerStorageGLES2::Texture *lightmap = NULL; + bool use_lightmap_capture = false; + bool rebind_light = false; + bool rebind_reflection = false; + bool rebind_lightmap = false; + + if (!p_shadow) { + + bool unshaded = material->shader->spatial.unshaded; + + if (unshaded != prev_unshaded) { + rebind = true; + if (unshaded) { + state.scene_shader.set_conditional(SceneShaderGLES2::SHADELESS, true); + state.scene_shader.set_conditional(SceneShaderGLES2::USE_RADIANCE_MAP, false); + state.scene_shader.set_conditional(SceneShaderGLES2::USE_LIGHTING, false); + } else { + state.scene_shader.set_conditional(SceneShaderGLES2::SHADELESS, false); + state.scene_shader.set_conditional(SceneShaderGLES2::USE_RADIANCE_MAP, use_radiance_map); } - state.scene_shader.set_uniform(SceneShaderGLES2::CAMERA_MATRIX, p_view_transform.inverse()); - state.scene_shader.set_uniform(SceneShaderGLES2::CAMERA_INVERSE_MATRIX, p_view_transform); - state.scene_shader.set_uniform(SceneShaderGLES2::PROJECTION_MATRIX, p_projection); - state.scene_shader.set_uniform(SceneShaderGLES2::PROJECTION_INVERSE_MATRIX, p_projection.inverse()); + prev_unshaded = unshaded; + } - state.scene_shader.set_uniform(SceneShaderGLES2::TIME, storage->frame.time[0]); + bool base_pass = !accum_pass && !unshaded; //conditions for a base pass - state.scene_shader.set_uniform(SceneShaderGLES2::SCREEN_PIXEL_SIZE, screen_pixel_size); - state.scene_shader.set_uniform(SceneShaderGLES2::NORMAL_MULT, 1.0); // TODO mirror? - state.scene_shader.set_uniform(SceneShaderGLES2::WORLD_TRANSFORM, e->instance->transform); + if (base_pass != prev_base_pass) { + state.scene_shader.set_conditional(SceneShaderGLES2::BASE_PASS, base_pass); + rebind = true; + prev_base_pass = base_pass; } - state.scene_shader.set_uniform(SceneShaderGLES2::LIGHT_TYPE, (int)0); - Vector3 direction = p_view_transform.inverse().basis.xform(light->transform.basis.xform(Vector3(0, 0, -1))).normalized(); - state.scene_shader.set_uniform(SceneShaderGLES2::LIGHT_DIRECTION, direction); - float energy = light_ptr->param[VS::LIGHT_PARAM_ENERGY]; - float specular = light_ptr->param[VS::LIGHT_PARAM_SPECULAR]; + if (!unshaded && e->light_index < RenderList::MAX_LIGHTS) { + light = render_light_instances[e->light_index]; + } - state.scene_shader.set_uniform(SceneShaderGLES2::LIGHT_ENERGY, energy); - state.scene_shader.set_uniform(SceneShaderGLES2::LIGHT_SPECULAR, specular); + if (light != prev_light) { - float sign = light_ptr->negative ? -1 : 1; + _setup_light_type(light, shadow_atlas); + rebind = true; + rebind_light = true; + } - Color linear_col = light_ptr->color.to_linear(); - Color color; - for (int c = 0; c < 3; c++) - color[c] = linear_col[c] * sign * energy * Math_PI; + int blend_mode = p_alpha_pass ? material->shader->spatial.blend_mode : -1; // -1 no blend, no mix - color[3] = 0; + if (accum_pass) { //accum pass force pass + blend_mode = RasterizerStorageGLES2::Shader::Spatial::BLEND_MODE_ADD; + } - state.scene_shader.set_uniform(SceneShaderGLES2::LIGHT_COLOR, color); + if (prev_blend_mode != blend_mode) { - CameraMatrix matrices[4]; + if (prev_blend_mode == -1 && blend_mode != -1) { + //does blend + glEnable(GL_BLEND); + } else if (blend_mode == -1 && prev_blend_mode != -1) { + //do not blend + glDisable(GL_BLEND); + } - if (light_ptr->shadow && directional_shadow.depth) { + switch (blend_mode) { + //-1 not handled because not blend is enabled anyway + case RasterizerStorageGLES2::Shader::Spatial::BLEND_MODE_MIX: { + glBlendEquation(GL_FUNC_ADD); + if (storage->frame.current_rt && storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT]) { + glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA); + } else { + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + } - int shadow_count = 0; - Color split_offsets; + } break; + case RasterizerStorageGLES2::Shader::Spatial::BLEND_MODE_ADD: { + + glBlendEquation(GL_FUNC_ADD); + glBlendFunc(p_alpha_pass ? GL_SRC_ALPHA : GL_ONE, GL_ONE); - switch (light_ptr->directional_shadow_mode) { - case VS::LIGHT_DIRECTIONAL_SHADOW_ORTHOGONAL: { - shadow_count = 1; } break; + case RasterizerStorageGLES2::Shader::Spatial::BLEND_MODE_SUB: { - case VS::LIGHT_DIRECTIONAL_SHADOW_PARALLEL_2_SPLITS: { - shadow_count = 2; + glBlendEquation(GL_FUNC_REVERSE_SUBTRACT); + glBlendFunc(GL_SRC_ALPHA, GL_ONE); } break; + case RasterizerStorageGLES2::Shader::Spatial::BLEND_MODE_MUL: { + glBlendEquation(GL_FUNC_ADD); + if (storage->frame.current_rt && storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT]) { + glBlendFuncSeparate(GL_DST_COLOR, GL_ZERO, GL_DST_ALPHA, GL_ZERO); + } else { + glBlendFuncSeparate(GL_DST_COLOR, GL_ZERO, GL_ZERO, GL_ONE); + } - case VS::LIGHT_DIRECTIONAL_SHADOW_PARALLEL_4_SPLITS: { - shadow_count = 4; } break; } - for (int k = 0; k < shadow_count; k++) { + prev_blend_mode = blend_mode; + } - uint32_t x = light->directional_rect.position.x; - uint32_t y = light->directional_rect.position.y; - uint32_t width = light->directional_rect.size.x; - uint32_t height = light->directional_rect.size.y; + //condition to enable vertex lighting on this object + bool vertex_lit = (material->shader->spatial.uses_vertex_lighting || storage->config.force_vertex_shading) && ((!unshaded && light) || using_fog); //fog forces vertex lighting because it still applies even if unshaded or no fog - if (light_ptr->directional_shadow_mode == VS::LIGHT_DIRECTIONAL_SHADOW_PARALLEL_4_SPLITS) { + if (vertex_lit != prev_vertex_lit) { + state.scene_shader.set_conditional(SceneShaderGLES2::USE_VERTEX_LIGHTING, vertex_lit); + prev_vertex_lit = vertex_lit; + } - width /= 2; - height /= 2; + if (!unshaded && !accum_pass && e->refprobe_0_index != RenderList::MAX_REFLECTION_PROBES) { + ERR_FAIL_INDEX(e->refprobe_0_index, reflection_probe_count); + refprobe_1 = reflection_probe_instances[e->refprobe_0_index]; + } + if (!unshaded && !accum_pass && e->refprobe_1_index != RenderList::MAX_REFLECTION_PROBES) { + ERR_FAIL_INDEX(e->refprobe_1_index, reflection_probe_count); + refprobe_2 = reflection_probe_instances[e->refprobe_1_index]; + } - if (k == 0) { + if (refprobe_1 != prev_refprobe_1 || refprobe_2 != prev_refprobe_2) { + state.scene_shader.set_conditional(SceneShaderGLES2::USE_REFLECTION_PROBE1, refprobe_1 != NULL); + state.scene_shader.set_conditional(SceneShaderGLES2::USE_REFLECTION_PROBE2, refprobe_2 != NULL); + if (refprobe_1 != NULL && refprobe_1 != prev_refprobe_1) { + glActiveTexture(GL_TEXTURE0 + storage->config.max_texture_image_units - 5); + glBindTexture(GL_TEXTURE_CUBE_MAP, refprobe_1->cubemap); + } + if (refprobe_2 != NULL && refprobe_2 != prev_refprobe_2) { + glActiveTexture(GL_TEXTURE0 + storage->config.max_texture_image_units - 6); + glBindTexture(GL_TEXTURE_CUBE_MAP, refprobe_2->cubemap); + } + rebind = true; + rebind_reflection = true; + } - } else if (k == 1) { - x += width; - } else if (k == 2) { - y += height; - } else if (k == 3) { - x += width; - y += height; - } + use_lightmap_capture = !unshaded && !accum_pass && !e->instance->lightmap_capture_data.empty(); - } else if (light_ptr->directional_shadow_mode == VS::LIGHT_DIRECTIONAL_SHADOW_PARALLEL_2_SPLITS) { + if (use_lightmap_capture != prev_use_lightmap_capture) { - height /= 2; + state.scene_shader.set_conditional(SceneShaderGLES2::USE_LIGHTMAP_CAPTURE, use_lightmap_capture); + rebind = true; + } - if (k == 0) { + if (!unshaded && !accum_pass && e->instance->lightmap.is_valid()) { - } else { - y += height; - } + lightmap = storage->texture_owner.getornull(e->instance->lightmap); + lightmap_energy = 1.0; + if (lightmap) { + RasterizerStorageGLES2::LightmapCapture *capture = storage->lightmap_capture_data_owner.getornull(e->instance->lightmap_capture->base); + if (capture) { + lightmap_energy = capture->energy; } + } + } - split_offsets[k] = light->shadow_transform[k].split; + if (lightmap != prev_lightmap) { + state.scene_shader.set_conditional(SceneShaderGLES2::USE_LIGHTMAP, lightmap != NULL); + if (lightmap != NULL) { + glActiveTexture(GL_TEXTURE0 + storage->config.max_texture_image_units - 4); + glBindTexture(GL_TEXTURE_2D, lightmap->tex_id); + } + rebind = true; + rebind_lightmap = true; + } + } - Transform modelview = (p_view_transform * light->shadow_transform[k].transform).inverse(); + bool instancing = e->instance->base_type == VS::INSTANCE_MULTIMESH; - CameraMatrix bias; - bias.set_light_bias(); - CameraMatrix rectm; - Rect2 atlas_rect = Rect2(float(x) / directional_shadow.size, float(y) / directional_shadow.size, float(width) / directional_shadow.size, float(height) / directional_shadow.size); - rectm.set_light_atlas_rect(atlas_rect); + if (instancing != prev_instancing) { - CameraMatrix shadow_mtx = rectm * bias * light->shadow_transform[k].camera * modelview; - matrices[k] = shadow_mtx.inverse(); + state.scene_shader.set_conditional(SceneShaderGLES2::USE_INSTANCING, instancing); + rebind = true; + } - Color light_clamp; - light_clamp[0] = atlas_rect.position.x; - light_clamp[1] = atlas_rect.position.y; - light_clamp[2] = atlas_rect.size.x; - light_clamp[3] = atlas_rect.size.y; + RasterizerStorageGLES2::Skeleton *skeleton = storage->skeleton_owner.getornull(e->instance->skeleton); - state.scene_shader.set_uniform(SceneShaderGLES2::LIGHT_HAS_SHADOW, 1.0); - state.scene_shader.set_uniform(SceneShaderGLES2::LIGHT_CLAMP, light_clamp); - state.scene_shader.set_uniform(SceneShaderGLES2::LIGHT_SPLIT_OFFSETS, split_offsets); - } + if (skeleton != prev_skeleton) { - state.scene_shader.set_uniform(SceneShaderGLES2::LIGHT_SHADOW_MATRIX1, matrices[0]); - state.scene_shader.set_uniform(SceneShaderGLES2::LIGHT_SHADOW_MATRIX2, matrices[1]); - state.scene_shader.set_uniform(SceneShaderGLES2::LIGHT_SHADOW_MATRIX3, matrices[2]); - state.scene_shader.set_uniform(SceneShaderGLES2::LIGHT_SHADOW_MATRIX4, matrices[3]); + if (skeleton) { + state.scene_shader.set_conditional(SceneShaderGLES2::USE_SKELETON, true); + state.scene_shader.set_conditional(SceneShaderGLES2::USE_SKELETON_SOFTWARE, !storage->config.float_texture_supported); } else { - state.scene_shader.set_uniform(SceneShaderGLES2::LIGHT_HAS_SHADOW, 0.0); + state.scene_shader.set_conditional(SceneShaderGLES2::USE_SKELETON, false); + state.scene_shader.set_conditional(SceneShaderGLES2::USE_SKELETON_SOFTWARE, false); } - _render_geometry(e); + rebind = true; } - } - state.scene_shader.set_conditional(SceneShaderGLES2::LIGHT_PASS, false); + if (e->owner != prev_owner || e->geometry != prev_geometry || skeleton != prev_skeleton) { + _setup_geometry(e, skeleton); + } + + bool shader_rebind = false; + if (rebind || material != prev_material) { + shader_rebind = _setup_material(material, p_reverse_cull, p_alpha_pass, Size2i(skeleton ? skeleton->size * 3 : 0, 0)); + } + + if (i == 0 || shader_rebind) { //first time must rebind + + if (p_shadow) { + state.scene_shader.set_uniform(SceneShaderGLES2::LIGHT_BIAS, p_shadow_bias); + state.scene_shader.set_uniform(SceneShaderGLES2::LIGHT_NORMAL_BIAS, p_shadow_normal_bias); + if (state.shadow_is_dual_parabolloid) { + state.scene_shader.set_uniform(SceneShaderGLES2::SHADOW_DUAL_PARABOLOID_RENDER_SIDE, state.dual_parbolloid_direction); + state.scene_shader.set_uniform(SceneShaderGLES2::SHADOW_DUAL_PARABOLOID_RENDER_ZFAR, state.dual_parbolloid_zfar); + } + } else { + if (use_radiance_map) { + state.scene_shader.set_uniform(SceneShaderGLES2::RADIANCE_INVERSE_XFORM, p_view_transform); + } + + if (p_env) { + state.scene_shader.set_uniform(SceneShaderGLES2::BG_ENERGY, p_env->bg_energy); + state.scene_shader.set_uniform(SceneShaderGLES2::AMBIENT_SKY_CONTRIBUTION, p_env->ambient_sky_contribution); + + state.scene_shader.set_uniform(SceneShaderGLES2::AMBIENT_COLOR, p_env->ambient_color); + state.scene_shader.set_uniform(SceneShaderGLES2::AMBIENT_ENERGY, p_env->ambient_energy); + + } else { + state.scene_shader.set_uniform(SceneShaderGLES2::BG_ENERGY, 1.0); + state.scene_shader.set_uniform(SceneShaderGLES2::AMBIENT_SKY_CONTRIBUTION, 1.0); + state.scene_shader.set_uniform(SceneShaderGLES2::AMBIENT_COLOR, Color(1.0, 1.0, 1.0, 1.0)); + state.scene_shader.set_uniform(SceneShaderGLES2::AMBIENT_ENERGY, 1.0); + } + + //rebind all these + rebind_light = true; + rebind_reflection = true; + rebind_lightmap = true; + + if (using_fog) { + + state.scene_shader.set_uniform(SceneShaderGLES2::FOG_COLOR_BASE, p_env->fog_color); + Color sun_color_amount = p_env->fog_sun_color; + sun_color_amount.a = p_env->fog_sun_amount; + state.scene_shader.set_uniform(SceneShaderGLES2::FOG_SUN_COLOR_AMOUNT, sun_color_amount); + state.scene_shader.set_uniform(SceneShaderGLES2::FOG_TRANSMIT_ENABLED, p_env->fog_transmit_enabled); + state.scene_shader.set_uniform(SceneShaderGLES2::FOG_TRANSMIT_CURVE, p_env->fog_transmit_curve); + + if (p_env->fog_depth_enabled) { + state.scene_shader.set_uniform(SceneShaderGLES2::FOG_DEPTH_BEGIN, p_env->fog_depth_begin); + state.scene_shader.set_uniform(SceneShaderGLES2::FOG_DEPTH_CURVE, p_env->fog_depth_curve); + state.scene_shader.set_uniform(SceneShaderGLES2::FOG_MAX_DISTANCE, fog_max_distance); + } + + if (p_env->fog_height_enabled) { + state.scene_shader.set_uniform(SceneShaderGLES2::FOG_HEIGHT_MIN, p_env->fog_height_min); + state.scene_shader.set_uniform(SceneShaderGLES2::FOG_HEIGHT_MAX, p_env->fog_height_max); + state.scene_shader.set_uniform(SceneShaderGLES2::FOG_HEIGHT_MAX, p_env->fog_height_max); + state.scene_shader.set_uniform(SceneShaderGLES2::FOG_HEIGHT_CURVE, p_env->fog_height_curve); + } + } + } + + state.scene_shader.set_uniform(SceneShaderGLES2::CAMERA_MATRIX, p_view_transform); + state.scene_shader.set_uniform(SceneShaderGLES2::CAMERA_INVERSE_MATRIX, view_transform_inverse); + state.scene_shader.set_uniform(SceneShaderGLES2::PROJECTION_MATRIX, p_projection); + state.scene_shader.set_uniform(SceneShaderGLES2::PROJECTION_INVERSE_MATRIX, projection_inverse); + + state.scene_shader.set_uniform(SceneShaderGLES2::TIME, storage->frame.time[0]); + + state.scene_shader.set_uniform(SceneShaderGLES2::VIEWPORT_SIZE, viewport_size); + + state.scene_shader.set_uniform(SceneShaderGLES2::SCREEN_PIXEL_SIZE, screen_pixel_size); + } + + if (rebind_light && light) { + _setup_light(light, shadow_atlas, p_view_transform); + } + + if (rebind_reflection && (refprobe_1 || refprobe_2)) { + _setup_refprobes(refprobe_1, refprobe_2, p_view_transform, p_env); + } + + if (rebind_lightmap && lightmap) { + state.scene_shader.set_uniform(SceneShaderGLES2::LIGHTMAP_ENERGY, lightmap_energy); + } + + state.scene_shader.set_uniform(SceneShaderGLES2::WORLD_TRANSFORM, e->instance->transform); + + if (use_lightmap_capture) { //this is per instance, must be set always if present + glUniform4fv(state.scene_shader.get_uniform_location(SceneShaderGLES2::LIGHTMAP_CAPTURES), 12, (const GLfloat *)e->instance->lightmap_capture_data.ptr()); + state.scene_shader.set_uniform(SceneShaderGLES2::LIGHTMAP_CAPTURE_SKY, false); + } + + _render_geometry(e); + + prev_geometry = e->geometry; + prev_owner = e->owner; + prev_material = material; + prev_skeleton = skeleton; + prev_instancing = instancing; + prev_light = light; + prev_refprobe_1 = refprobe_1; + prev_refprobe_2 = refprobe_2; + prev_lightmap = lightmap; + prev_use_lightmap_capture = use_lightmap_capture; + } + + _setup_light_type(NULL, NULL); //clear light stuff + state.scene_shader.set_conditional(SceneShaderGLES2::USE_SKELETON, false); + state.scene_shader.set_conditional(SceneShaderGLES2::SHADELESS, false); + state.scene_shader.set_conditional(SceneShaderGLES2::BASE_PASS, false); + state.scene_shader.set_conditional(SceneShaderGLES2::USE_INSTANCING, false); state.scene_shader.set_conditional(SceneShaderGLES2::USE_RADIANCE_MAP, false); state.scene_shader.set_conditional(SceneShaderGLES2::LIGHT_USE_PSSM4, false); state.scene_shader.set_conditional(SceneShaderGLES2::LIGHT_USE_PSSM2, false); state.scene_shader.set_conditional(SceneShaderGLES2::LIGHT_USE_PSSM_BLEND, false); + state.scene_shader.set_conditional(SceneShaderGLES2::USE_VERTEX_LIGHTING, false); + state.scene_shader.set_conditional(SceneShaderGLES2::USE_REFLECTION_PROBE1, false); + state.scene_shader.set_conditional(SceneShaderGLES2::USE_REFLECTION_PROBE2, false); + state.scene_shader.set_conditional(SceneShaderGLES2::USE_LIGHTMAP, false); + state.scene_shader.set_conditional(SceneShaderGLES2::USE_LIGHTMAP_CAPTURE, false); + state.scene_shader.set_conditional(SceneShaderGLES2::FOG_DEPTH_ENABLED, false); + state.scene_shader.set_conditional(SceneShaderGLES2::FOG_HEIGHT_ENABLED, false); } void RasterizerSceneGLES2::_draw_sky(RasterizerStorageGLES2::Sky *p_sky, const CameraMatrix &p_projection, const Transform &p_transform, bool p_vflip, float p_custom_fov, float p_energy) { @@ -1840,13 +2465,20 @@ void RasterizerSceneGLES2::_draw_sky(RasterizerStorageGLES2::Sky *p_sky, const C glEnableVertexAttribArray(VS::ARRAY_VERTEX); glEnableVertexAttribArray(VS::ARRAY_TEX_UV); + storage->shaders.copy.set_conditional(CopyShaderGLES2::USE_ASYM_PANO, asymmetrical); + storage->shaders.copy.set_conditional(CopyShaderGLES2::USE_PANORAMA, !asymmetrical); storage->shaders.copy.set_conditional(CopyShaderGLES2::USE_MULTIPLIER, true); storage->shaders.copy.set_conditional(CopyShaderGLES2::USE_CUBEMAP, false); - storage->shaders.copy.set_conditional(CopyShaderGLES2::USE_PANORAMA, true); storage->shaders.copy.set_conditional(CopyShaderGLES2::USE_COPY_SECTION, false); storage->shaders.copy.set_conditional(CopyShaderGLES2::USE_CUSTOM_ALPHA, false); storage->shaders.copy.bind(); storage->shaders.copy.set_uniform(CopyShaderGLES2::MULTIPLIER, p_energy); + if (asymmetrical) { + // pack the bits we need from our projection matrix + storage->shaders.copy.set_uniform(CopyShaderGLES2::ASYM_PROJ, camera.matrix[2][0], camera.matrix[0][0], camera.matrix[2][1], camera.matrix[1][1]); + ///@TODO I couldn't get mat3 + p_transform.basis to work, that would be better here. + storage->shaders.copy.set_uniform(CopyShaderGLES2::PANO_TRANSFORM, p_transform); + } glDrawArrays(GL_TRIANGLE_FAN, 0, 4); @@ -1854,16 +2486,98 @@ void RasterizerSceneGLES2::_draw_sky(RasterizerStorageGLES2::Sky *p_sky, const C glDisableVertexAttribArray(VS::ARRAY_TEX_UV); glBindBuffer(GL_ARRAY_BUFFER, 0); + storage->shaders.copy.set_conditional(CopyShaderGLES2::USE_ASYM_PANO, false); + storage->shaders.copy.set_conditional(CopyShaderGLES2::USE_PANORAMA, false); storage->shaders.copy.set_conditional(CopyShaderGLES2::USE_MULTIPLIER, false); storage->shaders.copy.set_conditional(CopyShaderGLES2::USE_CUBEMAP, false); } void RasterizerSceneGLES2::render_scene(const Transform &p_cam_transform, const CameraMatrix &p_cam_projection, bool p_cam_ortogonal, InstanceBase **p_cull_result, int p_cull_count, RID *p_light_cull_result, int p_light_cull_count, RID *p_reflection_probe_cull_result, int p_reflection_probe_cull_count, RID p_environment, RID p_shadow_atlas, RID p_reflection_atlas, RID p_reflection_probe, int p_reflection_probe_pass) { - glEnable(GL_BLEND); + GLuint current_fb = 0; + Environment *env = NULL; + + int viewport_width, viewport_height; + bool probe_interior = false; + + if (p_reflection_probe.is_valid()) { + ReflectionProbeInstance *probe = reflection_probe_instance_owner.getornull(p_reflection_probe); + ERR_FAIL_COND(!probe); + state.render_no_shadows = !probe->probe_ptr->enable_shadows; + + if (!probe->probe_ptr->interior) { //use env only if not interior + env = environment_owner.getornull(p_environment); + } + + current_fb = probe->fbo[p_reflection_probe_pass]; + + viewport_width = probe->probe_ptr->resolution; + viewport_height = probe->probe_ptr->resolution; + + probe_interior = probe->probe_ptr->interior; + + } else { + state.render_no_shadows = false; + current_fb = storage->frame.current_rt->fbo; + env = environment_owner.getornull(p_environment); + + viewport_width = storage->frame.current_rt->width; + viewport_height = storage->frame.current_rt->height; + } + + state.viewport_size.x = viewport_width; + state.viewport_size.y = viewport_height; + state.screen_pixel_size.x = 1.0 / viewport_width; + state.screen_pixel_size.y = 1.0 / viewport_height; + + //push back the directional lights + + if (p_light_cull_count) { + //harcoded limit of 256 lights + render_light_instance_count = MIN(RenderList::MAX_LIGHTS, p_light_cull_count); + render_light_instances = (LightInstance **)alloca(sizeof(LightInstance *) * render_light_instance_count); + render_directional_lights = 0; + + //doing this because directional lights are at the end, put them at the beginning + int index = 0; + for (int i = render_light_instance_count - 1; i >= 0; i--) { + RID light_rid = p_light_cull_result[i]; + + LightInstance *light = light_instance_owner.getornull(light_rid); + + if (light->light_ptr->type == VS::LIGHT_DIRECTIONAL) { + render_directional_lights++; + //as goin in reverse, directional lights are always first anyway + } + + light->light_index = index; + render_light_instances[index] = light; + + index++; + } + + } else { + render_light_instances = NULL; + render_directional_lights = 0; + render_light_instance_count = 0; + } + + if (p_reflection_probe_cull_count) { + + reflection_probe_instances = (ReflectionProbeInstance **)alloca(sizeof(ReflectionProbeInstance *) * p_reflection_probe_cull_count); + reflection_probe_count = p_reflection_probe_cull_count; + for (int i = 0; i < p_reflection_probe_cull_count; i++) { + ReflectionProbeInstance *rpi = reflection_probe_instance_owner.getornull(p_reflection_probe_cull_result[i]); + ERR_CONTINUE(!rpi); + rpi->last_pass = render_pass + 1; //will be incremented later + rpi->index = i; + reflection_probe_instances[i] = rpi; + } - GLuint current_fb = storage->frame.current_rt->fbo; - Environment *env = environment_owner.getornull(p_environment); + } else { + reflection_probe_instances = NULL; + reflection_probe_count = 0; + } // render list stuff @@ -1873,15 +2587,37 @@ void RasterizerSceneGLES2::render_scene(const Transform &p_cam_transform, const // other stuff glBindFramebuffer(GL_FRAMEBUFFER, current_fb); + glViewport(0, 0, viewport_width, viewport_height); glDepthFunc(GL_LEQUAL); glDepthMask(GL_TRUE); glClearDepth(1.0f); glEnable(GL_DEPTH_TEST); - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + // clear color - storage->frame.clear_request = false; + Color clear_color(0, 0, 0, 0); + + if (storage->frame.current_rt && storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT]) { + clear_color = Color(0, 0, 0, 0); + storage->frame.clear_request = false; + } else if (!env || env->bg_mode == VS::ENV_BG_CLEAR_COLOR || env->bg_mode == VS::ENV_BG_SKY) { + if (storage->frame.clear_request) { + clear_color = storage->frame.clear_request_color.to_linear(); + storage->frame.clear_request = false; + } + } else if (env->bg_mode == VS::ENV_BG_CANVAS || env->bg_mode == VS::ENV_BG_COLOR || env->bg_mode == VS::ENV_BG_COLOR_SKY) { + clear_color = env->bg_color.to_linear(); + storage->frame.clear_request = false; + } else { + storage->frame.clear_request = false; + } + + if (!env || env->bg_mode != VS::ENV_BG_KEEP) { + glClearColor(clear_color.r, clear_color.g, clear_color.b, clear_color.a); + } + + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glVertexAttrib4f(VS::ARRAY_COLOR, 1, 1, 1, 1); @@ -1904,7 +2640,7 @@ void RasterizerSceneGLES2::render_scene(const Transform &p_cam_transform, const } break; default: { - print_line("uhm"); + // FIXME: implement other background modes } break; } } @@ -1916,34 +2652,26 @@ void RasterizerSceneGLES2::render_scene(const Transform &p_cam_transform, const } } - Vector<RID> directional_lights; - - for (int i = 0; i < p_light_cull_count; i++) { - RID light_rid = p_light_cull_result[i]; - - LightInstance *light = light_instance_owner.getornull(light_rid); - - if (light->light_ptr->type == VS::LIGHT_DIRECTIONAL) { - directional_lights.push_back(light_rid); - } + if (probe_interior) { + env_radiance_tex = 0; //do not use radiance texture on interiors } // render opaque things first render_list.sort_by_key(false); - _render_render_list(render_list.elements, render_list.element_count, directional_lights.ptr(), directional_lights.size(), p_cam_transform, p_cam_projection, p_shadow_atlas, env, env_radiance_tex, 0.0, 0.0, false, false, false, false); + _render_render_list(render_list.elements, render_list.element_count, p_cam_transform, p_cam_projection, p_shadow_atlas, env, env_radiance_tex, 0.0, 0.0, false, false, false); // alpha pass glBlendEquation(GL_FUNC_ADD); glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); - render_list.sort_by_key(true); - _render_render_list(&render_list.elements[render_list.max_elements - render_list.alpha_element_count], render_list.alpha_element_count, directional_lights.ptr(), directional_lights.size(), p_cam_transform, p_cam_projection, p_shadow_atlas, env, env_radiance_tex, 0.0, 0.0, false, true, false, false); + render_list.sort_by_depth(true); + + _render_render_list(&render_list.elements[render_list.max_elements - render_list.alpha_element_count], render_list.alpha_element_count, p_cam_transform, p_cam_projection, p_shadow_atlas, env, env_radiance_tex, 0.0, 0.0, false, true, false); - glDepthMask(GL_FALSE); glDisable(GL_DEPTH_TEST); - // #define GLES2_SHADOW_ATLAS_DEBUG_VIEW + //#define GLES2_SHADOW_ATLAS_DEBUG_VIEW #ifdef GLES2_SHADOW_ATLAS_DEBUG_VIEW ShadowAtlas *shadow_atlas = shadow_atlas_owner.getornull(p_shadow_atlas); @@ -1962,10 +2690,31 @@ void RasterizerSceneGLES2::render_scene(const Transform &p_cam_transform, const storage->_copy_screen(); } #endif + + //#define GLES2_SHADOW_DIRECTIONAL_DEBUG_VIEW + +#ifdef GLES2_SHADOW_DIRECTIONAL_DEBUG_VIEW + if (true) { + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, directional_shadow.depth); + + glViewport(0, 0, storage->frame.current_rt->width / 4, storage->frame.current_rt->height / 4); + storage->shaders.copy.set_conditional(CopyShaderGLES2::USE_CUBEMAP, false); + storage->shaders.copy.set_conditional(CopyShaderGLES2::USE_COPY_SECTION, false); + storage->shaders.copy.set_conditional(CopyShaderGLES2::USE_CUSTOM_ALPHA, false); + storage->shaders.copy.set_conditional(CopyShaderGLES2::USE_MULTIPLIER, false); + storage->shaders.copy.set_conditional(CopyShaderGLES2::USE_PANORAMA, false); + storage->shaders.copy.bind(); + + storage->_copy_screen(); + } +#endif } void RasterizerSceneGLES2::render_shadow(RID p_light, RID p_shadow_atlas, int p_pass, InstanceBase **p_cull_result, int p_cull_count) { + state.render_no_shadows = false; + LightInstance *light_instance = light_instance_owner.getornull(p_light); ERR_FAIL_COND(!light_instance); @@ -1976,13 +2725,13 @@ void RasterizerSceneGLES2::render_shadow(RID p_light, RID p_shadow_atlas, int p_ uint32_t y; uint32_t width; uint32_t height; - uint32_t vp_height; float zfar = 0; bool flip_facing = false; int custom_vp_size = 0; - GLuint fbo = 0; + state.shadow_is_dual_parabolloid = false; + state.dual_parbolloid_direction = 0.0; int current_cubemap = -1; float bias = 0; @@ -2061,14 +2810,12 @@ void RasterizerSceneGLES2::render_shadow(RID p_light, RID p_shadow_atlas, int p_ normal_bias = light->param[VS::LIGHT_PARAM_SHADOW_NORMAL_BIAS] * bias_mult; fbo = directional_shadow.fbo; - vp_height = directional_shadow.size; } else { ShadowAtlas *shadow_atlas = shadow_atlas_owner.getornull(p_shadow_atlas); ERR_FAIL_COND(!shadow_atlas); ERR_FAIL_COND(!shadow_atlas->shadow_owners.has(p_light)); fbo = shadow_atlas->fbo; - vp_height = shadow_atlas->size; uint32_t key = shadow_atlas->shadow_owners[p_light]; @@ -2111,8 +2858,32 @@ void RasterizerSceneGLES2::render_shadow(RID p_light, RID p_shadow_atlas, int p_ zfar = light->param[VS::LIGHT_PARAM_RANGE]; current_cubemap = cubemap_index; + } else { + //dual parabolloid + state.shadow_is_dual_parabolloid = true; + light_projection = light_instance->shadow_transform[0].camera; + light_transform = light_instance->shadow_transform[0].transform; + + if (light->omni_shadow_detail == VS::LIGHT_OMNI_SHADOW_DETAIL_HORIZONTAL) { + + height /= 2; + y += p_pass * height; + } else { + width /= 2; + x += p_pass * width; + } + + state.dual_parbolloid_direction = p_pass == 0 ? 1.0 : -1.0; + flip_facing = (p_pass == 1); + zfar = light->param[VS::LIGHT_PARAM_RANGE]; + bias = light->param[VS::LIGHT_PARAM_SHADOW_BIAS]; + + state.dual_parbolloid_zfar = zfar; + + state.scene_shader.set_conditional(SceneShaderGLES2::RENDER_DEPTH_DUAL_PARABOLOID, true); } - } else { + + } else if (light->type == VS::LIGHT_SPOT) { light_projection = light_instance->shadow_transform[0].camera; light_transform = light_instance->shadow_transform[0].transform; @@ -2151,11 +2922,16 @@ void RasterizerSceneGLES2::render_shadow(RID p_light, RID p_shadow_atlas, int p_ glClear(GL_DEPTH_BUFFER_BIT); glDisable(GL_SCISSOR_TEST); + if (light->reverse_cull) { + flip_facing = !flip_facing; + } + state.scene_shader.set_conditional(SceneShaderGLES2::RENDER_DEPTH, true); - _render_render_list(render_list.elements, render_list.element_count, NULL, 0, light_transform, light_projection, RID(), NULL, 0, bias, normal_bias, false, false, true, false); + _render_render_list(render_list.elements, render_list.element_count, light_transform, light_projection, RID(), NULL, 0, bias, normal_bias, flip_facing, false, true); state.scene_shader.set_conditional(SceneShaderGLES2::RENDER_DEPTH, false); + state.scene_shader.set_conditional(SceneShaderGLES2::RENDER_DEPTH_DUAL_PARABOLOID, false); // convert cubemap to dual paraboloid if needed if (light->type == VS::LIGHT_OMNI && light->omni_shadow_mode == VS::LIGHT_OMNI_SHADOW_CUBE && p_pass == 5) { @@ -2205,6 +2981,7 @@ void RasterizerSceneGLES2::render_shadow(RID p_light, RID p_shadow_atlas, int p_ } glViewport(0, 0, storage->frame.current_rt->width, storage->frame.current_rt->height); + glColorMask(1, 1, 1, 1); } void RasterizerSceneGLES2::set_scene_pass(uint64_t p_pass) { @@ -2212,6 +2989,44 @@ void RasterizerSceneGLES2::set_scene_pass(uint64_t p_pass) { } bool RasterizerSceneGLES2::free(RID p_rid) { + + if (light_instance_owner.owns(p_rid)) { + + LightInstance *light_instance = light_instance_owner.getptr(p_rid); + + //remove from shadow atlases.. + for (Set<RID>::Element *E = light_instance->shadow_atlases.front(); E; E = E->next()) { + ShadowAtlas *shadow_atlas = shadow_atlas_owner.get(E->get()); + ERR_CONTINUE(!shadow_atlas->shadow_owners.has(p_rid)); + uint32_t key = shadow_atlas->shadow_owners[p_rid]; + uint32_t q = (key >> ShadowAtlas::QUADRANT_SHIFT) & 0x3; + uint32_t s = key & ShadowAtlas::SHADOW_INDEX_MASK; + + shadow_atlas->quadrants[q].shadows.write[s].owner = RID(); + shadow_atlas->shadow_owners.erase(p_rid); + } + + light_instance_owner.free(p_rid); + memdelete(light_instance); + + } else if (shadow_atlas_owner.owns(p_rid)) { + + ShadowAtlas *shadow_atlas = shadow_atlas_owner.get(p_rid); + shadow_atlas_set_size(p_rid, 0); + shadow_atlas_owner.free(p_rid); + memdelete(shadow_atlas); + } else if (reflection_probe_instance_owner.owns(p_rid)) { + + ReflectionProbeInstance *reflection_instance = reflection_probe_instance_owner.get(p_rid); + + reflection_probe_release_atlas_index(p_rid); + reflection_probe_instance_owner.free(p_rid); + memdelete(reflection_instance); + + } else { + return false; + } + return true; } @@ -2224,6 +3039,8 @@ void RasterizerSceneGLES2::initialize() { render_list.init(); + render_pass = 1; + shadow_atlas_realloc_tolerance_msec = 500; { @@ -2241,12 +3058,43 @@ void RasterizerSceneGLES2::initialize() { } { + default_worldcoord_shader = storage->shader_create(); + storage->shader_set_code(default_worldcoord_shader, "shader_type spatial; render_mode world_vertex_coords;\n"); + default_worldcoord_material = storage->material_create(); + storage->material_set_shader(default_worldcoord_material, default_worldcoord_shader); + + default_worldcoord_shader_twosided = storage->shader_create(); + default_worldcoord_material_twosided = storage->material_create(); + storage->shader_set_code(default_worldcoord_shader_twosided, "shader_type spatial; render_mode cull_disabled,world_vertex_coords;\n"); + storage->material_set_shader(default_worldcoord_material_twosided, default_worldcoord_shader_twosided); + } + + { + //default material and shader + + default_overdraw_shader = storage->shader_create(); + storage->shader_set_code(default_overdraw_shader, "shader_type spatial;\nrender_mode blend_add,unshaded;\n void fragment() { ALBEDO=vec3(0.4,0.8,0.8); ALPHA=0.2; }"); + default_overdraw_material = storage->material_create(); + storage->material_set_shader(default_overdraw_material, default_overdraw_shader); + } + + { glGenBuffers(1, &state.sky_verts); glBindBuffer(GL_ARRAY_BUFFER, state.sky_verts); glBufferData(GL_ARRAY_BUFFER, sizeof(Vector3) * 8, NULL, GL_DYNAMIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); } + { + uint32_t immediate_buffer_size = GLOBAL_DEF("rendering/limits/buffers/immediate_buffer_size_kb", 2048); + ProjectSettings::get_singleton()->set_custom_property_info("rendering/limits/buffers/immediate_buffer_size_kb", PropertyInfo(Variant::INT, "rendering/limits/buffers/immediate_buffer_size_kb", PROPERTY_HINT_RANGE, "0,8192,1,or_greater")); + + glGenBuffers(1, &state.immediate_buffer); + glBindBuffer(GL_ARRAY_BUFFER, state.immediate_buffer); + glBufferData(GL_ARRAY_BUFFER, immediate_buffer_size * 1024, NULL, GL_DYNAMIC_DRAW); + glBindBuffer(GL_ARRAY_BUFFER, 0); + } + // cubemaps for shadows { int max_shadow_cubemap_sampler_size = 512; @@ -2265,7 +3113,7 @@ void RasterizerSceneGLES2::initialize() { glBindTexture(GL_TEXTURE_CUBE_MAP, cube.cubemap); for (int i = 0; i < 6; i++) { - glTexImage2D(_cube_side_enum[i], 0, GL_DEPTH_COMPONENT16, cube_size, cube_size, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT, NULL); + glTexImage2D(_cube_side_enum[i], 0, GL_DEPTH_COMPONENT, cube_size, cube_size, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, NULL); } glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_NEAREST); @@ -2299,10 +3147,10 @@ void RasterizerSceneGLES2::initialize() { glGenTextures(1, &directional_shadow.depth); glBindTexture(GL_TEXTURE_2D, directional_shadow.depth); - glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT16, directional_shadow.size, directional_shadow.size, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, NULL); + glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, directional_shadow.size, directional_shadow.size, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, NULL); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); @@ -2313,9 +3161,12 @@ void RasterizerSceneGLES2::initialize() { ERR_PRINT("Directional shadow framebuffer status invalid"); } } + + shadow_filter_mode = SHADOW_FILTER_NEAREST; } void RasterizerSceneGLES2::iteration() { + shadow_filter_mode = ShadowFilterMode(int(GLOBAL_GET("rendering/quality/shadows/filter_mode"))); } void RasterizerSceneGLES2::finalize() { diff --git a/drivers/gles2/rasterizer_scene_gles2.h b/drivers/gles2/rasterizer_scene_gles2.h index f47d1f1d4e..4236554d9d 100644 --- a/drivers/gles2/rasterizer_scene_gles2.h +++ b/drivers/gles2/rasterizer_scene_gles2.h @@ -27,6 +27,7 @@ /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ + #ifndef RASTERIZERSCENEGLES2_H #define RASTERIZERSCENEGLES2_H @@ -52,12 +53,39 @@ class RasterizerSceneGLES2 : public RasterizerScene { public: + enum ShadowFilterMode { + SHADOW_FILTER_NEAREST, + SHADOW_FILTER_PCF5, + SHADOW_FILTER_PCF13, + }; + + enum { + INSTANCE_ATTRIB_BASE = 8, + INSTANCE_BONE_BASE = 13, + }; + + ShadowFilterMode shadow_filter_mode; + RID default_material; RID default_material_twosided; RID default_shader; RID default_shader_twosided; + RID default_worldcoord_material; + RID default_worldcoord_material_twosided; + RID default_worldcoord_shader; + RID default_worldcoord_shader_twosided; + + RID default_overdraw_material; + RID default_overdraw_shader; + + uint64_t render_pass; uint64_t scene_pass; + uint32_t current_material_index; + uint32_t current_geometry_index; + uint32_t current_light_index; + uint32_t current_refprobe_index; + uint32_t current_shader_index; RasterizerStorageGLES2 *storage; struct State { @@ -74,6 +102,8 @@ public: GLuint sky_verts; + GLuint immediate_buffer; + // ResolveShaderGLES3 resolve_shader; // ScreenSpaceReflectionShaderGLES3 ssr_shader; // EffectBlurShaderGLES3 effect_blur_shader; @@ -169,11 +199,21 @@ public: bool cull_front; bool cull_disabled; bool used_sss; - bool used_screen_texture; bool using_contact_shadows; VS::ViewportDebugDraw debug_draw; */ + + bool used_screen_texture; + bool shadow_is_dual_parabolloid; + float dual_parbolloid_direction; + float dual_parbolloid_zfar; + + bool render_no_shadows; + + Vector2 viewport_size; + + Vector2 screen_pixel_size; } state; /* SHADOW ATLAS API */ @@ -257,6 +297,38 @@ public: /* REFLECTION PROBE INSTANCE */ + struct ReflectionProbeInstance : public RID_Data { + + RasterizerStorageGLES2::ReflectionProbe *probe_ptr; + RID probe; + RID self; + RID atlas; + + int reflection_atlas_index; + + int render_step; + int reflection_index; + + GLuint fbo[6]; + GLuint cubemap; + GLuint depth; + + GLuint fbo_blur; + + int current_resolution; + mutable bool dirty; + + uint64_t last_pass; + uint32_t index; + + Transform transform; + }; + + mutable RID_Owner<ReflectionProbeInstance> reflection_probe_instance_owner; + + ReflectionProbeInstance **reflection_probe_instances; + int reflection_probe_count; + virtual RID reflection_probe_instance_create(RID p_probe); virtual void reflection_probe_instance_set_transform(RID p_instance, const Transform &p_transform); virtual void reflection_probe_release_atlas_index(RID p_instance); @@ -283,14 +355,44 @@ public: int canvas_max_layer; - Environment() { - bg_mode = VS::ENV_BG_CLEAR_COLOR; - sky_custom_fov = 0.0; - bg_energy = 1.0; - sky_ambient = 0; - ambient_energy = 1.0; - ambient_sky_contribution = 0.0; - canvas_max_layer = 0; + bool fog_enabled; + Color fog_color; + Color fog_sun_color; + float fog_sun_amount; + + bool fog_depth_enabled; + float fog_depth_begin; + float fog_depth_end; + float fog_depth_curve; + bool fog_transmit_enabled; + float fog_transmit_curve; + bool fog_height_enabled; + float fog_height_min; + float fog_height_max; + float fog_height_curve; + + Environment() : + bg_mode(VS::ENV_BG_CLEAR_COLOR), + sky_custom_fov(0.0), + bg_energy(1.0), + sky_ambient(0), + ambient_energy(1.0), + ambient_sky_contribution(0.0), + canvas_max_layer(0), + fog_enabled(false), + fog_color(Color(0.5, 0.5, 0.5)), + fog_sun_color(Color(0.8, 0.8, 0.0)), + fog_sun_amount(0), + fog_depth_enabled(true), + fog_depth_begin(10), + fog_depth_end(0), + fog_depth_curve(1), + fog_transmit_enabled(true), + fog_transmit_curve(1), + fog_height_enabled(false), + fog_height_min(0), + fog_height_max(100), + fog_height_curve(1) { } }; @@ -308,7 +410,7 @@ public: virtual void environment_set_dof_blur_near(RID p_env, bool p_enable, float p_distance, float p_transition, float p_amount, VS::EnvironmentDOFBlurQuality p_quality); virtual void environment_set_dof_blur_far(RID p_env, bool p_enable, float p_distance, float p_transition, float p_amount, VS::EnvironmentDOFBlurQuality p_quality); - virtual void environment_set_glow(RID p_env, bool p_enable, int p_level_flags, float p_intensity, float p_strength, float p_bloom_threshold, VS::EnvironmentGlowBlendMode p_blend_mode, float p_hdr_bleed_threshold, float p_hdr_bleed_scale, bool p_bicubic_upscale); + virtual void environment_set_glow(RID p_env, bool p_enable, int p_level_flags, float p_intensity, float p_strength, float p_bloom_threshold, VS::EnvironmentGlowBlendMode p_blend_mode, float p_hdr_bleed_threshold, float p_hdr_bleed_scale, float p_hdr_luminance_cap, bool p_bicubic_upscale); virtual void environment_set_fog(RID p_env, bool p_enable, float p_begin, float p_end, RID p_gradient_texture); virtual void environment_set_ssr(RID p_env, bool p_enable, int p_max_steps, float p_fade_in, float p_fade_out, float p_depth_tolerance, bool p_roughness); @@ -319,7 +421,7 @@ public: virtual void environment_set_adjustment(RID p_env, bool p_enable, float p_brightness, float p_contrast, float p_saturation, RID p_ramp); virtual void environment_set_fog(RID p_env, bool p_enable, const Color &p_color, const Color &p_sun_color, float p_sun_amount); - virtual void environment_set_fog_depth(RID p_env, bool p_enable, float p_depth_begin, float p_depth_curve, bool p_transmit, float p_transmit_curve); + virtual void environment_set_fog_depth(RID p_env, bool p_enable, float p_depth_begin, float p_depth_end, float p_depth_curve, bool p_transmit, float p_transmit_curve); virtual void environment_set_fog_height(RID p_env, bool p_enable, float p_min_height, float p_max_height, float p_height_curve); virtual bool is_environment(RID p_env); @@ -370,6 +472,10 @@ public: virtual void light_instance_set_shadow_transform(RID p_light_instance, const CameraMatrix &p_projection, const Transform &p_transform, float p_far, float p_split, int p_pass, float p_bias_scale = 1.0); virtual void light_instance_mark_visible(RID p_light_instance); + LightInstance **render_light_instances; + int render_directional_lights; + int render_light_instance_count; + /* REFLECTION INSTANCE */ virtual RID gi_probe_instance_create(); @@ -379,40 +485,19 @@ public: /* RENDER LIST */ + enum LightMode { + LIGHTMODE_NORMAL, + LIGHTMODE_UNSHADED, + LIGHTMODE_LIGHTMAP, + LIGHTMODE_LIGHTMAP_CAPTURE, + }; + struct RenderList { + enum { - DEFAULT_MAX_ELEMENTS = 65536, - SORT_FLAG_SKELETON = 1, - SORT_FLAG_INSTANCING = 2, - MAX_DIRECTIONAL_LIGHTS = 16, - MAX_LIGHTS = 4096, - MAX_REFLECTIONS = 1024, - - SORT_KEY_PRIORITY_SHIFT = 56, - SORT_KEY_PRIORITY_MASK = 0xFF, - //depth layer for opaque (56-52) - SORT_KEY_OPAQUE_DEPTH_LAYER_SHIFT = 52, - SORT_KEY_OPAQUE_DEPTH_LAYER_MASK = 0xF, -//64 bits unsupported in MSVC -#define SORT_KEY_UNSHADED_FLAG (uint64_t(1) << 49) -#define SORT_KEY_NO_DIRECTIONAL_FLAG (uint64_t(1) << 48) -#define SORT_KEY_LIGHTMAP_CAPTURE_FLAG (uint64_t(1) << 47) -#define SORT_KEY_LIGHTMAP_FLAG (uint64_t(1) << 46) -#define SORT_KEY_GI_PROBES_FLAG (uint64_t(1) << 45) -#define SORT_KEY_VERTEX_LIT_FLAG (uint64_t(1) << 44) - SORT_KEY_SHADING_SHIFT = 44, - SORT_KEY_SHADING_MASK = 63, - //44-28 material index - SORT_KEY_MATERIAL_INDEX_SHIFT = 28, - //28-8 geometry index - SORT_KEY_GEOMETRY_INDEX_SHIFT = 8, - //bits 5-7 geometry type - SORT_KEY_GEOMETRY_TYPE_SHIFT = 5, - //bits 0-5 for flags - SORT_KEY_OPAQUE_PRE_PASS = 8, - SORT_KEY_CULL_DISABLED_FLAG = 4, - SORT_KEY_SKELETON_FLAG = 2, - SORT_KEY_MIRROR_FLAG = 1 + MAX_LIGHTS = 255, + MAX_REFLECTION_PROBES = 255, + DEFAULT_MAX_ELEMENTS = 65536 }; int max_elements; @@ -424,7 +509,38 @@ public: RasterizerStorageGLES2::Material *material; RasterizerStorageGLES2::GeometryOwner *owner; - uint64_t sort_key; + bool use_accum; //is this an add pass for multipass + bool *use_accum_ptr; + + union { + //TODO: should be endian swapped on big endian + struct { + int32_t depth_layer : 16; + int32_t priority : 16; + }; + + uint32_t depth_key; + }; + + union { + struct { + //from least significant to most significant in sort, TODO: should be endian swapped on big endian + + uint64_t geometry_index : 14; + uint64_t instancing : 1; + uint64_t skeleton : 1; + uint64_t shader_index : 10; + uint64_t material_index : 10; + uint64_t light_index : 8; + uint64_t light_type2 : 1; // if 1==0 : nolight/directional, else omni/spot + uint64_t refprobe_1_index : 8; + uint64_t refprobe_0_index : 8; + uint64_t light_type1 : 1; //no light, directional is 0, omni spot is 1 + uint64_t light_mode : 2; // LightMode enum + }; + + uint64_t sort_key; + }; }; Element *base_elements; @@ -442,7 +558,11 @@ public: struct SortByKey { _FORCE_INLINE_ bool operator()(const Element *A, const Element *B) const { - return A->sort_key < B->sort_key; + if (A->depth_key == B->depth_key) { + return A->sort_key < B->sort_key; + } else { + return A->depth_key < B->depth_key; + } } }; @@ -473,29 +593,6 @@ public: } } - struct SortByReverseDepthAndPriority { - - _FORCE_INLINE_ bool operator()(const Element *A, const Element *B) const { - uint32_t layer_A = uint32_t(A->sort_key >> SORT_KEY_PRIORITY_SHIFT); - uint32_t layer_B = uint32_t(B->sort_key >> SORT_KEY_PRIORITY_SHIFT); - if (layer_A == layer_B) { - return A->instance->depth > B->instance->depth; - } else { - return layer_A < layer_B; - } - } - }; - - void sort_by_reverse_depth_and_priority(bool p_alpha) { //used for alpha - - SortArray<Element *, SortByReverseDepthAndPriority> sorter; - if (p_alpha) { - sorter.sort(&elements[max_elements - alpha_element_count], alpha_element_count); - } else { - sorter.sort(elements, element_count); - } - } - // element adding and stuff _FORCE_INLINE_ Element *add_element() { @@ -546,7 +643,6 @@ public: void _fill_render_list(InstanceBase **p_cull_result, int p_cull_count, bool p_depth_pass, bool p_shadow_pass); void _render_render_list(RenderList::Element **p_elements, int p_element_count, - const RID *p_directional_lights, int p_directional_light_count, const Transform &p_view_transform, const CameraMatrix &p_projection, RID p_shadow_atlas, @@ -556,14 +652,16 @@ public: float p_shadow_normal_bias, bool p_reverse_cull, bool p_alpha_pass, - bool p_shadow, - bool p_directional_add); + bool p_shadow); void _draw_sky(RasterizerStorageGLES2::Sky *p_sky, const CameraMatrix &p_projection, const Transform &p_transform, bool p_vflip, float p_custom_fov, float p_energy); - void _setup_material(RasterizerStorageGLES2::Material *p_material, bool p_reverse_cull, Size2i p_skeleton_tex_size = Size2i(0, 0)); - void _setup_geometry(RenderList::Element *p_element, RasterizerStorageGLES2::Skeleton *p_skeleton); - void _render_geometry(RenderList::Element *p_element); + _FORCE_INLINE_ bool _setup_material(RasterizerStorageGLES2::Material *p_material, bool p_reverse_cull, bool p_alpha_pass, Size2i p_skeleton_tex_size = Size2i(0, 0)); + _FORCE_INLINE_ void _setup_geometry(RenderList::Element *p_element, RasterizerStorageGLES2::Skeleton *p_skeleton); + _FORCE_INLINE_ void _setup_light_type(LightInstance *p_light, ShadowAtlas *shadow_atlas); + _FORCE_INLINE_ void _setup_light(LightInstance *p_light, ShadowAtlas *shadow_atlas, const Transform &p_view_transform); + _FORCE_INLINE_ void _setup_refprobes(ReflectionProbeInstance *p_refprobe1, ReflectionProbeInstance *p_refprobe2, const Transform &p_view_transform, Environment *p_env); + _FORCE_INLINE_ void _render_geometry(RenderList::Element *p_element); virtual void render_scene(const Transform &p_cam_transform, const CameraMatrix &p_cam_projection, bool p_cam_ortogonal, InstanceBase **p_cull_result, int p_cull_count, RID *p_light_cull_result, int p_light_cull_count, RID *p_reflection_probe_cull_result, int p_reflection_probe_cull_count, RID p_environment, RID p_shadow_atlas, RID p_reflection_atlas, RID p_reflection_probe, int p_reflection_probe_pass); virtual void render_shadow(RID p_light, RID p_shadow_atlas, int p_pass, InstanceBase **p_cull_result, int p_cull_count); diff --git a/drivers/gles2/rasterizer_storage_gles2.cpp b/drivers/gles2/rasterizer_storage_gles2.cpp index 8c4325ccde..3beb8eac33 100644 --- a/drivers/gles2/rasterizer_storage_gles2.cpp +++ b/drivers/gles2/rasterizer_storage_gles2.cpp @@ -27,13 +27,13 @@ /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ + #include "rasterizer_storage_gles2.h" -#include "project_settings.h" + +#include "core/math/transform.h" +#include "core/project_settings.h" #include "rasterizer_canvas_gles2.h" #include "rasterizer_scene_gles2.h" - -#include "math/transform.h" - #include "servers/visual/shader_language.h" GLuint RasterizerStorageGLES2::system_fbo = 0; @@ -52,6 +52,10 @@ GLuint RasterizerStorageGLES2::system_fbo = 0; #define _GL_HALF_FLOAT_OES 0x8D61 #endif +#define _EXT_TEXTURE_CUBE_MAP_SEAMLESS 0x884F + +#define _DEPTH_COMPONENT24_OES 0x81A6 + void RasterizerStorageGLES2::bind_quad_array() const { glBindBuffer(GL_ARRAY_BUFFER, resources.quadie); glVertexAttribPointer(VS::ARRAY_VERTEX, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 4, 0); @@ -61,11 +65,12 @@ void RasterizerStorageGLES2::bind_quad_array() const { glEnableVertexAttribArray(VS::ARRAY_TEX_UV); } -Ref<Image> RasterizerStorageGLES2::_get_gl_image_and_format(const Ref<Image> &p_image, Image::Format p_format, uint32_t p_flags, GLenum &r_gl_format, GLenum &r_gl_internal_format, GLenum &r_gl_type, bool &r_compressed) { +Ref<Image> RasterizerStorageGLES2::_get_gl_image_and_format(const Ref<Image> &p_image, Image::Format p_format, uint32_t p_flags, Image::Format &r_real_format, GLenum &r_gl_format, GLenum &r_gl_internal_format, GLenum &r_gl_type, bool &r_compressed) const { r_gl_format = 0; Ref<Image> image = p_image; r_compressed = false; + r_real_format = p_format; bool need_decompress = false; @@ -317,6 +322,7 @@ Ref<Image> RasterizerStorageGLES2::_get_gl_image_and_format(const Ref<Image> &p_ r_gl_format = GL_RGBA; r_gl_internal_format = GL_RGBA; r_gl_type = GL_UNSIGNED_BYTE; + r_real_format = Image::FORMAT_RGBA8; return image; } @@ -352,7 +358,6 @@ void RasterizerStorageGLES2::texture_allocate(RID p_texture, int p_width, int p_ GLenum type; bool compressed = false; - bool srgb = false; if (p_flags & VS::TEXTURE_FLAG_USED_FOR_STREAMING) { p_flags &= ~VS::TEXTURE_FLAG_MIPMAPS; // no mipies for video @@ -382,9 +387,14 @@ void RasterizerStorageGLES2::texture_allocate(RID p_texture, int p_width, int p_ case VS::TEXTURE_TYPE_3D: { texture->images.resize(p_depth_3d); } break; + default: { + ERR_PRINT("Unknown texture type!"); + return; + } } - _get_gl_image_and_format(Ref<Image>(), texture->format, texture->flags, format, internal_format, type, compressed); + Image::Format real_format; + _get_gl_image_and_format(Ref<Image>(), texture->format, texture->flags, real_format, format, internal_format, type, compressed); texture->alloc_width = texture->width; texture->alloc_height = texture->height; @@ -426,7 +436,8 @@ void RasterizerStorageGLES2::texture_set_data(RID p_texture, const Ref<Image> &p texture->images.write[p_layer] = p_image; } - Ref<Image> img = _get_gl_image_and_format(p_image, p_image->get_format(), texture->flags, format, internal_format, type, compressed); + Image::Format real_format; + Ref<Image> img = _get_gl_image_and_format(p_image, p_image->get_format(), texture->flags, real_format, format, internal_format, type, compressed); if (config.shrink_textures_x2 && (p_image->has_mipmaps() || !p_image->is_compressed()) && !(texture->flags & VS::TEXTURE_FLAG_USED_FOR_STREAMING)) { @@ -487,22 +498,6 @@ void RasterizerStorageGLES2::texture_set_data(RID p_texture, const Ref<Image> &p glTexParameterf(texture->target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); } -//set swizle for older format compatibility -#ifdef GLES_OVER_GL - switch (texture->format) { - - case Image::FORMAT_L8: { - - } break; - case Image::FORMAT_LA8: { - - } break; - default: { - - } break; - } -#endif - int mipmaps = ((texture->flags & VS::TEXTURE_FLAG_MIPMAPS) && img->has_mipmaps()) ? img->get_mipmap_count() + 1 : 1; int w = img->get_width(); @@ -570,11 +565,19 @@ Ref<Image> RasterizerStorageGLES2::texture_get_data(RID p_texture, int p_layer) if (texture->type == VS::TEXTURE_TYPE_CUBEMAP && p_layer < 6 && p_layer >= 0 && !texture->images[p_layer].is_null()) { return texture->images[p_layer]; } + #ifdef GLES_OVER_GL + Image::Format real_format; + GLenum gl_format; + GLenum gl_internal_format; + GLenum gl_type; + bool compressed; + _get_gl_image_and_format(Ref<Image>(), texture->format, texture->flags, real_format, gl_format, gl_internal_format, gl_type, compressed); + PoolVector<uint8_t> data; - int data_size = Image::get_image_data_size(texture->alloc_width, texture->alloc_height, texture->format, texture->mipmaps > 1 ? -1 : 0); + int data_size = Image::get_image_data_size(texture->alloc_width, texture->alloc_height, real_format, texture->mipmaps > 1); data.resize(data_size * 2); //add some memory at the end, just in case for buggy drivers PoolVector<uint8_t>::Write wb = data.write(); @@ -585,13 +588,11 @@ Ref<Image> RasterizerStorageGLES2::texture_get_data(RID p_texture, int p_layer) glBindBuffer(GL_PIXEL_PACK_BUFFER, 0); - //print_line("GET FORMAT: " + Image::get_format_name(texture->format) + " mipmaps: " + itos(texture->mipmaps)); - for (int i = 0; i < texture->mipmaps; i++) { int ofs = 0; if (i > 0) { - ofs = Image::get_image_data_size(texture->alloc_width, texture->alloc_height, texture->format, i - 1); + ofs = Image::get_image_data_size(texture->alloc_width, texture->alloc_height, real_format, i - 1); } if (texture->compressed) { @@ -607,13 +608,77 @@ Ref<Image> RasterizerStorageGLES2::texture_get_data(RID p_texture, int p_layer) data.resize(data_size); - Image *img = memnew(Image(texture->alloc_width, texture->alloc_height, texture->mipmaps > 1 ? true : false, texture->format, data)); + Image *img = memnew(Image(texture->alloc_width, texture->alloc_height, texture->mipmaps > 1 ? true : false, real_format, data)); return Ref<Image>(img); #else - ERR_EXPLAIN("Sorry, It's not posible to obtain images back in OpenGL ES"); - return Ref<Image>(); + Image::Format real_format; + GLenum gl_format; + GLenum gl_internal_format; + GLenum gl_type; + bool compressed; + _get_gl_image_and_format(Ref<Image>(), texture->format, texture->flags, real_format, gl_format, gl_internal_format, gl_type, compressed); + + PoolVector<uint8_t> data; + + int data_size = Image::get_image_data_size(texture->alloc_width, texture->alloc_height, Image::FORMAT_RGBA8, false); + + data.resize(data_size * 2); //add some memory at the end, just in case for buggy drivers + PoolVector<uint8_t>::Write wb = data.write(); + + GLuint temp_framebuffer; + glGenFramebuffers(1, &temp_framebuffer); + + GLuint temp_color_texture; + glGenTextures(1, &temp_color_texture); + + glBindFramebuffer(GL_FRAMEBUFFER, temp_framebuffer); + + glBindTexture(GL_TEXTURE_2D, temp_color_texture); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texture->alloc_width, texture->alloc_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); + + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, temp_color_texture, 0); + + glDepthMask(GL_FALSE); + glDisable(GL_DEPTH_TEST); + glDisable(GL_CULL_FACE); + glDisable(GL_BLEND); + glDepthFunc(GL_LEQUAL); + glColorMask(1, 1, 1, 1); + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, texture->tex_id); + + glViewport(0, 0, texture->alloc_width, texture->alloc_height); + + shaders.copy.bind(); + + glClearColor(0.0, 0.0, 0.0, 0.0); + glClear(GL_COLOR_BUFFER_BIT); + bind_quad_array(); + glDrawArrays(GL_TRIANGLE_FAN, 0, 4); + glBindBuffer(GL_ARRAY_BUFFER, 0); + + glReadPixels(0, 0, texture->alloc_width, texture->alloc_height, GL_RGBA, GL_UNSIGNED_BYTE, &wb[0]); + + glDeleteTextures(1, &temp_color_texture); + + glBindFramebuffer(GL_FRAMEBUFFER, 0); + glDeleteFramebuffers(1, &temp_framebuffer); + + wb = PoolVector<uint8_t>::Write(); + + data.resize(data_size); + + Image *img = memnew(Image(texture->alloc_width, texture->alloc_height, false, Image::FORMAT_RGBA8, data)); + if (!texture->compressed) { + img->convert(real_format); + } + + return Ref<Image>(img); + #endif } @@ -624,6 +689,8 @@ void RasterizerStorageGLES2::texture_set_flags(RID p_texture, uint32_t p_flags) bool had_mipmaps = texture->flags & VS::TEXTURE_FLAG_MIPMAPS; + texture->flags = p_flags; + glActiveTexture(GL_TEXTURE0); glBindTexture(texture->target, texture->tex_id); @@ -898,26 +965,27 @@ void RasterizerStorageGLES2::sky_set_texture(RID p_sky, RID p_panorama, int p_ra // attachements for it, so we can fill them by issuing draw calls. GLuint tmp_fb; - glGenFramebuffers(1, &tmp_fb); - glBindFramebuffer(GL_FRAMEBUFFER, tmp_fb); - int size = p_radiance_size; int lod = 0; - shaders.cubemap_filter.set_conditional(CubemapFilterShaderGLES2::USE_SOURCE_PANORAMA, texture->target == GL_TEXTURE_2D); - - shaders.cubemap_filter.bind(); - int mipmaps = 6; int mm_level = mipmaps; - GLenum internal_format = GL_RGBA; - GLenum format = GL_RGBA; - GLenum type = GL_UNSIGNED_BYTE; // This is suboptimal... TODO other format for FBO? + GLenum internal_format = GL_RGB; + GLenum format = GL_RGB; + GLenum type = GL_UNSIGNED_BYTE; // Set the initial (empty) mipmaps +#if 1 + //Mobile hardware (PowerVR specially) prefers this approach, the other one kills the game + for (int i = 0; i < 6; i++) { + glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, internal_format, size, size, 0, format, type, NULL); + } + + glGenerateMipmap(GL_TEXTURE_CUBE_MAP); +#else while (size >= 1) { for (int i = 0; i < 6; i++) { @@ -928,7 +996,14 @@ void RasterizerStorageGLES2::sky_set_texture(RID p_sky, RID p_panorama, int p_ra size >>= 1; } +#endif + //framebuffer + glGenFramebuffers(1, &tmp_fb); + glBindFramebuffer(GL_FRAMEBUFFER, tmp_fb); + + shaders.cubemap_filter.set_conditional(CubemapFilterShaderGLES2::USE_SOURCE_PANORAMA, texture->target == GL_TEXTURE_2D); + shaders.cubemap_filter.bind(); lod = 0; mm_level = mipmaps; @@ -947,6 +1022,7 @@ void RasterizerStorageGLES2::sky_set_texture(RID p_sky, RID p_panorama, int p_ra shaders.cubemap_filter.set_uniform(CubemapFilterShaderGLES2::FACE_ID, i); float roughness = mm_level ? lod / (float)(mipmaps - 1) : 1; + roughness = MIN(1.0, roughness); //keep max at 1 shaders.cubemap_filter.set_uniform(CubemapFilterShaderGLES2::ROUGHNESS, roughness); glDrawArrays(GL_TRIANGLE_FAN, 0, 4); @@ -1049,6 +1125,10 @@ void RasterizerStorageGLES2::_update_shader(Shader *p_shader) const { p_shader->uniforms.clear(); + if (p_shader->code == String()) { + return; //just invalid, but no error + } + ShaderCompilerGLES2::GeneratedCode gen_code; ShaderCompilerGLES2::IdentifierActions *actions = NULL; @@ -1291,6 +1371,15 @@ void RasterizerStorageGLES2::shader_get_param_list(RID p_shader, List<PropertyIn pi.hint = PROPERTY_HINT_RESOURCE_TYPE; pi.hint_string = "CubeMap"; } break; + + case ShaderLanguage::TYPE_SAMPLER2DARRAY: + case ShaderLanguage::TYPE_ISAMPLER2DARRAY: + case ShaderLanguage::TYPE_USAMPLER2DARRAY: + case ShaderLanguage::TYPE_SAMPLER3D: + case ShaderLanguage::TYPE_ISAMPLER3D: + case ShaderLanguage::TYPE_USAMPLER3D: { + // Not implemented in GLES2 + } break; } p_param_list->push_back(pi); @@ -1399,6 +1488,20 @@ Variant RasterizerStorageGLES2::material_get_param(RID p_material, const StringN return material->params[p_param]; } + return material_get_param_default(p_material, p_param); +} + +Variant RasterizerStorageGLES2::material_get_param_default(RID p_material, const StringName &p_param) const { + const Material *material = material_owner.get(p_material); + ERR_FAIL_COND_V(!material, Variant()); + + if (material->shader) { + if (material->shader->uniforms.has(p_param)) { + ShaderLanguage::ShaderNode::Uniform uniform = material->shader->uniforms[p_param]; + Vector<ShaderLanguage::ConstantNode::Value> default_value = uniform.default_value; + return ShaderLanguage::constant_value_to_variant(default_value, uniform.type, uniform.hint); + } + } return Variant(); } @@ -1504,7 +1607,7 @@ void RasterizerStorageGLES2::_update_material(Material *p_material) { if (p_material->shader && p_material->shader->mode == VS::SHADER_SPATIAL) { if (p_material->shader->spatial.blend_mode == Shader::Spatial::BLEND_MODE_MIX && - (!p_material->shader->spatial.uses_alpha || (p_material->shader->spatial.uses_alpha && p_material->shader->spatial.depth_draw_mode == Shader::Spatial::DEPTH_DRAW_ALPHA_PREPASS))) { + (!p_material->shader->spatial.uses_alpha || p_material->shader->spatial.depth_draw_mode == Shader::Spatial::DEPTH_DRAW_ALPHA_PREPASS)) { can_cast_shadow = true; } @@ -1525,13 +1628,13 @@ void RasterizerStorageGLES2::_update_material(Material *p_material) { } for (Map<RasterizerScene::InstanceBase *, int>::Element *E = p_material->instance_owners.front(); E; E = E->next()) { - E->key()->base_material_changed(); + E->key()->base_changed(false, true); } } } } - // uniforms and other thigns will be set in the use_material method in ShaderGLES2 + // uniforms and other things will be set in the use_material method in ShaderGLES2 if (p_material->shader && p_material->shader->texture_count > 0) { @@ -1912,7 +2015,7 @@ void RasterizerStorageGLES2::mesh_add_surface(RID p_mesh, uint32_t p_format, VS: } mesh->surfaces.push_back(surface); - mesh->instance_change_notify(); + mesh->instance_change_notify(true, false); info.vertex_mem += surface->total_data_size; } @@ -1982,7 +2085,7 @@ void RasterizerStorageGLES2::mesh_surface_set_material(RID p_mesh, int p_surface _material_add_geometry(mesh->surfaces[p_surface]->material, mesh->surfaces[p_surface]); } - mesh->instance_material_change_notify(); + mesh->instance_change_notify(false, true); } RID RasterizerStorageGLES2::mesh_surface_get_material(RID p_mesh, int p_surface) const { @@ -2090,13 +2193,11 @@ void RasterizerStorageGLES2::mesh_remove_surface(RID p_mesh, int p_surface) { info.vertex_mem -= surface->total_data_size; - mesh->instance_material_change_notify(); - memdelete(surface); mesh->surfaces.remove(p_surface); - mesh->instance_change_notify(); + mesh->instance_change_notify(true, true); } int RasterizerStorageGLES2::mesh_get_surface_count(RID p_mesh) const { @@ -2601,10 +2702,10 @@ void RasterizerStorageGLES2::update_dirty_multimeshes() { if (multimesh->mesh.is_valid()) { mesh_aabb = mesh_get_aabb(multimesh->mesh, RID()); - } else { - mesh_aabb.size += Vector3(0.001, 0.001, 0.001); } + mesh_aabb.size += Vector3(0.001, 0.001, 0.001); //in case mesh is empty in one of the sides + int stride = multimesh->color_floats + multimesh->xform_floats + multimesh->custom_data_floats; int count = multimesh->data.size(); float *data = multimesh->data.ptrw(); @@ -2670,7 +2771,7 @@ void RasterizerStorageGLES2::update_dirty_multimeshes() { multimesh->dirty_aabb = false; multimesh->dirty_data = false; - multimesh->instance_change_notify(); + multimesh->instance_change_notify(true, false); multimesh_update_list.remove(multimesh_update_list.first()); } @@ -2679,45 +2780,132 @@ void RasterizerStorageGLES2::update_dirty_multimeshes() { /* IMMEDIATE API */ RID RasterizerStorageGLES2::immediate_create() { - return RID(); + Immediate *im = memnew(Immediate); + return immediate_owner.make_rid(im); } -void RasterizerStorageGLES2::immediate_begin(RID p_immediate, VS::PrimitiveType p_rimitive, RID p_texture) { +void RasterizerStorageGLES2::immediate_begin(RID p_immediate, VS::PrimitiveType p_primitive, RID p_texture) { + Immediate *im = immediate_owner.get(p_immediate); + ERR_FAIL_COND(!im); + ERR_FAIL_COND(im->building); + + Immediate::Chunk ic; + ic.texture = p_texture; + ic.primitive = p_primitive; + im->chunks.push_back(ic); + im->mask = 0; + im->building = true; } void RasterizerStorageGLES2::immediate_vertex(RID p_immediate, const Vector3 &p_vertex) { + Immediate *im = immediate_owner.get(p_immediate); + ERR_FAIL_COND(!im); + ERR_FAIL_COND(!im->building); + + Immediate::Chunk *c = &im->chunks.back()->get(); + + if (c->vertices.empty() && im->chunks.size() == 1) { + im->aabb.position = p_vertex; + im->aabb.size = Vector3(); + } else { + im->aabb.expand_to(p_vertex); + } + + if (im->mask & VS::ARRAY_FORMAT_NORMAL) + c->normals.push_back(chunk_normal); + if (im->mask & VS::ARRAY_FORMAT_TANGENT) + c->tangents.push_back(chunk_tangent); + if (im->mask & VS::ARRAY_FORMAT_COLOR) + c->colors.push_back(chunk_color); + if (im->mask & VS::ARRAY_FORMAT_TEX_UV) + c->uvs.push_back(chunk_uv); + if (im->mask & VS::ARRAY_FORMAT_TEX_UV2) + c->uv2s.push_back(chunk_uv2); + im->mask |= VS::ARRAY_FORMAT_VERTEX; + c->vertices.push_back(p_vertex); } void RasterizerStorageGLES2::immediate_normal(RID p_immediate, const Vector3 &p_normal) { + Immediate *im = immediate_owner.get(p_immediate); + ERR_FAIL_COND(!im); + ERR_FAIL_COND(!im->building); + + im->mask |= VS::ARRAY_FORMAT_NORMAL; + chunk_normal = p_normal; } void RasterizerStorageGLES2::immediate_tangent(RID p_immediate, const Plane &p_tangent) { + Immediate *im = immediate_owner.get(p_immediate); + ERR_FAIL_COND(!im); + ERR_FAIL_COND(!im->building); + + im->mask |= VS::ARRAY_FORMAT_TANGENT; + chunk_tangent = p_tangent; } void RasterizerStorageGLES2::immediate_color(RID p_immediate, const Color &p_color) { + Immediate *im = immediate_owner.get(p_immediate); + ERR_FAIL_COND(!im); + ERR_FAIL_COND(!im->building); + + im->mask |= VS::ARRAY_FORMAT_COLOR; + chunk_color = p_color; } void RasterizerStorageGLES2::immediate_uv(RID p_immediate, const Vector2 &tex_uv) { + Immediate *im = immediate_owner.get(p_immediate); + ERR_FAIL_COND(!im); + ERR_FAIL_COND(!im->building); + + im->mask |= VS::ARRAY_FORMAT_TEX_UV; + chunk_uv = tex_uv; } void RasterizerStorageGLES2::immediate_uv2(RID p_immediate, const Vector2 &tex_uv) { + Immediate *im = immediate_owner.get(p_immediate); + ERR_FAIL_COND(!im); + ERR_FAIL_COND(!im->building); + + im->mask |= VS::ARRAY_FORMAT_TEX_UV2; + chunk_uv2 = tex_uv; } void RasterizerStorageGLES2::immediate_end(RID p_immediate) { + Immediate *im = immediate_owner.get(p_immediate); + ERR_FAIL_COND(!im); + ERR_FAIL_COND(!im->building); + + im->building = false; + im->instance_change_notify(true, false); } void RasterizerStorageGLES2::immediate_clear(RID p_immediate) { + Immediate *im = immediate_owner.get(p_immediate); + ERR_FAIL_COND(!im); + ERR_FAIL_COND(im->building); + + im->chunks.clear(); + im->instance_change_notify(true, false); } AABB RasterizerStorageGLES2::immediate_get_aabb(RID p_immediate) const { - return AABB(); + Immediate *im = immediate_owner.get(p_immediate); + ERR_FAIL_COND_V(!im, AABB()); + return im->aabb; } void RasterizerStorageGLES2::immediate_set_material(RID p_immediate, RID p_material) { + Immediate *im = immediate_owner.get(p_immediate); + ERR_FAIL_COND(!im); + + im->material = p_material; + im->instance_change_notify(false, true); } RID RasterizerStorageGLES2::immediate_get_material(RID p_immediate) const { - return RID(); + const Immediate *im = immediate_owner.get(p_immediate); + ERR_FAIL_COND_V(!im, RID()); + return im->material; } /* SKELETON API */ @@ -2749,7 +2937,11 @@ void RasterizerStorageGLES2::skeleton_allocate(RID p_skeleton, int p_bones, bool glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, skeleton->tex_id); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, p_bones * 3, 1, 0, GL_RGB, GL_FLOAT, NULL); +#ifdef GLES_OVER_GL + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, p_bones * 3, 1, 0, GL_RGBA, GL_FLOAT, NULL); +#else + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, p_bones * 3, 1, 0, GL_RGBA, GL_FLOAT, NULL); +#endif glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); @@ -2918,7 +3110,7 @@ void RasterizerStorageGLES2::update_dirty_skeletons() { } for (Set<RasterizerScene::InstanceBase *>::Element *E = skeleton->instances.front(); E; E = E->next()) { - E->get()->base_changed(); + E->get()->base_changed(true, false); } skeleton_update_list.remove(skeleton_update_list.first()); @@ -2983,8 +3175,9 @@ void RasterizerStorageGLES2::light_set_param(RID p_light, VS::LightParam p_param case VS::LIGHT_PARAM_SHADOW_NORMAL_BIAS: case VS::LIGHT_PARAM_SHADOW_BIAS: { light->version++; - light->instance_change_notify(); + light->instance_change_notify(true, false); } break; + default: {} } light->param[p_param] = p_value; @@ -2997,7 +3190,7 @@ void RasterizerStorageGLES2::light_set_shadow(RID p_light, bool p_enabled) { light->shadow = p_enabled; light->version++; - light->instance_change_notify(); + light->instance_change_notify(true, false); } void RasterizerStorageGLES2::light_set_shadow_color(RID p_light, const Color &p_color) { @@ -3028,7 +3221,7 @@ void RasterizerStorageGLES2::light_set_cull_mask(RID p_light, uint32_t p_mask) { light->cull_mask = p_mask; light->version++; - light->instance_change_notify(); + light->instance_change_notify(true, false); } void RasterizerStorageGLES2::light_set_reverse_cull_face_mode(RID p_light, bool p_enabled) { @@ -3036,6 +3229,9 @@ void RasterizerStorageGLES2::light_set_reverse_cull_face_mode(RID p_light, bool ERR_FAIL_COND(!light); light->reverse_cull = p_enabled; + + light->version++; + light->instance_change_notify(true, false); } void RasterizerStorageGLES2::light_omni_set_shadow_mode(RID p_light, VS::LightOmniShadowMode p_mode) { @@ -3045,7 +3241,7 @@ void RasterizerStorageGLES2::light_omni_set_shadow_mode(RID p_light, VS::LightOm light->omni_shadow_mode = p_mode; light->version++; - light->instance_change_notify(); + light->instance_change_notify(true, false); } VS::LightOmniShadowMode RasterizerStorageGLES2::light_omni_get_shadow_mode(RID p_light) { @@ -3062,7 +3258,7 @@ void RasterizerStorageGLES2::light_omni_set_shadow_detail(RID p_light, VS::Light light->omni_shadow_detail = p_detail; light->version++; - light->instance_change_notify(); + light->instance_change_notify(true, false); } void RasterizerStorageGLES2::light_directional_set_shadow_mode(RID p_light, VS::LightDirectionalShadowMode p_mode) { @@ -3072,7 +3268,7 @@ void RasterizerStorageGLES2::light_directional_set_shadow_mode(RID p_light, VS:: light->directional_shadow_mode = p_mode; light->version++; - light->instance_change_notify(); + light->instance_change_notify(true, false); } void RasterizerStorageGLES2::light_directional_set_blend_splits(RID p_light, bool p_enable) { @@ -3082,7 +3278,7 @@ void RasterizerStorageGLES2::light_directional_set_blend_splits(RID p_light, boo light->directional_blend_splits = p_enable; light->version++; - light->instance_change_notify(); + light->instance_change_notify(true, false); } bool RasterizerStorageGLES2::light_directional_get_blend_splits(RID p_light) const { @@ -3176,69 +3372,194 @@ AABB RasterizerStorageGLES2::light_get_aabb(RID p_light) const { /* PROBE API */ RID RasterizerStorageGLES2::reflection_probe_create() { - return RID(); + + ReflectionProbe *reflection_probe = memnew(ReflectionProbe); + + reflection_probe->intensity = 1.0; + reflection_probe->interior_ambient = Color(); + reflection_probe->interior_ambient_energy = 1.0; + reflection_probe->max_distance = 0; + reflection_probe->extents = Vector3(1, 1, 1); + reflection_probe->origin_offset = Vector3(0, 0, 0); + reflection_probe->interior = false; + reflection_probe->box_projection = false; + reflection_probe->enable_shadows = false; + reflection_probe->cull_mask = (1 << 20) - 1; + reflection_probe->update_mode = VS::REFLECTION_PROBE_UPDATE_ONCE; + reflection_probe->resolution = 128; + + return reflection_probe_owner.make_rid(reflection_probe); } void RasterizerStorageGLES2::reflection_probe_set_update_mode(RID p_probe, VS::ReflectionProbeUpdateMode p_mode) { + + ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe); + ERR_FAIL_COND(!reflection_probe); + + reflection_probe->update_mode = p_mode; + reflection_probe->instance_change_notify(true, false); } void RasterizerStorageGLES2::reflection_probe_set_intensity(RID p_probe, float p_intensity) { + + ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe); + ERR_FAIL_COND(!reflection_probe); + + reflection_probe->intensity = p_intensity; } void RasterizerStorageGLES2::reflection_probe_set_interior_ambient(RID p_probe, const Color &p_ambient) { + + ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe); + ERR_FAIL_COND(!reflection_probe); + + reflection_probe->interior_ambient = p_ambient; } void RasterizerStorageGLES2::reflection_probe_set_interior_ambient_energy(RID p_probe, float p_energy) { + + ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe); + ERR_FAIL_COND(!reflection_probe); + + reflection_probe->interior_ambient_energy = p_energy; } void RasterizerStorageGLES2::reflection_probe_set_interior_ambient_probe_contribution(RID p_probe, float p_contrib) { + + ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe); + ERR_FAIL_COND(!reflection_probe); + + reflection_probe->interior_ambient_probe_contrib = p_contrib; } void RasterizerStorageGLES2::reflection_probe_set_max_distance(RID p_probe, float p_distance) { -} -void RasterizerStorageGLES2::reflection_probe_set_extents(RID p_probe, const Vector3 &p_extents) { + ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe); + ERR_FAIL_COND(!reflection_probe); + + reflection_probe->max_distance = p_distance; + reflection_probe->instance_change_notify(true, false); } +void RasterizerStorageGLES2::reflection_probe_set_extents(RID p_probe, const Vector3 &p_extents) { + + ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe); + ERR_FAIL_COND(!reflection_probe); + reflection_probe->extents = p_extents; + reflection_probe->instance_change_notify(true, false); +} void RasterizerStorageGLES2::reflection_probe_set_origin_offset(RID p_probe, const Vector3 &p_offset) { + + ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe); + ERR_FAIL_COND(!reflection_probe); + + reflection_probe->origin_offset = p_offset; + reflection_probe->instance_change_notify(true, false); } void RasterizerStorageGLES2::reflection_probe_set_as_interior(RID p_probe, bool p_enable) { -} + ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe); + ERR_FAIL_COND(!reflection_probe); + + reflection_probe->interior = p_enable; +} void RasterizerStorageGLES2::reflection_probe_set_enable_box_projection(RID p_probe, bool p_enable) { + + ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe); + ERR_FAIL_COND(!reflection_probe); + + reflection_probe->box_projection = p_enable; } void RasterizerStorageGLES2::reflection_probe_set_enable_shadows(RID p_probe, bool p_enable) { -} + ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe); + ERR_FAIL_COND(!reflection_probe); + + reflection_probe->enable_shadows = p_enable; + reflection_probe->instance_change_notify(true, false); +} void RasterizerStorageGLES2::reflection_probe_set_cull_mask(RID p_probe, uint32_t p_layers) { + + ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe); + ERR_FAIL_COND(!reflection_probe); + + reflection_probe->cull_mask = p_layers; + reflection_probe->instance_change_notify(true, false); +} + +void RasterizerStorageGLES2::reflection_probe_set_resolution(RID p_probe, int p_resolution) { + + ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe); + ERR_FAIL_COND(!reflection_probe); + + reflection_probe->resolution = p_resolution; } AABB RasterizerStorageGLES2::reflection_probe_get_aabb(RID p_probe) const { - return AABB(); + const ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe); + ERR_FAIL_COND_V(!reflection_probe, AABB()); + + AABB aabb; + aabb.position = -reflection_probe->extents; + aabb.size = reflection_probe->extents * 2.0; + + return aabb; } VS::ReflectionProbeUpdateMode RasterizerStorageGLES2::reflection_probe_get_update_mode(RID p_probe) const { - return VS::REFLECTION_PROBE_UPDATE_ALWAYS; + + const ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe); + ERR_FAIL_COND_V(!reflection_probe, VS::REFLECTION_PROBE_UPDATE_ALWAYS); + + return reflection_probe->update_mode; } uint32_t RasterizerStorageGLES2::reflection_probe_get_cull_mask(RID p_probe) const { - return 0; + + const ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe); + ERR_FAIL_COND_V(!reflection_probe, 0); + + return reflection_probe->cull_mask; } Vector3 RasterizerStorageGLES2::reflection_probe_get_extents(RID p_probe) const { - return Vector3(); + + const ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe); + ERR_FAIL_COND_V(!reflection_probe, Vector3()); + + return reflection_probe->extents; } Vector3 RasterizerStorageGLES2::reflection_probe_get_origin_offset(RID p_probe) const { - return Vector3(); + + const ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe); + ERR_FAIL_COND_V(!reflection_probe, Vector3()); + + return reflection_probe->origin_offset; } bool RasterizerStorageGLES2::reflection_probe_renders_shadows(RID p_probe) const { - return false; + + const ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe); + ERR_FAIL_COND_V(!reflection_probe, false); + + return reflection_probe->enable_shadows; } float RasterizerStorageGLES2::reflection_probe_get_origin_max_distance(RID p_probe) const { - return 0; + + const ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe); + ERR_FAIL_COND_V(!reflection_probe, 0); + + return reflection_probe->max_distance; +} + +int RasterizerStorageGLES2::reflection_probe_get_resolution(RID p_probe) const { + + const ReflectionProbe *reflection_probe = reflection_probe_owner.getornull(p_probe); + ERR_FAIL_COND_V(!reflection_probe, 0); + + return reflection_probe->resolution; } RID RasterizerStorageGLES2::gi_probe_create() { @@ -3339,46 +3660,100 @@ void RasterizerStorageGLES2::gi_probe_dynamic_data_update(RID p_gi_probe_data, i /////// RID RasterizerStorageGLES2::lightmap_capture_create() { - return RID(); + + LightmapCapture *capture = memnew(LightmapCapture); + return lightmap_capture_data_owner.make_rid(capture); } void RasterizerStorageGLES2::lightmap_capture_set_bounds(RID p_capture, const AABB &p_bounds) { -} -AABB RasterizerStorageGLES2::lightmap_capture_get_bounds(RID p_capture) const { - return AABB(); + LightmapCapture *capture = lightmap_capture_data_owner.getornull(p_capture); + ERR_FAIL_COND(!capture); + capture->bounds = p_bounds; + capture->instance_change_notify(true, false); } +AABB RasterizerStorageGLES2::lightmap_capture_get_bounds(RID p_capture) const { -void RasterizerStorageGLES2::lightmap_capture_set_octree(RID p_capture, const PoolVector<uint8_t> &p_octree) { + const LightmapCapture *capture = lightmap_capture_data_owner.getornull(p_capture); + ERR_FAIL_COND_V(!capture, AABB()); + return capture->bounds; } +void RasterizerStorageGLES2::lightmap_capture_set_octree(RID p_capture, const PoolVector<uint8_t> &p_octree) { + + LightmapCapture *capture = lightmap_capture_data_owner.getornull(p_capture); + ERR_FAIL_COND(!capture); + ERR_FAIL_COND(p_octree.size() == 0 || (p_octree.size() % sizeof(LightmapCaptureOctree)) != 0); + + capture->octree.resize(p_octree.size() / sizeof(LightmapCaptureOctree)); + if (p_octree.size()) { + PoolVector<LightmapCaptureOctree>::Write w = capture->octree.write(); + PoolVector<uint8_t>::Read r = p_octree.read(); + copymem(w.ptr(), r.ptr(), p_octree.size()); + } + capture->instance_change_notify(true, false); +} PoolVector<uint8_t> RasterizerStorageGLES2::lightmap_capture_get_octree(RID p_capture) const { - return PoolVector<uint8_t>(); + + const LightmapCapture *capture = lightmap_capture_data_owner.getornull(p_capture); + ERR_FAIL_COND_V(!capture, PoolVector<uint8_t>()); + + if (capture->octree.size() == 0) + return PoolVector<uint8_t>(); + + PoolVector<uint8_t> ret; + ret.resize(capture->octree.size() * sizeof(LightmapCaptureOctree)); + { + PoolVector<LightmapCaptureOctree>::Read r = capture->octree.read(); + PoolVector<uint8_t>::Write w = ret.write(); + copymem(w.ptr(), r.ptr(), ret.size()); + } + + return ret; } void RasterizerStorageGLES2::lightmap_capture_set_octree_cell_transform(RID p_capture, const Transform &p_xform) { + LightmapCapture *capture = lightmap_capture_data_owner.getornull(p_capture); + ERR_FAIL_COND(!capture); + capture->cell_xform = p_xform; } Transform RasterizerStorageGLES2::lightmap_capture_get_octree_cell_transform(RID p_capture) const { - return Transform(); + const LightmapCapture *capture = lightmap_capture_data_owner.getornull(p_capture); + ERR_FAIL_COND_V(!capture, Transform()); + return capture->cell_xform; } void RasterizerStorageGLES2::lightmap_capture_set_octree_cell_subdiv(RID p_capture, int p_subdiv) { + LightmapCapture *capture = lightmap_capture_data_owner.getornull(p_capture); + ERR_FAIL_COND(!capture); + capture->cell_subdiv = p_subdiv; } int RasterizerStorageGLES2::lightmap_capture_get_octree_cell_subdiv(RID p_capture) const { - return 0; + const LightmapCapture *capture = lightmap_capture_data_owner.getornull(p_capture); + ERR_FAIL_COND_V(!capture, 0); + return capture->cell_subdiv; } void RasterizerStorageGLES2::lightmap_capture_set_energy(RID p_capture, float p_energy) { + + LightmapCapture *capture = lightmap_capture_data_owner.getornull(p_capture); + ERR_FAIL_COND(!capture); + capture->energy = p_energy; } float RasterizerStorageGLES2::lightmap_capture_get_energy(RID p_capture) const { - return 0.0; + + const LightmapCapture *capture = lightmap_capture_data_owner.getornull(p_capture); + ERR_FAIL_COND_V(!capture, 0); + return capture->energy; } const PoolVector<RasterizerStorage::LightmapCaptureOctree> *RasterizerStorageGLES2::lightmap_capture_get_octree_ptr(RID p_capture) const { - return NULL; + const LightmapCapture *capture = lightmap_capture_data_owner.getornull(p_capture); + ERR_FAIL_COND_V(!capture, NULL); + return &capture->octree; } /////// @@ -3467,18 +3842,122 @@ RID RasterizerStorageGLES2::particles_get_draw_pass_mesh(RID p_particles, int p_ void RasterizerStorageGLES2::update_particles() { } +bool RasterizerStorageGLES2::particles_is_inactive(RID p_particles) const { + return true; +} + //////// void RasterizerStorageGLES2::instance_add_skeleton(RID p_skeleton, RasterizerScene::InstanceBase *p_instance) { + + Skeleton *skeleton = skeleton_owner.getornull(p_skeleton); + ERR_FAIL_COND(!skeleton); + + skeleton->instances.insert(p_instance); } void RasterizerStorageGLES2::instance_remove_skeleton(RID p_skeleton, RasterizerScene::InstanceBase *p_instance) { + + Skeleton *skeleton = skeleton_owner.getornull(p_skeleton); + ERR_FAIL_COND(!skeleton); + + skeleton->instances.erase(p_instance); } void RasterizerStorageGLES2::instance_add_dependency(RID p_base, RasterizerScene::InstanceBase *p_instance) { + + Instantiable *inst = NULL; + switch (p_instance->base_type) { + case VS::INSTANCE_MESH: { + inst = mesh_owner.getornull(p_base); + ERR_FAIL_COND(!inst); + } break; + case VS::INSTANCE_MULTIMESH: { + inst = multimesh_owner.getornull(p_base); + ERR_FAIL_COND(!inst); + } break; + case VS::INSTANCE_IMMEDIATE: { + inst = immediate_owner.getornull(p_base); + ERR_FAIL_COND(!inst); + } break; + /*case VS::INSTANCE_PARTICLES: { + inst = particles_owner.getornull(p_base); + ERR_FAIL_COND(!inst); + } break;*/ + case VS::INSTANCE_REFLECTION_PROBE: { + inst = reflection_probe_owner.getornull(p_base); + ERR_FAIL_COND(!inst); + } break; + case VS::INSTANCE_LIGHT: { + inst = light_owner.getornull(p_base); + ERR_FAIL_COND(!inst); + } break; + /*case VS::INSTANCE_GI_PROBE: { + inst = gi_probe_owner.getornull(p_base); + ERR_FAIL_COND(!inst); + } break;*/ + case VS::INSTANCE_LIGHTMAP_CAPTURE: { + inst = lightmap_capture_data_owner.getornull(p_base); + ERR_FAIL_COND(!inst); + } break; + default: { + if (!inst) { + ERR_FAIL(); + } + } + } + + inst->instance_list.add(&p_instance->dependency_item); } void RasterizerStorageGLES2::instance_remove_dependency(RID p_base, RasterizerScene::InstanceBase *p_instance) { + + Instantiable *inst = NULL; + + switch (p_instance->base_type) { + case VS::INSTANCE_MESH: { + inst = mesh_owner.getornull(p_base); + ERR_FAIL_COND(!inst); + } break; + case VS::INSTANCE_MULTIMESH: { + inst = multimesh_owner.getornull(p_base); + ERR_FAIL_COND(!inst); + } break; + case VS::INSTANCE_IMMEDIATE: { + inst = immediate_owner.getornull(p_base); + ERR_FAIL_COND(!inst); + } break; + /*case VS::INSTANCE_PARTICLES: { + inst = particles_owner.getornull(p_base); + ERR_FAIL_COND(!inst); + } break;*/ + case VS::INSTANCE_REFLECTION_PROBE: { + inst = reflection_probe_owner.getornull(p_base); + ERR_FAIL_COND(!inst); + } break; + case VS::INSTANCE_LIGHT: { + inst = light_owner.getornull(p_base); + ERR_FAIL_COND(!inst); + } break; + /*case VS::INSTANCE_GI_PROBE: { + inst = gi_probe_owner.getornull(p_base); + ERR_FAIL_COND(!inst); + } break; */ + case VS::INSTANCE_LIGHTMAP_CAPTURE: { + inst = lightmap_capture_data_owner.getornull(p_base); + ERR_FAIL_COND(!inst); + } break; + default: { + + if (!inst) { + ERR_FAIL(); + } + } + } + + ERR_FAIL_COND(!inst); + + inst->instance_list.remove(&p_instance->dependency_item); } /* RENDER TARGET */ @@ -3520,7 +3999,7 @@ void RasterizerStorageGLES2::_render_target_allocate(RenderTarget *rt) { glGenRenderbuffers(1, &rt->depth); glBindRenderbuffer(GL_RENDERBUFFER, rt->depth); - glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, rt->width, rt->height); + glRenderbufferStorage(GL_RENDERBUFFER, _DEPTH_COMPONENT24_OES, rt->width, rt->height); glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rt->depth); GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER); @@ -3554,6 +4033,7 @@ void RasterizerStorageGLES2::_render_target_allocate(RenderTarget *rt) { texture_set_flags(rt->texture, texture->flags); + glClearColor(0, 0, 0, 0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // copy texscreen buffers @@ -3729,15 +4209,19 @@ VS::InstanceType RasterizerStorageGLES2::get_base_type(RID p_rid) const { if (mesh_owner.owns(p_rid)) { return VS::INSTANCE_MESH; - } - if (light_owner.owns(p_rid)) { + } else if (light_owner.owns(p_rid)) { return VS::INSTANCE_LIGHT; - } - if (multimesh_owner.owns(p_rid)) { + } else if (multimesh_owner.owns(p_rid)) { return VS::INSTANCE_MULTIMESH; + } else if (immediate_owner.owns(p_rid)) { + return VS::INSTANCE_IMMEDIATE; + } else if (reflection_probe_owner.owns(p_rid)) { + return VS::INSTANCE_REFLECTION_PROBE; + } else if (lightmap_capture_data_owner.owns(p_rid)) { + return VS::INSTANCE_LIGHTMAP_CAPTURE; + } else { + return VS::INSTANCE_NONE; } - - return VS::INSTANCE_NONE; } bool RasterizerStorageGLES2::free(RID p_rid) { @@ -3895,6 +4379,14 @@ bool RasterizerStorageGLES2::free(RID p_rid) { memdelete(multimesh); return true; + } else if (immediate_owner.owns(p_rid)) { + Immediate *im = immediate_owner.get(p_rid); + im->instance_remove_deps(); + + immediate_owner.free(p_rid); + memdelete(im); + + return true; } else if (light_owner.owns(p_rid)) { Light *light = light_owner.get(p_rid); @@ -3904,6 +4396,25 @@ bool RasterizerStorageGLES2::free(RID p_rid) { memdelete(light); return true; + } else if (reflection_probe_owner.owns(p_rid)) { + + // delete the texture + ReflectionProbe *reflection_probe = reflection_probe_owner.get(p_rid); + reflection_probe->instance_remove_deps(); + + reflection_probe_owner.free(p_rid); + memdelete(reflection_probe); + + return true; + } else if (lightmap_capture_data_owner.owns(p_rid)) { + + // delete the texture + LightmapCapture *lightmap_capture = lightmap_capture_data_owner.get(p_rid); + lightmap_capture->instance_remove_deps(); + + lightmap_capture_data_owner.free(p_rid); + memdelete(lightmap_capture); + return true; } else { return false; } @@ -3954,16 +4465,17 @@ void RasterizerStorageGLES2::initialize() { } } + config.keep_original_textures = false; config.shrink_textures_x2 = false; - config.float_texture_supported = config.extensions.find("GL_ARB_texture_float") != NULL || config.extensions.find("GL_OES_texture_float") != NULL; - config.s3tc_supported = config.extensions.find("GL_EXT_texture_compression_s3tc") != NULL; - config.etc1_supported = config.extensions.has("GL_OES_compressed_ETC1_RGB8_texture") != NULL; + + config.float_texture_supported = config.extensions.has("GL_ARB_texture_float") || config.extensions.has("GL_OES_texture_float"); + config.s3tc_supported = config.extensions.has("GL_EXT_texture_compression_s3tc"); + config.etc1_supported = config.extensions.has("GL_OES_compressed_ETC1_RGB8_texture"); frame.count = 0; frame.delta = 0; frame.current_rt = NULL; frame.clear_request = false; - // config.keep_original_textures = false; glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &config.max_texture_image_units); glGetIntegerv(GL_MAX_TEXTURE_SIZE, &config.max_texture_size); @@ -4066,13 +4578,13 @@ void RasterizerStorageGLES2::initialize() { // radical inverse vdc cache texture // used for cubemap filtering - if (config.float_texture_supported) { + if (true /*||config.float_texture_supported*/) { //uint8 is similar and works everywhere glGenTextures(1, &resources.radical_inverse_vdc_cache_tex); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, resources.radical_inverse_vdc_cache_tex); - float radical_inverse[512]; + uint8_t radical_inverse[512]; for (uint32_t i = 0; i < 512; i++) { uint32_t bits = i; @@ -4084,14 +4596,24 @@ void RasterizerStorageGLES2::initialize() { bits = ((bits & 0x00FF00FF) << 8) | ((bits & 0xFF00FF00) >> 8); float value = float(bits) * 2.3283064365386963e-10; - - radical_inverse[i] = value; + radical_inverse[i] = uint8_t(CLAMP(value * 255.0, 0, 255)); } - glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, 512, 1, 0, GL_LUMINANCE, GL_FLOAT, radical_inverse); + glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, 512, 1, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, radical_inverse); glBindTexture(GL_TEXTURE_2D, 0); } + +#ifdef GLES_OVER_GL + //this needs to be enabled manually in OpenGL 2.1 + + glEnable(_EXT_TEXTURE_CUBE_MAP_SEAMLESS); + glEnable(GL_POINT_SPRITE); + glEnable(GL_VERTEX_PROGRAM_POINT_SIZE); +#endif + + config.force_vertex_shading = GLOBAL_GET("rendering/quality/shading/force_vertex_shading"); + config.use_fast_texture_filter = GLOBAL_GET("rendering/quality/filters/use_nearest_mipmap_filter"); } void RasterizerStorageGLES2::finalize() { @@ -4106,6 +4628,7 @@ void RasterizerStorageGLES2::update_dirty_resources() { update_dirty_shaders(); update_dirty_materials(); update_dirty_skeletons(); + update_dirty_multimeshes(); } RasterizerStorageGLES2::RasterizerStorageGLES2() { diff --git a/drivers/gles2/rasterizer_storage_gles2.h b/drivers/gles2/rasterizer_storage_gles2.h index 8bc3369dbb..ec7616b9f3 100644 --- a/drivers/gles2/rasterizer_storage_gles2.h +++ b/drivers/gles2/rasterizer_storage_gles2.h @@ -27,11 +27,12 @@ /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ + #ifndef RASTERIZERSTORAGEGLES2_H #define RASTERIZERSTORAGEGLES2_H -#include "dvector.h" -#include "self_list.h" +#include "core/dvector.h" +#include "core/self_list.h" #include "servers/visual/rasterizer.h" #include "servers/visual/shader_language.h" #include "shader_compiler_gles2.h" @@ -42,7 +43,6 @@ /* #include "shaders/blend_shape.glsl.gen.h" #include "shaders/canvas.glsl.gen.h" -#include "shaders/copy.glsl.gen.h" #include "shaders/particles.glsl.gen.h" */ @@ -60,20 +60,12 @@ public: bool shrink_textures_x2; bool use_fast_texture_filter; - // bool use_anisotropic_filter; - - bool hdr_supported; - - bool use_rgba_2d_shadows; - - // float anisotropic_level; int max_texture_image_units; int max_texture_size; - bool generate_wireframes; - - bool use_texture_array_environment; + // TODO implement wireframe in GLES2 + // bool generate_wireframes; Set<String> extensions; @@ -83,7 +75,6 @@ public: bool keep_original_textures; - bool no_depth_prepass; bool force_vertex_shading; } config; @@ -140,10 +131,9 @@ public: } } render, render_final, snap; - Info() { - - texture_mem = 0; - vertex_mem = 0; + Info() : + texture_mem(0), + vertex_mem(0) { render.reset(); render_final.reset(); } @@ -156,23 +146,15 @@ public: //////////////////////////////////DATA/////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////// - struct Instanciable : public RID_Data { + struct Instantiable : public RID_Data { SelfList<RasterizerScene::InstanceBase>::List instance_list; - _FORCE_INLINE_ void instance_change_notify() { - SelfList<RasterizerScene::InstanceBase> *instances = instance_list.first(); - - while (instances) { - instances->self()->base_changed(); - instances = instances->next(); - } - } + _FORCE_INLINE_ void instance_change_notify(bool p_aabb, bool p_materials) { - _FORCE_INLINE_ void instance_material_change_notify() { SelfList<RasterizerScene::InstanceBase> *instances = instance_list.first(); - while (instances) { - instances->self()->base_material_changed(); + + instances->self()->base_changed(p_aabb, p_materials); instances = instances->next(); } } @@ -186,15 +168,15 @@ public: } } - Instanciable() {} + Instantiable() {} - virtual ~Instanciable() {} + virtual ~Instantiable() {} }; - struct GeometryOwner : public Instanciable { + struct GeometryOwner : public Instantiable { }; - struct Geometry : public Instanciable { + struct Geometry : public Instantiable { enum Type { GEOMETRY_INVALID, @@ -271,33 +253,31 @@ public: VisualServer::TextureDetectCallback detect_normal; void *detect_normal_ud; - Texture() { - flags = 0; - width = 0; - height = 0; - alloc_width = 0; - alloc_height = 0; - format = Image::FORMAT_L8; - - target = 0; - - data_size = 0; - total_data_size = 0; - ignore_mipmaps = false; - - compressed = false; - - active = false; - - tex_id = 0; - - stored_cube_sides = 0; - - proxy = NULL; - - render_target = NULL; - - redraw_if_visible = false; + Texture() : + proxy(NULL), + flags(0), + width(0), + height(0), + alloc_width(0), + alloc_height(0), + format(Image::FORMAT_L8), + target(0), + data_size(0), + total_data_size(0), + ignore_mipmaps(false), + compressed(false), + mipmaps(0), + active(false), + tex_id(0), + stored_cube_sides(0), + render_target(NULL), + redraw_if_visible(false), + detect_3d(NULL), + detect_3d_ud(NULL), + detect_srgb(NULL), + detect_srgb_ud(NULL), + detect_normal(NULL), + detect_normal_ud(NULL) { } _ALWAYS_INLINE_ Texture *get_ptr() { @@ -325,7 +305,7 @@ public: mutable RID_Owner<Texture> texture_owner; - Ref<Image> _get_gl_image_and_format(const Ref<Image> &p_image, Image::Format p_format, uint32_t p_flags, GLenum &r_gl_format, GLenum &r_gl_internal_format, GLenum &r_gl_type, bool &r_compressed); + Ref<Image> _get_gl_image_and_format(const Ref<Image> &p_image, Image::Format p_format, uint32_t p_flags, Image::Format &r_real_format, GLenum &r_gl_format, GLenum &r_gl_internal_format, GLenum &r_gl_type, bool &r_compressed) const; virtual RID texture_create(); virtual void texture_allocate(RID p_texture, int p_width, int p_height, int p_depth_3d, Image::Format p_format, VS::TextureType p_type, uint32_t p_flags = VS::TEXTURE_FLAGS_DEFAULT); @@ -405,6 +385,9 @@ public: String path; + uint32_t index; + uint64_t last_pass; + struct CanvasItem { enum BlendMode { @@ -426,6 +409,7 @@ public: int light_mode; */ + bool uses_screen_texture; bool uses_screen_uv; bool uses_time; @@ -490,6 +474,7 @@ public: valid = false; custom_code_id = 0; version = 1; + last_pass = 0; } }; @@ -562,6 +547,7 @@ public: virtual void material_set_param(RID p_material, const StringName &p_param, const Variant &p_value); virtual Variant material_get_param(RID p_material, const StringName &p_param) const; + virtual Variant material_get_param_default(RID p_material, const StringName &p_param) const; virtual void material_set_line_width(RID p_material, float p_width); virtual void material_set_next_pass(RID p_material, RID p_next_material); @@ -629,20 +615,15 @@ public: int total_data_size; - Surface() { - array_byte_size = 0; - index_array_byte_size = 0; - - array_len = 0; - index_array_len = 0; - - mesh = NULL; - - primitive = VS::PRIMITIVE_POINTS; - - active = false; - - total_data_size = 0; + Surface() : + mesh(NULL), + array_len(0), + index_array_len(0), + array_byte_size(0), + index_array_byte_size(0), + primitive(VS::PRIMITIVE_POINTS), + active(false), + total_data_size(0) { } }; @@ -667,14 +648,14 @@ public: SelfList<MultiMesh> *mm = multimeshes.first(); while (mm) { - mm->self()->instance_material_change_notify(); + mm->self()->instance_change_notify(false, true); mm = mm->next(); } } - Mesh() { - blend_shape_mode = VS::BLEND_SHAPE_MODE_NORMALIZED; - blend_shape_count = 0; + Mesh() : + blend_shape_count(0), + blend_shape_mode(VS::BLEND_SHAPE_MODE_NORMALIZED) { } }; @@ -745,22 +726,18 @@ public: bool dirty_data; MultiMesh() : + size(0), + transform_format(VS::MULTIMESH_TRANSFORM_2D), + color_format(VS::MULTIMESH_COLOR_NONE), + custom_data_format(VS::MULTIMESH_CUSTOM_DATA_NONE), update_list(this), - mesh_list(this) { - dirty_aabb = true; - dirty_data = true; - - xform_floats = 0; - color_floats = 0; - custom_data_floats = 0; - - visible_instances = -1; - - size = 0; - - transform_format = VS::MULTIMESH_TRANSFORM_2D; - color_format = VS::MULTIMESH_COLOR_NONE; - custom_data_format = VS::MULTIMESH_CUSTOM_DATA_NONE; + mesh_list(this), + visible_instances(-1), + xform_floats(0), + color_floats(0), + custom_data_floats(0), + dirty_aabb(true), + dirty_data(true) { } }; @@ -797,8 +774,40 @@ public: /* IMMEDIATE API */ + struct Immediate : public Geometry { + + struct Chunk { + RID texture; + VS::PrimitiveType primitive; + Vector<Vector3> vertices; + Vector<Vector3> normals; + Vector<Plane> tangents; + Vector<Color> colors; + Vector<Vector2> uvs; + Vector<Vector2> uv2s; + }; + + List<Chunk> chunks; + bool building; + int mask; + AABB aabb; + + Immediate() { + type = GEOMETRY_IMMEDIATE; + building = false; + } + }; + + Vector3 chunk_normal; + Plane chunk_tangent; + Color chunk_color; + Vector2 chunk_uv; + Vector2 chunk_uv2; + + mutable RID_Owner<Immediate> immediate_owner; + virtual RID immediate_create(); - virtual void immediate_begin(RID p_immediate, VS::PrimitiveType p_rimitive, RID p_texture = RID()); + virtual void immediate_begin(RID p_immediate, VS::PrimitiveType p_primitive, RID p_texture = RID()); virtual void immediate_vertex(RID p_immediate, const Vector3 &p_vertex); virtual void immediate_normal(RID p_immediate, const Vector3 &p_normal); virtual void immediate_tangent(RID p_immediate, const Plane &p_tangent); @@ -829,10 +838,10 @@ public: Set<RasterizerScene::InstanceBase *> instances; Skeleton() : + use_2d(false), + size(0), + tex_id(0), update_list(this) { - tex_id = 0; - size = 0; - use_2d = false; } }; @@ -855,7 +864,7 @@ public: /* Light API */ - struct Light : Instanciable { + struct Light : Instantiable { VS::LightType type; float param[VS::LIGHT_PARAM_MAX]; @@ -917,6 +926,26 @@ public: virtual uint64_t light_get_version(RID p_light) const; /* PROBE API */ + + struct ReflectionProbe : Instantiable { + + VS::ReflectionProbeUpdateMode update_mode; + float intensity; + Color interior_ambient; + float interior_ambient_energy; + float interior_ambient_probe_contrib; + float max_distance; + Vector3 extents; + Vector3 origin_offset; + bool interior; + bool box_projection; + bool enable_shadows; + uint32_t cull_mask; + int resolution; + }; + + mutable RID_Owner<ReflectionProbe> reflection_probe_owner; + virtual RID reflection_probe_create(); virtual void reflection_probe_set_update_mode(RID p_probe, VS::ReflectionProbeUpdateMode p_mode); @@ -931,11 +960,14 @@ public: virtual void reflection_probe_set_enable_box_projection(RID p_probe, bool p_enable); virtual void reflection_probe_set_enable_shadows(RID p_probe, bool p_enable); virtual void reflection_probe_set_cull_mask(RID p_probe, uint32_t p_layers); + virtual void reflection_probe_set_resolution(RID p_probe, int p_resolution); virtual AABB reflection_probe_get_aabb(RID p_probe) const; virtual VS::ReflectionProbeUpdateMode reflection_probe_get_update_mode(RID p_probe) const; virtual uint32_t reflection_probe_get_cull_mask(RID p_probe) const; + virtual int reflection_probe_get_resolution(RID p_probe) const; + virtual Vector3 reflection_probe_get_extents(RID p_probe) const; virtual Vector3 reflection_probe_get_origin_offset(RID p_probe) const; virtual float reflection_probe_get_origin_max_distance(RID p_probe) const; @@ -985,6 +1017,21 @@ public: /* LIGHTMAP */ + struct LightmapCapture : public Instantiable { + + PoolVector<LightmapCaptureOctree> octree; + AABB bounds; + Transform cell_xform; + int cell_subdiv; + float energy; + LightmapCapture() { + energy = 1.0; + cell_subdiv = 1; + } + }; + + mutable RID_Owner<LightmapCapture> lightmap_capture_data_owner; + virtual RID lightmap_capture_create(); virtual void lightmap_capture_set_bounds(RID p_capture, const AABB &p_bounds); virtual AABB lightmap_capture_get_bounds(RID p_capture) const; @@ -1034,6 +1081,8 @@ public: virtual int particles_get_draw_passes(RID p_particles) const; virtual RID particles_get_draw_pass_mesh(RID p_particles, int p_pass) const; + virtual bool particles_is_inactive(RID p_particles) const; + /* INSTANCE */ virtual void instance_add_skeleton(RID p_skeleton, RasterizerScene::InstanceBase *p_instance); @@ -1063,11 +1112,11 @@ public: GLuint color; - Effect() { - fbo = 0; - width = 0; - height = 0; - color = 0; + Effect() : + fbo(0), + width(0), + height(0), + color(0) { } }; @@ -1082,22 +1131,17 @@ public: RID texture; - RenderTarget() { - fbo = 0; - - color = 0; - depth = 0; - - width = 0; - height = 0; - - for (int i = 0; i < RENDER_TARGET_FLAG_MAX; i++) { + RenderTarget() : + fbo(0), + color(0), + depth(0), + width(0), + height(0), + used_in_frame(false), + msaa(VS::VIEWPORT_MSAA_DISABLED) { + for (int i = 0; i < RENDER_TARGET_FLAG_MAX; ++i) { flags[i] = false; } - - used_in_frame = false; - - msaa = VS::VIEWPORT_MSAA_DISABLED; } }; diff --git a/drivers/gles2/shader_compiler_gles2.cpp b/drivers/gles2/shader_compiler_gles2.cpp index 5ac2af6e5c..45b0d695a3 100644 --- a/drivers/gles2/shader_compiler_gles2.cpp +++ b/drivers/gles2/shader_compiler_gles2.cpp @@ -27,11 +27,13 @@ /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ + #include "shader_compiler_gles2.h" -#include "os/os.h" -#include "string_buffer.h" -#include "string_builder.h" +#include "core/os/os.h" +#include "core/project_settings.h" +#include "core/string_buffer.h" +#include "core/string_builder.h" #define SL ShaderLanguage @@ -78,19 +80,17 @@ static String _opstr(SL::Operator p_op) { static String _mkid(const String &p_id) { - StringBuffer<> id; - id += "m_"; - id += p_id; - - return id.as_string(); + String id = "m_" + p_id; + return id.replace("__", "_dus_"); //doubleunderscore is reserverd in glsl } static String f2sp0(float p_float) { - if (int(p_float) == p_float) - return itos(p_float) + ".0"; - else - return rtoss(p_float); + String num = rtoss(p_float); + if (num.find(".") == -1 && num.find("e") == -1) { + num += ".0"; + } + return num; } static String get_constant_text(SL::DataType p_type, const Vector<SL::ConstantNode::Value> &p_values) { @@ -161,7 +161,7 @@ static String get_constant_text(SL::DataType p_type, const Vector<SL::ConstantNo return text.as_string(); } break; - case SL::TYPE_FLOAT: return f2sp0(p_values[0].real) + "f"; + case SL::TYPE_FLOAT: return f2sp0(p_values[0].real); case SL::TYPE_VEC2: case SL::TYPE_VEC3: case SL::TYPE_VEC4: { @@ -362,6 +362,7 @@ String ShaderCompilerGLES2::_dump_node_code(SL::Node *p_node, int p_level, Gener for (int i = 0; i < snode->functions.size(); i++) { SL::FunctionNode *fnode = snode->functions[i].function; + current_func_name = fnode->name; function_code[fnode->name] = _dump_node_code(fnode->body, 1, r_gen_code, p_actions, p_default_actions, p_assigning); } @@ -596,6 +597,7 @@ String ShaderCompilerGLES2::_dump_node_code(SL::Node *p_node, int p_level, Gener default: { SL::DataType type = op_node->arguments[3]->get_datatype(); + // FIXME: Proper error print or graceful handling print_line(String("uhhhh invalid mix with type: ") + itos(type)); } break; } @@ -640,11 +642,11 @@ String ShaderCompilerGLES2::_dump_node_code(SL::Node *p_node, int p_level, Gener case SL::OP_MOD: { - code += "mod("; + code += "mod(float("; code += _dump_node_code(op_node->arguments[0], p_level, r_gen_code, p_actions, p_default_actions, p_assigning); - code += ", "; + code += "), float("; code += _dump_node_code(op_node->arguments[1], p_level, r_gen_code, p_actions, p_default_actions, p_assigning); - code += ")"; + code += "))"; } break; default: { @@ -812,8 +814,8 @@ ShaderCompilerGLES2::ShaderCompilerGLES2() { /** SPATIAL SHADER **/ actions[VS::SHADER_SPATIAL].renames["WORLD_MATRIX"] = "world_transform"; - actions[VS::SHADER_SPATIAL].renames["INV_CAMERA_MATRIX"] = "camera_matrix"; - actions[VS::SHADER_SPATIAL].renames["CAMERA_MATRIX"] = "camera_inverse_matrix"; + actions[VS::SHADER_SPATIAL].renames["INV_CAMERA_MATRIX"] = "camera_inverse_matrix"; + actions[VS::SHADER_SPATIAL].renames["CAMERA_MATRIX"] = "camera_matrix"; actions[VS::SHADER_SPATIAL].renames["PROJECTION_MATRIX"] = "projection_matrix"; actions[VS::SHADER_SPATIAL].renames["INV_PROJECTION_MATRIX"] = "projection_inverse_matrix"; actions[VS::SHADER_SPATIAL].renames["MODELVIEW_MATRIX"] = "modelview"; @@ -828,6 +830,7 @@ ShaderCompilerGLES2::ShaderCompilerGLES2() { actions[VS::SHADER_SPATIAL].renames["POINT_SIZE"] = "gl_PointSize"; // gl_InstanceID is not available in OpenGL ES 2.0 actions[VS::SHADER_SPATIAL].renames["INSTANCE_ID"] = "0"; + actions[VS::SHADER_SPATIAL].renames["OUTPUT_IS_SRGB"] = "SHADER_IS_SRGB"; //builtins @@ -898,23 +901,37 @@ ShaderCompilerGLES2::ShaderCompilerGLES2() { actions[VS::SHADER_SPATIAL].render_mode_defines["skip_vertex_transform"] = "#define SKIP_TRANSFORM_USED\n"; actions[VS::SHADER_SPATIAL].render_mode_defines["world_vertex_coords"] = "#define VERTEX_WORLD_COORDS_USED\n"; - actions[VS::SHADER_SPATIAL].render_mode_defines["diffuse_burley"] = "#define DIFFUSE_BURLEY\n"; + bool force_lambert = GLOBAL_GET("rendering/quality/shading/force_lambert_over_burley"); + + if (!force_lambert) { + actions[VS::SHADER_SPATIAL].render_mode_defines["diffuse_burley"] = "#define DIFFUSE_BURLEY\n"; + } + actions[VS::SHADER_SPATIAL].render_mode_defines["diffuse_oren_nayar"] = "#define DIFFUSE_OREN_NAYAR\n"; actions[VS::SHADER_SPATIAL].render_mode_defines["diffuse_lambert_wrap"] = "#define DIFFUSE_LAMBERT_WRAP\n"; actions[VS::SHADER_SPATIAL].render_mode_defines["diffuse_toon"] = "#define DIFFUSE_TOON\n"; - actions[VS::SHADER_SPATIAL].render_mode_defines["specular_schlick_ggx"] = "#define SPECULAR_SCHLICK_GGX\n"; + bool force_blinn = GLOBAL_GET("rendering/quality/shading/force_blinn_over_ggx"); + + if (!force_blinn) { + actions[VS::SHADER_SPATIAL].render_mode_defines["specular_schlick_ggx"] = "#define SPECULAR_SCHLICK_GGX\n"; + } else { + actions[VS::SHADER_SPATIAL].render_mode_defines["specular_schlick_ggx"] = "#define SPECULAR_BLINN\n"; + } + actions[VS::SHADER_SPATIAL].render_mode_defines["specular_blinn"] = "#define SPECULAR_BLINN\n"; actions[VS::SHADER_SPATIAL].render_mode_defines["specular_phong"] = "#define SPECULAR_PHONG\n"; actions[VS::SHADER_SPATIAL].render_mode_defines["specular_toon"] = "#define SPECULAR_TOON\n"; actions[VS::SHADER_SPATIAL].render_mode_defines["specular_disabled"] = "#define SPECULAR_DISABLED\n"; + actions[VS::SHADER_SPATIAL].render_mode_defines["shadows_disabled"] = "#define SHADOWS_DISABLED\n"; + actions[VS::SHADER_SPATIAL].render_mode_defines["ambient_light_disabled"] = "#define AMBIENT_LIGHT_DISABLED\n"; /* PARTICLES SHADER */ actions[VS::SHADER_PARTICLES].renames["COLOR"] = "out_color"; actions[VS::SHADER_PARTICLES].renames["VELOCITY"] = "out_velocity_active.xyz"; actions[VS::SHADER_PARTICLES].renames["MASS"] = "mass"; - actions[VS::SHADER_PARTICLES].renames["ACTIVE"] = "active"; + actions[VS::SHADER_PARTICLES].renames["ACTIVE"] = "shader_active"; actions[VS::SHADER_PARTICLES].renames["RESTART"] = "restart"; actions[VS::SHADER_PARTICLES].renames["CUSTOM"] = "out_custom"; actions[VS::SHADER_PARTICLES].renames["TRANSFORM"] = "xform"; diff --git a/drivers/gles2/shader_compiler_gles2.h b/drivers/gles2/shader_compiler_gles2.h index 804ead2172..5e9e295204 100644 --- a/drivers/gles2/shader_compiler_gles2.h +++ b/drivers/gles2/shader_compiler_gles2.h @@ -27,16 +27,16 @@ /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ + #ifndef SHADERCOMPILERGLES2_H #define SHADERCOMPILERGLES2_H -#include "pair.h" +#include "core/pair.h" +#include "core/string_builder.h" #include "servers/visual/shader_language.h" #include "servers/visual/shader_types.h" #include "servers/visual_server.h" -#include "string_builder.h" - class ShaderCompilerGLES2 { public: struct IdentifierActions { diff --git a/drivers/gles2/shader_gles2.cpp b/drivers/gles2/shader_gles2.cpp index e9b58cb272..84bd413abb 100644 --- a/drivers/gles2/shader_gles2.cpp +++ b/drivers/gles2/shader_gles2.cpp @@ -27,12 +27,12 @@ /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ -#include "shader_gles2.h" -#include "memory.h" -#include "print_string.h" -#include "string_builder.h" +#include "shader_gles2.h" +#include "core/os/memory.h" +#include "core/print_string.h" +#include "core/string_builder.h" #include "rasterizer_gles2.h" #include "rasterizer_storage_gles2.h" @@ -57,7 +57,7 @@ ShaderGLES2 *ShaderGLES2::active = NULL; -// #define DEBUG_SHADER +//#define DEBUG_SHADER #ifdef DEBUG_SHADER @@ -99,7 +99,7 @@ void ShaderGLES2::bind_uniforms() { const Map<uint32_t, CameraMatrix>::Element *C = uniform_cameras.front(); while (C) { - int idx = E->key(); + int idx = C->key(); int location = version->uniform_location[idx]; if (location < 0) { @@ -132,6 +132,11 @@ bool ShaderGLES2::bind() { ERR_FAIL_COND_V(!version, false); + if (!version->ok) { //broken, unable to bind (do not throw error, you saw it before already when it failed compilation). + glUseProgram(0); + return false; + } + glUseProgram(version->id); // find out uniform names and locations @@ -171,73 +176,30 @@ void ShaderGLES2::unbind() { active = NULL; } -static String _fix_error_code_line(const String &p_error, int p_code_start, int p_offset) { - - int last_find_pos = -1; - // NVIDIA - String error = p_error; - while ((last_find_pos = p_error.find("(", last_find_pos + 1)) != -1) { - - int end_pos = last_find_pos + 1; +static void _display_error_with_code(const String &p_error, const Vector<const char *> &p_code) { - while (true) { + int line = 1; + String total_code; - if (p_error[end_pos] >= '0' && p_error[end_pos] <= '9') { - - end_pos++; - continue; - } else if (p_error[end_pos] == ')') { - break; - } else { - - end_pos = -1; - break; - } - } - - if (end_pos == -1) - continue; - - String numstr = error.substr(last_find_pos + 1, (end_pos - last_find_pos) - 1); - String begin = error.substr(0, last_find_pos + 1); - String end = error.substr(end_pos, error.length()); - int num = numstr.to_int() + p_code_start - p_offset; - error = begin + itos(num) + end; + for (int i = 0; i < p_code.size(); i++) { + total_code += String(p_code[i]); } - // ATI - last_find_pos = -1; - while ((last_find_pos = p_error.find("ERROR: ", last_find_pos + 1)) != -1) { - - last_find_pos += 6; - int end_pos = last_find_pos + 1; + Vector<String> lines = String(total_code).split("\n"); - while (true) { + for (int j = 0; j < lines.size(); j++) { - if (p_error[end_pos] >= '0' && p_error[end_pos] <= '9') { + print_line(itos(line) + ": " + lines[j]); + line++; + } - end_pos++; - continue; - } else if (p_error[end_pos] == ':') { - break; - } else { + ERR_PRINTS(p_error); +} - end_pos = -1; - break; - } - } - continue; - if (end_pos == -1) - continue; +static String _mkid(const String &p_id) { - String numstr = error.substr(last_find_pos + 1, (end_pos - last_find_pos) - 1); - print_line("numstr: " + numstr); - String begin = error.substr(0, last_find_pos + 1); - String end = error.substr(end_pos, error.length()); - int num = numstr.to_int() + p_code_start - p_offset; - error = begin + itos(num) + end; - } - return error; + String id = "m_" + p_id; + return id.replace("__", "_dus_"); //doubleunderscore is reserverd in glsl } ShaderGLES2::Version *ShaderGLES2::get_current_version() { @@ -294,7 +256,7 @@ ShaderGLES2::Version *ShaderGLES2::get_current_version() { } } - // keep them around during the functino + // keep them around during the function CharString code_string; CharString code_string2; CharString code_globals; @@ -317,7 +279,7 @@ ShaderGLES2::Version *ShaderGLES2::get_current_version() { if (cc) { for (int i = 0; i < cc->custom_defines.size(); i++) { strings.push_back(cc->custom_defines.write[i]); - DEBUG_PRINT("CD #" + itos(i) + ": " + String(cc->custom_defines[i])); + DEBUG_PRINT("CD #" + itos(i) + ": " + String(cc->custom_defines[i].get_data())); } } @@ -376,9 +338,8 @@ ShaderGLES2::Version *ShaderGLES2::get_current_version() { String err_string = get_shader_name() + ": Vertex shader compilation failed:\n"; err_string += ilogmem; - err_string = _fix_error_code_line(err_string, vertex_code_start, define_line_ofs); - ERR_PRINTS(err_string); + _display_error_with_code(err_string, strings); Memory::free_static(ilogmem); glDeleteShader(v.vert_id); @@ -452,9 +413,8 @@ ShaderGLES2::Version *ShaderGLES2::get_current_version() { String err_string = get_shader_name() + ": Fragment shader compilation failed:\n"; err_string += ilogmem; - err_string = _fix_error_code_line(err_string, fragment_code_start, define_line_ofs); - ERR_PRINTS(err_string); + _display_error_with_code(err_string, strings); Memory::free_static(ilogmem); glDeleteShader(v.frag_id); @@ -504,9 +464,8 @@ ShaderGLES2::Version *ShaderGLES2::get_current_version() { String err_string = get_shader_name() + ": Program linking failed:\n"; err_string += ilogmem; - err_string = _fix_error_code_line(err_string, fragment_code_start, define_line_ofs); - ERR_PRINTS(err_string); + _display_error_with_code(err_string, strings); Memory::free_static(ilogmem); glDeleteShader(v.frag_id); @@ -539,15 +498,15 @@ ShaderGLES2::Version *ShaderGLES2::get_current_version() { if (cc) { // uniforms for (int i = 0; i < cc->custom_uniforms.size(); i++) { - StringName native_uniform_name = "m_" + cc->custom_uniforms[i]; - GLint location = glGetUniformLocation(v.id, ((String)native_uniform_name).ascii().get_data()); + String native_uniform_name = _mkid(cc->custom_uniforms[i]); + GLint location = glGetUniformLocation(v.id, (native_uniform_name).ascii().get_data()); v.custom_uniform_locations[cc->custom_uniforms[i]] = location; } // textures for (int i = 0; i < cc->texture_uniforms.size(); i++) { - StringName native_uniform_name = "m_" + cc->texture_uniforms[i]; - GLint location = glGetUniformLocation(v.id, ((String)native_uniform_name).ascii().get_data()); + String native_uniform_name = _mkid(cc->texture_uniforms[i]); + GLint location = glGetUniformLocation(v.id, (native_uniform_name).ascii().get_data()); v.custom_uniform_locations[cc->texture_uniforms[i]] = location; } } @@ -737,11 +696,6 @@ void ShaderGLES2::use_material(void *p_material) { Version *v = version_map.getptr(conditional_version); - CustomCode *cc = NULL; - if (v) { - cc = custom_code_map.getptr(v->code_version); - } - // bind uniforms for (Map<StringName, ShaderLanguage::ShaderNode::Uniform>::Element *E = material->shader->uniforms.front(); E; E = E->next()) { @@ -967,19 +921,7 @@ void ShaderGLES2::use_material(void *p_material) { value.second.write[15].real = 1; } break; - case ShaderLanguage::TYPE_SAMPLER2D: { - - } break; - - case ShaderLanguage::TYPE_ISAMPLER2D: { - - } break; - - case ShaderLanguage::TYPE_USAMPLER2D: { - - } break; - - case ShaderLanguage::TYPE_SAMPLERCUBE: { + default: { } break; } @@ -1041,7 +983,7 @@ void ShaderGLES2::use_material(void *p_material) { value.second.resize(default_arg_size); - for (int i = 0; i < default_arg_size; i++) { + for (size_t i = 0; i < default_arg_size; i++) { if (is_float) { value.second.write[i].real = 0.0; } else { @@ -1051,8 +993,6 @@ void ShaderGLES2::use_material(void *p_material) { } } - // GLint location = get_uniform_location(E->key()); - GLint location; if (v->custom_uniform_locations.has(E->key())) { location = v->custom_uniform_locations[E->key()]; @@ -1072,8 +1012,6 @@ void ShaderGLES2::use_material(void *p_material) { int tc = material->textures.size(); Pair<StringName, RID> *textures = material->textures.ptrw(); - ShaderLanguage::ShaderNode::Uniform::Hint *texture_hints = material->shader->texture_hints.ptrw(); - for (int i = 0; i < tc; i++) { Pair<ShaderLanguage::DataType, Vector<ShaderLanguage::ConstantNode::Value> > value; diff --git a/drivers/gles2/shader_gles2.h b/drivers/gles2/shader_gles2.h index cb515c199c..9160a7c265 100644 --- a/drivers/gles2/shader_gles2.h +++ b/drivers/gles2/shader_gles2.h @@ -27,11 +27,11 @@ /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ + #ifndef SHADER_GLES2_H #define SHADER_GLES2_H -#include <stdio.h> - +// This must come first to avoid windows.h mess #include "platform_config.h" #ifndef GLES2_INCLUDE_H #include <GLES2/gl2.h> @@ -39,14 +39,15 @@ #include GLES2_INCLUDE_H #endif -#include "camera_matrix.h" -#include "hash_map.h" -#include "map.h" -#include "variant.h" - +#include "core/hash_map.h" +#include "core/map.h" +#include "core/math/camera_matrix.h" #include "core/pair.h" +#include "core/variant.h" #include "servers/visual/shader_language.h" +#include <stdio.h> + class RasterizerStorageGLES2; class ShaderGLES2 { @@ -334,6 +335,19 @@ public: case ShaderLanguage::TYPE_SAMPLERCUBE: { } break; + + case ShaderLanguage::TYPE_SAMPLER2DARRAY: + case ShaderLanguage::TYPE_ISAMPLER2DARRAY: + case ShaderLanguage::TYPE_USAMPLER2DARRAY: + case ShaderLanguage::TYPE_SAMPLER3D: + case ShaderLanguage::TYPE_ISAMPLER3D: + case ShaderLanguage::TYPE_USAMPLER3D: { + // Not implemented in GLES2 + } break; + + case ShaderLanguage::TYPE_VOID: { + // Nothing to do? + } break; } } @@ -467,7 +481,8 @@ public: // like forward declared nested classes. void use_material(void *p_material); - uint32_t get_version() const { return new_conditional_version.version; } + _FORCE_INLINE_ uint32_t get_version() const { return new_conditional_version.version; } + _FORCE_INLINE_ bool is_version_valid() const { return version && version->ok; } void set_uniform_camera(int p_idx, const CameraMatrix &p_mat) { diff --git a/drivers/gles2/shaders/SCsub b/drivers/gles2/shaders/SCsub index acb93fff8f..d959d3f740 100644 --- a/drivers/gles2/shaders/SCsub +++ b/drivers/gles2/shaders/SCsub @@ -20,3 +20,4 @@ if 'GLES2_GLSL' in env['BUILDERS']: # env.GLES2_GLSL('exposure.glsl'); # env.GLES2_GLSL('tonemap.glsl'); # env.GLES2_GLSL('particles.glsl'); + env.GLES2_GLSL('lens_distorted.glsl'); diff --git a/drivers/gles2/shaders/blend_shape.glsl b/drivers/gles2/shaders/blend_shape.glsl index 4e0d066823..a1e954e33d 100644 --- a/drivers/gles2/shaders/blend_shape.glsl +++ b/drivers/gles2/shaders/blend_shape.glsl @@ -1,6 +1,6 @@ +/* clang-format off */ [vertex] - /* from VisualServer: @@ -23,56 +23,57 @@ ARRAY_INDEX=8, /* INPUT ATTRIBS */ -layout(location=0) in highp VFORMAT vertex_attrib; -layout(location=1) in vec3 normal_attrib; +layout(location = 0) in highp VFORMAT vertex_attrib; +/* clang-format on */ +layout(location = 1) in vec3 normal_attrib; #ifdef ENABLE_TANGENT -layout(location=2) in vec4 tangent_attrib; +layout(location = 2) in vec4 tangent_attrib; #endif #ifdef ENABLE_COLOR -layout(location=3) in vec4 color_attrib; +layout(location = 3) in vec4 color_attrib; #endif #ifdef ENABLE_UV -layout(location=4) in vec2 uv_attrib; +layout(location = 4) in vec2 uv_attrib; #endif #ifdef ENABLE_UV2 -layout(location=5) in vec2 uv2_attrib; +layout(location = 5) in vec2 uv2_attrib; #endif #ifdef ENABLE_SKELETON -layout(location=6) in ivec4 bone_attrib; -layout(location=7) in vec4 weight_attrib; +layout(location = 6) in ivec4 bone_attrib; +layout(location = 7) in vec4 weight_attrib; #endif /* BLEND ATTRIBS */ #ifdef ENABLE_BLEND -layout(location=8) in highp VFORMAT vertex_attrib_blend; -layout(location=9) in vec3 normal_attrib_blend; +layout(location = 8) in highp VFORMAT vertex_attrib_blend; +layout(location = 9) in vec3 normal_attrib_blend; #ifdef ENABLE_TANGENT -layout(location=10) in vec4 tangent_attrib_blend; +layout(location = 10) in vec4 tangent_attrib_blend; #endif #ifdef ENABLE_COLOR -layout(location=11) in vec4 color_attrib_blend; +layout(location = 11) in vec4 color_attrib_blend; #endif #ifdef ENABLE_UV -layout(location=12) in vec2 uv_attrib_blend; +layout(location = 12) in vec2 uv_attrib_blend; #endif #ifdef ENABLE_UV2 -layout(location=13) in vec2 uv2_attrib_blend; +layout(location = 13) in vec2 uv2_attrib_blend; #endif #ifdef ENABLE_SKELETON -layout(location=14) in ivec4 bone_attrib_blend; -layout(location=15) in vec4 weight_attrib_blend; +layout(location = 14) in ivec4 bone_attrib_blend; +layout(location = 15) in vec4 weight_attrib_blend; #endif #endif @@ -110,7 +111,6 @@ uniform float blend_amount; void main() { - #ifdef ENABLE_BLEND vertex_out = vertex_attrib_blend + vertex_attrib * blend_amount; @@ -140,7 +140,6 @@ void main() { uv2_out = uv2_attrib_blend + uv2_attrib * blend_amount; #endif - #ifdef ENABLE_SKELETON bone_out = bone_attrib_blend; @@ -149,7 +148,6 @@ void main() { #else //ENABLE_BLEND - vertex_out = vertex_attrib * blend_amount; #ifdef ENABLE_NORMAL @@ -177,7 +175,6 @@ void main() { uv2_out = uv2_attrib * blend_amount; #endif - #ifdef ENABLE_SKELETON bone_out = bone_attrib; @@ -188,10 +185,10 @@ void main() { gl_Position = vec4(0.0); } +/* clang-format off */ [fragment] - void main() { } - +/* clang-format on */ diff --git a/drivers/gles2/shaders/canvas.glsl b/drivers/gles2/shaders/canvas.glsl index 29d81bb2c4..79d4eb2243 100644 --- a/drivers/gles2/shaders/canvas.glsl +++ b/drivers/gles2/shaders/canvas.glsl @@ -1,3 +1,4 @@ +/* clang-format off */ [vertex] #ifdef USE_GLES_OVER_GL @@ -9,6 +10,7 @@ precision mediump int; #endif uniform highp mat4 projection_matrix; +/* clang-format on */ uniform highp mat4 modelview_matrix; uniform highp mat4 extra_matrix; attribute highp vec2 vertex; // attrib:0 @@ -29,8 +31,12 @@ uniform vec4 src_rect; uniform highp float time; +/* clang-format off */ + VERTEX_SHADER_GLOBALS +/* clang-format on */ + vec2 select(vec2 a, vec2 b, bvec2 c) { vec2 ret; @@ -74,18 +80,30 @@ void main() { #endif -{ - vec2 src_vtx=outvec.xy; + { + vec2 src_vtx = outvec.xy; + /* clang-format off */ + VERTEX_SHADER_CODE -} + /* clang-format on */ + } + +#if !defined(SKIP_TRANSFORM_USED) + outvec = extra_matrix * outvec; + outvec = modelview_matrix * outvec; +#endif color_interp = color; - gl_Position = projection_matrix * modelview_matrix * outvec; +#ifdef USE_PIXEL_SNAP + outvec.xy = floor(outvec + 0.5).xy; +#endif + gl_Position = projection_matrix * outvec; } +/* clang-format off */ [fragment] #ifdef USE_GLES_OVER_GL @@ -96,9 +114,10 @@ precision mediump float; precision mediump int; #endif -uniform sampler2D color_texture; // texunit:0 +uniform sampler2D color_texture; // texunit:-1 +/* clang-format on */ uniform highp vec2 color_texpixel_size; -uniform mediump sampler2D normal_texture; // texunit:1 +uniform mediump sampler2D normal_texture; // texunit:-2 varying mediump vec2 uv_interp; varying mediump vec4 color_interp; @@ -109,7 +128,7 @@ uniform vec4 final_modulate; #ifdef SCREEN_TEXTURE_USED -uniform sampler2D screen_texture; // texunit:2 +uniform sampler2D screen_texture; // texunit:-3 #endif @@ -119,27 +138,33 @@ uniform vec2 screen_pixel_size; #endif +/* clang-format off */ + FRAGMENT_SHADER_GLOBALS +/* clang-format on */ void main() { vec4 color = color_interp; +#if !defined(COLOR_USED) + //default behavior, texture by color color *= texture2D(color_texture, uv_interp); +#endif #ifdef SCREEN_UV_USED vec2 screen_uv = gl_FragCoord.xy * screen_pixel_size; #endif -{ + { + /* clang-format off */ FRAGMENT_SHADER_CODE - -} + /* clang-format on */ + } color *= final_modulate; gl_FragColor = color; - } diff --git a/drivers/gles2/shaders/canvas_shadow.glsl b/drivers/gles2/shaders/canvas_shadow.glsl index c757990de0..e3c8140e31 100644 --- a/drivers/gles2/shaders/canvas_shadow.glsl +++ b/drivers/gles2/shaders/canvas_shadow.glsl @@ -1,49 +1,50 @@ +/* clang-format off */ [vertex] - - uniform highp mat4 projection_matrix; +/* clang-format on */ uniform highp mat4 light_matrix; uniform highp mat4 world_matrix; uniform highp float distance_norm; -layout(location=0) in highp vec3 vertex; +layout(location = 0) in highp vec3 vertex; out highp vec4 position_interp; void main() { - gl_Position = projection_matrix * (light_matrix * (world_matrix * vec4(vertex,1.0))); - position_interp=gl_Position; + gl_Position = projection_matrix * (light_matrix * (world_matrix * vec4(vertex, 1.0))); + position_interp = gl_Position; } +/* clang-format off */ [fragment] in highp vec4 position_interp; +/* clang-format on */ #ifdef USE_RGBA_SHADOWS -layout(location=0) out lowp vec4 distance_buf; +layout(location = 0) out lowp vec4 distance_buf; #else -layout(location=0) out highp float distance_buf; +layout(location = 0) out highp float distance_buf; #endif void main() { - highp float depth = ((position_interp.z / position_interp.w) + 1.0) * 0.5 + 0.0;//bias; + highp float depth = ((position_interp.z / position_interp.w) + 1.0) * 0.5 + 0.0; //bias; #ifdef USE_RGBA_SHADOWS highp vec4 comp = fract(depth * vec4(256.0 * 256.0 * 256.0, 256.0 * 256.0, 256.0, 1.0)); comp -= comp.xxyz * vec4(0, 1.0 / 256.0, 1.0 / 256.0, 1.0 / 256.0); - distance_buf=comp; + distance_buf = comp; #else - distance_buf=depth; + distance_buf = depth; #endif } - diff --git a/drivers/gles2/shaders/copy.glsl b/drivers/gles2/shaders/copy.glsl index feaeb2152b..0b8da4f875 100644 --- a/drivers/gles2/shaders/copy.glsl +++ b/drivers/gles2/shaders/copy.glsl @@ -1,3 +1,4 @@ +/* clang-format off */ [vertex] #ifdef USE_GLES_OVER_GL @@ -9,6 +10,7 @@ precision mediump int; #endif attribute highp vec4 vertex_attrib; // attrib:0 +/* clang-format on */ #if defined(USE_CUBEMAP) || defined(USE_PANORAMA) attribute vec3 cube_in; // attrib:4 @@ -33,6 +35,8 @@ void main() { #if defined(USE_CUBEMAP) || defined(USE_PANORAMA) cube_interp = cube_in; +#elif defined(USE_ASYM_PANO) + uv_interp = vertex_attrib.xy; #else uv_interp = uv_in; #endif @@ -46,6 +50,7 @@ void main() { #endif } +/* clang-format off */ [fragment] #define M_PI 3.14159265359 @@ -63,6 +68,12 @@ varying vec3 cube_interp; #else varying vec2 uv_interp; #endif +/* clang-format on */ + +#ifdef USE_ASYM_PANO +uniform highp mat4 pano_transform; +uniform highp vec4 asym_proj; +#endif #ifdef USE_CUBEMAP uniform samplerCube source_cube; // texunit:0 @@ -85,17 +96,15 @@ uniform float custom_alpha; vec4 texturePanorama(sampler2D pano, vec3 normal) { vec2 st = vec2( - atan(normal.x, normal.z), - acos(normal.y) - ); + atan(normal.x, normal.z), + acos(normal.y)); - if(st.x < 0.0) - st.x += M_PI*2.0; + if (st.x < 0.0) + st.x += M_PI * 2.0; - st/=vec2(M_PI*2.0,M_PI); - - return texture2D(pano,st); + st /= vec2(M_PI * 2.0, M_PI); + return texture2D(pano, st); } #endif @@ -106,19 +115,33 @@ void main() { vec4 color = texturePanorama(source, normalize(cube_interp)); +#elif defined(USE_ASYM_PANO) + + // When an asymmetrical projection matrix is used (applicable for stereoscopic rendering i.e. VR) we need to do this calculation per fragment to get a perspective correct result. + // Note that we're ignoring the x-offset for IPD, with Z sufficiently in the distance it becomes neglectible, as a result we could probably just set cube_normal.z to -1. + // The Matrix[2][0] (= asym_proj.x) and Matrix[2][1] (= asym_proj.z) values are what provide the right shift in the image. + + vec3 cube_normal; + cube_normal.z = -1000000.0; + cube_normal.x = (cube_normal.z * (-uv_interp.x - asym_proj.x)) / asym_proj.y; + cube_normal.y = (cube_normal.z * (-uv_interp.y - asym_proj.z)) / asym_proj.a; + cube_normal = mat3(pano_transform) * cube_normal; + cube_normal.z = -cube_normal.z; + + vec4 color = texturePanorama(source, normalize(cube_normal.xyz)); + #elif defined(USE_CUBEMAP) vec4 color = textureCube(source_cube, normalize(cube_interp)); #else - vec4 color = texture2D( source, uv_interp ); + vec4 color = texture2D(source, uv_interp); #endif - #ifdef USE_NO_ALPHA - color.a=1.0; + color.a = 1.0; #endif #ifdef USE_CUSTOM_ALPHA - color.a=custom_alpha; + color.a = custom_alpha; #endif #ifdef USE_MULTIPLIER diff --git a/drivers/gles2/shaders/cube_to_dp.glsl b/drivers/gles2/shaders/cube_to_dp.glsl index 0b3f53a870..3d24c36336 100644 --- a/drivers/gles2/shaders/cube_to_dp.glsl +++ b/drivers/gles2/shaders/cube_to_dp.glsl @@ -1,3 +1,4 @@ +/* clang-format off */ [vertex] #ifdef USE_GLES_OVER_GL @@ -9,6 +10,7 @@ precision mediump int; #endif attribute highp vec4 vertex_attrib; // attrib:0 +/* clang-format on */ attribute vec2 uv_in; // attrib:4 varying vec2 uv_interp; @@ -19,6 +21,7 @@ void main() { gl_Position = vertex_attrib; } +/* clang-format off */ [fragment] #ifdef USE_GLES_OVER_GL @@ -30,6 +33,7 @@ precision mediump int; #endif uniform highp samplerCube source_cube; //texunit:0 +/* clang-format on */ varying vec2 uv_interp; uniform bool z_flip; @@ -39,55 +43,53 @@ uniform highp float bias; void main() { - highp vec3 normal = vec3( uv_interp * 2.0 - 1.0, 0.0 ); -/* - if(z_flip) { - normal.z = 0.5 - 0.5*((normal.x * normal.x) + (normal.y * normal.y)); + highp vec3 normal = vec3(uv_interp * 2.0 - 1.0, 0.0); + /* + if (z_flip) { + normal.z = 0.5 - 0.5 * ((normal.x * normal.x) + (normal.y * normal.y)); } else { - normal.z = -0.5 + 0.5*((normal.x * normal.x) + (normal.y * normal.y)); + normal.z = -0.5 + 0.5 * ((normal.x * normal.x) + (normal.y * normal.y)); } -*/ + */ - //normal.z = sqrt(1.0-dot(normal.xy,normal.xy)); - //normal.xy*=1.0+normal.z; + //normal.z = sqrt(1.0 - dot(normal.xy, normal.xy)); + //normal.xy *= 1.0 + normal.z; - normal.z = 0.5 - 0.5*((normal.x * normal.x) + (normal.y * normal.y)); + normal.z = 0.5 - 0.5 * ((normal.x * normal.x) + (normal.y * normal.y)); normal = normalize(normal); + /* + normal.z = 0.5; + normal = normalize(normal); + */ -/* - normal.z=0.5; - normal=normalize(normal); -*/ if (!z_flip) { - normal.z=-normal.z; + normal.z = -normal.z; } //normal = normalize(vec3( uv_interp * 2.0 - 1.0, 1.0 )); - float depth = textureCube(source_cube,normal).r; + float depth = textureCube(source_cube, normal).r; // absolute values for direction cosines, bigger value equals closer to basis axis vec3 unorm = abs(normal); - if ( (unorm.x >= unorm.y) && (unorm.x >= unorm.z) ) { - // x code - unorm = normal.x > 0.0 ? vec3( 1.0, 0.0, 0.0 ) : vec3( -1.0, 0.0, 0.0 ) ; - } else if ( (unorm.y > unorm.x) && (unorm.y >= unorm.z) ) { - // y code - unorm = normal.y > 0.0 ? vec3( 0.0, 1.0, 0.0 ) : vec3( 0.0, -1.0, 0.0 ) ; - } else if ( (unorm.z > unorm.x) && (unorm.z > unorm.y) ) { - // z code - unorm = normal.z > 0.0 ? vec3( 0.0, 0.0, 1.0 ) : vec3( 0.0, 0.0, -1.0 ) ; + if ((unorm.x >= unorm.y) && (unorm.x >= unorm.z)) { + // x code + unorm = normal.x > 0.0 ? vec3(1.0, 0.0, 0.0) : vec3(-1.0, 0.0, 0.0); + } else if ((unorm.y > unorm.x) && (unorm.y >= unorm.z)) { + // y code + unorm = normal.y > 0.0 ? vec3(0.0, 1.0, 0.0) : vec3(0.0, -1.0, 0.0); + } else if ((unorm.z > unorm.x) && (unorm.z > unorm.y)) { + // z code + unorm = normal.z > 0.0 ? vec3(0.0, 0.0, 1.0) : vec3(0.0, 0.0, -1.0); } else { - // oh-no we messed up code - // has to be - unorm = vec3( 1.0, 0.0, 0.0 ); + // oh-no we messed up code + // has to be + unorm = vec3(1.0, 0.0, 0.0); } - float depth_fix = 1.0 / dot(normal,unorm); - + float depth_fix = 1.0 / dot(normal, unorm); depth = 2.0 * depth - 1.0; float linear_depth = 2.0 * z_near * z_far / (z_far + z_near - depth * (z_far - z_near)); - gl_FragDepth = (linear_depth*depth_fix+bias) / z_far; + gl_FragDepth = (linear_depth * depth_fix + bias) / z_far; } - diff --git a/drivers/gles2/shaders/cubemap_filter.glsl b/drivers/gles2/shaders/cubemap_filter.glsl index 62ecd9471b..b1553c7cd5 100644 --- a/drivers/gles2/shaders/cubemap_filter.glsl +++ b/drivers/gles2/shaders/cubemap_filter.glsl @@ -1,3 +1,4 @@ +/* clang-format off */ [vertex] #ifdef USE_GLES_OVER_GL @@ -9,19 +10,26 @@ precision mediump int; #endif attribute highp vec2 vertex; // attrib:0 +/* clang-format on */ attribute highp vec2 uv; // attrib:4 varying highp vec2 uv_interp; void main() { - uv_interp=uv; - gl_Position=vec4(vertex,0,1); + uv_interp = uv; + gl_Position = vec4(vertex, 0, 1); } +/* clang-format off */ [fragment] -#extension GL_ARB_shader_texture_lod : require +#extension GL_ARB_shader_texture_lod : enable + +#ifndef GL_ARB_shader_texture_lod +#define texture2DLod(img, coord, lod) texture2D(img, coord) +#define textureCubeLod(img, coord, lod) textureCube(img, coord) +#endif #ifdef USE_GLES_OVER_GL #define mediump @@ -36,6 +44,7 @@ uniform sampler2D source_panorama; //texunit:0 #else uniform samplerCube source_cube; //texunit:0 #endif +/* clang-format on */ uniform int face_id; uniform float roughness; @@ -60,17 +69,15 @@ uniform sampler2D radical_inverse_vdc_cache; // texunit:1 vec4 texturePanorama(sampler2D pano, vec3 normal) { vec2 st = vec2( - atan(normal.x, normal.z), - acos(normal.y) - ); - - if(st.x < 0.0) - st.x += M_PI*2.0; + atan(normal.x, normal.z), + acos(normal.y)); - st/=vec2(M_PI*2.0,M_PI); + if (st.x < 0.0) + st.x += M_PI * 2.0; - return texture2DLod(pano,st,0.0); + st /= vec2(M_PI * 2.0, M_PI); + return texture2DLod(pano, st, 0.0); } #endif @@ -79,24 +86,24 @@ vec3 texelCoordToVec(vec2 uv, int faceID) { mat3 faceUvVectors[6]; // -x - faceUvVectors[0][0] = vec3(0.0, 0.0, 1.0); // u -> +z + faceUvVectors[0][0] = vec3(0.0, 0.0, 1.0); // u -> +z faceUvVectors[0][1] = vec3(0.0, -1.0, 0.0); // v -> -y faceUvVectors[0][2] = vec3(-1.0, 0.0, 0.0); // -x face // +x faceUvVectors[1][0] = vec3(0.0, 0.0, -1.0); // u -> -z faceUvVectors[1][1] = vec3(0.0, -1.0, 0.0); // v -> -y - faceUvVectors[1][2] = vec3(1.0, 0.0, 0.0); // +x face + faceUvVectors[1][2] = vec3(1.0, 0.0, 0.0); // +x face // -y - faceUvVectors[2][0] = vec3(1.0, 0.0, 0.0); // u -> +x + faceUvVectors[2][0] = vec3(1.0, 0.0, 0.0); // u -> +x faceUvVectors[2][1] = vec3(0.0, 0.0, -1.0); // v -> -z faceUvVectors[2][2] = vec3(0.0, -1.0, 0.0); // -y face // +y - faceUvVectors[3][0] = vec3(1.0, 0.0, 0.0); // u -> +x - faceUvVectors[3][1] = vec3(0.0, 0.0, 1.0); // v -> +z - faceUvVectors[3][2] = vec3(0.0, 1.0, 0.0); // +y face + faceUvVectors[3][0] = vec3(1.0, 0.0, 0.0); // u -> +x + faceUvVectors[3][1] = vec3(0.0, 0.0, 1.0); // v -> +z + faceUvVectors[3][2] = vec3(0.0, 1.0, 0.0); // +y face // -z faceUvVectors[4][0] = vec3(-1.0, 0.0, 0.0); // u -> -x @@ -104,9 +111,9 @@ vec3 texelCoordToVec(vec2 uv, int faceID) { faceUvVectors[4][2] = vec3(0.0, 0.0, -1.0); // -z face // +z - faceUvVectors[5][0] = vec3(1.0, 0.0, 0.0); // u -> +x + faceUvVectors[5][0] = vec3(1.0, 0.0, 0.0); // u -> +x faceUvVectors[5][1] = vec3(0.0, -1.0, 0.0); // v -> -y - faceUvVectors[5][2] = vec3(0.0, 0.0, 1.0); // +z face + faceUvVectors[5][2] = vec3(0.0, 0.0, 1.0); // +z face // out = u * s_faceUv[0] + v * s_faceUv[1] + s_faceUv[2]. vec3 result = (faceUvVectors[faceID][0] * uv.x) + (faceUvVectors[faceID][1] * uv.y) + faceUvVectors[faceID][2]; @@ -118,7 +125,7 @@ vec3 ImportanceSampleGGX(vec2 Xi, float Roughness, vec3 N) { // Compute distribution direction float Phi = 2.0 * M_PI * Xi.x; - float CosTheta = sqrt((1.0 - Xi.y) / (1.0 + (a*a - 1.0) * Xi.y)); + float CosTheta = sqrt((1.0 - Xi.y) / (1.0 + (a * a - 1.0) * Xi.y)); float SinTheta = sqrt(1.0 - CosTheta * CosTheta); // Convert to spherical direction @@ -160,28 +167,30 @@ void main() { vec3 H = ImportanceSampleGGX(xi, roughness, N); vec3 V = N; - vec3 L = normalize(2.0 * dot(V, H) * H - V); + vec3 L = (2.0 * dot(V, H) * H - V); float NdotL = clamp(dot(N, L), 0.0, 1.0); if (NdotL > 0.0) { #ifdef USE_SOURCE_PANORAMA - sum.rgb += texturePanorama(source_panorama, H).rgb * NdotL; + vec3 val = texturePanorama(source_panorama, L).rgb; #else - H.y = -H.y; - sum.rgb += textureCubeLod(source_cube, H, 0.0).rgb * NdotL; + vec3 val = textureCubeLod(source_cube, L, 0.0).rgb; #endif + //mix using Linear, to approximate high end back-end + val = mix(pow((val + vec3(0.055)) * (1.0 / (1.0 + 0.055)), vec3(2.4)), val * (1.0 / 12.92), vec3(lessThan(val, vec3(0.04045)))); - sum.a += NdotL; + sum.rgb += val * NdotL; + sum.a += NdotL; } - } sum /= sum.a; - gl_FragColor = vec4(sum.rgb, 1.0); + vec3 a = vec3(0.055); + sum.rgb = mix((vec3(1.0) + a) * pow(sum.rgb, vec3(1.0 / 2.4)) - a, 12.92 * sum.rgb, vec3(lessThan(sum.rgb, vec3(0.0031308)))); + gl_FragColor = vec4(sum.rgb, 1.0); } - diff --git a/drivers/gles2/shaders/effect_blur.glsl b/drivers/gles2/shaders/effect_blur.glsl index b5f98a1244..a531802c75 100644 --- a/drivers/gles2/shaders/effect_blur.glsl +++ b/drivers/gles2/shaders/effect_blur.glsl @@ -1,8 +1,9 @@ +/* clang-format off */ [vertex] - -layout(location=0) in highp vec4 vertex_attrib; -layout(location=4) in vec2 uv_in; +layout(location = 0) in highp vec4 vertex_attrib; +/* clang-format on */ +layout(location = 4) in vec2 uv_in; out vec2 uv_interp; @@ -23,6 +24,7 @@ void main() { #endif } +/* clang-format off */ [fragment] #if !defined(GLES_OVER_GL) @@ -30,6 +32,7 @@ precision mediump float; #endif in vec2 uv_interp; +/* clang-format on */ uniform sampler2D source_color; //texunit:0 #ifdef SSAO_MERGE @@ -39,7 +42,6 @@ uniform sampler2D source_ssao; //texunit:1 uniform float lod; uniform vec2 pixel_size; - layout(location = 0) out vec4 frag_color; #ifdef SSAO_MERGE @@ -48,31 +50,31 @@ uniform vec4 ssao_color; #endif -#if defined (GLOW_GAUSSIAN_HORIZONTAL) || defined(GLOW_GAUSSIAN_VERTICAL) +#if defined(GLOW_GAUSSIAN_HORIZONTAL) || defined(GLOW_GAUSSIAN_VERTICAL) uniform float glow_strength; #endif -#if defined(DOF_FAR_BLUR) || defined (DOF_NEAR_BLUR) +#if defined(DOF_FAR_BLUR) || defined(DOF_NEAR_BLUR) #ifdef DOF_QUALITY_LOW -const int dof_kernel_size=5; -const int dof_kernel_from=2; -const float dof_kernel[5] = float[] (0.153388,0.221461,0.250301,0.221461,0.153388); +const int dof_kernel_size = 5; +const int dof_kernel_from = 2; +const float dof_kernel[5] = float[](0.153388, 0.221461, 0.250301, 0.221461, 0.153388); #endif #ifdef DOF_QUALITY_MEDIUM -const int dof_kernel_size=11; -const int dof_kernel_from=5; -const float dof_kernel[11] = float[] (0.055037,0.072806,0.090506,0.105726,0.116061,0.119726,0.116061,0.105726,0.090506,0.072806,0.055037); +const int dof_kernel_size = 11; +const int dof_kernel_from = 5; +const float dof_kernel[11] = float[](0.055037, 0.072806, 0.090506, 0.105726, 0.116061, 0.119726, 0.116061, 0.105726, 0.090506, 0.072806, 0.055037); #endif #ifdef DOF_QUALITY_HIGH -const int dof_kernel_size=21; -const int dof_kernel_from=10; -const float dof_kernel[21] = float[] (0.028174,0.032676,0.037311,0.041944,0.046421,0.050582,0.054261,0.057307,0.059587,0.060998,0.061476,0.060998,0.059587,0.057307,0.054261,0.050582,0.046421,0.041944,0.037311,0.032676,0.028174); +const int dof_kernel_size = 21; +const int dof_kernel_from = 10; +const float dof_kernel[21] = float[](0.028174, 0.032676, 0.037311, 0.041944, 0.046421, 0.050582, 0.054261, 0.057307, 0.059587, 0.060998, 0.061476, 0.060998, 0.059587, 0.057307, 0.054261, 0.050582, 0.046421, 0.041944, 0.037311, 0.032676, 0.028174); #endif uniform sampler2D dof_source_depth; //texunit:1 @@ -88,7 +90,6 @@ uniform sampler2D source_dof_original; //texunit:2 #endif - #ifdef GLOW_FIRST_PASS uniform float exposure; @@ -112,53 +113,51 @@ uniform float camera_z_near; void main() { - - #ifdef GAUSSIAN_HORIZONTAL vec2 pix_size = pixel_size; - pix_size*=0.5; //reading from larger buffer, so use more samples - vec4 color =textureLod( source_color, uv_interp+vec2( 0.0, 0.0)*pix_size,lod )*0.214607; - color+=textureLod( source_color, uv_interp+vec2( 1.0, 0.0)*pix_size,lod )*0.189879; - color+=textureLod( source_color, uv_interp+vec2( 2.0, 0.0)*pix_size,lod )*0.157305; - color+=textureLod( source_color, uv_interp+vec2( 3.0, 0.0)*pix_size,lod )*0.071303; - color+=textureLod( source_color, uv_interp+vec2(-1.0, 0.0)*pix_size,lod )*0.189879; - color+=textureLod( source_color, uv_interp+vec2(-2.0, 0.0)*pix_size,lod )*0.157305; - color+=textureLod( source_color, uv_interp+vec2(-3.0, 0.0)*pix_size,lod )*0.071303; + pix_size *= 0.5; //reading from larger buffer, so use more samples + vec4 color = textureLod(source_color, uv_interp + vec2(0.0, 0.0) * pix_size, lod) * 0.214607; + color += textureLod(source_color, uv_interp + vec2(1.0, 0.0) * pix_size, lod) * 0.189879; + color += textureLod(source_color, uv_interp + vec2(2.0, 0.0) * pix_size, lod) * 0.157305; + color += textureLod(source_color, uv_interp + vec2(3.0, 0.0) * pix_size, lod) * 0.071303; + color += textureLod(source_color, uv_interp + vec2(-1.0, 0.0) * pix_size, lod) * 0.189879; + color += textureLod(source_color, uv_interp + vec2(-2.0, 0.0) * pix_size, lod) * 0.157305; + color += textureLod(source_color, uv_interp + vec2(-3.0, 0.0) * pix_size, lod) * 0.071303; frag_color = color; #endif #ifdef GAUSSIAN_VERTICAL - vec4 color =textureLod( source_color, uv_interp+vec2( 0.0, 0.0)*pixel_size,lod )*0.38774; - color+=textureLod( source_color, uv_interp+vec2( 0.0, 1.0)*pixel_size,lod )*0.24477; - color+=textureLod( source_color, uv_interp+vec2( 0.0, 2.0)*pixel_size,lod )*0.06136; - color+=textureLod( source_color, uv_interp+vec2( 0.0,-1.0)*pixel_size,lod )*0.24477; - color+=textureLod( source_color, uv_interp+vec2( 0.0,-2.0)*pixel_size,lod )*0.06136; + vec4 color = textureLod(source_color, uv_interp + vec2(0.0, 0.0) * pixel_size, lod) * 0.38774; + color += textureLod(source_color, uv_interp + vec2(0.0, 1.0) * pixel_size, lod) * 0.24477; + color += textureLod(source_color, uv_interp + vec2(0.0, 2.0) * pixel_size, lod) * 0.06136; + color += textureLod(source_color, uv_interp + vec2(0.0, -1.0) * pixel_size, lod) * 0.24477; + color += textureLod(source_color, uv_interp + vec2(0.0, -2.0) * pixel_size, lod) * 0.06136; frag_color = color; #endif -//glow uses larger sigma for a more rounded blur effect + //glow uses larger sigma for a more rounded blur effect #ifdef GLOW_GAUSSIAN_HORIZONTAL vec2 pix_size = pixel_size; - pix_size*=0.5; //reading from larger buffer, so use more samples - vec4 color =textureLod( source_color, uv_interp+vec2( 0.0, 0.0)*pix_size,lod )*0.174938; - color+=textureLod( source_color, uv_interp+vec2( 1.0, 0.0)*pix_size,lod )*0.165569; - color+=textureLod( source_color, uv_interp+vec2( 2.0, 0.0)*pix_size,lod )*0.140367; - color+=textureLod( source_color, uv_interp+vec2( 3.0, 0.0)*pix_size,lod )*0.106595; - color+=textureLod( source_color, uv_interp+vec2(-1.0, 0.0)*pix_size,lod )*0.165569; - color+=textureLod( source_color, uv_interp+vec2(-2.0, 0.0)*pix_size,lod )*0.140367; - color+=textureLod( source_color, uv_interp+vec2(-3.0, 0.0)*pix_size,lod )*0.106595; - color*=glow_strength; + pix_size *= 0.5; //reading from larger buffer, so use more samples + vec4 color = textureLod(source_color, uv_interp + vec2(0.0, 0.0) * pix_size, lod) * 0.174938; + color += textureLod(source_color, uv_interp + vec2(1.0, 0.0) * pix_size, lod) * 0.165569; + color += textureLod(source_color, uv_interp + vec2(2.0, 0.0) * pix_size, lod) * 0.140367; + color += textureLod(source_color, uv_interp + vec2(3.0, 0.0) * pix_size, lod) * 0.106595; + color += textureLod(source_color, uv_interp + vec2(-1.0, 0.0) * pix_size, lod) * 0.165569; + color += textureLod(source_color, uv_interp + vec2(-2.0, 0.0) * pix_size, lod) * 0.140367; + color += textureLod(source_color, uv_interp + vec2(-3.0, 0.0) * pix_size, lod) * 0.106595; + color *= glow_strength; frag_color = color; #endif #ifdef GLOW_GAUSSIAN_VERTICAL - vec4 color =textureLod( source_color, uv_interp+vec2(0.0, 0.0)*pixel_size,lod )*0.288713; - color+=textureLod( source_color, uv_interp+vec2(0.0, 1.0)*pixel_size,lod )*0.233062; - color+=textureLod( source_color, uv_interp+vec2(0.0, 2.0)*pixel_size,lod )*0.122581; - color+=textureLod( source_color, uv_interp+vec2(0.0,-1.0)*pixel_size,lod )*0.233062; - color+=textureLod( source_color, uv_interp+vec2(0.0,-2.0)*pixel_size,lod )*0.122581; - color*=glow_strength; + vec4 color = textureLod(source_color, uv_interp + vec2(0.0, 0.0) * pixel_size, lod) * 0.288713; + color += textureLod(source_color, uv_interp + vec2(0.0, 1.0) * pixel_size, lod) * 0.233062; + color += textureLod(source_color, uv_interp + vec2(0.0, 2.0) * pixel_size, lod) * 0.122581; + color += textureLod(source_color, uv_interp + vec2(0.0, -1.0) * pixel_size, lod) * 0.233062; + color += textureLod(source_color, uv_interp + vec2(0.0, -2.0) * pixel_size, lod) * 0.122581; + color *= glow_strength; frag_color = color; #endif @@ -166,47 +165,45 @@ void main() { vec4 color_accum = vec4(0.0); - float depth = textureLod( dof_source_depth, uv_interp, 0.0).r; + float depth = textureLod(dof_source_depth, uv_interp, 0.0).r; depth = depth * 2.0 - 1.0; #ifdef USE_ORTHOGONAL_PROJECTION - depth = ((depth + (camera_z_far + camera_z_near)/(camera_z_far - camera_z_near)) * (camera_z_far - camera_z_near))/2.0; + depth = ((depth + (camera_z_far + camera_z_near) / (camera_z_far - camera_z_near)) * (camera_z_far - camera_z_near)) / 2.0; #else depth = 2.0 * camera_z_near * camera_z_far / (camera_z_far + camera_z_near - depth * (camera_z_far - camera_z_near)); #endif - float amount = smoothstep(dof_begin,dof_end,depth); - float k_accum=0.0; + float amount = smoothstep(dof_begin, dof_end, depth); + float k_accum = 0.0; - for(int i=0;i<dof_kernel_size;i++) { + for (int i = 0; i < dof_kernel_size; i++) { - int int_ofs = i-dof_kernel_from; + int int_ofs = i - dof_kernel_from; vec2 tap_uv = uv_interp + dof_dir * float(int_ofs) * amount * dof_radius; float tap_k = dof_kernel[i]; - float tap_depth = texture( dof_source_depth, tap_uv, 0.0).r; + float tap_depth = texture(dof_source_depth, tap_uv, 0.0).r; tap_depth = tap_depth * 2.0 - 1.0; #ifdef USE_ORTHOGONAL_PROJECTION - tap_depth = ((tap_depth + (camera_z_far + camera_z_near)/(camera_z_far - camera_z_near)) * (camera_z_far - camera_z_near))/2.0; + tap_depth = ((tap_depth + (camera_z_far + camera_z_near) / (camera_z_far - camera_z_near)) * (camera_z_far - camera_z_near)) / 2.0; #else tap_depth = 2.0 * camera_z_near * camera_z_far / (camera_z_far + camera_z_near - tap_depth * (camera_z_far - camera_z_near)); #endif - float tap_amount = mix(smoothstep(dof_begin,dof_end,tap_depth),1.0,int_ofs==0); - tap_amount*=tap_amount*tap_amount; //prevent undesired glow effect - - vec4 tap_color = textureLod( source_color, tap_uv, 0.0) * tap_k; - - k_accum+=tap_k*tap_amount; - color_accum+=tap_color*tap_amount; + float tap_amount = mix(smoothstep(dof_begin, dof_end, tap_depth), 1.0, int_ofs == 0); + tap_amount *= tap_amount * tap_amount; //prevent undesired glow effect + vec4 tap_color = textureLod(source_color, tap_uv, 0.0) * tap_k; + k_accum += tap_k * tap_amount; + color_accum += tap_color * tap_amount; } - if (k_accum>0.0) { - color_accum/=k_accum; + if (k_accum > 0.0) { + color_accum /= k_accum; } - frag_color = color_accum;///k_accum; + frag_color = color_accum; ///k_accum; #endif @@ -214,47 +211,45 @@ void main() { vec4 color_accum = vec4(0.0); - float max_accum=0; + float max_accum = 0; - for(int i=0;i<dof_kernel_size;i++) { + for (int i = 0; i < dof_kernel_size; i++) { - int int_ofs = i-dof_kernel_from; + int int_ofs = i - dof_kernel_from; vec2 tap_uv = uv_interp + dof_dir * float(int_ofs) * dof_radius; - float ofs_influence = max(0.0,1.0-float(abs(int_ofs))/float(dof_kernel_from)); + float ofs_influence = max(0.0, 1.0 - float(abs(int_ofs)) / float(dof_kernel_from)); float tap_k = dof_kernel[i]; - vec4 tap_color = textureLod( source_color, tap_uv, 0.0); + vec4 tap_color = textureLod(source_color, tap_uv, 0.0); - float tap_depth = texture( dof_source_depth, tap_uv, 0.0).r; + float tap_depth = texture(dof_source_depth, tap_uv, 0.0).r; tap_depth = tap_depth * 2.0 - 1.0; -#ifdef USE_ORTHOGONAL_PROJECTION - tap_depth = ((tap_depth + (camera_z_far + camera_z_near)/(camera_z_far - camera_z_near)) * (camera_z_far - camera_z_near))/2.0; +#ifdef USE_ORTHOGONAL_PROJECTION + tap_depth = ((tap_depth + (camera_z_far + camera_z_near) / (camera_z_far - camera_z_near)) * (camera_z_far - camera_z_near)) / 2.0; #else tap_depth = 2.0 * camera_z_near * camera_z_far / (camera_z_far + camera_z_near - tap_depth * (camera_z_far - camera_z_near)); #endif - float tap_amount = 1.0-smoothstep(dof_end,dof_begin,tap_depth); - tap_amount*=tap_amount*tap_amount; //prevent undesired glow effect + float tap_amount = 1.0 - smoothstep(dof_end, dof_begin, tap_depth); + tap_amount *= tap_amount * tap_amount; //prevent undesired glow effect #ifdef DOF_NEAR_FIRST_TAP - tap_color.a= 1.0-smoothstep(dof_end,dof_begin,tap_depth); + tap_color.a = 1.0 - smoothstep(dof_end, dof_begin, tap_depth); #endif - max_accum=max(max_accum,tap_amount*ofs_influence); - - color_accum+=tap_color*tap_k; + max_accum = max(max_accum, tap_amount * ofs_influence); + color_accum += tap_color * tap_k; } - color_accum.a=max(color_accum.a,sqrt(max_accum)); - + color_accum.a = max(color_accum.a, sqrt(max_accum)); #ifdef DOF_NEAR_BLUR_MERGE - vec4 original = textureLod( source_dof_original, uv_interp, 0.0); - color_accum = mix(original,color_accum,color_accum.a); + vec4 original = textureLod(source_dof_original, uv_interp, 0.0); + color_accum = mix(original, color_accum, color_accum.a); #endif @@ -265,37 +260,32 @@ void main() { #endif - - #ifdef GLOW_FIRST_PASS #ifdef GLOW_USE_AUTO_EXPOSURE - frag_color/=texelFetch(source_auto_exposure,ivec2(0,0),0).r/auto_exposure_grey; + frag_color /= texelFetch(source_auto_exposure, ivec2(0, 0), 0).r / auto_exposure_grey; #endif - frag_color*=exposure; + frag_color *= exposure; - float luminance = max(frag_color.r,max(frag_color.g,frag_color.b)); - float feedback = max( smoothstep(glow_hdr_threshold,glow_hdr_threshold+glow_hdr_scale,luminance), glow_bloom ); + float luminance = max(frag_color.r, max(frag_color.g, frag_color.b)); + float feedback = max(smoothstep(glow_hdr_threshold, glow_hdr_threshold + glow_hdr_scale, luminance), glow_bloom); frag_color *= feedback; #endif - #ifdef SIMPLE_COPY - vec4 color =textureLod( source_color, uv_interp,0.0); + vec4 color = textureLod(source_color, uv_interp, 0.0); frag_color = color; #endif #ifdef SSAO_MERGE - vec4 color =textureLod( source_color, uv_interp,0.0); - float ssao =textureLod( source_ssao, uv_interp,0.0).r; + vec4 color = textureLod(source_color, uv_interp, 0.0); + float ssao = textureLod(source_ssao, uv_interp, 0.0).r; - frag_color = vec4( mix(color.rgb,color.rgb*mix(ssao_color.rgb,vec3(1.0),ssao),color.a), 1.0 ); + frag_color = vec4(mix(color.rgb, color.rgb * mix(ssao_color.rgb, vec3(1.0), ssao), color.a), 1.0); #endif - - } diff --git a/drivers/gles2/shaders/exposure.glsl b/drivers/gles2/shaders/exposure.glsl index 001b90a0f1..759adcda06 100644 --- a/drivers/gles2/shaders/exposure.glsl +++ b/drivers/gles2/shaders/exposure.glsl @@ -1,19 +1,19 @@ +/* clang-format off */ [vertex] - -layout(location=0) in highp vec4 vertex_attrib; - +layout(location = 0) in highp vec4 vertex_attrib; +/* clang-format on */ void main() { gl_Position = vertex_attrib; - } +/* clang-format off */ [fragment] - uniform highp sampler2D source_exposure; //texunit:0 +/* clang-format on */ #ifdef EXPOSURE_BEGIN @@ -33,66 +33,56 @@ uniform highp float max_luminance; layout(location = 0) out highp float exposure; - - void main() { - - #ifdef EXPOSURE_BEGIN - - ivec2 src_pos = ivec2(gl_FragCoord.xy)*source_render_size/target_size; + ivec2 src_pos = ivec2(gl_FragCoord.xy) * source_render_size / target_size; #if 1 //more precise and expensive, but less jittery - ivec2 next_pos = ivec2(gl_FragCoord.xy+ivec2(1))*source_render_size/target_size; - next_pos = max(next_pos,src_pos+ivec2(1)); //so it at least reads one pixel - highp vec3 source_color=vec3(0.0); - for(int i=src_pos.x;i<next_pos.x;i++) { - for(int j=src_pos.y;j<next_pos.y;j++) { - source_color += texelFetch(source_exposure,ivec2(i,j),0).rgb; + ivec2 next_pos = ivec2(gl_FragCoord.xy + ivec2(1)) * source_render_size / target_size; + next_pos = max(next_pos, src_pos + ivec2(1)); //so it at least reads one pixel + highp vec3 source_color = vec3(0.0); + for (int i = src_pos.x; i < next_pos.x; i++) { + for (int j = src_pos.y; j < next_pos.y; j++) { + source_color += texelFetch(source_exposure, ivec2(i, j), 0).rgb; } } - source_color/=float( (next_pos.x-src_pos.x)*(next_pos.y-src_pos.y) ); + source_color /= float((next_pos.x - src_pos.x) * (next_pos.y - src_pos.y)); #else - highp vec3 source_color = texelFetch(source_exposure,src_pos,0).rgb; + highp vec3 source_color = texelFetch(source_exposure, src_pos, 0).rgb; #endif - exposure = max(source_color.r,max(source_color.g,source_color.b)); + exposure = max(source_color.r, max(source_color.g, source_color.b)); #else ivec2 coord = ivec2(gl_FragCoord.xy); - exposure = texelFetch(source_exposure,coord*3+ivec2(0,0),0).r; - exposure += texelFetch(source_exposure,coord*3+ivec2(1,0),0).r; - exposure += texelFetch(source_exposure,coord*3+ivec2(2,0),0).r; - exposure += texelFetch(source_exposure,coord*3+ivec2(0,1),0).r; - exposure += texelFetch(source_exposure,coord*3+ivec2(1,1),0).r; - exposure += texelFetch(source_exposure,coord*3+ivec2(2,1),0).r; - exposure += texelFetch(source_exposure,coord*3+ivec2(0,2),0).r; - exposure += texelFetch(source_exposure,coord*3+ivec2(1,2),0).r; - exposure += texelFetch(source_exposure,coord*3+ivec2(2,2),0).r; - exposure *= (1.0/9.0); + exposure = texelFetch(source_exposure, coord * 3 + ivec2(0, 0), 0).r; + exposure += texelFetch(source_exposure, coord * 3 + ivec2(1, 0), 0).r; + exposure += texelFetch(source_exposure, coord * 3 + ivec2(2, 0), 0).r; + exposure += texelFetch(source_exposure, coord * 3 + ivec2(0, 1), 0).r; + exposure += texelFetch(source_exposure, coord * 3 + ivec2(1, 1), 0).r; + exposure += texelFetch(source_exposure, coord * 3 + ivec2(2, 1), 0).r; + exposure += texelFetch(source_exposure, coord * 3 + ivec2(0, 2), 0).r; + exposure += texelFetch(source_exposure, coord * 3 + ivec2(1, 2), 0).r; + exposure += texelFetch(source_exposure, coord * 3 + ivec2(2, 2), 0).r; + exposure *= (1.0 / 9.0); #ifdef EXPOSURE_END #ifdef EXPOSURE_FORCE_SET //will stay as is #else - highp float prev_lum = texelFetch(prev_exposure,ivec2(0,0),0).r; //1 pixel previous exposure - exposure = clamp( prev_lum + (exposure-prev_lum)*exposure_adjust,min_luminance,max_luminance); + highp float prev_lum = texelFetch(prev_exposure, ivec2(0, 0), 0).r; //1 pixel previous exposure + exposure = clamp(prev_lum + (exposure - prev_lum) * exposure_adjust, min_luminance, max_luminance); #endif //EXPOSURE_FORCE_SET - #endif //EXPOSURE_END #endif //EXPOSURE_BEGIN - - } - - diff --git a/drivers/gles2/shaders/lens_distorted.glsl b/drivers/gles2/shaders/lens_distorted.glsl new file mode 100644 index 0000000000..d541db9bf9 --- /dev/null +++ b/drivers/gles2/shaders/lens_distorted.glsl @@ -0,0 +1,62 @@ +/* clang-format off */ +[vertex] + +attribute highp vec2 vertex; // attrib:0 +/* clang-format on */ + +uniform vec2 offset; +uniform vec2 scale; + +varying vec2 uv_interp; + +void main() { + + uv_interp = vertex.xy * 2.0 - 1.0; + + vec2 v = vertex.xy * scale + offset; + gl_Position = vec4(v, 0.0, 1.0); +} + +/* clang-format off */ +[fragment] + +uniform sampler2D source; //texunit:0 +/* clang-format on */ + +uniform vec2 eye_center; +uniform float k1; +uniform float k2; +uniform float upscale; +uniform float aspect_ratio; + +varying vec2 uv_interp; + +void main() { + vec2 coords = uv_interp; + vec2 offset = coords - eye_center; + + // take aspect ratio into account + offset.y /= aspect_ratio; + + // distort + vec2 offset_sq = offset * offset; + float radius_sq = offset_sq.x + offset_sq.y; + float radius_s4 = radius_sq * radius_sq; + float distortion_scale = 1.0 + (k1 * radius_sq) + (k2 * radius_s4); + offset *= distortion_scale; + + // reapply aspect ratio + offset.y *= aspect_ratio; + + // add our eye center back in + coords = offset + eye_center; + coords /= upscale; + + // and check our color + if (coords.x < -1.0 || coords.y < -1.0 || coords.x > 1.0 || coords.y > 1.0) { + gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0); + } else { + coords = (coords + vec2(1.0)) / vec2(2.0); + gl_FragColor = texture2D(source, coords); + } +} diff --git a/drivers/gles2/shaders/particles.glsl b/drivers/gles2/shaders/particles.glsl index a62c124dfe..5974050fc1 100644 --- a/drivers/gles2/shaders/particles.glsl +++ b/drivers/gles2/shaders/particles.glsl @@ -1,14 +1,13 @@ +/* clang-format off */ [vertex] - - -layout(location=0) in highp vec4 color; -layout(location=1) in highp vec4 velocity_active; -layout(location=2) in highp vec4 custom; -layout(location=3) in highp vec4 xform_1; -layout(location=4) in highp vec4 xform_2; -layout(location=5) in highp vec4 xform_3; - +layout(location = 0) in highp vec4 color; +/* clang-format on */ +layout(location = 1) in highp vec4 velocity_active; +layout(location = 2) in highp vec4 custom; +layout(location = 3) in highp vec4 xform_1; +layout(location = 4) in highp vec4 xform_2; +layout(location = 5) in highp vec4 xform_3; struct Attractor { @@ -39,7 +38,6 @@ uniform float lifetime; uniform mat4 emission_transform; uniform uint random_seed; - out highp vec4 out_color; //tfb: out highp vec4 out_velocity_active; //tfb: out highp vec4 out_custom; //tfb: @@ -47,20 +45,24 @@ out highp vec4 out_xform_1; //tfb: out highp vec4 out_xform_2; //tfb: out highp vec4 out_xform_3; //tfb: - #if defined(USE_MATERIAL) +/* clang-format off */ layout(std140) uniform UniformData { //ubo:0 MATERIAL_UNIFORMS }; +/* clang-format on */ #endif +/* clang-format off */ VERTEX_SHADER_GLOBALS +/* clang-format on */ + uint hash(uint x) { x = ((x >> uint(16)) ^ x) * uint(0x45d9f3b); @@ -69,13 +71,12 @@ uint hash(uint x) { return x; } - void main() { #ifdef PARTICLES_COPY - out_color=color; - out_velocity_active=velocity_active; + out_color = color; + out_velocity_active = velocity_active; out_custom = custom; out_xform_1 = xform_1; out_xform_2 = xform_2; @@ -83,34 +84,34 @@ void main() { #else - bool apply_forces=true; - bool apply_velocity=true; - float local_delta=delta; + bool apply_forces = true; + bool apply_velocity = true; + float local_delta = delta; float mass = 1.0; - float restart_phase = float(gl_VertexID)/float(total_particles); + float restart_phase = float(gl_VertexID) / float(total_particles); - if (randomness>0.0) { + if (randomness > 0.0) { uint seed = cycle; if (restart_phase >= system_phase) { - seed-=uint(1); + seed -= uint(1); } - seed*=uint(total_particles); - seed+=uint(gl_VertexID); + seed *= uint(total_particles); + seed += uint(gl_VertexID); float random = float(hash(seed) % uint(65536)) / 65536.0; - restart_phase+=randomness * random * 1.0 / float(total_particles); + restart_phase += randomness * random * 1.0 / float(total_particles); } - restart_phase*= (1.0-explosiveness); - bool restart=false; + restart_phase *= (1.0 - explosiveness); + bool restart = false; bool shader_active = velocity_active.a > 0.5; if (system_phase > prev_system_phase) { // restart_phase >= prev_system_phase is used so particles emit in the first frame they are processed - if (restart_phase >= prev_system_phase && restart_phase < system_phase ) { - restart=true; + if (restart_phase >= prev_system_phase && restart_phase < system_phase) { + restart = true; #ifdef USE_FRACTIONAL_DELTA local_delta = (system_phase - restart_phase) * lifetime; #endif @@ -118,12 +119,12 @@ void main() { } else { if (restart_phase >= prev_system_phase) { - restart=true; + restart = true; #ifdef USE_FRACTIONAL_DELTA local_delta = (1.0 - restart_phase + system_phase) * lifetime; #endif - } else if (restart_phase < system_phase ) { - restart=true; + } else if (restart_phase < system_phase) { + restart = true; #ifdef USE_FRACTIONAL_DELTA local_delta = (system_phase - restart_phase) * lifetime; #endif @@ -133,14 +134,14 @@ void main() { uint current_cycle = cycle; if (system_phase < restart_phase) { - current_cycle-=uint(1); + current_cycle -= uint(1); } uint particle_number = current_cycle * uint(total_particles) + uint(gl_VertexID); int index = int(gl_VertexID); if (restart) { - shader_active=emitting; + shader_active = emitting; } mat4 xform; @@ -150,30 +151,33 @@ void main() { #else if (clear || restart) { #endif - out_color=vec4(1.0); - out_velocity_active=vec4(0.0); - out_custom=vec4(0.0); + out_color = vec4(1.0); + out_velocity_active = vec4(0.0); + out_custom = vec4(0.0); if (!restart) - shader_active=false; + shader_active = false; xform = mat4( - vec4(1.0,0.0,0.0,0.0), - vec4(0.0,1.0,0.0,0.0), - vec4(0.0,0.0,1.0,0.0), - vec4(0.0,0.0,0.0,1.0) - ); + vec4(1.0, 0.0, 0.0, 0.0), + vec4(0.0, 1.0, 0.0, 0.0), + vec4(0.0, 0.0, 1.0, 0.0), + vec4(0.0, 0.0, 0.0, 1.0)); } else { - out_color=color; - out_velocity_active=velocity_active; - out_custom=custom; - xform = transpose(mat4(xform_1,xform_2,xform_3,vec4(vec3(0.0),1.0))); + out_color = color; + out_velocity_active = velocity_active; + out_custom = custom; + xform = transpose(mat4(xform_1, xform_2, xform_3, vec4(vec3(0.0), 1.0))); } if (shader_active) { //execute shader { + /* clang-format off */ + VERTEX_SHADER_CODE + + /* clang-format on */ } #if !defined(DISABLE_FORCE) @@ -181,26 +185,25 @@ VERTEX_SHADER_CODE if (false) { vec3 force = vec3(0.0); - for(int i=0;i<attractor_count;i++) { + for (int i = 0; i < attractor_count; i++) { vec3 rel_vec = xform[3].xyz - attractors[i].pos; float dist = length(rel_vec); if (attractors[i].radius < dist) continue; - if (attractors[i].eat_radius>0.0 && attractors[i].eat_radius > dist) { - out_velocity_active.a=0.0; + if (attractors[i].eat_radius > 0.0 && attractors[i].eat_radius > dist) { + out_velocity_active.a = 0.0; } rel_vec = normalize(rel_vec); - float attenuation = pow(dist / attractors[i].radius,attractors[i].attenuation); + float attenuation = pow(dist / attractors[i].radius, attractors[i].attenuation); - if (attractors[i].dir==vec3(0.0)) { + if (attractors[i].dir == vec3(0.0)) { //towards center - force+=attractors[i].strength * rel_vec * attenuation * mass; + force += attractors[i].strength * rel_vec * attenuation * mass; } else { - force+=attractors[i].strength * attractors[i].dir * attenuation *mass; - + force += attractors[i].strength * attractors[i].dir * attenuation * mass; } } @@ -216,26 +219,25 @@ VERTEX_SHADER_CODE } #endif } else { - xform=mat4(0.0); + xform = mat4(0.0); } xform = transpose(xform); - out_velocity_active.a = mix(0.0,1.0,shader_active); + out_velocity_active.a = mix(0.0, 1.0, shader_active); out_xform_1 = xform[0]; out_xform_2 = xform[1]; out_xform_3 = xform[2]; #endif //PARTICLES_COPY - } +/* clang-format off */ [fragment] //any code here is never executed, stuff is filled just so it works - #if defined(USE_MATERIAL) layout(std140) uniform UniformData { @@ -249,12 +251,16 @@ MATERIAL_UNIFORMS FRAGMENT_SHADER_GLOBALS void main() { - { + LIGHT_SHADER_CODE + } { + FRAGMENT_SHADER_CODE + } } +/* clang-format on */ diff --git a/drivers/gles2/shaders/resolve.glsl b/drivers/gles2/shaders/resolve.glsl index 0b50a9c57b..5c6f5d6561 100644 --- a/drivers/gles2/shaders/resolve.glsl +++ b/drivers/gles2/shaders/resolve.glsl @@ -1,18 +1,19 @@ +/* clang-format off */ [vertex] - -layout(location=0) in highp vec4 vertex_attrib; -layout(location=4) in vec2 uv_in; +layout(location = 0) in highp vec4 vertex_attrib; +/* clang-format on */ +layout(location = 4) in vec2 uv_in; out vec2 uv_interp; - void main() { uv_interp = uv_in; gl_Position = vertex_attrib; } +/* clang-format off */ [fragment] #if !defined(GLES_OVER_GL) @@ -20,6 +21,7 @@ precision mediump float; #endif in vec2 uv_interp; +/* clang-format on */ uniform sampler2D source_specular; //texunit:0 uniform sampler2D source_ssr; //texunit:1 @@ -31,14 +33,12 @@ layout(location = 0) out vec4 frag_color; void main() { - vec4 specular = texture( source_specular, uv_interp ); + vec4 specular = texture(source_specular, uv_interp); #ifdef USE_SSR - - vec4 ssr = textureLod(source_ssr,uv_interp,0.0); - specular.rgb = mix(specular.rgb,ssr.rgb*specular.a,ssr.a); + vec4 ssr = textureLod(source_ssr, uv_interp, 0.0); + specular.rgb = mix(specular.rgb, ssr.rgb * specular.a, ssr.a); #endif - frag_color = vec4(specular.rgb,1.0); + frag_color = vec4(specular.rgb, 1.0); } - diff --git a/drivers/gles2/shaders/scene.glsl b/drivers/gles2/shaders/scene.glsl index 9251e21080..30dc55cc4c 100644 --- a/drivers/gles2/shaders/scene.glsl +++ b/drivers/gles2/shaders/scene.glsl @@ -1,15 +1,19 @@ +/* clang-format off */ [vertex] #ifdef USE_GLES_OVER_GL #define mediump #define highp #else -precision mediump float; -precision mediump int; +precision highp float; +precision highp int; #endif #include "stdlib.glsl" +#define SHADER_IS_SRGB true + +#define M_PI 3.14159265359 // @@ -17,21 +21,22 @@ precision mediump int; // attribute highp vec4 vertex_attrib; // attrib:0 +/* clang-format on */ attribute vec3 normal_attrib; // attrib:1 #if defined(ENABLE_TANGENT_INTERP) || defined(ENABLE_NORMALMAP) attribute vec4 tangent_attrib; // attrib:2 #endif -#ifdef ENABLE_COLOR_INTERP +#if defined(ENABLE_COLOR_INTERP) attribute vec4 color_attrib; // attrib:3 #endif -#ifdef ENABLE_UV_INTERP +#if defined(ENABLE_UV_INTERP) attribute vec2 uv_attrib; // attrib:4 #endif -#ifdef ENABLE_UV2_INTERP +#if defined(ENABLE_UV2_INTERP) || defined(USE_LIGHTMAP) attribute vec2 uv2_attrib; // attrib:5 #endif @@ -39,9 +44,9 @@ attribute vec2 uv2_attrib; // attrib:5 #ifdef USE_SKELETON_SOFTWARE -attribute highp vec4 bone_transform_row_0; // attrib:9 -attribute highp vec4 bone_transform_row_1; // attrib:10 -attribute highp vec4 bone_transform_row_2; // attrib:11 +attribute highp vec4 bone_transform_row_0; // attrib:13 +attribute highp vec4 bone_transform_row_1; // attrib:14 +attribute highp vec4 bone_transform_row_2; // attrib:15 #else @@ -57,38 +62,35 @@ uniform ivec2 skeleton_texture_size; #ifdef USE_INSTANCING -attribute highp vec4 instance_xform_row_0; // attrib:12 -attribute highp vec4 instance_xform_row_1; // attrib:13 -attribute highp vec4 instance_xform_row_2; // attrib:14 +attribute highp vec4 instance_xform_row_0; // attrib:8 +attribute highp vec4 instance_xform_row_1; // attrib:9 +attribute highp vec4 instance_xform_row_2; // attrib:10 -attribute highp vec4 instance_color; // attrib:15 -attribute highp vec4 instance_custom_data; // attrib:8 +attribute highp vec4 instance_color; // attrib:11 +attribute highp vec4 instance_custom_data; // attrib:12 #endif - - // // uniforms // -uniform mat4 camera_matrix; -uniform mat4 camera_inverse_matrix; -uniform mat4 projection_matrix; -uniform mat4 projection_inverse_matrix; +uniform highp mat4 camera_matrix; +uniform highp mat4 camera_inverse_matrix; +uniform highp mat4 projection_matrix; +uniform highp mat4 projection_inverse_matrix; -uniform mat4 world_transform; +uniform highp mat4 world_transform; uniform highp float time; -uniform float normal_mult; +uniform highp vec2 viewport_size; #ifdef RENDER_DEPTH uniform float light_bias; uniform float light_normal_bias; #endif - // // varyings // @@ -101,21 +103,215 @@ varying vec3 tangent_interp; varying vec3 binormal_interp; #endif -#ifdef ENABLE_COLOR_INTERP +#if defined(ENABLE_COLOR_INTERP) varying vec4 color_interp; #endif -#ifdef ENABLE_UV_INTERP +#if defined(ENABLE_UV_INTERP) varying vec2 uv_interp; #endif -#ifdef ENABLE_UV2_INTERP +#if defined(ENABLE_UV2_INTERP) || defined(USE_LIGHTMAP) varying vec2 uv2_interp; #endif +/* clang-format off */ VERTEX_SHADER_GLOBALS +/* clang-format on */ + +#ifdef RENDER_DEPTH_DUAL_PARABOLOID + +varying highp float dp_clip; +uniform highp float shadow_dual_paraboloid_render_zfar; +uniform highp float shadow_dual_paraboloid_render_side; + +#endif + +#if defined(USE_SHADOW) && defined(USE_LIGHTING) + +uniform highp mat4 light_shadow_matrix; +varying highp vec4 shadow_coord; + +#if defined(LIGHT_USE_PSSM2) || defined(LIGHT_USE_PSSM4) +uniform highp mat4 light_shadow_matrix2; +varying highp vec4 shadow_coord2; +#endif + +#if defined(LIGHT_USE_PSSM4) + +uniform highp mat4 light_shadow_matrix3; +uniform highp mat4 light_shadow_matrix4; +varying highp vec4 shadow_coord3; +varying highp vec4 shadow_coord4; + +#endif + +#endif + +#if defined(USE_VERTEX_LIGHTING) && defined(USE_LIGHTING) + +varying highp vec3 diffuse_interp; +varying highp vec3 specular_interp; + +// general for all lights +uniform highp vec4 light_color; +uniform highp float light_specular; + +// directional +uniform highp vec3 light_direction; + +// omni +uniform highp vec3 light_position; + +uniform highp float light_range; +uniform highp float light_attenuation; + +// spot +uniform highp float light_spot_attenuation; +uniform highp float light_spot_range; +uniform highp float light_spot_angle; + +void light_compute( + vec3 N, + vec3 L, + vec3 V, + vec3 light_color, + vec3 attenuation, + float roughness) { + +//this makes lights behave closer to linear, but then addition of lights looks bad +//better left disabled + +//#define SRGB_APPROX(m_var) m_var = pow(m_var,0.4545454545); +/* +#define SRGB_APPROX(m_var) {\ + float S1 = sqrt(m_var);\ + float S2 = sqrt(S1);\ + float S3 = sqrt(S2);\ + m_var = 0.662002687 * S1 + 0.684122060 * S2 - 0.323583601 * S3 - 0.0225411470 * m_var;\ + } +*/ +#define SRGB_APPROX(m_var) + + float NdotL = dot(N, L); + float cNdotL = max(NdotL, 0.0); // clamped NdotL + float NdotV = dot(N, V); + float cNdotV = max(NdotV, 0.0); + +#if defined(DIFFUSE_OREN_NAYAR) + vec3 diffuse_brdf_NL; +#else + float diffuse_brdf_NL; // BRDF times N.L for calculating diffuse radiance +#endif + +#if defined(DIFFUSE_LAMBERT_WRAP) + // energy conserving lambert wrap shader + diffuse_brdf_NL = max(0.0, (NdotL + roughness) / ((1.0 + roughness) * (1.0 + roughness))); + +#elif defined(DIFFUSE_OREN_NAYAR) + + { + // see http://mimosa-pudica.net/improved-oren-nayar.html + float LdotV = dot(L, V); + + float s = LdotV - NdotL * NdotV; + float t = mix(1.0, max(NdotL, NdotV), step(0.0, s)); + + float sigma2 = roughness * roughness; // TODO: this needs checking + vec3 A = 1.0 + sigma2 * (-0.5 / (sigma2 + 0.33) + 0.17 * diffuse_color / (sigma2 + 0.13)); + float B = 0.45 * sigma2 / (sigma2 + 0.09); + + diffuse_brdf_NL = cNdotL * (A + vec3(B) * s / t) * (1.0 / M_PI); + } +#else + // lambert by default for everything else + diffuse_brdf_NL = cNdotL * (1.0 / M_PI); +#endif + + SRGB_APPROX(diffuse_brdf_NL) + + diffuse_interp += light_color * diffuse_brdf_NL * attenuation; + + if (roughness > 0.0) { + + // D + float specular_brdf_NL = 0.0; + +#if !defined(SPECULAR_DISABLED) + //normalized blinn always unless disabled + vec3 H = normalize(V + L); + float cNdotH = max(dot(N, H), 0.0); + float cVdotH = max(dot(V, H), 0.0); + float cLdotH = max(dot(L, H), 0.0); + float shininess = exp2(15.0 * (1.0 - roughness) + 1.0) * 0.25; + float blinn = pow(cNdotH, shininess); + blinn *= (shininess + 8.0) * (1.0 / (8.0 * M_PI)); + specular_brdf_NL = (blinn) / max(4.0 * cNdotV * cNdotL, 0.75); +#endif + + SRGB_APPROX(specular_brdf_NL) + specular_interp += specular_brdf_NL * light_color * attenuation; + } +} + +#endif + +#ifdef USE_VERTEX_LIGHTING + +#ifdef USE_REFLECTION_PROBE1 + +uniform highp mat4 refprobe1_local_matrix; +varying mediump vec4 refprobe1_reflection_normal_blend; +uniform highp vec3 refprobe1_box_extents; + +#ifndef USE_LIGHTMAP +varying mediump vec3 refprobe1_ambient_normal; +#endif + +#endif //reflection probe1 + +#ifdef USE_REFLECTION_PROBE2 + +uniform highp mat4 refprobe2_local_matrix; +varying mediump vec4 refprobe2_reflection_normal_blend; +uniform highp vec3 refprobe2_box_extents; + +#ifndef USE_LIGHTMAP +varying mediump vec3 refprobe2_ambient_normal; +#endif + +#endif //reflection probe2 + +#endif //vertex lighting for refprobes + +#if defined(FOG_DEPTH_ENABLED) || defined(FOG_HEIGHT_ENABLED) + +varying vec4 fog_interp; + +uniform mediump vec4 fog_color_base; +#ifdef LIGHT_MODE_DIRECTIONAL +uniform mediump vec4 fog_sun_color_amount; +#endif + +uniform bool fog_transmit_enabled; +uniform mediump float fog_transmit_curve; + +#ifdef FOG_DEPTH_ENABLED +uniform highp float fog_depth_begin; +uniform mediump float fog_depth_curve; +uniform mediump float fog_max_distance; +#endif + +#ifdef FOG_HEIGHT_ENABLED +uniform highp float fog_height_min; +uniform highp float fog_height_max; +uniform mediump float fog_height_curve; +#endif + +#endif //fog + void main() { highp vec4 vertex = vertex_attrib; @@ -124,35 +320,36 @@ void main() { #ifdef USE_INSTANCING { - highp mat4 m = mat4(instance_xform_row_0, - instance_xform_row_1, - instance_xform_row_2, - vec4(0.0, 0.0, 0.0, 1.0)); + highp mat4 m = mat4( + instance_xform_row_0, + instance_xform_row_1, + instance_xform_row_2, + vec4(0.0, 0.0, 0.0, 1.0)); world_matrix = world_matrix * transpose(m); } + #endif - vec3 normal = normal_attrib * normal_mult; + vec3 normal = normal_attrib; #if defined(ENABLE_TANGENT_INTERP) || defined(ENABLE_NORMALMAP) vec3 tangent = tangent_attrib.xyz; - tangent *= normal_mult; float binormalf = tangent_attrib.a; vec3 binormal = normalize(cross(normal, tangent) * binormalf); #endif -#ifdef ENABLE_COLOR_INTERP +#if defined(ENABLE_COLOR_INTERP) color_interp = color_attrib; #ifdef USE_INSTANCING color_interp *= instance_color; #endif #endif -#ifdef ENABLE_UV_INTERP +#if defined(ENABLE_UV_INTERP) uv_interp = uv_attrib; #endif -#ifdef ENABLE_UV2_INTERP +#if defined(ENABLE_UV2_INTERP) || defined(USE_LIGHTMAP) uv2_interp = uv2_attrib; #endif @@ -161,14 +358,14 @@ void main() { normal = normalize((world_matrix * vec4(normal, 0.0)).xyz); #if defined(ENABLE_TANGENT_INTERP) || defined(ENABLE_NORMALMAP) - tangent = normalize((world_matrix * vec4(tangent, 0.0)),xyz); + tangent = normalize((world_matrix * vec4(tangent, 0.0)).xyz); binormal = normalize((world_matrix * vec4(binormal, 0.0)).xyz); #endif #endif #ifdef USE_SKELETON - highp mat4 bone_transform = mat4(1.0); + highp mat4 bone_transform = mat4(0.0); #ifdef USE_SKELETON_SOFTWARE // passing the transform as attributes @@ -185,10 +382,11 @@ void main() { for (int i = 0; i < 4; i++) { ivec2 tex_ofs = ivec2(int(bone_ids[i]) * 3, 0); - highp mat4 b = mat4(texel2DFetch(bone_transforms, skeleton_texture_size, tex_ofs + ivec2(0, 0)), - texel2DFetch(bone_transforms, skeleton_texture_size, tex_ofs + ivec2(1, 0)), - texel2DFetch(bone_transforms, skeleton_texture_size, tex_ofs + ivec2(2, 0)), - vec4(0.0, 0.0, 0.0, 1.0)); + highp mat4 b = mat4( + texel2DFetch(bone_transforms, skeleton_texture_size, tex_ofs + ivec2(0, 0)), + texel2DFetch(bone_transforms, skeleton_texture_size, tex_ofs + ivec2(1, 0)), + texel2DFetch(bone_transforms, skeleton_texture_size, tex_ofs + ivec2(2, 0)), + vec4(0.0, 0.0, 0.0, 1.0)); bone_transform += transpose(b) * bone_weights[i]; } @@ -199,7 +397,6 @@ void main() { world_matrix = bone_transform * world_matrix; #endif - #ifdef USE_INSTANCING vec4 instance_custom = instance_custom_data; #else @@ -207,16 +404,18 @@ void main() { #endif - - mat4 modelview = camera_matrix * world_matrix; + mat4 modelview = camera_inverse_matrix * world_matrix; + float roughness = 1.0; #define world_transform world_matrix -{ + { + /* clang-format off */ VERTEX_SHADER_CODE -} + /* clang-format on */ + } vec4 outvec = vertex; @@ -232,11 +431,11 @@ VERTEX_SHADER_CODE #endif #if !defined(SKIP_TRANSFORM_USED) && defined(VERTEX_WORLD_COORDS_USED) - vertex = camera_matrix * vertex; - normal = normalize((camera_matrix * vec4(normal, 0.0)).xyz); + vertex = camera_inverse_matrix * vertex; + normal = normalize((camera_inverse_matrix * vec4(normal, 0.0)).xyz); #if defined(ENABLE_TANGENT_INTERP) || defined(ENABLE_NORMALMAP) - tangent = normalize((camera_matrix * vec4(tangent, 0.0)).xyz); - binormal = normalize((camera_matrix * vec4(binormal, 0.0)).xyz); + tangent = normalize((camera_inverse_matrix * vec4(tangent, 0.0)).xyz); + binormal = normalize((camera_inverse_matrix * vec4(binormal, 0.0)).xyz); #endif #endif @@ -250,60 +449,411 @@ VERTEX_SHADER_CODE #ifdef RENDER_DEPTH +#ifdef RENDER_DEPTH_DUAL_PARABOLOID + + vertex_interp.z *= shadow_dual_paraboloid_render_side; + normal_interp.z *= shadow_dual_paraboloid_render_side; + + dp_clip = vertex_interp.z; //this attempts to avoid noise caused by objects sent to the other parabolloid side due to bias + + //for dual paraboloid shadow mapping, this is the fastest but least correct way, as it curves straight edges + + highp vec3 vtx = vertex_interp + normalize(vertex_interp) * light_bias; + highp float distance = length(vtx); + vtx = normalize(vtx); + vtx.xy /= 1.0 - vtx.z; + vtx.z = (distance / shadow_dual_paraboloid_render_zfar); + vtx.z = vtx.z * 2.0 - 1.0; + + vertex_interp = vtx; + +#else float z_ofs = light_bias; z_ofs += (1.0 - abs(normal_interp.z)) * light_normal_bias; vertex_interp.z -= z_ofs; +#endif //dual parabolloid + +#endif //depth + +//vertex lighting +#if defined(USE_VERTEX_LIGHTING) && defined(USE_LIGHTING) + //vertex shaded version of lighting (more limited) + vec3 L; + vec3 light_att; + +#ifdef LIGHT_MODE_OMNI + vec3 light_vec = light_position - vertex_interp; + float light_length = length(light_vec); + + float normalized_distance = light_length / light_range; + + if (normalized_distance < 1.0) { + + float omni_attenuation = pow(1.0 - normalized_distance, light_attenuation); + + vec3 attenuation = vec3(omni_attenuation); + light_att = vec3(omni_attenuation); + } else { + light_att = vec3(0.0); + } + + L = normalize(light_vec); #endif - gl_Position = projection_matrix * vec4(vertex_interp, 1.0); +#ifdef LIGHT_MODE_SPOT + + vec3 light_rel_vec = light_position - vertex_interp; + float light_length = length(light_rel_vec); + float normalized_distance = light_length / light_range; + + if (normalized_distance < 1.0) { + + float spot_attenuation = pow(1.0 - normalized_distance, light_attenuation); + vec3 spot_dir = light_direction; + + float spot_cutoff = light_spot_angle; + + float angle = dot(-normalize(light_rel_vec), spot_dir); + + if (angle > spot_cutoff) { + + float scos = max(angle, spot_cutoff); + float spot_rim = max(0.0001, (1.0 - scos) / (1.0 - spot_cutoff)); + + spot_attenuation *= 1.0 - pow(spot_rim, light_spot_attenuation); + + light_att = vec3(spot_attenuation); + } else { + light_att = vec3(0.0); + } + } else { + light_att = vec3(0.0); + } + + L = normalize(light_rel_vec); + +#endif + +#ifdef LIGHT_MODE_DIRECTIONAL + vec3 light_vec = -light_direction; + light_att = vec3(1.0); //no base attenuation + L = normalize(light_vec); +#endif + + diffuse_interp = vec3(0.0); + specular_interp = vec3(0.0); + light_compute(normal_interp, L, -normalize(vertex_interp), light_color.rgb, light_att, roughness); + +#endif + +//shadows (for both vertex and fragment) +#if defined(USE_SHADOW) && defined(USE_LIGHTING) + + vec4 vi4 = vec4(vertex_interp, 1.0); + shadow_coord = light_shadow_matrix * vi4; + +#if defined(LIGHT_USE_PSSM2) || defined(LIGHT_USE_PSSM4) + shadow_coord2 = light_shadow_matrix2 * vi4; +#endif + +#if defined(LIGHT_USE_PSSM4) + shadow_coord3 = light_shadow_matrix3 * vi4; + shadow_coord4 = light_shadow_matrix4 * vi4; + +#endif + +#endif //use shadow and use lighting + +#ifdef USE_VERTEX_LIGHTING + +#ifdef USE_REFLECTION_PROBE1 + { + vec3 ref_normal = normalize(reflect(vertex_interp, normal_interp)); + vec3 local_pos = (refprobe1_local_matrix * vec4(vertex_interp, 1.0)).xyz; + vec3 inner_pos = abs(local_pos / refprobe1_box_extents); + float blend = max(inner_pos.x, max(inner_pos.y, inner_pos.z)); + + { + vec3 local_ref_vec = (refprobe1_local_matrix * vec4(ref_normal, 0.0)).xyz; + refprobe1_reflection_normal_blend.xyz = local_ref_vec; + refprobe1_reflection_normal_blend.a = blend; + } +#ifndef USE_LIGHTMAP + refprobe1_ambient_normal = (refprobe1_local_matrix * vec4(normal_interp, 0.0)).xyz; +#endif + } + +#endif //USE_REFLECTION_PROBE1 + +#ifdef USE_REFLECTION_PROBE2 + { + vec3 ref_normal = normalize(reflect(vertex_interp, normal_interp)); + vec3 local_pos = (refprobe2_local_matrix * vec4(vertex_interp, 1.0)).xyz; + vec3 inner_pos = abs(local_pos / refprobe2_box_extents); + float blend = max(inner_pos.x, max(inner_pos.y, inner_pos.z)); + + { + vec3 local_ref_vec = (refprobe2_local_matrix * vec4(ref_normal, 0.0)).xyz; + refprobe2_reflection_normal_blend.xyz = local_ref_vec; + refprobe2_reflection_normal_blend.a = blend; + } +#ifndef USE_LIGHTMAP + + refprobe2_ambient_normal = (refprobe2_local_matrix * vec4(normal_interp, 0.0)).xyz; +#endif + } + +#endif //USE_REFLECTION_PROBE2 + +#if defined(FOG_DEPTH_ENABLED) || defined(FOG_HEIGHT_ENABLED) + + float fog_amount = 0.0; + +#ifdef LIGHT_MODE_DIRECTIONAL + + vec3 fog_color = mix(fog_color_base.rgb, fog_sun_color_amount.rgb, fog_sun_color_amount.a * pow(max(dot(normalize(vertex_interp), light_direction), 0.0), 8.0)); +#else + vec3 fog_color = fog_color_base.rgb; +#endif + +#ifdef FOG_DEPTH_ENABLED + + { + + float fog_z = smoothstep(fog_depth_begin, fog_max_distance, length(vertex)); + + fog_amount = pow(fog_z, fog_depth_curve) * fog_color_base.a; + } +#endif + +#ifdef FOG_HEIGHT_ENABLED + { + float y = (camera_matrix * vec4(vertex_interp, 1.0)).y; + fog_amount = max(fog_amount, pow(smoothstep(fog_height_min, fog_height_max, y), fog_height_curve)); + } +#endif + fog_interp = vec4(fog_color, fog_amount); + +#endif //fog + +#endif //use vertex lighting + gl_Position = projection_matrix * vec4(vertex_interp, 1.0); } +/* clang-format off */ [fragment] -#extension GL_ARB_shader_texture_lod : require +#extension GL_ARB_shader_texture_lod : enable + +#ifndef GL_ARB_shader_texture_lod +#define texture2DLod(img, coord, lod) texture2D(img, coord) +#define textureCubeLod(img, coord, lod) textureCube(img, coord) +#endif #ifdef USE_GLES_OVER_GL #define mediump #define highp #else precision mediump float; -precision mediump int; +precision highp int; #endif #include "stdlib.glsl" #define M_PI 3.14159265359 +#define SHADER_IS_SRGB true // // uniforms // -uniform mat4 camera_matrix; -uniform mat4 camera_inverse_matrix; -uniform mat4 projection_matrix; -uniform mat4 projection_inverse_matrix; +uniform highp mat4 camera_matrix; +/* clang-format on */ +uniform highp mat4 camera_inverse_matrix; +uniform highp mat4 projection_matrix; +uniform highp mat4 projection_inverse_matrix; -uniform mat4 world_transform; +uniform highp mat4 world_transform; uniform highp float time; +uniform highp vec2 viewport_size; -#ifdef SCREEN_UV_USED +#if defined(SCREEN_UV_USED) uniform vec2 screen_pixel_size; #endif -uniform highp sampler2D depth_buffer; //texunit:-5 +// I think supporting this in GLES2 is difficult +// uniform highp sampler2D depth_buffer; #if defined(SCREEN_TEXTURE_USED) -uniform highp sampler2D screen_texture; //texunit:-6 +uniform highp sampler2D screen_texture; //texunit:-4 #endif -#ifdef USE_RADIANCE_MAP +#ifdef USE_REFLECTION_PROBE1 + +#ifdef USE_VERTEX_LIGHTING + +varying mediump vec4 refprobe1_reflection_normal_blend; +#ifndef USE_LIGHTMAP +varying mediump vec3 refprobe1_ambient_normal; +#endif + +#else + +uniform bool refprobe1_use_box_project; +uniform highp vec3 refprobe1_box_extents; +uniform vec3 refprobe1_box_offset; +uniform highp mat4 refprobe1_local_matrix; + +#endif //use vertex lighting + +uniform bool refprobe1_exterior; + +uniform highp samplerCube reflection_probe1; //texunit:-5 + +uniform float refprobe1_intensity; +uniform vec4 refprobe1_ambient; + +#endif //USE_REFLECTION_PROBE1 + +#ifdef USE_REFLECTION_PROBE2 + +#ifdef USE_VERTEX_LIGHTING + +varying mediump vec4 refprobe2_reflection_normal_blend; +#ifndef USE_LIGHTMAP +varying mediump vec3 refprobe2_ambient_normal; +#endif + +#else + +uniform bool refprobe2_use_box_project; +uniform highp vec3 refprobe2_box_extents; +uniform vec3 refprobe2_box_offset; +uniform highp mat4 refprobe2_local_matrix; + +#endif //use vertex lighting + +uniform bool refprobe2_exterior; + +uniform highp samplerCube reflection_probe2; //texunit:-6 + +uniform float refprobe2_intensity; +uniform vec4 refprobe2_ambient; + +#endif //USE_REFLECTION_PROBE2 #define RADIANCE_MAX_LOD 6.0 +#if defined(USE_REFLECTION_PROBE1) || defined(USE_REFLECTION_PROBE2) + +void reflection_process(samplerCube reflection_map, +#ifdef USE_VERTEX_LIGHTING + vec3 ref_normal, +#ifndef USE_LIGHTMAP + vec3 amb_normal, +#endif + float ref_blend, + +#else //no vertex lighting + vec3 normal, vec3 vertex, + mat4 local_matrix, + bool use_box_project, vec3 box_extents, vec3 box_offset, +#endif //vertex lighting + bool exterior, float intensity, vec4 ref_ambient, float roughness, vec3 ambient, vec3 skybox, inout highp vec4 reflection_accum, inout highp vec4 ambient_accum) { + + vec4 reflection; + +#ifdef USE_VERTEX_LIGHTING + + reflection.rgb = textureCubeLod(reflection_map, ref_normal, roughness * RADIANCE_MAX_LOD).rgb; + + float blend = ref_blend; //crappier blend formula for vertex + blend *= blend; + blend = max(0.0, 1.0 - blend); + +#else //fragment lighting + + vec3 local_pos = (local_matrix * vec4(vertex, 1.0)).xyz; + + if (any(greaterThan(abs(local_pos), box_extents))) { //out of the reflection box + return; + } + + vec3 inner_pos = abs(local_pos / box_extents); + float blend = max(inner_pos.x, max(inner_pos.y, inner_pos.z)); + blend = mix(length(inner_pos), blend, blend); + blend *= blend; + blend = max(0.0, 1.0 - blend); + + //reflect and make local + vec3 ref_normal = normalize(reflect(vertex, normal)); + ref_normal = (local_matrix * vec4(ref_normal, 0.0)).xyz; + + if (use_box_project) { //box project + + vec3 nrdir = normalize(ref_normal); + vec3 rbmax = (box_extents - local_pos) / nrdir; + vec3 rbmin = (-box_extents - local_pos) / nrdir; + + vec3 rbminmax = mix(rbmin, rbmax, vec3(greaterThan(nrdir, vec3(0.0, 0.0, 0.0)))); + + float fa = min(min(rbminmax.x, rbminmax.y), rbminmax.z); + vec3 posonbox = local_pos + nrdir * fa; + ref_normal = posonbox - box_offset.xyz; + } + + reflection.rgb = textureCubeLod(reflection_map, ref_normal, roughness * RADIANCE_MAX_LOD).rgb; +#endif + + if (exterior) { + reflection.rgb = mix(skybox, reflection.rgb, blend); + } + reflection.rgb *= intensity; + reflection.a = blend; + reflection.rgb *= blend; + + reflection_accum += reflection; + +#ifndef USE_LIGHTMAP + + vec4 ambient_out; +#ifndef USE_VERTEX_LIGHTING + + vec3 amb_normal = (local_matrix * vec4(normal, 0.0)).xyz; +#endif + + ambient_out.rgb = textureCubeLod(reflection_map, amb_normal, RADIANCE_MAX_LOD).rgb; + ambient_out.rgb = mix(ref_ambient.rgb, ambient_out.rgb, ref_ambient.a); + if (exterior) { + ambient_out.rgb = mix(ambient, ambient_out.rgb, blend); + } + + ambient_out.a = blend; + ambient_out.rgb *= blend; + ambient_accum += ambient_out; + +#endif +} + +#endif //use refprobe 1 or 2 + +#ifdef USE_LIGHTMAP +uniform mediump sampler2D lightmap; //texunit:-4 +uniform mediump float lightmap_energy; +#endif + +#ifdef USE_LIGHTMAP_CAPTURE +uniform mediump vec4[12] lightmap_captures; +uniform bool lightmap_capture_sky; + +#endif + +#ifdef USE_RADIANCE_MAP + uniform samplerCube radiance_map; // texunit:-2 uniform mat4 radiance_inverse_xform; @@ -316,52 +866,71 @@ uniform float ambient_sky_contribution; uniform vec4 ambient_color; uniform float ambient_energy; -#ifdef LIGHT_PASS +#ifdef USE_LIGHTING -#define LIGHT_TYPE_DIRECTIONAL 0 -#define LIGHT_TYPE_OMNI 1 -#define LIGHT_TYPE_SPOT 2 +#ifdef USE_VERTEX_LIGHTING -// general for all lights -uniform int light_type; +//get from vertex +varying highp vec3 diffuse_interp; +varying highp vec3 specular_interp; -uniform float light_energy; -uniform vec4 light_color; -uniform float light_specular; +uniform highp vec3 light_direction; //may be used by fog, so leave here -// directional -uniform vec3 light_direction; +#else +//done in fragment +// general for all lights +uniform highp vec4 light_color; +uniform highp float light_specular; +// directional +uniform highp vec3 light_direction; // omni -uniform vec3 light_position; +uniform highp vec3 light_position; -uniform float light_range; -uniform vec4 light_attenuation; +uniform highp float light_attenuation; // spot -uniform float light_spot_attenuation; -uniform float light_spot_range; -uniform float light_spot_angle; +uniform highp float light_spot_attenuation; +uniform highp float light_spot_range; +uniform highp float light_spot_angle; +#endif +//this is needed outside above if because dual paraboloid wants it +uniform highp float light_range; -// shadows -uniform highp sampler2D light_shadow_atlas; //texunit:-4 -uniform float light_has_shadow; +#ifdef USE_SHADOW -uniform mat4 light_shadow_matrix; -uniform vec4 light_clamp; +uniform highp vec2 shadow_pixel_size; -// directional shadow +#if defined(LIGHT_MODE_OMNI) || defined(LIGHT_MODE_SPOT) +uniform highp sampler2D light_shadow_atlas; //texunit:-3 +#endif -uniform highp sampler2D light_directional_shadow; // texunit:-4 -uniform vec4 light_split_offsets; +#ifdef LIGHT_MODE_DIRECTIONAL +uniform highp sampler2D light_directional_shadow; // texunit:-3 +uniform highp vec4 light_split_offsets; +#endif -uniform mat4 light_shadow_matrix1; -uniform mat4 light_shadow_matrix2; -uniform mat4 light_shadow_matrix3; -uniform mat4 light_shadow_matrix4; +varying highp vec4 shadow_coord; + +#if defined(LIGHT_USE_PSSM2) || defined(LIGHT_USE_PSSM4) +varying highp vec4 shadow_coord2; #endif +#if defined(LIGHT_USE_PSSM4) + +varying highp vec4 shadow_coord3; +varying highp vec4 shadow_coord4; + +#endif + +uniform vec4 light_clamp; + +#endif // light shadow + +// directional shadow + +#endif // // varyings @@ -375,103 +944,447 @@ varying vec3 tangent_interp; varying vec3 binormal_interp; #endif -#ifdef ENABLE_COLOR_INTERP +#if defined(ENABLE_COLOR_INTERP) varying vec4 color_interp; #endif -#ifdef ENABLE_UV_INTERP +#if defined(ENABLE_UV_INTERP) varying vec2 uv_interp; #endif -#ifdef ENABLE_UV2_INTERP +#if defined(ENABLE_UV2_INTERP) || defined(USE_LIGHTMAP) varying vec2 uv2_interp; #endif varying vec3 view_interp; -vec3 metallic_to_specular_color(float metallic, float specular, vec3 albedo) { - float dielectric = (0.034 * 2.0) * specular; - // energy conservation - return mix(vec3(dielectric), albedo, metallic); // TODO: reference? +vec3 F0(float metallic, float specular, vec3 albedo) { + float dielectric = 0.16 * specular * specular; + // use albedo * metallic as colored specular reflectance at 0 angle for metallic materials; + // see https://google.github.io/filament/Filament.md.html + return mix(vec3(dielectric), albedo, vec3(metallic)); } +/* clang-format off */ + FRAGMENT_SHADER_GLOBALS +/* clang-format on */ + +#ifdef RENDER_DEPTH_DUAL_PARABOLOID -#ifdef LIGHT_PASS -void light_compute(vec3 N, - vec3 L, - vec3 V, - vec3 B, - vec3 T, - vec3 light_color, - vec3 attenuation, - vec3 diffuse_color, - vec3 transmission, - float specular_blob_intensity, - float roughness, - float metallic, - float rim, - float rim_tint, - float clearcoat, - float clearcoat_gloss, - float anisotropy, - inout vec3 diffuse_light, - inout vec3 specular_light) { +varying highp float dp_clip; + +#endif + +#ifdef USE_LIGHTING + +// This returns the G_GGX function divided by 2 cos_theta_m, where in practice cos_theta_m is either N.L or N.V. +// We're dividing this factor off because the overall term we'll end up looks like +// (see, for example, the first unnumbered equation in B. Burley, "Physically Based Shading at Disney", SIGGRAPH 2012): +// +// F(L.V) D(N.H) G(N.L) G(N.V) / (4 N.L N.V) +// +// We're basically regouping this as +// +// F(L.V) D(N.H) [G(N.L)/(2 N.L)] [G(N.V) / (2 N.V)] +// +// and thus, this function implements the [G(N.m)/(2 N.m)] part with m = L or V. +// +// The contents of the D and G (G1) functions (GGX) are taken from +// E. Heitz, "Understanding the Masking-Shadowing Function in Microfacet-Based BRDFs", J. Comp. Graph. Tech. 3 (2) (2014). +// Eqns 71-72 and 85-86 (see also Eqns 43 and 80). + +/* +float G_GGX_2cos(float cos_theta_m, float alpha) { + // Schlick's approximation + // C. Schlick, "An Inexpensive BRDF Model for Physically-based Rendering", Computer Graphics Forum. 13 (3): 233 (1994) + // Eq. (19), although see Heitz (2014) the about the problems with his derivation. + // It nevertheless approximates GGX well with k = alpha/2. + float k = 0.5 * alpha; + return 0.5 / (cos_theta_m * (1.0 - k) + k); + + // float cos2 = cos_theta_m * cos_theta_m; + // float sin2 = (1.0 - cos2); + // return 1.0 / (cos_theta_m + sqrt(cos2 + alpha * alpha * sin2)); +} +*/ + +// This approximates G_GGX_2cos(cos_theta_l, alpha) * G_GGX_2cos(cos_theta_v, alpha) +// See Filament docs, Specular G section. +float V_GGX(float cos_theta_l, float cos_theta_v, float alpha) { + return 0.5 / mix(2.0 * cos_theta_l * cos_theta_v, cos_theta_l + cos_theta_v, alpha); +} + +float D_GGX(float cos_theta_m, float alpha) { + float alpha2 = alpha * alpha; + float d = 1.0 + (alpha2 - 1.0) * cos_theta_m * cos_theta_m; + return alpha2 / (M_PI * d * d); +} + +/* +float G_GGX_anisotropic_2cos(float cos_theta_m, float alpha_x, float alpha_y, float cos_phi, float sin_phi) { + float cos2 = cos_theta_m * cos_theta_m; + float sin2 = (1.0 - cos2); + float s_x = alpha_x * cos_phi; + float s_y = alpha_y * sin_phi; + return 1.0 / max(cos_theta_m + sqrt(cos2 + (s_x * s_x + s_y * s_y) * sin2), 0.001); +} +*/ + +// This approximates G_GGX_anisotropic_2cos(cos_theta_l, ...) * G_GGX_anisotropic_2cos(cos_theta_v, ...) +// See Filament docs, Anisotropic specular BRDF section. +float V_GGX_anisotropic(float alpha_x, float alpha_y, float TdotV, float TdotL, float BdotV, float BdotL, float NdotV, float NdotL) { + float Lambda_V = NdotL * length(vec3(alpha_x * TdotV, alpha_y * BdotV, NdotV)); + float Lambda_L = NdotV * length(vec3(alpha_x * TdotL, alpha_y * BdotL, NdotL)); + return 0.5 / (Lambda_V + Lambda_L); +} + +float D_GGX_anisotropic(float cos_theta_m, float alpha_x, float alpha_y, float cos_phi, float sin_phi, float NdotH) { + float alpha2 = alpha_x * alpha_y; + highp vec3 v = vec3(alpha_y * cos_phi, alpha_x * sin_phi, alpha2 * NdotH); + highp float v2 = dot(v, v); + float w2 = alpha2 / v2; + float D = alpha2 * w2 * w2 * (1.0 / M_PI); + return D; + + /* float cos2 = cos_theta_m * cos_theta_m; + float sin2 = (1.0 - cos2); + float r_x = cos_phi / alpha_x; + float r_y = sin_phi / alpha_y; + float d = cos2 + sin2 * (r_x * r_x + r_y * r_y); + return 1.0 / max(M_PI * alpha_x * alpha_y * d * d, 0.001); */ +} +float SchlickFresnel(float u) { + float m = 1.0 - u; + float m2 = m * m; + return m2 * m2 * m; // pow(m,5) +} + +float GTR1(float NdotH, float a) { + if (a >= 1.0) return 1.0 / M_PI; + float a2 = a * a; + float t = 1.0 + (a2 - 1.0) * NdotH * NdotH; + return (a2 - 1.0) / (M_PI * log(a2) * t); +} + +void light_compute( + vec3 N, + vec3 L, + vec3 V, + vec3 B, + vec3 T, + vec3 light_color, + vec3 attenuation, + vec3 diffuse_color, + vec3 transmission, + float specular_blob_intensity, + float roughness, + float metallic, + float specular, + float rim, + float rim_tint, + float clearcoat, + float clearcoat_gloss, + float anisotropy, + inout vec3 diffuse_light, + inout vec3 specular_light) { + +//this makes lights behave closer to linear, but then addition of lights looks bad +//better left disabled + +//#define SRGB_APPROX(m_var) m_var = pow(m_var,0.4545454545); +/* +#define SRGB_APPROX(m_var) {\ + float S1 = sqrt(m_var);\ + float S2 = sqrt(S1);\ + float S3 = sqrt(S2);\ + m_var = 0.662002687 * S1 + 0.684122060 * S2 - 0.323583601 * S3 - 0.0225411470 * m_var;\ + } +*/ +#define SRGB_APPROX(m_var) + +#if defined(USE_LIGHT_SHADER_CODE) + // light is written by the light shader + + vec3 normal = N; + vec3 albedo = diffuse_color; + vec3 light = L; + vec3 view = V; + + /* clang-format off */ + +LIGHT_SHADER_CODE + + /* clang-format on */ + +#else float NdotL = dot(N, L); - float cNdotL = max(NdotL, 0.0); + float cNdotL = max(NdotL, 0.0); // clamped NdotL float NdotV = dot(N, V); - float cNdotV = max(NdotV, 0.0); + float cNdotV = max(abs(NdotV), 1e-6); - { - // calculate diffuse reflection +#if defined(DIFFUSE_BURLEY) || defined(SPECULAR_BLINN) || defined(SPECULAR_SCHLICK_GGX) || defined(LIGHT_USE_CLEARCOAT) + vec3 H = normalize(V + L); +#endif + +#if defined(SPECULAR_BLINN) || defined(SPECULAR_SCHLICK_GGX) || defined(LIGHT_USE_CLEARCOAT) + float cNdotH = max(dot(N, H), 0.0); +#endif + +#if defined(DIFFUSE_BURLEY) || defined(SPECULAR_SCHLICK_GGX) || defined(LIGHT_USE_CLEARCOAT) + float cLdotH = max(dot(L, H), 0.0); +#endif + + if (metallic < 1.0) { +#if defined(DIFFUSE_OREN_NAYAR) + vec3 diffuse_brdf_NL; +#else + float diffuse_brdf_NL; // BRDF times N.L for calculating diffuse radiance +#endif + +#if defined(DIFFUSE_LAMBERT_WRAP) + // energy conserving lambert wrap shader + diffuse_brdf_NL = max(0.0, (NdotL + roughness) / ((1.0 + roughness) * (1.0 + roughness))); + +#elif defined(DIFFUSE_OREN_NAYAR) + + { + // see http://mimosa-pudica.net/improved-oren-nayar.html + float LdotV = dot(L, V); + + float s = LdotV - NdotL * NdotV; + float t = mix(1.0, max(NdotL, NdotV), step(0.0, s)); + + float sigma2 = roughness * roughness; // TODO: this needs checking + vec3 A = 1.0 + sigma2 * (-0.5 / (sigma2 + 0.33) + 0.17 * diffuse_color / (sigma2 + 0.13)); + float B = 0.45 * sigma2 / (sigma2 + 0.09); - // TODO hardcode Oren Nayar for now - float diffuse_brdf_NL; + diffuse_brdf_NL = cNdotL * (A + vec3(B) * s / t) * (1.0 / M_PI); + } + +#elif defined(DIFFUSE_TOON) + + diffuse_brdf_NL = smoothstep(-roughness, max(roughness, 0.01), NdotL); + +#elif defined(DIFFUSE_BURLEY) + + { + float FD90_minus_1 = 2.0 * cLdotH * cLdotH * roughness - 0.5; + float FdV = 1.0 + FD90_minus_1 * SchlickFresnel(cNdotV); + float FdL = 1.0 + FD90_minus_1 * SchlickFresnel(cNdotL); + diffuse_brdf_NL = (1.0 / M_PI) * FdV * FdL * cNdotL; + /* + float energyBias = mix(roughness, 0.0, 0.5); + float energyFactor = mix(roughness, 1.0, 1.0 / 1.51); + float fd90 = energyBias + 2.0 * VoH * VoH * roughness; + float f0 = 1.0; + float lightScatter = f0 + (fd90 - f0) * pow(1.0 - cNdotL, 5.0); + float viewScatter = f0 + (fd90 - f0) * pow(1.0 - cNdotV, 5.0); + + diffuse_brdf_NL = lightScatter * viewScatter * energyFactor; + */ + } +#else + // lambert + diffuse_brdf_NL = cNdotL * (1.0 / M_PI); +#endif - diffuse_brdf_NL = max(0.0,(NdotL + roughness) / ((1.0 + roughness) * (1.0 + roughness))); - // diffuse_brdf_NL = cNdotL * (1.0 / M_PI); + SRGB_APPROX(diffuse_brdf_NL) diffuse_light += light_color * diffuse_color * diffuse_brdf_NL * attenuation; + +#if defined(TRANSMISSION_USED) + diffuse_light += light_color * diffuse_color * (vec3(1.0 / M_PI) - diffuse_brdf_NL) * transmission * attenuation; +#endif + +#if defined(LIGHT_USE_RIM) + float rim_light = pow(max(0.0, 1.0 - cNdotV), max(0.0, (1.0 - roughness) * 16.0)); + diffuse_light += rim_light * rim * mix(vec3(1.0), diffuse_color, rim_tint) * light_color; +#endif } - { - // calculate specular reflection + if (roughness > 0.0) { + +#if defined(SPECULAR_SCHLICK_GGX) + vec3 specular_brdf_NL = vec3(0.0); +#else + float specular_brdf_NL = 0.0; +#endif + +#if defined(SPECULAR_BLINN) + + //normalized blinn + float shininess = exp2(15.0 * (1.0 - roughness) + 1.0) * 0.25; + float blinn = pow(cNdotH, shininess); + blinn *= (shininess + 8.0) * (1.0 / (8.0 * M_PI)); + specular_brdf_NL = (blinn) / max(4.0 * cNdotV * cNdotL, 0.75); + +#elif defined(SPECULAR_PHONG) + + vec3 R = normalize(-reflect(L, N)); + float cRdotV = max(0.0, dot(R, V)); + float shininess = exp2(15.0 * (1.0 - roughness) + 1.0) * 0.25; + float phong = pow(cRdotV, shininess); + phong *= (shininess + 8.0) * (1.0 / (8.0 * M_PI)); + specular_brdf_NL = (phong) / max(4.0 * cNdotV * cNdotL, 0.75); + +#elif defined(SPECULAR_TOON) + + vec3 R = normalize(-reflect(L, N)); + float RdotV = dot(R, V); + float mid = 1.0 - roughness; + mid *= mid; + specular_brdf_NL = smoothstep(mid - roughness * 0.5, mid + roughness * 0.5, RdotV) * mid; + +#elif defined(SPECULAR_DISABLED) + // none.. +#elif defined(SPECULAR_SCHLICK_GGX) + // shlick+ggx as default + +#if defined(LIGHT_USE_ANISOTROPY) + float alpha = roughness * roughness; + float aspect = sqrt(1.0 - anisotropy * 0.9); + float ax = alpha / aspect; + float ay = alpha * aspect; + float XdotH = dot(T, H); + float YdotH = dot(B, H); + float D = D_GGX_anisotropic(cNdotH, ax, ay, XdotH, YdotH, cNdotH); + //float G = G_GGX_anisotropic_2cos(cNdotL, ax, ay, XdotH, YdotH) * G_GGX_anisotropic_2cos(cNdotV, ax, ay, XdotH, YdotH); + float G = V_GGX_anisotropic(ax, ay, dot(T, V), dot(T, L), dot(B, V), dot(B, L), cNdotV, cNdotL)) - vec3 R = normalize(-reflect(L,N)); - float cRdotV = max(dot(R, V), 0.0); - float blob_intensity = pow(cRdotV, (1.0 - roughness) * 256.0); - specular_light += light_color * attenuation * blob_intensity * specular_blob_intensity; +#else + float alpha = roughness * roughness; + float D = D_GGX(cNdotH, alpha); + //float G = G_GGX_2cos(cNdotL, alpha) * G_GGX_2cos(cNdotV, alpha); + float G = V_GGX(cNdotL, cNdotV, alpha); +#endif + // F + vec3 f0 = F0(metallic, specular, diffuse_color); + float cLdotH5 = SchlickFresnel(cLdotH); + vec3 F = mix(vec3(cLdotH5), vec3(1.0), f0); + + specular_brdf_NL = cNdotL * D * F * G; + +#endif + + SRGB_APPROX(specular_brdf_NL) + specular_light += specular_brdf_NL * light_color * specular_blob_intensity * attenuation; + +#if defined(LIGHT_USE_CLEARCOAT) + +#if !defined(SPECULAR_SCHLICK_GGX) + float cLdotH5 = SchlickFresnel(cLdotH); +#endif + float Dr = GTR1(cNdotH, mix(.1, .001, clearcoat_gloss)); + float Fr = mix(.04, 1.0, cLdotH5); + //float Gr = G_GGX_2cos(cNdotL, .25) * G_GGX_2cos(cNdotV, .25); + float Gr = V_GGX(cNdotL, cNdotV, 0.25); + float clearcoat_specular_brdf_NL = 0.25 * clearcoat * Gr * Fr * Dr * cNdotL; + + specular_light += clearcoat_specular_brdf_NL * light_color * specular_blob_intensity * attenuation; +#endif } + +#endif //defined(USE_LIGHT_SHADER_CODE) } +#endif +// shadows +#ifdef USE_SHADOW + +#define SAMPLE_SHADOW_TEXEL(p_shadow, p_pos, p_depth) step(p_depth, texture2D(p_shadow, p_pos).r) +#define SAMPLE_SHADOW_TEXEL_PROJ(p_shadow, p_pos) step(p_pos.z, texture2DProj(p_shadow, p_pos).r) + +float sample_shadow(highp sampler2D shadow, highp vec4 spos) { + +#ifdef SHADOW_MODE_PCF_13 + + spos.xyz /= spos.w; + vec2 pos = spos.xy; + float depth = spos.z; + + float avg = SAMPLE_SHADOW_TEXEL(shadow, pos, depth); + avg += SAMPLE_SHADOW_TEXEL(shadow, pos + vec2(shadow_pixel_size.x, 0.0), depth); + avg += SAMPLE_SHADOW_TEXEL(shadow, pos + vec2(-shadow_pixel_size.x, 0.0), depth); + avg += SAMPLE_SHADOW_TEXEL(shadow, pos + vec2(0.0, shadow_pixel_size.y), depth); + avg += SAMPLE_SHADOW_TEXEL(shadow, pos + vec2(0.0, -shadow_pixel_size.y), depth); + avg += SAMPLE_SHADOW_TEXEL(shadow, pos + vec2(shadow_pixel_size.x, shadow_pixel_size.y), depth); + avg += SAMPLE_SHADOW_TEXEL(shadow, pos + vec2(-shadow_pixel_size.x, shadow_pixel_size.y), depth); + avg += SAMPLE_SHADOW_TEXEL(shadow, pos + vec2(shadow_pixel_size.x, -shadow_pixel_size.y), depth); + avg += SAMPLE_SHADOW_TEXEL(shadow, pos + vec2(-shadow_pixel_size.x, -shadow_pixel_size.y), depth); + avg += SAMPLE_SHADOW_TEXEL(shadow, pos + vec2(shadow_pixel_size.x * 2.0, 0.0), depth); + avg += SAMPLE_SHADOW_TEXEL(shadow, pos + vec2(-shadow_pixel_size.x * 2.0, 0.0), depth); + avg += SAMPLE_SHADOW_TEXEL(shadow, pos + vec2(0.0, shadow_pixel_size.y * 2.0), depth); + avg += SAMPLE_SHADOW_TEXEL(shadow, pos + vec2(0.0, -shadow_pixel_size.y * 2.0), depth); + return avg * (1.0 / 13.0); +#endif +#ifdef SHADOW_MODE_PCF_5 -// shadows + spos.xyz /= spos.w; + vec2 pos = spos.xy; + float depth = spos.z; -float sample_shadow(highp sampler2D shadow, - vec2 shadow_pixel_size, - vec2 pos, - float depth, - vec4 clamp_rect) -{ - // vec4 depth_value = texture2D(shadow, pos); - - // return depth_value.z; - return texture2DProj(shadow, vec4(pos, depth, 1.0)).r; - // return (depth_value.x + depth_value.y + depth_value.z + depth_value.w) / 4.0; + float avg = SAMPLE_SHADOW_TEXEL(shadow, pos, depth); + avg += SAMPLE_SHADOW_TEXEL(shadow, pos + vec2(shadow_pixel_size.x, 0.0), depth); + avg += SAMPLE_SHADOW_TEXEL(shadow, pos + vec2(-shadow_pixel_size.x, 0.0), depth); + avg += SAMPLE_SHADOW_TEXEL(shadow, pos + vec2(0.0, shadow_pixel_size.y), depth); + avg += SAMPLE_SHADOW_TEXEL(shadow, pos + vec2(0.0, -shadow_pixel_size.y), depth); + return avg * (1.0 / 5.0); + +#endif + +#if !defined(SHADOW_MODE_PCF_5) || !defined(SHADOW_MODE_PCF_13) + + return SAMPLE_SHADOW_TEXEL_PROJ(shadow, spos); +#endif } +#endif + +#if defined(FOG_DEPTH_ENABLED) || defined(FOG_HEIGHT_ENABLED) + +#if defined(USE_VERTEX_LIGHTING) +varying vec4 fog_interp; + +#else +uniform mediump vec4 fog_color_base; +#ifdef LIGHT_MODE_DIRECTIONAL +uniform mediump vec4 fog_sun_color_amount; #endif -void main() -{ +uniform bool fog_transmit_enabled; +uniform mediump float fog_transmit_curve; +#ifdef FOG_DEPTH_ENABLED +uniform highp float fog_depth_begin; +uniform mediump float fog_depth_curve; +uniform mediump float fog_max_distance; +#endif + +#ifdef FOG_HEIGHT_ENABLED +uniform highp float fog_height_min; +uniform highp float fog_height_max; +uniform mediump float fog_height_curve; +#endif + +#endif //vertex lit +#endif //fog + +void main() { + +#ifdef RENDER_DEPTH_DUAL_PARABOLOID + + if (dp_clip > 0.0) + discard; +#endif highp vec3 vertex = vertex_interp; + vec3 view = -normalize(vertex_interp); vec3 albedo = vec3(1.0); vec3 transmission = vec3(0.0); float metallic = 0.0; @@ -484,16 +1397,21 @@ void main() float clearcoat_gloss = 0.0; float anisotropy = 0.0; vec2 anisotropy_flow = vec2(1.0, 0.0); + float sss_strength = 0.0; //unused float alpha = 1.0; float side = 1.0; + float specular_blob_intensity = 1.0; +#if defined(SPECULAR_TOON) + specular_blob_intensity *= specular * 2.0; +#endif + #if defined(ENABLE_AO) float ao = 1.0; float ao_light_affect = 0.0; #endif - #if defined(ENABLE_TANGENT_INTERP) || defined(ENABLE_NORMALMAP) vec3 binormal = normalize(binormal_interp) * side; vec3 tangent = normalize(tangent_interp) * side; @@ -508,28 +1426,28 @@ void main() #endif float normaldepth = 1.0; - -#ifdef ALPHA_SCISSOR_USED +#if defined(ALPHA_SCISSOR_USED) float alpha_scissor = 0.5; #endif -#ifdef SCREEN_UV_USED +#if defined(SCREEN_UV_USED) vec2 screen_uv = gl_FragCoord.xy * screen_pixel_size; #endif -{ + { + /* clang-format off */ FRAGMENT_SHADER_CODE - -} + /* clang-format on */ + } #if defined(ENABLE_NORMALMAP) normalmap.xy = normalmap.xy * 2.0 - 1.0; - normalmap.z = sqrt(1.0 - dot(normalmap.xy, normalmap.xy)); + normalmap.z = sqrt(max(0.0, 1.0 - dot(normalmap.xy, normalmap.xy))); - // normal = normalize(mix(normal_interp, tangent * normalmap.x + binormal * normalmap.y + normal * normalmap.z, normaldepth)) * side; - normal = normalmap; + normal = normalize(mix(normal_interp, tangent * normalmap.x + binormal * normalmap.y + normal * normalmap.z, normaldepth)) * side; + //normal = normalmap; #endif normal = normalize(normal); @@ -538,129 +1456,356 @@ FRAGMENT_SHADER_CODE vec3 specular_light = vec3(0.0, 0.0, 0.0); vec3 diffuse_light = vec3(0.0, 0.0, 0.0); - vec3 ambient_light = vec3(0.0, 0.0, 0.0); - vec3 env_reflection_light = vec3(0.0, 0.0, 0.0); - - vec3 eye_position = -normalize(vertex_interp); + vec3 eye_position = view; -#ifdef ALPHA_SCISSOR_USED +#if defined(ALPHA_SCISSOR_USED) if (alpha < alpha_scissor) { discard; } #endif +#ifdef BASE_PASS + //none +#ifdef USE_RADIANCE_MAP + + vec3 ref_vec = reflect(-eye_position, N); + ref_vec = normalize((radiance_inverse_xform * vec4(ref_vec, 0.0)).xyz); + + ref_vec.z *= -1.0; + + specular_light = textureCubeLod(radiance_map, ref_vec, roughness * RADIANCE_MAX_LOD).xyz * bg_energy; + + { + vec3 ambient_dir = normalize((radiance_inverse_xform * vec4(normal, 0.0)).xyz); + vec3 env_ambient = textureCubeLod(radiance_map, ambient_dir, RADIANCE_MAX_LOD).xyz * bg_energy; + + ambient_light = mix(ambient_color.rgb, env_ambient, ambient_sky_contribution); + } + +#else + + ambient_light = ambient_color.rgb; + +#endif + + ambient_light *= ambient_energy; + +#if defined(USE_REFLECTION_PROBE1) || defined(USE_REFLECTION_PROBE2) + + vec4 ambient_accum = vec4(0.0); + vec4 reflection_accum = vec4(0.0); + +#ifdef USE_REFLECTION_PROBE1 + + reflection_process(reflection_probe1, +#ifdef USE_VERTEX_LIGHTING + refprobe1_reflection_normal_blend.rgb, +#ifndef USE_LIGHTMAP + refprobe1_ambient_normal, +#endif + refprobe1_reflection_normal_blend.a, +#else + normal_interp, vertex_interp, refprobe1_local_matrix, + refprobe1_use_box_project, refprobe1_box_extents, refprobe1_box_offset, +#endif + refprobe1_exterior, refprobe1_intensity, refprobe1_ambient, roughness, + ambient_light, specular_light, reflection_accum, ambient_accum); + +#endif // USE_REFLECTION_PROBE1 + +#ifdef USE_REFLECTION_PROBE2 + + reflection_process(reflection_probe2, +#ifdef USE_VERTEX_LIGHTING + refprobe2_reflection_normal_blend.rgb, +#ifndef USE_LIGHTMAP + refprobe2_ambient_normal, +#endif + refprobe2_reflection_normal_blend.a, +#else + normal_interp, vertex_interp, refprobe2_local_matrix, + refprobe2_use_box_project, refprobe2_box_extents, refprobe2_box_offset, +#endif + refprobe2_exterior, refprobe2_intensity, refprobe2_ambient, roughness, + ambient_light, specular_light, reflection_accum, ambient_accum); + +#endif // USE_REFLECTION_PROBE2 + + if (reflection_accum.a > 0.0) { + specular_light = reflection_accum.rgb / reflection_accum.a; + } + +#ifndef USE_LIGHTMAP + if (ambient_accum.a > 0.0) { + ambient_light = ambient_accum.rgb / ambient_accum.a; + } +#endif + +#endif // defined(USE_REFLECTION_PROBE1) || defined(USE_REFLECTION_PROBE2) + +#ifdef USE_LIGHTMAP + //ambient light will come entirely from lightmap is lightmap is used + ambient_light = texture2D(lightmap, uv2_interp).rgb * lightmap_energy; +#endif + +#ifdef USE_LIGHTMAP_CAPTURE + { + vec3 cone_dirs[12] = vec3[]( + vec3(0, 0, 1), + vec3(0.866025, 0, 0.5), + vec3(0.267617, 0.823639, 0.5), + vec3(-0.700629, 0.509037, 0.5), + vec3(-0.700629, -0.509037, 0.5), + vec3(0.267617, -0.823639, 0.5), + vec3(0, 0, -1), + vec3(0.866025, 0, -0.5), + vec3(0.267617, 0.823639, -0.5), + vec3(-0.700629, 0.509037, -0.5), + vec3(-0.700629, -0.509037, -0.5), + vec3(0.267617, -0.823639, -0.5)); + + vec3 local_normal = normalize(camera_matrix * vec4(normal, 0.0)).xyz; + vec4 captured = vec4(0.0); + float sum = 0.0; + for (int i = 0; i < 12; i++) { + float amount = max(0.0, dot(local_normal, cone_dirs[i])); //not correct, but creates a nice wrap around effect + captured += lightmap_captures[i] * amount; + sum += amount; + } + + captured /= sum; + + if (lightmap_capture_sky) { + ambient_light = mix(ambient_light, captured.rgb, captured.a); + } else { + ambient_light = captured.rgb; + } + } +#endif + +#endif //BASE PASS + // // Lighting // -#ifdef LIGHT_PASS +#ifdef USE_LIGHTING - if (light_type == LIGHT_TYPE_OMNI) { - vec3 light_vec = light_position - vertex; - float light_length = length(light_vec); +#ifndef USE_VERTEX_LIGHTING + vec3 L; +#endif + vec3 light_att = vec3(1.0); - float normalized_distance = light_length / light_range; +#ifdef LIGHT_MODE_OMNI - float omni_attenuation = pow(1.0 - normalized_distance, light_attenuation.w); +#ifndef USE_VERTEX_LIGHTING + vec3 light_vec = light_position - vertex; + float light_length = length(light_vec); - vec3 attenuation = vec3(omni_attenuation); + float normalized_distance = light_length / light_range; + if (normalized_distance < 1.0) { + + float omni_attenuation = pow(1.0 - normalized_distance, light_attenuation); + + light_att = vec3(omni_attenuation); + } else { + light_att = vec3(0.0); + } + L = normalize(light_vec); + +#endif + +#ifdef USE_SHADOW + { + highp vec4 splane = shadow_coord; + float shadow_len = length(splane.xyz); - if (light_has_shadow > 0.5) { - highp vec3 splane = (light_shadow_matrix * vec4(vertex, 1.0)).xyz; - float shadow_len = length(splane); + splane.xyz = normalize(splane.xyz); - splane = normalize(splane); + vec4 clamp_rect = light_clamp; - vec4 clamp_rect = light_clamp; + if (splane.z >= 0.0) { + splane.z += 1.0; + + clamp_rect.y += clamp_rect.w; + } else { + splane.z = 1.0 - splane.z; + } - if (splane.z >= 0.0) { - splane.z += 1.0; + splane.xy /= splane.z; + splane.xy = splane.xy * 0.5 + 0.5; + splane.z = shadow_len / light_range; - clamp_rect.y += clamp_rect.w; + splane.xy = clamp_rect.xy + splane.xy * clamp_rect.zw; + splane.w = 1.0; + + float shadow = sample_shadow(light_shadow_atlas, splane); + + light_att *= shadow; + } +#endif + +#endif //type omni + +#ifdef LIGHT_MODE_DIRECTIONAL + +#ifndef USE_VERTEX_LIGHTING + vec3 light_vec = -light_direction; + L = normalize(light_vec); +#endif + float depth_z = -vertex.z; + +#ifdef USE_SHADOW + +#ifdef USE_VERTEX_LIGHTING + //compute shadows in a mobile friendly way + +#ifdef LIGHT_USE_PSSM4 + //take advantage of prefetch + float shadow1 = sample_shadow(light_directional_shadow, shadow_coord); + float shadow2 = sample_shadow(light_directional_shadow, shadow_coord2); + float shadow3 = sample_shadow(light_directional_shadow, shadow_coord3); + float shadow4 = sample_shadow(light_directional_shadow, shadow_coord4); + + if (depth_z < light_split_offsets.w) { + float pssm_fade = 0.0; + float shadow_att = 1.0; +#ifdef LIGHT_USE_PSSM_BLEND + float shadow_att2 = 1.0; + float pssm_blend = 0.0; + bool use_blend = true; +#endif + if (depth_z < light_split_offsets.y) { + if (depth_z < light_split_offsets.x) { + shadow_att = shadow1; + +#ifdef LIGHT_USE_PSSM_BLEND + shadow_att2 = shadow2; + + pssm_blend = smoothstep(0.0, light_split_offsets.x, depth_z); +#endif } else { - splane.z = 1.0 - splane.z; - } + shadow_att = shadow2; - splane.xy /= splane.z; - splane.xy = splane.xy * 0.5 + 0.5; - splane.z = shadow_len / light_range; +#ifdef LIGHT_USE_PSSM_BLEND + shadow_att2 = shadow3; - splane.xy = clamp_rect.xy + splane.xy * clamp_rect.zw; + pssm_blend = smoothstep(light_split_offsets.x, light_split_offsets.y, depth_z); +#endif + } + } else { + if (depth_z < light_split_offsets.z) { - float shadow = sample_shadow(light_shadow_atlas, vec2(0.0), splane.xy, splane.z, clamp_rect); + shadow_att = shadow3; + +#if defined(LIGHT_USE_PSSM_BLEND) + shadow_att2 = shadow4; + pssm_blend = smoothstep(light_split_offsets.y, light_split_offsets.z, depth_z); +#endif - if (shadow > splane.z) { } else { - attenuation = vec3(0.0); + + shadow_att = shadow4; + pssm_fade = smoothstep(light_split_offsets.z, light_split_offsets.w, depth_z); + +#if defined(LIGHT_USE_PSSM_BLEND) + use_blend = false; +#endif } } +#if defined(LIGHT_USE_PSSM_BLEND) + if (use_blend) { + shadow_att = mix(shadow_att, shadow_att2, pssm_blend); + } +#endif + light_att *= shadow_att; + } + +#endif //LIGHT_USE_PSSM4 + +#ifdef LIGHT_USE_PSSM2 + + //take advantage of prefetch + float shadow1 = sample_shadow(light_directional_shadow, shadow_coord); + float shadow2 = sample_shadow(light_directional_shadow, shadow_coord2); + + if (depth_z < light_split_offsets.y) { + float shadow_att = 1.0; + float pssm_fade = 0.0; + +#ifdef LIGHT_USE_PSSM_BLEND + float shadow_att2 = 1.0; + float pssm_blend = 0.0; + bool use_blend = true; +#endif + if (depth_z < light_split_offsets.x) { + float pssm_fade = 0.0; + shadow_att = shadow1; + +#ifdef LIGHT_USE_PSSM_BLEND + shadow_att2 = shadow2; + pssm_blend = smoothstep(0.0, light_split_offsets.x, depth_z); +#endif + } else { + + shadow_att = shadow2; + pssm_fade = smoothstep(light_split_offsets.x, light_split_offsets.y, depth_z); +#ifdef LIGHT_USE_PSSM_BLEND + use_blend = false; +#endif + } +#ifdef LIGHT_USE_PSSM_BLEND + if (use_blend) { + shadow_att = mix(shadow_att, shadow_att2, pssm_blend); + } +#endif + light_att *= shadow_att; + } - light_compute(normal, - normalize(light_vec), - eye_position, - binormal, - tangent, - light_color.xyz * light_energy, - attenuation, - albedo, - transmission, - specular * light_specular, - roughness, - metallic, - rim, - rim_tint, - clearcoat, - clearcoat_gloss, - anisotropy, - diffuse_light, - specular_light); - - } else if (light_type == LIGHT_TYPE_DIRECTIONAL) { - - vec3 light_vec = -light_direction; - vec3 attenuation = vec3(1.0, 1.0, 1.0); - - float depth_z = -vertex.z; - - if (light_has_shadow > 0.5) { +#endif //LIGHT_USE_PSSM2 +#if !defined(LIGHT_USE_PSSM4) && !defined(LIGHT_USE_PSSM2) + + light_att *= sample_shadow(light_directional_shadow, shadow_coord); +#endif //orthogonal + +#else //fragment version of pssm + + { #ifdef LIGHT_USE_PSSM4 - if (depth_z < light_split_offsets.w) { + if (depth_z < light_split_offsets.w) { #elif defined(LIGHT_USE_PSSM2) - if (depth_z < light_split_offsets.y) { + if (depth_z < light_split_offsets.y) { #else - if (depth_z < light_split_offsets.x) { -#endif + if (depth_z < light_split_offsets.x) { +#endif //pssm2 - vec3 pssm_coord; + highp vec4 pssm_coord; float pssm_fade = 0.0; #ifdef LIGHT_USE_PSSM_BLEND float pssm_blend; - vec3 pssm_coord2; + highp vec4 pssm_coord2; bool use_blend = true; #endif #ifdef LIGHT_USE_PSSM4 + if (depth_z < light_split_offsets.y) { if (depth_z < light_split_offsets.x) { - highp vec4 splane = (light_shadow_matrix1 * vec4(vertex, 1.0)); - pssm_coord = splane.xyz / splane.w; + pssm_coord = shadow_coord; #ifdef LIGHT_USE_PSSM_BLEND - splane = (light_shadow_matrix2 * vec4(vertex, 1.0)); - pssm_coord2 = splane.xyz / splane.w; + pssm_coord2 = shadow_coord2; pssm_blend = smoothstep(0.0, light_split_offsets.x, depth_z); #endif } else { - highp vec4 splane = (light_shadow_matrix2 * vec4(vertex, 1.0)); - pssm_coord = splane.xyz / splane.w; + pssm_coord = shadow_coord2; #ifdef LIGHT_USE_PSSM_BLEND - splane = (light_shadow_matrix3 * vec4(vertex, 1.0)); - pssm_coord2 = splane.xyz / splane.w; + pssm_coord2 = shadow_coord3; pssm_blend = smoothstep(light_split_offsets.x, light_split_offsets.y, depth_z); #endif @@ -668,19 +1813,16 @@ FRAGMENT_SHADER_CODE } else { if (depth_z < light_split_offsets.z) { - highp vec4 splane = (light_shadow_matrix3 * vec4(vertex, 1.0)); - pssm_coord = splane.xyz / splane.w; + pssm_coord = shadow_coord3; #if defined(LIGHT_USE_PSSM_BLEND) - splane = (light_shadow_matrix4 * vec4(vertex, 1.0)); - pssm_coord2 = splane.xyz / splane.w; + pssm_coord2 = shadow_coord4; pssm_blend = smoothstep(light_split_offsets.y, light_split_offsets.z, depth_z); #endif } else { - highp vec4 splane = (light_shadow_matrix4 * vec4(vertex, 1.0)); - pssm_coord = splane.xyz / splane.w; + pssm_coord = shadow_coord4; pssm_fade = smoothstep(light_split_offsets.z, light_split_offsets.w, depth_z); #if defined(LIGHT_USE_PSSM_BLEND) @@ -694,17 +1836,15 @@ FRAGMENT_SHADER_CODE #ifdef LIGHT_USE_PSSM2 if (depth_z < light_split_offsets.x) { - highp vec4 splane = (light_shadow_matrix1 * vec4(vertex, 1.0)); - pssm_coord = splane.xyz / splane.w; + pssm_coord = shadow_coord; #ifdef LIGHT_USE_PSSM_BLEND - splane = (light_shadow_matrix2 * vec4(vertex, 1.0)); - pssm_coord2 = splane.xyz / splane.w; + pssm_coord2 = shadow_coord2; pssm_blend = smoothstep(0.0, light_split_offsets.x, depth_z); #endif } else { - highp vec4 splane = (light_shadow_matrix2 * vec4(vertex, 1.0)); - pssm_coord = splane.xyz / splane.w; + + pssm_coord = shadow_coord2; pssm_fade = smoothstep(light_split_offsets.x, light_split_offsets.y, depth_z); #ifdef LIGHT_USE_PSSM_BLEND use_blend = false; @@ -715,128 +1855,114 @@ FRAGMENT_SHADER_CODE #if !defined(LIGHT_USE_PSSM4) && !defined(LIGHT_USE_PSSM2) { - highp vec4 splane = (light_shadow_matrix1 * vec4(vertex, 1.0)); - pssm_coord = splane.xyz / splane.w; + pssm_coord = shadow_coord; } #endif - float shadow = sample_shadow(light_shadow_atlas, vec2(0.0), pssm_coord.xy, pssm_coord.z, light_clamp); + float shadow = sample_shadow(light_directional_shadow, pssm_coord); #ifdef LIGHT_USE_PSSM_BLEND if (use_blend) { - shadow = mix(shadow, sample_shadow(light_shadow_atlas, vec2(0.0), pssm_coord2.xy, pssm_coord2.z, light_clamp), pssm_blend); + shadow = mix(shadow, sample_shadow(light_directional_shadow, pssm_coord2), pssm_blend); } #endif - attenuation *= shadow; - - + light_att *= shadow; } + } +#endif //use vertex lighting - } +#endif //use shadow - light_compute(normal, - normalize(light_vec), - eye_position, - binormal, - tangent, - light_color.xyz * light_energy, - attenuation, - albedo, - transmission, - specular * light_specular, - roughness, - metallic, - rim, - rim_tint, - clearcoat, - clearcoat_gloss, - anisotropy, - diffuse_light, - specular_light); - } else if (light_type == LIGHT_TYPE_SPOT) { - - vec3 light_att = vec3(1.0); - - if (light_has_shadow > 0.5) { - highp vec4 splane = (light_shadow_matrix * vec4(vertex, 1.0)); - splane.xyz /= splane.w; - - float shadow = sample_shadow(light_shadow_atlas, vec2(0.0), splane.xy, splane.z, light_clamp); - - if (shadow > splane.z) { - } else { - light_att = vec3(0.0); - } +#endif +#ifdef LIGHT_MODE_SPOT - } + light_att = vec3(1.0); - vec3 light_rel_vec = light_position - vertex; - float light_length = length(light_rel_vec); - float normalized_distance = light_length / light_range; +#ifndef USE_VERTEX_LIGHTING - float spot_attenuation = pow(1.0 - normalized_distance, light_attenuation.w); + vec3 light_rel_vec = light_position - vertex; + float light_length = length(light_rel_vec); + float normalized_distance = light_length / light_range; + + if (normalized_distance < 1.0) { + float spot_attenuation = pow(1.0 - normalized_distance, light_attenuation); vec3 spot_dir = light_direction; float spot_cutoff = light_spot_angle; + float angle = dot(-normalize(light_rel_vec), spot_dir); - float scos = max(dot(-normalize(light_rel_vec), spot_dir), spot_cutoff); - float spot_rim = max(0.0001, (1.0 - scos) / (1.0 - spot_cutoff)); - - spot_attenuation *= 1.0 - pow(spot_rim, light_spot_attenuation); - - light_att *= vec3(spot_attenuation); - - light_compute(normal, - normalize(light_rel_vec), - eye_position, - binormal, - tangent, - light_color.xyz * light_energy, - light_att, - albedo, - transmission, - specular * light_specular, - roughness, - metallic, - rim, - rim_tint, - clearcoat, - clearcoat_gloss, - anisotropy, - diffuse_light, - specular_light); - } - - gl_FragColor = vec4(ambient_light + diffuse_light + specular_light, alpha); -#else - -#ifdef RENDER_DEPTH - -#else + if (angle > spot_cutoff) { + float scos = max(angle, spot_cutoff); + float spot_rim = max(0.0001, (1.0 - scos) / (1.0 - spot_cutoff)); + spot_attenuation *= 1.0 - pow(spot_rim, light_spot_attenuation); -#ifdef USE_RADIANCE_MAP + light_att = vec3(spot_attenuation); + } else { + light_att = vec3(0.0); + } + } else { + light_att = vec3(0.0); + } + L = normalize(light_rel_vec); - vec3 ref_vec = reflect(-eye_position, N); - ref_vec = normalize((radiance_inverse_xform * vec4(ref_vec, 0.0)).xyz); +#endif - ref_vec.z *= -1.0; +#ifdef USE_SHADOW + { + highp vec4 splane = shadow_coord; + splane.xyz /= splane.w; - env_reflection_light = textureCubeLod(radiance_map, ref_vec, roughness * RADIANCE_MAX_LOD).xyz * bg_energy; + float shadow = sample_shadow(light_shadow_atlas, splane); + light_att *= shadow; + } +#endif - { - vec3 ambient_dir = normalize((radiance_inverse_xform * vec4(normal, 0.0)).xyz); - vec3 env_ambient = textureCubeLod(radiance_map, ambient_dir, RADIANCE_MAX_LOD).xyz * bg_energy; +#endif // LIGHT_MODE_SPOT - ambient_light = mix(ambient_color.rgb, env_ambient, ambient_sky_contribution); +#ifdef USE_VERTEX_LIGHTING + //vertex lighting - } + specular_light += specular_interp * specular_blob_intensity * light_att; + diffuse_light += diffuse_interp * albedo * light_att; - ambient_light *= ambient_energy; +#else + //fragment lighting + light_compute( + normal, + L, + eye_position, + binormal, + tangent, + light_color.xyz, + light_att, + albedo, + transmission, + specular_blob_intensity * light_specular, + roughness, + metallic, + specular, + rim, + rim_tint, + clearcoat, + clearcoat_gloss, + anisotropy, + diffuse_light, + specular_light); + +#endif //vertex lighting + +#endif //USE_LIGHTING + //compute and merge + +#ifndef RENDER_DEPTH + +#ifdef SHADELESS - specular_light += env_reflection_light; + gl_FragColor = vec4(albedo, alpha); +#else ambient_light *= albedo; @@ -852,31 +1978,79 @@ FRAGMENT_SHADER_CODE // environment BRDF approximation - // TODO shadeless { + +#if defined(DIFFUSE_TOON) + //simplify for toon, as + specular_light *= specular * metallic * albedo * 2.0; +#else + //TODO: this curve is not really designed for gammaspace, should be adjusted const vec4 c0 = vec4(-1.0, -0.0275, -0.572, 0.022); - const vec4 c1 = vec4( 1.0, 0.0425, 1.04, -0.04); + const vec4 c1 = vec4(1.0, 0.0425, 1.04, -0.04); vec4 r = roughness * c0 + c1; - float ndotv = clamp(dot(normal,eye_position),0.0,1.0); - float a004 = min( r.x * r.x, exp2( -9.28 * ndotv ) ) * r.x + r.y; - vec2 AB = vec2( -1.04, 1.04 ) * a004 + r.zw; + float ndotv = clamp(dot(normal, eye_position), 0.0, 1.0); + float a004 = min(r.x * r.x, exp2(-9.28 * ndotv)) * r.x + r.y; + vec2 env = vec2(-1.04, 1.04) * a004 + r.zw; - vec3 specular_color = metallic_to_specular_color(metallic, specular, albedo); - specular_light *= AB.x * specular_color + AB.y; + vec3 f0 = F0(metallic, specular, albedo); + specular_light *= env.x * f0 + env.y; +#endif } - gl_FragColor = vec4(ambient_light + diffuse_light + specular_light, alpha); + + //add emission if in base pass +#ifdef BASE_PASS + gl_FragColor.rgb += emission; +#endif // gl_FragColor = vec4(normal, 1.0); +#endif //unshaded + +//apply fog +#if defined(FOG_DEPTH_ENABLED) || defined(FOG_HEIGHT_ENABLED) +#if defined(USE_VERTEX_LIGHTING) + + gl_FragColor.rgb = mix(gl_FragColor.rgb, fog_interp.rgb, fog_interp.a); +#else //pixel based fog + float fog_amount = 0.0; + +#ifdef LIGHT_MODE_DIRECTIONAL + + vec3 fog_color = mix(fog_color_base.rgb, fog_sun_color_amount.rgb, fog_sun_color_amount.a * pow(max(dot(eye_position, light_direction), 0.0), 8.0)); #else - gl_FragColor = vec4(albedo, alpha); + vec3 fog_color = fog_color_base.rgb; +#endif + +#ifdef FOG_DEPTH_ENABLED + + { + + float fog_z = smoothstep(fog_depth_begin, fog_max_distance, length(vertex)); + + fog_amount = pow(fog_z, fog_depth_curve) * fog_color_base.a; + + if (fog_transmit_enabled) { + vec3 total_light = gl_FragColor.rgb; + float transmit = pow(fog_z, fog_transmit_curve); + fog_color = mix(max(total_light, fog_color), fog_color, transmit); + } + } +#endif + +#ifdef FOG_HEIGHT_ENABLED + { + float y = (camera_matrix * vec4(vertex, 1.0)).y; + fog_amount = max(fog_amount, pow(smoothstep(fog_height_min, fog_height_max, y), fog_height_curve)); + } #endif -#endif // RENDER_DEPTH + gl_FragColor.rgb = mix(gl_FragColor.rgb, fog_color, fog_amount); -#endif // lighting +#endif //use vertex lit +#endif // defined(FOG_DEPTH_ENABLED) || defined(FOG_HEIGHT_ENABLED) +#endif // not RENDER_DEPTH } diff --git a/drivers/gles2/shaders/screen_space_reflection.glsl b/drivers/gles2/shaders/screen_space_reflection.glsl index b2e6f7a736..a11da10b61 100644 --- a/drivers/gles2/shaders/screen_space_reflection.glsl +++ b/drivers/gles2/shaders/screen_space_reflection.glsl @@ -1,8 +1,9 @@ +/* clang-format off */ [vertex] - -layout(location=0) in highp vec4 vertex_attrib; -layout(location=4) in vec2 uv_in; +layout(location = 0) in highp vec4 vertex_attrib; +/* clang-format on */ +layout(location = 4) in vec2 uv_in; out vec2 uv_interp; out vec2 pos_interp; @@ -11,13 +12,14 @@ void main() { uv_interp = uv_in; gl_Position = vertex_attrib; - pos_interp.xy=gl_Position.xy; + pos_interp.xy = gl_Position.xy; } +/* clang-format off */ [fragment] - in vec2 uv_interp; +/* clang-format on */ in vec2 pos_interp; uniform sampler2D source_diffuse; //texunit:0 @@ -40,81 +42,70 @@ uniform float depth_tolerance; uniform float distance_fade; uniform float curve_fade_in; - layout(location = 0) out vec4 frag_color; - -vec2 view_to_screen(vec3 view_pos,out float w) { - vec4 projected = projection * vec4(view_pos, 1.0); - projected.xyz /= projected.w; - projected.xy = projected.xy * 0.5 + 0.5; - w=projected.w; - return projected.xy; +vec2 view_to_screen(vec3 view_pos, out float w) { + vec4 projected = projection * vec4(view_pos, 1.0); + projected.xyz /= projected.w; + projected.xy = projected.xy * 0.5 + 0.5; + w = projected.w; + return projected.xy; } - - #define M_PI 3.14159265359 void main() { - - //// - - vec4 diffuse = texture( source_diffuse, uv_interp ); - vec4 normal_roughness = texture( source_normal_roughness, uv_interp); + vec4 diffuse = texture(source_diffuse, uv_interp); + vec4 normal_roughness = texture(source_normal_roughness, uv_interp); vec3 normal; - normal = normal_roughness.xyz*2.0-1.0; + normal = normal_roughness.xyz * 2.0 - 1.0; float roughness = normal_roughness.w; - float depth_tex = texture(source_depth,uv_interp).r; + float depth_tex = texture(source_depth, uv_interp).r; - vec4 world_pos = inverse_projection * vec4( uv_interp*2.0-1.0, depth_tex*2.0-1.0, 1.0 ); - vec3 vertex = world_pos.xyz/world_pos.w; + vec4 world_pos = inverse_projection * vec4(uv_interp * 2.0 - 1.0, depth_tex * 2.0 - 1.0, 1.0); + vec3 vertex = world_pos.xyz / world_pos.w; vec3 view_dir = normalize(vertex); vec3 ray_dir = normalize(reflect(view_dir, normal)); - if (dot(ray_dir,normal)<0.001) { - frag_color=vec4(0.0); + if (dot(ray_dir, normal) < 0.001) { + frag_color = vec4(0.0); return; } //ray_dir = normalize(view_dir - normal * dot(normal,view_dir) * 2.0); //ray_dir = normalize(vec3(1,1,-1)); - //////////////// - //make ray length and clip it against the near plane (don't want to trace beyond visible) float ray_len = (vertex.z + ray_dir.z * camera_z_far) > -camera_z_near ? (-camera_z_near - vertex.z) / ray_dir.z : camera_z_far; - vec3 ray_end = vertex + ray_dir*ray_len; + vec3 ray_end = vertex + ray_dir * ray_len; float w_begin; - vec2 vp_line_begin = view_to_screen(vertex,w_begin); + vec2 vp_line_begin = view_to_screen(vertex, w_begin); float w_end; - vec2 vp_line_end = view_to_screen( ray_end, w_end); - vec2 vp_line_dir = vp_line_end-vp_line_begin; + vec2 vp_line_end = view_to_screen(ray_end, w_end); + vec2 vp_line_dir = vp_line_end - vp_line_begin; //we need to interpolate w along the ray, to generate perspective correct reflections - w_begin = 1.0/w_begin; - w_end = 1.0/w_end; + w_begin = 1.0 / w_begin; + w_end = 1.0 / w_end; + float z_begin = vertex.z * w_begin; + float z_end = ray_end.z * w_end; - float z_begin = vertex.z*w_begin; - float z_end = ray_end.z*w_end; - - vec2 line_begin = vp_line_begin/pixel_size; - vec2 line_dir = vp_line_dir/pixel_size; + vec2 line_begin = vp_line_begin / pixel_size; + vec2 line_dir = vp_line_dir / pixel_size; float z_dir = z_end - z_begin; float w_dir = w_end - w_begin; - // clip the line to the viewport edges float scale_max_x = min(1.0, 0.99 * (1.0 - vp_line_begin.x) / max(1e-5, vp_line_dir.x)); @@ -124,121 +115,109 @@ void main() { float line_clip = min(scale_max_x, scale_max_y) * min(scale_min_x, scale_min_y); line_dir *= line_clip; z_dir *= line_clip; - w_dir *=line_clip; + w_dir *= line_clip; //clip z and w advance to line advance vec2 line_advance = normalize(line_dir); //down to pixel - float step_size = length(line_advance)/length(line_dir); - float z_advance = z_dir*step_size; // adapt z advance to line advance - float w_advance = w_dir*step_size; // adapt w advance to line advance + float step_size = length(line_advance) / length(line_dir); + float z_advance = z_dir * step_size; // adapt z advance to line advance + float w_advance = w_dir * step_size; // adapt w advance to line advance //make line advance faster if direction is closer to pixel edges (this avoids sampling the same pixel twice) - float advance_angle_adj = 1.0/max(abs(line_advance.x),abs(line_advance.y)); - line_advance*=advance_angle_adj; // adapt z advance to line advance - z_advance*=advance_angle_adj; - w_advance*=advance_angle_adj; + float advance_angle_adj = 1.0 / max(abs(line_advance.x), abs(line_advance.y)); + line_advance *= advance_angle_adj; // adapt z advance to line advance + z_advance *= advance_angle_adj; + w_advance *= advance_angle_adj; vec2 pos = line_begin; float z = z_begin; float w = w_begin; - float z_from=z/w; - float z_to=z_from; + float z_from = z / w; + float z_to = z_from; float depth; - vec2 prev_pos=pos; + vec2 prev_pos = pos; - bool found=false; + bool found = false; - float steps_taken=0.0; + float steps_taken = 0.0; - for(int i=0;i<num_steps;i++) { + for (int i = 0; i < num_steps; i++) { - pos+=line_advance; - z+=z_advance; - w+=w_advance; + pos += line_advance; + z += z_advance; + w += w_advance; //convert to linear depth - depth = texture(source_depth, pos*pixel_size).r * 2.0 - 1.0; + depth = texture(source_depth, pos * pixel_size).r * 2.0 - 1.0; #ifdef USE_ORTHOGONAL_PROJECTION - depth = ((depth + (camera_z_far + camera_z_near)/(camera_z_far - camera_z_near)) * (camera_z_far - camera_z_near))/2.0; + depth = ((depth + (camera_z_far + camera_z_near) / (camera_z_far - camera_z_near)) * (camera_z_far - camera_z_near)) / 2.0; #else depth = 2.0 * camera_z_near * camera_z_far / (camera_z_far + camera_z_near - depth * (camera_z_far - camera_z_near)); #endif - depth=-depth; + depth = -depth; z_from = z_to; - z_to = z/w; + z_to = z / w; - if (depth>z_to) { + if (depth > z_to) { //if depth was surpassed - if (depth<=max(z_to,z_from)+depth_tolerance) { + if (depth <= max(z_to, z_from) + depth_tolerance) { //check the depth tolerance - found=true; + found = true; } break; } - steps_taken+=1.0; - prev_pos=pos; + steps_taken += 1.0; + prev_pos = pos; } - - - if (found) { - float margin_blend=1.0; - + float margin_blend = 1.0; - vec2 margin = vec2((viewport_size.x+viewport_size.y)*0.5*0.05); //make a uniform margin - if (any(bvec4(lessThan(pos,-margin),greaterThan(pos,viewport_size+margin)))) { + vec2 margin = vec2((viewport_size.x + viewport_size.y) * 0.5 * 0.05); //make a uniform margin + if (any(bvec4(lessThan(pos, -margin), greaterThan(pos, viewport_size + margin)))) { //clip outside screen + margin - frag_color=vec4(0.0); + frag_color = vec4(0.0); return; } { //blend fading out towards external margin - vec2 margin_grad = mix(pos-viewport_size,-pos,lessThan(pos,vec2(0.0))); - margin_blend = 1.0-smoothstep(0.0,margin.x,max(margin_grad.x,margin_grad.y)); + vec2 margin_grad = mix(pos - viewport_size, -pos, lessThan(pos, vec2(0.0))); + margin_blend = 1.0 - smoothstep(0.0, margin.x, max(margin_grad.x, margin_grad.y)); //margin_blend=1.0; - } vec2 final_pos; float grad; - grad=steps_taken/float(num_steps); - float initial_fade = curve_fade_in==0.0 ? 1.0 : pow(clamp(grad,0.0,1.0),curve_fade_in); - float fade = pow(clamp(1.0-grad,0.0,1.0),distance_fade)*initial_fade; - final_pos=pos; - - - - - - + grad = steps_taken / float(num_steps); + float initial_fade = curve_fade_in == 0.0 ? 1.0 : pow(clamp(grad, 0.0, 1.0), curve_fade_in); + float fade = pow(clamp(1.0 - grad, 0.0, 1.0), distance_fade) * initial_fade; + final_pos = pos; #ifdef REFLECT_ROUGHNESS - vec4 final_color; //if roughness is enabled, do screen space cone tracing if (roughness > 0.001) { /////////////////////////////////////////////////////////////////////////////////////// //use a blurred version (in consecutive mipmaps) of the screen to simulate roughness - float gloss = 1.0-roughness; + float gloss = 1.0 - roughness; float cone_angle = roughness * M_PI * 0.5; vec2 cone_dir = final_pos - line_begin; float cone_len = length(cone_dir); cone_dir = normalize(cone_dir); //will be used normalized from now on float max_mipmap = filter_mipmap_levels - 1.0; - float gloss_mult=gloss; + float gloss_mult = gloss; - float rem_alpha=1.0; + float rem_alpha = 1.0; final_color = vec4(0.0); - for(int i=0;i<7;i++) { + for (int i = 0; i < 7; i++) { float op_len = 2.0 * tan(cone_angle) * cone_len; //opposite side of iso triangle float radius; @@ -258,30 +237,30 @@ void main() { } //find the place where screen must be sampled - vec2 sample_pos = ( line_begin + cone_dir * (cone_len - radius) ) * pixel_size; + vec2 sample_pos = (line_begin + cone_dir * (cone_len - radius)) * pixel_size; //radius is in pixels, so it's natural that log2(radius) maps to the right mipmap for the amount of pixels - float mipmap = clamp( log2( radius ), 0.0, max_mipmap ); + float mipmap = clamp(log2(radius), 0.0, max_mipmap); //mipmap = max(mipmap-1.0,0.0); //do sampling vec4 sample_color; { - sample_color = textureLod(source_diffuse,sample_pos,mipmap); + sample_color = textureLod(source_diffuse, sample_pos, mipmap); } //multiply by gloss - sample_color.rgb*=gloss_mult; - sample_color.a=gloss_mult; + sample_color.rgb *= gloss_mult; + sample_color.a = gloss_mult; rem_alpha -= sample_color.a; - if(rem_alpha < 0.0) { + if (rem_alpha < 0.0) { sample_color.rgb *= (1.0 - abs(rem_alpha)); } - final_color+=sample_color; + final_color += sample_color; - if (final_color.a>=0.95) { + if (final_color.a >= 0.95) { // This code of accumulating gloss and aborting on near one // makes sense when you think of cone tracing. // Think of it as if roughness was 0, then we could abort on the first @@ -290,29 +269,21 @@ void main() { break; } - cone_len-=radius*2.0; //go to next (smaller) circle. - - gloss_mult*=gloss; - + cone_len -= radius * 2.0; //go to next (smaller) circle. + gloss_mult *= gloss; } } else { - final_color = textureLod(source_diffuse,final_pos*pixel_size,0.0); + final_color = textureLod(source_diffuse, final_pos * pixel_size, 0.0); } - frag_color = vec4(final_color.rgb,fade*margin_blend); + frag_color = vec4(final_color.rgb, fade * margin_blend); #else - frag_color = vec4(textureLod(source_diffuse,final_pos*pixel_size,0.0).rgb,fade*margin_blend); + frag_color = vec4(textureLod(source_diffuse, final_pos * pixel_size, 0.0).rgb, fade * margin_blend); #endif - - } else { - frag_color = vec4(0.0,0.0,0.0,0.0); + frag_color = vec4(0.0, 0.0, 0.0, 0.0); } - - - } - diff --git a/drivers/gles2/shaders/ssao.glsl b/drivers/gles2/shaders/ssao.glsl index 219f0957e0..82eea8f274 100644 --- a/drivers/gles2/shaders/ssao.glsl +++ b/drivers/gles2/shaders/ssao.glsl @@ -1,14 +1,16 @@ +/* clang-format off */ [vertex] - -layout(location=0) in highp vec4 vertex_attrib; +layout(location = 0) in highp vec4 vertex_attrib; +/* clang-format on */ void main() { gl_Position = vertex_attrib; - gl_Position.z=1.0; + gl_Position.z = 1.0; } +/* clang-format off */ [fragment] #define TWO_PI 6.283185307179586476925286766559 @@ -43,19 +45,21 @@ void main() { // This is the number of turns around the circle that the spiral pattern makes. This should be prime to prevent // taps from lining up. This particular choice was tuned for NUM_SAMPLES == 9 -const int ROTATIONS[] = int[]( 1, 1, 2, 3, 2, 5, 2, 3, 2, -3, 3, 5, 5, 3, 4, 7, 5, 5, 7, -9, 8, 5, 5, 7, 7, 7, 8, 5, 8, -11, 12, 7, 10, 13, 8, 11, 8, 7, 14, -11, 11, 13, 12, 13, 19, 17, 13, 11, 18, -19, 11, 11, 14, 17, 21, 15, 16, 17, 18, -13, 17, 11, 17, 19, 18, 25, 18, 19, 19, -29, 21, 19, 27, 31, 29, 21, 18, 17, 29, -31, 31, 23, 18, 25, 26, 25, 23, 19, 34, -19, 27, 21, 25, 39, 29, 17, 21, 27 ); +const int ROTATIONS[] = int[]( + 1, 1, 2, 3, 2, 5, 2, 3, 2, + 3, 3, 5, 5, 3, 4, 7, 5, 5, 7, + 9, 8, 5, 5, 7, 7, 7, 8, 5, 8, + 11, 12, 7, 10, 13, 8, 11, 8, 7, 14, + 11, 11, 13, 12, 13, 19, 17, 13, 11, 18, + 19, 11, 11, 14, 17, 21, 15, 16, 17, 18, + 13, 17, 11, 17, 19, 18, 25, 18, 19, 19, + 29, 21, 19, 27, 31, 29, 21, 18, 17, 29, + 31, 31, 23, 18, 25, 26, 25, 23, 19, 34, + 19, 27, 21, 25, 39, 29, 17, 21, 27); +/* clang-format on */ //#define NUM_SPIRAL_TURNS (7) -const int NUM_SPIRAL_TURNS = ROTATIONS[NUM_SAMPLES-1]; +const int NUM_SPIRAL_TURNS = ROTATIONS[NUM_SAMPLES - 1]; uniform sampler2D source_depth; //texunit:0 uniform highp usampler2D source_depth_mipmaps; //texunit:1 @@ -90,44 +94,41 @@ vec3 reconstructCSPosition(vec2 S, float z) { } vec3 getPosition(ivec2 ssP) { - vec3 P; - P.z = texelFetch(source_depth, ssP, 0).r; + vec3 P; + P.z = texelFetch(source_depth, ssP, 0).r; - P.z = P.z * 2.0 - 1.0; + P.z = P.z * 2.0 - 1.0; #ifdef USE_ORTHOGONAL_PROJECTION - P.z = ((P.z + (camera_z_far + camera_z_near)/(camera_z_far - camera_z_near)) * (camera_z_far - camera_z_near))/2.0; + P.z = ((P.z + (camera_z_far + camera_z_near) / (camera_z_far - camera_z_near)) * (camera_z_far - camera_z_near)) / 2.0; #else - P.z = 2.0 * camera_z_near * camera_z_far / (camera_z_far + camera_z_near - P.z * (camera_z_far - camera_z_near)); + P.z = 2.0 * camera_z_near * camera_z_far / (camera_z_far + camera_z_near - P.z * (camera_z_far - camera_z_near)); #endif - P.z = -P.z; + P.z = -P.z; - // Offset to pixel center - P = reconstructCSPosition(vec2(ssP) + vec2(0.5), P.z); - return P; + // Offset to pixel center + P = reconstructCSPosition(vec2(ssP) + vec2(0.5), P.z); + return P; } /** Reconstructs screen-space unit normal from screen-space position */ vec3 reconstructCSFaceNormal(vec3 C) { - return normalize(cross(dFdy(C), dFdx(C))); + return normalize(cross(dFdy(C), dFdx(C))); } - - /** Returns a unit vector and a screen-space radius for the tap on a unit disk (the caller should scale by the actual disk radius) */ -vec2 tapLocation(int sampleNumber, float spinAngle, out float ssR){ - // Radius relative to ssR - float alpha = (float(sampleNumber) + 0.5) * (1.0 / float(NUM_SAMPLES)); - float angle = alpha * (float(NUM_SPIRAL_TURNS) * 6.28) + spinAngle; +vec2 tapLocation(int sampleNumber, float spinAngle, out float ssR) { + // Radius relative to ssR + float alpha = (float(sampleNumber) + 0.5) * (1.0 / float(NUM_SAMPLES)); + float angle = alpha * (float(NUM_SPIRAL_TURNS) * 6.28) + spinAngle; - ssR = alpha; - return vec2(cos(angle), sin(angle)); + ssR = alpha; + return vec2(cos(angle), sin(angle)); } - /** Read the camera-space position of the point at screen-space pixel ssP + unitOffset * ssR. Assumes length(unitOffset) == 1 */ vec3 getOffsetPosition(ivec2 ssC, vec2 unitOffset, float ssR) { - // Derivation: - // mipLevel = floor(log(ssR / MAX_OFFSET)); + // Derivation: + // mipLevel = floor(log(ssR / MAX_OFFSET)); int mipLevel = clamp(int(floor(log2(ssR))) - LOG_MAX_OFFSET, 0, MAX_MIP_LEVEL); ivec2 ssP = ivec2(ssR * unitOffset) + ssC; @@ -138,13 +139,12 @@ vec3 getOffsetPosition(ivec2 ssC, vec2 unitOffset, float ssR) { // Manually clamp to the texture size because texelFetch bypasses the texture unit ivec2 mipP = clamp(ssP >> mipLevel, ivec2(0), (screen_size >> mipLevel) - ivec2(1)); - if (mipLevel < 1) { //read from depth buffer P.z = texelFetch(source_depth, mipP, 0).r; P.z = P.z * 2.0 - 1.0; #ifdef USE_ORTHOGONAL_PROJECTION - P.z = ((P.z + (camera_z_far + camera_z_near)/(camera_z_far - camera_z_near)) * (camera_z_far - camera_z_near))/2.0; + P.z = ((P.z + (camera_z_far + camera_z_near) / (camera_z_far - camera_z_near)) * (camera_z_far - camera_z_near)) / 2.0; #else P.z = 2.0 * camera_z_near * camera_z_far / (camera_z_far + camera_z_near - P.z * (camera_z_far - camera_z_near)); @@ -153,78 +153,74 @@ vec3 getOffsetPosition(ivec2 ssC, vec2 unitOffset, float ssR) { } else { //read from mipmaps - uint d = texelFetch(source_depth_mipmaps, mipP, mipLevel-1).r; - P.z = -(float(d)/65535.0)*camera_z_far; + uint d = texelFetch(source_depth_mipmaps, mipP, mipLevel - 1).r; + P.z = -(float(d) / 65535.0) * camera_z_far; } - // Offset to pixel center P = reconstructCSPosition(vec2(ssP) + vec2(0.5), P.z); return P; } - - /** Compute the occlusion due to sample with index \a i about the pixel at \a ssC that corresponds - to camera-space point \a C with unit normal \a n_C, using maximum screen-space sampling radius \a ssDiskRadius + to camera-space point \a C with unit normal \a n_C, using maximum screen-space sampling radius \a ssDiskRadius - Note that units of H() in the HPG12 paper are meters, not - unitless. The whole falloff/sampling function is therefore - unitless. In this implementation, we factor out (9 / radius). + Note that units of H() in the HPG12 paper are meters, not + unitless. The whole falloff/sampling function is therefore + unitless. In this implementation, we factor out (9 / radius). - Four versions of the falloff function are implemented below + Four versions of the falloff function are implemented below */ -float sampleAO(in ivec2 ssC, in vec3 C, in vec3 n_C, in float ssDiskRadius,in float p_radius, in int tapIndex, in float randomPatternRotationAngle) { - // Offset on the unit disk, spun for this pixel - float ssR; - vec2 unitOffset = tapLocation(tapIndex, randomPatternRotationAngle, ssR); - ssR *= ssDiskRadius; +float sampleAO(in ivec2 ssC, in vec3 C, in vec3 n_C, in float ssDiskRadius, in float p_radius, in int tapIndex, in float randomPatternRotationAngle) { + // Offset on the unit disk, spun for this pixel + float ssR; + vec2 unitOffset = tapLocation(tapIndex, randomPatternRotationAngle, ssR); + ssR *= ssDiskRadius; - // The occluding point in camera space - vec3 Q = getOffsetPosition(ssC, unitOffset, ssR); + // The occluding point in camera space + vec3 Q = getOffsetPosition(ssC, unitOffset, ssR); - vec3 v = Q - C; + vec3 v = Q - C; - float vv = dot(v, v); - float vn = dot(v, n_C); + float vv = dot(v, v); + float vn = dot(v, n_C); - const float epsilon = 0.01; - float radius2 = p_radius*p_radius; + const float epsilon = 0.01; + float radius2 = p_radius * p_radius; - // A: From the HPG12 paper - // Note large epsilon to avoid overdarkening within cracks - //return float(vv < radius2) * max((vn - bias) / (epsilon + vv), 0.0) * radius2 * 0.6; + // A: From the HPG12 paper + // Note large epsilon to avoid overdarkening within cracks + //return float(vv < radius2) * max((vn - bias) / (epsilon + vv), 0.0) * radius2 * 0.6; - // B: Smoother transition to zero (lowers contrast, smoothing out corners). [Recommended] - float f=max(radius2 - vv, 0.0); - return f * f * f * max((vn - bias) / (epsilon + vv), 0.0); + // B: Smoother transition to zero (lowers contrast, smoothing out corners). [Recommended] + float f = max(radius2 - vv, 0.0); + return f * f * f * max((vn - bias) / (epsilon + vv), 0.0); - // C: Medium contrast (which looks better at high radii), no division. Note that the - // contribution still falls off with radius^2, but we've adjusted the rate in a way that is - // more computationally efficient and happens to be aesthetically pleasing. - // return 4.0 * max(1.0 - vv * invRadius2, 0.0) * max(vn - bias, 0.0); + // C: Medium contrast (which looks better at high radii), no division. Note that the + // contribution still falls off with radius^2, but we've adjusted the rate in a way that is + // more computationally efficient and happens to be aesthetically pleasing. + // return 4.0 * max(1.0 - vv * invRadius2, 0.0) * max(vn - bias, 0.0); - // D: Low contrast, no division operation - // return 2.0 * float(vv < radius * radius) * max(vn - bias, 0.0); + // D: Low contrast, no division operation + // return 2.0 * float(vv < radius * radius) * max(vn - bias, 0.0); } - - void main() { - // Pixel being shaded ivec2 ssC = ivec2(gl_FragCoord.xy); // World space point being shaded vec3 C = getPosition(ssC); -/* if (C.z <= -camera_z_far*0.999) { - // We're on the skybox - visibility=1.0; - return; - }*/ + /* + if (C.z <= -camera_z_far*0.999) { + // We're on the skybox + visibility=1.0; + return; + } + */ //visibility=-C.z/camera_z_far; //return; @@ -251,7 +247,7 @@ void main() { #endif float sum = 0.0; for (int i = 0; i < NUM_SAMPLES; ++i) { - sum += sampleAO(ssC, C, n_C, ssDiskRadius, radius,i, randomPatternRotationAngle); + sum += sampleAO(ssC, C, n_C, ssDiskRadius, radius, i, randomPatternRotationAngle); } float A = max(0.0, 1.0 - sum * intensity_div_r6 * (5.0 / float(NUM_SAMPLES))); @@ -271,10 +267,10 @@ void main() { sum = 0.0; for (int i = 0; i < NUM_SAMPLES; ++i) { - sum += sampleAO(ssC, C, n_C, ssDiskRadius,radius2, i, randomPatternRotationAngle); + sum += sampleAO(ssC, C, n_C, ssDiskRadius, radius2, i, randomPatternRotationAngle); } - A= min(A,max(0.0, 1.0 - sum * intensity_div_r62 * (5.0 / float(NUM_SAMPLES)))); + A = min(A, max(0.0, 1.0 - sum * intensity_div_r62 * (5.0 / float(NUM_SAMPLES)))); #endif // Bilateral box-filter over a quad for free, respecting depth edges // (the difference that this makes is subtle) @@ -286,8 +282,4 @@ void main() { } visibility = A; - } - - - diff --git a/drivers/gles2/shaders/ssao_blur.glsl b/drivers/gles2/shaders/ssao_blur.glsl index 472dc21acf..e4133ad534 100644 --- a/drivers/gles2/shaders/ssao_blur.glsl +++ b/drivers/gles2/shaders/ssao_blur.glsl @@ -1,26 +1,25 @@ +/* clang-format off */ [vertex] - -layout(location=0) in highp vec4 vertex_attrib; - +layout(location = 0) in highp vec4 vertex_attrib; +/* clang-format on */ void main() { gl_Position = vertex_attrib; - gl_Position.z=1.0; + gl_Position.z = 1.0; } +/* clang-format off */ [fragment] - uniform sampler2D source_ssao; //texunit:0 +/* clang-format on */ uniform sampler2D source_depth; //texunit:1 uniform sampler2D source_normal; //texunit:3 - layout(location = 0) out float visibility; - ////////////////////////////////////////////////////////////////////////////////////////////// // Tunable Parameters: @@ -28,32 +27,30 @@ layout(location = 0) out float visibility; uniform float edge_sharpness; /** Step in 2-pixel intervals since we already blurred against neighbors in the - first AO pass. This constant can be increased while R decreases to improve - performance at the expense of some dithering artifacts. + first AO pass. This constant can be increased while R decreases to improve + performance at the expense of some dithering artifacts. - Morgan found that a scale of 3 left a 1-pixel checkerboard grid that was - unobjectionable after shading was applied but eliminated most temporal incoherence - from using small numbers of sample taps. - */ + Morgan found that a scale of 3 left a 1-pixel checkerboard grid that was + unobjectionable after shading was applied but eliminated most temporal incoherence + from using small numbers of sample taps. + */ uniform int filter_scale; /** Filter radius in pixels. This will be multiplied by SCALE. */ -#define R (4) - +#define R (4) ////////////////////////////////////////////////////////////////////////////////////////////// - // Gaussian coefficients const float gaussian[R + 1] = -// float[](0.356642, 0.239400, 0.072410, 0.009869); -// float[](0.398943, 0.241971, 0.053991, 0.004432, 0.000134); // stddev = 1.0 - float[](0.153170, 0.144893, 0.122649, 0.092902, 0.062970); // stddev = 2.0 -// float[](0.111220, 0.107798, 0.098151, 0.083953, 0.067458, 0.050920, 0.036108); // stddev = 3.0 + //float[](0.356642, 0.239400, 0.072410, 0.009869); + //float[](0.398943, 0.241971, 0.053991, 0.004432, 0.000134); // stddev = 1.0 + float[](0.153170, 0.144893, 0.122649, 0.092902, 0.062970); // stddev = 2.0 +//float[](0.111220, 0.107798, 0.098151, 0.083953, 0.067458, 0.050920, 0.036108); // stddev = 3.0 /** (1, 0) or (0, 1)*/ -uniform ivec2 axis; +uniform ivec2 axis; uniform float camera_z_far; uniform float camera_z_near; @@ -72,11 +69,11 @@ void main() { float depth_divide = 1.0 / camera_z_far; -// depth*=depth_divide; + //depth *= depth_divide; /* - if (depth > camera_z_far*0.999) { - discard;//skybox + if (depth > camera_z_far * 0.999) { + discard; //skybox } */ @@ -96,23 +93,21 @@ void main() { if (r != 0) { ivec2 ppos = ssC + axis * (r * filter_scale); - float value = texelFetch(source_ssao, clamp(ppos,ivec2(0),clamp_limit), 0).r; - ivec2 rpos = clamp(ppos,ivec2(0),clamp_limit); + float value = texelFetch(source_ssao, clamp(ppos, ivec2(0), clamp_limit), 0).r; + ivec2 rpos = clamp(ppos, ivec2(0), clamp_limit); float temp_depth = texelFetch(source_depth, rpos, 0).r; //vec3 temp_normal = texelFetch(source_normal, rpos, 0).rgb * 2.0 - 1.0; temp_depth = temp_depth * 2.0 - 1.0; temp_depth = 2.0 * camera_z_near * camera_z_far / (camera_z_far + camera_z_near - temp_depth * (camera_z_far - camera_z_near)); -// temp_depth *= depth_divide; + // temp_depth *= depth_divide; // spatial domain: offset gaussian tap float weight = 0.3 + gaussian[abs(r)]; //weight *= max(0.0,dot(temp_normal,normal)); // range domain (the "bilateral" weight). As depth difference increases, decrease weight. - weight *= max(0.0, 1.0 - - edge_sharpness * abs(temp_depth - depth) - ); + weight *= max(0.0, 1.0 - edge_sharpness * abs(temp_depth - depth)); sum += value * weight; totalWeight += weight; diff --git a/drivers/gles2/shaders/ssao_minify.glsl b/drivers/gles2/shaders/ssao_minify.glsl index 647c762438..272f3e236e 100644 --- a/drivers/gles2/shaders/ssao_minify.glsl +++ b/drivers/gles2/shaders/ssao_minify.glsl @@ -1,21 +1,23 @@ +/* clang-format off */ [vertex] - -layout(location=0) in highp vec4 vertex_attrib; +layout(location = 0) in highp vec4 vertex_attrib; +/* clang-format on */ void main() { gl_Position = vertex_attrib; } +/* clang-format off */ [fragment] - #ifdef MINIFY_START #define SDEPTH_TYPE highp sampler2D uniform float camera_z_far; uniform float camera_z_near; +/* clang-format on */ #else @@ -32,28 +34,23 @@ layout(location = 0) out mediump uint depth; void main() { - ivec2 ssP = ivec2(gl_FragCoord.xy); - // Rotated grid subsampling to avoid XY directional bias or Z precision bias while downsampling. - // On DX9, the bit-and can be implemented with floating-point modulo + // Rotated grid subsampling to avoid XY directional bias or Z precision bias while downsampling. + // On DX9, the bit-and can be implemented with floating-point modulo #ifdef MINIFY_START float fdepth = texelFetch(source_depth, clamp(ssP * 2 + ivec2(ssP.y & 1, ssP.x & 1), ivec2(0), from_size - ivec2(1)), source_mipmap).r; fdepth = fdepth * 2.0 - 1.0; #ifdef USE_ORTHOGONAL_PROJECTION - fdepth = ((fdepth + (camera_z_far + camera_z_near)/(camera_z_far - camera_z_near)) * (camera_z_far - camera_z_near))/2.0; + fdepth = ((fdepth + (camera_z_far + camera_z_near) / (camera_z_far - camera_z_near)) * (camera_z_far - camera_z_near)) / 2.0; #else fdepth = 2.0 * camera_z_near * camera_z_far / (camera_z_far + camera_z_near - fdepth * (camera_z_far - camera_z_near)); #endif fdepth /= camera_z_far; - depth = uint(clamp(fdepth*65535.0,0.0,65535.0)); + depth = uint(clamp(fdepth * 65535.0, 0.0, 65535.0)); #else depth = texelFetch(source_depth, clamp(ssP * 2 + ivec2(ssP.y & 1, ssP.x & 1), ivec2(0), from_size - ivec2(1)), source_mipmap).r; #endif - - } - - diff --git a/drivers/gles2/shaders/stdlib.glsl b/drivers/gles2/shaders/stdlib.glsl index ebbdb96311..3674d70c9f 100644 --- a/drivers/gles2/shaders/stdlib.glsl +++ b/drivers/gles2/shaders/stdlib.glsl @@ -1,6 +1,5 @@ -vec2 select2(vec2 a, vec2 b, bvec2 c) -{ +vec2 select2(vec2 a, vec2 b, bvec2 c) { vec2 ret; ret.x = c.x ? b.x : a.x; @@ -9,8 +8,7 @@ vec2 select2(vec2 a, vec2 b, bvec2 c) return ret; } -vec3 select3(vec3 a, vec3 b, bvec3 c) -{ +vec3 select3(vec3 a, vec3 b, bvec3 c) { vec3 ret; ret.x = c.x ? b.x : a.x; @@ -20,8 +18,7 @@ vec3 select3(vec3 a, vec3 b, bvec3 c) return ret; } -vec4 select4(vec4 a, vec4 b, bvec4 c) -{ +vec4 select4(vec4 a, vec4 b, bvec4 c) { vec4 ret; ret.x = c.x ? b.x : a.x; @@ -32,14 +29,19 @@ vec4 select4(vec4 a, vec4 b, bvec4 c) return ret; } - -highp vec4 texel2DFetch(highp sampler2D tex, ivec2 size, ivec2 coord) -{ +highp vec4 texel2DFetch(highp sampler2D tex, ivec2 size, ivec2 coord) { float x_coord = float(2 * coord.x + 1) / float(size.x * 2); float y_coord = float(2 * coord.y + 1) / float(size.y * 2); - x_coord = float(coord.x) / float(size.x); - y_coord = float(coord.y) / float(size.y); - return texture2DLod(tex, vec2(x_coord, y_coord), 0.0); } + +#ifndef USE_GLES_OVER_GL +highp mat4 transpose(highp mat4 src) { + return mat4( + vec4(src[0].x, src[1].x, src[2].x, src[3].x), + vec4(src[0].y, src[1].y, src[2].y, src[3].y), + vec4(src[0].z, src[1].z, src[2].z, src[3].z), + vec4(src[0].w, src[1].w, src[2].w, src[3].w)); +} +#endif diff --git a/drivers/gles2/shaders/subsurf_scattering.glsl b/drivers/gles2/shaders/subsurf_scattering.glsl index fc66d66198..f40fb3a244 100644 --- a/drivers/gles2/shaders/subsurf_scattering.glsl +++ b/drivers/gles2/shaders/subsurf_scattering.glsl @@ -1,105 +1,93 @@ +/* clang-format off */ [vertex] - -layout(location=0) in highp vec4 vertex_attrib; -layout(location=4) in vec2 uv_in; +layout(location = 0) in highp vec4 vertex_attrib; +/* clang-format on */ +layout(location = 4) in vec2 uv_in; out vec2 uv_interp; - void main() { uv_interp = uv_in; gl_Position = vertex_attrib; } +/* clang-format off */ [fragment] //#define QUALIFIER uniform // some guy on the interweb says it may be faster with this #define QUALIFIER const #ifdef USE_25_SAMPLES - -const int kernel_size=25; -QUALIFIER vec2 kernel[25] = vec2[] ( - vec2(0.530605, 0.0), - vec2(0.000973794, -3.0), - vec2(0.00333804, -2.52083), - vec2(0.00500364, -2.08333), - vec2(0.00700976, -1.6875), - vec2(0.0094389, -1.33333), - vec2(0.0128496, -1.02083), - vec2(0.017924, -0.75), - vec2(0.0263642, -0.520833), - vec2(0.0410172, -0.333333), - vec2(0.0493588, -0.1875), - vec2(0.0402784, -0.0833333), - vec2(0.0211412, -0.0208333), - vec2(0.0211412, 0.0208333), - vec2(0.0402784, 0.0833333), - vec2(0.0493588, 0.1875), - vec2(0.0410172, 0.333333), - vec2(0.0263642, 0.520833), - vec2(0.017924, 0.75), - vec2(0.0128496, 1.02083), - vec2(0.0094389, 1.33333), - vec2(0.00700976, 1.6875), - vec2(0.00500364, 2.08333), - vec2(0.00333804, 2.52083), - vec2(0.000973794, 3.0) -); - +const int kernel_size = 25; +/* clang-format on */ +QUALIFIER vec2 kernel[25] = vec2[]( + vec2(0.530605, 0.0), + vec2(0.000973794, -3.0), + vec2(0.00333804, -2.52083), + vec2(0.00500364, -2.08333), + vec2(0.00700976, -1.6875), + vec2(0.0094389, -1.33333), + vec2(0.0128496, -1.02083), + vec2(0.017924, -0.75), + vec2(0.0263642, -0.520833), + vec2(0.0410172, -0.333333), + vec2(0.0493588, -0.1875), + vec2(0.0402784, -0.0833333), + vec2(0.0211412, -0.0208333), + vec2(0.0211412, 0.0208333), + vec2(0.0402784, 0.0833333), + vec2(0.0493588, 0.1875), + vec2(0.0410172, 0.333333), + vec2(0.0263642, 0.520833), + vec2(0.017924, 0.75), + vec2(0.0128496, 1.02083), + vec2(0.0094389, 1.33333), + vec2(0.00700976, 1.6875), + vec2(0.00500364, 2.08333), + vec2(0.00333804, 2.52083), + vec2(0.000973794, 3.0)); #endif //USE_25_SAMPLES #ifdef USE_17_SAMPLES - -const int kernel_size=17; - +const int kernel_size = 17; QUALIFIER vec2 kernel[17] = vec2[]( - vec2(0.536343, 0.0), - vec2(0.00317394, -2.0), - vec2(0.0100386, -1.53125), - vec2(0.0144609, -1.125), - vec2(0.0216301, -0.78125), - vec2(0.0347317, -0.5), - vec2(0.0571056, -0.28125), - vec2(0.0582416, -0.125), - vec2(0.0324462, -0.03125), - vec2(0.0324462, 0.03125), - vec2(0.0582416, 0.125), - vec2(0.0571056, 0.28125), - vec2(0.0347317, 0.5), - vec2(0.0216301, 0.78125), - vec2(0.0144609, 1.125), - vec2(0.0100386, 1.53125), - vec2(0.00317394,2.0) -); - + vec2(0.536343, 0.0), + vec2(0.00317394, -2.0), + vec2(0.0100386, -1.53125), + vec2(0.0144609, -1.125), + vec2(0.0216301, -0.78125), + vec2(0.0347317, -0.5), + vec2(0.0571056, -0.28125), + vec2(0.0582416, -0.125), + vec2(0.0324462, -0.03125), + vec2(0.0324462, 0.03125), + vec2(0.0582416, 0.125), + vec2(0.0571056, 0.28125), + vec2(0.0347317, 0.5), + vec2(0.0216301, 0.78125), + vec2(0.0144609, 1.125), + vec2(0.0100386, 1.53125), + vec2(0.00317394, 2.0)); #endif //USE_17_SAMPLES - #ifdef USE_11_SAMPLES - -const int kernel_size=11; - +const int kernel_size = 11; QUALIFIER vec2 kernel[11] = vec2[]( - vec2(0.560479, 0.0), - vec2(0.00471691, -2.0), - vec2(0.0192831, -1.28), - vec2(0.03639, -0.72), - vec2(0.0821904, -0.32), - vec2(0.0771802, -0.08), - vec2(0.0771802, 0.08), - vec2(0.0821904, 0.32), - vec2(0.03639, 0.72), - vec2(0.0192831, 1.28), - vec2(0.00471691,2.0) -); - + vec2(0.560479, 0.0), + vec2(0.00471691, -2.0), + vec2(0.0192831, -1.28), + vec2(0.03639, -0.72), + vec2(0.0821904, -0.32), + vec2(0.0771802, -0.08), + vec2(0.0771802, 0.08), + vec2(0.0821904, 0.32), + vec2(0.03639, 0.72), + vec2(0.0192831, 1.28), + vec2(0.00471691, 2.0)); #endif //USE_11_SAMPLES - - uniform float max_radius; uniform float camera_z_far; uniform float camera_z_near; @@ -115,28 +103,24 @@ layout(location = 0) out vec4 frag_color; void main() { - float strength = texture(source_sss,uv_interp).r; - strength*=strength; //stored as sqrt + float strength = texture(source_sss, uv_interp).r; + strength *= strength; //stored as sqrt // Fetch color of current pixel: vec4 base_color = texture(source_diffuse, uv_interp); - - if (strength>0.0) { - + if (strength > 0.0) { // Fetch linear depth of current pixel: float depth = texture(source_depth, uv_interp).r * 2.0 - 1.0; #ifdef USE_ORTHOGONAL_PROJECTION - depth = ((depth + (camera_z_far + camera_z_near)/(camera_z_far - camera_z_near)) * (camera_z_far - camera_z_near))/2.0; + depth = ((depth + (camera_z_far + camera_z_near) / (camera_z_far - camera_z_near)) * (camera_z_far - camera_z_near)) / 2.0; float scale = unit_size; //remember depth is negative by default in OpenGL #else depth = 2.0 * camera_z_near * camera_z_far / (camera_z_far + camera_z_near - depth * (camera_z_far - camera_z_near)); float scale = unit_size / depth; //remember depth is negative by default in OpenGL #endif - - // Calculate the final step to fetch the surrounding pixels: vec2 step = max_radius * scale * dir; step *= strength; // Modulate it using the alpha channel. @@ -157,35 +141,33 @@ void main() { #ifdef ENABLE_FOLLOW_SURFACE // If the difference in depth is huge, we lerp color back to "colorM": - float depth_cmp = texture(source_depth, offset).r *2.0 - 1.0; + float depth_cmp = texture(source_depth, offset).r * 2.0 - 1.0; #ifdef USE_ORTHOGONAL_PROJECTION - depth_cmp = ((depth_cmp + (camera_z_far + camera_z_near)/(camera_z_far - camera_z_near)) * (camera_z_far - camera_z_near))/2.0; + depth_cmp = ((depth_cmp + (camera_z_far + camera_z_near) / (camera_z_far - camera_z_near)) * (camera_z_far - camera_z_near)) / 2.0; #else depth_cmp = 2.0 * camera_z_near * camera_z_far / (camera_z_far + camera_z_near - depth_cmp * (camera_z_far - camera_z_near)); #endif - float s = clamp(300.0f * scale * - max_radius * abs(depth - depth_cmp),0.0,1.0); + float s = clamp(300.0f * scale * max_radius * abs(depth - depth_cmp), 0.0, 1.0); color = mix(color, base_color.rgb, s); #endif // Accumulate: - color*=kernel[i].x; + color *= kernel[i].x; #ifdef ENABLE_STRENGTH_WEIGHTING float color_s = texture(source_sss, offset).r; - color_weight+=color_s * kernel[i].x; - color*=color_s; + color_weight += color_s * kernel[i].x; + color *= color_s; #endif color_accum += color; - } #ifdef ENABLE_STRENGTH_WEIGHTING - color_accum/=color_weight; + color_accum /= color_weight; #endif - frag_color = vec4(color_accum,base_color.a); //keep alpha (used for SSAO) + frag_color = vec4(color_accum, base_color.a); //keep alpha (used for SSAO) } else { frag_color = base_color; } diff --git a/drivers/gles2/shaders/tonemap.glsl b/drivers/gles2/shaders/tonemap.glsl index 2f671158b2..eae3b5a1ca 100644 --- a/drivers/gles2/shaders/tonemap.glsl +++ b/drivers/gles2/shaders/tonemap.glsl @@ -1,8 +1,9 @@ +/* clang-format off */ [vertex] - -layout(location=0) in highp vec4 vertex_attrib; -layout(location=4) in vec2 uv_in; +layout(location = 0) in highp vec4 vertex_attrib; +/* clang-format on */ +layout(location = 4) in vec2 uv_in; out vec2 uv_interp; @@ -11,19 +12,19 @@ void main() { gl_Position = vertex_attrib; uv_interp = uv_in; #ifdef V_FLIP - uv_interp.y = 1.0-uv_interp.y; + uv_interp.y = 1.0 - uv_interp.y; #endif - } +/* clang-format off */ [fragment] #if !defined(GLES_OVER_GL) precision mediump float; #endif - in vec2 uv_interp; +/* clang-format on */ uniform highp sampler2D source; //texunit:0 @@ -56,64 +57,54 @@ uniform sampler2D color_correction; //texunit:3 #endif - layout(location = 0) out vec4 frag_color; #ifdef USE_GLOW_FILTER_BICUBIC // w0, w1, w2, and w3 are the four cubic B-spline basis functions -float w0(float a) -{ - return (1.0/6.0)*(a*(a*(-a + 3.0) - 3.0) + 1.0); +float w0(float a) { + return (1.0 / 6.0) * (a * (a * (-a + 3.0) - 3.0) + 1.0); } -float w1(float a) -{ - return (1.0/6.0)*(a*a*(3.0*a - 6.0) + 4.0); +float w1(float a) { + return (1.0 / 6.0) * (a * a * (3.0 * a - 6.0) + 4.0); } -float w2(float a) -{ - return (1.0/6.0)*(a*(a*(-3.0*a + 3.0) + 3.0) + 1.0); +float w2(float a) { + return (1.0 / 6.0) * (a * (a * (-3.0 * a + 3.0) + 3.0) + 1.0); } -float w3(float a) -{ - return (1.0/6.0)*(a*a*a); +float w3(float a) { + return (1.0 / 6.0) * (a * a * a); } // g0 and g1 are the two amplitude functions -float g0(float a) -{ - return w0(a) + w1(a); +float g0(float a) { + return w0(a) + w1(a); } -float g1(float a) -{ - return w2(a) + w3(a); +float g1(float a) { + return w2(a) + w3(a); } // h0 and h1 are the two offset functions -float h0(float a) -{ - return -1.0 + w1(a) / (w0(a) + w1(a)); +float h0(float a) { + return -1.0 + w1(a) / (w0(a) + w1(a)); } -float h1(float a) -{ - return 1.0 + w3(a) / (w2(a) + w3(a)); +float h1(float a) { + return 1.0 + w3(a) / (w2(a) + w3(a)); } uniform ivec2 glow_texture_size; -vec4 texture2D_bicubic(sampler2D tex, vec2 uv,int p_lod) -{ - float lod=float(p_lod); +vec4 texture2D_bicubic(sampler2D tex, vec2 uv, int p_lod) { + float lod = float(p_lod); vec2 tex_size = vec2(glow_texture_size >> p_lod); - vec2 pixel_size =1.0/tex_size; - uv = uv*tex_size + 0.5; - vec2 iuv = floor( uv ); - vec2 fuv = fract( uv ); + vec2 pixel_size = 1.0 / tex_size; + uv = uv * tex_size + 0.5; + vec2 iuv = floor(uv); + vec2 fuv = fract(uv); float g0x = g0(fuv.x); float g1x = g1(fuv.x); @@ -127,24 +118,19 @@ vec4 texture2D_bicubic(sampler2D tex, vec2 uv,int p_lod) vec2 p2 = (vec2(iuv.x + h0x, iuv.y + h1y) - 0.5) * pixel_size; vec2 p3 = (vec2(iuv.x + h1x, iuv.y + h1y) - 0.5) * pixel_size; - return g0(fuv.y) * (g0x * textureLod(tex, p0,lod) + - g1x * textureLod(tex, p1,lod)) + - g1(fuv.y) * (g0x * textureLod(tex, p2,lod) + - g1x * textureLod(tex, p3,lod)); + return (g0(fuv.y) * (g0x * textureLod(tex, p0, lod) + g1x * textureLod(tex, p1, lod))) + + (g1(fuv.y) * (g0x * textureLod(tex, p2, lod) + g1x * textureLod(tex, p3, lod))); } - - -#define GLOW_TEXTURE_SAMPLE(m_tex,m_uv,m_lod) texture2D_bicubic(m_tex,m_uv,m_lod) +#define GLOW_TEXTURE_SAMPLE(m_tex, m_uv, m_lod) texture2D_bicubic(m_tex, m_uv, m_lod) #else -#define GLOW_TEXTURE_SAMPLE(m_tex,m_uv,m_lod) textureLod(m_tex,m_uv,float(m_lod)) +#define GLOW_TEXTURE_SAMPLE(m_tex, m_uv, m_lod) textureLod(m_tex, m_uv, float(m_lod)) #endif - -vec3 tonemap_filmic(vec3 color,float white) { +vec3 tonemap_filmic(vec3 color, float white) { float A = 0.15; float B = 0.50; @@ -154,11 +140,10 @@ vec3 tonemap_filmic(vec3 color,float white) { float F = 0.30; float W = 11.2; - vec3 coltn = ((color*(A*color+C*B)+D*E)/(color*(A*color+B)+D*F))-E/F; - float whitetn = ((white*(A*white+C*B)+D*E)/(white*(A*white+B)+D*F))-E/F; - - return coltn/whitetn; + vec3 coltn = ((color * (A * color + C * B) + D * E) / (color * (A * color + B) + D * F)) - E / F; + float whitetn = ((white * (A * white + C * B) + D * E) / (white * (A * white + B) + D * F)) - E / F; + return coltn / whitetn; } vec3 tonemap_aces(vec3 color) { @@ -167,12 +152,12 @@ vec3 tonemap_aces(vec3 color) { float c = 2.43f; float d = 0.59f; float e = 0.14f; - return color = clamp((color*(a*color+b))/(color*(c*color+d)+e),vec3(0.0),vec3(1.0)); + return color = clamp((color * (a * color + b)) / (color * (c * color + d) + e), vec3(0.0), vec3(1.0)); } -vec3 tonemap_reindhart(vec3 color,float white) { +vec3 tonemap_reindhart(vec3 color, float white) { - return ( color * ( 1.0 + ( color / ( white) ) ) ) / ( 1.0 + color ); + return (color * (1.0 + (color / (white)))) / (1.0 + color); } void main() { @@ -181,10 +166,10 @@ void main() { #ifdef USE_AUTO_EXPOSURE - color/=texelFetch(source_auto_exposure,ivec2(0,0),0).r/auto_exposure_grey; + color /= texelFetch(source_auto_exposure, ivec2(0, 0), 0).r / auto_exposure_grey; #endif - color*=exposure; + color *= exposure; #if defined(USE_GLOW_LEVEL1) || defined(USE_GLOW_LEVEL2) || defined(USE_GLOW_LEVEL3) || defined(USE_GLOW_LEVEL4) || defined(USE_GLOW_LEVEL5) || defined(USE_GLOW_LEVEL6) || defined(USE_GLOW_LEVEL7) #define USING_GLOW @@ -195,56 +180,54 @@ void main() { #ifdef USE_GLOW_LEVEL1 - glow+=GLOW_TEXTURE_SAMPLE(source_glow,uv_interp,1).rgb; + glow += GLOW_TEXTURE_SAMPLE(source_glow, uv_interp, 1).rgb; #endif #ifdef USE_GLOW_LEVEL2 - glow+=GLOW_TEXTURE_SAMPLE(source_glow,uv_interp,2).rgb; + glow += GLOW_TEXTURE_SAMPLE(source_glow, uv_interp, 2).rgb; #endif #ifdef USE_GLOW_LEVEL3 - glow+=GLOW_TEXTURE_SAMPLE(source_glow,uv_interp,3).rgb; + glow += GLOW_TEXTURE_SAMPLE(source_glow, uv_interp, 3).rgb; #endif #ifdef USE_GLOW_LEVEL4 - glow+=GLOW_TEXTURE_SAMPLE(source_glow,uv_interp,4).rgb; + glow += GLOW_TEXTURE_SAMPLE(source_glow, uv_interp, 4).rgb; #endif #ifdef USE_GLOW_LEVEL5 - glow+=GLOW_TEXTURE_SAMPLE(source_glow,uv_interp,5).rgb; + glow += GLOW_TEXTURE_SAMPLE(source_glow, uv_interp, 5).rgb; #endif #ifdef USE_GLOW_LEVEL6 - glow+=GLOW_TEXTURE_SAMPLE(source_glow,uv_interp,6).rgb; + glow += GLOW_TEXTURE_SAMPLE(source_glow, uv_interp, 6).rgb; #endif #ifdef USE_GLOW_LEVEL7 - glow+=GLOW_TEXTURE_SAMPLE(source_glow,uv_interp,7).rgb; + glow += GLOW_TEXTURE_SAMPLE(source_glow, uv_interp, 7).rgb; #endif - glow *= glow_intensity; #endif - #ifdef USE_REINDHART_TONEMAPPER - color.rgb = tonemap_reindhart(color.rgb,white); + color.rgb = tonemap_reindhart(color.rgb, white); -# if defined(USING_GLOW) - glow = tonemap_reindhart(glow,white); -# endif +#if defined(USING_GLOW) + glow = tonemap_reindhart(glow, white); +#endif #endif #ifdef USE_FILMIC_TONEMAPPER - color.rgb = tonemap_filmic(color.rgb,white); + color.rgb = tonemap_filmic(color.rgb, white); -# if defined(USING_GLOW) - glow = tonemap_filmic(glow,white); -# endif +#if defined(USING_GLOW) + glow = tonemap_filmic(glow, white); +#endif #endif @@ -252,26 +235,26 @@ void main() { color.rgb = tonemap_aces(color.rgb); -# if defined(USING_GLOW) +#if defined(USING_GLOW) glow = tonemap_aces(glow); -# endif +#endif #endif //regular Linear -> SRGB conversion vec3 a = vec3(0.055); - color.rgb = mix( (vec3(1.0)+a)*pow(color.rgb,vec3(1.0/2.4))-a , 12.92*color.rgb , lessThan(color.rgb,vec3(0.0031308))); + color.rgb = mix((vec3(1.0) + a) * pow(color.rgb, vec3(1.0 / 2.4)) - a, 12.92 * color.rgb, lessThan(color.rgb, vec3(0.0031308))); #if defined(USING_GLOW) - glow = mix( (vec3(1.0)+a)*pow(glow,vec3(1.0/2.4))-a , 12.92*glow , lessThan(glow,vec3(0.0031308))); + glow = mix((vec3(1.0) + a) * pow(glow, vec3(1.0 / 2.4)) - a, 12.92 * glow, lessThan(glow, vec3(0.0031308))); #endif -//glow needs to be added in SRGB space (together with image space effects) + //glow needs to be added in SRGB space (together with image space effects) - color.rgb = clamp(color.rgb,0.0,1.0); + color.rgb = clamp(color.rgb, 0.0, 1.0); #if defined(USING_GLOW) - glow = clamp(glow,0.0,1.0); + glow = clamp(glow, 0.0, 1.0); #endif #ifdef USE_GLOW_REPLACE @@ -291,33 +274,32 @@ void main() { { glow = (glow * 0.5) + 0.5; - color.r = (glow.r <= 0.5) ? (color.r - (1.0 - 2.0 * glow.r) * color.r * (1.0 - color.r)) : (((glow.r > 0.5) && (color.r <= 0.25)) ? (color.r + (2.0 * glow.r - 1.0) * (4.0 * color.r * (4.0 * color.r + 1.0) * (color.r - 1.0) + 7.0 * color.r)) : (color.r + (2.0 * glow.r - 1.0) * (sqrt(color.r) - color.r))); - color.g = (glow.g <= 0.5) ? (color.g - (1.0 - 2.0 * glow.g) * color.g * (1.0 - color.g)) : (((glow.g > 0.5) && (color.g <= 0.25)) ? (color.g + (2.0 * glow.g - 1.0) * (4.0 * color.g * (4.0 * color.g + 1.0) * (color.g - 1.0) + 7.0 * color.g)) : (color.g + (2.0 * glow.g - 1.0) * (sqrt(color.g) - color.g))); - color.b = (glow.b <= 0.5) ? (color.b - (1.0 - 2.0 * glow.b) * color.b * (1.0 - color.b)) : (((glow.b > 0.5) && (color.b <= 0.25)) ? (color.b + (2.0 * glow.b - 1.0) * (4.0 * color.b * (4.0 * color.b + 1.0) * (color.b - 1.0) + 7.0 * color.b)) : (color.b + (2.0 * glow.b - 1.0) * (sqrt(color.b) - color.b))); + color.r = (glow.r <= 0.5) ? (color.r - (1.0 - 2.0 * glow.r) * color.r * (1.0 - color.r)) : (((glow.r > 0.5) && (color.r <= 0.25)) ? (color.r + (2.0 * glow.r - 1.0) * (4.0 * color.r * (4.0 * color.r + 1.0) * (color.r - 1.0) + 7.0 * color.r)) : (color.r + (2.0 * glow.r - 1.0) * (sqrt(color.r) - color.r))); + color.g = (glow.g <= 0.5) ? (color.g - (1.0 - 2.0 * glow.g) * color.g * (1.0 - color.g)) : (((glow.g > 0.5) && (color.g <= 0.25)) ? (color.g + (2.0 * glow.g - 1.0) * (4.0 * color.g * (4.0 * color.g + 1.0) * (color.g - 1.0) + 7.0 * color.g)) : (color.g + (2.0 * glow.g - 1.0) * (sqrt(color.g) - color.g))); + color.b = (glow.b <= 0.5) ? (color.b - (1.0 - 2.0 * glow.b) * color.b * (1.0 - color.b)) : (((glow.b > 0.5) && (color.b <= 0.25)) ? (color.b + (2.0 * glow.b - 1.0) * (4.0 * color.b * (4.0 * color.b + 1.0) * (color.b - 1.0) + 7.0 * color.b)) : (color.b + (2.0 * glow.b - 1.0) * (sqrt(color.b) - color.b))); } #endif #if defined(USING_GLOW) && !defined(USE_GLOW_SCREEN) && !defined(USE_GLOW_SOFTLIGHT) && !defined(USE_GLOW_REPLACE) //additive - color.rgb+=glow; + color.rgb += glow; #endif #ifdef USE_BCS - color.rgb = mix(vec3(0.0),color.rgb,bcs.x); - color.rgb = mix(vec3(0.5),color.rgb,bcs.y); - color.rgb = mix(vec3(dot(vec3(1.0),color.rgb)*0.33333),color.rgb,bcs.z); + color.rgb = mix(vec3(0.0), color.rgb, bcs.x); + color.rgb = mix(vec3(0.5), color.rgb, bcs.y); + color.rgb = mix(vec3(dot(vec3(1.0), color.rgb) * 0.33333), color.rgb, bcs.z); #endif #ifdef USE_COLOR_CORRECTION - color.r = texture(color_correction,vec2(color.r,0.0)).r; - color.g = texture(color_correction,vec2(color.g,0.0)).g; - color.b = texture(color_correction,vec2(color.b,0.0)).b; + color.r = texture(color_correction, vec2(color.r, 0.0)).r; + color.g = texture(color_correction, vec2(color.g, 0.0)).g; + color.b = texture(color_correction, vec2(color.b, 0.0)).b; #endif - - frag_color=vec4(color.rgb,1.0); + frag_color = vec4(color.rgb, 1.0); } diff --git a/drivers/gles3/rasterizer_canvas_gles3.cpp b/drivers/gles3/rasterizer_canvas_gles3.cpp index 5e13bed198..4166cb8361 100644 --- a/drivers/gles3/rasterizer_canvas_gles3.cpp +++ b/drivers/gles3/rasterizer_canvas_gles3.cpp @@ -29,10 +29,12 @@ /*************************************************************************/ #include "rasterizer_canvas_gles3.h" -#include "os/os.h" -#include "project_settings.h" + +#include "core/os/os.h" +#include "core/project_settings.h" #include "rasterizer_scene_gles3.h" #include "servers/visual/visual_server_raster.h" + #ifndef GLES_OVER_GL #define glClearDepth glClearDepthf #endif @@ -211,12 +213,12 @@ RasterizerStorageGLES3::Texture *RasterizerCanvasGLES3::_bind_canvas_texture(con } else { + texture = texture->get_ptr(); + if (texture->redraw_if_visible) { //check before proxy, because this is usually used with proxies VisualServerRaster::redraw_request(); } - texture = texture->get_ptr(); - if (texture->render_target) texture->render_target->used_in_frame = true; @@ -252,11 +254,12 @@ RasterizerStorageGLES3::Texture *RasterizerCanvasGLES3::_bind_canvas_texture(con } else { + normal_map = normal_map->get_ptr(); + if (normal_map->redraw_if_visible) { //check before proxy, because this is usually used with proxies VisualServerRaster::redraw_request(); } - normal_map = normal_map->get_ptr(); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, normal_map->tex_id); state.current_normal = p_normal_map; @@ -832,6 +835,120 @@ void RasterizerCanvasGLES3::_canvas_item_render_commands(Item *p_item, Item *cur } } } break; + case Item::Command::TYPE_MULTIMESH: { + + Item::CommandMultiMesh *mmesh = static_cast<Item::CommandMultiMesh *>(c); + + RasterizerStorageGLES3::MultiMesh *multi_mesh = storage->multimesh_owner.getornull(mmesh->multimesh); + + if (!multi_mesh) + break; + + RasterizerStorageGLES3::Mesh *mesh_data = storage->mesh_owner.getornull(multi_mesh->mesh); + + if (!mesh_data) + break; + + RasterizerStorageGLES3::Texture *texture = _bind_canvas_texture(mmesh->texture, mmesh->normal_map); + + state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_INSTANCE_CUSTOM, multi_mesh->custom_data_format != VS::MULTIMESH_CUSTOM_DATA_NONE); + state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_INSTANCING, true); + //reset shader and force rebind + state.using_texture_rect = true; + _set_texture_rect_mode(false); + + if (texture) { + Size2 texpixel_size(1.0 / texture->width, 1.0 / texture->height); + state.canvas_shader.set_uniform(CanvasShaderGLES3::COLOR_TEXPIXEL_SIZE, texpixel_size); + } + + int amount = MAX(multi_mesh->size, multi_mesh->visible_instances); + + for (int j = 0; j < mesh_data->surfaces.size(); j++) { + RasterizerStorageGLES3::Surface *s = mesh_data->surfaces[j]; + // materials are ignored in 2D meshes, could be added but many things (ie, lighting mode, reading from screen, etc) would break as they are not meant be set up at this point of drawing + glBindVertexArray(s->instancing_array_id); + + glBindBuffer(GL_ARRAY_BUFFER, multi_mesh->buffer); //modify the buffer + + int stride = (multi_mesh->xform_floats + multi_mesh->color_floats + multi_mesh->custom_data_floats) * 4; + glEnableVertexAttribArray(8); + glVertexAttribPointer(8, 4, GL_FLOAT, GL_FALSE, stride, ((uint8_t *)NULL) + 0); + glVertexAttribDivisor(8, 1); + glEnableVertexAttribArray(9); + glVertexAttribPointer(9, 4, GL_FLOAT, GL_FALSE, stride, ((uint8_t *)NULL) + 4 * 4); + glVertexAttribDivisor(9, 1); + + int color_ofs; + + if (multi_mesh->transform_format == VS::MULTIMESH_TRANSFORM_3D) { + glEnableVertexAttribArray(10); + glVertexAttribPointer(10, 4, GL_FLOAT, GL_FALSE, stride, ((uint8_t *)NULL) + 8 * 4); + glVertexAttribDivisor(10, 1); + color_ofs = 12 * 4; + } else { + glDisableVertexAttribArray(10); + glVertexAttrib4f(10, 0, 0, 1, 0); + color_ofs = 8 * 4; + } + + int custom_data_ofs = color_ofs; + + switch (multi_mesh->color_format) { + + case VS::MULTIMESH_COLOR_NONE: { + glDisableVertexAttribArray(11); + glVertexAttrib4f(11, 1, 1, 1, 1); + } break; + case VS::MULTIMESH_COLOR_8BIT: { + glEnableVertexAttribArray(11); + glVertexAttribPointer(11, 4, GL_UNSIGNED_BYTE, GL_TRUE, stride, ((uint8_t *)NULL) + color_ofs); + glVertexAttribDivisor(11, 1); + custom_data_ofs += 4; + + } break; + case VS::MULTIMESH_COLOR_FLOAT: { + glEnableVertexAttribArray(11); + glVertexAttribPointer(11, 4, GL_FLOAT, GL_FALSE, stride, ((uint8_t *)NULL) + color_ofs); + glVertexAttribDivisor(11, 1); + custom_data_ofs += 4 * 4; + } break; + } + + switch (multi_mesh->custom_data_format) { + + case VS::MULTIMESH_CUSTOM_DATA_NONE: { + glDisableVertexAttribArray(12); + glVertexAttrib4f(12, 1, 1, 1, 1); + } break; + case VS::MULTIMESH_CUSTOM_DATA_8BIT: { + glEnableVertexAttribArray(12); + glVertexAttribPointer(12, 4, GL_UNSIGNED_BYTE, GL_TRUE, stride, ((uint8_t *)NULL) + custom_data_ofs); + glVertexAttribDivisor(12, 1); + + } break; + case VS::MULTIMESH_CUSTOM_DATA_FLOAT: { + glEnableVertexAttribArray(12); + glVertexAttribPointer(12, 4, GL_FLOAT, GL_FALSE, stride, ((uint8_t *)NULL) + custom_data_ofs); + glVertexAttribDivisor(12, 1); + } break; + } + + if (s->index_array_len) { + glDrawElementsInstanced(gl_primitive[s->primitive], s->index_array_len, (s->array_len >= (1 << 16)) ? GL_UNSIGNED_INT : GL_UNSIGNED_SHORT, 0, amount); + } else { + glDrawArraysInstanced(gl_primitive[s->primitive], 0, s->array_len, amount); + } + + glBindVertexArray(0); + } + + state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_INSTANCE_CUSTOM, false); + state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_INSTANCING, false); + state.using_texture_rect = true; + _set_texture_rect_mode(false); + + } break; case Item::Command::TYPE_PARTICLES: { Item::CommandParticles *particles_cmd = static_cast<Item::CommandParticles *>(c); @@ -860,7 +977,7 @@ void RasterizerCanvasGLES3::_canvas_item_render_commands(Item *p_item, Item *cur RasterizerStorageGLES3::Texture *texture = _bind_canvas_texture(particles_cmd->texture, particles_cmd->normal_map); if (texture) { - Size2 texpixel_size(1.0 / (texture->width / particles_cmd->h_frames), 1.0 / (texture->height / particles_cmd->v_frames)); + Size2 texpixel_size(1.0 / texture->width, 1.0 / texture->height); state.canvas_shader.set_uniform(CanvasShaderGLES3::COLOR_TEXPIXEL_SIZE, texpixel_size); } else { state.canvas_shader.set_uniform(CanvasShaderGLES3::COLOR_TEXPIXEL_SIZE, Vector2(1.0, 1.0)); @@ -877,9 +994,6 @@ void RasterizerCanvasGLES3::_canvas_item_render_commands(Item *p_item, Item *cur state.canvas_shader.set_uniform(CanvasShaderGLES3::MODELVIEW_MATRIX, state.final_transform * inv_xf); } - state.canvas_shader.set_uniform(CanvasShaderGLES3::H_FRAMES, particles_cmd->h_frames); - state.canvas_shader.set_uniform(CanvasShaderGLES3::V_FRAMES, particles_cmd->v_frames); - glBindVertexArray(data.particle_quad_array); //use particle quad array glBindBuffer(GL_ARRAY_BUFFER, particles->particle_buffers[0]); //bind particle buffer @@ -956,8 +1070,8 @@ void RasterizerCanvasGLES3::_canvas_item_render_commands(Item *p_item, Item *cur glBindVertexArray(0); state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_INSTANCE_CUSTOM, false); - state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_INSTANCING, false); state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_PARTICLES, false); + state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_INSTANCING, false); state.using_texture_rect = true; _set_texture_rect_mode(false); @@ -1107,10 +1221,6 @@ void RasterizerCanvasGLES3::canvas_render_items(Item *p_item_list, int p_z, cons bool rebind_shader = true; - Size2 rt_size = Size2(storage->frame.current_rt->width, storage->frame.current_rt->height); - - state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_DISTANCE_FIELD, false); - glBindBuffer(GL_UNIFORM_BUFFER, state.canvas_item_ubo); glBufferData(GL_UNIFORM_BUFFER, sizeof(CanvasItemUBO), &state.canvas_item_ubo_data, GL_DYNAMIC_DRAW); glBindBuffer(GL_UNIFORM_BUFFER, 0); @@ -1173,8 +1283,8 @@ void RasterizerCanvasGLES3::canvas_render_items(Item *p_item_list, int p_z, cons { //skeleton handling - if (ci->skeleton.is_valid()) { - skeleton = storage->skeleton_owner.getornull(ci->skeleton); + if (ci->skeleton.is_valid() && storage->skeleton_owner.owns(ci->skeleton)) { + skeleton = storage->skeleton_owner.get(ci->skeleton); if (!skeleton->use_2d) { skeleton = NULL; } else { @@ -1228,7 +1338,7 @@ void RasterizerCanvasGLES3::canvas_render_items(Item *p_item_list, int p_z, cons last_blend_mode = last_blend_mode != RasterizerStorageGLES3::Shader::CanvasItem::BLEND_MODE_DISABLED ? last_blend_mode : -1; } - if (shader_ptr != shader_cache) { + if (shader_ptr != shader_cache || rebind_shader) { if (shader_ptr->canvas_item.uses_time) { VisualServerRaster::redraw_request(); @@ -1274,12 +1384,12 @@ void RasterizerCanvasGLES3::canvas_render_items(Item *p_item_list, int p_z, cons continue; } + t = t->get_ptr(); + if (t->redraw_if_visible) { //check before proxy, because this is usually used with proxies VisualServerRaster::redraw_request(); } - t = t->get_ptr(); - if (storage->config.srgb_decode_supported && t->using_srgb) { //no srgb in 2D glTexParameteri(t->target, _TEXTURE_SRGB_DECODE_EXT, _SKIP_DECODE_EXT); @@ -1304,7 +1414,7 @@ void RasterizerCanvasGLES3::canvas_render_items(Item *p_item_list, int p_z, cons if (blend_mode == RasterizerStorageGLES3::Shader::CanvasItem::BLEND_MODE_DISABLED && (!storage->frame.current_rt || !storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT])) { blend_mode = RasterizerStorageGLES3::Shader::CanvasItem::BLEND_MODE_MIX; } - bool unshaded = shader_cache && (shader_cache->canvas_item.light_mode == RasterizerStorageGLES3::Shader::CanvasItem::LIGHT_MODE_UNSHADED || blend_mode != RasterizerStorageGLES3::Shader::CanvasItem::BLEND_MODE_MIX); + bool unshaded = shader_cache && (shader_cache->canvas_item.light_mode == RasterizerStorageGLES3::Shader::CanvasItem::LIGHT_MODE_UNSHADED || (blend_mode != RasterizerStorageGLES3::Shader::CanvasItem::BLEND_MODE_MIX && blend_mode != RasterizerStorageGLES3::Shader::CanvasItem::BLEND_MODE_PMALPHA)); bool reclip = false; if (last_blend_mode != blend_mode) { @@ -1477,6 +1587,7 @@ void RasterizerCanvasGLES3::canvas_render_items(Item *p_item_list, int p_z, cons if (!t) { glBindTexture(GL_TEXTURE_2D, storage->resources.white_tex); } else { + t = t->get_ptr(); glBindTexture(t->target, t->tex_id); } @@ -1544,6 +1655,14 @@ void RasterizerCanvasGLES3::canvas_render_items(Item *p_item_list, int p_z, cons if (current_clip) { glDisable(GL_SCISSOR_TEST); } + //disable states that may have been used + state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_DISTANCE_FIELD, false); + state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_SKELETON, false); + state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_INSTANCE_CUSTOM, false); + state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_PARTICLES, false); + state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_INSTANCING, false); + state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_LIGHTING, false); + state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_SHADOWS, false); } void RasterizerCanvasGLES3::canvas_debug_viewport_shadows(Light *p_lights_with_shadow) { @@ -1557,17 +1676,12 @@ void RasterizerCanvasGLES3::canvas_debug_viewport_shadows(Light *p_lights_with_s int ofs = h; glDisable(GL_BLEND); - //print_line(" debug lights "); while (light) { - - //print_line("debug light"); if (light->shadow_buffer.is_valid()) { - //print_line("sb is valid"); RasterizerStorageGLES3::CanvasLightShadow *sb = storage->canvas_light_shadow_owner.get(light->shadow_buffer); if (sb) { glBindTexture(GL_TEXTURE_2D, sb->distance); - //glBindTexture(GL_TEXTURE_2D,storage->resources.white_tex); draw_generic_textured_rect(Rect2(h, ofs, w - h * 2, h), Rect2(0, 0, 1, 1)); ofs += h * 2; } @@ -1677,19 +1791,7 @@ void RasterizerCanvasGLES3::canvas_light_shadow_buffer_update(RID p_buffer, cons } break; } } - /* - if (i==0) { - for(int i=0;i<cc->lines.size();i++) { - Vector2 p = instance->xform_cache.xform(cc->lines.get(i)); - Plane pp(Vector3(p.x,p.y,0),1); - pp.normal = light.xform(pp.normal); - pp = projection.xform4(pp); - print_line(itos(i)+": "+pp.normal/pp.d); - //pp=light_mat.xform4(pp); - //print_line(itos(i)+": "+pp.normal/pp.d); - } - } -*/ + glBindVertexArray(cc->array_id); glDrawElements(GL_TRIANGLES, cc->len * 3, GL_UNSIGNED_SHORT, 0); @@ -1771,6 +1873,39 @@ void RasterizerCanvasGLES3::draw_generic_textured_rect(const Rect2 &p_rect, cons glDrawArrays(GL_TRIANGLE_FAN, 0, 4); } +void RasterizerCanvasGLES3::draw_lens_distortion_rect(const Rect2 &p_rect, float p_k1, float p_k2, const Vector2 &p_eye_center, float p_oversample) { + Vector2 half_size; + if (storage->frame.current_rt) { + half_size = Vector2(storage->frame.current_rt->width, storage->frame.current_rt->height); + } else { + half_size = OS::get_singleton()->get_window_size(); + } + half_size *= 0.5; + Vector2 offset((p_rect.position.x - half_size.x) / half_size.x, (p_rect.position.y - half_size.y) / half_size.y); + Vector2 scale(p_rect.size.x / half_size.x, p_rect.size.y / half_size.y); + + float aspect_ratio = p_rect.size.x / p_rect.size.y; + + // setup our lens shader + state.lens_shader.bind(); + state.lens_shader.set_uniform(LensDistortedShaderGLES3::OFFSET, offset); + state.lens_shader.set_uniform(LensDistortedShaderGLES3::SCALE, scale); + state.lens_shader.set_uniform(LensDistortedShaderGLES3::K1, p_k1); + state.lens_shader.set_uniform(LensDistortedShaderGLES3::K2, p_k2); + state.lens_shader.set_uniform(LensDistortedShaderGLES3::EYE_CENTER, p_eye_center); + state.lens_shader.set_uniform(LensDistortedShaderGLES3::UPSCALE, p_oversample); + state.lens_shader.set_uniform(LensDistortedShaderGLES3::ASPECT_RATIO, aspect_ratio); + + glBindBufferBase(GL_UNIFORM_BUFFER, 0, state.canvas_item_ubo); + glBindVertexArray(data.canvas_quad_array); + + // and draw + glDrawArrays(GL_TRIANGLE_FAN, 0, 4); + + glBindVertexArray(0); + glBindBufferBase(GL_UNIFORM_BUFFER, 0, 0); +} + void RasterizerCanvasGLES3::draw_window_margins(int *black_margin, RID *black_image) { Vector2 window_size = OS::get_singleton()->get_window_size(); @@ -1896,6 +2031,7 @@ void RasterizerCanvasGLES3::initialize() { { uint32_t poly_size = GLOBAL_DEF_RST("rendering/limits/buffers/canvas_polygon_buffer_size_kb", 128); + ProjectSettings::get_singleton()->set_custom_property_info("rendering/limits/buffers/canvas_polygon_buffer_size_kb", PropertyInfo(Variant::INT, "rendering/limits/buffers/canvas_polygon_buffer_size_kb", PROPERTY_HINT_RANGE, "0,256,1,or_greater")); poly_size *= 1024; //kb poly_size = MAX(poly_size, (2 + 2 + 4) * 4 * sizeof(float)); glGenBuffers(1, &data.polygon_buffer); @@ -1943,6 +2079,7 @@ void RasterizerCanvasGLES3::initialize() { glGenVertexArrays(1, &data.polygon_buffer_pointer_array); uint32_t index_size = GLOBAL_DEF_RST("rendering/limits/buffers/canvas_polygon_index_buffer_size_kb", 128); + ProjectSettings::get_singleton()->set_custom_property_info("rendering/limits/buffers/canvas_polygon_index_buffer_size_kb", PropertyInfo(Variant::INT, "rendering/limits/buffers/canvas_polygon_index_buffer_size_kb", PROPERTY_HINT_RANGE, "0,256,1,or_greater")); index_size *= 1024; //kb glGenBuffers(1, &data.polygon_index_buffer); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, data.polygon_index_buffer); @@ -1960,6 +2097,7 @@ void RasterizerCanvasGLES3::initialize() { state.canvas_shader.init(); state.canvas_shader.set_base_material_tex_index(2); state.canvas_shadow_shader.init(); + state.lens_shader.init(); state.canvas_shader.set_conditional(CanvasShaderGLES3::USE_RGBA_SHADOWS, storage->config.use_rgba_2d_shadows); state.canvas_shadow_shader.set_conditional(CanvasShadowShaderGLES3::USE_RGBA_SHADOWS, storage->config.use_rgba_2d_shadows); diff --git a/drivers/gles3/rasterizer_canvas_gles3.h b/drivers/gles3/rasterizer_canvas_gles3.h index c7f2e54efb..3f306003b4 100644 --- a/drivers/gles3/rasterizer_canvas_gles3.h +++ b/drivers/gles3/rasterizer_canvas_gles3.h @@ -33,7 +33,9 @@ #include "rasterizer_storage_gles3.h" #include "servers/visual/rasterizer.h" + #include "shaders/canvas_shadow.glsl.gen.h" +#include "shaders/lens_distorted.glsl.gen.h" class RasterizerSceneGLES3; @@ -71,6 +73,7 @@ public: bool canvas_texscreen_used; CanvasShaderGLES3 canvas_shader; CanvasShadowShaderGLES3 canvas_shadow_shader; + LensDistortedShaderGLES3 lens_shader; bool using_texture_rect; bool using_ninepatch; @@ -140,6 +143,7 @@ public: virtual void reset_canvas(); void draw_generic_textured_rect(const Rect2 &p_rect, const Rect2 &p_src); + void draw_lens_distortion_rect(const Rect2 &p_rect, float p_k1, float p_k2, const Vector2 &p_eye_center, float p_oversample); void initialize(); void finalize(); diff --git a/drivers/gles3/rasterizer_gles3.cpp b/drivers/gles3/rasterizer_gles3.cpp index cb17695c5f..8242d214d3 100644 --- a/drivers/gles3/rasterizer_gles3.cpp +++ b/drivers/gles3/rasterizer_gles3.cpp @@ -30,10 +30,10 @@ #include "rasterizer_gles3.h" -#include "gl_context/context_gl.h" -#include "os/os.h" -#include "project_settings.h" -#include <string.h> +#include "core/os/os.h" +#include "core/project_settings.h" +#include "drivers/gl_context/context_gl.h" + RasterizerStorage *RasterizerGLES3::get_storage() { return storage; @@ -73,12 +73,12 @@ RasterizerScene *RasterizerGLES3::get_scene() { #define _EXT_DEBUG_SEVERITY_LOW_ARB 0x9148 #define _EXT_DEBUG_OUTPUT 0x92E0 -#if (defined WINDOWS_ENABLED) && !(defined UWP_ENABLED) -#define GLAPIENTRY APIENTRY -#else -#define GLAPIENTRY +#if defined(MINGW_ENABLED) || defined(_MSC_VER) +#define strcpy strcpy_s #endif +#ifdef GLAD_ENABLED +// Restricting to GLAD as only used in initialize() with GLAD_GL_ARB_debug_output static void GLAPIENTRY _gl_debug_print(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar *message, const GLvoid *userParam) { if (type == _EXT_DEBUG_TYPE_OTHER_ARB) @@ -123,6 +123,7 @@ static void GLAPIENTRY _gl_debug_print(GLenum source, GLenum type, GLuint id, GL ERR_PRINTS(output); } +#endif // GLAD_ENABLED typedef void (*DEBUGPROCARB)(GLenum source, GLenum type, @@ -134,30 +135,32 @@ typedef void (*DEBUGPROCARB)(GLenum source, typedef void (*DebugMessageCallbackARB)(DEBUGPROCARB callback, const void *userParam); -void RasterizerGLES3::initialize() { - - if (OS::get_singleton()->is_stdout_verbose()) { - print_line("Using GLES3 video driver"); - } +Error RasterizerGLES3::is_viable() { #ifdef GLAD_ENABLED if (!gladLoadGL()) { ERR_PRINT("Error initializing GLAD"); + return ERR_UNAVAILABLE; } // GLVersion seems to be used for both GL and GL ES, so we need different version checks for them #ifdef OPENGL_ENABLED // OpenGL 3.3 Core Profile required - if (GLVersion.major < 3 && GLVersion.minor < 3) { + if (GLVersion.major < 3 || (GLVersion.major == 3 && GLVersion.minor < 3)) { #else // OpenGL ES 3.0 if (GLVersion.major < 3) { #endif - ERR_PRINT("Your system's graphic drivers seem not to support OpenGL 3.3 / OpenGL ES 3.0, sorry :(\n" - "Try a drivers update, buy a new GPU or try software rendering on Linux; Godot will now crash with a segmentation fault."); - OS::get_singleton()->alert("Your system's graphic drivers seem not to support OpenGL 3.3 / OpenGL ES 3.0, sorry :(\n" - "Godot Engine will self-destruct as soon as you acknowledge this error message.", - "Fatal error: Insufficient OpenGL / GLES driver support"); + return ERR_UNAVAILABLE; } +#endif // GLAD_ENABLED + return OK; +} + +void RasterizerGLES3::initialize() { + + print_verbose("Using GLES3 video driver"); + +#ifdef GLAD_ENABLED if (OS::get_singleton()->is_stdout_verbose()) { if (GLAD_GL_ARB_debug_output) { glEnable(_EXT_DEBUG_OUTPUT_SYNCHRONOUS_ARB); @@ -167,7 +170,6 @@ void RasterizerGLES3::initialize() { print_line("OpenGL debugging not supported!"); } } - #endif // GLAD_ENABLED /* // For debugging @@ -358,6 +360,26 @@ void RasterizerGLES3::blit_render_target_to_screen(RID p_render_target, const Re #endif } +void RasterizerGLES3::output_lens_distorted_to_screen(RID p_render_target, const Rect2 &p_screen_rect, float p_k1, float p_k2, const Vector2 &p_eye_center, float p_oversample) { + ERR_FAIL_COND(storage->frame.current_rt); + + RasterizerStorageGLES3::RenderTarget *rt = storage->render_target_owner.getornull(p_render_target); + ERR_FAIL_COND(!rt); + + glDisable(GL_BLEND); + + // render to our framebuffer + glBindFramebuffer(GL_FRAMEBUFFER, RasterizerStorageGLES3::system_fbo); + + // output our texture + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, rt->color); + + canvas->draw_lens_distortion_rect(p_screen_rect, p_k1, p_k2, p_eye_center, p_oversample); + + glBindTexture(GL_TEXTURE_2D, 0); +} + void RasterizerGLES3::end_frame(bool p_swap_buffers) { if (OS::get_singleton()->is_layered_allowed()) { @@ -404,10 +426,10 @@ void RasterizerGLES3::make_current() { void RasterizerGLES3::register_config() { - GLOBAL_DEF("rendering/quality/filters/use_nearest_mipmap_filter", false); GLOBAL_DEF("rendering/quality/filters/anisotropic_filter_level", 4); ProjectSettings::get_singleton()->set_custom_property_info("rendering/quality/filters/anisotropic_filter_level", PropertyInfo(Variant::INT, "rendering/quality/filters/anisotropic_filter_level", PROPERTY_HINT_RANGE, "1,16,1")); GLOBAL_DEF("rendering/limits/time/time_rollover_secs", 3600); + ProjectSettings::get_singleton()->set_custom_property_info("rendering/limits/time/time_rollover_secs", PropertyInfo(Variant::REAL, "rendering/limits/time/time_rollover_secs", PROPERTY_HINT_RANGE, "0,10000,1,or_greater")); } RasterizerGLES3::RasterizerGLES3() { diff --git a/drivers/gles3/rasterizer_gles3.h b/drivers/gles3/rasterizer_gles3.h index f4449ac0f9..477e0dfd48 100644 --- a/drivers/gles3/rasterizer_gles3.h +++ b/drivers/gles3/rasterizer_gles3.h @@ -59,12 +59,16 @@ public: virtual void restore_render_target(); virtual void clear_render_target(const Color &p_color); virtual void blit_render_target_to_screen(RID p_render_target, const Rect2 &p_screen_rect, int p_screen = 0); + virtual void output_lens_distorted_to_screen(RID p_render_target, const Rect2 &p_screen_rect, float p_k1, float p_k2, const Vector2 &p_eye_center, float p_oversample); virtual void end_frame(bool p_swap_buffers); virtual void finalize(); + static Error is_viable(); static void make_current(); - static void register_config(); + + virtual bool is_low_end() const { return false; } + RasterizerGLES3(); ~RasterizerGLES3(); }; diff --git a/drivers/gles3/rasterizer_scene_gles3.cpp b/drivers/gles3/rasterizer_scene_gles3.cpp index eebdbe9493..ffe9e1c831 100644 --- a/drivers/gles3/rasterizer_scene_gles3.cpp +++ b/drivers/gles3/rasterizer_scene_gles3.cpp @@ -29,9 +29,10 @@ /*************************************************************************/ #include "rasterizer_scene_gles3.h" -#include "math_funcs.h" -#include "os/os.h" -#include "project_settings.h" + +#include "core/math/math_funcs.h" +#include "core/os/os.h" +#include "core/project_settings.h" #include "rasterizer_canvas_gles3.h" #include "servers/visual/visual_server_raster.h" @@ -50,26 +51,6 @@ static const GLenum _cube_side_enum[6] = { }; -static _FORCE_INLINE_ void store_transform2d(const Transform2D &p_mtx, float *p_array) { - - p_array[0] = p_mtx.elements[0][0]; - p_array[1] = p_mtx.elements[0][1]; - p_array[2] = 0; - p_array[3] = 0; - p_array[4] = p_mtx.elements[1][0]; - p_array[5] = p_mtx.elements[1][1]; - p_array[6] = 0; - p_array[7] = 0; - p_array[8] = 0; - p_array[9] = 0; - p_array[10] = 1; - p_array[11] = 0; - p_array[12] = p_mtx.elements[2][0]; - p_array[13] = p_mtx.elements[2][1]; - p_array[14] = 0; - p_array[15] = 1; -} - static _FORCE_INLINE_ void store_transform(const Transform &p_mtx, float *p_array) { p_array[0] = p_mtx.basis.elements[0][0]; p_array[1] = p_mtx.basis.elements[1][0]; @@ -672,7 +653,7 @@ bool RasterizerSceneGLES3::reflection_probe_instance_begin_render(RID p_instance int best_free = -1; int best_used = -1; - uint64_t best_used_frame; + uint64_t best_used_frame = 0; for (int i = 0; i < reflection_atlas->reflections.size(); i++) { if (reflection_atlas->reflections[i].owner == RID()) { @@ -865,7 +846,7 @@ void RasterizerSceneGLES3::environment_set_dof_blur_near(RID p_env, bool p_enabl env->dof_blur_near_amount = p_amount; env->dof_blur_near_quality = p_quality; } -void RasterizerSceneGLES3::environment_set_glow(RID p_env, bool p_enable, int p_level_flags, float p_intensity, float p_strength, float p_bloom_threshold, VS::EnvironmentGlowBlendMode p_blend_mode, float p_hdr_bleed_threshold, float p_hdr_bleed_scale, bool p_bicubic_upscale) { +void RasterizerSceneGLES3::environment_set_glow(RID p_env, bool p_enable, int p_level_flags, float p_intensity, float p_strength, float p_bloom_threshold, VS::EnvironmentGlowBlendMode p_blend_mode, float p_hdr_bleed_threshold, float p_hdr_bleed_scale, float p_hdr_luminance_cap, bool p_bicubic_upscale) { Environment *env = environment_owner.getornull(p_env); ERR_FAIL_COND(!env); @@ -878,6 +859,7 @@ void RasterizerSceneGLES3::environment_set_glow(RID p_env, bool p_enable, int p_ env->glow_blend_mode = p_blend_mode; env->glow_hdr_bleed_threshold = p_hdr_bleed_threshold; env->glow_hdr_bleed_scale = p_hdr_bleed_scale; + env->glow_hdr_luminance_cap = p_hdr_luminance_cap; env->glow_bicubic_upscale = p_bicubic_upscale; } void RasterizerSceneGLES3::environment_set_fog(RID p_env, bool p_enable, float p_begin, float p_end, RID p_gradient_texture) { @@ -953,13 +935,14 @@ void RasterizerSceneGLES3::environment_set_fog(RID p_env, bool p_enable, const C env->fog_sun_amount = p_sun_amount; } -void RasterizerSceneGLES3::environment_set_fog_depth(RID p_env, bool p_enable, float p_depth_begin, float p_depth_curve, bool p_transmit, float p_transmit_curve) { +void RasterizerSceneGLES3::environment_set_fog_depth(RID p_env, bool p_enable, float p_depth_begin, float p_depth_end, float p_depth_curve, bool p_transmit, float p_transmit_curve) { Environment *env = environment_owner.getornull(p_env); ERR_FAIL_COND(!env); env->fog_depth_enabled = p_enable; env->fog_depth_begin = p_depth_begin; + env->fog_depth_end = p_depth_end; env->fog_depth_curve = p_depth_curve; env->fog_transmit_enabled = p_transmit; env->fog_transmit_curve = p_transmit_curve; @@ -1008,7 +991,10 @@ RID RasterizerSceneGLES3::light_instance_create(RID p_light) { light_instance->light = p_light; light_instance->light_ptr = storage->light_owner.getornull(p_light); - ERR_FAIL_COND_V(!light_instance->light_ptr, RID()); + if (!light_instance->light_ptr) { + memdelete(light_instance); + ERR_FAIL_COND_V(!light_instance->light_ptr, RID()); + } light_instance->self = light_instance_owner.make_rid(light_instance); @@ -1198,18 +1184,19 @@ bool RasterizerSceneGLES3::_setup_material(RasterizerStorageGLES3::Material *p_m glActiveTexture(GL_TEXTURE0 + i); - GLenum target; + GLenum target = GL_TEXTURE_2D; GLuint tex = 0; RasterizerStorageGLES3::Texture *t = storage->texture_owner.getptr(textures[i]); if (t) { + t = t->get_ptr(); //resolve for proxies + if (t->redraw_if_visible) { //must check before proxy because this is often used with proxies VisualServerRaster::redraw_request(); } - t = t->get_ptr(); //resolve for proxies #ifdef TOOLS_ENABLED if (t->detect_3d) { t->detect_3d(t->detect_3d_ud); @@ -1262,14 +1249,11 @@ bool RasterizerSceneGLES3::_setup_material(RasterizerStorageGLES3::Material *p_m case ShaderLanguage::TYPE_SAMPLER3D: { target = GL_TEXTURE_3D; + tex = storage->resources.white_tex_3d; - switch (texture_hints[i]) { - - // TODO - default: { - tex = storage->resources.white_tex_3d; - } break; - } + //switch (texture_hints[i]) { + // TODO + //} } break; @@ -1278,6 +1262,8 @@ bool RasterizerSceneGLES3::_setup_material(RasterizerStorageGLES3::Material *p_m case ShaderLanguage::TYPE_SAMPLER2DARRAY: { // TODO } break; + + default: {} } } @@ -1445,7 +1431,16 @@ void RasterizerSceneGLES3::_setup_geometry(RenderList::Element *e, const Transfo if (particles->draw_order == VS::PARTICLES_DRAW_ORDER_VIEW_DEPTH && particles->particle_valid_histories[1]) { glBindBuffer(GL_ARRAY_BUFFER, particles->particle_buffer_histories[1]); //modify the buffer, this was used 2 frames ago so it should be good enough for flushing - RasterizerGLES3Particle *particle_array = (RasterizerGLES3Particle *)glMapBufferRange(GL_ARRAY_BUFFER, 0, particles->amount * 24 * sizeof(float), GL_MAP_READ_BIT | GL_MAP_WRITE_BIT); + RasterizerGLES3Particle *particle_array; +#ifndef __EMSCRIPTEN__ + particle_array = static_cast<RasterizerGLES3Particle *>(glMapBufferRange(GL_ARRAY_BUFFER, 0, particles->amount * 24 * sizeof(float), GL_MAP_READ_BIT | GL_MAP_WRITE_BIT)); +#else + PoolVector<RasterizerGLES3Particle> particle_vector; + particle_vector.resize(particles->amount); + PoolVector<RasterizerGLES3Particle>::Write w = particle_vector.write(); + particle_array = w.ptr(); + glGetBufferSubData(GL_ARRAY_BUFFER, 0, particles->amount * sizeof(RasterizerGLES3Particle), particle_array); +#endif SortArray<RasterizerGLES3Particle, RasterizerGLES3ParticleSort> sorter; @@ -1457,7 +1452,17 @@ void RasterizerSceneGLES3::_setup_geometry(RenderList::Element *e, const Transfo sorter.sort(particle_array, particles->amount); +#ifndef __EMSCRIPTEN__ glUnmapBuffer(GL_ARRAY_BUFFER); +#else + w = PoolVector<RasterizerGLES3Particle>::Write(); + particle_array = NULL; + { + PoolVector<RasterizerGLES3Particle>::Read r = particle_vector.read(); + glBufferSubData(GL_ARRAY_BUFFER, 0, particles->amount * sizeof(RasterizerGLES3Particle), r.ptr()); + } + particle_vector = PoolVector<RasterizerGLES3Particle>(); +#endif #ifdef DEBUG_ENABLED if (state.debug_draw == VS::VIEWPORT_DEBUG_DRAW_WIREFRAME && s->instancing_array_wireframe_id) { glBindVertexArray(s->instancing_array_wireframe_id); // use the wireframe instancing array ID @@ -1505,6 +1510,7 @@ void RasterizerSceneGLES3::_setup_geometry(RenderList::Element *e, const Transfo } } break; + default: {} } } @@ -1553,8 +1559,11 @@ void RasterizerSceneGLES3::_render_geometry(RenderList::Element *e) { RasterizerStorageGLES3::MultiMesh *multi_mesh = static_cast<RasterizerStorageGLES3::MultiMesh *>(e->owner); RasterizerStorageGLES3::Surface *s = static_cast<RasterizerStorageGLES3::Surface *>(e->geometry); - int amount = MAX(multi_mesh->size, multi_mesh->visible_instances); + int amount = MIN(multi_mesh->size, multi_mesh->visible_instances); + if (amount == -1) { + amount = multi_mesh->size; + } #ifdef DEBUG_ENABLED if (state.debug_draw == VS::VIEWPORT_DEBUG_DRAW_WIREFRAME && s->array_wireframe_id) { @@ -1823,6 +1832,7 @@ void RasterizerSceneGLES3::_render_geometry(RenderList::Element *e) { } } break; + default: {} } } @@ -2214,7 +2224,6 @@ void RasterizerSceneGLES3::_render_list(RenderList::Element **p_elements, int p_ _set_cull(e->sort_key & RenderList::SORT_KEY_MIRROR_FLAG, e->sort_key & RenderList::SORT_KEY_CULL_DISABLED_FLAG, p_reverse_cull); - state.scene_shader.set_uniform(SceneShaderGLES3::NORMAL_MULT, e->instance->mirror ? -1.0 : 1.0); state.scene_shader.set_uniform(SceneShaderGLES3::WORLD_TRANSFORM, e->instance->transform); _render_geometry(e); @@ -2360,14 +2369,15 @@ void RasterizerSceneGLES3::_add_geometry_with_material(RasterizerStorageGLES3::G e->sort_key |= uint64_t(e->geometry->index) << RenderList::SORT_KEY_GEOMETRY_INDEX_SHIFT; e->sort_key |= uint64_t(e->instance->base_type) << RenderList::SORT_KEY_GEOMETRY_TYPE_SHIFT; - if (!p_depth_pass) { + if (e->material->last_pass != render_pass) { + e->material->last_pass = render_pass; + e->material->index = current_material_index++; + } - if (e->material->last_pass != render_pass) { - e->material->last_pass = render_pass; - e->material->index = current_material_index++; - } + e->sort_key |= uint64_t(e->material->index) << RenderList::SORT_KEY_MATERIAL_INDEX_SHIFT; + e->sort_key |= uint64_t(e->instance->depth_layer) << RenderList::SORT_KEY_OPAQUE_DEPTH_LAYER_SHIFT; - e->sort_key |= uint64_t(e->material->index) << RenderList::SORT_KEY_MATERIAL_INDEX_SHIFT; + if (!p_depth_pass) { if (e->instance->gi_probe_instances.size()) { e->sort_key |= SORT_KEY_GI_PROBES_FLAG; @@ -2382,9 +2392,6 @@ void RasterizerSceneGLES3::_add_geometry_with_material(RasterizerStorageGLES3::G } e->sort_key |= uint64_t(p_material->render_priority + 128) << RenderList::SORT_KEY_PRIORITY_SHIFT; - } else { - e->sort_key |= uint64_t(e->instance->depth_layer) << RenderList::SORT_KEY_OPAQUE_DEPTH_LAYER_SHIFT; - e->sort_key |= uint64_t(e->material->index) << RenderList::SORT_KEY_MATERIAL_INDEX_SHIFT; } /* @@ -2530,7 +2537,7 @@ void RasterizerSceneGLES3::_draw_sky(RasterizerStorageGLES3::Sky *p_sky, const C storage->shaders.copy.set_conditional(CopyShaderGLES3::USE_PANORAMA, false); } -void RasterizerSceneGLES3::_setup_environment(Environment *env, const CameraMatrix &p_cam_projection, const Transform &p_cam_transform) { +void RasterizerSceneGLES3::_setup_environment(Environment *env, const CameraMatrix &p_cam_projection, const Transform &p_cam_transform, bool p_no_fog) { //store camera into ubo store_camera(p_cam_projection, state.ubo_data.projection_matrix); @@ -2581,7 +2588,8 @@ void RasterizerSceneGLES3::_setup_environment(Environment *env, const CameraMatr state.ubo_data.fog_color_enabled[0] = linear_fog.r; state.ubo_data.fog_color_enabled[1] = linear_fog.g; state.ubo_data.fog_color_enabled[2] = linear_fog.b; - state.ubo_data.fog_color_enabled[3] = env->fog_enabled ? 1.0 : 0.0; + state.ubo_data.fog_color_enabled[3] = (!p_no_fog && env->fog_enabled) ? 1.0 : 0.0; + state.ubo_data.fog_density = linear_fog.a; Color linear_sun = env->fog_sun_color.to_linear(); state.ubo_data.fog_sun_color_amount[0] = linear_sun.r; @@ -2590,6 +2598,7 @@ void RasterizerSceneGLES3::_setup_environment(Environment *env, const CameraMatr state.ubo_data.fog_sun_color_amount[3] = env->fog_sun_amount; state.ubo_data.fog_depth_enabled = env->fog_depth_enabled; state.ubo_data.fog_depth_begin = env->fog_depth_begin; + state.ubo_data.fog_depth_end = env->fog_depth_end; state.ubo_data.fog_depth_curve = env->fog_depth_curve; state.ubo_data.fog_transmit_enabled = env->fog_transmit_enabled; state.ubo_data.fog_transmit_curve = env->fog_transmit_curve; @@ -2734,7 +2743,7 @@ void RasterizerSceneGLES3::_setup_directional_light(int p_index, const Transform ubo_data.shadow_split_offsets[j] = li->shadow_transform[j].split; - Transform modelview = (p_camera_inverse_transform * li->shadow_transform[j].transform).inverse(); + Transform modelview = (p_camera_inverse_transform * li->shadow_transform[j].transform).affine_inverse(); CameraMatrix bias; bias.set_light_bias(); @@ -3031,19 +3040,17 @@ void RasterizerSceneGLES3::_setup_reflections(RID *p_reflection_probe_cull_resul reflection_ubo.ambient[3] = rpi->probe_ptr->interior_ambient_probe_contrib; } else { Color ambient_linear; - float contrib = 0; if (p_env) { ambient_linear = p_env->ambient_color.to_linear(); ambient_linear.r *= p_env->ambient_energy; ambient_linear.g *= p_env->ambient_energy; ambient_linear.b *= p_env->ambient_energy; - contrib = p_env->ambient_sky_contribution; } reflection_ubo.ambient[0] = ambient_linear.r; reflection_ubo.ambient[1] = ambient_linear.g; reflection_ubo.ambient[2] = ambient_linear.b; - reflection_ubo.ambient[3] = 0; + reflection_ubo.ambient[3] = 0; //not used in exterior mode, since it just blends with regular ambient light } int cell_size = reflection_atlas->size / reflection_atlas->subdiv; @@ -3094,40 +3101,6 @@ void RasterizerSceneGLES3::_copy_screen(bool p_invalidate_color, bool p_invalida glBindVertexArray(0); } -void RasterizerSceneGLES3::_copy_to_front_buffer(Environment *env) { - - //copy to front buffer - glBindFramebuffer(GL_FRAMEBUFFER, storage->frame.current_rt->fbo); - - glDepthMask(GL_FALSE); - glDisable(GL_DEPTH_TEST); - glDisable(GL_CULL_FACE); - glDisable(GL_BLEND); - glDepthFunc(GL_LEQUAL); - glColorMask(1, 1, 1, 1); - - glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, storage->frame.current_rt->buffers.diffuse); - - storage->shaders.copy.set_conditional(CopyShaderGLES3::DISABLE_ALPHA, true); - - if (!env) { - //no environment, simply convert from linear to srgb - storage->shaders.copy.set_conditional(CopyShaderGLES3::LINEAR_TO_SRGB, true); - } else { - /* FIXME: Why are both statements equal? */ - storage->shaders.copy.set_conditional(CopyShaderGLES3::LINEAR_TO_SRGB, true); - } - - storage->shaders.copy.bind(); - - _copy_screen(); - - //turn off everything used - storage->shaders.copy.set_conditional(CopyShaderGLES3::LINEAR_TO_SRGB, false); - storage->shaders.copy.set_conditional(CopyShaderGLES3::DISABLE_ALPHA, false); -} - void RasterizerSceneGLES3::_copy_texture_to_front_buffer(GLuint p_texture) { //copy to front buffer @@ -3161,7 +3134,6 @@ void RasterizerSceneGLES3::_fill_render_list(InstanceBase **p_cull_result, int p current_material_index = 0; state.used_sss = false; state.used_screen_texture = false; - //fill list for (int i = 0; i < p_cull_count; i++) { @@ -3239,6 +3211,7 @@ void RasterizerSceneGLES3::_fill_render_list(InstanceBase **p_cull_result, int p } } break; + default: {} } } } @@ -3624,7 +3597,7 @@ void RasterizerSceneGLES3::_post_process(Environment *env, const CameraMatrix &p glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0); } - if (!env || storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT]) { + if (!env || storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_TRANSPARENT] || storage->frame.current_rt->width < 4 || storage->frame.current_rt->height < 4) { //no post process on small render targets //no environment or transparent render, simply return and convert to SRGB glBindFramebuffer(GL_FRAMEBUFFER, storage->frame.current_rt->fbo); glActiveTexture(GL_TEXTURE0); @@ -3926,6 +3899,7 @@ void RasterizerSceneGLES3::_post_process(Environment *env, const CameraMatrix &p state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::PIXEL_SIZE, Vector2(1.0 / vp_w, 1.0 / vp_h)); state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::LOD, float(i)); state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::GLOW_STRENGTH, env->glow_strength); + state.effect_blur_shader.set_uniform(EffectBlurShaderGLES3::LUMINANCE_CAP, env->glow_hdr_luminance_cap); glActiveTexture(GL_TEXTURE0); if (i == 0) { @@ -3975,7 +3949,7 @@ void RasterizerSceneGLES3::_post_process(Environment *env, const CameraMatrix &p state.tonemap_shader.set_conditional(TonemapShaderGLES3::USE_FILMIC_TONEMAPPER, env->tone_mapper == VS::ENV_TONE_MAPPER_FILMIC); state.tonemap_shader.set_conditional(TonemapShaderGLES3::USE_ACES_TONEMAPPER, env->tone_mapper == VS::ENV_TONE_MAPPER_ACES); - state.tonemap_shader.set_conditional(TonemapShaderGLES3::USE_REINDHART_TONEMAPPER, env->tone_mapper == VS::ENV_TONE_MAPPER_REINHARDT); + state.tonemap_shader.set_conditional(TonemapShaderGLES3::USE_REINHARD_TONEMAPPER, env->tone_mapper == VS::ENV_TONE_MAPPER_REINHARD); state.tonemap_shader.set_conditional(TonemapShaderGLES3::KEEP_3D_LINEAR, storage->frame.current_rt->flags[RasterizerStorage::RENDER_TARGET_KEEP_3D_LINEAR]); state.tonemap_shader.set_conditional(TonemapShaderGLES3::USE_AUTO_EXPOSURE, env->auto_exposure); @@ -4062,7 +4036,7 @@ void RasterizerSceneGLES3::_post_process(Environment *env, const CameraMatrix &p state.tonemap_shader.set_conditional(TonemapShaderGLES3::USE_AUTO_EXPOSURE, false); state.tonemap_shader.set_conditional(TonemapShaderGLES3::USE_FILMIC_TONEMAPPER, false); state.tonemap_shader.set_conditional(TonemapShaderGLES3::USE_ACES_TONEMAPPER, false); - state.tonemap_shader.set_conditional(TonemapShaderGLES3::USE_REINDHART_TONEMAPPER, false); + state.tonemap_shader.set_conditional(TonemapShaderGLES3::USE_REINHARD_TONEMAPPER, false); state.tonemap_shader.set_conditional(TonemapShaderGLES3::USE_GLOW_LEVEL1, false); state.tonemap_shader.set_conditional(TonemapShaderGLES3::USE_GLOW_LEVEL2, false); state.tonemap_shader.set_conditional(TonemapShaderGLES3::USE_GLOW_LEVEL3, false); @@ -4127,7 +4101,7 @@ void RasterizerSceneGLES3::render_scene(const Transform &p_cam_transform, const state.ubo_data.screen_pixel_size[1] = 1.0 / storage->frame.current_rt->height; } - _setup_environment(env, p_cam_projection, p_cam_transform); + _setup_environment(env, p_cam_projection, p_cam_transform, p_reflection_probe.is_valid()); bool fb_cleared = false; @@ -4322,10 +4296,11 @@ void RasterizerSceneGLES3::render_scene(const Transform &p_cam_transform, const glClearBufferfv(GL_COLOR, 0, clear_color.components); // specular } + VS::EnvironmentBG bg_mode = (!env || (probe && env->bg_mode == VS::ENV_BG_CANVAS)) ? VS::ENV_BG_CLEAR_COLOR : env->bg_mode; //if no environment, or canvas while rendering a probe (invalid use case), use color. + if (env) { - switch (env->bg_mode) { + switch (bg_mode) { case VS::ENV_BG_COLOR_SKY: - case VS::ENV_BG_SKY: sky = storage->sky_owner.getornull(env->sky); @@ -4363,9 +4338,14 @@ void RasterizerSceneGLES3::render_scene(const Transform &p_cam_transform, const glEnable(GL_DEPTH_TEST); glEnable(GL_CULL_FACE); break; + default: {} } } + if (probe && probe->probe_ptr->interior) { + env_radiance_tex = 0; //for rendering probe interiors, radiance must not be used. + } + state.texscreen_copied = false; glBlendEquation(GL_FUNC_ADD); @@ -4531,7 +4511,7 @@ void RasterizerSceneGLES3::render_shadow(RID p_light, RID p_shadow_atlas, int p_ RasterizerStorageGLES3::Light *light = storage->light_owner.getornull(light_instance->light); ERR_FAIL_COND(!light); - uint32_t x, y, width, height, vp_height; + uint32_t x, y, width, height; float dp_direction = 0.0; float zfar = 0; @@ -4613,7 +4593,6 @@ void RasterizerSceneGLES3::render_shadow(RID p_light, RID p_shadow_atlas, int p_ bias = light->param[VS::LIGHT_PARAM_SHADOW_BIAS] * bias_mult; normal_bias = light->param[VS::LIGHT_PARAM_SHADOW_NORMAL_BIAS] * bias_mult; fbo = directional_shadow.fbo; - vp_height = directional_shadow.size; } else { //set from shadow atlas @@ -4623,7 +4602,6 @@ void RasterizerSceneGLES3::render_shadow(RID p_light, RID p_shadow_atlas, int p_ ERR_FAIL_COND(!shadow_atlas->shadow_owners.has(p_light)); fbo = shadow_atlas->fbo; - vp_height = shadow_atlas->size; uint32_t key = shadow_atlas->shadow_owners[p_light]; @@ -4901,10 +4879,7 @@ void RasterizerSceneGLES3::initialize() { glBindBuffer(GL_UNIFORM_BUFFER, 0); render_list.max_elements = GLOBAL_DEF_RST("rendering/limits/rendering/max_renderable_elements", (int)RenderList::DEFAULT_MAX_ELEMENTS); - if (render_list.max_elements > 1000000) - render_list.max_elements = 1000000; - if (render_list.max_elements < 1024) - render_list.max_elements = 1024; + ProjectSettings::get_singleton()->set_custom_property_info("rendering/limits/rendering/max_renderable_elements", PropertyInfo(Variant::INT, "rendering/limits/rendering/max_renderable_elements", PROPERTY_HINT_RANGE, "1024,1000000,1")); { //quad buffers @@ -5103,6 +5078,7 @@ void RasterizerSceneGLES3::initialize() { { uint32_t immediate_buffer_size = GLOBAL_DEF("rendering/limits/buffers/immediate_buffer_size_kb", 2048); + ProjectSettings::get_singleton()->set_custom_property_info("rendering/limits/buffers/immediate_buffer_size_kb", PropertyInfo(Variant::INT, "rendering/limits/buffers/immediate_buffer_size_kb", PROPERTY_HINT_RANGE, "0,8192,1,or_greater")); glGenBuffers(1, &state.immediate_buffer); glBindBuffer(GL_ARRAY_BUFFER, state.immediate_buffer); @@ -5177,13 +5153,13 @@ void RasterizerSceneGLES3::initialize() { void RasterizerSceneGLES3::iteration() { - shadow_filter_mode = ShadowFilterMode(int(ProjectSettings::get_singleton()->get("rendering/quality/shadows/filter_mode"))); - subsurface_scatter_follow_surface = ProjectSettings::get_singleton()->get("rendering/quality/subsurface_scattering/follow_surface"); - subsurface_scatter_weight_samples = ProjectSettings::get_singleton()->get("rendering/quality/subsurface_scattering/weight_samples"); - subsurface_scatter_quality = SubSurfaceScatterQuality(int(ProjectSettings::get_singleton()->get("rendering/quality/subsurface_scattering/quality"))); - subsurface_scatter_size = ProjectSettings::get_singleton()->get("rendering/quality/subsurface_scattering/scale"); + shadow_filter_mode = ShadowFilterMode(int(GLOBAL_GET("rendering/quality/shadows/filter_mode"))); + subsurface_scatter_follow_surface = GLOBAL_GET("rendering/quality/subsurface_scattering/follow_surface"); + subsurface_scatter_weight_samples = GLOBAL_GET("rendering/quality/subsurface_scattering/weight_samples"); + subsurface_scatter_quality = SubSurfaceScatterQuality(int(GLOBAL_GET("rendering/quality/subsurface_scattering/quality"))); + subsurface_scatter_size = GLOBAL_GET("rendering/quality/subsurface_scattering/scale"); - state.scene_shader.set_conditional(SceneShaderGLES3::VCT_QUALITY_HIGH, ProjectSettings::get_singleton()->get("rendering/quality/voxel_cone_tracing/high_quality")); + state.scene_shader.set_conditional(SceneShaderGLES3::VCT_QUALITY_HIGH, GLOBAL_GET("rendering/quality/voxel_cone_tracing/high_quality")); } void RasterizerSceneGLES3::finalize() { diff --git a/drivers/gles3/rasterizer_scene_gles3.h b/drivers/gles3/rasterizer_scene_gles3.h index cf387a69bc..1d84fb487b 100644 --- a/drivers/gles3/rasterizer_scene_gles3.h +++ b/drivers/gles3/rasterizer_scene_gles3.h @@ -145,6 +145,8 @@ public: uint32_t fog_depth_enabled; float fog_depth_begin; + float fog_depth_end; + float fog_density; float fog_depth_curve; uint32_t fog_transmit_enabled; float fog_transmit_curve; @@ -402,6 +404,7 @@ public: VS::EnvironmentGlowBlendMode glow_blend_mode; float glow_hdr_bleed_threshold; float glow_hdr_bleed_scale; + float glow_hdr_luminance_cap; bool glow_bicubic_upscale; VS::EnvironmentToneMapper tone_mapper; @@ -438,6 +441,7 @@ public: bool fog_depth_enabled; float fog_depth_begin; + float fog_depth_end; float fog_depth_curve; bool fog_transmit_enabled; float fog_transmit_curve; @@ -446,87 +450,77 @@ public: float fog_height_max; float fog_height_curve; - Environment() { - bg_mode = VS::ENV_BG_CLEAR_COLOR; - sky_custom_fov = 0.0; - bg_energy = 1.0; - sky_ambient = 0; - ambient_energy = 1.0; - ambient_sky_contribution = 0.0; - canvas_max_layer = 0; - - ssr_enabled = false; - ssr_max_steps = 64; - ssr_fade_in = 0.15; - ssr_fade_out = 2.0; - ssr_depth_tolerance = 0.2; - ssr_roughness = true; - - ssao_enabled = false; - ssao_intensity = 1.0; - ssao_radius = 1.0; - ssao_intensity2 = 1.0; - ssao_radius2 = 0.0; - ssao_bias = 0.01; - ssao_light_affect = 0; - ssao_ao_channel_affect = 0; - ssao_filter = VS::ENV_SSAO_BLUR_3x3; - ssao_quality = VS::ENV_SSAO_QUALITY_LOW; - ssao_bilateral_sharpness = 4; - - tone_mapper = VS::ENV_TONE_MAPPER_LINEAR; - tone_mapper_exposure = 1.0; - tone_mapper_exposure_white = 1.0; - auto_exposure = false; - auto_exposure_speed = 0.5; - auto_exposure_min = 0.05; - auto_exposure_max = 8; - auto_exposure_grey = 0.4; - - glow_enabled = false; - glow_levels = (1 << 2) | (1 << 4); - glow_intensity = 0.8; - glow_strength = 1.0; - glow_bloom = 0.0; - glow_blend_mode = VS::GLOW_BLEND_MODE_SOFTLIGHT; - glow_hdr_bleed_threshold = 1.0; - glow_hdr_bleed_scale = 2.0; - glow_bicubic_upscale = false; - - dof_blur_far_enabled = false; - dof_blur_far_distance = 10; - dof_blur_far_transition = 5; - dof_blur_far_amount = 0.1; - dof_blur_far_quality = VS::ENV_DOF_BLUR_QUALITY_MEDIUM; - - dof_blur_near_enabled = false; - dof_blur_near_distance = 2; - dof_blur_near_transition = 1; - dof_blur_near_amount = 0.1; - dof_blur_near_quality = VS::ENV_DOF_BLUR_QUALITY_MEDIUM; - - adjustments_enabled = false; - adjustments_brightness = 1.0; - adjustments_contrast = 1.0; - adjustments_saturation = 1.0; - - fog_enabled = false; - fog_color = Color(0.5, 0.5, 0.5); - fog_sun_color = Color(0.8, 0.8, 0.0); - fog_sun_amount = 0; - - fog_depth_enabled = true; - - fog_depth_begin = 10; - fog_depth_curve = 1; - - fog_transmit_enabled = true; - fog_transmit_curve = 1; - - fog_height_enabled = false; - fog_height_min = 0; - fog_height_max = 100; - fog_height_curve = 1; + Environment() : + bg_mode(VS::ENV_BG_CLEAR_COLOR), + sky_custom_fov(0.0), + bg_energy(1.0), + sky_ambient(0), + ambient_energy(1.0), + ambient_sky_contribution(0.0), + canvas_max_layer(0), + ssr_enabled(false), + ssr_max_steps(64), + ssr_fade_in(0.15), + ssr_fade_out(2.0), + ssr_depth_tolerance(0.2), + ssr_roughness(true), + ssao_enabled(false), + ssao_intensity(1.0), + ssao_radius(1.0), + ssao_intensity2(1.0), + ssao_radius2(0.0), + ssao_bias(0.01), + ssao_light_affect(0), + ssao_ao_channel_affect(0), + ssao_quality(VS::ENV_SSAO_QUALITY_LOW), + ssao_bilateral_sharpness(4), + ssao_filter(VS::ENV_SSAO_BLUR_3x3), + glow_enabled(false), + glow_levels((1 << 2) | (1 << 4)), + glow_intensity(0.8), + glow_strength(1.0), + glow_bloom(0.0), + glow_blend_mode(VS::GLOW_BLEND_MODE_SOFTLIGHT), + glow_hdr_bleed_threshold(1.0), + glow_hdr_bleed_scale(2.0), + glow_hdr_luminance_cap(12.0), + glow_bicubic_upscale(false), + tone_mapper(VS::ENV_TONE_MAPPER_LINEAR), + tone_mapper_exposure(1.0), + tone_mapper_exposure_white(1.0), + auto_exposure(false), + auto_exposure_speed(0.5), + auto_exposure_min(0.05), + auto_exposure_max(8), + auto_exposure_grey(0.4), + dof_blur_far_enabled(false), + dof_blur_far_distance(10), + dof_blur_far_transition(5), + dof_blur_far_amount(0.1), + dof_blur_far_quality(VS::ENV_DOF_BLUR_QUALITY_MEDIUM), + dof_blur_near_enabled(false), + dof_blur_near_distance(2), + dof_blur_near_transition(1), + dof_blur_near_amount(0.1), + dof_blur_near_quality(VS::ENV_DOF_BLUR_QUALITY_MEDIUM), + adjustments_enabled(false), + adjustments_brightness(1.0), + adjustments_contrast(1.0), + adjustments_saturation(1.0), + fog_enabled(false), + fog_color(Color(0.5, 0.5, 0.5)), + fog_sun_color(Color(0.8, 0.8, 0.0)), + fog_sun_amount(0), + fog_depth_enabled(true), + fog_depth_begin(10), + fog_depth_end(0), + fog_depth_curve(1), + fog_transmit_enabled(true), + fog_transmit_curve(1), + fog_height_enabled(false), + fog_height_min(0), + fog_height_max(100), + fog_height_curve(1) { } }; @@ -544,7 +538,7 @@ public: virtual void environment_set_dof_blur_near(RID p_env, bool p_enable, float p_distance, float p_transition, float p_amount, VS::EnvironmentDOFBlurQuality p_quality); virtual void environment_set_dof_blur_far(RID p_env, bool p_enable, float p_distance, float p_transition, float p_amount, VS::EnvironmentDOFBlurQuality p_quality); - virtual void environment_set_glow(RID p_env, bool p_enable, int p_level_flags, float p_intensity, float p_strength, float p_bloom_threshold, VS::EnvironmentGlowBlendMode p_blend_mode, float p_hdr_bleed_threshold, float p_hdr_bleed_scale, bool p_bicubic_upscale); + virtual void environment_set_glow(RID p_env, bool p_enable, int p_level_flags, float p_intensity, float p_strength, float p_bloom_threshold, VS::EnvironmentGlowBlendMode p_blend_mode, float p_hdr_bleed_threshold, float p_hdr_bleed_scale, float p_hdr_luminance_cap, bool p_bicubic_upscale); virtual void environment_set_fog(RID p_env, bool p_enable, float p_begin, float p_end, RID p_gradient_texture); virtual void environment_set_ssr(RID p_env, bool p_enable, int p_max_steps, float p_fade_in, float p_fade_out, float p_depth_tolerance, bool p_roughness); @@ -555,7 +549,7 @@ public: virtual void environment_set_adjustment(RID p_env, bool p_enable, float p_brightness, float p_contrast, float p_saturation, RID p_ramp); virtual void environment_set_fog(RID p_env, bool p_enable, const Color &p_color, const Color &p_sun_color, float p_sun_amount); - virtual void environment_set_fog_depth(RID p_env, bool p_enable, float p_depth_begin, float p_depth_curve, bool p_transmit, float p_transmit_curve); + virtual void environment_set_fog_depth(RID p_env, bool p_enable, float p_depth_begin, float p_depth_end, float p_depth_curve, bool p_transmit, float p_transmit_curve); virtual void environment_set_fog_height(RID p_env, bool p_enable, float p_min_height, float p_max_height, float p_height_curve); virtual bool is_environment(RID p_env); @@ -637,9 +631,9 @@ public: Vector3 bounds; Transform transform_to_data; - GIProbeInstance() { - probe = NULL; - tex_cache = 0; + GIProbeInstance() : + probe(NULL), + tex_cache(0) { } }; @@ -831,13 +825,12 @@ public: void _draw_sky(RasterizerStorageGLES3::Sky *p_sky, const CameraMatrix &p_projection, const Transform &p_transform, bool p_vflip, float p_custom_fov, float p_energy); - void _setup_environment(Environment *env, const CameraMatrix &p_cam_projection, const Transform &p_cam_transform); + void _setup_environment(Environment *env, const CameraMatrix &p_cam_projection, const Transform &p_cam_transform, bool p_no_fog = false); void _setup_directional_light(int p_index, const Transform &p_camera_inverse_transform, bool p_use_shadows); void _setup_lights(RID *p_light_cull_result, int p_light_cull_count, const Transform &p_camera_inverse_transform, const CameraMatrix &p_camera_projection, RID p_shadow_atlas); void _setup_reflections(RID *p_reflection_probe_cull_result, int p_reflection_probe_cull_count, const Transform &p_camera_inverse_transform, const CameraMatrix &p_camera_projection, RID p_reflection_atlas, Environment *p_env); void _copy_screen(bool p_invalidate_color = false, bool p_invalidate_depth = false); - void _copy_to_front_buffer(Environment *env); void _copy_texture_to_front_buffer(GLuint p_texture); //used for debug void _fill_render_list(InstanceBase **p_cull_result, int p_cull_count, bool p_depth_pass, bool p_shadow_pass); diff --git a/drivers/gles3/rasterizer_storage_gles3.cpp b/drivers/gles3/rasterizer_storage_gles3.cpp index a5c81d6c4d..2b038fcc0e 100644 --- a/drivers/gles3/rasterizer_storage_gles3.cpp +++ b/drivers/gles3/rasterizer_storage_gles3.cpp @@ -29,8 +29,8 @@ /*************************************************************************/ #include "rasterizer_storage_gles3.h" -#include "engine.h" -#include "project_settings.h" +#include "core/engine.h" +#include "core/project_settings.h" #include "rasterizer_canvas_gles3.h" #include "rasterizer_scene_gles3.h" @@ -101,6 +101,28 @@ #define _EXT_COMPRESSED_RGB_BPTC_SIGNED_FLOAT 0x8E8E #define _EXT_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT 0x8E8F +#ifdef __EMSCRIPTEN__ +#include <emscripten/emscripten.h> + +void glGetBufferSubData(GLenum target, GLintptr offset, GLsizeiptr size, GLvoid *data) { + + /* clang-format off */ + EM_ASM({ + GLctx.getBufferSubData($0, $1, HEAPU8, $2, $3); + }, target, offset, data, size); + /* clang-format on */ +} + +void glBufferSubData(GLenum target, GLintptr offset, GLsizeiptr size, const GLvoid *data) { + + /* clang-format off */ + EM_ASM({ + GLctx.bufferSubData($0, $1, HEAPU8, $2, $3); + }, target, offset, data, size); + /* clang-format on */ +} +#endif + void glTexStorage2DCustom(GLenum target, GLsizei levels, GLenum internalformat, GLsizei width, GLsizei height, GLenum format, GLenum type) { #ifdef GLES_OVER_GL @@ -118,10 +140,11 @@ void glTexStorage2DCustom(GLenum target, GLsizei levels, GLenum internalformat, GLuint RasterizerStorageGLES3::system_fbo = 0; -Ref<Image> RasterizerStorageGLES3::_get_gl_image_and_format(const Ref<Image> &p_image, Image::Format p_format, uint32_t p_flags, GLenum &r_gl_format, GLenum &r_gl_internal_format, GLenum &r_gl_type, bool &r_compressed, bool &srgb) { +Ref<Image> RasterizerStorageGLES3::_get_gl_image_and_format(const Ref<Image> &p_image, Image::Format p_format, uint32_t p_flags, Image::Format &r_real_format, GLenum &r_gl_format, GLenum &r_gl_internal_format, GLenum &r_gl_type, bool &r_compressed, bool &srgb) const { r_compressed = false; r_gl_format = 0; + r_real_format = p_format; Ref<Image> image = p_image; srgb = false; @@ -565,6 +588,7 @@ Ref<Image> RasterizerStorageGLES3::_get_gl_image_and_format(const Ref<Image> &p_ r_gl_internal_format = (config.srgb_decode_supported || (p_flags & VS::TEXTURE_FLAG_CONVERT_TO_LINEAR)) ? GL_SRGB8_ALPHA8 : GL_RGBA8; r_gl_type = GL_UNSIGNED_BYTE; r_compressed = false; + r_real_format = Image::FORMAT_RGBA8; srgb = true; return image; @@ -638,7 +662,8 @@ void RasterizerStorageGLES3::texture_allocate(RID p_texture, int p_width, int p_ } break; } - _get_gl_image_and_format(Ref<Image>(), texture->format, texture->flags, format, internal_format, type, compressed, srgb); + Image::Format real_format; + _get_gl_image_and_format(Ref<Image>(), texture->format, texture->flags, real_format, format, internal_format, type, compressed, srgb); texture->alloc_width = texture->width; texture->alloc_height = texture->height; @@ -710,7 +735,8 @@ void RasterizerStorageGLES3::texture_set_data(RID p_texture, const Ref<Image> &p texture->images.write[p_layer] = p_image; } - Ref<Image> img = _get_gl_image_and_format(p_image, p_image->get_format(), texture->flags, format, internal_format, type, compressed, srgb); + Image::Format real_format; + Ref<Image> img = _get_gl_image_and_format(p_image, p_image->get_format(), texture->flags, real_format, format, internal_format, type, compressed, srgb); if (config.shrink_textures_x2 && (p_image->has_mipmaps() || !p_image->is_compressed()) && !(texture->flags & VS::TEXTURE_FLAG_USED_FOR_STREAMING)) { @@ -726,7 +752,7 @@ void RasterizerStorageGLES3::texture_set_data(RID p_texture, const Ref<Image> &p } }; - GLenum blit_target; + GLenum blit_target = GL_TEXTURE_2D; switch (texture->type) { case VS::TEXTURE_TYPE_2D: { @@ -848,8 +874,6 @@ void RasterizerStorageGLES3::texture_set_data(RID p_texture, const Ref<Image> &p int size, ofs; img->get_mipmap_offset_and_size(i, ofs, size); - //print_line("mipmap: "+itos(i)+" size: "+itos(size)+" w: "+itos(mm_w)+", h: "+itos(mm_h)); - if (texture->type == VS::TEXTURE_TYPE_2D || texture->type == VS::TEXTURE_TYPE_CUBEMAP) { if (texture->compressed) { @@ -941,9 +965,10 @@ void RasterizerStorageGLES3::texture_set_data_partial(RID p_texture, const Ref<I p_sub_img = p_image->get_rect(Rect2(src_x, src_y, src_w, src_h)); } - Ref<Image> img = _get_gl_image_and_format(p_sub_img, p_sub_img->get_format(), texture->flags, format, internal_format, type, compressed, srgb); + Image::Format real_format; + Ref<Image> img = _get_gl_image_and_format(p_sub_img, p_sub_img->get_format(), texture->flags, real_format, format, internal_format, type, compressed, srgb); - GLenum blit_target; + GLenum blit_target = GL_TEXTURE_2D; switch (texture->type) { case VS::TEXTURE_TYPE_2D: { @@ -1014,9 +1039,17 @@ Ref<Image> RasterizerStorageGLES3::texture_get_data(RID p_texture, int p_layer) #ifdef GLES_OVER_GL + Image::Format real_format; + GLenum gl_format; + GLenum gl_internal_format; + GLenum gl_type; + bool compressed; + bool srgb; + _get_gl_image_and_format(Ref<Image>(), texture->format, texture->flags, real_format, gl_format, gl_internal_format, gl_type, compressed, srgb); + PoolVector<uint8_t> data; - int data_size = Image::get_image_data_size(texture->alloc_width, texture->alloc_height, texture->format, texture->mipmaps > 1 ? -1 : 0); + int data_size = Image::get_image_data_size(texture->alloc_width, texture->alloc_height, real_format, texture->mipmaps > 1); data.resize(data_size * 2); //add some memory at the end, just in case for buggy drivers PoolVector<uint8_t>::Write wb = data.write(); @@ -1027,13 +1060,11 @@ Ref<Image> RasterizerStorageGLES3::texture_get_data(RID p_texture, int p_layer) glBindBuffer(GL_PIXEL_PACK_BUFFER, 0); - //print_line("GET FORMAT: " + Image::get_format_name(texture->format) + " mipmaps: " + itos(texture->mipmaps)); - for (int i = 0; i < texture->mipmaps; i++) { int ofs = 0; if (i > 0) { - ofs = Image::get_image_data_size(texture->alloc_width, texture->alloc_height, texture->format, i - 1); + ofs = Image::get_image_data_size(texture->alloc_width, texture->alloc_height, real_format, i - 1); } if (texture->compressed) { @@ -1059,7 +1090,7 @@ Ref<Image> RasterizerStorageGLES3::texture_get_data(RID p_texture, int p_layer) uint32_t *ptr = (uint32_t *)wb.ptr(); uint32_t num_pixels = data_size / 4; - for (int ofs = 0; ofs < num_pixels; ofs++) { + for (uint32_t ofs = 0; ofs < num_pixels; ofs++) { uint32_t px = ptr[ofs]; uint32_t a = px >> 30 & 0xFF; @@ -1069,7 +1100,7 @@ Ref<Image> RasterizerStorageGLES3::texture_get_data(RID p_texture, int p_layer) (a | a << 2 | a << 4 | a << 6) << 24; } } else { - img_format = texture->format; + img_format = real_format; } wb = PoolVector<uint8_t>::Write(); @@ -1081,8 +1112,76 @@ Ref<Image> RasterizerStorageGLES3::texture_get_data(RID p_texture, int p_layer) return Ref<Image>(img); #else - ERR_EXPLAIN("Sorry, It's not possible to obtain images back in OpenGL ES"); - return Ref<Image>(); + Image::Format real_format; + GLenum gl_format; + GLenum gl_internal_format; + GLenum gl_type; + bool compressed; + bool srgb; + _get_gl_image_and_format(Ref<Image>(), texture->format, texture->flags, real_format, gl_format, gl_internal_format, gl_type, compressed, srgb); + + PoolVector<uint8_t> data; + + int data_size = Image::get_image_data_size(texture->alloc_width, texture->alloc_height, Image::FORMAT_RGBA8, false); + + data.resize(data_size * 2); //add some memory at the end, just in case for buggy drivers + PoolVector<uint8_t>::Write wb = data.write(); + + GLuint temp_framebuffer; + glGenFramebuffers(1, &temp_framebuffer); + + GLuint temp_color_texture; + glGenTextures(1, &temp_color_texture); + + glBindFramebuffer(GL_FRAMEBUFFER, temp_framebuffer); + + glBindTexture(GL_TEXTURE_2D, temp_color_texture); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texture->alloc_width, texture->alloc_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); + + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, temp_color_texture, 0); + + glDepthMask(GL_FALSE); + glDisable(GL_DEPTH_TEST); + glDisable(GL_CULL_FACE); + glDisable(GL_BLEND); + glDepthFunc(GL_LEQUAL); + glColorMask(1, 1, 1, 1); + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, texture->tex_id); + + glViewport(0, 0, texture->alloc_width, texture->alloc_height); + + shaders.copy.bind(); + + shaders.copy.set_conditional(CopyShaderGLES3::LINEAR_TO_SRGB, !srgb); + + glClearColor(0.0, 0.0, 0.0, 0.0); + glClear(GL_COLOR_BUFFER_BIT); + glBindVertexArray(resources.quadie_array); + glDrawArrays(GL_TRIANGLE_FAN, 0, 4); + glBindVertexArray(0); + + glReadPixels(0, 0, texture->alloc_width, texture->alloc_height, GL_RGBA, GL_UNSIGNED_BYTE, &wb[0]); + + shaders.copy.set_conditional(CopyShaderGLES3::LINEAR_TO_SRGB, false); + + glDeleteTextures(1, &temp_color_texture); + + glBindFramebuffer(GL_FRAMEBUFFER, 0); + glDeleteFramebuffers(1, &temp_framebuffer); + + wb = PoolVector<uint8_t>::Write(); + + data.resize(data_size); + + Image *img = memnew(Image(texture->alloc_width, texture->alloc_height, false, Image::FORMAT_RGBA8, data)); + if (!texture->compressed) { + img->convert(real_format); + } + + return Ref<Image>(img); #endif } @@ -1798,6 +1897,10 @@ void RasterizerStorageGLES3::_update_shader(Shader *p_shader) const { p_shader->uniforms.clear(); + if (p_shader->code == String()) { + return; //just invalid, but no error + } + ShaderCompilerGLES3::GeneratedCode gen_code; ShaderCompilerGLES3::IdentifierActions *actions = NULL; @@ -1892,6 +1995,7 @@ void RasterizerStorageGLES3::_update_shader(Shader *p_shader) const { actions = &shaders.actions_particles; actions->uniforms = &p_shader->uniforms; } break; + case VS::SHADER_MAX: break; // Can't happen, but silences warning } Error err = shaders.compiler.compile(p_shader->mode, p_shader->code, actions, p_shader->path, gen_code); @@ -2015,6 +2119,14 @@ void RasterizerStorageGLES3::shader_get_param_list(RID p_shader, List<PropertyIn pi.hint = PROPERTY_HINT_RESOURCE_TYPE; pi.hint_string = "Texture"; } break; + case ShaderLanguage::TYPE_SAMPLER2DARRAY: + case ShaderLanguage::TYPE_ISAMPLER2DARRAY: + case ShaderLanguage::TYPE_USAMPLER2DARRAY: { + + pi.type = Variant::OBJECT; + pi.hint = PROPERTY_HINT_RESOURCE_TYPE; + pi.hint_string = "TextureArray"; + } break; case ShaderLanguage::TYPE_SAMPLER3D: case ShaderLanguage::TYPE_ISAMPLER3D: case ShaderLanguage::TYPE_USAMPLER3D: { @@ -2126,6 +2238,20 @@ Variant RasterizerStorageGLES3::material_get_param(RID p_material, const StringN if (material->params.has(p_param)) return material->params[p_param]; + return material_get_param_default(p_material, p_param); +} + +Variant RasterizerStorageGLES3::material_get_param_default(RID p_material, const StringName &p_param) const { + const Material *material = material_owner.get(p_material); + ERR_FAIL_COND_V(!material, Variant()); + + if (material->shader) { + if (material->shader->uniforms.has(p_param)) { + ShaderLanguage::ShaderNode::Uniform uniform = material->shader->uniforms[p_param]; + Vector<ShaderLanguage::ConstantNode::Value> default_value = uniform.default_value; + return ShaderLanguage::constant_value_to_variant(default_value, uniform.type, uniform.hint); + } + } return Variant(); } @@ -2703,7 +2829,7 @@ void RasterizerStorageGLES3::_update_material(Material *material) { if (material->shader && material->shader->mode == VS::SHADER_SPATIAL) { if (material->shader->spatial.blend_mode == Shader::Spatial::BLEND_MODE_MIX && - (!material->shader->spatial.uses_alpha || (material->shader->spatial.uses_alpha && material->shader->spatial.depth_draw_mode == Shader::Spatial::DEPTH_DRAW_ALPHA_PREPASS))) { + (!material->shader->spatial.uses_alpha || material->shader->spatial.depth_draw_mode == Shader::Spatial::DEPTH_DRAW_ALPHA_PREPASS)) { can_cast_shadow = true; } @@ -2724,7 +2850,7 @@ void RasterizerStorageGLES3::_update_material(Material *material) { } for (Map<RasterizerScene::InstanceBase *, int>::Element *E = material->instance_owners.front(); E; E = E->next()) { - E->key()->base_material_changed(); + E->key()->base_changed(false, true); } } } @@ -2760,9 +2886,6 @@ void RasterizerStorageGLES3::_update_material(Material *material) { if (E->get().order < 0) continue; // texture, does not go here - //if (material->shader->mode == VS::SHADER_PARTICLES) { - // print_line("uniform " + String(E->key()) + " order " + itos(E->get().order) + " offset " + itos(material->shader->ubo_offsets[E->get().order])); - //} //regular uniform uint8_t *data = &local_ubo[material->shader->ubo_offsets[E->get().order]]; @@ -3350,7 +3473,7 @@ void RasterizerStorageGLES3::mesh_add_surface(RID p_mesh, uint32_t p_format, VS: } mesh->surfaces.push_back(surface); - mesh->instance_change_notify(); + mesh->instance_change_notify(true, false); info.vertex_mem += surface->total_data_size; } @@ -3423,7 +3546,7 @@ void RasterizerStorageGLES3::mesh_surface_set_material(RID p_mesh, int p_surface _material_add_geometry(mesh->surfaces[p_surface]->material, mesh->surfaces[p_surface]); } - mesh->instance_material_change_notify(); + mesh->instance_change_notify(false, true); } RID RasterizerStorageGLES3::mesh_surface_get_material(RID p_mesh, int p_surface) const { @@ -3459,21 +3582,26 @@ PoolVector<uint8_t> RasterizerStorageGLES3::mesh_surface_get_array(RID p_mesh, i Surface *surface = mesh->surfaces[p_surface]; - glBindBuffer(GL_ARRAY_BUFFER, surface->vertex_id); - void *data = glMapBufferRange(GL_ARRAY_BUFFER, 0, surface->array_byte_size, GL_MAP_READ_BIT); - - ERR_FAIL_COND_V(!data, PoolVector<uint8_t>()); - PoolVector<uint8_t> ret; ret.resize(surface->array_byte_size); + glBindBuffer(GL_ARRAY_BUFFER, surface->vertex_id); +#if defined(GLES_OVER_GL) || defined(__EMSCRIPTEN__) + { + PoolVector<uint8_t>::Write w = ret.write(); + glGetBufferSubData(GL_ARRAY_BUFFER, 0, surface->array_byte_size, w.ptr()); + } +#else + void *data = glMapBufferRange(GL_ARRAY_BUFFER, 0, surface->array_byte_size, GL_MAP_READ_BIT); + ERR_FAIL_NULL_V(data, PoolVector<uint8_t>()); { - PoolVector<uint8_t>::Write w = ret.write(); copymem(w.ptr(), data, surface->array_byte_size); } glUnmapBuffer(GL_ARRAY_BUFFER); +#endif + glBindBuffer(GL_ARRAY_BUFFER, 0); return ret; } @@ -3486,22 +3614,26 @@ PoolVector<uint8_t> RasterizerStorageGLES3::mesh_surface_get_index_array(RID p_m ERR_FAIL_COND_V(surface->index_array_len == 0, PoolVector<uint8_t>()); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, surface->index_id); - void *data = glMapBufferRange(GL_ELEMENT_ARRAY_BUFFER, 0, surface->index_array_byte_size, GL_MAP_READ_BIT); - - ERR_FAIL_COND_V(!data, PoolVector<uint8_t>()); - PoolVector<uint8_t> ret; ret.resize(surface->index_array_byte_size); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, surface->index_id); +#if defined(GLES_OVER_GL) || defined(__EMSCRIPTEN__) + { + PoolVector<uint8_t>::Write w = ret.write(); + glGetBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, surface->index_array_byte_size, w.ptr()); + } +#else + void *data = glMapBufferRange(GL_ELEMENT_ARRAY_BUFFER, 0, surface->index_array_byte_size, GL_MAP_READ_BIT); + ERR_FAIL_NULL_V(data, PoolVector<uint8_t>()); { - PoolVector<uint8_t>::Write w = ret.write(); copymem(w.ptr(), data, surface->index_array_byte_size); } - glUnmapBuffer(GL_ELEMENT_ARRAY_BUFFER); +#endif + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); return ret; } @@ -3542,23 +3674,26 @@ Vector<PoolVector<uint8_t> > RasterizerStorageGLES3::mesh_surface_get_blend_shap for (int i = 0; i < mesh->surfaces[p_surface]->blend_shapes.size(); i++) { - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mesh->surfaces[p_surface]->blend_shapes[i].vertex_id); - void *data = glMapBufferRange(GL_ELEMENT_ARRAY_BUFFER, 0, mesh->surfaces[p_surface]->array_byte_size, GL_MAP_READ_BIT); - - ERR_FAIL_COND_V(!data, Vector<PoolVector<uint8_t> >()); - PoolVector<uint8_t> ret; ret.resize(mesh->surfaces[p_surface]->array_byte_size); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mesh->surfaces[p_surface]->blend_shapes[i].vertex_id); +#if defined(GLES_OVER_GL) || defined(__EMSCRIPTEN__) + { + PoolVector<uint8_t>::Write w = ret.write(); + glGetBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, mesh->surfaces[p_surface]->array_byte_size, w.ptr()); + } +#else + void *data = glMapBufferRange(GL_ELEMENT_ARRAY_BUFFER, 0, mesh->surfaces[p_surface]->array_byte_size, GL_MAP_READ_BIT); + ERR_FAIL_COND_V(!data, Vector<PoolVector<uint8_t> >()); { - PoolVector<uint8_t>::Write w = ret.write(); copymem(w.ptr(), data, mesh->surfaces[p_surface]->array_byte_size); } + glUnmapBuffer(GL_ELEMENT_ARRAY_BUFFER); +#endif bsarr.push_back(ret); - - glUnmapBuffer(GL_ELEMENT_ARRAY_BUFFER); } return bsarr; @@ -3607,13 +3742,11 @@ void RasterizerStorageGLES3::mesh_remove_surface(RID p_mesh, int p_surface) { info.vertex_mem -= surface->total_data_size; - mesh->instance_material_change_notify(); - memdelete(surface); mesh->surfaces.remove(p_surface); - mesh->instance_change_notify(); + mesh->instance_change_notify(true, true); } int RasterizerStorageGLES3::mesh_get_surface_count(RID p_mesh) const { @@ -3629,7 +3762,7 @@ void RasterizerStorageGLES3::mesh_set_custom_aabb(RID p_mesh, const AABB &p_aabb ERR_FAIL_COND(!mesh); mesh->custom_aabb = p_aabb; - mesh->instance_change_notify(); + mesh->instance_change_notify(true, false); } AABB RasterizerStorageGLES3::mesh_get_custom_aabb(RID p_mesh) const { @@ -3645,12 +3778,14 @@ AABB RasterizerStorageGLES3::mesh_get_aabb(RID p_mesh, RID p_skeleton) const { Mesh *mesh = mesh_owner.get(p_mesh); ERR_FAIL_COND_V(!mesh, AABB()); - if (mesh->custom_aabb != AABB()) + if (mesh->custom_aabb != AABB()) { return mesh->custom_aabb; + } Skeleton *sk = NULL; - if (p_skeleton.is_valid()) + if (p_skeleton.is_valid()) { sk = skeleton_owner.get(p_skeleton); + } AABB aabb; @@ -3689,6 +3824,7 @@ AABB RasterizerStorageGLES3::mesh_get_aabb(RID p_mesh, RID p_skeleton) const { mtx.origin.y = texture[base_ofs + 3]; AABB baabb = mtx.xform(skbones[j]); + if (first) { laabb = baabb; first = false; @@ -4031,35 +4167,37 @@ void RasterizerStorageGLES3::multimesh_allocate(RID p_multimesh, int p_instances multimesh->data.resize(format_floats * p_instances); + float *dataptr = multimesh->data.ptrw(); + for (int i = 0; i < p_instances * format_floats; i += format_floats) { int color_from = 0; int custom_data_from = 0; if (multimesh->transform_format == VS::MULTIMESH_TRANSFORM_2D) { - multimesh->data.write[i + 0] = 1.0; - multimesh->data.write[i + 1] = 0.0; - multimesh->data.write[i + 2] = 0.0; - multimesh->data.write[i + 3] = 0.0; - multimesh->data.write[i + 4] = 0.0; - multimesh->data.write[i + 5] = 1.0; - multimesh->data.write[i + 6] = 0.0; - multimesh->data.write[i + 7] = 0.0; + dataptr[i + 0] = 1.0; + dataptr[i + 1] = 0.0; + dataptr[i + 2] = 0.0; + dataptr[i + 3] = 0.0; + dataptr[i + 4] = 0.0; + dataptr[i + 5] = 1.0; + dataptr[i + 6] = 0.0; + dataptr[i + 7] = 0.0; color_from = 8; custom_data_from = 8; } else { - multimesh->data.write[i + 0] = 1.0; - multimesh->data.write[i + 1] = 0.0; - multimesh->data.write[i + 2] = 0.0; - multimesh->data.write[i + 3] = 0.0; - multimesh->data.write[i + 4] = 0.0; - multimesh->data.write[i + 5] = 1.0; - multimesh->data.write[i + 6] = 0.0; - multimesh->data.write[i + 7] = 0.0; - multimesh->data.write[i + 8] = 0.0; - multimesh->data.write[i + 9] = 0.0; - multimesh->data.write[i + 10] = 1.0; - multimesh->data.write[i + 11] = 0.0; + dataptr[i + 0] = 1.0; + dataptr[i + 1] = 0.0; + dataptr[i + 2] = 0.0; + dataptr[i + 3] = 0.0; + dataptr[i + 4] = 0.0; + dataptr[i + 5] = 1.0; + dataptr[i + 6] = 0.0; + dataptr[i + 7] = 0.0; + dataptr[i + 8] = 0.0; + dataptr[i + 9] = 0.0; + dataptr[i + 10] = 1.0; + dataptr[i + 11] = 0.0; color_from = 12; custom_data_from = 12; } @@ -4074,14 +4212,14 @@ void RasterizerStorageGLES3::multimesh_allocate(RID p_multimesh, int p_instances } cu; cu.colu = 0xFFFFFFFF; - multimesh->data.write[i + color_from + 0] = cu.colf; + dataptr[i + color_from + 0] = cu.colf; custom_data_from = color_from + 1; } else if (multimesh->color_format == VS::MULTIMESH_COLOR_FLOAT) { - multimesh->data.write[i + color_from + 0] = 1.0; - multimesh->data.write[i + color_from + 1] = 1.0; - multimesh->data.write[i + color_from + 2] = 1.0; - multimesh->data.write[i + color_from + 3] = 1.0; + dataptr[i + color_from + 0] = 1.0; + dataptr[i + color_from + 1] = 1.0; + dataptr[i + color_from + 2] = 1.0; + dataptr[i + color_from + 3] = 1.0; custom_data_from = color_from + 4; } @@ -4095,13 +4233,13 @@ void RasterizerStorageGLES3::multimesh_allocate(RID p_multimesh, int p_instances } cu; cu.colu = 0; - multimesh->data.write[i + custom_data_from + 0] = cu.colf; + dataptr[i + custom_data_from + 0] = cu.colf; } else if (multimesh->custom_data_format == VS::MULTIMESH_CUSTOM_DATA_FLOAT) { - multimesh->data.write[i + custom_data_from + 0] = 0.0; - multimesh->data.write[i + custom_data_from + 1] = 0.0; - multimesh->data.write[i + custom_data_from + 2] = 0.0; - multimesh->data.write[i + custom_data_from + 3] = 0.0; + dataptr[i + custom_data_from + 0] = 0.0; + dataptr[i + custom_data_from + 1] = 0.0; + dataptr[i + custom_data_from + 2] = 0.0; + dataptr[i + custom_data_from + 3] = 0.0; } } @@ -4526,7 +4664,7 @@ void RasterizerStorageGLES3::update_dirty_multimeshes() { multimesh->dirty_aabb = false; multimesh->dirty_data = false; - multimesh->instance_change_notify(); + multimesh->instance_change_notify(true, false); multimesh_update_list.remove(multimesh_update_list.first()); } @@ -4637,7 +4775,7 @@ void RasterizerStorageGLES3::immediate_end(RID p_immediate) { im->building = false; - im->instance_change_notify(); + im->instance_change_notify(true, false); } void RasterizerStorageGLES3::immediate_clear(RID p_immediate) { @@ -4646,7 +4784,7 @@ void RasterizerStorageGLES3::immediate_clear(RID p_immediate) { ERR_FAIL_COND(im->building); im->chunks.clear(); - im->instance_change_notify(); + im->instance_change_notify(true, false); } AABB RasterizerStorageGLES3::immediate_get_aabb(RID p_immediate) const { @@ -4661,7 +4799,7 @@ void RasterizerStorageGLES3::immediate_set_material(RID p_immediate, RID p_mater Immediate *im = immediate_owner.get(p_immediate); ERR_FAIL_COND(!im); im->material = p_material; - im->instance_material_change_notify(); + im->instance_change_notify(false, true); } RID RasterizerStorageGLES3::immediate_get_material(RID p_immediate) const { @@ -4867,7 +5005,7 @@ void RasterizerStorageGLES3::update_dirty_skeletons() { } for (Set<RasterizerScene::InstanceBase *>::Element *E = skeleton->instances.front(); E; E = E->next()) { - E->get()->base_changed(); + E->get()->base_changed(true, false); } skeleton_update_list.remove(skeleton_update_list.first()); @@ -4933,8 +5071,9 @@ void RasterizerStorageGLES3::light_set_param(RID p_light, VS::LightParam p_param case VS::LIGHT_PARAM_SHADOW_BIAS: { light->version++; - light->instance_change_notify(); + light->instance_change_notify(true, false); } break; + default: {} } light->param[p_param] = p_value; @@ -4946,7 +5085,7 @@ void RasterizerStorageGLES3::light_set_shadow(RID p_light, bool p_enabled) { light->shadow = p_enabled; light->version++; - light->instance_change_notify(); + light->instance_change_notify(true, false); } void RasterizerStorageGLES3::light_set_shadow_color(RID p_light, const Color &p_color) { @@ -4979,7 +5118,7 @@ void RasterizerStorageGLES3::light_set_cull_mask(RID p_light, uint32_t p_mask) { light->cull_mask = p_mask; light->version++; - light->instance_change_notify(); + light->instance_change_notify(true, false); } void RasterizerStorageGLES3::light_set_reverse_cull_face_mode(RID p_light, bool p_enabled) { @@ -4988,6 +5127,9 @@ void RasterizerStorageGLES3::light_set_reverse_cull_face_mode(RID p_light, bool ERR_FAIL_COND(!light); light->reverse_cull = p_enabled; + + light->version++; + light->instance_change_notify(true, false); } void RasterizerStorageGLES3::light_omni_set_shadow_mode(RID p_light, VS::LightOmniShadowMode p_mode) { @@ -4998,7 +5140,7 @@ void RasterizerStorageGLES3::light_omni_set_shadow_mode(RID p_light, VS::LightOm light->omni_shadow_mode = p_mode; light->version++; - light->instance_change_notify(); + light->instance_change_notify(true, false); } VS::LightOmniShadowMode RasterizerStorageGLES3::light_omni_get_shadow_mode(RID p_light) { @@ -5016,7 +5158,7 @@ void RasterizerStorageGLES3::light_omni_set_shadow_detail(RID p_light, VS::Light light->omni_shadow_detail = p_detail; light->version++; - light->instance_change_notify(); + light->instance_change_notify(true, false); } void RasterizerStorageGLES3::light_directional_set_shadow_mode(RID p_light, VS::LightDirectionalShadowMode p_mode) { @@ -5026,7 +5168,7 @@ void RasterizerStorageGLES3::light_directional_set_shadow_mode(RID p_light, VS:: light->directional_shadow_mode = p_mode; light->version++; - light->instance_change_notify(); + light->instance_change_notify(true, false); } void RasterizerStorageGLES3::light_directional_set_blend_splits(RID p_light, bool p_enable) { @@ -5036,7 +5178,7 @@ void RasterizerStorageGLES3::light_directional_set_blend_splits(RID p_light, boo light->directional_blend_splits = p_enable; light->version++; - light->instance_change_notify(); + light->instance_change_notify(true, false); } bool RasterizerStorageGLES3::light_directional_get_blend_splits(RID p_light) const { @@ -5167,7 +5309,7 @@ void RasterizerStorageGLES3::reflection_probe_set_update_mode(RID p_probe, VS::R ERR_FAIL_COND(!reflection_probe); reflection_probe->update_mode = p_mode; - reflection_probe->instance_change_notify(); + reflection_probe->instance_change_notify(true, false); } void RasterizerStorageGLES3::reflection_probe_set_intensity(RID p_probe, float p_intensity) { @@ -5208,7 +5350,7 @@ void RasterizerStorageGLES3::reflection_probe_set_max_distance(RID p_probe, floa ERR_FAIL_COND(!reflection_probe); reflection_probe->max_distance = p_distance; - reflection_probe->instance_change_notify(); + reflection_probe->instance_change_notify(true, false); } void RasterizerStorageGLES3::reflection_probe_set_extents(RID p_probe, const Vector3 &p_extents) { @@ -5216,7 +5358,7 @@ void RasterizerStorageGLES3::reflection_probe_set_extents(RID p_probe, const Vec ERR_FAIL_COND(!reflection_probe); reflection_probe->extents = p_extents; - reflection_probe->instance_change_notify(); + reflection_probe->instance_change_notify(true, false); } void RasterizerStorageGLES3::reflection_probe_set_origin_offset(RID p_probe, const Vector3 &p_offset) { @@ -5224,7 +5366,7 @@ void RasterizerStorageGLES3::reflection_probe_set_origin_offset(RID p_probe, con ERR_FAIL_COND(!reflection_probe); reflection_probe->origin_offset = p_offset; - reflection_probe->instance_change_notify(); + reflection_probe->instance_change_notify(true, false); } void RasterizerStorageGLES3::reflection_probe_set_as_interior(RID p_probe, bool p_enable) { @@ -5248,7 +5390,7 @@ void RasterizerStorageGLES3::reflection_probe_set_enable_shadows(RID p_probe, bo ERR_FAIL_COND(!reflection_probe); reflection_probe->enable_shadows = p_enable; - reflection_probe->instance_change_notify(); + reflection_probe->instance_change_notify(true, false); } void RasterizerStorageGLES3::reflection_probe_set_cull_mask(RID p_probe, uint32_t p_layers) { @@ -5256,7 +5398,10 @@ void RasterizerStorageGLES3::reflection_probe_set_cull_mask(RID p_probe, uint32_ ERR_FAIL_COND(!reflection_probe); reflection_probe->cull_mask = p_layers; - reflection_probe->instance_change_notify(); + reflection_probe->instance_change_notify(true, false); +} + +void RasterizerStorageGLES3::reflection_probe_set_resolution(RID p_probe, int p_resolution) { } AABB RasterizerStorageGLES3::reflection_probe_get_aabb(RID p_probe) const { @@ -5341,7 +5486,7 @@ void RasterizerStorageGLES3::gi_probe_set_bounds(RID p_probe, const AABB &p_boun gip->bounds = p_bounds; gip->version++; - gip->instance_change_notify(); + gip->instance_change_notify(true, false); } AABB RasterizerStorageGLES3::gi_probe_get_bounds(RID p_probe) const { @@ -5358,7 +5503,7 @@ void RasterizerStorageGLES3::gi_probe_set_cell_size(RID p_probe, float p_size) { gip->cell_size = p_size; gip->version++; - gip->instance_change_notify(); + gip->instance_change_notify(true, false); } float RasterizerStorageGLES3::gi_probe_get_cell_size(RID p_probe) const { @@ -5391,7 +5536,7 @@ void RasterizerStorageGLES3::gi_probe_set_dynamic_data(RID p_probe, const PoolVe gip->dynamic_data = p_data; gip->version++; - gip->instance_change_notify(); + gip->instance_change_notify(true, false); } PoolVector<int> RasterizerStorageGLES3::gi_probe_get_dynamic_data(RID p_probe) const { @@ -5623,7 +5768,7 @@ void RasterizerStorageGLES3::lightmap_capture_set_bounds(RID p_capture, const AA LightmapCapture *capture = lightmap_capture_data_owner.getornull(p_capture); ERR_FAIL_COND(!capture); capture->bounds = p_bounds; - capture->instance_change_notify(); + capture->instance_change_notify(true, false); } AABB RasterizerStorageGLES3::lightmap_capture_get_bounds(RID p_capture) const { @@ -5644,7 +5789,7 @@ void RasterizerStorageGLES3::lightmap_capture_set_octree(RID p_capture, const Po PoolVector<uint8_t>::Read r = p_octree.read(); copymem(w.ptr(), r.ptr(), p_octree.size()); } - capture->instance_change_notify(); + capture->instance_change_notify(true, false); } PoolVector<uint8_t> RasterizerStorageGLES3::lightmap_capture_get_octree(RID p_capture) const { @@ -5867,7 +6012,7 @@ void RasterizerStorageGLES3::particles_set_custom_aabb(RID p_particles, const AA ERR_FAIL_COND(!particles); particles->custom_aabb = p_aabb; _particles_update_histories(particles); - particles->instance_change_notify(); + particles->instance_change_notify(true, false); } void RasterizerStorageGLES3::particles_set_speed_scale(RID p_particles, float p_scale) { @@ -5957,9 +6102,21 @@ AABB RasterizerStorageGLES3::particles_get_current_aabb(RID p_particles) { const Particles *particles = particles_owner.getornull(p_particles); ERR_FAIL_COND_V(!particles, AABB()); + const float *data; glBindBuffer(GL_ARRAY_BUFFER, particles->particle_buffers[0]); - float *data = (float *)glMapBufferRange(GL_ARRAY_BUFFER, 0, particles->amount * 16 * 6, GL_MAP_READ_BIT); +#if defined(GLES_OVER_GL) || defined(__EMSCRIPTEN__) + PoolVector<uint8_t> vector; + vector.resize(particles->amount * 16 * 6); + { + PoolVector<uint8_t>::Write w = vector.write(); + glGetBufferSubData(GL_ARRAY_BUFFER, 0, particles->amount * 16 * 6, w.ptr()); + } + PoolVector<uint8_t>::Read r = vector.read(); + data = reinterpret_cast<const float *>(r.ptr()); +#else + data = (float *)glMapBufferRange(GL_ARRAY_BUFFER, 0, particles->amount * 16 * 6, GL_MAP_READ_BIT); +#endif AABB aabb; Transform inv = particles->emission_transform.affine_inverse(); @@ -5976,7 +6133,13 @@ AABB RasterizerStorageGLES3::particles_get_current_aabb(RID p_particles) { aabb.expand_to(pos); } +#if defined(GLES_OVER_GL) || defined(__EMSCRIPTEN__) + r = PoolVector<uint8_t>::Read(); + vector = PoolVector<uint8_t>(); +#else glUnmapBuffer(GL_ARRAY_BUFFER); +#endif + glBindBuffer(GL_ARRAY_BUFFER, 0); float longest_axis = 0; @@ -6275,12 +6438,19 @@ void RasterizerStorageGLES3::update_particles() { particles->particle_valid_histories[0] = true; } - particles->instance_change_notify(); //make sure shadows are updated + particles->instance_change_notify(true, false); //make sure shadows are updated } glDisable(GL_RASTERIZER_DISCARD); } +bool RasterizerStorageGLES3::particles_is_inactive(RID p_particles) const { + + const Particles *particles = particles_owner.getornull(p_particles); + ERR_FAIL_COND_V(!particles, false); + return !particles->emitting && particles->inactive; +} + //////// void RasterizerStorageGLES3::instance_add_skeleton(RID p_skeleton, RasterizerScene::InstanceBase *p_instance) { @@ -6799,7 +6969,7 @@ void RasterizerStorageGLES3::_render_target_allocate(RenderTarget *rt) { glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, level); glDisable(GL_SCISSOR_TEST); glColorMask(1, 1, 1, 1); - if (rt->buffers.active == false) { + if (!rt->buffers.active) { glDepthMask(GL_TRUE); } @@ -6985,7 +7155,10 @@ RID RasterizerStorageGLES3::canvas_light_shadow_buffer_create(int p_width) { //printf("errnum: %x\n",status); glBindFramebuffer(GL_FRAMEBUFFER, RasterizerStorageGLES3::system_fbo); - ERR_FAIL_COND_V(status != GL_FRAMEBUFFER_COMPLETE, RID()); + if (status != GL_FRAMEBUFFER_COMPLETE) { + memdelete(cls); + ERR_FAIL_COND_V(status != GL_FRAMEBUFFER_COMPLETE, RID()); + } return canvas_light_shadow_owner.make_rid(cls); } @@ -7322,7 +7495,7 @@ bool RasterizerStorageGLES3::free(RID p_rid) { GIProbeData *gi_probe_data = gi_probe_data_owner.get(p_rid); glDeleteTextures(1, &gi_probe_data->tex_id); - gi_probe_owner.free(p_rid); + gi_probe_data_owner.free(p_rid); memdelete(gi_probe_data); } else if (lightmap_capture_data_owner.owns(p_rid)) { @@ -7330,7 +7503,7 @@ bool RasterizerStorageGLES3::free(RID p_rid) { LightmapCapture *lightmap_capture = lightmap_capture_data_owner.get(p_rid); lightmap_capture->instance_remove_deps(); - gi_probe_owner.free(p_rid); + lightmap_capture_data_owner.free(p_rid); memdelete(lightmap_capture); } else if (canvas_occluder_owner.owns(p_rid)) { @@ -7368,6 +7541,9 @@ bool RasterizerStorageGLES3::free(RID p_rid) { bool RasterizerStorageGLES3::has_os_feature(const String &p_feature) const { + if (p_feature == "bptc") + return config.bptc_supported; + if (p_feature == "s3tc") return config.s3tc_supported; @@ -7497,7 +7673,7 @@ void RasterizerStorageGLES3::initialize() { config.etc2_supported = true; config.hdr_supported = false; config.s3tc_supported = config.extensions.has("GL_EXT_texture_compression_dxt1") || config.extensions.has("GL_EXT_texture_compression_s3tc") || config.extensions.has("WEBGL_compressed_texture_s3tc"); - config.rgtc_supported = config.extensions.has("GL_EXT_texture_compression_rgtc") || config.extensions.has("GL_ARB_texture_compression_rgtc"); + config.rgtc_supported = config.extensions.has("GL_EXT_texture_compression_rgtc") || config.extensions.has("GL_ARB_texture_compression_rgtc") || config.extensions.has("EXT_texture_compression_rgtc"); #endif config.pvrtc_supported = config.extensions.has("GL_IMG_texture_compression_pvrtc"); @@ -7636,6 +7812,8 @@ void RasterizerStorageGLES3::initialize() { { //transform feedback buffers uint32_t xf_feedback_size = GLOBAL_DEF_RST("rendering/limits/buffers/blend_shape_max_buffer_size_kb", 4096); + ProjectSettings::get_singleton()->set_custom_property_info("rendering/limits/buffers/blend_shape_max_buffer_size_kb", PropertyInfo(Variant::INT, "rendering/limits/buffers/blend_shape_max_buffer_size_kb", PROPERTY_HINT_RANGE, "0,8192,1,or_greater")); + for (int i = 0; i < 2; i++) { glGenBuffers(1, &resources.transform_feedback_buffers[i]); diff --git a/drivers/gles3/rasterizer_storage_gles3.h b/drivers/gles3/rasterizer_storage_gles3.h index f55c8026ea..958086f6c7 100644 --- a/drivers/gles3/rasterizer_storage_gles3.h +++ b/drivers/gles3/rasterizer_storage_gles3.h @@ -31,17 +31,24 @@ #ifndef RASTERIZERSTORAGEGLES3_H #define RASTERIZERSTORAGEGLES3_H -#include "self_list.h" +#include "core/self_list.h" #include "servers/visual/rasterizer.h" #include "servers/visual/shader_language.h" #include "shader_compiler_gles3.h" #include "shader_gles3.h" + #include "shaders/blend_shape.glsl.gen.h" #include "shaders/canvas.glsl.gen.h" #include "shaders/copy.glsl.gen.h" #include "shaders/cubemap_filter.glsl.gen.h" #include "shaders/particles.glsl.gen.h" +// WebGL 2.0 has no MapBufferRange/UnmapBuffer, but offers a non-ES style BufferSubData API instead. +#ifdef __EMSCRIPTEN__ +void glGetBufferSubData(GLenum target, GLintptr offset, GLsizeiptr size, GLvoid *data); +void glBufferSubData(GLenum target, GLintptr offset, GLsizeiptr size, const GLvoid *data); +#endif + class RasterizerCanvasGLES3; class RasterizerSceneGLES3; @@ -174,22 +181,12 @@ public: SelfList<RasterizerScene::InstanceBase>::List instance_list; - _FORCE_INLINE_ void instance_change_notify() { - - SelfList<RasterizerScene::InstanceBase> *instances = instance_list.first(); - while (instances) { - - instances->self()->base_changed(); - instances = instances->next(); - } - } - - _FORCE_INLINE_ void instance_material_change_notify() { + _FORCE_INLINE_ void instance_change_notify(bool p_aabb, bool p_materials) { SelfList<RasterizerScene::InstanceBase> *instances = instance_list.first(); while (instances) { - instances->self()->base_material_changed(); + instances->self()->base_changed(p_aabb, p_materials); instances = instances->next(); } } @@ -288,29 +285,30 @@ public: VisualServer::TextureDetectCallback detect_normal; void *detect_normal_ud; - Texture() { - - using_srgb = false; - stored_cube_sides = 0; - ignore_mipmaps = false; - render_target = NULL; - flags = width = height = 0; - tex_id = 0; - data_size = 0; - format = Image::FORMAT_L8; - active = false; - compressed = false; - total_data_size = 0; - target = GL_TEXTURE_2D; - mipmaps = 0; - detect_3d = NULL; - detect_3d_ud = NULL; - detect_srgb = NULL; - detect_srgb_ud = NULL; - detect_normal = NULL; - detect_normal_ud = NULL; - proxy = NULL; - redraw_if_visible = false; + Texture() : + proxy(NULL), + flags(0), + width(0), + height(0), + format(Image::FORMAT_L8), + target(GL_TEXTURE_2D), + data_size(0), + compressed(false), + total_data_size(0), + ignore_mipmaps(false), + mipmaps(0), + active(false), + tex_id(0), + using_srgb(false), + redraw_if_visible(false), + stored_cube_sides(0), + render_target(NULL), + detect_3d(NULL), + detect_3d_ud(NULL), + detect_srgb(NULL), + detect_srgb_ud(NULL), + detect_normal(NULL), + detect_normal_ud(NULL) { } _ALWAYS_INLINE_ Texture *get_ptr() { @@ -340,7 +338,7 @@ public: mutable RID_Owner<Texture> texture_owner; - Ref<Image> _get_gl_image_and_format(const Ref<Image> &p_image, Image::Format p_format, uint32_t p_flags, GLenum &r_gl_format, GLenum &r_gl_internal_format, GLenum &r_gl_type, bool &r_compressed, bool &srgb); + Ref<Image> _get_gl_image_and_format(const Ref<Image> &p_image, Image::Format p_format, uint32_t p_flags, Image::Format &r_real_format, GLenum &r_gl_format, GLenum &r_gl_internal_format, GLenum &r_gl_type, bool &r_compressed, bool &srgb) const; virtual RID texture_create(); virtual void texture_allocate(RID p_texture, int p_width, int p_height, int p_depth_3d, Image::Format p_format, VS::TextureType p_type, uint32_t p_flags = VS::TEXTURE_FLAGS_DEFAULT); @@ -442,6 +440,7 @@ public: }; int light_mode; + bool uses_screen_texture; bool uses_screen_uv; bool uses_time; @@ -555,16 +554,16 @@ public: bool is_animated_cache; Material() : + shader(NULL), + ubo_id(0), + ubo_size(0), list(this), - dirty_list(this) { - can_cast_shadow_cache = false; - is_animated_cache = false; - shader = NULL; - line_width = 1.0; - ubo_id = 0; - ubo_size = 0; - last_pass = 0; - render_priority = 0; + dirty_list(this), + line_width(1.0), + render_priority(0), + last_pass(0), + can_cast_shadow_cache(false), + is_animated_cache(false) { } }; @@ -582,6 +581,7 @@ public: virtual void material_set_param(RID p_material, const StringName &p_param, const Variant &p_value); virtual Variant material_get_param(RID p_material, const StringName &p_param) const; + virtual Variant material_get_param_default(RID p_material, const StringName &p_param) const; virtual void material_set_line_width(RID p_material, float p_width); virtual void material_set_next_pass(RID p_material, RID p_next_material); @@ -656,40 +656,37 @@ public: bool active; virtual void material_changed_notify() { - mesh->instance_material_change_notify(); + mesh->instance_change_notify(false, true); mesh->update_multimeshes(); } int total_data_size; - Surface() { - - array_byte_size = 0; - index_array_byte_size = 0; - mesh = NULL; - format = 0; - array_id = 0; - vertex_id = 0; - index_id = 0; - array_len = 0; + Surface() : + mesh(NULL), + format(0), + array_id(0), + vertex_id(0), + index_id(0), + index_wireframe_id(0), + array_wireframe_id(0), + instancing_array_wireframe_id(0), + index_wireframe_len(0), + array_len(0), + index_array_len(0), + array_byte_size(0), + index_array_byte_size(0), + primitive(VS::PRIMITIVE_POINTS), + active(false), + total_data_size(0) { type = GEOMETRY_SURFACE; - primitive = VS::PRIMITIVE_POINTS; - index_array_len = 0; - active = false; - - total_data_size = 0; - - index_wireframe_id = 0; - array_wireframe_id = 0; - instancing_array_wireframe_id = 0; - index_wireframe_len = 0; } ~Surface() { } }; - class MultiMesh; + struct MultiMesh; struct Mesh : public GeometryOwner { @@ -704,16 +701,16 @@ public: SelfList<MultiMesh> *mm = multimeshes.first(); while (mm) { - mm->self()->instance_material_change_notify(); + mm->self()->instance_change_notify(false, true); mm = mm->next(); } } - Mesh() { - blend_shape_mode = VS::BLEND_SHAPE_MODE_NORMALIZED; - blend_shape_count = 0; - last_pass = 0; - active = false; + Mesh() : + active(false), + blend_shape_count(0), + blend_shape_mode(VS::BLEND_SHAPE_MODE_NORMALIZED), + last_pass(0) { } }; @@ -781,19 +778,19 @@ public: bool dirty_data; MultiMesh() : + size(0), + transform_format(VS::MULTIMESH_TRANSFORM_2D), + color_format(VS::MULTIMESH_COLOR_NONE), + custom_data_format(VS::MULTIMESH_CUSTOM_DATA_NONE), update_list(this), - mesh_list(this) { - dirty_aabb = true; - dirty_data = true; - xform_floats = 0; - color_floats = 0; - custom_data_floats = 0; - visible_instances = -1; - size = 0; - buffer = 0; - transform_format = VS::MULTIMESH_TRANSFORM_2D; - color_format = VS::MULTIMESH_COLOR_NONE; - custom_data_format = VS::MULTIMESH_CUSTOM_DATA_NONE; + mesh_list(this), + buffer(0), + visible_instances(-1), + xform_floats(0), + color_floats(0), + custom_data_floats(0), + dirty_aabb(true), + dirty_data(true) { } }; @@ -890,11 +887,10 @@ public: Transform2D base_transform_2d; Skeleton() : + use_2d(false), + size(0), + texture(0), update_list(this) { - size = 0; - - use_2d = false; - texture = 0; } }; @@ -1003,6 +999,7 @@ public: virtual void reflection_probe_set_enable_box_projection(RID p_probe, bool p_enable); virtual void reflection_probe_set_enable_shadows(RID p_probe, bool p_enable); virtual void reflection_probe_set_cull_mask(RID p_probe, uint32_t p_layers); + virtual void reflection_probe_set_resolution(RID p_probe, int p_resolution); virtual AABB reflection_probe_get_aabb(RID p_probe) const; virtual VS::ReflectionProbeUpdateMode reflection_probe_get_update_mode(RID p_probe) const; @@ -1174,37 +1171,31 @@ public: Transform emission_transform; Particles() : - particle_element(this) { - cycle_number = 0; - emitting = false; - one_shot = false; - amount = 0; - lifetime = 1.0; - pre_process_time = 0.0; - explosiveness = 0.0; - randomness = 0.0; - use_local_coords = true; - fixed_fps = 0; - fractional_delta = false; - frame_remainder = 0; - histories_enabled = false; - speed_scale = 1.0; - random_seed = 0; - - restart_request = false; - - custom_aabb = AABB(Vector3(-4, -4, -4), Vector3(8, 8, 8)); - - draw_order = VS::PARTICLES_DRAW_ORDER_INDEX; + inactive(true), + inactive_time(0.0), + emitting(false), + one_shot(false), + amount(0), + lifetime(1.0), + pre_process_time(0.0), + explosiveness(0.0), + randomness(0.0), + restart_request(false), + custom_aabb(AABB(Vector3(-4, -4, -4), Vector3(8, 8, 8))), + use_local_coords(true), + draw_order(VS::PARTICLES_DRAW_ORDER_INDEX), + histories_enabled(false), + particle_element(this), + prev_ticks(0), + random_seed(0), + cycle_number(0), + speed_scale(1.0), + fixed_fps(0), + fractional_delta(false), + frame_remainder(0), + clear(true) { particle_buffers[0] = 0; particle_buffers[1] = 0; - - prev_ticks = 0; - - clear = true; - inactive = true; - inactive_time = 0.0; - glGenBuffers(2, particle_buffers); glGenVertexArrays(2, particle_vaos); } @@ -1261,6 +1252,8 @@ public: virtual int particles_get_draw_passes(RID p_particles) const; virtual RID particles_get_draw_pass_mesh(RID p_particles, int p_pass) const; + virtual bool particles_is_inactive(RID p_particles) const; + /* INSTANCE */ virtual void instance_add_skeleton(RID p_skeleton, RasterizerScene::InstanceBase *p_instance); @@ -1307,9 +1300,9 @@ public: GLuint color; int levels; - MipMaps() { - color = 0; - levels = 0; + MipMaps() : + color(0), + levels(0) { } }; @@ -1324,10 +1317,10 @@ public: Vector<GLuint> depth_mipmap_fbos; //fbos for depth mipmapsla ver - SSAO() { + SSAO() : + linear_depth(0) { blur_fbo[0] = 0; blur_fbo[1] = 0; - linear_depth = 0; } } ssao; @@ -1339,7 +1332,8 @@ public: GLuint fbo; GLuint color; - Exposure() { fbo = 0; } + Exposure() : + fbo(0) {} } exposure; uint64_t last_exposure_tick; @@ -1353,26 +1347,22 @@ public: RID texture; - RenderTarget() { - - msaa = VS::VIEWPORT_MSAA_DISABLED; - width = 0; - height = 0; - depth = 0; - fbo = 0; + RenderTarget() : + fbo(0), + depth(0), + last_exposure_tick(0), + width(0), + height(0), + used_in_frame(false), + msaa(VS::VIEWPORT_MSAA_DISABLED) { exposure.fbo = 0; buffers.fbo = 0; - used_in_frame = false; - for (int i = 0; i < RENDER_TARGET_FLAG_MAX; i++) { flags[i] = false; } flags[RENDER_TARGET_HDR] = true; - buffers.active = false; buffers.effects_active = false; - - last_exposure_tick = 0; } }; diff --git a/drivers/gles3/shader_compiler_gles3.cpp b/drivers/gles3/shader_compiler_gles3.cpp index 0c353d42bb..2a2280e8a4 100644 --- a/drivers/gles3/shader_compiler_gles3.cpp +++ b/drivers/gles3/shader_compiler_gles3.cpp @@ -30,7 +30,8 @@ #include "shader_compiler_gles3.h" -#include "os/os.h" +#include "core/os/os.h" +#include "core/project_settings.h" #define SL ShaderLanguage @@ -78,6 +79,12 @@ static int _get_datatype_size(SL::DataType p_type) { case SL::TYPE_SAMPLER2D: return 16; case SL::TYPE_ISAMPLER2D: return 16; case SL::TYPE_USAMPLER2D: return 16; + case SL::TYPE_SAMPLER2DARRAY: return 16; + case SL::TYPE_ISAMPLER2DARRAY: return 16; + case SL::TYPE_USAMPLER2DARRAY: return 16; + case SL::TYPE_SAMPLER3D: return 16; + case SL::TYPE_ISAMPLER3D: return 16; + case SL::TYPE_USAMPLER3D: return 16; case SL::TYPE_SAMPLERCUBE: return 16; } @@ -111,6 +118,12 @@ static int _get_datatype_alignment(SL::DataType p_type) { case SL::TYPE_SAMPLER2D: return 16; case SL::TYPE_ISAMPLER2D: return 16; case SL::TYPE_USAMPLER2D: return 16; + case SL::TYPE_SAMPLER2DARRAY: return 16; + case SL::TYPE_ISAMPLER2DARRAY: return 16; + case SL::TYPE_USAMPLER2DARRAY: return 16; + case SL::TYPE_SAMPLER3D: return 16; + case SL::TYPE_ISAMPLER3D: return 16; + case SL::TYPE_USAMPLER3D: return 16; case SL::TYPE_SAMPLERCUBE: return 16; } @@ -120,8 +133,7 @@ static String _interpstr(SL::DataInterpolation p_interp) { switch (p_interp) { case SL::INTERPOLATION_FLAT: return "flat "; - case SL::INTERPOLATION_NO_PERSPECTIVE: return "noperspective "; - case SL::INTERPOLATION_SMOOTH: return "smooth "; + case SL::INTERPOLATION_SMOOTH: return ""; } return ""; } @@ -154,15 +166,17 @@ static String _opstr(SL::Operator p_op) { static String _mkid(const String &p_id) { - return "m_" + p_id; + String id = "m_" + p_id; + return id.replace("__", "_dus_"); //doubleunderscore is reserverd in glsl } static String f2sp0(float p_float) { - if (int(p_float) == p_float) - return itos(p_float) + ".0"; - else - return rtoss(p_float); + String num = rtoss(p_float); + if (num.find(".") == -1 && num.find("e") == -1) { + num += ".0"; + } + return num; } static String get_constant_text(SL::DataType p_type, const Vector<SL::ConstantNode::Value> &p_values) { @@ -215,7 +229,7 @@ static String get_constant_text(SL::DataType p_type, const Vector<SL::ConstantNo text += ")"; return text; } break; - case SL::TYPE_FLOAT: return f2sp0(p_values[0].real) + "f"; + case SL::TYPE_FLOAT: return f2sp0(p_values[0].real); case SL::TYPE_VEC2: case SL::TYPE_VEC3: case SL::TYPE_VEC4: { @@ -436,8 +450,6 @@ String ShaderCompilerGLES3::_dump_node_code(SL::Node *p_node, int p_level, Gener continue; } - print_line("u - "+String(E->key())+" offset: "+itos(r_gen_code.uniform_offsets[E->get().order])); - } */ @@ -465,6 +477,7 @@ String ShaderCompilerGLES3::_dump_node_code(SL::Node *p_node, int p_level, Gener //code for functions for (int i = 0; i < pnode->functions.size(); i++) { SL::FunctionNode *fnode = pnode->functions[i].function; + current_func_name = fnode->name; function_code[fnode->name] = _dump_node_code(fnode->body, p_level + 1, r_gen_code, p_actions, p_default_actions, p_assigning); } @@ -773,7 +786,7 @@ ShaderCompilerGLES3::ShaderCompilerGLES3() { /** CANVAS ITEM SHADER **/ actions[VS::SHADER_CANVAS_ITEM].renames["VERTEX"] = "outvec.xy"; - actions[VS::SHADER_CANVAS_ITEM].renames["UV"] = "uv_interp"; + actions[VS::SHADER_CANVAS_ITEM].renames["UV"] = "uv"; actions[VS::SHADER_CANVAS_ITEM].renames["POINT_SIZE"] = "gl_PointSize"; actions[VS::SHADER_CANVAS_ITEM].renames["WORLD_MATRIX"] = "modelview_matrix"; @@ -861,7 +874,9 @@ ShaderCompilerGLES3::ShaderCompilerGLES3() { actions[VS::SHADER_SPATIAL].renames["SCREEN_UV"] = "screen_uv"; actions[VS::SHADER_SPATIAL].renames["SCREEN_TEXTURE"] = "screen_texture"; actions[VS::SHADER_SPATIAL].renames["DEPTH_TEXTURE"] = "depth_buffer"; + actions[VS::SHADER_SPATIAL].renames["DEPTH"] = "gl_FragDepth"; actions[VS::SHADER_SPATIAL].renames["ALPHA_SCISSOR"] = "alpha_scissor"; + actions[VS::SHADER_SPATIAL].renames["OUTPUT_IS_SRGB"] = "SHADER_IS_SRGB"; //for light actions[VS::SHADER_SPATIAL].renames["VIEW"] = "view"; @@ -903,12 +918,24 @@ ShaderCompilerGLES3::ShaderCompilerGLES3() { actions[VS::SHADER_SPATIAL].render_mode_defines["cull_front"] = "#define DO_SIDE_CHECK\n"; actions[VS::SHADER_SPATIAL].render_mode_defines["cull_disabled"] = "#define DO_SIDE_CHECK\n"; - actions[VS::SHADER_SPATIAL].render_mode_defines["diffuse_burley"] = "#define DIFFUSE_BURLEY\n"; + bool force_lambert = GLOBAL_GET("rendering/quality/shading/force_lambert_over_burley"); + + if (!force_lambert) { + actions[VS::SHADER_SPATIAL].render_mode_defines["diffuse_burley"] = "#define DIFFUSE_BURLEY\n"; + } + actions[VS::SHADER_SPATIAL].render_mode_defines["diffuse_oren_nayar"] = "#define DIFFUSE_OREN_NAYAR\n"; actions[VS::SHADER_SPATIAL].render_mode_defines["diffuse_lambert_wrap"] = "#define DIFFUSE_LAMBERT_WRAP\n"; actions[VS::SHADER_SPATIAL].render_mode_defines["diffuse_toon"] = "#define DIFFUSE_TOON\n"; - actions[VS::SHADER_SPATIAL].render_mode_defines["specular_schlick_ggx"] = "#define SPECULAR_SCHLICK_GGX\n"; + bool force_blinn = GLOBAL_GET("rendering/quality/shading/force_blinn_over_ggx"); + + if (!force_blinn) { + actions[VS::SHADER_SPATIAL].render_mode_defines["specular_schlick_ggx"] = "#define SPECULAR_SCHLICK_GGX\n"; + } else { + actions[VS::SHADER_SPATIAL].render_mode_defines["specular_schlick_ggx"] = "#define SPECULAR_BLINN\n"; + } + actions[VS::SHADER_SPATIAL].render_mode_defines["specular_blinn"] = "#define SPECULAR_BLINN\n"; actions[VS::SHADER_SPATIAL].render_mode_defines["specular_phong"] = "#define SPECULAR_PHONG\n"; actions[VS::SHADER_SPATIAL].render_mode_defines["specular_toon"] = "#define SPECULAR_TOON\n"; @@ -921,7 +948,7 @@ ShaderCompilerGLES3::ShaderCompilerGLES3() { actions[VS::SHADER_PARTICLES].renames["COLOR"] = "out_color"; actions[VS::SHADER_PARTICLES].renames["VELOCITY"] = "out_velocity_active.xyz"; actions[VS::SHADER_PARTICLES].renames["MASS"] = "mass"; - actions[VS::SHADER_PARTICLES].renames["ACTIVE"] = "active"; + actions[VS::SHADER_PARTICLES].renames["ACTIVE"] = "shader_active"; actions[VS::SHADER_PARTICLES].renames["RESTART"] = "restart"; actions[VS::SHADER_PARTICLES].renames["CUSTOM"] = "out_custom"; actions[VS::SHADER_PARTICLES].renames["TRANSFORM"] = "xform"; diff --git a/drivers/gles3/shader_compiler_gles3.h b/drivers/gles3/shader_compiler_gles3.h index 7a32057741..1f903b8935 100644 --- a/drivers/gles3/shader_compiler_gles3.h +++ b/drivers/gles3/shader_compiler_gles3.h @@ -31,7 +31,7 @@ #ifndef SHADERCOMPILERGLES3_H #define SHADERCOMPILERGLES3_H -#include "pair.h" +#include "core/pair.h" #include "servers/visual/shader_language.h" #include "servers/visual/shader_types.h" #include "servers/visual_server.h" diff --git a/drivers/gles3/shader_gles3.cpp b/drivers/gles3/shader_gles3.cpp index ca0ce5cd3e..404a9107ab 100644 --- a/drivers/gles3/shader_gles3.cpp +++ b/drivers/gles3/shader_gles3.cpp @@ -30,7 +30,7 @@ #include "shader_gles3.h" -#include "print_string.h" +#include "core/print_string.h" //#define DEBUG_OPENGL @@ -122,6 +122,11 @@ bool ShaderGLES3::bind() { ERR_FAIL_COND_V(!version, false); + if (!version->ok) { //broken, unable to bind (do not throw error, you saw it before already when it failed compilation). + glUseProgram(0); + return false; + } + glUseProgram(version->id); DEBUG_TEST_ERROR("Use Program"); @@ -214,20 +219,15 @@ ShaderGLES3::Version *ShaderGLES3::get_current_version() { strings.push_back("#version 300 es\n"); #endif - int define_line_ofs = 1; - for (int i = 0; i < custom_defines.size(); i++) { strings.push_back(custom_defines[i].get_data()); - define_line_ofs++; } for (int j = 0; j < conditional_count; j++) { bool enable = ((1 << j) & conditional_version.version); strings.push_back(enable ? conditional_defines[j] : ""); - if (enable) - define_line_ofs++; if (enable) { DEBUG_PRINT(conditional_defines[j]); @@ -240,8 +240,6 @@ ShaderGLES3::Version *ShaderGLES3::get_current_version() { CharString code_globals; CharString material_string; - //print_line("code version? "+itos(conditional_version.code_version)); - CustomCode *cc = NULL; if (conditional_version.code_version > 0) { @@ -250,7 +248,6 @@ ShaderGLES3::Version *ShaderGLES3::get_current_version() { ERR_FAIL_COND_V(!custom_code_map.has(conditional_version.code_version), NULL); cc = &custom_code_map[conditional_version.code_version]; v.code_version = cc->version; - define_line_ofs += 2; } /* CREATE PROGRAM */ @@ -743,13 +740,6 @@ void ShaderGLES3::set_custom_shader(uint32_t p_code_id) { void ShaderGLES3::free_custom_shader(uint32_t p_code_id) { - /* if (! custom_code_map.has( p_code_id )) { - print_line("no code id "+itos(p_code_id)); - } else { - print_line("freed code id "+itos(p_code_id)); - - }*/ - ERR_FAIL_COND(!custom_code_map.has(p_code_id)); if (conditional_version.code_version == p_code_id) conditional_version.code_version = 0; //bye diff --git a/drivers/gles3/shader_gles3.h b/drivers/gles3/shader_gles3.h index 996655615e..0d360e8453 100644 --- a/drivers/gles3/shader_gles3.h +++ b/drivers/gles3/shader_gles3.h @@ -31,7 +31,10 @@ #ifndef SHADER_GLES3_H #define SHADER_GLES3_H -#include <stdio.h> +#include "core/hash_map.h" +#include "core/map.h" +#include "core/math/camera_matrix.h" +#include "core/variant.h" #include "platform_config.h" #ifndef GLES3_INCLUDE_H @@ -40,10 +43,7 @@ #include GLES3_INCLUDE_H #endif -#include "camera_matrix.h" -#include "hash_map.h" -#include "map.h" -#include "variant.h" +#include <stdio.h> /** @author Juan Linietsky <reduzio@gmail.com> @@ -128,11 +128,13 @@ private: Vector<GLint> texture_uniform_locations; uint32_t code_version; bool ok; - Version() { - code_version = 0; - ok = false; - uniform_location = NULL; - } + Version() : + id(0), + vert_id(0), + frag_id(0), + uniform_location(NULL), + code_version(0), + ok(false) {} }; Version *version; @@ -336,6 +338,7 @@ public: } uint32_t get_version() const { return new_conditional_version.version; } + _FORCE_INLINE_ bool is_version_valid() const { return version && version->ok; } void set_uniform_camera(int p_idx, const CameraMatrix &p_mat) { diff --git a/drivers/gles3/shaders/SCsub b/drivers/gles3/shaders/SCsub index f1811fa7b5..27fd1514e7 100644 --- a/drivers/gles3/shaders/SCsub +++ b/drivers/gles3/shaders/SCsub @@ -20,3 +20,4 @@ if 'GLES3_GLSL' in env['BUILDERS']: env.GLES3_GLSL('exposure.glsl'); env.GLES3_GLSL('tonemap.glsl'); env.GLES3_GLSL('particles.glsl'); + env.GLES3_GLSL('lens_distorted.glsl'); diff --git a/drivers/gles3/shaders/blend_shape.glsl b/drivers/gles3/shaders/blend_shape.glsl index 4e0d066823..a1e954e33d 100644 --- a/drivers/gles3/shaders/blend_shape.glsl +++ b/drivers/gles3/shaders/blend_shape.glsl @@ -1,6 +1,6 @@ +/* clang-format off */ [vertex] - /* from VisualServer: @@ -23,56 +23,57 @@ ARRAY_INDEX=8, /* INPUT ATTRIBS */ -layout(location=0) in highp VFORMAT vertex_attrib; -layout(location=1) in vec3 normal_attrib; +layout(location = 0) in highp VFORMAT vertex_attrib; +/* clang-format on */ +layout(location = 1) in vec3 normal_attrib; #ifdef ENABLE_TANGENT -layout(location=2) in vec4 tangent_attrib; +layout(location = 2) in vec4 tangent_attrib; #endif #ifdef ENABLE_COLOR -layout(location=3) in vec4 color_attrib; +layout(location = 3) in vec4 color_attrib; #endif #ifdef ENABLE_UV -layout(location=4) in vec2 uv_attrib; +layout(location = 4) in vec2 uv_attrib; #endif #ifdef ENABLE_UV2 -layout(location=5) in vec2 uv2_attrib; +layout(location = 5) in vec2 uv2_attrib; #endif #ifdef ENABLE_SKELETON -layout(location=6) in ivec4 bone_attrib; -layout(location=7) in vec4 weight_attrib; +layout(location = 6) in ivec4 bone_attrib; +layout(location = 7) in vec4 weight_attrib; #endif /* BLEND ATTRIBS */ #ifdef ENABLE_BLEND -layout(location=8) in highp VFORMAT vertex_attrib_blend; -layout(location=9) in vec3 normal_attrib_blend; +layout(location = 8) in highp VFORMAT vertex_attrib_blend; +layout(location = 9) in vec3 normal_attrib_blend; #ifdef ENABLE_TANGENT -layout(location=10) in vec4 tangent_attrib_blend; +layout(location = 10) in vec4 tangent_attrib_blend; #endif #ifdef ENABLE_COLOR -layout(location=11) in vec4 color_attrib_blend; +layout(location = 11) in vec4 color_attrib_blend; #endif #ifdef ENABLE_UV -layout(location=12) in vec2 uv_attrib_blend; +layout(location = 12) in vec2 uv_attrib_blend; #endif #ifdef ENABLE_UV2 -layout(location=13) in vec2 uv2_attrib_blend; +layout(location = 13) in vec2 uv2_attrib_blend; #endif #ifdef ENABLE_SKELETON -layout(location=14) in ivec4 bone_attrib_blend; -layout(location=15) in vec4 weight_attrib_blend; +layout(location = 14) in ivec4 bone_attrib_blend; +layout(location = 15) in vec4 weight_attrib_blend; #endif #endif @@ -110,7 +111,6 @@ uniform float blend_amount; void main() { - #ifdef ENABLE_BLEND vertex_out = vertex_attrib_blend + vertex_attrib * blend_amount; @@ -140,7 +140,6 @@ void main() { uv2_out = uv2_attrib_blend + uv2_attrib * blend_amount; #endif - #ifdef ENABLE_SKELETON bone_out = bone_attrib_blend; @@ -149,7 +148,6 @@ void main() { #else //ENABLE_BLEND - vertex_out = vertex_attrib * blend_amount; #ifdef ENABLE_NORMAL @@ -177,7 +175,6 @@ void main() { uv2_out = uv2_attrib * blend_amount; #endif - #ifdef ENABLE_SKELETON bone_out = bone_attrib; @@ -188,10 +185,10 @@ void main() { gl_Position = vec4(0.0); } +/* clang-format off */ [fragment] - void main() { } - +/* clang-format on */ diff --git a/drivers/gles3/shaders/canvas.glsl b/drivers/gles3/shaders/canvas.glsl index e7828d265c..974eff86f3 100644 --- a/drivers/gles3/shaders/canvas.glsl +++ b/drivers/gles3/shaders/canvas.glsl @@ -1,12 +1,13 @@ +/* clang-format off */ [vertex] - -layout(location=0) in highp vec2 vertex; -layout(location=3) in vec4 color_attrib; +layout(location = 0) in highp vec2 vertex; +/* clang-format on */ +layout(location = 3) in vec4 color_attrib; #ifdef USE_SKELETON -layout(location=6) in uvec4 bone_indices; // attrib:6 -layout(location=7) in vec4 bone_weights; // attrib:7 +layout(location = 6) in uvec4 bone_indices; // attrib:6 +layout(location = 7) in vec4 bone_weights; // attrib:7 #endif #ifdef USE_TEXTURE_RECT @@ -18,25 +19,24 @@ uniform vec4 src_rect; #ifdef USE_INSTANCING -layout(location=8) in highp vec4 instance_xform0; -layout(location=9) in highp vec4 instance_xform1; -layout(location=10) in highp vec4 instance_xform2; -layout(location=11) in lowp vec4 instance_color; +layout(location = 8) in highp vec4 instance_xform0; +layout(location = 9) in highp vec4 instance_xform1; +layout(location = 10) in highp vec4 instance_xform2; +layout(location = 11) in lowp vec4 instance_color; #ifdef USE_INSTANCE_CUSTOM -layout(location=12) in highp vec4 instance_custom_data; +layout(location = 12) in highp vec4 instance_custom_data; #endif #endif -layout(location=4) in highp vec2 uv_attrib; +layout(location = 4) in highp vec2 uv_attrib; -//skeletn +// skeleton #endif uniform highp vec2 color_texpixel_size; - layout(std140) uniform CanvasItemData { //ubo:0 highp mat4 projection_matrix; @@ -46,7 +46,6 @@ layout(std140) uniform CanvasItemData { //ubo:0 uniform highp mat4 modelview_matrix; uniform highp mat4 extra_matrix; - out highp vec2 uv_interp; out mediump vec4 color_interp; @@ -55,7 +54,6 @@ out mediump vec4 color_interp; out highp vec2 pixel_size_interp; #endif - #ifdef USE_SKELETON uniform mediump sampler2D skeleton_texture; // texunit:-1 uniform highp mat4 skeleton_transform; @@ -66,7 +64,7 @@ uniform highp mat4 skeleton_transform_inverse; layout(std140) uniform LightData { //ubo:1 - //light matrices + // light matrices highp mat4 light_matrix; highp mat4 light_local_matrix; highp mat4 shadow_matrix; @@ -80,11 +78,9 @@ layout(std140) uniform LightData { //ubo:1 highp float shadow_distance_mult; }; - out vec4 light_uv_interp; out vec2 transformed_light_uv; - out vec4 local_rot; #ifdef USE_SHADOWS @@ -96,36 +92,35 @@ const bool at_light_pass = true; const bool at_light_pass = false; #endif -#ifdef USE_PARTICLES -uniform int h_frames; -uniform int v_frames; -#endif - - #if defined(USE_MATERIAL) +/* clang-format off */ layout(std140) uniform UniformData { //ubo:2 MATERIAL_UNIFORMS }; +/* clang-format on */ #endif +/* clang-format off */ VERTEX_SHADER_GLOBALS +/* clang-format on */ + void main() { vec4 color = color_attrib; #ifdef USE_INSTANCING - mat4 extra_matrix2 = extra_matrix * transpose(mat4(instance_xform0,instance_xform1,instance_xform2,vec4(0.0,0.0,0.0,1.0))); - color*=instance_color; + mat4 extra_matrix_instance = extra_matrix * transpose(mat4(instance_xform0, instance_xform1, instance_xform2, vec4(0.0, 0.0, 0.0, 1.0))); + color *= instance_color; vec4 instance_custom = instance_custom_data; #else - mat4 extra_matrix2 = extra_matrix; + mat4 extra_matrix_instance = extra_matrix; vec4 instance_custom = vec4(0.0); #endif @@ -136,41 +131,35 @@ void main() { } else { uv_interp = src_rect.xy + abs(src_rect.zw) * vertex; } - highp vec4 outvec = vec4(dst_rect.xy + abs(dst_rect.zw) * mix(vertex,vec2(1.0,1.0)-vertex,lessThan(src_rect.zw,vec2(0.0,0.0))),0.0,1.0); + highp vec4 outvec = vec4(dst_rect.xy + abs(dst_rect.zw) * mix(vertex, vec2(1.0, 1.0) - vertex, lessThan(src_rect.zw, vec2(0.0, 0.0))), 0.0, 1.0); #else uv_interp = uv_attrib; - highp vec4 outvec = vec4(vertex,0.0,1.0); + highp vec4 outvec = vec4(vertex, 0.0, 1.0); #endif - #ifdef USE_PARTICLES //scale by texture size - outvec.xy/=color_texpixel_size; - - //compute h and v frames and adjust UV interp for animation - int total_frames = h_frames * v_frames; - int frame = min(int(float(total_frames) *instance_custom.z),total_frames-1); - float frame_w = 1.0/float(h_frames); - float frame_h = 1.0/float(v_frames); - uv_interp.x = uv_interp.x * frame_w + frame_w * float(frame % h_frames); - uv_interp.y = uv_interp.y * frame_h + frame_h * float(frame / h_frames); - + outvec.xy /= color_texpixel_size; #endif +#define extra_matrix extra_matrix_instance -#define extra_matrix extra_matrix2 - -{ + //for compatibility with the fragment shader we need to use uv here + vec2 uv = uv_interp; + { + /* clang-format off */ VERTEX_SHADER_CODE -} + /* clang-format on */ + } + uv_interp = uv; #ifdef USE_NINEPATCH - pixel_size_interp=abs(dst_rect.zw) * vertex; + pixel_size_interp = abs(dst_rect.zw) * vertex; #endif #if !defined(SKIP_TRANSFORM_USED) @@ -183,48 +172,46 @@ VERTEX_SHADER_CODE color_interp = color; #ifdef USE_PIXEL_SNAP - - outvec.xy=floor(outvec+0.5).xy; + outvec.xy = floor(outvec + 0.5).xy; #endif - #ifdef USE_SKELETON - if (bone_weights!=vec4(0.0)){ //must be a valid bone + if (bone_weights != vec4(0.0)) { //must be a valid bone //skeleton transform ivec4 bone_indicesi = ivec4(bone_indices); - ivec2 tex_ofs = ivec2( bone_indicesi.x%256, (bone_indicesi.x/256)*2 ); - - highp mat2x4 m = mat2x4( - texelFetch(skeleton_texture,tex_ofs,0), - texelFetch(skeleton_texture,tex_ofs+ivec2(0,1),0) - ) * bone_weights.x; + ivec2 tex_ofs = ivec2(bone_indicesi.x % 256, (bone_indicesi.x / 256) * 2); - tex_ofs = ivec2( bone_indicesi.y%256, (bone_indicesi.y/256)*2 ); + highp mat2x4 m; + m = mat2x4( + texelFetch(skeleton_texture, tex_ofs, 0), + texelFetch(skeleton_texture, tex_ofs + ivec2(0, 1), 0)) * + bone_weights.x; - m+= mat2x4( - texelFetch(skeleton_texture,tex_ofs,0), - texelFetch(skeleton_texture,tex_ofs+ivec2(0,1),0) - ) * bone_weights.y; + tex_ofs = ivec2(bone_indicesi.y % 256, (bone_indicesi.y / 256) * 2); - tex_ofs = ivec2( bone_indicesi.z%256, (bone_indicesi.z/256)*2 ); + m += mat2x4( + texelFetch(skeleton_texture, tex_ofs, 0), + texelFetch(skeleton_texture, tex_ofs + ivec2(0, 1), 0)) * + bone_weights.y; - m+= mat2x4( - texelFetch(skeleton_texture,tex_ofs,0), - texelFetch(skeleton_texture,tex_ofs+ivec2(0,1),0) - ) * bone_weights.z; + tex_ofs = ivec2(bone_indicesi.z % 256, (bone_indicesi.z / 256) * 2); + m += mat2x4( + texelFetch(skeleton_texture, tex_ofs, 0), + texelFetch(skeleton_texture, tex_ofs + ivec2(0, 1), 0)) * + bone_weights.z; - tex_ofs = ivec2( bone_indicesi.w%256, (bone_indicesi.w/256)*2 ); + tex_ofs = ivec2(bone_indicesi.w % 256, (bone_indicesi.w / 256) * 2); - m+= mat2x4( - texelFetch(skeleton_texture,tex_ofs,0), - texelFetch(skeleton_texture,tex_ofs+ivec2(0,1),0) - ) * bone_weights.w; + m += mat2x4( + texelFetch(skeleton_texture, tex_ofs, 0), + texelFetch(skeleton_texture, tex_ofs + ivec2(0, 1), 0)) * + bone_weights.w; - mat4 bone_matrix = skeleton_transform * transpose(mat4(m[0],m[1],vec4(0.0,0.0,1.0,0.0),vec4(0.0,0.0,0.0,1.0))) * skeleton_transform_inverse; + mat4 bone_matrix = skeleton_transform * transpose(mat4(m[0], m[1], vec4(0.0, 0.0, 1.0, 0.0), vec4(0.0, 0.0, 0.0, 1.0))) * skeleton_transform_inverse; outvec = bone_matrix * outvec; } @@ -236,45 +223,39 @@ VERTEX_SHADER_CODE #ifdef USE_LIGHTING light_uv_interp.xy = (light_matrix * outvec).xy; - light_uv_interp.zw =(light_local_matrix * outvec).xy; + light_uv_interp.zw = (light_local_matrix * outvec).xy; mat3 inverse_light_matrix = mat3(inverse(light_matrix)); inverse_light_matrix[0] = normalize(inverse_light_matrix[0]); inverse_light_matrix[1] = normalize(inverse_light_matrix[1]); inverse_light_matrix[2] = normalize(inverse_light_matrix[2]); - transformed_light_uv = (inverse_light_matrix * vec3(light_uv_interp.zw,0.0)).xy; //for normal mapping + transformed_light_uv = (inverse_light_matrix * vec3(light_uv_interp.zw, 0.0)).xy; //for normal mapping #ifdef USE_SHADOWS - pos=outvec.xy; + pos = outvec.xy; #endif - - local_rot.xy=normalize( (modelview_matrix * ( extra_matrix * vec4(1.0,0.0,0.0,0.0) )).xy ); - local_rot.zw=normalize( (modelview_matrix * ( extra_matrix * vec4(0.0,1.0,0.0,0.0) )).xy ); + local_rot.xy = normalize((modelview_matrix * (extra_matrix_instance * vec4(1.0, 0.0, 0.0, 0.0))).xy); + local_rot.zw = normalize((modelview_matrix * (extra_matrix_instance * vec4(0.0, 1.0, 0.0, 0.0))).xy); #ifdef USE_TEXTURE_RECT - local_rot.xy*=sign(src_rect.z); - local_rot.zw*=sign(src_rect.w); + local_rot.xy *= sign(src_rect.z); + local_rot.zw *= sign(src_rect.w); #endif - - #endif - } +/* clang-format off */ [fragment] - - uniform mediump sampler2D color_texture; // texunit:0 +/* clang-format on */ uniform highp vec2 color_texpixel_size; uniform mediump sampler2D normal_texture; // texunit:1 - in highp vec2 uv_interp; in mediump vec4 color_interp; - #if defined(SCREEN_TEXTURE_USED) uniform sampler2D screen_texture; // texunit:-3 @@ -292,7 +273,6 @@ layout(std140) uniform CanvasItemData { highp float time; }; - #ifdef USE_LIGHTING layout(std140) uniform LightData { @@ -314,10 +294,8 @@ uniform lowp sampler2D light_texture; // texunit:-1 in vec4 light_uv_interp; in vec2 transformed_light_uv; - in vec4 local_rot; - #ifdef USE_SHADOWS uniform highp sampler2D shadow_texture; // texunit:-2 @@ -332,44 +310,49 @@ const bool at_light_pass = false; uniform mediump vec4 final_modulate; - - - -layout(location=0) out mediump vec4 frag_color; - +layout(location = 0) out mediump vec4 frag_color; #if defined(USE_MATERIAL) +/* clang-format off */ layout(std140) uniform UniformData { MATERIAL_UNIFORMS }; +/* clang-format on */ #endif +/* clang-format off */ + FRAGMENT_SHADER_GLOBALS +/* clang-format on */ + void light_compute( - inout vec4 light, - inout vec2 light_vec, - inout float light_height, - inout vec4 light_color, - vec2 light_uv, - inout vec4 shadow_color, - vec3 normal, - vec2 uv, + inout vec4 light, + inout vec2 light_vec, + inout float light_height, + inout vec4 light_color, + vec2 light_uv, + inout vec4 shadow_color, + vec3 normal, + vec2 uv, #if defined(SCREEN_UV_USED) - vec2 screen_uv, + vec2 screen_uv, #endif - vec4 color) { + vec4 color) { #if defined(USE_LIGHT_SHADER_CODE) + /* clang-format off */ + LIGHT_SHADER_CODE -#endif + /* clang-format on */ +#endif } #ifdef USE_TEXTURE_RECT @@ -385,48 +368,44 @@ in highp vec2 pixel_size_interp; uniform int np_repeat_v; uniform int np_repeat_h; uniform bool np_draw_center; -//left top right bottom in pixel coordinates +// left top right bottom in pixel coordinates uniform vec4 np_margins; +float map_ninepatch_axis(float pixel, float draw_size, float tex_pixel_size, float margin_begin, float margin_end, int np_repeat, inout int draw_center) { - -float map_ninepatch_axis(float pixel, float draw_size,float tex_pixel_size,float margin_begin,float margin_end,int np_repeat,inout int draw_center) { - - - float tex_size = 1.0/tex_pixel_size; + float tex_size = 1.0 / tex_pixel_size; if (pixel < margin_begin) { return pixel * tex_pixel_size; - } else if (pixel >= draw_size-margin_end) { - return (tex_size-(draw_size-pixel)) * tex_pixel_size; + } else if (pixel >= draw_size - margin_end) { + return (tex_size - (draw_size - pixel)) * tex_pixel_size; } else { - if (!np_draw_center){ + if (!np_draw_center) { draw_center--; } - if (np_repeat==0) { //stretch + if (np_repeat == 0) { //stretch //convert to ratio float ratio = (pixel - margin_begin) / (draw_size - margin_begin - margin_end); //scale to source texture return (margin_begin + ratio * (tex_size - margin_begin - margin_end)) * tex_pixel_size; - } else if (np_repeat==1) { //tile + } else if (np_repeat == 1) { //tile //convert to ratio float ofs = mod((pixel - margin_begin), tex_size - margin_begin - margin_end); //scale to source texture return (margin_begin + ofs) * tex_pixel_size; - } else if (np_repeat==2) { //tile fit + } else if (np_repeat == 2) { //tile fit //convert to ratio float src_area = draw_size - margin_begin - margin_end; float dst_area = tex_size - margin_begin - margin_end; - float scale = max(1.0,floor(src_area / max(dst_area,0.0000001) + 0.5)); + float scale = max(1.0, floor(src_area / max(dst_area, 0.0000001) + 0.5)); //convert to ratio float ratio = (pixel - margin_begin) / src_area; - ratio = mod(ratio * scale,1.0); + ratio = mod(ratio * scale, 1.0); return (margin_begin + ratio * dst_area) * tex_pixel_size; } } - } #endif @@ -443,42 +422,39 @@ void main() { #ifdef USE_NINEPATCH - int draw_center=2; + int draw_center = 2; uv = vec2( - map_ninepatch_axis(pixel_size_interp.x,abs(dst_rect.z),color_texpixel_size.x,np_margins.x,np_margins.z,np_repeat_h,draw_center), - map_ninepatch_axis(pixel_size_interp.y,abs(dst_rect.w),color_texpixel_size.y,np_margins.y,np_margins.w,np_repeat_v,draw_center) - ); + map_ninepatch_axis(pixel_size_interp.x, abs(dst_rect.z), color_texpixel_size.x, np_margins.x, np_margins.z, np_repeat_h, draw_center), + map_ninepatch_axis(pixel_size_interp.y, abs(dst_rect.w), color_texpixel_size.y, np_margins.y, np_margins.w, np_repeat_v, draw_center)); - if (draw_center==0) { - color.a=0.0; + if (draw_center == 0) { + color.a = 0.0; } - uv = uv*src_rect.zw+src_rect.xy; //apply region if needed + uv = uv * src_rect.zw + src_rect.xy; //apply region if needed #endif if (clip_rect_uv) { - uv = clamp(uv,src_rect.xy,src_rect.xy+abs(src_rect.zw)); + uv = clamp(uv, src_rect.xy, src_rect.xy + abs(src_rect.zw)); } #endif #if !defined(COLOR_USED) -//default behavior, texture by color + //default behavior, texture by color #ifdef USE_DISTANCE_FIELD - const float smoothing = 1.0/32.0; - float distance = textureLod(color_texture, uv,0.0).a; + const float smoothing = 1.0 / 32.0; + float distance = textureLod(color_texture, uv, 0.0).a; color.a = smoothstep(0.5 - smoothing, 0.5 + smoothing, distance) * color.a; #else - color *= texture( color_texture, uv ); + color *= texture(color_texture, uv); #endif #endif - - vec3 normal; #if defined(NORMAL_USED) @@ -489,59 +465,57 @@ void main() { #endif if (use_default_normal) { - normal.xy = textureLod(normal_texture, uv,0.0).xy * 2.0 - 1.0; - normal.z = sqrt(1.0-dot(normal.xy,normal.xy)); - normal_used=true; + normal.xy = textureLod(normal_texture, uv, 0.0).xy * 2.0 - 1.0; + normal.z = sqrt(1.0 - dot(normal.xy, normal.xy)); + normal_used = true; } else { - normal = vec3(0.0,0.0,1.0); + normal = vec3(0.0, 0.0, 1.0); } - - #if defined(SCREEN_UV_USED) - vec2 screen_uv = gl_FragCoord.xy*screen_pixel_size; + vec2 screen_uv = gl_FragCoord.xy * screen_pixel_size; #endif - -{ - float normal_depth=1.0; + { + float normal_depth = 1.0; #if defined(NORMALMAP_USED) - vec3 normal_map=vec3(0.0,0.0,1.0); + vec3 normal_map = vec3(0.0, 0.0, 1.0); + normal_used = true; #endif + /* clang-format off */ + FRAGMENT_SHADER_CODE + /* clang-format on */ + #if defined(NORMALMAP_USED) - normal = mix(vec3(0.0,0.0,1.0), normal_map * vec3(2.0,-2.0,1.0) - vec3( 1.0, -1.0, 0.0 ), normal_depth ); + normal = mix(vec3(0.0, 0.0, 1.0), normal_map * vec3(2.0, -2.0, 1.0) - vec3(1.0, -1.0, 0.0), normal_depth); #endif - -} + } #ifdef DEBUG_ENCODED_32 - highp float enc32 = dot( color,highp vec4(1.0 / (256.0 * 256.0 * 256.0),1.0 / (256.0 * 256.0),1.0 / 256.0,1) ); - color = vec4(vec3(enc32),1.0); + highp float enc32 = dot(color, highp vec4(1.0 / (256.0 * 256.0 * 256.0), 1.0 / (256.0 * 256.0), 1.0 / 256.0, 1)); + color = vec4(vec3(enc32), 1.0); #endif - - color*=final_modulate; - - + color *= final_modulate; #ifdef USE_LIGHTING vec2 light_vec = transformed_light_uv; if (normal_used) { - normal.xy = mat2(local_rot.xy,local_rot.zw) * normal.xy; + normal.xy = mat2(local_rot.xy, local_rot.zw) * normal.xy; } - float att=1.0; + float att = 1.0; vec2 light_uv = light_uv_interp.xy; - vec4 light = texture(light_texture,light_uv); + vec4 light = texture(light_texture, light_uv); - if (any(lessThan(light_uv_interp.xy,vec2(0.0,0.0))) || any(greaterThanEqual(light_uv_interp.xy,vec2(1.0,1.0)))) { - color.a*=light_outside_alpha; //invisible + if (any(lessThan(light_uv_interp.xy, vec2(0.0, 0.0))) || any(greaterThanEqual(light_uv_interp.xy, vec2(1.0, 1.0)))) { + color.a *= light_outside_alpha; //invisible } else { float real_light_height = light_height; @@ -549,178 +523,176 @@ FRAGMENT_SHADER_CODE vec4 real_light_shadow_color = light_shadow_color; #if defined(USE_LIGHT_SHADER_CODE) -//light is written by the light shader + //light is written by the light shader light_compute( - light, - light_vec, - real_light_height, - real_light_color, - light_uv, - real_light_shadow_color, - normal, - uv, + light, + light_vec, + real_light_height, + real_light_color, + light_uv, + real_light_shadow_color, + normal, + uv, #if defined(SCREEN_UV_USED) - screen_uv, + screen_uv, #endif - color); + color); #endif light *= real_light_color; if (normal_used) { - vec3 light_normal = normalize(vec3(light_vec,-real_light_height)); - light*=max(dot(-light_normal,normal),0.0); + vec3 light_normal = normalize(vec3(light_vec, -real_light_height)); + light *= max(dot(-light_normal, normal), 0.0); } - color*=light; + color *= light; #ifdef USE_SHADOWS light_vec = light_uv_interp.zw; //for shadows - float angle_to_light = -atan(light_vec.x,light_vec.y); + float angle_to_light = -atan(light_vec.x, light_vec.y); float PI = 3.14159265358979323846264; /*int i = int(mod(floor((angle_to_light+7.0*PI/6.0)/(4.0*PI/6.0))+1.0, 3.0)); // +1 pq os indices estao em ordem 2,0,1 nos arrays float ang*/ - float su,sz; + float su, sz; float abs_angle = abs(angle_to_light); vec2 point; float sh; - if (abs_angle<45.0*PI/180.0) { + if (abs_angle < 45.0 * PI / 180.0) { point = light_vec; - sh=0.0+(1.0/8.0); - } else if (abs_angle>135.0*PI/180.0) { + sh = 0.0 + (1.0 / 8.0); + } else if (abs_angle > 135.0 * PI / 180.0) { point = -light_vec; - sh = 0.5+(1.0/8.0); - } else if (angle_to_light>0.0) { + sh = 0.5 + (1.0 / 8.0); + } else if (angle_to_light > 0.0) { - point = vec2(light_vec.y,-light_vec.x); - sh = 0.25+(1.0/8.0); + point = vec2(light_vec.y, -light_vec.x); + sh = 0.25 + (1.0 / 8.0); } else { - point = vec2(-light_vec.y,light_vec.x); - sh = 0.75+(1.0/8.0); - + point = vec2(-light_vec.y, light_vec.x); + sh = 0.75 + (1.0 / 8.0); } - - highp vec4 s = shadow_matrix * vec4(point,0.0,1.0); - s.xyz/=s.w; - su=s.x*0.5+0.5; - sz=s.z*0.5+0.5; + highp vec4 s = shadow_matrix * vec4(point, 0.0, 1.0); + s.xyz /= s.w; + su = s.x * 0.5 + 0.5; + sz = s.z * 0.5 + 0.5; //sz=lightlength(light_vec); - highp float shadow_attenuation=0.0; + highp float shadow_attenuation = 0.0; #ifdef USE_RGBA_SHADOWS -#define SHADOW_DEPTH(m_tex,m_uv) dot(texture((m_tex),(m_uv)),vec4(1.0 / (256.0 * 256.0 * 256.0),1.0 / (256.0 * 256.0),1.0 / 256.0,1) ) +#define SHADOW_DEPTH(m_tex, m_uv) dot(texture((m_tex), (m_uv)), vec4(1.0 / (256.0 * 256.0 * 256.0), 1.0 / (256.0 * 256.0), 1.0 / 256.0, 1)) #else -#define SHADOW_DEPTH(m_tex,m_uv) (texture((m_tex),(m_uv)).r) +#define SHADOW_DEPTH(m_tex, m_uv) (texture((m_tex), (m_uv)).r) #endif - - #ifdef SHADOW_USE_GRADIENT -#define SHADOW_TEST(m_ofs) { highp float sd = SHADOW_DEPTH(shadow_texture,vec2(m_ofs,sh)); shadow_attenuation+=1.0-smoothstep(sd,sd+shadow_gradient,sz); } +#define SHADOW_TEST(m_ofs) \ + { \ + highp float sd = SHADOW_DEPTH(shadow_texture, vec2(m_ofs, sh)); \ + shadow_attenuation += 1.0 - smoothstep(sd, sd + shadow_gradient, sz); \ + } #else -#define SHADOW_TEST(m_ofs) { highp float sd = SHADOW_DEPTH(shadow_texture,vec2(m_ofs,sh)); shadow_attenuation+=step(sz,sd); } +#define SHADOW_TEST(m_ofs) \ + { \ + highp float sd = SHADOW_DEPTH(shadow_texture, vec2(m_ofs, sh)); \ + shadow_attenuation += step(sz, sd); \ + } #endif - #ifdef SHADOW_FILTER_NEAREST SHADOW_TEST(su); #endif - #ifdef SHADOW_FILTER_PCF3 - SHADOW_TEST(su+shadowpixel_size); + SHADOW_TEST(su + shadowpixel_size); SHADOW_TEST(su); - SHADOW_TEST(su-shadowpixel_size); - shadow_attenuation/=3.0; + SHADOW_TEST(su - shadowpixel_size); + shadow_attenuation /= 3.0; #endif - #ifdef SHADOW_FILTER_PCF5 - SHADOW_TEST(su+shadowpixel_size*2.0); - SHADOW_TEST(su+shadowpixel_size); + SHADOW_TEST(su + shadowpixel_size * 2.0); + SHADOW_TEST(su + shadowpixel_size); SHADOW_TEST(su); - SHADOW_TEST(su-shadowpixel_size); - SHADOW_TEST(su-shadowpixel_size*2.0); - shadow_attenuation/=5.0; + SHADOW_TEST(su - shadowpixel_size); + SHADOW_TEST(su - shadowpixel_size * 2.0); + shadow_attenuation /= 5.0; #endif - #ifdef SHADOW_FILTER_PCF7 - SHADOW_TEST(su+shadowpixel_size*3.0); - SHADOW_TEST(su+shadowpixel_size*2.0); - SHADOW_TEST(su+shadowpixel_size); + SHADOW_TEST(su + shadowpixel_size * 3.0); + SHADOW_TEST(su + shadowpixel_size * 2.0); + SHADOW_TEST(su + shadowpixel_size); SHADOW_TEST(su); - SHADOW_TEST(su-shadowpixel_size); - SHADOW_TEST(su-shadowpixel_size*2.0); - SHADOW_TEST(su-shadowpixel_size*3.0); - shadow_attenuation/=7.0; + SHADOW_TEST(su - shadowpixel_size); + SHADOW_TEST(su - shadowpixel_size * 2.0); + SHADOW_TEST(su - shadowpixel_size * 3.0); + shadow_attenuation /= 7.0; #endif - #ifdef SHADOW_FILTER_PCF9 - SHADOW_TEST(su+shadowpixel_size*4.0); - SHADOW_TEST(su+shadowpixel_size*3.0); - SHADOW_TEST(su+shadowpixel_size*2.0); - SHADOW_TEST(su+shadowpixel_size); + SHADOW_TEST(su + shadowpixel_size * 4.0); + SHADOW_TEST(su + shadowpixel_size * 3.0); + SHADOW_TEST(su + shadowpixel_size * 2.0); + SHADOW_TEST(su + shadowpixel_size); SHADOW_TEST(su); - SHADOW_TEST(su-shadowpixel_size); - SHADOW_TEST(su-shadowpixel_size*2.0); - SHADOW_TEST(su-shadowpixel_size*3.0); - SHADOW_TEST(su-shadowpixel_size*4.0); - shadow_attenuation/=9.0; + SHADOW_TEST(su - shadowpixel_size); + SHADOW_TEST(su - shadowpixel_size * 2.0); + SHADOW_TEST(su - shadowpixel_size * 3.0); + SHADOW_TEST(su - shadowpixel_size * 4.0); + shadow_attenuation /= 9.0; #endif #ifdef SHADOW_FILTER_PCF13 - SHADOW_TEST(su+shadowpixel_size*6.0); - SHADOW_TEST(su+shadowpixel_size*5.0); - SHADOW_TEST(su+shadowpixel_size*4.0); - SHADOW_TEST(su+shadowpixel_size*3.0); - SHADOW_TEST(su+shadowpixel_size*2.0); - SHADOW_TEST(su+shadowpixel_size); + SHADOW_TEST(su + shadowpixel_size * 6.0); + SHADOW_TEST(su + shadowpixel_size * 5.0); + SHADOW_TEST(su + shadowpixel_size * 4.0); + SHADOW_TEST(su + shadowpixel_size * 3.0); + SHADOW_TEST(su + shadowpixel_size * 2.0); + SHADOW_TEST(su + shadowpixel_size); SHADOW_TEST(su); - SHADOW_TEST(su-shadowpixel_size); - SHADOW_TEST(su-shadowpixel_size*2.0); - SHADOW_TEST(su-shadowpixel_size*3.0); - SHADOW_TEST(su-shadowpixel_size*4.0); - SHADOW_TEST(su-shadowpixel_size*5.0); - SHADOW_TEST(su-shadowpixel_size*6.0); - shadow_attenuation/=13.0; + SHADOW_TEST(su - shadowpixel_size); + SHADOW_TEST(su - shadowpixel_size * 2.0); + SHADOW_TEST(su - shadowpixel_size * 3.0); + SHADOW_TEST(su - shadowpixel_size * 4.0); + SHADOW_TEST(su - shadowpixel_size * 5.0); + SHADOW_TEST(su - shadowpixel_size * 6.0); + shadow_attenuation /= 13.0; #endif - //color*=shadow_attenuation; - color=mix(real_light_shadow_color,color,shadow_attenuation); + //color *= shadow_attenuation; + color = mix(real_light_shadow_color, color, shadow_attenuation); //use shadows #endif } //use lighting #endif - //color.rgb*=color.a; + //color.rgb *= color.a; frag_color = color; - } diff --git a/drivers/gles3/shaders/canvas_shadow.glsl b/drivers/gles3/shaders/canvas_shadow.glsl index c757990de0..68d0713385 100644 --- a/drivers/gles3/shaders/canvas_shadow.glsl +++ b/drivers/gles3/shaders/canvas_shadow.glsl @@ -1,49 +1,45 @@ +/* clang-format off */ [vertex] - - uniform highp mat4 projection_matrix; +/* clang-format on */ uniform highp mat4 light_matrix; uniform highp mat4 world_matrix; uniform highp float distance_norm; -layout(location=0) in highp vec3 vertex; +layout(location = 0) in highp vec3 vertex; out highp vec4 position_interp; void main() { - gl_Position = projection_matrix * (light_matrix * (world_matrix * vec4(vertex,1.0))); - position_interp=gl_Position; + gl_Position = projection_matrix * (light_matrix * (world_matrix * vec4(vertex, 1.0))); + position_interp = gl_Position; } +/* clang-format off */ [fragment] in highp vec4 position_interp; +/* clang-format on */ #ifdef USE_RGBA_SHADOWS - -layout(location=0) out lowp vec4 distance_buf; - +layout(location = 0) out lowp vec4 distance_buf; #else - -layout(location=0) out highp float distance_buf; - +layout(location = 0) out highp float distance_buf; #endif void main() { - highp float depth = ((position_interp.z / position_interp.w) + 1.0) * 0.5 + 0.0;//bias; + highp float depth = ((position_interp.z / position_interp.w) + 1.0) * 0.5 + 0.0; // bias #ifdef USE_RGBA_SHADOWS highp vec4 comp = fract(depth * vec4(256.0 * 256.0 * 256.0, 256.0 * 256.0, 256.0, 1.0)); comp -= comp.xxyz * vec4(0, 1.0 / 256.0, 1.0 / 256.0, 1.0 / 256.0); - distance_buf=comp; + distance_buf = comp; #else - distance_buf=depth; - + distance_buf = depth; #endif } - diff --git a/drivers/gles3/shaders/copy.glsl b/drivers/gles3/shaders/copy.glsl index 1b7c626d3c..a5637537d2 100644 --- a/drivers/gles3/shaders/copy.glsl +++ b/drivers/gles3/shaders/copy.glsl @@ -1,13 +1,14 @@ +/* clang-format off */ [vertex] - -layout(location=0) in highp vec4 vertex_attrib; +layout(location = 0) in highp vec4 vertex_attrib; +/* clang-format on */ #if defined(USE_CUBEMAP) || defined(USE_PANORAMA) -layout(location=4) in vec3 cube_in; +layout(location = 4) in vec3 cube_in; #else -layout(location=4) in vec2 uv_in; +layout(location = 4) in vec2 uv_in; #endif -layout(location=5) in vec2 uv2_in; +layout(location = 5) in vec2 uv2_in; #if defined(USE_CUBEMAP) || defined(USE_PANORAMA) out vec3 cube_interp; @@ -32,7 +33,7 @@ void main() { #else uv_interp = uv_in; #ifdef V_FLIP - uv_interp.y = 1.0-uv_interp.y; + uv_interp.y = 1.0 - uv_interp.y; #endif #endif @@ -44,9 +45,9 @@ void main() { uv_interp = copy_section.xy + uv_interp * copy_section.zw; gl_Position.xy = (copy_section.xy + (gl_Position.xy * 0.5 + 0.5) * copy_section.zw) * 2.0 - 1.0; #endif - } +/* clang-format off */ [fragment] #define M_PI 3.14159265359 @@ -60,6 +61,7 @@ in vec3 cube_interp; #else in vec2 uv_interp; #endif +/* clang-format on */ #ifdef USE_ASYM_PANO uniform highp mat4 pano_transform; @@ -72,38 +74,33 @@ uniform samplerCube source_cube; //texunit:0 uniform sampler2D source; //texunit:0 #endif - #ifdef USE_MULTIPLIER uniform float multiplier; #endif #if defined(USE_PANORAMA) || defined(USE_ASYM_PANO) -vec4 texturePanorama(vec3 normal,sampler2D pano ) { +vec4 texturePanorama(vec3 normal, sampler2D pano) { vec2 st = vec2( - atan(normal.x, normal.z), - acos(normal.y) - ); - - if(st.x < 0.0) - st.x += M_PI*2.0; + atan(normal.x, normal.z), + acos(normal.y)); - st/=vec2(M_PI*2.0,M_PI); + if (st.x < 0.0) + st.x += M_PI * 2.0; - return textureLod(pano,st,0.0); + st /= vec2(M_PI * 2.0, M_PI); + return textureLod(pano, st, 0.0); } #endif - uniform float stuff; uniform vec2 pixel_size; in vec2 uv2_interp; - #ifdef USE_BCS uniform vec3 bcs; @@ -118,20 +115,17 @@ uniform sampler2D color_correction; //texunit:1 layout(location = 0) out vec4 frag_color; - - - void main() { //vec4 color = color_interp; #ifdef USE_PANORAMA - vec4 color = texturePanorama( normalize(cube_interp), source ); + vec4 color = texturePanorama(normalize(cube_interp), source); #elif defined(USE_ASYM_PANO) - // When an asymmetrical projection matrix is used (applicable for stereoscopic rendering i.e. VR) we need to do this calculation per fragment to get a perspective correct result. + // When an asymmetrical projection matrix is used (applicable for stereoscopic rendering i.e. VR) we need to do this calculation per fragment to get a perspective correct result. // Note that we're ignoring the x-offset for IPD, with Z sufficiently in the distance it becomes neglectible, as a result we could probably just set cube_normal.z to -1. // The Matrix[2][0] (= asym_proj.x) and Matrix[2][1] (= asym_proj.z) values are what provide the right shift in the image. @@ -142,72 +136,68 @@ void main() { cube_normal = mat3(pano_transform) * cube_normal; cube_normal.z = -cube_normal.z; - vec4 color = texturePanorama( normalize(cube_normal.xyz), source ); + vec4 color = texturePanorama(normalize(cube_normal.xyz), source); #elif defined(USE_CUBEMAP) - vec4 color = texture( source_cube, normalize(cube_interp) ); + vec4 color = texture(source_cube, normalize(cube_interp)); #else - vec4 color = textureLod( source, uv_interp,0.0 ); + vec4 color = textureLod(source, uv_interp, 0.0); #endif - - #ifdef LINEAR_TO_SRGB //regular Linear -> SRGB conversion vec3 a = vec3(0.055); - color.rgb = mix( (vec3(1.0)+a)*pow(color.rgb,vec3(1.0/2.4))-a , 12.92*color.rgb , lessThan(color.rgb,vec3(0.0031308))); + color.rgb = mix((vec3(1.0) + a) * pow(color.rgb, vec3(1.0 / 2.4)) - a, 12.92 * color.rgb, lessThan(color.rgb, vec3(0.0031308))); #endif #ifdef SRGB_TO_LINEAR - color.rgb = mix(pow((color.rgb + vec3(0.055)) * (1.0 / (1.0 + 0.055)),vec3(2.4)),color.rgb * (1.0 / 12.92),lessThan(color.rgb,vec3(0.04045))); + color.rgb = mix(pow((color.rgb + vec3(0.055)) * (1.0 / (1.0 + 0.055)), vec3(2.4)), color.rgb * (1.0 / 12.92), lessThan(color.rgb, vec3(0.04045))); #endif #ifdef DEBUG_GRADIENT - color.rg=uv_interp; - color.b=0.0; + color.rg = uv_interp; + color.b = 0.0; #endif #ifdef DISABLE_ALPHA - color.a=1.0; + color.a = 1.0; #endif - #ifdef GAUSSIAN_HORIZONTAL - color*=0.38774; - color+=texture( source, uv_interp+vec2( 1.0, 0.0)*pixel_size )*0.24477; - color+=texture( source, uv_interp+vec2( 2.0, 0.0)*pixel_size )*0.06136; - color+=texture( source, uv_interp+vec2(-1.0, 0.0)*pixel_size )*0.24477; - color+=texture( source, uv_interp+vec2(-2.0, 0.0)*pixel_size )*0.06136; + color *= 0.38774; + color += texture(source, uv_interp + vec2(1.0, 0.0) * pixel_size) * 0.24477; + color += texture(source, uv_interp + vec2(2.0, 0.0) * pixel_size) * 0.06136; + color += texture(source, uv_interp + vec2(-1.0, 0.0) * pixel_size) * 0.24477; + color += texture(source, uv_interp + vec2(-2.0, 0.0) * pixel_size) * 0.06136; #endif #ifdef GAUSSIAN_VERTICAL - color*=0.38774; - color+=texture( source, uv_interp+vec2( 0.0, 1.0)*pixel_size )*0.24477; - color+=texture( source, uv_interp+vec2( 0.0, 2.0)*pixel_size )*0.06136; - color+=texture( source, uv_interp+vec2( 0.0,-1.0)*pixel_size )*0.24477; - color+=texture( source, uv_interp+vec2( 0.0,-2.0)*pixel_size )*0.06136; + color *= 0.38774; + color += texture(source, uv_interp + vec2(0.0, 1.0) * pixel_size) * 0.24477; + color += texture(source, uv_interp + vec2(0.0, 2.0) * pixel_size) * 0.06136; + color += texture(source, uv_interp + vec2(0.0, -1.0) * pixel_size) * 0.24477; + color += texture(source, uv_interp + vec2(0.0, -2.0) * pixel_size) * 0.06136; #endif #ifdef USE_BCS - color.rgb = mix(vec3(0.0),color.rgb,bcs.x); - color.rgb = mix(vec3(0.5),color.rgb,bcs.y); - color.rgb = mix(vec3(dot(vec3(1.0),color.rgb)*0.33333),color.rgb,bcs.z); + color.rgb = mix(vec3(0.0), color.rgb, bcs.x); + color.rgb = mix(vec3(0.5), color.rgb, bcs.y); + color.rgb = mix(vec3(dot(vec3(1.0), color.rgb) * 0.33333), color.rgb, bcs.z); #endif #ifdef USE_COLOR_CORRECTION - color.r = texture(color_correction,vec2(color.r,0.0)).r; - color.g = texture(color_correction,vec2(color.g,0.0)).g; - color.b = texture(color_correction,vec2(color.b,0.0)).b; + color.r = texture(color_correction, vec2(color.r, 0.0)).r; + color.g = texture(color_correction, vec2(color.g, 0.0)).g; + color.b = texture(color_correction, vec2(color.b, 0.0)).b; #endif #ifdef USE_MULTIPLIER - color.rgb*=multiplier; + color.rgb *= multiplier; #endif frag_color = color; } - diff --git a/drivers/gles3/shaders/cube_to_dp.glsl b/drivers/gles3/shaders/cube_to_dp.glsl index 5ffc78c0b9..2b74f054f9 100644 --- a/drivers/gles3/shaders/cube_to_dp.glsl +++ b/drivers/gles3/shaders/cube_to_dp.glsl @@ -1,8 +1,9 @@ +/* clang-format off */ [vertex] - -layout(location=0) in highp vec4 vertex_attrib; -layout(location=4) in vec2 uv_in; +layout(location = 0) in highp vec4 vertex_attrib; +/* clang-format on */ +layout(location = 4) in vec2 uv_in; out vec2 uv_interp; @@ -12,10 +13,11 @@ void main() { gl_Position = vertex_attrib; } +/* clang-format off */ [fragment] - uniform highp samplerCube source_cube; //texunit:0 +/* clang-format on */ in vec2 uv_interp; uniform bool z_flip; @@ -25,55 +27,53 @@ uniform highp float bias; void main() { - highp vec3 normal = vec3( uv_interp * 2.0 - 1.0, 0.0 ); -/* - if(z_flip) { - normal.z = 0.5 - 0.5*((normal.x * normal.x) + (normal.y * normal.y)); + highp vec3 normal = vec3(uv_interp * 2.0 - 1.0, 0.0); + /* + if (z_flip) { + normal.z = 0.5 - 0.5 * ((normal.x * normal.x) + (normal.y * normal.y)); } else { - normal.z = -0.5 + 0.5*((normal.x * normal.x) + (normal.y * normal.y)); + normal.z = -0.5 + 0.5 * ((normal.x * normal.x) + (normal.y * normal.y)); } -*/ + */ - //normal.z = sqrt(1.0-dot(normal.xy,normal.xy)); - //normal.xy*=1.0+normal.z; + //normal.z = sqrt(1.0 - dot(normal.xy, normal.xy)); + //normal.xy *= 1.0 + normal.z; - normal.z = 0.5 - 0.5*((normal.x * normal.x) + (normal.y * normal.y)); + normal.z = 0.5 - 0.5 * ((normal.x * normal.x) + (normal.y * normal.y)); + normal = normalize(normal); + /* + normal.z = 0.5; normal = normalize(normal); + */ -/* - normal.z=0.5; - normal=normalize(normal); -*/ if (!z_flip) { - normal.z=-normal.z; + normal.z = -normal.z; } - //normal = normalize(vec3( uv_interp * 2.0 - 1.0, 1.0 )); - float depth = texture(source_cube,normal).r; + //normal = normalize(vec3(uv_interp * 2.0 - 1.0, 1.0)); + float depth = texture(source_cube, normal).r; // absolute values for direction cosines, bigger value equals closer to basis axis vec3 unorm = abs(normal); - if ( (unorm.x >= unorm.y) && (unorm.x >= unorm.z) ) { - // x code - unorm = normal.x > 0.0 ? vec3( 1.0, 0.0, 0.0 ) : vec3( -1.0, 0.0, 0.0 ) ; - } else if ( (unorm.y > unorm.x) && (unorm.y >= unorm.z) ) { - // y code - unorm = normal.y > 0.0 ? vec3( 0.0, 1.0, 0.0 ) : vec3( 0.0, -1.0, 0.0 ) ; - } else if ( (unorm.z > unorm.x) && (unorm.z > unorm.y) ) { - // z code - unorm = normal.z > 0.0 ? vec3( 0.0, 0.0, 1.0 ) : vec3( 0.0, 0.0, -1.0 ) ; + if ((unorm.x >= unorm.y) && (unorm.x >= unorm.z)) { + // x code + unorm = normal.x > 0.0 ? vec3(1.0, 0.0, 0.0) : vec3(-1.0, 0.0, 0.0); + } else if ((unorm.y > unorm.x) && (unorm.y >= unorm.z)) { + // y code + unorm = normal.y > 0.0 ? vec3(0.0, 1.0, 0.0) : vec3(0.0, -1.0, 0.0); + } else if ((unorm.z > unorm.x) && (unorm.z > unorm.y)) { + // z code + unorm = normal.z > 0.0 ? vec3(0.0, 0.0, 1.0) : vec3(0.0, 0.0, -1.0); } else { - // oh-no we messed up code - // has to be - unorm = vec3( 1.0, 0.0, 0.0 ); + // oh-no we messed up code + // has to be + unorm = vec3(1.0, 0.0, 0.0); } - float depth_fix = 1.0 / dot(normal,unorm); - + float depth_fix = 1.0 / dot(normal, unorm); depth = 2.0 * depth - 1.0; float linear_depth = 2.0 * z_near * z_far / (z_far + z_near - depth * (z_far - z_near)); - gl_FragDepth = (linear_depth*depth_fix+bias) / z_far; + gl_FragDepth = (linear_depth * depth_fix + bias) / z_far; } - diff --git a/drivers/gles3/shaders/cubemap_filter.glsl b/drivers/gles3/shaders/cubemap_filter.glsl index 485fbb6ee0..f65f798ff0 100644 --- a/drivers/gles3/shaders/cubemap_filter.glsl +++ b/drivers/gles3/shaders/cubemap_filter.glsl @@ -1,22 +1,24 @@ +/* clang-format off */ [vertex] +layout(location = 0) in highp vec2 vertex; +/* clang-format on */ -layout(location=0) in highp vec2 vertex; - -layout(location=4) in highp vec2 uv; +layout(location = 4) in highp vec2 uv; out highp vec2 uv_interp; void main() { - uv_interp=uv; - gl_Position=vec4(vertex,0,1); + uv_interp = uv; + gl_Position = vec4(vertex, 0, 1); } +/* clang-format off */ [fragment] - precision highp float; +/* clang-format on */ precision highp int; #ifdef USE_SOURCE_PANORAMA @@ -36,90 +38,85 @@ uniform int face_id; uniform float roughness; in highp vec2 uv_interp; - layout(location = 0) out vec4 frag_color; - #define M_PI 3.14159265359 - -vec3 texelCoordToVec(vec2 uv, int faceID) -{ - mat3 faceUvVectors[6]; -/* - // -x - faceUvVectors[1][0] = vec3(0.0, 0.0, 1.0); // u -> +z - faceUvVectors[1][1] = vec3(0.0, -1.0, 0.0); // v -> -y - faceUvVectors[1][2] = vec3(-1.0, 0.0, 0.0); // -x face - - // +x - faceUvVectors[0][0] = vec3(0.0, 0.0, -1.0); // u -> -z - faceUvVectors[0][1] = vec3(0.0, -1.0, 0.0); // v -> -y - faceUvVectors[0][2] = vec3(1.0, 0.0, 0.0); // +x face - - // -y - faceUvVectors[3][0] = vec3(1.0, 0.0, 0.0); // u -> +x - faceUvVectors[3][1] = vec3(0.0, 0.0, -1.0); // v -> -z - faceUvVectors[3][2] = vec3(0.0, -1.0, 0.0); // -y face - - // +y - faceUvVectors[2][0] = vec3(1.0, 0.0, 0.0); // u -> +x - faceUvVectors[2][1] = vec3(0.0, 0.0, 1.0); // v -> +z - faceUvVectors[2][2] = vec3(0.0, 1.0, 0.0); // +y face - - // -z - faceUvVectors[5][0] = vec3(-1.0, 0.0, 0.0); // u -> -x - faceUvVectors[5][1] = vec3(0.0, -1.0, 0.0); // v -> -y - faceUvVectors[5][2] = vec3(0.0, 0.0, -1.0); // -z face - - // +z - faceUvVectors[4][0] = vec3(1.0, 0.0, 0.0); // u -> +x - faceUvVectors[4][1] = vec3(0.0, -1.0, 0.0); // v -> -y - faceUvVectors[4][2] = vec3(0.0, 0.0, 1.0); // +z face -*/ - - // -x - faceUvVectors[0][0] = vec3(0.0, 0.0, 1.0); // u -> +z - faceUvVectors[0][1] = vec3(0.0, -1.0, 0.0); // v -> -y - faceUvVectors[0][2] = vec3(-1.0, 0.0, 0.0); // -x face - - // +x - faceUvVectors[1][0] = vec3(0.0, 0.0, -1.0); // u -> -z - faceUvVectors[1][1] = vec3(0.0, -1.0, 0.0); // v -> -y - faceUvVectors[1][2] = vec3(1.0, 0.0, 0.0); // +x face - - // -y - faceUvVectors[2][0] = vec3(1.0, 0.0, 0.0); // u -> +x - faceUvVectors[2][1] = vec3(0.0, 0.0, -1.0); // v -> -z - faceUvVectors[2][2] = vec3(0.0, -1.0, 0.0); // -y face - - // +y - faceUvVectors[3][0] = vec3(1.0, 0.0, 0.0); // u -> +x - faceUvVectors[3][1] = vec3(0.0, 0.0, 1.0); // v -> +z - faceUvVectors[3][2] = vec3(0.0, 1.0, 0.0); // +y face - - // -z - faceUvVectors[4][0] = vec3(-1.0, 0.0, 0.0); // u -> -x - faceUvVectors[4][1] = vec3(0.0, -1.0, 0.0); // v -> -y - faceUvVectors[4][2] = vec3(0.0, 0.0, -1.0); // -z face - - // +z - faceUvVectors[5][0] = vec3(1.0, 0.0, 0.0); // u -> +x - faceUvVectors[5][1] = vec3(0.0, -1.0, 0.0); // v -> -y - faceUvVectors[5][2] = vec3(0.0, 0.0, 1.0); // +z face - - // out = u * s_faceUv[0] + v * s_faceUv[1] + s_faceUv[2]. - vec3 result = (faceUvVectors[faceID][0] * uv.x) + (faceUvVectors[faceID][1] * uv.y) + faceUvVectors[faceID][2]; - return normalize(result); +vec3 texelCoordToVec(vec2 uv, int faceID) { + mat3 faceUvVectors[6]; + /* + // -x + faceUvVectors[1][0] = vec3(0.0, 0.0, 1.0); // u -> +z + faceUvVectors[1][1] = vec3(0.0, -1.0, 0.0); // v -> -y + faceUvVectors[1][2] = vec3(-1.0, 0.0, 0.0); // -x face + + // +x + faceUvVectors[0][0] = vec3(0.0, 0.0, -1.0); // u -> -z + faceUvVectors[0][1] = vec3(0.0, -1.0, 0.0); // v -> -y + faceUvVectors[0][2] = vec3(1.0, 0.0, 0.0); // +x face + + // -y + faceUvVectors[3][0] = vec3(1.0, 0.0, 0.0); // u -> +x + faceUvVectors[3][1] = vec3(0.0, 0.0, -1.0); // v -> -z + faceUvVectors[3][2] = vec3(0.0, -1.0, 0.0); // -y face + + // +y + faceUvVectors[2][0] = vec3(1.0, 0.0, 0.0); // u -> +x + faceUvVectors[2][1] = vec3(0.0, 0.0, 1.0); // v -> +z + faceUvVectors[2][2] = vec3(0.0, 1.0, 0.0); // +y face + + // -z + faceUvVectors[5][0] = vec3(-1.0, 0.0, 0.0); // u -> -x + faceUvVectors[5][1] = vec3(0.0, -1.0, 0.0); // v -> -y + faceUvVectors[5][2] = vec3(0.0, 0.0, -1.0); // -z face + + // +z + faceUvVectors[4][0] = vec3(1.0, 0.0, 0.0); // u -> +x + faceUvVectors[4][1] = vec3(0.0, -1.0, 0.0); // v -> -y + faceUvVectors[4][2] = vec3(0.0, 0.0, 1.0); // +z face + */ + + // -x + faceUvVectors[0][0] = vec3(0.0, 0.0, 1.0); // u -> +z + faceUvVectors[0][1] = vec3(0.0, -1.0, 0.0); // v -> -y + faceUvVectors[0][2] = vec3(-1.0, 0.0, 0.0); // -x face + + // +x + faceUvVectors[1][0] = vec3(0.0, 0.0, -1.0); // u -> -z + faceUvVectors[1][1] = vec3(0.0, -1.0, 0.0); // v -> -y + faceUvVectors[1][2] = vec3(1.0, 0.0, 0.0); // +x face + + // -y + faceUvVectors[2][0] = vec3(1.0, 0.0, 0.0); // u -> +x + faceUvVectors[2][1] = vec3(0.0, 0.0, -1.0); // v -> -z + faceUvVectors[2][2] = vec3(0.0, -1.0, 0.0); // -y face + + // +y + faceUvVectors[3][0] = vec3(1.0, 0.0, 0.0); // u -> +x + faceUvVectors[3][1] = vec3(0.0, 0.0, 1.0); // v -> +z + faceUvVectors[3][2] = vec3(0.0, 1.0, 0.0); // +y face + + // -z + faceUvVectors[4][0] = vec3(-1.0, 0.0, 0.0); // u -> -x + faceUvVectors[4][1] = vec3(0.0, -1.0, 0.0); // v -> -y + faceUvVectors[4][2] = vec3(0.0, 0.0, -1.0); // -z face + + // +z + faceUvVectors[5][0] = vec3(1.0, 0.0, 0.0); // u -> +x + faceUvVectors[5][1] = vec3(0.0, -1.0, 0.0); // v -> -y + faceUvVectors[5][2] = vec3(0.0, 0.0, 1.0); // +z face + + // out = u * s_faceUv[0] + v * s_faceUv[1] + s_faceUv[2]. + vec3 result = (faceUvVectors[faceID][0] * uv.x) + (faceUvVectors[faceID][1] * uv.y) + faceUvVectors[faceID][2]; + return normalize(result); } -vec3 ImportanceSampleGGX(vec2 Xi, float Roughness, vec3 N) -{ +vec3 ImportanceSampleGGX(vec2 Xi, float Roughness, vec3 N) { float a = Roughness * Roughness; // DISNEY'S ROUGHNESS [see Burley'12 siggraph] // Compute distribution direction float Phi = 2.0 * M_PI * Xi.x; - float CosTheta = sqrt((1.0 - Xi.y) / (1.0 + (a*a - 1.0) * Xi.y)); + float CosTheta = sqrt((1.0 - Xi.y) / (1.0 + (a * a - 1.0) * Xi.y)); float SinTheta = sqrt(1.0 - CosTheta * CosTheta); // Convert to spherical direction @@ -137,33 +134,29 @@ vec3 ImportanceSampleGGX(vec2 Xi, float Roughness, vec3 N) } // http://graphicrants.blogspot.com.au/2013/08/specular-brdf-reference.html -float GGX(float NdotV, float a) -{ +float GGX(float NdotV, float a) { float k = a / 2.0; return NdotV / (NdotV * (1.0 - k) + k); } // http://graphicrants.blogspot.com.au/2013/08/specular-brdf-reference.html -float G_Smith(float a, float nDotV, float nDotL) -{ +float G_Smith(float a, float nDotV, float nDotL) { return GGX(nDotL, a * a) * GGX(nDotV, a * a); } float radicalInverse_VdC(uint bits) { - bits = (bits << 16u) | (bits >> 16u); - bits = ((bits & 0x55555555u) << 1u) | ((bits & 0xAAAAAAAAu) >> 1u); - bits = ((bits & 0x33333333u) << 2u) | ((bits & 0xCCCCCCCCu) >> 2u); - bits = ((bits & 0x0F0F0F0Fu) << 4u) | ((bits & 0xF0F0F0F0u) >> 4u); - bits = ((bits & 0x00FF00FFu) << 8u) | ((bits & 0xFF00FF00u) >> 8u); - return float(bits) * 2.3283064365386963e-10; // / 0x100000000 + bits = (bits << 16u) | (bits >> 16u); + bits = ((bits & 0x55555555u) << 1u) | ((bits & 0xAAAAAAAAu) >> 1u); + bits = ((bits & 0x33333333u) << 2u) | ((bits & 0xCCCCCCCCu) >> 2u); + bits = ((bits & 0x0F0F0F0Fu) << 4u) | ((bits & 0xF0F0F0F0u) >> 4u); + bits = ((bits & 0x00FF00FFu) << 8u) | ((bits & 0xFF00FF00u) >> 8u); + return float(bits) * 2.3283064365386963e-10; // / 0x100000000 } vec2 Hammersley(uint i, uint N) { - return vec2(float(i)/float(N), radicalInverse_VdC(i)); + return vec2(float(i) / float(N), radicalInverse_VdC(i)); } - - #ifdef LOW_QUALITY #define SAMPLE_COUNT 64u @@ -178,37 +171,33 @@ uniform bool z_flip; #ifdef USE_SOURCE_PANORAMA -vec4 texturePanorama(vec3 normal,sampler2D pano ) { +vec4 texturePanorama(vec3 normal, sampler2D pano) { vec2 st = vec2( - atan(normal.x, normal.z), - acos(normal.y) - ); - - if(st.x < 0.0) - st.x += M_PI*2.0; + atan(normal.x, normal.z), + acos(normal.y)); - st/=vec2(M_PI*2.0,M_PI); + if (st.x < 0.0) + st.x += M_PI * 2.0; - return textureLod(pano,st,0.0); + st /= vec2(M_PI * 2.0, M_PI); + return textureLod(pano, st, 0.0); } #endif #ifdef USE_SOURCE_DUAL_PARABOLOID_ARRAY - vec4 textureDualParaboloidArray(vec3 normal) { vec3 norm = normalize(normal); - norm.xy/=1.0+abs(norm.z); - norm.xy=norm.xy * vec2(0.5,0.25) + vec2(0.5,0.25); - if (norm.z<0.0) { - norm.y=0.5-norm.y+0.5; + norm.xy /= 1.0 + abs(norm.z); + norm.xy = norm.xy * vec2(0.5, 0.25) + vec2(0.5, 0.25); + if (norm.z < 0.0) { + norm.y = 0.5 - norm.y + 0.5; } - return textureLod(source_dual_paraboloid_array, vec3(norm.xy, float(source_array_index) ), 0.0); - + return textureLod(source_dual_paraboloid_array, vec3(norm.xy, float(source_array_index)), 0.0); } #endif @@ -217,19 +206,18 @@ void main() { #ifdef USE_DUAL_PARABOLOID - vec3 N = vec3( uv_interp * 2.0 - 1.0, 0.0 ); - N.z = 0.5 - 0.5*((N.x * N.x) + (N.y * N.y)); + vec3 N = vec3(uv_interp * 2.0 - 1.0, 0.0); + N.z = 0.5 - 0.5 * ((N.x * N.x) + (N.y * N.y)); N = normalize(N); if (z_flip) { - N.y=-N.y; //y is flipped to improve blending between both sides - N.z=-N.z; + N.y = -N.y; //y is flipped to improve blending between both sides + N.z = -N.z; } - #else - vec2 uv = (uv_interp * 2.0) - 1.0; - vec3 N = texelCoordToVec(uv, face_id); + vec2 uv = (uv_interp * 2.0) - 1.0; + vec3 N = texelCoordToVec(uv, face_id); #endif //vec4 color = color_interp; @@ -237,49 +225,46 @@ void main() { #ifdef USE_SOURCE_PANORAMA - frag_color=vec4(texturePanorama(N,source_panorama).rgb,1.0); + frag_color = vec4(texturePanorama(N, source_panorama).rgb, 1.0); #endif #ifdef USE_SOURCE_DUAL_PARABOLOID_ARRAY - frag_color=vec4(textureDualParaboloidArray(N).rgb,1.0); + frag_color = vec4(textureDualParaboloidArray(N).rgb, 1.0); #endif #if !defined(USE_SOURCE_DUAL_PARABOLOID_ARRAY) && !defined(USE_SOURCE_PANORAMA) - N.y=-N.y; - frag_color=vec4(texture(N,source_cube).rgb,1.0); + N.y = -N.y; + frag_color = vec4(texture(N, source_cube).rgb, 1.0); #endif - - - #else vec4 sum = vec4(0.0, 0.0, 0.0, 0.0); - for(uint sampleNum = 0u; sampleNum < SAMPLE_COUNT; sampleNum++) { + for (uint sampleNum = 0u; sampleNum < SAMPLE_COUNT; sampleNum++) { vec2 xi = Hammersley(sampleNum, SAMPLE_COUNT); - vec3 H = ImportanceSampleGGX( xi, roughness, N ); - vec3 V = N; - vec3 L = normalize(2.0 * dot( V, H ) * H - V); + vec3 H = ImportanceSampleGGX(xi, roughness, N); + vec3 V = N; + vec3 L = (2.0 * dot(V, H) * H - V); - float ndotl = clamp(dot(N, L),0.0,1.0); + float ndotl = clamp(dot(N, L), 0.0, 1.0); - if (ndotl>0.0) { + if (ndotl > 0.0) { #ifdef USE_SOURCE_PANORAMA - sum.rgb += texturePanorama(H,source_panorama).rgb *ndotl; + sum.rgb += texturePanorama(L, source_panorama).rgb * ndotl; #endif #ifdef USE_SOURCE_DUAL_PARABOLOID_ARRAY - sum.rgb += textureDualParaboloidArray(H).rgb *ndotl; + sum.rgb += textureDualParaboloidArray(L).rgb * ndotl; #endif #if !defined(USE_SOURCE_DUAL_PARABOLOID_ARRAY) && !defined(USE_SOURCE_PANORAMA) - H.y=-H.y; - sum.rgb += textureLod(source_cube, H, 0.0).rgb *ndotl; + L.y = -L.y; + sum.rgb += textureLod(source_cube, L, 0.0).rgb * ndotl; #endif sum.a += ndotl; } @@ -289,6 +274,4 @@ void main() { frag_color = vec4(sum.rgb, 1.0); #endif - } - diff --git a/drivers/gles3/shaders/effect_blur.glsl b/drivers/gles3/shaders/effect_blur.glsl index c8567b4d53..fc15ca31b1 100644 --- a/drivers/gles3/shaders/effect_blur.glsl +++ b/drivers/gles3/shaders/effect_blur.glsl @@ -1,8 +1,9 @@ +/* clang-format off */ [vertex] - -layout(location=0) in highp vec4 vertex_attrib; -layout(location=4) in vec2 uv_in; +layout(location = 0) in highp vec4 vertex_attrib; +/* clang-format on */ +layout(location = 4) in vec2 uv_in; out vec2 uv_interp; @@ -23,11 +24,13 @@ void main() { #endif } +/* clang-format off */ [fragment] #if !defined(GLES_OVER_GL) precision mediump float; #endif +/* clang-format on */ in vec2 uv_interp; uniform sampler2D source_color; //texunit:0 @@ -39,7 +42,6 @@ uniform sampler2D source_ssao; //texunit:1 uniform float lod; uniform vec2 pixel_size; - layout(location = 0) out vec4 frag_color; #ifdef SSAO_MERGE @@ -48,31 +50,31 @@ uniform vec4 ssao_color; #endif -#if defined (GLOW_GAUSSIAN_HORIZONTAL) || defined(GLOW_GAUSSIAN_VERTICAL) +#if defined(GLOW_GAUSSIAN_HORIZONTAL) || defined(GLOW_GAUSSIAN_VERTICAL) uniform float glow_strength; #endif -#if defined(DOF_FAR_BLUR) || defined (DOF_NEAR_BLUR) +#if defined(DOF_FAR_BLUR) || defined(DOF_NEAR_BLUR) #ifdef DOF_QUALITY_LOW -const int dof_kernel_size=5; -const int dof_kernel_from=2; -const float dof_kernel[5] = float[] (0.153388,0.221461,0.250301,0.221461,0.153388); +const int dof_kernel_size = 5; +const int dof_kernel_from = 2; +const float dof_kernel[5] = float[](0.153388, 0.221461, 0.250301, 0.221461, 0.153388); #endif #ifdef DOF_QUALITY_MEDIUM -const int dof_kernel_size=11; -const int dof_kernel_from=5; -const float dof_kernel[11] = float[] (0.055037,0.072806,0.090506,0.105726,0.116061,0.119726,0.116061,0.105726,0.090506,0.072806,0.055037); +const int dof_kernel_size = 11; +const int dof_kernel_from = 5; +const float dof_kernel[11] = float[](0.055037, 0.072806, 0.090506, 0.105726, 0.116061, 0.119726, 0.116061, 0.105726, 0.090506, 0.072806, 0.055037); #endif #ifdef DOF_QUALITY_HIGH -const int dof_kernel_size=21; -const int dof_kernel_from=10; -const float dof_kernel[21] = float[] (0.028174,0.032676,0.037311,0.041944,0.046421,0.050582,0.054261,0.057307,0.059587,0.060998,0.061476,0.060998,0.059587,0.057307,0.054261,0.050582,0.046421,0.041944,0.037311,0.032676,0.028174); +const int dof_kernel_size = 21; +const int dof_kernel_from = 10; +const float dof_kernel[21] = float[](0.028174, 0.032676, 0.037311, 0.041944, 0.046421, 0.050582, 0.054261, 0.057307, 0.059587, 0.060998, 0.061476, 0.060998, 0.059587, 0.057307, 0.054261, 0.050582, 0.046421, 0.041944, 0.037311, 0.032676, 0.028174); #endif uniform sampler2D dof_source_depth; //texunit:1 @@ -88,11 +90,11 @@ uniform sampler2D source_dof_original; //texunit:2 #endif - #ifdef GLOW_FIRST_PASS uniform float exposure; uniform float white; +uniform highp float luminance_cap; #ifdef GLOW_USE_AUTO_EXPOSURE @@ -112,53 +114,51 @@ uniform float camera_z_near; void main() { - - #ifdef GAUSSIAN_HORIZONTAL vec2 pix_size = pixel_size; - pix_size*=0.5; //reading from larger buffer, so use more samples - vec4 color =textureLod( source_color, uv_interp+vec2( 0.0, 0.0)*pix_size,lod )*0.214607; - color+=textureLod( source_color, uv_interp+vec2( 1.0, 0.0)*pix_size,lod )*0.189879; - color+=textureLod( source_color, uv_interp+vec2( 2.0, 0.0)*pix_size,lod )*0.157305; - color+=textureLod( source_color, uv_interp+vec2( 3.0, 0.0)*pix_size,lod )*0.071303; - color+=textureLod( source_color, uv_interp+vec2(-1.0, 0.0)*pix_size,lod )*0.189879; - color+=textureLod( source_color, uv_interp+vec2(-2.0, 0.0)*pix_size,lod )*0.157305; - color+=textureLod( source_color, uv_interp+vec2(-3.0, 0.0)*pix_size,lod )*0.071303; + pix_size *= 0.5; //reading from larger buffer, so use more samples + vec4 color = textureLod(source_color, uv_interp + vec2(0.0, 0.0) * pix_size, lod) * 0.214607; + color += textureLod(source_color, uv_interp + vec2(1.0, 0.0) * pix_size, lod) * 0.189879; + color += textureLod(source_color, uv_interp + vec2(2.0, 0.0) * pix_size, lod) * 0.157305; + color += textureLod(source_color, uv_interp + vec2(3.0, 0.0) * pix_size, lod) * 0.071303; + color += textureLod(source_color, uv_interp + vec2(-1.0, 0.0) * pix_size, lod) * 0.189879; + color += textureLod(source_color, uv_interp + vec2(-2.0, 0.0) * pix_size, lod) * 0.157305; + color += textureLod(source_color, uv_interp + vec2(-3.0, 0.0) * pix_size, lod) * 0.071303; frag_color = color; #endif #ifdef GAUSSIAN_VERTICAL - vec4 color =textureLod( source_color, uv_interp+vec2( 0.0, 0.0)*pixel_size,lod )*0.38774; - color+=textureLod( source_color, uv_interp+vec2( 0.0, 1.0)*pixel_size,lod )*0.24477; - color+=textureLod( source_color, uv_interp+vec2( 0.0, 2.0)*pixel_size,lod )*0.06136; - color+=textureLod( source_color, uv_interp+vec2( 0.0,-1.0)*pixel_size,lod )*0.24477; - color+=textureLod( source_color, uv_interp+vec2( 0.0,-2.0)*pixel_size,lod )*0.06136; + vec4 color = textureLod(source_color, uv_interp + vec2(0.0, 0.0) * pixel_size, lod) * 0.38774; + color += textureLod(source_color, uv_interp + vec2(0.0, 1.0) * pixel_size, lod) * 0.24477; + color += textureLod(source_color, uv_interp + vec2(0.0, 2.0) * pixel_size, lod) * 0.06136; + color += textureLod(source_color, uv_interp + vec2(0.0, -1.0) * pixel_size, lod) * 0.24477; + color += textureLod(source_color, uv_interp + vec2(0.0, -2.0) * pixel_size, lod) * 0.06136; frag_color = color; #endif -//glow uses larger sigma for a more rounded blur effect + //glow uses larger sigma for a more rounded blur effect #ifdef GLOW_GAUSSIAN_HORIZONTAL vec2 pix_size = pixel_size; - pix_size*=0.5; //reading from larger buffer, so use more samples - vec4 color =textureLod( source_color, uv_interp+vec2( 0.0, 0.0)*pix_size,lod )*0.174938; - color+=textureLod( source_color, uv_interp+vec2( 1.0, 0.0)*pix_size,lod )*0.165569; - color+=textureLod( source_color, uv_interp+vec2( 2.0, 0.0)*pix_size,lod )*0.140367; - color+=textureLod( source_color, uv_interp+vec2( 3.0, 0.0)*pix_size,lod )*0.106595; - color+=textureLod( source_color, uv_interp+vec2(-1.0, 0.0)*pix_size,lod )*0.165569; - color+=textureLod( source_color, uv_interp+vec2(-2.0, 0.0)*pix_size,lod )*0.140367; - color+=textureLod( source_color, uv_interp+vec2(-3.0, 0.0)*pix_size,lod )*0.106595; - color*=glow_strength; + pix_size *= 0.5; //reading from larger buffer, so use more samples + vec4 color = textureLod(source_color, uv_interp + vec2(0.0, 0.0) * pix_size, lod) * 0.174938; + color += textureLod(source_color, uv_interp + vec2(1.0, 0.0) * pix_size, lod) * 0.165569; + color += textureLod(source_color, uv_interp + vec2(2.0, 0.0) * pix_size, lod) * 0.140367; + color += textureLod(source_color, uv_interp + vec2(3.0, 0.0) * pix_size, lod) * 0.106595; + color += textureLod(source_color, uv_interp + vec2(-1.0, 0.0) * pix_size, lod) * 0.165569; + color += textureLod(source_color, uv_interp + vec2(-2.0, 0.0) * pix_size, lod) * 0.140367; + color += textureLod(source_color, uv_interp + vec2(-3.0, 0.0) * pix_size, lod) * 0.106595; + color *= glow_strength; frag_color = color; #endif #ifdef GLOW_GAUSSIAN_VERTICAL - vec4 color =textureLod( source_color, uv_interp+vec2(0.0, 0.0)*pixel_size,lod )*0.288713; - color+=textureLod( source_color, uv_interp+vec2(0.0, 1.0)*pixel_size,lod )*0.233062; - color+=textureLod( source_color, uv_interp+vec2(0.0, 2.0)*pixel_size,lod )*0.122581; - color+=textureLod( source_color, uv_interp+vec2(0.0,-1.0)*pixel_size,lod )*0.233062; - color+=textureLod( source_color, uv_interp+vec2(0.0,-2.0)*pixel_size,lod )*0.122581; - color*=glow_strength; + vec4 color = textureLod(source_color, uv_interp + vec2(0.0, 0.0) * pixel_size, lod) * 0.288713; + color += textureLod(source_color, uv_interp + vec2(0.0, 1.0) * pixel_size, lod) * 0.233062; + color += textureLod(source_color, uv_interp + vec2(0.0, 2.0) * pixel_size, lod) * 0.122581; + color += textureLod(source_color, uv_interp + vec2(0.0, -1.0) * pixel_size, lod) * 0.233062; + color += textureLod(source_color, uv_interp + vec2(0.0, -2.0) * pixel_size, lod) * 0.122581; + color *= glow_strength; frag_color = color; #endif @@ -166,47 +166,45 @@ void main() { vec4 color_accum = vec4(0.0); - float depth = textureLod( dof_source_depth, uv_interp, 0.0).r; + float depth = textureLod(dof_source_depth, uv_interp, 0.0).r; depth = depth * 2.0 - 1.0; #ifdef USE_ORTHOGONAL_PROJECTION - depth = ((depth + (camera_z_far + camera_z_near)/(camera_z_far - camera_z_near)) * (camera_z_far - camera_z_near))/2.0; + depth = ((depth + (camera_z_far + camera_z_near) / (camera_z_far - camera_z_near)) * (camera_z_far - camera_z_near)) / 2.0; #else depth = 2.0 * camera_z_near * camera_z_far / (camera_z_far + camera_z_near - depth * (camera_z_far - camera_z_near)); #endif - float amount = smoothstep(dof_begin,dof_end,depth); - float k_accum=0.0; + float amount = smoothstep(dof_begin, dof_end, depth); + float k_accum = 0.0; - for(int i=0;i<dof_kernel_size;i++) { + for (int i = 0; i < dof_kernel_size; i++) { - int int_ofs = i-dof_kernel_from; + int int_ofs = i - dof_kernel_from; vec2 tap_uv = uv_interp + dof_dir * float(int_ofs) * amount * dof_radius; float tap_k = dof_kernel[i]; - float tap_depth = texture( dof_source_depth, tap_uv, 0.0).r; + float tap_depth = texture(dof_source_depth, tap_uv, 0.0).r; tap_depth = tap_depth * 2.0 - 1.0; #ifdef USE_ORTHOGONAL_PROJECTION - tap_depth = ((tap_depth + (camera_z_far + camera_z_near)/(camera_z_far - camera_z_near)) * (camera_z_far - camera_z_near))/2.0; + tap_depth = ((tap_depth + (camera_z_far + camera_z_near) / (camera_z_far - camera_z_near)) * (camera_z_far - camera_z_near)) / 2.0; #else tap_depth = 2.0 * camera_z_near * camera_z_far / (camera_z_far + camera_z_near - tap_depth * (camera_z_far - camera_z_near)); #endif - float tap_amount = mix(smoothstep(dof_begin,dof_end,tap_depth),1.0,int_ofs==0); - tap_amount*=tap_amount*tap_amount; //prevent undesired glow effect - - vec4 tap_color = textureLod( source_color, tap_uv, 0.0) * tap_k; - - k_accum+=tap_k*tap_amount; - color_accum+=tap_color*tap_amount; + float tap_amount = mix(smoothstep(dof_begin, dof_end, tap_depth), 1.0, int_ofs == 0); + tap_amount *= tap_amount * tap_amount; //prevent undesired glow effect + vec4 tap_color = textureLod(source_color, tap_uv, 0.0) * tap_k; + k_accum += tap_k * tap_amount; + color_accum += tap_color * tap_amount; } - if (k_accum>0.0) { - color_accum/=k_accum; + if (k_accum > 0.0) { + color_accum /= k_accum; } - frag_color = color_accum;///k_accum; + frag_color = color_accum; ///k_accum; #endif @@ -214,47 +212,45 @@ void main() { vec4 color_accum = vec4(0.0); - float max_accum=0.0; + float max_accum = 0.0; - for(int i=0;i<dof_kernel_size;i++) { + for (int i = 0; i < dof_kernel_size; i++) { - int int_ofs = i-dof_kernel_from; + int int_ofs = i - dof_kernel_from; vec2 tap_uv = uv_interp + dof_dir * float(int_ofs) * dof_radius; - float ofs_influence = max(0.0,1.0-float(abs(int_ofs))/float(dof_kernel_from)); + float ofs_influence = max(0.0, 1.0 - float(abs(int_ofs)) / float(dof_kernel_from)); float tap_k = dof_kernel[i]; - vec4 tap_color = textureLod( source_color, tap_uv, 0.0); + vec4 tap_color = textureLod(source_color, tap_uv, 0.0); - float tap_depth = texture( dof_source_depth, tap_uv, 0.0).r; + float tap_depth = texture(dof_source_depth, tap_uv, 0.0).r; tap_depth = tap_depth * 2.0 - 1.0; -#ifdef USE_ORTHOGONAL_PROJECTION - tap_depth = ((tap_depth + (camera_z_far + camera_z_near)/(camera_z_far - camera_z_near)) * (camera_z_far - camera_z_near))/2.0; +#ifdef USE_ORTHOGONAL_PROJECTION + tap_depth = ((tap_depth + (camera_z_far + camera_z_near) / (camera_z_far - camera_z_near)) * (camera_z_far - camera_z_near)) / 2.0; #else tap_depth = 2.0 * camera_z_near * camera_z_far / (camera_z_far + camera_z_near - tap_depth * (camera_z_far - camera_z_near)); #endif - float tap_amount = 1.0-smoothstep(dof_end,dof_begin,tap_depth); - tap_amount*=tap_amount*tap_amount; //prevent undesired glow effect + float tap_amount = 1.0 - smoothstep(dof_end, dof_begin, tap_depth); + tap_amount *= tap_amount * tap_amount; //prevent undesired glow effect #ifdef DOF_NEAR_FIRST_TAP - tap_color.a= 1.0-smoothstep(dof_end,dof_begin,tap_depth); + tap_color.a = 1.0 - smoothstep(dof_end, dof_begin, tap_depth); #endif - max_accum=max(max_accum,tap_amount*ofs_influence); - - color_accum+=tap_color*tap_k; + max_accum = max(max_accum, tap_amount * ofs_influence); + color_accum += tap_color * tap_k; } - color_accum.a=max(color_accum.a,sqrt(max_accum)); - + color_accum.a = max(color_accum.a, sqrt(max_accum)); #ifdef DOF_NEAR_BLUR_MERGE - vec4 original = textureLod( source_dof_original, uv_interp, 0.0); - color_accum = mix(original,color_accum,color_accum.a); + vec4 original = textureLod(source_dof_original, uv_interp, 0.0); + color_accum = mix(original, color_accum, color_accum.a); #endif @@ -265,37 +261,32 @@ void main() { #endif - - #ifdef GLOW_FIRST_PASS #ifdef GLOW_USE_AUTO_EXPOSURE - frag_color/=texelFetch(source_auto_exposure,ivec2(0,0),0).r/auto_exposure_grey; + frag_color /= texelFetch(source_auto_exposure, ivec2(0, 0), 0).r / auto_exposure_grey; #endif - frag_color*=exposure; + frag_color *= exposure; - float luminance = max(frag_color.r,max(frag_color.g,frag_color.b)); - float feedback = max( smoothstep(glow_hdr_threshold,glow_hdr_threshold+glow_hdr_scale,luminance), glow_bloom ); + float luminance = max(frag_color.r, max(frag_color.g, frag_color.b)); + float feedback = max(smoothstep(glow_hdr_threshold, glow_hdr_threshold + glow_hdr_scale, luminance), glow_bloom); - frag_color *= feedback; + frag_color = min(frag_color * feedback, vec4(luminance_cap)); #endif - #ifdef SIMPLE_COPY - vec4 color =textureLod( source_color, uv_interp,0.0); + vec4 color = textureLod(source_color, uv_interp, 0.0); frag_color = color; #endif #ifdef SSAO_MERGE - vec4 color =textureLod( source_color, uv_interp,0.0); - float ssao =textureLod( source_ssao, uv_interp,0.0).r; + vec4 color = textureLod(source_color, uv_interp, 0.0); + float ssao = textureLod(source_ssao, uv_interp, 0.0).r; - frag_color = vec4( mix(color.rgb,color.rgb*mix(ssao_color.rgb,vec3(1.0),ssao),color.a), 1.0 ); + frag_color = vec4(mix(color.rgb, color.rgb * mix(ssao_color.rgb, vec3(1.0), ssao), color.a), 1.0); #endif - - } diff --git a/drivers/gles3/shaders/exposure.glsl b/drivers/gles3/shaders/exposure.glsl index 001b90a0f1..759adcda06 100644 --- a/drivers/gles3/shaders/exposure.glsl +++ b/drivers/gles3/shaders/exposure.glsl @@ -1,19 +1,19 @@ +/* clang-format off */ [vertex] - -layout(location=0) in highp vec4 vertex_attrib; - +layout(location = 0) in highp vec4 vertex_attrib; +/* clang-format on */ void main() { gl_Position = vertex_attrib; - } +/* clang-format off */ [fragment] - uniform highp sampler2D source_exposure; //texunit:0 +/* clang-format on */ #ifdef EXPOSURE_BEGIN @@ -33,66 +33,56 @@ uniform highp float max_luminance; layout(location = 0) out highp float exposure; - - void main() { - - #ifdef EXPOSURE_BEGIN - - ivec2 src_pos = ivec2(gl_FragCoord.xy)*source_render_size/target_size; + ivec2 src_pos = ivec2(gl_FragCoord.xy) * source_render_size / target_size; #if 1 //more precise and expensive, but less jittery - ivec2 next_pos = ivec2(gl_FragCoord.xy+ivec2(1))*source_render_size/target_size; - next_pos = max(next_pos,src_pos+ivec2(1)); //so it at least reads one pixel - highp vec3 source_color=vec3(0.0); - for(int i=src_pos.x;i<next_pos.x;i++) { - for(int j=src_pos.y;j<next_pos.y;j++) { - source_color += texelFetch(source_exposure,ivec2(i,j),0).rgb; + ivec2 next_pos = ivec2(gl_FragCoord.xy + ivec2(1)) * source_render_size / target_size; + next_pos = max(next_pos, src_pos + ivec2(1)); //so it at least reads one pixel + highp vec3 source_color = vec3(0.0); + for (int i = src_pos.x; i < next_pos.x; i++) { + for (int j = src_pos.y; j < next_pos.y; j++) { + source_color += texelFetch(source_exposure, ivec2(i, j), 0).rgb; } } - source_color/=float( (next_pos.x-src_pos.x)*(next_pos.y-src_pos.y) ); + source_color /= float((next_pos.x - src_pos.x) * (next_pos.y - src_pos.y)); #else - highp vec3 source_color = texelFetch(source_exposure,src_pos,0).rgb; + highp vec3 source_color = texelFetch(source_exposure, src_pos, 0).rgb; #endif - exposure = max(source_color.r,max(source_color.g,source_color.b)); + exposure = max(source_color.r, max(source_color.g, source_color.b)); #else ivec2 coord = ivec2(gl_FragCoord.xy); - exposure = texelFetch(source_exposure,coord*3+ivec2(0,0),0).r; - exposure += texelFetch(source_exposure,coord*3+ivec2(1,0),0).r; - exposure += texelFetch(source_exposure,coord*3+ivec2(2,0),0).r; - exposure += texelFetch(source_exposure,coord*3+ivec2(0,1),0).r; - exposure += texelFetch(source_exposure,coord*3+ivec2(1,1),0).r; - exposure += texelFetch(source_exposure,coord*3+ivec2(2,1),0).r; - exposure += texelFetch(source_exposure,coord*3+ivec2(0,2),0).r; - exposure += texelFetch(source_exposure,coord*3+ivec2(1,2),0).r; - exposure += texelFetch(source_exposure,coord*3+ivec2(2,2),0).r; - exposure *= (1.0/9.0); + exposure = texelFetch(source_exposure, coord * 3 + ivec2(0, 0), 0).r; + exposure += texelFetch(source_exposure, coord * 3 + ivec2(1, 0), 0).r; + exposure += texelFetch(source_exposure, coord * 3 + ivec2(2, 0), 0).r; + exposure += texelFetch(source_exposure, coord * 3 + ivec2(0, 1), 0).r; + exposure += texelFetch(source_exposure, coord * 3 + ivec2(1, 1), 0).r; + exposure += texelFetch(source_exposure, coord * 3 + ivec2(2, 1), 0).r; + exposure += texelFetch(source_exposure, coord * 3 + ivec2(0, 2), 0).r; + exposure += texelFetch(source_exposure, coord * 3 + ivec2(1, 2), 0).r; + exposure += texelFetch(source_exposure, coord * 3 + ivec2(2, 2), 0).r; + exposure *= (1.0 / 9.0); #ifdef EXPOSURE_END #ifdef EXPOSURE_FORCE_SET //will stay as is #else - highp float prev_lum = texelFetch(prev_exposure,ivec2(0,0),0).r; //1 pixel previous exposure - exposure = clamp( prev_lum + (exposure-prev_lum)*exposure_adjust,min_luminance,max_luminance); + highp float prev_lum = texelFetch(prev_exposure, ivec2(0, 0), 0).r; //1 pixel previous exposure + exposure = clamp(prev_lum + (exposure - prev_lum) * exposure_adjust, min_luminance, max_luminance); #endif //EXPOSURE_FORCE_SET - #endif //EXPOSURE_END #endif //EXPOSURE_BEGIN - - } - - diff --git a/drivers/gles3/shaders/lens_distorted.glsl b/drivers/gles3/shaders/lens_distorted.glsl new file mode 100644 index 0000000000..7b9d0b347f --- /dev/null +++ b/drivers/gles3/shaders/lens_distorted.glsl @@ -0,0 +1,64 @@ +/* clang-format off */ +[vertex] + +layout(location = 0) in highp vec4 vertex_attrib; +/* clang-format on */ + +uniform vec2 offset; +uniform vec2 scale; + +out vec2 uv_interp; + +void main() { + + uv_interp = vertex_attrib.xy * 2.0 - 1.0; + + vec2 v = vertex_attrib.xy * scale + offset; + gl_Position = vec4(v, 0.0, 1.0); +} + +/* clang-format off */ +[fragment] + +uniform sampler2D source; //texunit:0 +/* clang-format on */ + +uniform vec2 eye_center; +uniform float k1; +uniform float k2; +uniform float upscale; +uniform float aspect_ratio; + +in vec2 uv_interp; + +layout(location = 0) out vec4 frag_color; + +void main() { + vec2 coords = uv_interp; + vec2 offset = coords - eye_center; + + // take aspect ratio into account + offset.y /= aspect_ratio; + + // distort + vec2 offset_sq = offset * offset; + float radius_sq = offset_sq.x + offset_sq.y; + float radius_s4 = radius_sq * radius_sq; + float distortion_scale = 1.0 + (k1 * radius_sq) + (k2 * radius_s4); + offset *= distortion_scale; + + // reapply aspect ratio + offset.y *= aspect_ratio; + + // add our eye center back in + coords = offset + eye_center; + coords /= upscale; + + // and check our color + if (coords.x < -1.0 || coords.y < -1.0 || coords.x > 1.0 || coords.y > 1.0) { + frag_color = vec4(0.0, 0.0, 0.0, 1.0); + } else { + coords = (coords + vec2(1.0)) / vec2(2.0); + frag_color = textureLod(source, coords, 0.0); + } +} diff --git a/drivers/gles3/shaders/particles.glsl b/drivers/gles3/shaders/particles.glsl index fbee08c0fe..8523c08597 100644 --- a/drivers/gles3/shaders/particles.glsl +++ b/drivers/gles3/shaders/particles.glsl @@ -1,14 +1,13 @@ +/* clang-format off */ [vertex] - - -layout(location=0) in highp vec4 color; -layout(location=1) in highp vec4 velocity_active; -layout(location=2) in highp vec4 custom; -layout(location=3) in highp vec4 xform_1; -layout(location=4) in highp vec4 xform_2; -layout(location=5) in highp vec4 xform_3; - +layout(location = 0) in highp vec4 color; +/* clang-format on */ +layout(location = 1) in highp vec4 velocity_active; +layout(location = 2) in highp vec4 custom; +layout(location = 3) in highp vec4 xform_1; +layout(location = 4) in highp vec4 xform_2; +layout(location = 5) in highp vec4 xform_3; struct Attractor { @@ -39,7 +38,6 @@ uniform float lifetime; uniform mat4 emission_transform; uniform uint random_seed; - out highp vec4 out_color; //tfb: out highp vec4 out_velocity_active; //tfb: out highp vec4 out_custom; //tfb: @@ -47,20 +45,24 @@ out highp vec4 out_xform_1; //tfb: out highp vec4 out_xform_2; //tfb: out highp vec4 out_xform_3; //tfb: - #if defined(USE_MATERIAL) +/* clang-format off */ layout(std140) uniform UniformData { //ubo:0 MATERIAL_UNIFORMS }; +/* clang-format on */ #endif +/* clang-format off */ VERTEX_SHADER_GLOBALS +/* clang-format on */ + uint hash(uint x) { x = ((x >> uint(16)) ^ x) * uint(0x45d9f3b); @@ -69,13 +71,12 @@ uint hash(uint x) { return x; } - void main() { #ifdef PARTICLES_COPY - out_color=color; - out_velocity_active=velocity_active; + out_color = color; + out_velocity_active = velocity_active; out_custom = custom; out_xform_1 = xform_1; out_xform_2 = xform_2; @@ -83,47 +84,47 @@ void main() { #else - bool apply_forces=true; - bool apply_velocity=true; - float local_delta=delta; + bool apply_forces = true; + bool apply_velocity = true; + float local_delta = delta; float mass = 1.0; - float restart_phase = float(gl_VertexID)/float(total_particles); + float restart_phase = float(gl_VertexID) / float(total_particles); - if (randomness>0.0) { + if (randomness > 0.0) { uint seed = cycle; if (restart_phase >= system_phase) { - seed-=uint(1); + seed -= uint(1); } - seed*=uint(total_particles); - seed+=uint(gl_VertexID); + seed *= uint(total_particles); + seed += uint(gl_VertexID); float random = float(hash(seed) % uint(65536)) / 65536.0; - restart_phase+=randomness * random * 1.0 / float(total_particles); + restart_phase += randomness * random * 1.0 / float(total_particles); } - restart_phase*= (1.0-explosiveness); - bool restart=false; + restart_phase *= (1.0 - explosiveness); + bool restart = false; bool shader_active = velocity_active.a > 0.5; if (system_phase > prev_system_phase) { // restart_phase >= prev_system_phase is used so particles emit in the first frame they are processed - if (restart_phase >= prev_system_phase && restart_phase < system_phase ) { - restart=true; + if (restart_phase >= prev_system_phase && restart_phase < system_phase) { + restart = true; #ifdef USE_FRACTIONAL_DELTA local_delta = (system_phase - restart_phase) * lifetime; #endif } - } else if(delta > 0.0) { + } else if (delta > 0.0) { if (restart_phase >= prev_system_phase) { - restart=true; + restart = true; #ifdef USE_FRACTIONAL_DELTA local_delta = (1.0 - restart_phase + system_phase) * lifetime; #endif - } else if (restart_phase < system_phase ) { - restart=true; + } else if (restart_phase < system_phase) { + restart = true; #ifdef USE_FRACTIONAL_DELTA local_delta = (system_phase - restart_phase) * lifetime; #endif @@ -133,14 +134,14 @@ void main() { uint current_cycle = cycle; if (system_phase < restart_phase) { - current_cycle-=uint(1); + current_cycle -= uint(1); } uint particle_number = current_cycle * uint(total_particles) + uint(gl_VertexID); int index = int(gl_VertexID); if (restart) { - shader_active=emitting; + shader_active = emitting; } mat4 xform; @@ -150,30 +151,33 @@ void main() { #else if (clear || restart) { #endif - out_color=vec4(1.0); - out_velocity_active=vec4(0.0); - out_custom=vec4(0.0); + out_color = vec4(1.0); + out_velocity_active = vec4(0.0); + out_custom = vec4(0.0); if (!restart) - shader_active=false; + shader_active = false; xform = mat4( - vec4(1.0,0.0,0.0,0.0), - vec4(0.0,1.0,0.0,0.0), - vec4(0.0,0.0,1.0,0.0), - vec4(0.0,0.0,0.0,1.0) - ); + vec4(1.0, 0.0, 0.0, 0.0), + vec4(0.0, 1.0, 0.0, 0.0), + vec4(0.0, 0.0, 1.0, 0.0), + vec4(0.0, 0.0, 0.0, 1.0)); } else { - out_color=color; - out_velocity_active=velocity_active; - out_custom=custom; - xform = transpose(mat4(xform_1,xform_2,xform_3,vec4(vec3(0.0),1.0))); + out_color = color; + out_velocity_active = velocity_active; + out_custom = custom; + xform = transpose(mat4(xform_1, xform_2, xform_3, vec4(vec3(0.0), 1.0))); } if (shader_active) { //execute shader { + /* clang-format off */ + VERTEX_SHADER_CODE + + /* clang-format on */ } #if !defined(DISABLE_FORCE) @@ -181,26 +185,25 @@ VERTEX_SHADER_CODE if (false) { vec3 force = vec3(0.0); - for(int i=0;i<attractor_count;i++) { + for (int i = 0; i < attractor_count; i++) { vec3 rel_vec = xform[3].xyz - attractors[i].pos; float dist = length(rel_vec); if (attractors[i].radius < dist) continue; - if (attractors[i].eat_radius>0.0 && attractors[i].eat_radius > dist) { - out_velocity_active.a=0.0; + if (attractors[i].eat_radius > 0.0 && attractors[i].eat_radius > dist) { + out_velocity_active.a = 0.0; } rel_vec = normalize(rel_vec); - float attenuation = pow(dist / attractors[i].radius,attractors[i].attenuation); + float attenuation = pow(dist / attractors[i].radius, attractors[i].attenuation); - if (attractors[i].dir==vec3(0.0)) { + if (attractors[i].dir == vec3(0.0)) { //towards center - force+=attractors[i].strength * rel_vec * attenuation * mass; + force += attractors[i].strength * rel_vec * attenuation * mass; } else { - force+=attractors[i].strength * attractors[i].dir * attenuation *mass; - + force += attractors[i].strength * attractors[i].dir * attenuation * mass; } } @@ -216,25 +219,24 @@ VERTEX_SHADER_CODE } #endif } else { - xform=mat4(0.0); + xform = mat4(0.0); } xform = transpose(xform); - out_velocity_active.a = mix(0.0,1.0,shader_active); + out_velocity_active.a = mix(0.0, 1.0, shader_active); out_xform_1 = xform[0]; out_xform_2 = xform[1]; out_xform_3 = xform[2]; #endif //PARTICLES_COPY - } +/* clang-format off */ [fragment] -//any code here is never executed, stuff is filled just so it works - +// any code here is never executed, stuff is filled just so it works #if defined(USE_MATERIAL) @@ -251,10 +253,15 @@ FRAGMENT_SHADER_GLOBALS void main() { { + LIGHT_SHADER_CODE + } { + FRAGMENT_SHADER_CODE + } } +/* clang-format on */ diff --git a/drivers/gles3/shaders/resolve.glsl b/drivers/gles3/shaders/resolve.glsl index 0b50a9c57b..d64d8308c1 100644 --- a/drivers/gles3/shaders/resolve.glsl +++ b/drivers/gles3/shaders/resolve.glsl @@ -1,27 +1,29 @@ +/* clang-format off */ [vertex] - -layout(location=0) in highp vec4 vertex_attrib; -layout(location=4) in vec2 uv_in; +layout(location = 0) in highp vec4 vertex_attrib; +/* clang-format on */ +layout(location = 4) in vec2 uv_in; out vec2 uv_interp; - void main() { uv_interp = uv_in; gl_Position = vertex_attrib; } +/* clang-format off */ [fragment] #if !defined(GLES_OVER_GL) precision mediump float; #endif +/* clang-format on */ in vec2 uv_interp; -uniform sampler2D source_specular; //texunit:0 -uniform sampler2D source_ssr; //texunit:1 +uniform sampler2D source_specular; // texunit:0 +uniform sampler2D source_ssr; // texunit:1 uniform vec2 pixel_size; @@ -31,14 +33,12 @@ layout(location = 0) out vec4 frag_color; void main() { - vec4 specular = texture( source_specular, uv_interp ); + vec4 specular = texture(source_specular, uv_interp); #ifdef USE_SSR - - vec4 ssr = textureLod(source_ssr,uv_interp,0.0); - specular.rgb = mix(specular.rgb,ssr.rgb*specular.a,ssr.a); + vec4 ssr = textureLod(source_ssr, uv_interp, 0.0); + specular.rgb = mix(specular.rgb, ssr.rgb * specular.a, ssr.a); #endif - frag_color = vec4(specular.rgb,1.0); + frag_color = vec4(specular.rgb, 1.0); } - diff --git a/drivers/gles3/shaders/scene.glsl b/drivers/gles3/shaders/scene.glsl index dee8bcbc58..ff273e4e9f 100644 --- a/drivers/gles3/shaders/scene.glsl +++ b/drivers/gles3/shaders/scene.glsl @@ -1,7 +1,10 @@ +/* clang-format off */ [vertex] #define M_PI 3.14159265359 +#define SHADER_IS_SRGB false + /* from VisualServer: @@ -16,50 +19,48 @@ ARRAY_WEIGHTS=7, ARRAY_INDEX=8, */ -//hack to use uv if no uv present so it works with lightmap - +// hack to use uv if no uv present so it works with lightmap /* INPUT ATTRIBS */ -layout(location=0) in highp vec4 vertex_attrib; -layout(location=1) in vec3 normal_attrib; +layout(location = 0) in highp vec4 vertex_attrib; +/* clang-format on */ +layout(location = 1) in vec3 normal_attrib; #if defined(ENABLE_TANGENT_INTERP) || defined(ENABLE_NORMALMAP) || defined(LIGHT_USE_ANISOTROPY) -layout(location=2) in vec4 tangent_attrib; +layout(location = 2) in vec4 tangent_attrib; #endif #if defined(ENABLE_COLOR_INTERP) -layout(location=3) in vec4 color_attrib; +layout(location = 3) in vec4 color_attrib; #endif #if defined(ENABLE_UV_INTERP) -layout(location=4) in vec2 uv_attrib; +layout(location = 4) in vec2 uv_attrib; #endif #if defined(ENABLE_UV2_INTERP) || defined(USE_LIGHTMAP) -layout(location=5) in vec2 uv2_attrib; +layout(location = 5) in vec2 uv2_attrib; #endif -uniform float normal_mult; - #ifdef USE_SKELETON -layout(location=6) in uvec4 bone_indices; // attrib:6 -layout(location=7) in vec4 bone_weights; // attrib:7 +layout(location = 6) in uvec4 bone_indices; // attrib:6 +layout(location = 7) in vec4 bone_weights; // attrib:7 #endif #ifdef USE_INSTANCING -layout(location=8) in highp vec4 instance_xform0; -layout(location=9) in highp vec4 instance_xform1; -layout(location=10) in highp vec4 instance_xform2; -layout(location=11) in lowp vec4 instance_color; +layout(location = 8) in highp vec4 instance_xform0; +layout(location = 9) in highp vec4 instance_xform1; +layout(location = 10) in highp vec4 instance_xform2; +layout(location = 11) in lowp vec4 instance_color; #if defined(ENABLE_INSTANCE_CUSTOM) -layout(location=12) in highp vec4 instance_custom_data; +layout(location = 12) in highp vec4 instance_custom_data; #endif #endif -layout(std140) uniform SceneData { //ubo:0 +layout(std140) uniform SceneData { // ubo:0 highp mat4 projection_matrix; highp mat4 inv_projection_matrix; @@ -95,6 +96,8 @@ layout(std140) uniform SceneData { //ubo:0 bool fog_depth_enabled; highp float fog_depth_begin; + highp float fog_depth_end; + mediump float fog_density; highp float fog_depth_curve; bool fog_transmit_enabled; highp float fog_transmit_curve; @@ -102,12 +105,10 @@ layout(std140) uniform SceneData { //ubo:0 highp float fog_height_min; highp float fog_height_max; highp float fog_height_curve; - }; uniform highp mat4 world_transform; - #ifdef USE_LIGHT_DIRECTIONAL layout(std140) uniform DirectionalLightData { //ubo:3 @@ -115,7 +116,7 @@ layout(std140) uniform DirectionalLightData { //ubo:3 highp vec4 light_pos_inv_radius; mediump vec4 light_direction_attenuation; mediump vec4 light_color_energy; - mediump vec4 light_params; //cone attenuation, angle, specular, shadow enabled, + mediump vec4 light_params; // cone attenuation, angle, specular, shadow enabled, mediump vec4 light_clamp; mediump vec4 shadow_color_contact; highp mat4 shadow_matrix1; @@ -135,14 +136,12 @@ struct LightData { highp vec4 light_pos_inv_radius; mediump vec4 light_direction_attenuation; mediump vec4 light_color_energy; - mediump vec4 light_params; //cone attenuation, angle, specular, shadow enabled, + mediump vec4 light_params; // cone attenuation, angle, specular, shadow enabled, mediump vec4 light_clamp; mediump vec4 shadow_color_contact; highp mat4 shadow_matrix; - }; - layout(std140) uniform OmniLightData { //ubo:4 LightData omni_lights[MAX_LIGHT_DATA_STRUCTS]; @@ -155,7 +154,6 @@ layout(std140) uniform SpotLightData { //ubo:5 #ifdef USE_FORWARD_LIGHTING - uniform int omni_light_indices[MAX_FORWARD_LIGHTS]; uniform int omni_light_count; @@ -167,49 +165,45 @@ uniform int spot_light_count; out vec4 diffuse_light_interp; out vec4 specular_light_interp; -void light_compute(vec3 N, vec3 L,vec3 V, vec3 light_color, float roughness, inout vec3 diffuse, inout vec3 specular) { +void light_compute(vec3 N, vec3 L, vec3 V, vec3 light_color, float roughness, inout vec3 diffuse, inout vec3 specular) { - float dotNL = max(dot(N,L), 0.0 ); + float dotNL = max(dot(N, L), 0.0); diffuse += dotNL * light_color / M_PI; if (roughness > 0.0) { vec3 H = normalize(V + L); - float dotNH = max(dot(N,H), 0.0 ); - float intensity = (roughness >= 1.0 ? 1.0 : pow( dotNH, (1.0-roughness) * 256.0)); + float dotNH = max(dot(N, H), 0.0); + float intensity = (roughness >= 1.0 ? 1.0 : pow(dotNH, (1.0 - roughness) * 256.0)); specular += light_color * intensity; - } } -void light_process_omni(int idx, vec3 vertex, vec3 eye_vec,vec3 normal, float roughness,inout vec3 diffuse, inout vec3 specular) { +void light_process_omni(int idx, vec3 vertex, vec3 eye_vec, vec3 normal, float roughness, inout vec3 diffuse, inout vec3 specular) { - vec3 light_rel_vec = omni_lights[idx].light_pos_inv_radius.xyz-vertex; - float light_length = length( light_rel_vec ); - float normalized_distance = light_length*omni_lights[idx].light_pos_inv_radius.w; - vec3 light_attenuation = vec3(pow( max(1.0 - normalized_distance, 0.0), omni_lights[idx].light_direction_attenuation.w )); - - light_compute(normal,normalize(light_rel_vec),eye_vec,omni_lights[idx].light_color_energy.rgb * light_attenuation,roughness,diffuse,specular); + vec3 light_rel_vec = omni_lights[idx].light_pos_inv_radius.xyz - vertex; + float light_length = length(light_rel_vec); + float normalized_distance = light_length * omni_lights[idx].light_pos_inv_radius.w; + vec3 light_attenuation = vec3(pow(max(1.0 - normalized_distance, 0.0), omni_lights[idx].light_direction_attenuation.w)); + light_compute(normal, normalize(light_rel_vec), eye_vec, omni_lights[idx].light_color_energy.rgb * light_attenuation, roughness, diffuse, specular); } void light_process_spot(int idx, vec3 vertex, vec3 eye_vec, vec3 normal, float roughness, inout vec3 diffuse, inout vec3 specular) { - vec3 light_rel_vec = spot_lights[idx].light_pos_inv_radius.xyz-vertex; - float light_length = length( light_rel_vec ); - float normalized_distance = light_length*spot_lights[idx].light_pos_inv_radius.w; - vec3 light_attenuation = vec3(pow( max(1.0 - normalized_distance, 0.001), spot_lights[idx].light_direction_attenuation.w )); + vec3 light_rel_vec = spot_lights[idx].light_pos_inv_radius.xyz - vertex; + float light_length = length(light_rel_vec); + float normalized_distance = light_length * spot_lights[idx].light_pos_inv_radius.w; + vec3 light_attenuation = vec3(pow(max(1.0 - normalized_distance, 0.001), spot_lights[idx].light_direction_attenuation.w)); vec3 spot_dir = spot_lights[idx].light_direction_attenuation.xyz; - float spot_cutoff=spot_lights[idx].light_params.y; - float scos = max(dot(-normalize(light_rel_vec), spot_dir),spot_cutoff); + float spot_cutoff = spot_lights[idx].light_params.y; + float scos = max(dot(-normalize(light_rel_vec), spot_dir), spot_cutoff); float spot_rim = (1.0 - scos) / (1.0 - spot_cutoff); - light_attenuation *= 1.0 - pow( max(spot_rim,0.001), spot_lights[idx].light_params.x); - + light_attenuation *= 1.0 - pow(max(spot_rim, 0.001), spot_lights[idx].light_params.x); - light_compute(normal,normalize(light_rel_vec),eye_vec,spot_lights[idx].light_color_energy.rgb*light_attenuation,roughness,diffuse,specular); + light_compute(normal, normalize(light_rel_vec), eye_vec, spot_lights[idx].light_color_energy.rgb * light_attenuation, roughness, diffuse, specular); } - #endif /* Varyings */ @@ -225,29 +219,33 @@ out vec4 color_interp; out vec2 uv_interp; #endif -#if defined(ENABLE_UV2_INTERP) || defined (USE_LIGHTMAP) +#if defined(ENABLE_UV2_INTERP) || defined(USE_LIGHTMAP) out vec2 uv2_interp; #endif - #if defined(ENABLE_TANGENT_INTERP) || defined(ENABLE_NORMALMAP) || defined(LIGHT_USE_ANISOTROPY) out vec3 tangent_interp; out vec3 binormal_interp; #endif - #if defined(USE_MATERIAL) -layout(std140) uniform UniformData { //ubo:1 +/* clang-format off */ +layout(std140) uniform UniformData { // ubo:1 MATERIAL_UNIFORMS }; +/* clang-format on */ #endif +/* clang-format off */ + VERTEX_SHADER_GLOBALS +/* clang-format on */ + #ifdef RENDER_DEPTH_DUAL_PARABOLOID out highp float dp_clip; @@ -257,7 +255,7 @@ out highp float dp_clip; #define SKELETON_TEXTURE_WIDTH 256 #ifdef USE_SKELETON -uniform highp sampler2D skeleton_texture; //texunit:-1 +uniform highp sampler2D skeleton_texture; // texunit:-1 #endif out highp vec4 position_interp; @@ -272,21 +270,18 @@ void main() { mat4 world_matrix = world_transform; - #ifdef USE_INSTANCING { - highp mat4 m=mat4(instance_xform0,instance_xform1,instance_xform2,vec4(0.0,0.0,0.0,1.0)); + highp mat4 m = mat4(instance_xform0, instance_xform1, instance_xform2, vec4(0.0, 0.0, 0.0, 1.0)); world_matrix = world_matrix * transpose(m); } #endif - vec3 normal = normal_attrib * normal_mult; - + vec3 normal = normal_attrib; #if defined(ENABLE_TANGENT_INTERP) || defined(ENABLE_NORMALMAP) || defined(LIGHT_USE_ANISOTROPY) vec3 tangent = tangent_attrib.xyz; - tangent*=normal_mult; float binormalf = tangent_attrib.a; #endif @@ -298,10 +293,9 @@ void main() { #endif - #if defined(ENABLE_TANGENT_INTERP) || defined(ENABLE_NORMALMAP) || defined(LIGHT_USE_ANISOTROPY) - vec3 binormal = normalize( cross(normal,tangent) * binormalf ); + vec3 binormal = normalize(cross(normal, tangent) * binormalf); #endif #if defined(ENABLE_UV_INTERP) @@ -329,13 +323,13 @@ void main() { mat3 normal_matrix = mat3(transpose(inverse(world_matrix))); normal = normal_matrix * normal; #else - normal = normalize((world_matrix * vec4(normal,0.0)).xyz); + normal = normalize((world_matrix * vec4(normal, 0.0)).xyz); #endif #if defined(ENABLE_TANGENT_INTERP) || defined(ENABLE_NORMALMAP) || defined(LIGHT_USE_ANISOTROPY) - tangent = normalize((world_matrix * vec4(tangent,0.0)).xyz); - binormal = normalize((world_matrix * vec4(binormal,0.0)).xyz); + tangent = normalize((world_matrix * vec4(tangent, 0.0)).xyz); + binormal = normalize((world_matrix * vec4(binormal, 0.0)).xyz); #endif #endif @@ -345,60 +339,59 @@ void main() { #define projection_matrix local_projection #define world_transform world_matrix - #ifdef USE_SKELETON { //skeleton transform ivec4 bone_indicesi = ivec4(bone_indices); // cast to signed int - ivec2 tex_ofs = ivec2( bone_indicesi.x%256, (bone_indicesi.x/256)*3 ); - highp mat3x4 m = mat3x4( - texelFetch(skeleton_texture,tex_ofs,0), - texelFetch(skeleton_texture,tex_ofs+ivec2(0,1),0), - texelFetch(skeleton_texture,tex_ofs+ivec2(0,2),0) - ) * bone_weights.x; + ivec2 tex_ofs = ivec2(bone_indicesi.x % 256, (bone_indicesi.x / 256) * 3); + highp mat3x4 m; + m = mat3x4( + texelFetch(skeleton_texture, tex_ofs, 0), + texelFetch(skeleton_texture, tex_ofs + ivec2(0, 1), 0), + texelFetch(skeleton_texture, tex_ofs + ivec2(0, 2), 0)) * + bone_weights.x; - tex_ofs = ivec2( bone_indicesi.y%256, (bone_indicesi.y/256)*3 ); + tex_ofs = ivec2(bone_indicesi.y % 256, (bone_indicesi.y / 256) * 3); - m+= mat3x4( - texelFetch(skeleton_texture,tex_ofs,0), - texelFetch(skeleton_texture,tex_ofs+ivec2(0,1),0), - texelFetch(skeleton_texture,tex_ofs+ivec2(0,2),0) - ) * bone_weights.y; + m += mat3x4( + texelFetch(skeleton_texture, tex_ofs, 0), + texelFetch(skeleton_texture, tex_ofs + ivec2(0, 1), 0), + texelFetch(skeleton_texture, tex_ofs + ivec2(0, 2), 0)) * + bone_weights.y; - tex_ofs = ivec2( bone_indicesi.z%256, (bone_indicesi.z/256)*3 ); + tex_ofs = ivec2(bone_indicesi.z % 256, (bone_indicesi.z / 256) * 3); - m+= mat3x4( - texelFetch(skeleton_texture,tex_ofs,0), - texelFetch(skeleton_texture,tex_ofs+ivec2(0,1),0), - texelFetch(skeleton_texture,tex_ofs+ivec2(0,2),0) - ) * bone_weights.z; + m += mat3x4( + texelFetch(skeleton_texture, tex_ofs, 0), + texelFetch(skeleton_texture, tex_ofs + ivec2(0, 1), 0), + texelFetch(skeleton_texture, tex_ofs + ivec2(0, 2), 0)) * + bone_weights.z; + tex_ofs = ivec2(bone_indicesi.w % 256, (bone_indicesi.w / 256) * 3); - tex_ofs = ivec2( bone_indicesi.w%256, (bone_indicesi.w/256)*3 ); + m += mat3x4( + texelFetch(skeleton_texture, tex_ofs, 0), + texelFetch(skeleton_texture, tex_ofs + ivec2(0, 1), 0), + texelFetch(skeleton_texture, tex_ofs + ivec2(0, 2), 0)) * + bone_weights.w; - m+= mat3x4( - texelFetch(skeleton_texture,tex_ofs,0), - texelFetch(skeleton_texture,tex_ofs+ivec2(0,1),0), - texelFetch(skeleton_texture,tex_ofs+ivec2(0,2),0) - ) * bone_weights.w; - - mat4 bone_matrix = transpose(mat4(m[0],m[1],m[2],vec4(0.0,0.0,0.0,1.0))); + mat4 bone_matrix = transpose(mat4(m[0], m[1], m[2], vec4(0.0, 0.0, 0.0, 1.0))); world_matrix = bone_matrix * world_matrix; } #endif mat4 modelview = camera_inverse_matrix * world_matrix; -{ + { + /* clang-format off */ VERTEX_SHADER_CODE -} - - + /* clang-format on */ + } -//using local coordinates (default) +// using local coordinates (default) #if !defined(SKIP_TRANSFORM_USED) && !defined(VERTEX_WORLD_COORDS_USED) vertex = modelview * vertex; @@ -407,13 +400,13 @@ VERTEX_SHADER_CODE mat3 normal_matrix = mat3(transpose(inverse(modelview))); normal = normal_matrix * normal; #else - normal = normalize((modelview * vec4(normal,0.0)).xyz); + normal = normalize((modelview * vec4(normal, 0.0)).xyz); #endif #if defined(ENABLE_TANGENT_INTERP) || defined(ENABLE_NORMALMAP) || defined(LIGHT_USE_ANISOTROPY) - tangent = normalize((modelview * vec4(tangent,0.0)).xyz); - binormal = normalize((modelview * vec4(binormal,0.0)).xyz); + tangent = normalize((modelview * vec4(tangent, 0.0)).xyz); + binormal = normalize((modelview * vec4(binormal, 0.0)).xyz); #endif #endif @@ -421,74 +414,70 @@ VERTEX_SHADER_CODE #if !defined(SKIP_TRANSFORM_USED) && defined(VERTEX_WORLD_COORDS_USED) vertex = camera_inverse_matrix * vertex; - normal = normalize((camera_inverse_matrix * vec4(normal,0.0)).xyz); + normal = normalize((camera_inverse_matrix * vec4(normal, 0.0)).xyz); #if defined(ENABLE_TANGENT_INTERP) || defined(ENABLE_NORMALMAP) || defined(LIGHT_USE_ANISOTROPY) - tangent = normalize((camera_inverse_matrix * vec4(tangent,0.0)).xyz); - binormal = normalize((camera_inverse_matrix * vec4(binormal,0.0)).xyz); + tangent = normalize((camera_inverse_matrix * vec4(tangent, 0.0)).xyz); + binormal = normalize((camera_inverse_matrix * vec4(binormal, 0.0)).xyz); #endif #endif vertex_interp = vertex.xyz; normal_interp = normal; - #if defined(ENABLE_TANGENT_INTERP) || defined(ENABLE_NORMALMAP) || defined(LIGHT_USE_ANISOTROPY) tangent_interp = tangent; binormal_interp = binormal; #endif - #ifdef RENDER_DEPTH - #ifdef RENDER_DEPTH_DUAL_PARABOLOID - vertex_interp.z*= shadow_dual_paraboloid_render_side; - normal_interp.z*= shadow_dual_paraboloid_render_side; + vertex_interp.z *= shadow_dual_paraboloid_render_side; + normal_interp.z *= shadow_dual_paraboloid_render_side; - dp_clip=vertex_interp.z; //this attempts to avoid noise caused by objects sent to the other parabolloid side due to bias + dp_clip = vertex_interp.z; //this attempts to avoid noise caused by objects sent to the other parabolloid side due to bias //for dual paraboloid shadow mapping, this is the fastest but least correct way, as it curves straight edges - highp vec3 vtx = vertex_interp+normalize(vertex_interp)*z_offset; + highp vec3 vtx = vertex_interp + normalize(vertex_interp) * z_offset; highp float distance = length(vtx); vtx = normalize(vtx); - vtx.xy/=1.0-vtx.z; - vtx.z=(distance/shadow_dual_paraboloid_render_zfar); - vtx.z=vtx.z * 2.0 - 1.0; + vtx.xy /= 1.0 - vtx.z; + vtx.z = (distance / shadow_dual_paraboloid_render_zfar); + vtx.z = vtx.z * 2.0 - 1.0; vertex_interp = vtx; - #else float z_ofs = z_offset; - z_ofs += (1.0-abs(normal_interp.z))*z_slope_scale; - vertex_interp.z-=z_ofs; + z_ofs += (1.0 - abs(normal_interp.z)) * z_slope_scale; + vertex_interp.z -= z_ofs; #endif //RENDER_DEPTH_DUAL_PARABOLOID #endif //RENDER_DEPTH - gl_Position = projection_matrix * vec4(vertex_interp,1.0); + gl_Position = projection_matrix * vec4(vertex_interp, 1.0); - position_interp=gl_Position; + position_interp = gl_Position; #ifdef USE_VERTEX_LIGHTING - diffuse_light_interp=vec4(0.0); - specular_light_interp=vec4(0.0); + diffuse_light_interp = vec4(0.0); + specular_light_interp = vec4(0.0); #ifdef USE_FORWARD_LIGHTING - for(int i=0;i<omni_light_count;i++) { - light_process_omni(omni_light_indices[i],vertex_interp,-normalize( vertex_interp ),normal_interp,roughness,diffuse_light_interp.rgb,specular_light_interp.rgb); + for (int i = 0; i < omni_light_count; i++) { + light_process_omni(omni_light_indices[i], vertex_interp, -normalize(vertex_interp), normal_interp, roughness, diffuse_light_interp.rgb, specular_light_interp.rgb); } - for(int i=0;i<spot_light_count;i++) { - light_process_spot(spot_light_indices[i],vertex_interp,-normalize( vertex_interp ),normal_interp,roughness,diffuse_light_interp.rgb,specular_light_interp.rgb); + for (int i = 0; i < spot_light_count; i++) { + light_process_spot(spot_light_indices[i], vertex_interp, -normalize(vertex_interp), normal_interp, roughness, diffuse_light_interp.rgb, specular_light_interp.rgb); } #endif @@ -496,38 +485,37 @@ VERTEX_SHADER_CODE vec3 directional_diffuse = vec3(0.0); vec3 directional_specular = vec3(0.0); - light_compute(normal_interp,-light_direction_attenuation.xyz,-normalize( vertex_interp ),light_color_energy.rgb,roughness,directional_diffuse,directional_specular); + light_compute(normal_interp, -light_direction_attenuation.xyz, -normalize(vertex_interp), light_color_energy.rgb, roughness, directional_diffuse, directional_specular); - float diff_avg = dot(diffuse_light_interp.rgb,vec3(0.33333)); - float diff_dir_avg = dot(directional_diffuse,vec3(0.33333)); - if (diff_avg>0.0) { - diffuse_light_interp.a=diff_dir_avg/(diff_avg+diff_dir_avg); + float diff_avg = dot(diffuse_light_interp.rgb, vec3(0.33333)); + float diff_dir_avg = dot(directional_diffuse, vec3(0.33333)); + if (diff_avg > 0.0) { + diffuse_light_interp.a = diff_dir_avg / (diff_avg + diff_dir_avg); } else { - diffuse_light_interp.a=1.0; + diffuse_light_interp.a = 1.0; } - diffuse_light_interp.rgb+=directional_diffuse; + diffuse_light_interp.rgb += directional_diffuse; - float spec_avg = dot(specular_light_interp.rgb,vec3(0.33333)); - float spec_dir_avg = dot(directional_specular,vec3(0.33333)); - if (spec_avg>0.0) { - specular_light_interp.a=spec_dir_avg/(spec_avg+spec_dir_avg); + float spec_avg = dot(specular_light_interp.rgb, vec3(0.33333)); + float spec_dir_avg = dot(directional_specular, vec3(0.33333)); + if (spec_avg > 0.0) { + specular_light_interp.a = spec_dir_avg / (spec_avg + spec_dir_avg); } else { - specular_light_interp.a=1.0; + specular_light_interp.a = 1.0; } - specular_light_interp.rgb+=directional_specular; + specular_light_interp.rgb += directional_specular; #endif //USE_LIGHT_DIRECTIONAL - #endif // USE_VERTEX_LIGHTING - } - +/* clang-format off */ [fragment] + /* texture unit usage, N is max_texture_unity-N 1-skeleton @@ -544,8 +532,10 @@ VERTEX_SHADER_CODE */ uniform highp mat4 world_transform; +/* clang-format on */ #define M_PI 3.14159265359 +#define SHADER_IS_SRGB false /* Varyings */ @@ -569,37 +559,33 @@ in vec3 binormal_interp; in highp vec3 vertex_interp; in vec3 normal_interp; - /* PBR CHANNELS */ #ifdef USE_RADIANCE_MAP - - -layout(std140) uniform Radiance { //ubo:2 +layout(std140) uniform Radiance { // ubo:2 mat4 radiance_inverse_xform; float radiance_ambient_contribution; - }; #define RADIANCE_MAX_LOD 5.0 #ifdef USE_RADIANCE_MAP_ARRAY -uniform sampler2DArray radiance_map; //texunit:-2 +uniform sampler2DArray radiance_map; // texunit:-2 -vec3 textureDualParaboloid(sampler2DArray p_tex, vec3 p_vec,float p_roughness) { +vec3 textureDualParaboloid(sampler2DArray p_tex, vec3 p_vec, float p_roughness) { vec3 norm = normalize(p_vec); - norm.xy/=1.0+abs(norm.z); - norm.xy=norm.xy * vec2(0.5,0.25) + vec2(0.5,0.25); + norm.xy /= 1.0 + abs(norm.z); + norm.xy = norm.xy * vec2(0.5, 0.25) + vec2(0.5, 0.25); // we need to lie the derivatives (normg) and assume that DP side is always the same // to get proper texture filtering - vec2 normg=norm.xy; - if (norm.z>0.0) { - norm.y=0.5-norm.y+0.5; + vec2 normg = norm.xy; + if (norm.z > 0.0) { + norm.y = 0.5 - norm.y + 0.5; } // thanks to OpenGL spec using floor(layer + 0.5) for texture arrays, @@ -608,22 +594,22 @@ vec3 textureDualParaboloid(sampler2DArray p_tex, vec3 p_vec,float p_roughness) { float index = p_roughness * RADIANCE_MAX_LOD; int indexi = int(index * 256.0); - vec3 base = textureGrad(p_tex, vec3(norm.xy, float(indexi/256)),dFdx(normg),dFdy(normg)).xyz; - vec3 next = textureGrad(p_tex, vec3(norm.xy, float(indexi/256+1)),dFdx(normg),dFdy(normg)).xyz; - return mix(base,next,float(indexi%256)/256.0); + vec3 base = textureGrad(p_tex, vec3(norm.xy, float(indexi / 256)), dFdx(normg), dFdy(normg)).xyz; + vec3 next = textureGrad(p_tex, vec3(norm.xy, float(indexi / 256 + 1)), dFdx(normg), dFdy(normg)).xyz; + return mix(base, next, float(indexi % 256) / 256.0); } #else -uniform sampler2D radiance_map; //texunit:-2 +uniform sampler2D radiance_map; // texunit:-2 -vec3 textureDualParaboloid(sampler2D p_tex, vec3 p_vec,float p_roughness) { +vec3 textureDualParaboloid(sampler2D p_tex, vec3 p_vec, float p_roughness) { vec3 norm = normalize(p_vec); - norm.xy/=1.0+abs(norm.z); - norm.xy=norm.xy * vec2(0.5,0.25) + vec2(0.5,0.25); - if (norm.z>0.0) { - norm.y=0.5-norm.y+0.5; + norm.xy /= 1.0 + abs(norm.z); + norm.xy = norm.xy * vec2(0.5, 0.25) + vec2(0.5, 0.25); + if (norm.z > 0.0) { + norm.y = 0.5 - norm.y + 0.5; } return textureLod(p_tex, norm.xy, p_roughness * RADIANCE_MAX_LOD).xyz; } @@ -634,20 +620,24 @@ vec3 textureDualParaboloid(sampler2D p_tex, vec3 p_vec,float p_roughness) { /* Material Uniforms */ - - #if defined(USE_MATERIAL) +/* clang-format off */ layout(std140) uniform UniformData { MATERIAL_UNIFORMS }; +/* clang-format on */ #endif +/* clang-format off */ + FRAGMENT_SHADER_GLOBALS +/* clang-format on */ + layout(std140) uniform SceneData { highp mat4 projection_matrix; @@ -684,6 +674,8 @@ layout(std140) uniform SceneData { bool fog_depth_enabled; highp float fog_depth_begin; + highp float fog_depth_end; + mediump float fog_density; highp float fog_depth_curve; bool fog_transmit_enabled; highp float fog_transmit_curve; @@ -693,7 +685,7 @@ layout(std140) uniform SceneData { highp float fog_height_curve; }; -//directional light data + //directional light data #ifdef USE_LIGHT_DIRECTIONAL @@ -702,7 +694,7 @@ layout(std140) uniform DirectionalLightData { highp vec4 light_pos_inv_radius; mediump vec4 light_direction_attenuation; mediump vec4 light_color_energy; - mediump vec4 light_params; //cone attenuation, angle, specular, shadow enabled, + mediump vec4 light_params; // cone attenuation, angle, specular, shadow enabled, mediump vec4 light_clamp; mediump vec4 shadow_color_contact; highp mat4 shadow_matrix1; @@ -712,8 +704,7 @@ layout(std140) uniform DirectionalLightData { mediump vec4 shadow_split_offsets; }; - -uniform highp sampler2DShadow directional_shadow; //texunit:-4 +uniform highp sampler2DShadow directional_shadow; // texunit:-4 #endif @@ -721,52 +712,47 @@ uniform highp sampler2DShadow directional_shadow; //texunit:-4 in vec4 diffuse_light_interp; in vec4 specular_light_interp; #endif -//omni and spot +// omni and spot struct LightData { highp vec4 light_pos_inv_radius; mediump vec4 light_direction_attenuation; mediump vec4 light_color_energy; - mediump vec4 light_params; //cone attenuation, angle, specular, shadow enabled, + mediump vec4 light_params; // cone attenuation, angle, specular, shadow enabled, mediump vec4 light_clamp; mediump vec4 shadow_color_contact; highp mat4 shadow_matrix; - }; - -layout(std140) uniform OmniLightData { //ubo:4 +layout(std140) uniform OmniLightData { // ubo:4 LightData omni_lights[MAX_LIGHT_DATA_STRUCTS]; }; -layout(std140) uniform SpotLightData { //ubo:5 +layout(std140) uniform SpotLightData { // ubo:5 LightData spot_lights[MAX_LIGHT_DATA_STRUCTS]; }; - -uniform highp sampler2DShadow shadow_atlas; //texunit:-5 - +uniform highp sampler2DShadow shadow_atlas; // texunit:-5 struct ReflectionData { mediump vec4 box_extents; mediump vec4 box_offset; mediump vec4 params; // intensity, 0, interior , boxproject - mediump vec4 ambient; //ambient color, energy + mediump vec4 ambient; // ambient color, energy mediump vec4 atlas_clamp; - highp mat4 local_matrix; //up to here for spot and omni, rest is for directional - //notes: for ambientblend, use distance to edge to blend between already existing global environment + highp mat4 local_matrix; // up to here for spot and omni, rest is for directional + // notes: for ambientblend, use distance to edge to blend between already existing global environment }; layout(std140) uniform ReflectionProbeData { //ubo:6 ReflectionData reflections[MAX_REFLECTION_DATA_STRUCTS]; }; -uniform mediump sampler2D reflection_atlas; //texunit:-3 - +uniform mediump sampler2D reflection_atlas; // texunit:-3 #ifdef USE_FORWARD_LIGHTING @@ -781,39 +767,38 @@ uniform int reflection_count; #endif - #if defined(SCREEN_TEXTURE_USED) -uniform highp sampler2D screen_texture; //texunit:-7 +uniform highp sampler2D screen_texture; // texunit:-7 #endif #ifdef USE_MULTIPLE_RENDER_TARGETS -layout(location=0) out vec4 diffuse_buffer; -layout(location=1) out vec4 specular_buffer; -layout(location=2) out vec4 normal_mr_buffer; +layout(location = 0) out vec4 diffuse_buffer; +layout(location = 1) out vec4 specular_buffer; +layout(location = 2) out vec4 normal_mr_buffer; #if defined(ENABLE_SSS) -layout(location=3) out float sss_buffer; +layout(location = 3) out float sss_buffer; #endif #else -layout(location=0) out vec4 frag_color; +layout(location = 0) out vec4 frag_color; #endif in highp vec4 position_interp; -uniform highp sampler2D depth_buffer; //texunit:-8 +uniform highp sampler2D depth_buffer; // texunit:-8 #ifdef USE_CONTACT_SHADOWS float contact_shadow_compute(vec3 pos, vec3 dir, float max_distance) { - if (abs(dir.z)>0.99) + if (abs(dir.z) > 0.99) return 1.0; - vec3 endpoint = pos+dir*max_distance; + vec3 endpoint = pos + dir * max_distance; vec4 source = position_interp; vec4 dest = projection_matrix * vec4(endpoint, 1.0); @@ -822,51 +807,48 @@ float contact_shadow_compute(vec3 pos, vec3 dir, float max_distance) { vec2 screen_rel = to_screen - from_screen; - if (length(screen_rel)<0.00001) - return 1.0; //too small, don't do anything + if (length(screen_rel) < 0.00001) + return 1.0; // too small, don't do anything - /*float pixel_size; //approximate pixel size + /* + float pixel_size; // approximate pixel size if (screen_rel.x > screen_rel.y) { - pixel_size = abs((pos.x-endpoint.x)/(screen_rel.x/screen_pixel_size.x)); + pixel_size = abs((pos.x - endpoint.x) / (screen_rel.x / screen_pixel_size.x)); } else { - pixel_size = abs((pos.y-endpoint.y)/(screen_rel.y/screen_pixel_size.y)); - - }*/ - vec4 bias = projection_matrix * vec4(pos+vec3(0.0,0.0,max_distance*0.5), 1.0); //todo un-harcode the 0.04 - - - - vec2 pixel_incr = normalize(screen_rel)*screen_pixel_size; + pixel_size = abs((pos.y - endpoint.y) / (screen_rel.y / screen_pixel_size.y)); + } + */ + vec4 bias = projection_matrix * vec4(pos + vec3(0.0, 0.0, max_distance * 0.5), 1.0); + vec2 pixel_incr = normalize(screen_rel) * screen_pixel_size; float steps = length(screen_rel) / length(pixel_incr); - steps = min(2000.0,steps); //put a limit to avoid freezing in some strange situation - //steps=10.0; + steps = min(2000.0, steps); // put a limit to avoid freezing in some strange situation + //steps = 10.0; - vec4 incr = (dest - source)/steps; - float ratio=0.0; - float ratio_incr = 1.0/steps; + vec4 incr = (dest - source) / steps; + float ratio = 0.0; + float ratio_incr = 1.0 / steps; - while(steps>0.0) { - source += incr*2.0; - bias+=incr*2.0; + while (steps > 0.0) { + source += incr * 2.0; + bias += incr * 2.0; vec3 uv_depth = (source.xyz / source.w) * 0.5 + 0.5; - float depth = texture(depth_buffer,uv_depth.xy).r; + float depth = texture(depth_buffer, uv_depth.xy).r; if (depth < uv_depth.z) { - if (depth > (bias.z/bias.w) * 0.5 + 0.5) { - return min(pow(ratio,4.0),1.0); + if (depth > (bias.z / bias.w) * 0.5 + 0.5) { + return min(pow(ratio, 4.0), 1.0); } else { return 1.0; } } - - ratio+=ratio_incr; - steps-=1.0; + ratio += ratio_incr; + steps -= 1.0; } return 1.0; @@ -874,7 +856,6 @@ float contact_shadow_compute(vec3 pos, vec3 dir, float max_distance) { #endif - // This returns the G_GGX function divided by 2 cos_theta_m, where in practice cos_theta_m is either N.L or N.V. // We're dividing this factor off because the overall term we'll end up looks like // (see, for example, the first unnumbered equation in B. Burley, "Physically Based Shading at Disney", SIGGRAPH 2012): @@ -896,78 +877,91 @@ float G_GGX_2cos(float cos_theta_m, float alpha) { // C. Schlick, "An Inexpensive BRDF Model for Physically-based Rendering", Computer Graphics Forum. 13 (3): 233 (1994) // Eq. (19), although see Heitz (2014) the about the problems with his derivation. // It nevertheless approximates GGX well with k = alpha/2. - float k = 0.5*alpha; + float k = 0.5 * alpha; return 0.5 / (cos_theta_m * (1.0 - k) + k); - // float cos2 = cos_theta_m*cos_theta_m; - // float sin2 = (1.0-cos2); - // return 1.0 /( cos_theta_m + sqrt(cos2 + alpha*alpha*sin2) ); + // float cos2 = cos_theta_m * cos_theta_m; + // float sin2 = (1.0 - cos2); + // return 1.0 / (cos_theta_m + sqrt(cos2 + alpha * alpha * sin2)); } float D_GGX(float cos_theta_m, float alpha) { - float alpha2 = alpha*alpha; - float d = 1.0 + (alpha2-1.0)*cos_theta_m*cos_theta_m; - return alpha2/(M_PI * d * d); + float alpha2 = alpha * alpha; + float d = 1.0 + (alpha2 - 1.0) * cos_theta_m * cos_theta_m; + return alpha2 / (M_PI * d * d); } float G_GGX_anisotropic_2cos(float cos_theta_m, float alpha_x, float alpha_y, float cos_phi, float sin_phi) { float cos2 = cos_theta_m * cos_theta_m; - float sin2 = (1.0-cos2); + float sin2 = (1.0 - cos2); float s_x = alpha_x * cos_phi; float s_y = alpha_y * sin_phi; - return 1.0 / max(cos_theta_m + sqrt(cos2 + (s_x*s_x + s_y*s_y)*sin2 ), 0.001); + return 1.0 / max(cos_theta_m + sqrt(cos2 + (s_x * s_x + s_y * s_y) * sin2), 0.001); } float D_GGX_anisotropic(float cos_theta_m, float alpha_x, float alpha_y, float cos_phi, float sin_phi) { float cos2 = cos_theta_m * cos_theta_m; - float sin2 = (1.0-cos2); - float r_x = cos_phi/alpha_x; - float r_y = sin_phi/alpha_y; - float d = cos2 + sin2*(r_x * r_x + r_y * r_y); + float sin2 = (1.0 - cos2); + float r_x = cos_phi / alpha_x; + float r_y = sin_phi / alpha_y; + float d = cos2 + sin2 * (r_x * r_x + r_y * r_y); return 1.0 / max(M_PI * alpha_x * alpha_y * d * d, 0.001); } - -float SchlickFresnel(float u) -{ - float m = 1.0-u; - float m2 = m*m; - return m2*m2*m; // pow(m,5) +float SchlickFresnel(float u) { + float m = 1.0 - u; + float m2 = m * m; + return m2 * m2 * m; // pow(m,5) } -float GTR1(float NdotH, float a) -{ - if (a >= 1.0) return 1.0/M_PI; - float a2 = a*a; - float t = 1.0 + (a2-1.0)*NdotH*NdotH; - return (a2-1.0) / (M_PI*log(a2)*t); +float GTR1(float NdotH, float a) { + if (a >= 1.0) return 1.0 / M_PI; + float a2 = a * a; + float t = 1.0 + (a2 - 1.0) * NdotH * NdotH; + return (a2 - 1.0) / (M_PI * log(a2) * t); } -vec3 metallic_to_specular_color(float metallic, float specular, vec3 albedo) { - float dielectric = (0.034 * 2.0) * specular; - // energy conservation - return mix(vec3(dielectric), albedo, metallic); // TODO: reference? +vec3 F0(float metallic, float specular, vec3 albedo) { + float dielectric = 0.16 * specular * specular; + // use albedo * metallic as colored specular reflectance at 0 angle for metallic materials; + // see https://google.github.io/filament/Filament.md.html + return mix(vec3(dielectric), albedo, vec3(metallic)); } -void light_compute(vec3 N, vec3 L, vec3 V, vec3 B, vec3 T, vec3 light_color, vec3 attenuation, vec3 diffuse_color, vec3 transmission, float specular_blob_intensity, float roughness, float metallic, float rim, float rim_tint, float clearcoat, float clearcoat_gloss, float anisotropy, inout vec3 diffuse_light, inout vec3 specular_light) { +void light_compute(vec3 N, vec3 L, vec3 V, vec3 B, vec3 T, vec3 light_color, vec3 attenuation, vec3 diffuse_color, vec3 transmission, float specular_blob_intensity, float roughness, float metallic, float specular, float rim, float rim_tint, float clearcoat, float clearcoat_gloss, float anisotropy, inout vec3 diffuse_light, inout vec3 specular_light) { #if defined(USE_LIGHT_SHADER_CODE) -//light is written by the light shader + // light is written by the light shader vec3 normal = N; vec3 albedo = diffuse_color; vec3 light = L; vec3 view = V; + /* clang-format off */ + LIGHT_SHADER_CODE + /* clang-format on */ #else - float NdotL = dot(N,L); + float NdotL = dot(N, L); float cNdotL = max(NdotL, 0.0); // clamped NdotL float NdotV = dot(N, V); float cNdotV = max(NdotV, 0.0); +#if defined(DIFFUSE_BURLEY) || defined(SPECULAR_BLINN) || defined(SPECULAR_SCHLICK_GGX) || defined(LIGHT_USE_CLEARCOAT) + vec3 H = normalize(V + L); +#endif + +#if defined(SPECULAR_BLINN) || defined(SPECULAR_SCHLICK_GGX) || defined(LIGHT_USE_CLEARCOAT) + float cNdotH = max(dot(N, H), 0.0); +#endif + +#if defined(DIFFUSE_BURLEY) || defined(SPECULAR_SCHLICK_GGX) || defined(LIGHT_USE_CLEARCOAT) + float cLdotH = max(dot(L, H), 0.0); +#endif + if (metallic < 1.0) { #if defined(DIFFUSE_OREN_NAYAR) vec3 diffuse_brdf_NL; @@ -975,10 +969,9 @@ LIGHT_SHADER_CODE float diffuse_brdf_NL; // BRDF times N.L for calculating diffuse radiance #endif - #if defined(DIFFUSE_LAMBERT_WRAP) - //energy conserving lambert wrap shader - diffuse_brdf_NL = max(0.0,(NdotL + roughness) / ((1.0 + roughness) * (1.0 + roughness))); + // energy conserving lambert wrap shader + diffuse_brdf_NL = max(0.0, (NdotL + roughness) / ((1.0 + roughness) * (1.0 + roughness))); #elif defined(DIFFUSE_OREN_NAYAR) @@ -986,12 +979,11 @@ LIGHT_SHADER_CODE // see http://mimosa-pudica.net/improved-oren-nayar.html float LdotV = dot(L, V); - float s = LdotV - NdotL * NdotV; float t = mix(1.0, max(NdotL, NdotV), step(0.0, s)); float sigma2 = roughness * roughness; // TODO: this needs checking - vec3 A = 1.0 + sigma2 * (- 0.5 / (sigma2 + 0.33) + 0.17*diffuse_color / (sigma2 + 0.13) ); + vec3 A = 1.0 + sigma2 * (-0.5 / (sigma2 + 0.33) + 0.17 * diffuse_color / (sigma2 + 0.13)); float B = 0.45 * sigma2 / (sigma2 + 0.09); diffuse_brdf_NL = cNdotL * (A + vec3(B) * s / t) * (1.0 / M_PI); @@ -999,21 +991,16 @@ LIGHT_SHADER_CODE #elif defined(DIFFUSE_TOON) - diffuse_brdf_NL = smoothstep(-roughness,max(roughness,0.01),NdotL); + diffuse_brdf_NL = smoothstep(-roughness, max(roughness, 0.01), NdotL); #elif defined(DIFFUSE_BURLEY) { - - - vec3 H = normalize(V + L); - float cLdotH = max(0.0,dot(L, H)); - - float FD90 = 0.5 + 2.0 * cLdotH * cLdotH * roughness; - float FdV = 1.0 + (FD90 - 1.0) * SchlickFresnel(cNdotV); - float FdL = 1.0 + (FD90 - 1.0) * SchlickFresnel(cNdotL); + float FD90_minus_1 = 2.0 * cLdotH * cLdotH * roughness - 0.5; + float FdV = 1.0 + FD90_minus_1 * SchlickFresnel(cNdotV); + float FdL = 1.0 + FD90_minus_1 * SchlickFresnel(cNdotL); diffuse_brdf_NL = (1.0 / M_PI) * FdV * FdL * cNdotL; - /* + /* float energyBias = mix(roughness, 0.0, 0.5); float energyFactor = mix(roughness, 1.0, 1.0 / 1.51); float fd90 = energyBias + 2.0 * VoH * VoH * roughness; @@ -1021,10 +1008,11 @@ LIGHT_SHADER_CODE float lightScatter = f0 + (fd90 - f0) * pow(1.0 - cNdotL, 5.0); float viewScatter = f0 + (fd90 - f0) * pow(1.0 - cNdotV, 5.0); - diffuse_brdf_NL = lightScatter * viewScatter * energyFactor;*/ + diffuse_brdf_NL = lightScatter * viewScatter * energyFactor; + */ } #else - //lambert + // lambert diffuse_brdf_NL = cNdotL * (1.0 / M_PI); #endif @@ -1034,141 +1022,132 @@ LIGHT_SHADER_CODE diffuse_light += light_color * diffuse_color * (vec3(1.0 / M_PI) - diffuse_brdf_NL) * transmission * attenuation; #endif - #if defined(LIGHT_USE_RIM) - float rim_light = pow(max(0.0,1.0-cNdotV), max(0.0,(1.0-roughness)*16.0)); - diffuse_light += rim_light * rim * mix(vec3(1.0),diffuse_color,rim_tint) * light_color; + float rim_light = pow(max(0.0, 1.0 - cNdotV), max(0.0, (1.0 - roughness) * 16.0)); + diffuse_light += rim_light * rim * mix(vec3(1.0), diffuse_color, rim_tint) * light_color; #endif } - if (roughness > 0.0) { // FIXME: roughness == 0 should not disable specular light entirely - // D #if defined(SPECULAR_BLINN) - vec3 H = normalize(V + L); - float cNdotH = max(dot(N,H), 0.0 ); - float intensity = pow( cNdotH, (1.0-roughness) * 256.0); + //normalized blinn + float shininess = exp2(15.0 * (1.0 - roughness) + 1.0) * 0.25; + float blinn = pow(cNdotH, shininess); + blinn *= (shininess + 8.0) * (1.0 / (8.0 * M_PI)); + float intensity = (blinn) / max(4.0 * cNdotV * cNdotL, 0.75); + specular_light += light_color * intensity * specular_blob_intensity * attenuation; #elif defined(SPECULAR_PHONG) - vec3 R = normalize(-reflect(L,N)); - float cRdotV = max(0.0,dot(R,V)); - float intensity = pow( cRdotV, (1.0-roughness) * 256.0); - specular_light += light_color * intensity * specular_blob_intensity * attenuation; + vec3 R = normalize(-reflect(L, N)); + float cRdotV = max(0.0, dot(R, V)); + float shininess = exp2(15.0 * (1.0 - roughness) + 1.0) * 0.25; + float phong = pow(cRdotV, shininess); + phong *= (shininess + 8.0) * (1.0 / (8.0 * M_PI)); + float intensity = (phong) / max(4.0 * cNdotV * cNdotL, 0.75); + + specular_light += light_color * intensity * specular_blob_intensity * attenuation; #elif defined(SPECULAR_TOON) - vec3 R = normalize(-reflect(L,N)); - float RdotV = dot(R,V); - float mid = 1.0-roughness; - mid*=mid; - float intensity = smoothstep(mid-roughness*0.5, mid+roughness*0.5, RdotV) * mid; + vec3 R = normalize(-reflect(L, N)); + float RdotV = dot(R, V); + float mid = 1.0 - roughness; + mid *= mid; + float intensity = smoothstep(mid - roughness * 0.5, mid + roughness * 0.5, RdotV) * mid; diffuse_light += light_color * intensity * specular_blob_intensity * attenuation; // write to diffuse_light, as in toon shading you generally want no reflection #elif defined(SPECULAR_DISABLED) - //none.. + // none.. #elif defined(SPECULAR_SCHLICK_GGX) // shlick+ggx as default - vec3 H = normalize(V + L); - - float cNdotH = max(dot(N,H), 0.0); - float cLdotH = max(dot(L,H), 0.0); - -# if defined(LIGHT_USE_ANISOTROPY) +#if defined(LIGHT_USE_ANISOTROPY) - float aspect = sqrt(1.0-anisotropy*0.9); - float rx = roughness/aspect; - float ry = roughness*aspect; - float ax = rx*rx; - float ay = ry*ry; - float XdotH = dot( T, H ); - float YdotH = dot( B, H ); + float alpha = roughness * roughness; + float aspect = sqrt(1.0 - anisotropy * 0.9); + float ax = alpha / aspect; + float ay = alpha * aspect; + float XdotH = dot(T, H); + float YdotH = dot(B, H); float D = D_GGX_anisotropic(cNdotH, ax, ay, XdotH, YdotH); float G = G_GGX_anisotropic_2cos(cNdotL, ax, ay, XdotH, YdotH) * G_GGX_anisotropic_2cos(cNdotV, ax, ay, XdotH, YdotH); -# else +#else float alpha = roughness * roughness; float D = D_GGX(cNdotH, alpha); float G = G_GGX_2cos(cNdotL, alpha) * G_GGX_2cos(cNdotV, alpha); -# endif +#endif // F - float F0 = 1.0; // FIXME + vec3 f0 = F0(metallic, specular, diffuse_color); float cLdotH5 = SchlickFresnel(cLdotH); - float F = mix(cLdotH5, 1.0, F0); + vec3 F = mix(vec3(cLdotH5), vec3(1.0), f0); - float specular_brdf_NL = cNdotL * D * F * G; + vec3 specular_brdf_NL = cNdotL * D * F * G; specular_light += specular_brdf_NL * light_color * specular_blob_intensity * attenuation; #endif #if defined(LIGHT_USE_CLEARCOAT) - if (clearcoat_gloss > 0.0) { -# if !defined(SPECULAR_SCHLICK_GGX) && !defined(SPECULAR_BLINN) - vec3 H = normalize(V + L); -# endif -# if !defined(SPECULAR_SCHLICK_GGX) - float cNdotH = max(dot(N,H), 0.0); - float cLdotH = max(dot(L,H), 0.0); - float cLdotH5 = SchlickFresnel(cLdotH); -#endif - float Dr = GTR1(cNdotH, mix(.1, .001, clearcoat_gloss)); - float Fr = mix(.04, 1.0, cLdotH5); - float Gr = G_GGX_2cos(cNdotL, .25) * G_GGX_2cos(cNdotV, .25); +#if !defined(SPECULAR_SCHLICK_GGX) + float cLdotH5 = SchlickFresnel(cLdotH); +#endif + float Dr = GTR1(cNdotH, mix(.1, .001, clearcoat_gloss)); + float Fr = mix(.04, 1.0, cLdotH5); + float Gr = G_GGX_2cos(cNdotL, .25) * G_GGX_2cos(cNdotV, .25); - float specular_brdf_NL = 0.25 * clearcoat * Gr * Fr * Dr * cNdotL; + float clearcoat_specular_brdf_NL = 0.25 * clearcoat * Gr * Fr * Dr * cNdotL; - specular_light += specular_brdf_NL * light_color * specular_blob_intensity * attenuation; - } + specular_light += clearcoat_specular_brdf_NL * light_color * specular_blob_intensity * attenuation; #endif } - #endif //defined(USE_LIGHT_SHADER_CODE) } - float sample_shadow(highp sampler2DShadow shadow, vec2 shadow_pixel_size, vec2 pos, float depth, vec4 clamp_rect) { #ifdef SHADOW_MODE_PCF_13 - float avg=textureProj(shadow,vec4(pos,depth,1.0)); - avg+=textureProj(shadow,vec4(pos+vec2(shadow_pixel_size.x,0.0),depth,1.0)); - avg+=textureProj(shadow,vec4(pos+vec2(-shadow_pixel_size.x,0.0),depth,1.0)); - avg+=textureProj(shadow,vec4(pos+vec2(0.0,shadow_pixel_size.y),depth,1.0)); - avg+=textureProj(shadow,vec4(pos+vec2(0.0,-shadow_pixel_size.y),depth,1.0)); - avg+=textureProj(shadow,vec4(pos+vec2(shadow_pixel_size.x,shadow_pixel_size.y),depth,1.0)); - avg+=textureProj(shadow,vec4(pos+vec2(-shadow_pixel_size.x,shadow_pixel_size.y),depth,1.0)); - avg+=textureProj(shadow,vec4(pos+vec2(shadow_pixel_size.x,-shadow_pixel_size.y),depth,1.0)); - avg+=textureProj(shadow,vec4(pos+vec2(-shadow_pixel_size.x,-shadow_pixel_size.y),depth,1.0)); - avg+=textureProj(shadow,vec4(pos+vec2(shadow_pixel_size.x*2.0,0.0),depth,1.0)); - avg+=textureProj(shadow,vec4(pos+vec2(-shadow_pixel_size.x*2.0,0.0),depth,1.0)); - avg+=textureProj(shadow,vec4(pos+vec2(0.0,shadow_pixel_size.y*2.0),depth,1.0)); - avg+=textureProj(shadow,vec4(pos+vec2(0.0,-shadow_pixel_size.y*2.0),depth,1.0)); - return avg*(1.0/13.0); - -#elif defined(SHADOW_MODE_PCF_5) - - float avg=textureProj(shadow,vec4(pos,depth,1.0)); - avg+=textureProj(shadow,vec4(pos+vec2(shadow_pixel_size.x,0.0),depth,1.0)); - avg+=textureProj(shadow,vec4(pos+vec2(-shadow_pixel_size.x,0.0),depth,1.0)); - avg+=textureProj(shadow,vec4(pos+vec2(0.0,shadow_pixel_size.y),depth,1.0)); - avg+=textureProj(shadow,vec4(pos+vec2(0.0,-shadow_pixel_size.y),depth,1.0)); - return avg*(1.0/5.0); + float avg = textureProj(shadow, vec4(pos, depth, 1.0)); + avg += textureProj(shadow, vec4(pos + vec2(shadow_pixel_size.x, 0.0), depth, 1.0)); + avg += textureProj(shadow, vec4(pos + vec2(-shadow_pixel_size.x, 0.0), depth, 1.0)); + avg += textureProj(shadow, vec4(pos + vec2(0.0, shadow_pixel_size.y), depth, 1.0)); + avg += textureProj(shadow, vec4(pos + vec2(0.0, -shadow_pixel_size.y), depth, 1.0)); + avg += textureProj(shadow, vec4(pos + vec2(shadow_pixel_size.x, shadow_pixel_size.y), depth, 1.0)); + avg += textureProj(shadow, vec4(pos + vec2(-shadow_pixel_size.x, shadow_pixel_size.y), depth, 1.0)); + avg += textureProj(shadow, vec4(pos + vec2(shadow_pixel_size.x, -shadow_pixel_size.y), depth, 1.0)); + avg += textureProj(shadow, vec4(pos + vec2(-shadow_pixel_size.x, -shadow_pixel_size.y), depth, 1.0)); + avg += textureProj(shadow, vec4(pos + vec2(shadow_pixel_size.x * 2.0, 0.0), depth, 1.0)); + avg += textureProj(shadow, vec4(pos + vec2(-shadow_pixel_size.x * 2.0, 0.0), depth, 1.0)); + avg += textureProj(shadow, vec4(pos + vec2(0.0, shadow_pixel_size.y * 2.0), depth, 1.0)); + avg += textureProj(shadow, vec4(pos + vec2(0.0, -shadow_pixel_size.y * 2.0), depth, 1.0)); + return avg * (1.0 / 13.0); +#endif -#else +#ifdef SHADOW_MODE_PCF_5 - return textureProj(shadow,vec4(pos,depth,1.0)); + float avg = textureProj(shadow, vec4(pos, depth, 1.0)); + avg += textureProj(shadow, vec4(pos + vec2(shadow_pixel_size.x, 0.0), depth, 1.0)); + avg += textureProj(shadow, vec4(pos + vec2(-shadow_pixel_size.x, 0.0), depth, 1.0)); + avg += textureProj(shadow, vec4(pos + vec2(0.0, shadow_pixel_size.y), depth, 1.0)); + avg += textureProj(shadow, vec4(pos + vec2(0.0, -shadow_pixel_size.y), depth, 1.0)); + return avg * (1.0 / 5.0); #endif +#if !defined(SHADOW_MODE_PCF_5) || !defined(SHADOW_MODE_PCF_13) + + return textureProj(shadow, vec4(pos, depth, 1.0)); + +#endif } #ifdef RENDER_DEPTH_DUAL_PARABOLOID @@ -1177,239 +1156,227 @@ in highp float dp_clip; #endif - - #if 0 -//need to save texture depth for this - +// need to save texture depth for this vec3 light_transmittance(float translucency,vec3 light_vec, vec3 normal, vec3 pos, float distance) { float scale = 8.25 * (1.0 - translucency) / subsurface_scatter_width; float d = scale * distance; - /** - * Armed with the thickness, we can now calculate the color by means of the - * precalculated transmittance profile. - * (It can be precomputed into a texture, for maximum performance): - */ + /** + * Armed with the thickness, we can now calculate the color by means of the + * precalculated transmittance profile. + * (It can be precomputed into a texture, for maximum performance): + */ float dd = -d * d; - vec3 profile = vec3(0.233, 0.455, 0.649) * exp(dd / 0.0064) + - vec3(0.1, 0.336, 0.344) * exp(dd / 0.0484) + - vec3(0.118, 0.198, 0.0) * exp(dd / 0.187) + - vec3(0.113, 0.007, 0.007) * exp(dd / 0.567) + - vec3(0.358, 0.004, 0.0) * exp(dd / 1.99) + - vec3(0.078, 0.0, 0.0) * exp(dd / 7.41); - - /** - * Using the profile, we finally approximate the transmitted lighting from - * the back of the object: - */ - return profile * clamp(0.3 + dot(light_vec, normal),0.0,1.0); + vec3 profile = + vec3(0.233, 0.455, 0.649) * exp(dd / 0.0064) + + vec3(0.1, 0.336, 0.344) * exp(dd / 0.0484) + + vec3(0.118, 0.198, 0.0) * exp(dd / 0.187) + + vec3(0.113, 0.007, 0.007) * exp(dd / 0.567) + + vec3(0.358, 0.004, 0.0) * exp(dd / 1.99) + + vec3(0.078, 0.0, 0.0) * exp(dd / 7.41); + + /** + * Using the profile, we finally approximate the transmitted lighting from + * the back of the object: + */ + return profile * clamp(0.3 + dot(light_vec, normal),0.0,1.0); } #endif -void light_process_omni(int idx, vec3 vertex, vec3 eye_vec,vec3 normal,vec3 binormal, vec3 tangent, vec3 albedo, vec3 transmission, float roughness, float metallic, float rim, float rim_tint, float clearcoat, float clearcoat_gloss, float anisotropy, float p_blob_intensity, inout vec3 diffuse_light, inout vec3 specular_light) { +void light_process_omni(int idx, vec3 vertex, vec3 eye_vec, vec3 normal, vec3 binormal, vec3 tangent, vec3 albedo, vec3 transmission, float roughness, float metallic, float specular, float rim, float rim_tint, float clearcoat, float clearcoat_gloss, float anisotropy, float p_blob_intensity, inout vec3 diffuse_light, inout vec3 specular_light) { - vec3 light_rel_vec = omni_lights[idx].light_pos_inv_radius.xyz-vertex; - float light_length = length( light_rel_vec ); - float normalized_distance = light_length*omni_lights[idx].light_pos_inv_radius.w; - float omni_attenuation = pow( max(1.0 - normalized_distance, 0.0), omni_lights[idx].light_direction_attenuation.w ); + vec3 light_rel_vec = omni_lights[idx].light_pos_inv_radius.xyz - vertex; + float light_length = length(light_rel_vec); + float normalized_distance = light_length * omni_lights[idx].light_pos_inv_radius.w; + float omni_attenuation = pow(max(1.0 - normalized_distance, 0.0), omni_lights[idx].light_direction_attenuation.w); vec3 light_attenuation = vec3(omni_attenuation); #if !defined(SHADOWS_DISABLED) - if (omni_lights[idx].light_params.w>0.5) { - //there is a shadowmap + if (omni_lights[idx].light_params.w > 0.5) { + // there is a shadowmap - highp vec3 splane=(omni_lights[idx].shadow_matrix * vec4(vertex,1.0)).xyz; - float shadow_len=length(splane); - splane=normalize(splane); - vec4 clamp_rect=omni_lights[idx].light_clamp; + highp vec3 splane = (omni_lights[idx].shadow_matrix * vec4(vertex, 1.0)).xyz; + float shadow_len = length(splane); + splane = normalize(splane); + vec4 clamp_rect = omni_lights[idx].light_clamp; - if (splane.z>=0.0) { + if (splane.z >= 0.0) { - splane.z+=1.0; + splane.z += 1.0; - clamp_rect.y+=clamp_rect.w; + clamp_rect.y += clamp_rect.w; } else { - splane.z=1.0 - splane.z; + splane.z = 1.0 - splane.z; /* - if (clamp_rect.z<clamp_rect.w) { - clamp_rect.x+=clamp_rect.z; + if (clamp_rect.z < clamp_rect.w) { + clamp_rect.x += clamp_rect.z; } else { - clamp_rect.y+=clamp_rect.w; + clamp_rect.y += clamp_rect.w; } */ - } - splane.xy/=splane.z; - splane.xy=splane.xy * 0.5 + 0.5; + splane.xy /= splane.z; + splane.xy = splane.xy * 0.5 + 0.5; splane.z = shadow_len * omni_lights[idx].light_pos_inv_radius.w; - splane.xy = clamp_rect.xy+splane.xy*clamp_rect.zw; - float shadow = sample_shadow(shadow_atlas,shadow_atlas_pixel_size,splane.xy,splane.z,clamp_rect); + splane.xy = clamp_rect.xy + splane.xy * clamp_rect.zw; + float shadow = sample_shadow(shadow_atlas, shadow_atlas_pixel_size, splane.xy, splane.z, clamp_rect); #ifdef USE_CONTACT_SHADOWS - if (shadow>0.01 && omni_lights[idx].shadow_color_contact.a>0.0) { - - float contact_shadow = contact_shadow_compute(vertex,normalize(light_rel_vec),min(light_length,omni_lights[idx].shadow_color_contact.a)); - shadow=min(shadow,contact_shadow); + if (shadow > 0.01 && omni_lights[idx].shadow_color_contact.a > 0.0) { + float contact_shadow = contact_shadow_compute(vertex, normalize(light_rel_vec), min(light_length, omni_lights[idx].shadow_color_contact.a)); + shadow = min(shadow, contact_shadow); } #endif - light_attenuation*=mix(omni_lights[idx].shadow_color_contact.rgb,vec3(1.0),shadow); + light_attenuation *= mix(omni_lights[idx].shadow_color_contact.rgb, vec3(1.0), shadow); } #endif //SHADOWS_DISABLED - - light_compute(normal,normalize(light_rel_vec),eye_vec,binormal,tangent,omni_lights[idx].light_color_energy.rgb,light_attenuation,albedo,transmission,omni_lights[idx].light_params.z*p_blob_intensity,roughness,metallic,rim * omni_attenuation,rim_tint,clearcoat,clearcoat_gloss,anisotropy,diffuse_light,specular_light); - + light_compute(normal, normalize(light_rel_vec), eye_vec, binormal, tangent, omni_lights[idx].light_color_energy.rgb, light_attenuation, albedo, transmission, omni_lights[idx].light_params.z * p_blob_intensity, roughness, metallic, specular, rim * omni_attenuation, rim_tint, clearcoat, clearcoat_gloss, anisotropy, diffuse_light, specular_light); } -void light_process_spot(int idx, vec3 vertex, vec3 eye_vec, vec3 normal, vec3 binormal, vec3 tangent,vec3 albedo, vec3 transmission,float roughness, float metallic, float rim, float rim_tint, float clearcoat, float clearcoat_gloss,float anisotropy,float p_blob_intensity, inout vec3 diffuse_light, inout vec3 specular_light) { +void light_process_spot(int idx, vec3 vertex, vec3 eye_vec, vec3 normal, vec3 binormal, vec3 tangent, vec3 albedo, vec3 transmission, float roughness, float metallic, float specular, float rim, float rim_tint, float clearcoat, float clearcoat_gloss, float anisotropy, float p_blob_intensity, inout vec3 diffuse_light, inout vec3 specular_light) { - vec3 light_rel_vec = spot_lights[idx].light_pos_inv_radius.xyz-vertex; - float light_length = length( light_rel_vec ); - float normalized_distance = light_length*spot_lights[idx].light_pos_inv_radius.w; - float spot_attenuation = pow( max(1.0 - normalized_distance, 0.001), spot_lights[idx].light_direction_attenuation.w ); + vec3 light_rel_vec = spot_lights[idx].light_pos_inv_radius.xyz - vertex; + float light_length = length(light_rel_vec); + float normalized_distance = light_length * spot_lights[idx].light_pos_inv_radius.w; + float spot_attenuation = pow(max(1.0 - normalized_distance, 0.001), spot_lights[idx].light_direction_attenuation.w); vec3 spot_dir = spot_lights[idx].light_direction_attenuation.xyz; - float spot_cutoff=spot_lights[idx].light_params.y; - float scos = max(dot(-normalize(light_rel_vec), spot_dir),spot_cutoff); - float spot_rim = max(0.0001,(1.0 - scos) / (1.0 - spot_cutoff)); - spot_attenuation*= 1.0 - pow( spot_rim, spot_lights[idx].light_params.x); + float spot_cutoff = spot_lights[idx].light_params.y; + float scos = max(dot(-normalize(light_rel_vec), spot_dir), spot_cutoff); + float spot_rim = max(0.0001, (1.0 - scos) / (1.0 - spot_cutoff)); + spot_attenuation *= 1.0 - pow(spot_rim, spot_lights[idx].light_params.x); vec3 light_attenuation = vec3(spot_attenuation); #if !defined(SHADOWS_DISABLED) - if (spot_lights[idx].light_params.w>0.5) { + if (spot_lights[idx].light_params.w > 0.5) { //there is a shadowmap - highp vec4 splane=(spot_lights[idx].shadow_matrix * vec4(vertex,1.0)); - splane.xyz/=splane.w; + highp vec4 splane = (spot_lights[idx].shadow_matrix * vec4(vertex, 1.0)); + splane.xyz /= splane.w; - float shadow = sample_shadow(shadow_atlas,shadow_atlas_pixel_size,splane.xy,splane.z,spot_lights[idx].light_clamp); + float shadow = sample_shadow(shadow_atlas, shadow_atlas_pixel_size, splane.xy, splane.z, spot_lights[idx].light_clamp); #ifdef USE_CONTACT_SHADOWS - if (shadow>0.01 && spot_lights[idx].shadow_color_contact.a>0.0) { - - float contact_shadow = contact_shadow_compute(vertex,normalize(light_rel_vec),min(light_length,spot_lights[idx].shadow_color_contact.a)); - shadow=min(shadow,contact_shadow); + if (shadow > 0.01 && spot_lights[idx].shadow_color_contact.a > 0.0) { + float contact_shadow = contact_shadow_compute(vertex, normalize(light_rel_vec), min(light_length, spot_lights[idx].shadow_color_contact.a)); + shadow = min(shadow, contact_shadow); } #endif - light_attenuation*=mix(spot_lights[idx].shadow_color_contact.rgb,vec3(1.0),shadow); + light_attenuation *= mix(spot_lights[idx].shadow_color_contact.rgb, vec3(1.0), shadow); } #endif //SHADOWS_DISABLED - light_compute(normal,normalize(light_rel_vec),eye_vec,binormal,tangent,spot_lights[idx].light_color_energy.rgb,light_attenuation,albedo,transmission,spot_lights[idx].light_params.z*p_blob_intensity,roughness,metallic,rim * spot_attenuation,rim_tint,clearcoat,clearcoat_gloss,anisotropy,diffuse_light,specular_light); - + light_compute(normal, normalize(light_rel_vec), eye_vec, binormal, tangent, spot_lights[idx].light_color_energy.rgb, light_attenuation, albedo, transmission, spot_lights[idx].light_params.z * p_blob_intensity, roughness, metallic, specular, rim * spot_attenuation, rim_tint, clearcoat, clearcoat_gloss, anisotropy, diffuse_light, specular_light); } -void reflection_process(int idx, vec3 vertex, vec3 normal,vec3 binormal, vec3 tangent,float roughness,float anisotropy,vec3 ambient,vec3 skybox, inout highp vec4 reflection_accum,inout highp vec4 ambient_accum) { +void reflection_process(int idx, vec3 vertex, vec3 normal, vec3 binormal, vec3 tangent, float roughness, float anisotropy, vec3 ambient, vec3 skybox, inout highp vec4 reflection_accum, inout highp vec4 ambient_accum) { - vec3 ref_vec = normalize(reflect(vertex,normal)); - vec3 local_pos = (reflections[idx].local_matrix * vec4(vertex,1.0)).xyz; + vec3 ref_vec = normalize(reflect(vertex, normal)); + vec3 local_pos = (reflections[idx].local_matrix * vec4(vertex, 1.0)).xyz; vec3 box_extents = reflections[idx].box_extents.xyz; - if (any(greaterThan(abs(local_pos),box_extents))) { //out of the reflection box + if (any(greaterThan(abs(local_pos), box_extents))) { //out of the reflection box return; } vec3 inner_pos = abs(local_pos / box_extents); - float blend = max(inner_pos.x,max(inner_pos.y,inner_pos.z)); + float blend = max(inner_pos.x, max(inner_pos.y, inner_pos.z)); //make blend more rounded - blend=mix(length(inner_pos),blend,blend); - blend*=blend; - blend=max(0.0, 1.0-blend); + blend = mix(length(inner_pos), blend, blend); + blend *= blend; + blend = max(0.0, 1.0 - blend); - if (reflections[idx].params.x>0.0){// compute reflection + if (reflections[idx].params.x > 0.0) { // compute reflection - vec3 local_ref_vec = (reflections[idx].local_matrix * vec4(ref_vec,0.0)).xyz; + vec3 local_ref_vec = (reflections[idx].local_matrix * vec4(ref_vec, 0.0)).xyz; if (reflections[idx].params.w > 0.5) { //box project vec3 nrdir = normalize(local_ref_vec); - vec3 rbmax = (box_extents - local_pos)/nrdir; - vec3 rbmin = (-box_extents - local_pos)/nrdir; - + vec3 rbmax = (box_extents - local_pos) / nrdir; + vec3 rbmin = (-box_extents - local_pos) / nrdir; - vec3 rbminmax = mix(rbmin,rbmax,greaterThan(nrdir,vec3(0.0,0.0,0.0))); + vec3 rbminmax = mix(rbmin, rbmax, greaterThan(nrdir, vec3(0.0, 0.0, 0.0))); float fa = min(min(rbminmax.x, rbminmax.y), rbminmax.z); vec3 posonbox = local_pos + nrdir * fa; local_ref_vec = posonbox - reflections[idx].box_offset.xyz; } - - vec4 clamp_rect=reflections[idx].atlas_clamp; + vec4 clamp_rect = reflections[idx].atlas_clamp; vec3 norm = normalize(local_ref_vec); - norm.xy/=1.0+abs(norm.z); - norm.xy=norm.xy * vec2(0.5,0.25) + vec2(0.5,0.25); - if (norm.z>0.0) { - norm.y=0.5-norm.y+0.5; + norm.xy /= 1.0 + abs(norm.z); + norm.xy = norm.xy * vec2(0.5, 0.25) + vec2(0.5, 0.25); + if (norm.z > 0.0) { + norm.y = 0.5 - norm.y + 0.5; } - vec2 atlas_uv = norm.xy * clamp_rect.zw + clamp_rect.xy; - atlas_uv = clamp(atlas_uv,clamp_rect.xy,clamp_rect.xy+clamp_rect.zw); + vec2 atlas_uv = norm.xy * clamp_rect.zw + clamp_rect.xy; + atlas_uv = clamp(atlas_uv, clamp_rect.xy, clamp_rect.xy + clamp_rect.zw); highp vec4 reflection; - reflection.rgb = textureLod(reflection_atlas,atlas_uv,roughness*5.0).rgb; + reflection.rgb = textureLod(reflection_atlas, atlas_uv, roughness * 5.0).rgb; if (reflections[idx].params.z < 0.5) { - reflection.rgb = mix(skybox,reflection.rgb,blend); + reflection.rgb = mix(skybox, reflection.rgb, blend); } - reflection.rgb*=reflections[idx].params.x; + reflection.rgb *= reflections[idx].params.x; reflection.a = blend; - reflection.rgb*=reflection.a; + reflection.rgb *= reflection.a; - reflection_accum+=reflection; + reflection_accum += reflection; } -#ifndef USE_LIGHTMAP - if (reflections[idx].ambient.a>0.0) { //compute ambient using skybox +#if !defined(USE_LIGHTMAP) && !defined(USE_LIGHTMAP_CAPTURE) + if (reflections[idx].ambient.a > 0.0) { //compute ambient using skybox + vec3 local_amb_vec = (reflections[idx].local_matrix * vec4(normal, 0.0)).xyz; - vec3 local_amb_vec = (reflections[idx].local_matrix * vec4(normal,0.0)).xyz; + vec3 splane = normalize(local_amb_vec); + vec4 clamp_rect = reflections[idx].atlas_clamp; - vec3 splane=normalize(local_amb_vec); - vec4 clamp_rect=reflections[idx].atlas_clamp; - - splane.z*=-1.0; - if (splane.z>=0.0) { - splane.z+=1.0; - clamp_rect.y+=clamp_rect.w; + splane.z *= -1.0; + if (splane.z >= 0.0) { + splane.z += 1.0; + clamp_rect.y += clamp_rect.w; } else { - splane.z=1.0 - splane.z; - splane.y=-splane.y; + splane.z = 1.0 - splane.z; + splane.y = -splane.y; } - splane.xy/=splane.z; - splane.xy=splane.xy * 0.5 + 0.5; + splane.xy /= splane.z; + splane.xy = splane.xy * 0.5 + 0.5; splane.xy = splane.xy * clamp_rect.zw + clamp_rect.xy; - splane.xy = clamp(splane.xy,clamp_rect.xy,clamp_rect.xy+clamp_rect.zw); + splane.xy = clamp(splane.xy, clamp_rect.xy, clamp_rect.xy + clamp_rect.zw); highp vec4 ambient_out; - ambient_out.a=blend; - ambient_out.rgb = textureLod(reflection_atlas,splane.xy,5.0).rgb; - ambient_out.rgb=mix(reflections[idx].ambient.rgb,ambient_out.rgb,reflections[idx].ambient.a); + ambient_out.a = blend; + ambient_out.rgb = textureLod(reflection_atlas, splane.xy, 5.0).rgb; + ambient_out.rgb = mix(reflections[idx].ambient.rgb, ambient_out.rgb, reflections[idx].ambient.a); if (reflections[idx].params.z < 0.5) { - ambient_out.rgb = mix(ambient,ambient_out.rgb,blend); + ambient_out.rgb = mix(ambient, ambient_out.rgb, blend); } ambient_out.rgb *= ambient_out.a; - ambient_accum+=ambient_out; + ambient_accum += ambient_out; } else { highp vec4 ambient_out; - ambient_out.a=blend; - ambient_out.rgb=reflections[idx].ambient.rgb; + ambient_out.a = blend; + ambient_out.rgb = reflections[idx].ambient.rgb; if (reflections[idx].params.z < 0.5) { - ambient_out.rgb = mix(ambient,ambient_out.rgb,blend); + ambient_out.rgb = mix(ambient, ambient_out.rgb, blend); } ambient_out.rgb *= ambient_out.a; - ambient_accum+=ambient_out; - + ambient_accum += ambient_out; } #endif } @@ -1448,13 +1415,13 @@ uniform bool gi_probe_blend_ambient2; vec3 voxel_cone_trace(mediump sampler3D probe, vec3 cell_size, vec3 pos, vec3 ambient, bool blend_ambient, vec3 direction, float tan_half_angle, float max_distance, float p_bias) { - float dist = p_bias;//1.0; //dot(direction,mix(vec3(-1.0),vec3(1.0),greaterThan(direction,vec3(0.0))))*2.0; - float alpha=0.0; + float dist = p_bias; //1.0; //dot(direction,mix(vec3(-1.0),vec3(1.0),greaterThan(direction,vec3(0.0))))*2.0; + float alpha = 0.0; vec3 color = vec3(0.0); - while(dist < max_distance && alpha < 0.95) { + while (dist < max_distance && alpha < 0.95) { float diameter = max(1.0, 2.0 * tan_half_angle * dist); - vec4 scolor = textureLod(probe, (pos + dist * direction) * cell_size, log2(diameter) ); + vec4 scolor = textureLod(probe, (pos + dist * direction) * cell_size, log2(diameter)); float a = (1.0 - alpha); color += scolor.rgb * a; alpha += a * scolor.a; @@ -1462,35 +1429,33 @@ vec3 voxel_cone_trace(mediump sampler3D probe, vec3 cell_size, vec3 pos, vec3 am } if (blend_ambient) { - color.rgb = mix(ambient,color.rgb,min(1.0,alpha/0.95)); + color.rgb = mix(ambient, color.rgb, min(1.0, alpha / 0.95)); } return color; } -void gi_probe_compute(mediump sampler3D probe, mat4 probe_xform, vec3 bounds,vec3 cell_size,vec3 pos, vec3 ambient, vec3 environment, bool blend_ambient,float multiplier, mat3 normal_mtx,vec3 ref_vec, float roughness,float p_bias,float p_normal_bias, inout vec4 out_spec, inout vec4 out_diff) { - +void gi_probe_compute(mediump sampler3D probe, mat4 probe_xform, vec3 bounds, vec3 cell_size, vec3 pos, vec3 ambient, vec3 environment, bool blend_ambient, float multiplier, mat3 normal_mtx, vec3 ref_vec, float roughness, float p_bias, float p_normal_bias, inout vec4 out_spec, inout vec4 out_diff) { - - vec3 probe_pos = (probe_xform * vec4(pos,1.0)).xyz; - vec3 ref_pos = (probe_xform * vec4(pos+ref_vec,1.0)).xyz; + vec3 probe_pos = (probe_xform * vec4(pos, 1.0)).xyz; + vec3 ref_pos = (probe_xform * vec4(pos + ref_vec, 1.0)).xyz; ref_vec = normalize(ref_pos - probe_pos); - probe_pos+=(probe_xform * vec4(normal_mtx[2],0.0)).xyz*p_normal_bias; + probe_pos += (probe_xform * vec4(normal_mtx[2], 0.0)).xyz * p_normal_bias; -/* out_diff.rgb = voxel_cone_trace(probe,cell_size,probe_pos,normalize((probe_xform * vec4(ref_vec,0.0)).xyz),0.0 ,100.0); + /* out_diff.rgb = voxel_cone_trace(probe,cell_size,probe_pos,normalize((probe_xform * vec4(ref_vec,0.0)).xyz),0.0 ,100.0); out_diff.a = 1.0; return;*/ //out_diff = vec4(textureLod(probe,probe_pos*cell_size,3.0).rgb,1.0); //return; //this causes corrupted pixels, i have no idea why.. - if (any(bvec2(any(lessThan(probe_pos,vec3(0.0))),any(greaterThan(probe_pos,bounds))))) { + if (any(bvec2(any(lessThan(probe_pos, vec3(0.0))), any(greaterThan(probe_pos, bounds))))) { return; } - vec3 blendv = abs(probe_pos/bounds * 2.0 - 1.0); - float blend = clamp(1.0-max(blendv.x,max(blendv.y,blendv.z)), 0.0, 1.0); + vec3 blendv = abs(probe_pos / bounds * 2.0 - 1.0); + float blend = clamp(1.0 - max(blendv.x, max(blendv.y, blendv.z)), 0.0, 1.0); //float blend=1.0; float max_distance = length(bounds); @@ -1499,14 +1464,13 @@ void gi_probe_compute(mediump sampler3D probe, mat4 probe_xform, vec3 bounds,vec #ifdef VCT_QUALITY_HIGH #define MAX_CONE_DIRS 6 - vec3 cone_dirs[MAX_CONE_DIRS] = vec3[] ( - vec3(0, 0, 1), - vec3(0.866025, 0, 0.5), - vec3(0.267617, 0.823639, 0.5), - vec3(-0.700629, 0.509037, 0.5), - vec3(-0.700629, -0.509037, 0.5), - vec3(0.267617, -0.823639, 0.5) - ); + vec3 cone_dirs[MAX_CONE_DIRS] = vec3[]( + vec3(0, 0, 1), + vec3(0.866025, 0, 0.5), + vec3(0.267617, 0.823639, 0.5), + vec3(-0.700629, 0.509037, 0.5), + vec3(-0.700629, -0.509037, 0.5), + vec3(0.267617, -0.823639, 0.5)); float cone_weights[MAX_CONE_DIRS] = float[](0.25, 0.15, 0.15, 0.15, 0.15, 0.15); float cone_angle_tan = 0.577; @@ -1515,54 +1479,50 @@ void gi_probe_compute(mediump sampler3D probe, mat4 probe_xform, vec3 bounds,vec #define MAX_CONE_DIRS 4 - vec3 cone_dirs[MAX_CONE_DIRS] = vec3[] ( + vec3 cone_dirs[MAX_CONE_DIRS] = vec3[]( vec3(0.707107, 0, 0.707107), vec3(0, 0.707107, 0.707107), vec3(-0.707107, 0, 0.707107), - vec3(0, -0.707107, 0.707107) - ); + vec3(0, -0.707107, 0.707107)); float cone_weights[MAX_CONE_DIRS] = float[](0.25, 0.25, 0.25, 0.25); float cone_angle_tan = 0.98269; - max_distance*=0.5; + max_distance *= 0.5; float min_ref_tan = 0.2; #endif - vec3 light=vec3(0.0); - for(int i=0;i<MAX_CONE_DIRS;i++) { - - vec3 dir = normalize( (probe_xform * vec4(pos + normal_mtx * cone_dirs[i],1.0)).xyz - probe_pos); - light+=cone_weights[i] * voxel_cone_trace(probe,cell_size,probe_pos,ambient,blend_ambient,dir,cone_angle_tan,max_distance,p_bias); + vec3 light = vec3(0.0); + for (int i = 0; i < MAX_CONE_DIRS; i++) { + vec3 dir = normalize((probe_xform * vec4(pos + normal_mtx * cone_dirs[i], 1.0)).xyz - probe_pos); + light += cone_weights[i] * voxel_cone_trace(probe, cell_size, probe_pos, ambient, blend_ambient, dir, cone_angle_tan, max_distance, p_bias); } - light*=multiplier; + light *= multiplier; - out_diff += vec4(light*blend,blend); + out_diff += vec4(light * blend, blend); //irradiance - vec3 irr_light = voxel_cone_trace(probe,cell_size,probe_pos,environment,blend_ambient,ref_vec,max(min_ref_tan,tan(roughness * 0.5 * M_PI)) ,max_distance,p_bias); + vec3 irr_light = voxel_cone_trace(probe, cell_size, probe_pos, environment, blend_ambient, ref_vec, max(min_ref_tan, tan(roughness * 0.5 * M_PI * 0.99)), max_distance, p_bias); irr_light *= multiplier; //irr_light=vec3(0.0); - out_spec += vec4(irr_light*blend,blend); - + out_spec += vec4(irr_light * blend, blend); } - void gi_probes_compute(vec3 pos, vec3 normal, float roughness, inout vec3 out_specular, inout vec3 out_ambient) { roughness = roughness * roughness; - vec3 ref_vec = normalize(reflect(normalize(pos),normal)); + vec3 ref_vec = normalize(reflect(normalize(pos), normal)); //find arbitrary tangent and bitangent, then build a matrix vec3 v0 = abs(normal.z) < 0.999 ? vec3(0, 0, 1) : vec3(0, 1, 0); vec3 tangent = normalize(cross(v0, normal)); vec3 bitangent = normalize(cross(tangent, normal)); - mat3 normal_mat = mat3(tangent,bitangent,normal); + mat3 normal_mat = mat3(tangent, bitangent, normal); vec4 diff_accum = vec4(0.0); vec4 spec_accum = vec4(0.0); @@ -1574,40 +1534,38 @@ void gi_probes_compute(vec3 pos, vec3 normal, float roughness, inout vec3 out_sp out_specular = vec3(0.0); - gi_probe_compute(gi_probe1,gi_probe_xform1,gi_probe_bounds1,gi_probe_cell_size1,pos,ambient,environment,gi_probe_blend_ambient1,gi_probe_multiplier1,normal_mat,ref_vec,roughness,gi_probe_bias1,gi_probe_normal_bias1,spec_accum,diff_accum); + gi_probe_compute(gi_probe1, gi_probe_xform1, gi_probe_bounds1, gi_probe_cell_size1, pos, ambient, environment, gi_probe_blend_ambient1, gi_probe_multiplier1, normal_mat, ref_vec, roughness, gi_probe_bias1, gi_probe_normal_bias1, spec_accum, diff_accum); if (gi_probe2_enabled) { - gi_probe_compute(gi_probe2,gi_probe_xform2,gi_probe_bounds2,gi_probe_cell_size2,pos,ambient,environment,gi_probe_blend_ambient2,gi_probe_multiplier2,normal_mat,ref_vec,roughness,gi_probe_bias2,gi_probe_normal_bias2,spec_accum,diff_accum); + gi_probe_compute(gi_probe2, gi_probe_xform2, gi_probe_bounds2, gi_probe_cell_size2, pos, ambient, environment, gi_probe_blend_ambient2, gi_probe_multiplier2, normal_mat, ref_vec, roughness, gi_probe_bias2, gi_probe_normal_bias2, spec_accum, diff_accum); } - if (diff_accum.a>0.0) { - diff_accum.rgb/=diff_accum.a; + if (diff_accum.a > 0.0) { + diff_accum.rgb /= diff_accum.a; } - if (spec_accum.a>0.0) { - spec_accum.rgb/=spec_accum.a; + if (spec_accum.a > 0.0) { + spec_accum.rgb /= spec_accum.a; } - out_specular+=spec_accum.rgb; - out_ambient+=diff_accum.rgb; - + out_specular += spec_accum.rgb; + out_ambient += diff_accum.rgb; } #endif - - void main() { #ifdef RENDER_DEPTH_DUAL_PARABOLOID - if (dp_clip>0.0) + if (dp_clip > 0.0) discard; #endif //lay out everything, whathever is unused is optimized away anyway highp vec3 vertex = vertex_interp; + vec3 view = -normalize(vertex_interp); vec3 albedo = vec3(1.0); vec3 transmission = vec3(0.0); float metallic = 0.0; @@ -1622,37 +1580,36 @@ void main() { vec2 anisotropy_flow = vec2(1.0, 0.0); #if defined(ENABLE_AO) - float ao=1.0; - float ao_light_affect=0.0; + float ao = 1.0; + float ao_light_affect = 0.0; #endif float alpha = 1.0; -#if defined(DO_SIDE_CHECK) - float side=gl_FrontFacing ? 1.0 : -1.0; -#else - float side=1.0; -#endif - - #if defined(ALPHA_SCISSOR_USED) float alpha_scissor = 0.5; #endif #if defined(ENABLE_TANGENT_INTERP) || defined(ENABLE_NORMALMAP) || defined(LIGHT_USE_ANISOTROPY) - vec3 binormal = normalize(binormal_interp)*side; - vec3 tangent = normalize(tangent_interp)*side; + vec3 binormal = normalize(binormal_interp); + vec3 tangent = normalize(tangent_interp); #else vec3 binormal = vec3(0.0); vec3 tangent = vec3(0.0); #endif - vec3 normal = normalize(normal_interp)*side; + vec3 normal = normalize(normal_interp); + +#if defined(DO_SIDE_CHECK) + if (!gl_FrontFacing) { + normal = -normal; + } +#endif #if defined(ENABLE_UV_INTERP) vec2 uv = uv_interp; #endif -#if defined(ENABLE_UV2_INTERP) || defined (USE_LIGHTMAP) +#if defined(ENABLE_UV2_INTERP) || defined(USE_LIGHTMAP) vec2 uv2 = uv2_interp; #endif @@ -1665,33 +1622,33 @@ void main() { vec3 normalmap = vec3(0.5); #endif - float normaldepth=1.0; + float normaldepth = 1.0; #if defined(SCREEN_UV_USED) - vec2 screen_uv = gl_FragCoord.xy*screen_pixel_size; + vec2 screen_uv = gl_FragCoord.xy * screen_pixel_size; #endif -#if defined (ENABLE_SSS) - float sss_strength=0.0; +#if defined(ENABLE_SSS) + float sss_strength = 0.0; #endif -{ - + { + /* clang-format off */ FRAGMENT_SHADER_CODE -} - + /* clang-format on */ + } #if defined(ALPHA_SCISSOR_USED) - if (alpha<alpha_scissor) { + if (alpha < alpha_scissor) { discard; } #endif #ifdef USE_OPAQUE_PREPASS - if (alpha<opaque_prepass_threshold) { + if (alpha < opaque_prepass_threshold) { discard; } @@ -1699,33 +1656,33 @@ FRAGMENT_SHADER_CODE #if defined(ENABLE_NORMALMAP) - normalmap.xy=normalmap.xy*2.0-1.0; - normalmap.z=sqrt(1.0-dot(normalmap.xy,normalmap.xy)); //always ignore Z, as it can be RG packed, Z may be pos/neg, etc. + normalmap.xy = normalmap.xy * 2.0 - 1.0; + normalmap.z = sqrt(max(0.0, 1.0 - dot(normalmap.xy, normalmap.xy))); //always ignore Z, as it can be RG packed, Z may be pos/neg, etc. - normal = normalize( mix(normal_interp,tangent * normalmap.x + binormal * normalmap.y + normal * normalmap.z,normaldepth) ) * side; + normal = normalize(mix(normal, tangent * normalmap.x + binormal * normalmap.y + normal * normalmap.z, normaldepth)); #endif #if defined(LIGHT_USE_ANISOTROPY) - if (anisotropy>0.01) { + if (anisotropy > 0.01) { //rotation matrix - mat3 rot = mat3( tangent, binormal, normal ); + mat3 rot = mat3(tangent, binormal, normal); //make local to space - tangent = normalize(rot * vec3(anisotropy_flow.x,anisotropy_flow.y,0.0)); - binormal = normalize(rot * vec3(-anisotropy_flow.y,anisotropy_flow.x,0.0)); + tangent = normalize(rot * vec3(anisotropy_flow.x, anisotropy_flow.y, 0.0)); + binormal = normalize(rot * vec3(-anisotropy_flow.y, anisotropy_flow.x, 0.0)); } #endif #ifdef ENABLE_CLIP_ALPHA - if (albedo.a<0.99) { + if (albedo.a < 0.99) { //used for doublepass and shadowmapping discard; } #endif -/////////////////////// LIGHTING ////////////////////////////// + /////////////////////// LIGHTING ////////////////////////////// //apply energy conservation @@ -1735,44 +1692,40 @@ FRAGMENT_SHADER_CODE vec3 diffuse_light = diffuse_light_interp.rgb; #else - vec3 specular_light = vec3(0.0,0.0,0.0); - vec3 diffuse_light = vec3(0.0,0.0,0.0); + vec3 specular_light = vec3(0.0, 0.0, 0.0); + vec3 diffuse_light = vec3(0.0, 0.0, 0.0); #endif vec3 ambient_light; - vec3 env_reflection_light = vec3(0.0,0.0,0.0); - - vec3 eye_vec = -normalize( vertex_interp ); - + vec3 env_reflection_light = vec3(0.0, 0.0, 0.0); + vec3 eye_vec = view; #ifdef USE_RADIANCE_MAP #ifdef AMBIENT_LIGHT_DISABLED - ambient_light=vec3(0.0,0.0,0.0); + ambient_light = vec3(0.0, 0.0, 0.0); #else { { //read radiance from dual paraboloid - vec3 ref_vec = reflect(-eye_vec,normal); //2.0 * ndotv * normal - view; // reflect(v, n); - ref_vec=normalize((radiance_inverse_xform * vec4(ref_vec,0.0)).xyz); - vec3 radiance = textureDualParaboloid(radiance_map,ref_vec,roughness) * bg_energy; + vec3 ref_vec = reflect(-eye_vec, normal); //2.0 * ndotv * normal - view; // reflect(v, n); + ref_vec = normalize((radiance_inverse_xform * vec4(ref_vec, 0.0)).xyz); + vec3 radiance = textureDualParaboloid(radiance_map, ref_vec, roughness) * bg_energy; env_reflection_light = radiance; - } //no longer a cubemap //vec3 radiance = textureLod(radiance_cube, r, lod).xyz * ( brdf.x + brdf.y); - } #ifndef USE_LIGHTMAP { - vec3 ambient_dir=normalize((radiance_inverse_xform * vec4(normal,0.0)).xyz); - vec3 env_ambient=textureDualParaboloid(radiance_map,ambient_dir,1.0) * bg_energy; + vec3 ambient_dir = normalize((radiance_inverse_xform * vec4(normal, 0.0)).xyz); + vec3 env_ambient = textureDualParaboloid(radiance_map, ambient_dir, 1.0) * bg_energy; - ambient_light=mix(ambient_light_color.rgb,env_ambient,radiance_ambient_contribution); + ambient_light = mix(ambient_light_color.rgb, env_ambient, radiance_ambient_contribution); //ambient_light=vec3(0.0,0.0,0.0); } #endif @@ -1781,23 +1734,23 @@ FRAGMENT_SHADER_CODE #else #ifdef AMBIENT_LIGHT_DISABLED - ambient_light=vec3(0.0,0.0,0.0); + ambient_light = vec3(0.0, 0.0, 0.0); #else - ambient_light=ambient_light_color.rgb; + ambient_light = ambient_light_color.rgb; #endif //AMBIENT_LIGHT_DISABLED #endif - ambient_light*=ambient_energy; + ambient_light *= ambient_energy; - float specular_blob_intensity=1.0; + float specular_blob_intensity = 1.0; #if defined(SPECULAR_TOON) - specular_blob_intensity*=specular * 2.0; + specular_blob_intensity *= specular * 2.0; #endif #if defined(USE_LIGHT_DIRECTIONAL) - vec3 light_attenuation=vec3(1.0); + vec3 light_attenuation = vec3(1.0); float depth_z = -vertex.z; #ifdef LIGHT_DIRECTIONAL_SHADOW @@ -1811,261 +1764,234 @@ FRAGMENT_SHADER_CODE if (depth_z < shadow_split_offsets.x) { #endif //LIGHT_USE_PSSM4 - vec3 pssm_coord; - float pssm_fade=0.0; + vec3 pssm_coord; + float pssm_fade = 0.0; #ifdef LIGHT_USE_PSSM_BLEND - float pssm_blend; - vec3 pssm_coord2; - bool use_blend=true; + float pssm_blend; + vec3 pssm_coord2; + bool use_blend = true; #endif - #ifdef LIGHT_USE_PSSM4 + if (depth_z < shadow_split_offsets.y) { - if (depth_z < shadow_split_offsets.y) { - - if (depth_z < shadow_split_offsets.x) { - - highp vec4 splane=(shadow_matrix1 * vec4(vertex,1.0)); - pssm_coord=splane.xyz/splane.w; + if (depth_z < shadow_split_offsets.x) { + highp vec4 splane = (shadow_matrix1 * vec4(vertex, 1.0)); + pssm_coord = splane.xyz / splane.w; #if defined(LIGHT_USE_PSSM_BLEND) - splane=(shadow_matrix2 * vec4(vertex,1.0)); - pssm_coord2=splane.xyz/splane.w; - pssm_blend=smoothstep(0.0,shadow_split_offsets.x,depth_z); + splane = (shadow_matrix2 * vec4(vertex, 1.0)); + pssm_coord2 = splane.xyz / splane.w; + pssm_blend = smoothstep(0.0, shadow_split_offsets.x, depth_z); #endif - } else { + } else { - highp vec4 splane=(shadow_matrix2 * vec4(vertex,1.0)); - pssm_coord=splane.xyz/splane.w; + highp vec4 splane = (shadow_matrix2 * vec4(vertex, 1.0)); + pssm_coord = splane.xyz / splane.w; #if defined(LIGHT_USE_PSSM_BLEND) - splane=(shadow_matrix3 * vec4(vertex,1.0)); - pssm_coord2=splane.xyz/splane.w; - pssm_blend=smoothstep(shadow_split_offsets.x,shadow_split_offsets.y,depth_z); + splane = (shadow_matrix3 * vec4(vertex, 1.0)); + pssm_coord2 = splane.xyz / splane.w; + pssm_blend = smoothstep(shadow_split_offsets.x, shadow_split_offsets.y, depth_z); #endif + } + } else { - } - } else { - - - if (depth_z < shadow_split_offsets.z) { + if (depth_z < shadow_split_offsets.z) { - highp vec4 splane=(shadow_matrix3 * vec4(vertex,1.0)); - pssm_coord=splane.xyz/splane.w; + highp vec4 splane = (shadow_matrix3 * vec4(vertex, 1.0)); + pssm_coord = splane.xyz / splane.w; #if defined(LIGHT_USE_PSSM_BLEND) - splane=(shadow_matrix4 * vec4(vertex,1.0)); - pssm_coord2=splane.xyz/splane.w; - pssm_blend=smoothstep(shadow_split_offsets.y,shadow_split_offsets.z,depth_z); + splane = (shadow_matrix4 * vec4(vertex, 1.0)); + pssm_coord2 = splane.xyz / splane.w; + pssm_blend = smoothstep(shadow_split_offsets.y, shadow_split_offsets.z, depth_z); #endif - } else { + } else { - highp vec4 splane=(shadow_matrix4 * vec4(vertex,1.0)); - pssm_coord=splane.xyz/splane.w; - pssm_fade = smoothstep(shadow_split_offsets.z,shadow_split_offsets.w,depth_z); + highp vec4 splane = (shadow_matrix4 * vec4(vertex, 1.0)); + pssm_coord = splane.xyz / splane.w; + pssm_fade = smoothstep(shadow_split_offsets.z, shadow_split_offsets.w, depth_z); #if defined(LIGHT_USE_PSSM_BLEND) - use_blend=false; + use_blend = false; #endif - + } } - } - - #endif //LIGHT_USE_PSSM4 #ifdef LIGHT_USE_PSSM2 - if (depth_z < shadow_split_offsets.x) { - - highp vec4 splane=(shadow_matrix1 * vec4(vertex,1.0)); - pssm_coord=splane.xyz/splane.w; + if (depth_z < shadow_split_offsets.x) { + highp vec4 splane = (shadow_matrix1 * vec4(vertex, 1.0)); + pssm_coord = splane.xyz / splane.w; #if defined(LIGHT_USE_PSSM_BLEND) - splane=(shadow_matrix2 * vec4(vertex,1.0)); - pssm_coord2=splane.xyz/splane.w; - pssm_blend=smoothstep(0.0,shadow_split_offsets.x,depth_z); + splane = (shadow_matrix2 * vec4(vertex, 1.0)); + pssm_coord2 = splane.xyz / splane.w; + pssm_blend = smoothstep(0.0, shadow_split_offsets.x, depth_z); #endif - } else { - highp vec4 splane=(shadow_matrix2 * vec4(vertex,1.0)); - pssm_coord=splane.xyz/splane.w; - pssm_fade = smoothstep(shadow_split_offsets.x,shadow_split_offsets.y,depth_z); + } else { + highp vec4 splane = (shadow_matrix2 * vec4(vertex, 1.0)); + pssm_coord = splane.xyz / splane.w; + pssm_fade = smoothstep(shadow_split_offsets.x, shadow_split_offsets.y, depth_z); #if defined(LIGHT_USE_PSSM_BLEND) - use_blend=false; + use_blend = false; #endif - - } + } #endif //LIGHT_USE_PSSM2 #if !defined(LIGHT_USE_PSSM4) && !defined(LIGHT_USE_PSSM2) - { //regular orthogonal - highp vec4 splane=(shadow_matrix1 * vec4(vertex,1.0)); - pssm_coord=splane.xyz/splane.w; - } + { //regular orthogonal + highp vec4 splane = (shadow_matrix1 * vec4(vertex, 1.0)); + pssm_coord = splane.xyz / splane.w; + } #endif + //one one sample - //one one sample - - float shadow = sample_shadow(directional_shadow,directional_shadow_pixel_size,pssm_coord.xy,pssm_coord.z,light_clamp); + float shadow = sample_shadow(directional_shadow, directional_shadow_pixel_size, pssm_coord.xy, pssm_coord.z, light_clamp); #if defined(LIGHT_USE_PSSM_BLEND) - if (use_blend) { - shadow=mix(shadow, sample_shadow(directional_shadow,directional_shadow_pixel_size,pssm_coord2.xy,pssm_coord2.z,light_clamp),pssm_blend); - } + if (use_blend) { + shadow = mix(shadow, sample_shadow(directional_shadow, directional_shadow_pixel_size, pssm_coord2.xy, pssm_coord2.z, light_clamp), pssm_blend); + } #endif #ifdef USE_CONTACT_SHADOWS - if (shadow>0.01 && shadow_color_contact.a>0.0) { + if (shadow > 0.01 && shadow_color_contact.a > 0.0) { - float contact_shadow = contact_shadow_compute(vertex,-light_direction_attenuation.xyz,shadow_color_contact.a); - shadow=min(shadow,contact_shadow); - - } + float contact_shadow = contact_shadow_compute(vertex, -light_direction_attenuation.xyz, shadow_color_contact.a); + shadow = min(shadow, contact_shadow); + } #endif - light_attenuation=mix(mix(shadow_color_contact.rgb,vec3(1.0),shadow),vec3(1.0),pssm_fade); - - + light_attenuation = mix(mix(shadow_color_contact.rgb, vec3(1.0), shadow), vec3(1.0), pssm_fade); } - #endif // !defined(SHADOWS_DISABLED) #endif //LIGHT_DIRECTIONAL_SHADOW #ifdef USE_VERTEX_LIGHTING - diffuse_light*=mix(vec3(1.0),light_attenuation,diffuse_light_interp.a); - specular_light*=mix(vec3(1.0),light_attenuation,specular_light_interp.a); + diffuse_light *= mix(vec3(1.0), light_attenuation, diffuse_light_interp.a); + specular_light *= mix(vec3(1.0), light_attenuation, specular_light_interp.a); #else - light_compute(normal,-light_direction_attenuation.xyz,eye_vec,binormal,tangent,light_color_energy.rgb,light_attenuation,albedo,transmission,light_params.z*specular_blob_intensity,roughness,metallic,rim,rim_tint,clearcoat,clearcoat_gloss,anisotropy,diffuse_light,specular_light); + light_compute(normal, -light_direction_attenuation.xyz, eye_vec, binormal, tangent, light_color_energy.rgb, light_attenuation, albedo, transmission, light_params.z * specular_blob_intensity, roughness, metallic, specular, rim, rim_tint, clearcoat, clearcoat_gloss, anisotropy, diffuse_light, specular_light); #endif - #endif //#USE_LIGHT_DIRECTIONAL #ifdef USE_GI_PROBES - gi_probes_compute(vertex,normal,roughness,env_reflection_light,ambient_light); + gi_probes_compute(vertex, normal, roughness, env_reflection_light, ambient_light); #endif #ifdef USE_LIGHTMAP - ambient_light = texture(lightmap,uv2).rgb * lightmap_energy; + ambient_light = texture(lightmap, uv2).rgb * lightmap_energy; #endif #ifdef USE_LIGHTMAP_CAPTURE { - vec3 cone_dirs[12] = vec3[] ( - vec3(0, 0, 1), - vec3(0.866025, 0, 0.5), - vec3(0.267617, 0.823639, 0.5), - vec3(-0.700629, 0.509037, 0.5), - vec3(-0.700629, -0.509037, 0.5), - vec3(0.267617, -0.823639, 0.5), - vec3(0, 0, -1), - vec3(0.866025, 0, -0.5), - vec3(0.267617, 0.823639, -0.5), - vec3(-0.700629, 0.509037, -0.5), - vec3(-0.700629, -0.509037, -0.5), - vec3(0.267617, -0.823639, -0.5) - ); - - - vec3 local_normal = normalize(camera_matrix * vec4(normal,0.0)).xyz; + vec3 cone_dirs[12] = vec3[]( + vec3(0, 0, 1), + vec3(0.866025, 0, 0.5), + vec3(0.267617, 0.823639, 0.5), + vec3(-0.700629, 0.509037, 0.5), + vec3(-0.700629, -0.509037, 0.5), + vec3(0.267617, -0.823639, 0.5), + vec3(0, 0, -1), + vec3(0.866025, 0, -0.5), + vec3(0.267617, 0.823639, -0.5), + vec3(-0.700629, 0.509037, -0.5), + vec3(-0.700629, -0.509037, -0.5), + vec3(0.267617, -0.823639, -0.5)); + + vec3 local_normal = normalize(camera_matrix * vec4(normal, 0.0)).xyz; vec4 captured = vec4(0.0); float sum = 0.0; - for(int i=0;i<12;i++) { - float amount = max(0.0,dot(local_normal,cone_dirs[i])); //not correct, but creates a nice wrap around effect - captured += lightmap_captures[i]*amount; - sum+=amount; + for (int i = 0; i < 12; i++) { + float amount = max(0.0, dot(local_normal, cone_dirs[i])); //not correct, but creates a nice wrap around effect + captured += lightmap_captures[i] * amount; + sum += amount; } - captured/=sum; + captured /= sum; if (lightmap_capture_sky) { - ambient_light = mix( ambient_light, captured.rgb, captured.a); + ambient_light = mix(ambient_light, captured.rgb, captured.a); } else { ambient_light = captured.rgb; } - } #endif #ifdef USE_FORWARD_LIGHTING - - highp vec4 reflection_accum = vec4(0.0,0.0,0.0,0.0); - highp vec4 ambient_accum = vec4(0.0,0.0,0.0,0.0); - for(int i=0;i<reflection_count;i++) { - reflection_process(reflection_indices[i],vertex,normal,binormal,tangent,roughness,anisotropy,ambient_light,env_reflection_light,reflection_accum,ambient_accum); + highp vec4 reflection_accum = vec4(0.0, 0.0, 0.0, 0.0); + highp vec4 ambient_accum = vec4(0.0, 0.0, 0.0, 0.0); + for (int i = 0; i < reflection_count; i++) { + reflection_process(reflection_indices[i], vertex, normal, binormal, tangent, roughness, anisotropy, ambient_light, env_reflection_light, reflection_accum, ambient_accum); } - if (reflection_accum.a>0.0) { - specular_light+=reflection_accum.rgb/reflection_accum.a; + if (reflection_accum.a > 0.0) { + specular_light += reflection_accum.rgb / reflection_accum.a; } else { - specular_light+=env_reflection_light; + specular_light += env_reflection_light; } -#ifndef USE_LIGHTMAP - if (ambient_accum.a>0.0) { - ambient_light=ambient_accum.rgb/ambient_accum.a; +#if !defined(USE_LIGHTMAP) && !defined(USE_LIGHTMAP_CAPTURE) + if (ambient_accum.a > 0.0) { + ambient_light = ambient_accum.rgb / ambient_accum.a; } #endif - #ifdef USE_VERTEX_LIGHTING - diffuse_light*=albedo; + diffuse_light *= albedo; #else - for(int i=0;i<omni_light_count;i++) { - light_process_omni(omni_light_indices[i],vertex,eye_vec,normal,binormal,tangent,albedo,transmission,roughness,metallic,rim,rim_tint,clearcoat,clearcoat_gloss,anisotropy,specular_blob_intensity,diffuse_light,specular_light); + for (int i = 0; i < omni_light_count; i++) { + light_process_omni(omni_light_indices[i], vertex, eye_vec, normal, binormal, tangent, albedo, transmission, roughness, metallic, specular, rim, rim_tint, clearcoat, clearcoat_gloss, anisotropy, specular_blob_intensity, diffuse_light, specular_light); } - for(int i=0;i<spot_light_count;i++) { - light_process_spot(spot_light_indices[i],vertex,eye_vec,normal,binormal,tangent,albedo,transmission,roughness,metallic,rim,rim_tint,clearcoat,clearcoat_gloss,anisotropy,specular_blob_intensity,diffuse_light,specular_light); + for (int i = 0; i < spot_light_count; i++) { + light_process_spot(spot_light_indices[i], vertex, eye_vec, normal, binormal, tangent, albedo, transmission, roughness, metallic, specular, rim, rim_tint, clearcoat, clearcoat_gloss, anisotropy, specular_blob_intensity, diffuse_light, specular_light); } #endif //USE_VERTEX_LIGHTING #endif - - - #ifdef RENDER_DEPTH //nothing happens, so a tree-ssa optimizer will result in no fragment shader :) #else - specular_light*=reflection_multiplier; - ambient_light*=albedo; //ambient must be multiplied by albedo at the end + specular_light *= reflection_multiplier; + ambient_light *= albedo; //ambient must be multiplied by albedo at the end #if defined(ENABLE_AO) - ambient_light*=ao; - ao_light_affect = mix(1.0,ao,ao_light_affect); - specular_light*=ao_light_affect; - diffuse_light*=ao_light_affect; + ambient_light *= ao; + ao_light_affect = mix(1.0, ao, ao_light_affect); + specular_light *= ao_light_affect; + diffuse_light *= ao_light_affect; #endif - - - //energy conservation - diffuse_light *= 1.0-metallic; // TODO: avoid all diffuse and ambient light calculations when metallic == 1 up to this point - ambient_light *= 1.0-metallic; - + // base color remapping + diffuse_light *= 1.0 - metallic; // TODO: avoid all diffuse and ambient light calculations when metallic == 1 up to this point + ambient_light *= 1.0 - metallic; { @@ -2076,27 +2002,24 @@ FRAGMENT_SHADER_CODE // Environment brdf approximation (Lazarov 2013) // see https://www.unrealengine.com/en-US/blog/physically-based-shading-on-mobile const vec4 c0 = vec4(-1.0, -0.0275, -0.572, 0.022); - const vec4 c1 = vec4( 1.0, 0.0425, 1.04, -0.04); + const vec4 c1 = vec4(1.0, 0.0425, 1.04, -0.04); vec4 r = roughness * c0 + c1; - float ndotv = clamp(dot(normal,eye_vec),0.0,1.0); - float a004 = min( r.x * r.x, exp2( -9.28 * ndotv ) ) * r.x + r.y; - vec2 AB = vec2( -1.04, 1.04 ) * a004 + r.zw; + float ndotv = clamp(dot(normal, eye_vec), 0.0, 1.0); + float a004 = min(r.x * r.x, exp2(-9.28 * ndotv)) * r.x + r.y; + vec2 env = vec2(-1.04, 1.04) * a004 + r.zw; - vec3 specular_color = metallic_to_specular_color(metallic, specular, albedo); - specular_light *= AB.x * specular_color + AB.y; + vec3 f0 = F0(metallic, specular, albedo); + specular_light *= env.x * f0 + env.y; #endif - } if (fog_color_enabled.a > 0.5) { - float fog_amount=0.0; - - + float fog_amount = 0.0; #ifdef USE_LIGHT_DIRECTIONAL - vec3 fog_color = mix( fog_color_enabled.rgb, fog_sun_color_amount.rgb,fog_sun_color_amount.a * pow(max( dot(normalize(vertex),-light_direction_attenuation.xyz), 0.0),8.0) ); + vec3 fog_color = mix(fog_color_enabled.rgb, fog_sun_color_amount.rgb, fog_sun_color_amount.a * pow(max(dot(normalize(vertex), -light_direction_attenuation.xyz), 0.0), 8.0)); #else vec3 fog_color = fog_color_enabled.rgb; @@ -2105,79 +2028,69 @@ FRAGMENT_SHADER_CODE //apply fog if (fog_depth_enabled) { + float fog_far = fog_depth_end > 0 ? fog_depth_end : z_far; - float fog_z = smoothstep(fog_depth_begin,z_far,length(vertex)); + float fog_z = smoothstep(fog_depth_begin, fog_far, length(vertex)); - fog_amount = pow(fog_z,fog_depth_curve); + fog_amount = pow(fog_z, fog_depth_curve) * fog_density; if (fog_transmit_enabled) { vec3 total_light = emission + ambient_light + specular_light + diffuse_light; - float transmit = pow(fog_z,fog_transmit_curve); - fog_color = mix(max(total_light,fog_color),fog_color,transmit); + float transmit = pow(fog_z, fog_transmit_curve); + fog_color = mix(max(total_light, fog_color), fog_color, transmit); } } if (fog_height_enabled) { - float y = (camera_matrix * vec4(vertex,1.0)).y; - fog_amount = max(fog_amount,pow(smoothstep(fog_height_min,fog_height_max,y),fog_height_curve)); + float y = (camera_matrix * vec4(vertex, 1.0)).y; + fog_amount = max(fog_amount, pow(smoothstep(fog_height_min, fog_height_max, y), fog_height_curve)); } float rev_amount = 1.0 - fog_amount; - emission = emission * rev_amount + fog_color * fog_amount; - ambient_light*=rev_amount; - specular_light*rev_amount; - diffuse_light*=rev_amount; - + ambient_light *= rev_amount; + specular_light *= rev_amount; + diffuse_light *= rev_amount; } #ifdef USE_MULTIPLE_RENDER_TARGETS - #ifdef SHADELESS - diffuse_buffer=vec4(albedo.rgb,0.0); - specular_buffer=vec4(0.0); + diffuse_buffer = vec4(albedo.rgb, 0.0); + specular_buffer = vec4(0.0); #else - - //approximate ambient scale for SSAO, since we will lack full ambient - float max_emission=max(emission.r,max(emission.g,emission.b)); - float max_ambient=max(ambient_light.r,max(ambient_light.g,ambient_light.b)); - float max_diffuse=max(diffuse_light.r,max(diffuse_light.g,diffuse_light.b)); - float total_ambient = max_ambient+max_diffuse+max_emission; - float ambient_scale = (total_ambient>0.0) ? (max_ambient+ambient_occlusion_affect_light*max_diffuse)/total_ambient : 0.0; + float max_emission = max(emission.r, max(emission.g, emission.b)); + float max_ambient = max(ambient_light.r, max(ambient_light.g, ambient_light.b)); + float max_diffuse = max(diffuse_light.r, max(diffuse_light.g, diffuse_light.b)); + float total_ambient = max_ambient + max_diffuse + max_emission; + float ambient_scale = (total_ambient > 0.0) ? (max_ambient + ambient_occlusion_affect_light * max_diffuse) / total_ambient : 0.0; #if defined(ENABLE_AO) - ambient_scale=mix(0.0,ambient_scale,ambient_occlusion_affect_ao_channel); + ambient_scale = mix(0.0, ambient_scale, ambient_occlusion_affect_ao_channel); #endif - diffuse_buffer=vec4(emission+diffuse_light+ambient_light,ambient_scale); - specular_buffer=vec4(specular_light,metallic); + diffuse_buffer = vec4(emission + diffuse_light + ambient_light, ambient_scale); + specular_buffer = vec4(specular_light, metallic); #endif //SHADELESS - normal_mr_buffer=vec4(normalize(normal)*0.5+0.5,roughness); + normal_mr_buffer = vec4(normalize(normal) * 0.5 + 0.5, roughness); -#if defined (ENABLE_SSS) +#if defined(ENABLE_SSS) sss_buffer = sss_strength; #endif - #else //USE_MULTIPLE_RENDER_TARGETS - #ifdef SHADELESS - frag_color=vec4(albedo,alpha); + frag_color = vec4(albedo, alpha); #else - frag_color=vec4(emission+ambient_light+diffuse_light+specular_light,alpha); + frag_color = vec4(emission + ambient_light + diffuse_light + specular_light, alpha); #endif //SHADELESS #endif //USE_MULTIPLE_RENDER_TARGETS - - #endif //RENDER_DEPTH - - } diff --git a/drivers/gles3/shaders/screen_space_reflection.glsl b/drivers/gles3/shaders/screen_space_reflection.glsl index b2e6f7a736..86546319a0 100644 --- a/drivers/gles3/shaders/screen_space_reflection.glsl +++ b/drivers/gles3/shaders/screen_space_reflection.glsl @@ -1,8 +1,9 @@ +/* clang-format off */ [vertex] - -layout(location=0) in highp vec4 vertex_attrib; -layout(location=4) in vec2 uv_in; +layout(location = 0) in highp vec4 vertex_attrib; +/* clang-format on */ +layout(location = 4) in vec2 uv_in; out vec2 uv_interp; out vec2 pos_interp; @@ -11,13 +12,14 @@ void main() { uv_interp = uv_in; gl_Position = vertex_attrib; - pos_interp.xy=gl_Position.xy; + pos_interp.xy = gl_Position.xy; } +/* clang-format off */ [fragment] - in vec2 uv_interp; +/* clang-format on */ in vec2 pos_interp; uniform sampler2D source_diffuse; //texunit:0 @@ -40,81 +42,67 @@ uniform float depth_tolerance; uniform float distance_fade; uniform float curve_fade_in; - layout(location = 0) out vec4 frag_color; - -vec2 view_to_screen(vec3 view_pos,out float w) { - vec4 projected = projection * vec4(view_pos, 1.0); - projected.xyz /= projected.w; - projected.xy = projected.xy * 0.5 + 0.5; - w=projected.w; - return projected.xy; +vec2 view_to_screen(vec3 view_pos, out float w) { + vec4 projected = projection * vec4(view_pos, 1.0); + projected.xyz /= projected.w; + projected.xy = projected.xy * 0.5 + 0.5; + w = projected.w; + return projected.xy; } - - #define M_PI 3.14159265359 void main() { - - //// - - vec4 diffuse = texture( source_diffuse, uv_interp ); - vec4 normal_roughness = texture( source_normal_roughness, uv_interp); + vec4 diffuse = texture(source_diffuse, uv_interp); + vec4 normal_roughness = texture(source_normal_roughness, uv_interp); vec3 normal; - - normal = normal_roughness.xyz*2.0-1.0; + normal = normal_roughness.xyz * 2.0 - 1.0; float roughness = normal_roughness.w; - float depth_tex = texture(source_depth,uv_interp).r; + float depth_tex = texture(source_depth, uv_interp).r; - vec4 world_pos = inverse_projection * vec4( uv_interp*2.0-1.0, depth_tex*2.0-1.0, 1.0 ); - vec3 vertex = world_pos.xyz/world_pos.w; + vec4 world_pos = inverse_projection * vec4(uv_interp * 2.0 - 1.0, depth_tex * 2.0 - 1.0, 1.0); + vec3 vertex = world_pos.xyz / world_pos.w; vec3 view_dir = normalize(vertex); vec3 ray_dir = normalize(reflect(view_dir, normal)); - if (dot(ray_dir,normal)<0.001) { - frag_color=vec4(0.0); + if (dot(ray_dir, normal) < 0.001) { + frag_color = vec4(0.0); return; } //ray_dir = normalize(view_dir - normal * dot(normal,view_dir) * 2.0); - - //ray_dir = normalize(vec3(1,1,-1)); - + //ray_dir = normalize(vec3(1, 1, -1)); //////////////// - - //make ray length and clip it against the near plane (don't want to trace beyond visible) + // make ray length and clip it against the near plane (don't want to trace beyond visible) float ray_len = (vertex.z + ray_dir.z * camera_z_far) > -camera_z_near ? (-camera_z_near - vertex.z) / ray_dir.z : camera_z_far; - vec3 ray_end = vertex + ray_dir*ray_len; + vec3 ray_end = vertex + ray_dir * ray_len; float w_begin; - vec2 vp_line_begin = view_to_screen(vertex,w_begin); + vec2 vp_line_begin = view_to_screen(vertex, w_begin); float w_end; - vec2 vp_line_end = view_to_screen( ray_end, w_end); - vec2 vp_line_dir = vp_line_end-vp_line_begin; - - //we need to interpolate w along the ray, to generate perspective correct reflections - - w_begin = 1.0/w_begin; - w_end = 1.0/w_end; + vec2 vp_line_end = view_to_screen(ray_end, w_end); + vec2 vp_line_dir = vp_line_end - vp_line_begin; + // we need to interpolate w along the ray, to generate perspective correct reflections + w_begin = 1.0 / w_begin; + w_end = 1.0 / w_end; - float z_begin = vertex.z*w_begin; - float z_end = ray_end.z*w_end; + float z_begin = vertex.z * w_begin; + float z_end = ray_end.z * w_end; - vec2 line_begin = vp_line_begin/pixel_size; - vec2 line_dir = vp_line_dir/pixel_size; + vec2 line_begin = vp_line_begin / pixel_size; + vec2 line_dir = vp_line_dir / pixel_size; float z_dir = z_end - z_begin; float w_dir = w_end - w_begin; - // clip the line to the viewport edges float scale_max_x = min(1.0, 0.99 * (1.0 - vp_line_begin.x) / max(1e-5, vp_line_dir.x)); @@ -124,126 +112,114 @@ void main() { float line_clip = min(scale_max_x, scale_max_y) * min(scale_min_x, scale_min_y); line_dir *= line_clip; z_dir *= line_clip; - w_dir *=line_clip; + w_dir *= line_clip; - //clip z and w advance to line advance - vec2 line_advance = normalize(line_dir); //down to pixel - float step_size = length(line_advance)/length(line_dir); - float z_advance = z_dir*step_size; // adapt z advance to line advance - float w_advance = w_dir*step_size; // adapt w advance to line advance + // clip z and w advance to line advance + vec2 line_advance = normalize(line_dir); // down to pixel + float step_size = length(line_advance) / length(line_dir); + float z_advance = z_dir * step_size; // adapt z advance to line advance + float w_advance = w_dir * step_size; // adapt w advance to line advance - //make line advance faster if direction is closer to pixel edges (this avoids sampling the same pixel twice) - float advance_angle_adj = 1.0/max(abs(line_advance.x),abs(line_advance.y)); - line_advance*=advance_angle_adj; // adapt z advance to line advance - z_advance*=advance_angle_adj; - w_advance*=advance_angle_adj; + // make line advance faster if direction is closer to pixel edges (this avoids sampling the same pixel twice) + float advance_angle_adj = 1.0 / max(abs(line_advance.x), abs(line_advance.y)); + line_advance *= advance_angle_adj; // adapt z advance to line advance + z_advance *= advance_angle_adj; + w_advance *= advance_angle_adj; vec2 pos = line_begin; float z = z_begin; float w = w_begin; - float z_from=z/w; - float z_to=z_from; + float z_from = z / w; + float z_to = z_from; float depth; - vec2 prev_pos=pos; + vec2 prev_pos = pos; - bool found=false; + bool found = false; - float steps_taken=0.0; + float steps_taken = 0.0; - for(int i=0;i<num_steps;i++) { + for (int i = 0; i < num_steps; i++) { - pos+=line_advance; - z+=z_advance; - w+=w_advance; + pos += line_advance; + z += z_advance; + w += w_advance; - //convert to linear depth + // convert to linear depth - depth = texture(source_depth, pos*pixel_size).r * 2.0 - 1.0; + depth = texture(source_depth, pos * pixel_size).r * 2.0 - 1.0; #ifdef USE_ORTHOGONAL_PROJECTION - depth = ((depth + (camera_z_far + camera_z_near)/(camera_z_far - camera_z_near)) * (camera_z_far - camera_z_near))/2.0; + depth = ((depth + (camera_z_far + camera_z_near) / (camera_z_far - camera_z_near)) * (camera_z_far - camera_z_near)) / 2.0; #else depth = 2.0 * camera_z_near * camera_z_far / (camera_z_far + camera_z_near - depth * (camera_z_far - camera_z_near)); #endif - depth=-depth; + depth = -depth; z_from = z_to; - z_to = z/w; + z_to = z / w; - if (depth>z_to) { - //if depth was surpassed - if (depth<=max(z_to,z_from)+depth_tolerance) { - //check the depth tolerance - found=true; + if (depth > z_to) { + // if depth was surpassed + if (depth <= max(z_to, z_from) + depth_tolerance) { + // check the depth tolerance + found = true; } break; } - steps_taken+=1.0; - prev_pos=pos; + steps_taken += 1.0; + prev_pos = pos; } - - - if (found) { - float margin_blend=1.0; + float margin_blend = 1.0; - - vec2 margin = vec2((viewport_size.x+viewport_size.y)*0.5*0.05); //make a uniform margin - if (any(bvec4(lessThan(pos,-margin),greaterThan(pos,viewport_size+margin)))) { - //clip outside screen + margin - frag_color=vec4(0.0); + vec2 margin = vec2((viewport_size.x + viewport_size.y) * 0.5 * 0.05); // make a uniform margin + if (any(bvec4(lessThan(pos, -margin), greaterThan(pos, viewport_size + margin)))) { + // clip outside screen + margin + frag_color = vec4(0.0); return; } { //blend fading out towards external margin - vec2 margin_grad = mix(pos-viewport_size,-pos,lessThan(pos,vec2(0.0))); - margin_blend = 1.0-smoothstep(0.0,margin.x,max(margin_grad.x,margin_grad.y)); - //margin_blend=1.0; - + vec2 margin_grad = mix(pos - viewport_size, -pos, lessThan(pos, vec2(0.0))); + margin_blend = 1.0 - smoothstep(0.0, margin.x, max(margin_grad.x, margin_grad.y)); + //margin_blend = 1.0; } vec2 final_pos; float grad; - grad=steps_taken/float(num_steps); - float initial_fade = curve_fade_in==0.0 ? 1.0 : pow(clamp(grad,0.0,1.0),curve_fade_in); - float fade = pow(clamp(1.0-grad,0.0,1.0),distance_fade)*initial_fade; - final_pos=pos; - - - - - - + grad = steps_taken / float(num_steps); + float initial_fade = curve_fade_in == 0.0 ? 1.0 : pow(clamp(grad, 0.0, 1.0), curve_fade_in); + float fade = pow(clamp(1.0 - grad, 0.0, 1.0), distance_fade) * initial_fade; + final_pos = pos; #ifdef REFLECT_ROUGHNESS - vec4 final_color; - //if roughness is enabled, do screen space cone tracing + // if roughness is enabled, do screen space cone tracing if (roughness > 0.001) { /////////////////////////////////////////////////////////////////////////////////////// - //use a blurred version (in consecutive mipmaps) of the screen to simulate roughness + // use a blurred version (in consecutive mipmaps) of the screen to simulate roughness - float gloss = 1.0-roughness; + float gloss = 1.0 - roughness; float cone_angle = roughness * M_PI * 0.5; vec2 cone_dir = final_pos - line_begin; float cone_len = length(cone_dir); - cone_dir = normalize(cone_dir); //will be used normalized from now on + cone_dir = normalize(cone_dir); // will be used normalized from now on float max_mipmap = filter_mipmap_levels - 1.0; - float gloss_mult=gloss; + float gloss_mult = gloss; - float rem_alpha=1.0; + float rem_alpha = 1.0; final_color = vec4(0.0); - for(int i=0;i<7;i++) { + for (int i = 0; i < 7; i++) { - float op_len = 2.0 * tan(cone_angle) * cone_len; //opposite side of iso triangle + float op_len = 2.0 * tan(cone_angle) * cone_len; // opposite side of iso triangle float radius; { - //fit to sphere inside cone (sphere ends at end of cone), something like this: + // fit to sphere inside cone (sphere ends at end of cone), something like this: // ___ // \O/ // V @@ -257,31 +233,31 @@ void main() { radius = (a * (sqrt(a2 + fh2) - a)) / (4.0f * h); } - //find the place where screen must be sampled - vec2 sample_pos = ( line_begin + cone_dir * (cone_len - radius) ) * pixel_size; - //radius is in pixels, so it's natural that log2(radius) maps to the right mipmap for the amount of pixels - float mipmap = clamp( log2( radius ), 0.0, max_mipmap ); + // find the place where screen must be sampled + vec2 sample_pos = (line_begin + cone_dir * (cone_len - radius)) * pixel_size; + // radius is in pixels, so it's natural that log2(radius) maps to the right mipmap for the amount of pixels + float mipmap = clamp(log2(radius), 0.0, max_mipmap); + //mipmap = max(mipmap - 1.0, 0.0); - //mipmap = max(mipmap-1.0,0.0); - //do sampling + // do sampling vec4 sample_color; { - sample_color = textureLod(source_diffuse,sample_pos,mipmap); + sample_color = textureLod(source_diffuse, sample_pos, mipmap); } - //multiply by gloss - sample_color.rgb*=gloss_mult; - sample_color.a=gloss_mult; + // multiply by gloss + sample_color.rgb *= gloss_mult; + sample_color.a = gloss_mult; rem_alpha -= sample_color.a; - if(rem_alpha < 0.0) { + if (rem_alpha < 0.0) { sample_color.rgb *= (1.0 - abs(rem_alpha)); } - final_color+=sample_color; + final_color += sample_color; - if (final_color.a>=0.95) { + if (final_color.a >= 0.95) { // This code of accumulating gloss and aborting on near one // makes sense when you think of cone tracing. // Think of it as if roughness was 0, then we could abort on the first @@ -290,29 +266,21 @@ void main() { break; } - cone_len-=radius*2.0; //go to next (smaller) circle. - - gloss_mult*=gloss; - + cone_len -= radius * 2.0; // go to next (smaller) circle. + gloss_mult *= gloss; } } else { - final_color = textureLod(source_diffuse,final_pos*pixel_size,0.0); + final_color = textureLod(source_diffuse, final_pos * pixel_size, 0.0); } - frag_color = vec4(final_color.rgb,fade*margin_blend); + frag_color = vec4(final_color.rgb, fade * margin_blend); #else - frag_color = vec4(textureLod(source_diffuse,final_pos*pixel_size,0.0).rgb,fade*margin_blend); + frag_color = vec4(textureLod(source_diffuse, final_pos * pixel_size, 0.0).rgb, fade * margin_blend); #endif - - } else { - frag_color = vec4(0.0,0.0,0.0,0.0); + frag_color = vec4(0.0, 0.0, 0.0, 0.0); } - - - } - diff --git a/drivers/gles3/shaders/ssao.glsl b/drivers/gles3/shaders/ssao.glsl index 219f0957e0..be44365169 100644 --- a/drivers/gles3/shaders/ssao.glsl +++ b/drivers/gles3/shaders/ssao.glsl @@ -1,34 +1,30 @@ +/* clang-format off */ [vertex] - -layout(location=0) in highp vec4 vertex_attrib; +layout(location = 0) in highp vec4 vertex_attrib; +/* clang-format on */ void main() { gl_Position = vertex_attrib; - gl_Position.z=1.0; + gl_Position.z = 1.0; } +/* clang-format off */ [fragment] #define TWO_PI 6.283185307179586476925286766559 #ifdef SSAO_QUALITY_HIGH - #define NUM_SAMPLES (80) - #endif #ifdef SSAO_QUALITY_LOW - #define NUM_SAMPLES (15) - #endif #if !defined(SSAO_QUALITY_LOW) && !defined(SSAO_QUALITY_HIGH) - #define NUM_SAMPLES (40) - #endif // If using depth mip levels, the log of the maximum pixel offset before we need to switch to a lower @@ -43,19 +39,21 @@ void main() { // This is the number of turns around the circle that the spiral pattern makes. This should be prime to prevent // taps from lining up. This particular choice was tuned for NUM_SAMPLES == 9 -const int ROTATIONS[] = int[]( 1, 1, 2, 3, 2, 5, 2, 3, 2, -3, 3, 5, 5, 3, 4, 7, 5, 5, 7, -9, 8, 5, 5, 7, 7, 7, 8, 5, 8, -11, 12, 7, 10, 13, 8, 11, 8, 7, 14, -11, 11, 13, 12, 13, 19, 17, 13, 11, 18, -19, 11, 11, 14, 17, 21, 15, 16, 17, 18, -13, 17, 11, 17, 19, 18, 25, 18, 19, 19, -29, 21, 19, 27, 31, 29, 21, 18, 17, 29, -31, 31, 23, 18, 25, 26, 25, 23, 19, 34, -19, 27, 21, 25, 39, 29, 17, 21, 27 ); +const int ROTATIONS[] = int[]( + 1, 1, 2, 3, 2, 5, 2, 3, 2, + 3, 3, 5, 5, 3, 4, 7, 5, 5, 7, + 9, 8, 5, 5, 7, 7, 7, 8, 5, 8, + 11, 12, 7, 10, 13, 8, 11, 8, 7, 14, + 11, 11, 13, 12, 13, 19, 17, 13, 11, 18, + 19, 11, 11, 14, 17, 21, 15, 16, 17, 18, + 13, 17, 11, 17, 19, 18, 25, 18, 19, 19, + 29, 21, 19, 27, 31, 29, 21, 18, 17, 29, + 31, 31, 23, 18, 25, 26, 25, 23, 19, 34, + 19, 27, 21, 25, 39, 29, 17, 21, 27); +/* clang-format on */ //#define NUM_SPIRAL_TURNS (7) -const int NUM_SPIRAL_TURNS = ROTATIONS[NUM_SAMPLES-1]; +const int NUM_SPIRAL_TURNS = ROTATIONS[NUM_SAMPLES - 1]; uniform sampler2D source_depth; //texunit:0 uniform highp usampler2D source_depth_mipmaps; //texunit:1 @@ -90,44 +88,41 @@ vec3 reconstructCSPosition(vec2 S, float z) { } vec3 getPosition(ivec2 ssP) { - vec3 P; - P.z = texelFetch(source_depth, ssP, 0).r; + vec3 P; + P.z = texelFetch(source_depth, ssP, 0).r; - P.z = P.z * 2.0 - 1.0; + P.z = P.z * 2.0 - 1.0; #ifdef USE_ORTHOGONAL_PROJECTION - P.z = ((P.z + (camera_z_far + camera_z_near)/(camera_z_far - camera_z_near)) * (camera_z_far - camera_z_near))/2.0; + P.z = ((P.z + (camera_z_far + camera_z_near) / (camera_z_far - camera_z_near)) * (camera_z_far - camera_z_near)) / 2.0; #else - P.z = 2.0 * camera_z_near * camera_z_far / (camera_z_far + camera_z_near - P.z * (camera_z_far - camera_z_near)); + P.z = 2.0 * camera_z_near * camera_z_far / (camera_z_far + camera_z_near - P.z * (camera_z_far - camera_z_near)); #endif - P.z = -P.z; + P.z = -P.z; - // Offset to pixel center - P = reconstructCSPosition(vec2(ssP) + vec2(0.5), P.z); - return P; + // Offset to pixel center + P = reconstructCSPosition(vec2(ssP) + vec2(0.5), P.z); + return P; } /** Reconstructs screen-space unit normal from screen-space position */ vec3 reconstructCSFaceNormal(vec3 C) { - return normalize(cross(dFdy(C), dFdx(C))); + return normalize(cross(dFdy(C), dFdx(C))); } - - /** Returns a unit vector and a screen-space radius for the tap on a unit disk (the caller should scale by the actual disk radius) */ -vec2 tapLocation(int sampleNumber, float spinAngle, out float ssR){ - // Radius relative to ssR - float alpha = (float(sampleNumber) + 0.5) * (1.0 / float(NUM_SAMPLES)); - float angle = alpha * (float(NUM_SPIRAL_TURNS) * 6.28) + spinAngle; +vec2 tapLocation(int sampleNumber, float spinAngle, out float ssR) { + // Radius relative to ssR + float alpha = (float(sampleNumber) + 0.5) * (1.0 / float(NUM_SAMPLES)); + float angle = alpha * (float(NUM_SPIRAL_TURNS) * 6.28) + spinAngle; - ssR = alpha; - return vec2(cos(angle), sin(angle)); + ssR = alpha; + return vec2(cos(angle), sin(angle)); } - /** Read the camera-space position of the point at screen-space pixel ssP + unitOffset * ssR. Assumes length(unitOffset) == 1 */ vec3 getOffsetPosition(ivec2 ssC, vec2 unitOffset, float ssR) { - // Derivation: - // mipLevel = floor(log(ssR / MAX_OFFSET)); + // Derivation: + // mipLevel = floor(log(ssR / MAX_OFFSET)); int mipLevel = clamp(int(floor(log2(ssR))) - LOG_MAX_OFFSET, 0, MAX_MIP_LEVEL); ivec2 ssP = ivec2(ssR * unitOffset) + ssC; @@ -138,98 +133,91 @@ vec3 getOffsetPosition(ivec2 ssC, vec2 unitOffset, float ssR) { // Manually clamp to the texture size because texelFetch bypasses the texture unit ivec2 mipP = clamp(ssP >> mipLevel, ivec2(0), (screen_size >> mipLevel) - ivec2(1)); - if (mipLevel < 1) { //read from depth buffer P.z = texelFetch(source_depth, mipP, 0).r; P.z = P.z * 2.0 - 1.0; #ifdef USE_ORTHOGONAL_PROJECTION - P.z = ((P.z + (camera_z_far + camera_z_near)/(camera_z_far - camera_z_near)) * (camera_z_far - camera_z_near))/2.0; + P.z = ((P.z + (camera_z_far + camera_z_near) / (camera_z_far - camera_z_near)) * (camera_z_far - camera_z_near)) / 2.0; #else P.z = 2.0 * camera_z_near * camera_z_far / (camera_z_far + camera_z_near - P.z * (camera_z_far - camera_z_near)); - #endif P.z = -P.z; } else { //read from mipmaps - uint d = texelFetch(source_depth_mipmaps, mipP, mipLevel-1).r; - P.z = -(float(d)/65535.0)*camera_z_far; + uint d = texelFetch(source_depth_mipmaps, mipP, mipLevel - 1).r; + P.z = -(float(d) / 65535.0) * camera_z_far; } - // Offset to pixel center P = reconstructCSPosition(vec2(ssP) + vec2(0.5), P.z); return P; } - - /** Compute the occlusion due to sample with index \a i about the pixel at \a ssC that corresponds - to camera-space point \a C with unit normal \a n_C, using maximum screen-space sampling radius \a ssDiskRadius + to camera-space point \a C with unit normal \a n_C, using maximum screen-space sampling radius \a ssDiskRadius - Note that units of H() in the HPG12 paper are meters, not - unitless. The whole falloff/sampling function is therefore - unitless. In this implementation, we factor out (9 / radius). + Note that units of H() in the HPG12 paper are meters, not + unitless. The whole falloff/sampling function is therefore + unitless. In this implementation, we factor out (9 / radius). - Four versions of the falloff function are implemented below + Four versions of the falloff function are implemented below */ -float sampleAO(in ivec2 ssC, in vec3 C, in vec3 n_C, in float ssDiskRadius,in float p_radius, in int tapIndex, in float randomPatternRotationAngle) { - // Offset on the unit disk, spun for this pixel - float ssR; - vec2 unitOffset = tapLocation(tapIndex, randomPatternRotationAngle, ssR); - ssR *= ssDiskRadius; +float sampleAO(in ivec2 ssC, in vec3 C, in vec3 n_C, in float ssDiskRadius, in float p_radius, in int tapIndex, in float randomPatternRotationAngle) { + // Offset on the unit disk, spun for this pixel + float ssR; + vec2 unitOffset = tapLocation(tapIndex, randomPatternRotationAngle, ssR); + ssR *= ssDiskRadius; - // The occluding point in camera space - vec3 Q = getOffsetPosition(ssC, unitOffset, ssR); + // The occluding point in camera space + vec3 Q = getOffsetPosition(ssC, unitOffset, ssR); - vec3 v = Q - C; + vec3 v = Q - C; - float vv = dot(v, v); - float vn = dot(v, n_C); + float vv = dot(v, v); + float vn = dot(v, n_C); - const float epsilon = 0.01; - float radius2 = p_radius*p_radius; + const float epsilon = 0.01; + float radius2 = p_radius * p_radius; - // A: From the HPG12 paper - // Note large epsilon to avoid overdarkening within cracks - //return float(vv < radius2) * max((vn - bias) / (epsilon + vv), 0.0) * radius2 * 0.6; + // A: From the HPG12 paper + // Note large epsilon to avoid overdarkening within cracks + //return float(vv < radius2) * max((vn - bias) / (epsilon + vv), 0.0) * radius2 * 0.6; - // B: Smoother transition to zero (lowers contrast, smoothing out corners). [Recommended] - float f=max(radius2 - vv, 0.0); - return f * f * f * max((vn - bias) / (epsilon + vv), 0.0); + // B: Smoother transition to zero (lowers contrast, smoothing out corners). [Recommended] + float f = max(radius2 - vv, 0.0); + return f * f * f * max((vn - bias) / (epsilon + vv), 0.0); - // C: Medium contrast (which looks better at high radii), no division. Note that the - // contribution still falls off with radius^2, but we've adjusted the rate in a way that is - // more computationally efficient and happens to be aesthetically pleasing. - // return 4.0 * max(1.0 - vv * invRadius2, 0.0) * max(vn - bias, 0.0); + // C: Medium contrast (which looks better at high radii), no division. Note that the + // contribution still falls off with radius^2, but we've adjusted the rate in a way that is + // more computationally efficient and happens to be aesthetically pleasing. + // return 4.0 * max(1.0 - vv * invRadius2, 0.0) * max(vn - bias, 0.0); - // D: Low contrast, no division operation - // return 2.0 * float(vv < radius * radius) * max(vn - bias, 0.0); + // D: Low contrast, no division operation + // return 2.0 * float(vv < radius * radius) * max(vn - bias, 0.0); } - - void main() { - - // Pixel being shaded ivec2 ssC = ivec2(gl_FragCoord.xy); // World space point being shaded vec3 C = getPosition(ssC); -/* if (C.z <= -camera_z_far*0.999) { - // We're on the skybox - visibility=1.0; - return; - }*/ + /* + if (C.z <= -camera_z_far * 0.999) { + // We're on the skybox + visibility=1.0; + return; + } + */ - //visibility=-C.z/camera_z_far; + //visibility = -C.z / camera_z_far; //return; #if 0 - vec3 n_C = texelFetch(source_normal,ssC,0).rgb * 2.0 - 1.0; + vec3 n_C = texelFetch(source_normal, ssC, 0).rgb * 2.0 - 1.0; #else vec3 n_C = reconstructCSFaceNormal(C); n_C = -n_C; @@ -251,7 +239,7 @@ void main() { #endif float sum = 0.0; for (int i = 0; i < NUM_SAMPLES; ++i) { - sum += sampleAO(ssC, C, n_C, ssDiskRadius, radius,i, randomPatternRotationAngle); + sum += sampleAO(ssC, C, n_C, ssDiskRadius, radius, i, randomPatternRotationAngle); } float A = max(0.0, 1.0 - sum * intensity_div_r6 * (5.0 / float(NUM_SAMPLES))); @@ -271,10 +259,10 @@ void main() { sum = 0.0; for (int i = 0; i < NUM_SAMPLES; ++i) { - sum += sampleAO(ssC, C, n_C, ssDiskRadius,radius2, i, randomPatternRotationAngle); + sum += sampleAO(ssC, C, n_C, ssDiskRadius, radius2, i, randomPatternRotationAngle); } - A= min(A,max(0.0, 1.0 - sum * intensity_div_r62 * (5.0 / float(NUM_SAMPLES)))); + A = min(A, max(0.0, 1.0 - sum * intensity_div_r62 * (5.0 / float(NUM_SAMPLES)))); #endif // Bilateral box-filter over a quad for free, respecting depth edges // (the difference that this makes is subtle) @@ -286,8 +274,4 @@ void main() { } visibility = A; - } - - - diff --git a/drivers/gles3/shaders/ssao_blur.glsl b/drivers/gles3/shaders/ssao_blur.glsl index 472dc21acf..c49ea1e957 100644 --- a/drivers/gles3/shaders/ssao_blur.glsl +++ b/drivers/gles3/shaders/ssao_blur.glsl @@ -1,26 +1,25 @@ +/* clang-format off */ [vertex] - -layout(location=0) in highp vec4 vertex_attrib; - +layout(location = 0) in highp vec4 vertex_attrib; +/* clang-format on */ void main() { gl_Position = vertex_attrib; - gl_Position.z=1.0; + gl_Position.z = 1.0; } +/* clang-format off */ [fragment] - uniform sampler2D source_ssao; //texunit:0 +/* clang-format on */ uniform sampler2D source_depth; //texunit:1 uniform sampler2D source_normal; //texunit:3 - layout(location = 0) out float visibility; - ////////////////////////////////////////////////////////////////////////////////////////////// // Tunable Parameters: @@ -28,32 +27,30 @@ layout(location = 0) out float visibility; uniform float edge_sharpness; /** Step in 2-pixel intervals since we already blurred against neighbors in the - first AO pass. This constant can be increased while R decreases to improve - performance at the expense of some dithering artifacts. + first AO pass. This constant can be increased while R decreases to improve + performance at the expense of some dithering artifacts. - Morgan found that a scale of 3 left a 1-pixel checkerboard grid that was - unobjectionable after shading was applied but eliminated most temporal incoherence - from using small numbers of sample taps. - */ + Morgan found that a scale of 3 left a 1-pixel checkerboard grid that was + unobjectionable after shading was applied but eliminated most temporal incoherence + from using small numbers of sample taps. + */ uniform int filter_scale; /** Filter radius in pixels. This will be multiplied by SCALE. */ -#define R (4) - +#define R (4) ////////////////////////////////////////////////////////////////////////////////////////////// - // Gaussian coefficients const float gaussian[R + 1] = -// float[](0.356642, 0.239400, 0.072410, 0.009869); -// float[](0.398943, 0.241971, 0.053991, 0.004432, 0.000134); // stddev = 1.0 - float[](0.153170, 0.144893, 0.122649, 0.092902, 0.062970); // stddev = 2.0 -// float[](0.111220, 0.107798, 0.098151, 0.083953, 0.067458, 0.050920, 0.036108); // stddev = 3.0 + //float[](0.356642, 0.239400, 0.072410, 0.009869); + //float[](0.398943, 0.241971, 0.053991, 0.004432, 0.000134); // stddev = 1.0 + float[](0.153170, 0.144893, 0.122649, 0.092902, 0.062970); // stddev = 2.0 +//float[](0.111220, 0.107798, 0.098151, 0.083953, 0.067458, 0.050920, 0.036108); // stddev = 3.0 -/** (1, 0) or (0, 1)*/ -uniform ivec2 axis; +/** (1, 0) or (0, 1) */ +uniform ivec2 axis; uniform float camera_z_far; uniform float camera_z_near; @@ -65,18 +62,18 @@ void main() { ivec2 ssC = ivec2(gl_FragCoord.xy); float depth = texelFetch(source_depth, ssC, 0).r; - //vec3 normal = texelFetch(source_normal,ssC,0).rgb * 2.0 - 1.0; + //vec3 normal = texelFetch(source_normal, ssC, 0).rgb * 2.0 - 1.0; depth = depth * 2.0 - 1.0; depth = 2.0 * camera_z_near * camera_z_far / (camera_z_far + camera_z_near - depth * (camera_z_far - camera_z_near)); float depth_divide = 1.0 / camera_z_far; -// depth*=depth_divide; + //depth *= depth_divide; /* - if (depth > camera_z_far*0.999) { - discard;//skybox + if (depth > camera_z_far * 0.999) { + discard; //skybox } */ @@ -96,23 +93,21 @@ void main() { if (r != 0) { ivec2 ppos = ssC + axis * (r * filter_scale); - float value = texelFetch(source_ssao, clamp(ppos,ivec2(0),clamp_limit), 0).r; - ivec2 rpos = clamp(ppos,ivec2(0),clamp_limit); + float value = texelFetch(source_ssao, clamp(ppos, ivec2(0), clamp_limit), 0).r; + ivec2 rpos = clamp(ppos, ivec2(0), clamp_limit); float temp_depth = texelFetch(source_depth, rpos, 0).r; //vec3 temp_normal = texelFetch(source_normal, rpos, 0).rgb * 2.0 - 1.0; temp_depth = temp_depth * 2.0 - 1.0; temp_depth = 2.0 * camera_z_near * camera_z_far / (camera_z_far + camera_z_near - temp_depth * (camera_z_far - camera_z_near)); -// temp_depth *= depth_divide; + //temp_depth *= depth_divide; // spatial domain: offset gaussian tap float weight = 0.3 + gaussian[abs(r)]; - //weight *= max(0.0,dot(temp_normal,normal)); + //weight *= max(0.0, dot(temp_normal, normal)); // range domain (the "bilateral" weight). As depth difference increases, decrease weight. - weight *= max(0.0, 1.0 - - edge_sharpness * abs(temp_depth - depth) - ); + weight *= max(0.0, 1.0 - edge_sharpness * abs(temp_depth - depth)); sum += value * weight; totalWeight += weight; diff --git a/drivers/gles3/shaders/ssao_minify.glsl b/drivers/gles3/shaders/ssao_minify.glsl index 647c762438..1696648dae 100644 --- a/drivers/gles3/shaders/ssao_minify.glsl +++ b/drivers/gles3/shaders/ssao_minify.glsl @@ -1,20 +1,22 @@ +/* clang-format off */ [vertex] - -layout(location=0) in highp vec4 vertex_attrib; +layout(location = 0) in highp vec4 vertex_attrib; +/* clang-format on */ void main() { gl_Position = vertex_attrib; } +/* clang-format off */ [fragment] - #ifdef MINIFY_START #define SDEPTH_TYPE highp sampler2D uniform float camera_z_far; +/* clang-format on */ uniform float camera_z_near; #else @@ -32,28 +34,23 @@ layout(location = 0) out mediump uint depth; void main() { - ivec2 ssP = ivec2(gl_FragCoord.xy); - // Rotated grid subsampling to avoid XY directional bias or Z precision bias while downsampling. - // On DX9, the bit-and can be implemented with floating-point modulo + // Rotated grid subsampling to avoid XY directional bias or Z precision bias while downsampling. + // On DX9, the bit-and can be implemented with floating-point modulo #ifdef MINIFY_START float fdepth = texelFetch(source_depth, clamp(ssP * 2 + ivec2(ssP.y & 1, ssP.x & 1), ivec2(0), from_size - ivec2(1)), source_mipmap).r; fdepth = fdepth * 2.0 - 1.0; #ifdef USE_ORTHOGONAL_PROJECTION - fdepth = ((fdepth + (camera_z_far + camera_z_near)/(camera_z_far - camera_z_near)) * (camera_z_far - camera_z_near))/2.0; + fdepth = ((fdepth + (camera_z_far + camera_z_near) / (camera_z_far - camera_z_near)) * (camera_z_far - camera_z_near)) / 2.0; #else fdepth = 2.0 * camera_z_near * camera_z_far / (camera_z_far + camera_z_near - fdepth * (camera_z_far - camera_z_near)); #endif fdepth /= camera_z_far; - depth = uint(clamp(fdepth*65535.0,0.0,65535.0)); + depth = uint(clamp(fdepth * 65535.0, 0.0, 65535.0)); #else depth = texelFetch(source_depth, clamp(ssP * 2 + ivec2(ssP.y & 1, ssP.x & 1), ivec2(0), from_size - ivec2(1)), source_mipmap).r; #endif - - } - - diff --git a/drivers/gles3/shaders/subsurf_scattering.glsl b/drivers/gles3/shaders/subsurf_scattering.glsl index fc66d66198..f40fb3a244 100644 --- a/drivers/gles3/shaders/subsurf_scattering.glsl +++ b/drivers/gles3/shaders/subsurf_scattering.glsl @@ -1,105 +1,93 @@ +/* clang-format off */ [vertex] - -layout(location=0) in highp vec4 vertex_attrib; -layout(location=4) in vec2 uv_in; +layout(location = 0) in highp vec4 vertex_attrib; +/* clang-format on */ +layout(location = 4) in vec2 uv_in; out vec2 uv_interp; - void main() { uv_interp = uv_in; gl_Position = vertex_attrib; } +/* clang-format off */ [fragment] //#define QUALIFIER uniform // some guy on the interweb says it may be faster with this #define QUALIFIER const #ifdef USE_25_SAMPLES - -const int kernel_size=25; -QUALIFIER vec2 kernel[25] = vec2[] ( - vec2(0.530605, 0.0), - vec2(0.000973794, -3.0), - vec2(0.00333804, -2.52083), - vec2(0.00500364, -2.08333), - vec2(0.00700976, -1.6875), - vec2(0.0094389, -1.33333), - vec2(0.0128496, -1.02083), - vec2(0.017924, -0.75), - vec2(0.0263642, -0.520833), - vec2(0.0410172, -0.333333), - vec2(0.0493588, -0.1875), - vec2(0.0402784, -0.0833333), - vec2(0.0211412, -0.0208333), - vec2(0.0211412, 0.0208333), - vec2(0.0402784, 0.0833333), - vec2(0.0493588, 0.1875), - vec2(0.0410172, 0.333333), - vec2(0.0263642, 0.520833), - vec2(0.017924, 0.75), - vec2(0.0128496, 1.02083), - vec2(0.0094389, 1.33333), - vec2(0.00700976, 1.6875), - vec2(0.00500364, 2.08333), - vec2(0.00333804, 2.52083), - vec2(0.000973794, 3.0) -); - +const int kernel_size = 25; +/* clang-format on */ +QUALIFIER vec2 kernel[25] = vec2[]( + vec2(0.530605, 0.0), + vec2(0.000973794, -3.0), + vec2(0.00333804, -2.52083), + vec2(0.00500364, -2.08333), + vec2(0.00700976, -1.6875), + vec2(0.0094389, -1.33333), + vec2(0.0128496, -1.02083), + vec2(0.017924, -0.75), + vec2(0.0263642, -0.520833), + vec2(0.0410172, -0.333333), + vec2(0.0493588, -0.1875), + vec2(0.0402784, -0.0833333), + vec2(0.0211412, -0.0208333), + vec2(0.0211412, 0.0208333), + vec2(0.0402784, 0.0833333), + vec2(0.0493588, 0.1875), + vec2(0.0410172, 0.333333), + vec2(0.0263642, 0.520833), + vec2(0.017924, 0.75), + vec2(0.0128496, 1.02083), + vec2(0.0094389, 1.33333), + vec2(0.00700976, 1.6875), + vec2(0.00500364, 2.08333), + vec2(0.00333804, 2.52083), + vec2(0.000973794, 3.0)); #endif //USE_25_SAMPLES #ifdef USE_17_SAMPLES - -const int kernel_size=17; - +const int kernel_size = 17; QUALIFIER vec2 kernel[17] = vec2[]( - vec2(0.536343, 0.0), - vec2(0.00317394, -2.0), - vec2(0.0100386, -1.53125), - vec2(0.0144609, -1.125), - vec2(0.0216301, -0.78125), - vec2(0.0347317, -0.5), - vec2(0.0571056, -0.28125), - vec2(0.0582416, -0.125), - vec2(0.0324462, -0.03125), - vec2(0.0324462, 0.03125), - vec2(0.0582416, 0.125), - vec2(0.0571056, 0.28125), - vec2(0.0347317, 0.5), - vec2(0.0216301, 0.78125), - vec2(0.0144609, 1.125), - vec2(0.0100386, 1.53125), - vec2(0.00317394,2.0) -); - + vec2(0.536343, 0.0), + vec2(0.00317394, -2.0), + vec2(0.0100386, -1.53125), + vec2(0.0144609, -1.125), + vec2(0.0216301, -0.78125), + vec2(0.0347317, -0.5), + vec2(0.0571056, -0.28125), + vec2(0.0582416, -0.125), + vec2(0.0324462, -0.03125), + vec2(0.0324462, 0.03125), + vec2(0.0582416, 0.125), + vec2(0.0571056, 0.28125), + vec2(0.0347317, 0.5), + vec2(0.0216301, 0.78125), + vec2(0.0144609, 1.125), + vec2(0.0100386, 1.53125), + vec2(0.00317394, 2.0)); #endif //USE_17_SAMPLES - #ifdef USE_11_SAMPLES - -const int kernel_size=11; - +const int kernel_size = 11; QUALIFIER vec2 kernel[11] = vec2[]( - vec2(0.560479, 0.0), - vec2(0.00471691, -2.0), - vec2(0.0192831, -1.28), - vec2(0.03639, -0.72), - vec2(0.0821904, -0.32), - vec2(0.0771802, -0.08), - vec2(0.0771802, 0.08), - vec2(0.0821904, 0.32), - vec2(0.03639, 0.72), - vec2(0.0192831, 1.28), - vec2(0.00471691,2.0) -); - + vec2(0.560479, 0.0), + vec2(0.00471691, -2.0), + vec2(0.0192831, -1.28), + vec2(0.03639, -0.72), + vec2(0.0821904, -0.32), + vec2(0.0771802, -0.08), + vec2(0.0771802, 0.08), + vec2(0.0821904, 0.32), + vec2(0.03639, 0.72), + vec2(0.0192831, 1.28), + vec2(0.00471691, 2.0)); #endif //USE_11_SAMPLES - - uniform float max_radius; uniform float camera_z_far; uniform float camera_z_near; @@ -115,28 +103,24 @@ layout(location = 0) out vec4 frag_color; void main() { - float strength = texture(source_sss,uv_interp).r; - strength*=strength; //stored as sqrt + float strength = texture(source_sss, uv_interp).r; + strength *= strength; //stored as sqrt // Fetch color of current pixel: vec4 base_color = texture(source_diffuse, uv_interp); - - if (strength>0.0) { - + if (strength > 0.0) { // Fetch linear depth of current pixel: float depth = texture(source_depth, uv_interp).r * 2.0 - 1.0; #ifdef USE_ORTHOGONAL_PROJECTION - depth = ((depth + (camera_z_far + camera_z_near)/(camera_z_far - camera_z_near)) * (camera_z_far - camera_z_near))/2.0; + depth = ((depth + (camera_z_far + camera_z_near) / (camera_z_far - camera_z_near)) * (camera_z_far - camera_z_near)) / 2.0; float scale = unit_size; //remember depth is negative by default in OpenGL #else depth = 2.0 * camera_z_near * camera_z_far / (camera_z_far + camera_z_near - depth * (camera_z_far - camera_z_near)); float scale = unit_size / depth; //remember depth is negative by default in OpenGL #endif - - // Calculate the final step to fetch the surrounding pixels: vec2 step = max_radius * scale * dir; step *= strength; // Modulate it using the alpha channel. @@ -157,35 +141,33 @@ void main() { #ifdef ENABLE_FOLLOW_SURFACE // If the difference in depth is huge, we lerp color back to "colorM": - float depth_cmp = texture(source_depth, offset).r *2.0 - 1.0; + float depth_cmp = texture(source_depth, offset).r * 2.0 - 1.0; #ifdef USE_ORTHOGONAL_PROJECTION - depth_cmp = ((depth_cmp + (camera_z_far + camera_z_near)/(camera_z_far - camera_z_near)) * (camera_z_far - camera_z_near))/2.0; + depth_cmp = ((depth_cmp + (camera_z_far + camera_z_near) / (camera_z_far - camera_z_near)) * (camera_z_far - camera_z_near)) / 2.0; #else depth_cmp = 2.0 * camera_z_near * camera_z_far / (camera_z_far + camera_z_near - depth_cmp * (camera_z_far - camera_z_near)); #endif - float s = clamp(300.0f * scale * - max_radius * abs(depth - depth_cmp),0.0,1.0); + float s = clamp(300.0f * scale * max_radius * abs(depth - depth_cmp), 0.0, 1.0); color = mix(color, base_color.rgb, s); #endif // Accumulate: - color*=kernel[i].x; + color *= kernel[i].x; #ifdef ENABLE_STRENGTH_WEIGHTING float color_s = texture(source_sss, offset).r; - color_weight+=color_s * kernel[i].x; - color*=color_s; + color_weight += color_s * kernel[i].x; + color *= color_s; #endif color_accum += color; - } #ifdef ENABLE_STRENGTH_WEIGHTING - color_accum/=color_weight; + color_accum /= color_weight; #endif - frag_color = vec4(color_accum,base_color.a); //keep alpha (used for SSAO) + frag_color = vec4(color_accum, base_color.a); //keep alpha (used for SSAO) } else { frag_color = base_color; } diff --git a/drivers/gles3/shaders/tonemap.glsl b/drivers/gles3/shaders/tonemap.glsl index 63475c9039..80ad003c80 100644 --- a/drivers/gles3/shaders/tonemap.glsl +++ b/drivers/gles3/shaders/tonemap.glsl @@ -1,26 +1,29 @@ +/* clang-format off */ [vertex] -layout (location = 0) in highp vec4 vertex_attrib; -layout (location = 4) in vec2 uv_in; +layout(location = 0) in highp vec4 vertex_attrib; +/* clang-format on */ +layout(location = 4) in vec2 uv_in; out vec2 uv_interp; -void main() -{ +void main() { gl_Position = vertex_attrib; uv_interp = uv_in; - #ifdef V_FLIP - uv_interp.y = 1.0f - uv_interp.y; - #endif +#ifdef V_FLIP + uv_interp.y = 1.0f - uv_interp.y; +#endif } +/* clang-format off */ [fragment] #if !defined(GLES_OVER_GL) - precision mediump float; +precision mediump float; #endif +/* clang-format on */ in vec2 uv_interp; @@ -30,116 +33,107 @@ uniform float exposure; uniform float white; #ifdef USE_AUTO_EXPOSURE - uniform highp sampler2D source_auto_exposure; //texunit:1 - uniform highp float auto_exposure_grey; +uniform highp sampler2D source_auto_exposure; //texunit:1 +uniform highp float auto_exposure_grey; #endif #if defined(USE_GLOW_LEVEL1) || defined(USE_GLOW_LEVEL2) || defined(USE_GLOW_LEVEL3) || defined(USE_GLOW_LEVEL4) || defined(USE_GLOW_LEVEL5) || defined(USE_GLOW_LEVEL6) || defined(USE_GLOW_LEVEL7) - #define USING_GLOW // only use glow when at least one glow level is selected +#define USING_GLOW // only use glow when at least one glow level is selected - uniform highp sampler2D source_glow; //texunit:2 - uniform highp float glow_intensity; +uniform highp sampler2D source_glow; //texunit:2 +uniform highp float glow_intensity; #endif #ifdef USE_BCS - uniform vec3 bcs; +uniform vec3 bcs; #endif #ifdef USE_COLOR_CORRECTION - uniform sampler2D color_correction; //texunit:3 +uniform sampler2D color_correction; //texunit:3 #endif -layout (location = 0) out vec4 frag_color; +layout(location = 0) out vec4 frag_color; #ifdef USE_GLOW_FILTER_BICUBIC - // w0, w1, w2, and w3 are the four cubic B-spline basis functions - float w0(float a) - { - return (1.0f / 6.0f) * (a * (a * (-a + 3.0f) - 3.0f) + 1.0f); - } - - float w1(float a) - { - return (1.0f / 6.0f) * (a * a * (3.0f * a - 6.0f) + 4.0f); - } - - float w2(float a) - { - return (1.0f / 6.0f) * (a * (a * (-3.0f * a + 3.0f) + 3.0f) + 1.0f); - } - - float w3(float a) - { - return (1.0f / 6.0f) * (a * a * a); - } - - // g0 and g1 are the two amplitude functions - float g0(float a) - { - return w0(a) + w1(a); - } - - float g1(float a) - { - return w2(a) + w3(a); - } - - // h0 and h1 are the two offset functions - float h0(float a) - { - return -1.0f + w1(a) / (w0(a) + w1(a)); - } - - float h1(float a) - { - return 1.0f + w3(a) / (w2(a) + w3(a)); - } - - uniform ivec2 glow_texture_size; - - vec4 texture2D_bicubic(sampler2D tex, vec2 uv, int p_lod) - { - float lod = float(p_lod); - vec2 tex_size = vec2(glow_texture_size >> p_lod); - vec2 pixel_size = vec2(1.0f) / tex_size; - - uv = uv * tex_size + vec2(0.5f); - - vec2 iuv = floor(uv); - vec2 fuv = fract(uv); - - float g0x = g0(fuv.x); - float g1x = g1(fuv.x); - float h0x = h0(fuv.x); - float h1x = h1(fuv.x); - float h0y = h0(fuv.y); - float h1y = h1(fuv.y); - - vec2 p0 = (vec2(iuv.x + h0x, iuv.y + h0y) - vec2(0.5f)) * pixel_size; - vec2 p1 = (vec2(iuv.x + h1x, iuv.y + h0y) - vec2(0.5f)) * pixel_size; - vec2 p2 = (vec2(iuv.x + h0x, iuv.y + h1y) - vec2(0.5f)) * pixel_size; - vec2 p3 = (vec2(iuv.x + h1x, iuv.y + h1y) - vec2(0.5f)) * pixel_size; - - return g0(fuv.y) * (g0x * textureLod(tex, p0,lod) + - g1x * textureLod(tex, p1,lod)) + - g1(fuv.y) * (g0x * textureLod(tex, p2,lod) + - g1x * textureLod(tex, p3,lod)); - } - - #define GLOW_TEXTURE_SAMPLE(m_tex, m_uv, m_lod) texture2D_bicubic(m_tex, m_uv, m_lod) +// w0, w1, w2, and w3 are the four cubic B-spline basis functions +float w0(float a) { + return (1.0f / 6.0f) * (a * (a * (-a + 3.0f) - 3.0f) + 1.0f); +} + +float w1(float a) { + return (1.0f / 6.0f) * (a * a * (3.0f * a - 6.0f) + 4.0f); +} + +float w2(float a) { + return (1.0f / 6.0f) * (a * (a * (-3.0f * a + 3.0f) + 3.0f) + 1.0f); +} + +float w3(float a) { + return (1.0f / 6.0f) * (a * a * a); +} + +// g0 and g1 are the two amplitude functions +float g0(float a) { + return w0(a) + w1(a); +} + +float g1(float a) { + return w2(a) + w3(a); +} + +// h0 and h1 are the two offset functions +float h0(float a) { + return -1.0f + w1(a) / (w0(a) + w1(a)); +} + +float h1(float a) { + return 1.0f + w3(a) / (w2(a) + w3(a)); +} + +uniform ivec2 glow_texture_size; + +vec4 texture2D_bicubic(sampler2D tex, vec2 uv, int p_lod) { + float lod = float(p_lod); + vec2 tex_size = vec2(glow_texture_size >> p_lod); + vec2 pixel_size = vec2(1.0f) / tex_size; + + uv = uv * tex_size + vec2(0.5f); + + vec2 iuv = floor(uv); + vec2 fuv = fract(uv); + + float g0x = g0(fuv.x); + float g1x = g1(fuv.x); + float h0x = h0(fuv.x); + float h1x = h1(fuv.x); + float h0y = h0(fuv.y); + float h1y = h1(fuv.y); + + vec2 p0 = (vec2(iuv.x + h0x, iuv.y + h0y) - vec2(0.5f)) * pixel_size; + vec2 p1 = (vec2(iuv.x + h1x, iuv.y + h0y) - vec2(0.5f)) * pixel_size; + vec2 p2 = (vec2(iuv.x + h0x, iuv.y + h1y) - vec2(0.5f)) * pixel_size; + vec2 p3 = (vec2(iuv.x + h1x, iuv.y + h1y) - vec2(0.5f)) * pixel_size; + + return (g0(fuv.y) * (g0x * textureLod(tex, p0, lod) + g1x * textureLod(tex, p1, lod))) + + (g1(fuv.y) * (g0x * textureLod(tex, p2, lod) + g1x * textureLod(tex, p3, lod))); +} + +#define GLOW_TEXTURE_SAMPLE(m_tex, m_uv, m_lod) texture2D_bicubic(m_tex, m_uv, m_lod) #else - #define GLOW_TEXTURE_SAMPLE(m_tex, m_uv, m_lod) textureLod(m_tex, m_uv, float(m_lod)) +#define GLOW_TEXTURE_SAMPLE(m_tex, m_uv, m_lod) textureLod(m_tex, m_uv, float(m_lod)) #endif -vec3 tonemap_filmic(vec3 color, float white) -{ - const float A = 0.15f; - const float B = 0.50f; +vec3 tonemap_filmic(vec3 color, float white) { + // exposure bias: input scale (color *= bias, white *= bias) to make the brighness consistent with other tonemappers + // also useful to scale the input to the range that the tonemapper is designed for (some require very high input values) + // has no effect on the curve's general shape or visual properties + const float exposure_bias = 2.0f; + const float A = 0.22f * exposure_bias * exposure_bias; // bias baked into constants for performance + const float B = 0.30f * exposure_bias; const float C = 0.10f; const float D = 0.20f; - const float E = 0.02f; + const float E = 0.01f; const float F = 0.30f; - const float W = 11.2f; vec3 color_tonemapped = ((color * (A * color + C * B) + D * E) / (color * (A * color + B) + D * F)) - E / F; float white_tonemapped = ((white * (A * white + C * B) + D * E) / (white * (A * white + B) + D * F)) - E / F; @@ -147,12 +141,12 @@ vec3 tonemap_filmic(vec3 color, float white) return clamp(color_tonemapped / white_tonemapped, vec3(0.0f), vec3(1.0f)); } -vec3 tonemap_aces(vec3 color, float white) -{ - const float A = 2.51f; - const float B = 0.03f; - const float C = 2.43f; - const float D = 0.59f; +vec3 tonemap_aces(vec3 color, float white) { + const float exposure_bias = 0.85f; + const float A = 2.51f * exposure_bias * exposure_bias; + const float B = 0.03f * exposure_bias; + const float C = 2.43f * exposure_bias * exposure_bias; + const float D = 0.59f * exposure_bias; const float E = 0.14f; vec3 color_tonemapped = (color * (A * color + B)) / (color * (C * color + D) + E); @@ -161,96 +155,90 @@ vec3 tonemap_aces(vec3 color, float white) return clamp(color_tonemapped / white_tonemapped, vec3(0.0f), vec3(1.0f)); } -vec3 tonemap_reindhart(vec3 color, float white) -{ - return clamp((color) / (1.0f + color) * (1.0f + (color / (white))), vec3(0.0f), vec3(1.0f)); // whitepoint is probably not in linear space here! +vec3 tonemap_reinhard(vec3 color, float white) { + return clamp((white * color + color) / (color * white + white), vec3(0.0f), vec3(1.0f)); } -vec3 linear_to_srgb(vec3 color) // convert linear rgb to srgb, assumes clamped input in range [0;1] -{ +vec3 linear_to_srgb(vec3 color) { // convert linear rgb to srgb, assumes clamped input in range [0;1] const vec3 a = vec3(0.055f); return mix((vec3(1.0f) + a) * pow(color.rgb, vec3(1.0f / 2.4f)) - a, 12.92f * color.rgb, lessThan(color.rgb, vec3(0.0031308f))); } -vec3 apply_tonemapping(vec3 color, float white) // inputs are LINEAR, always outputs clamped [0;1] color -{ - #ifdef USE_REINDHART_TONEMAPPER - return tonemap_reindhart(color, white); - #endif +vec3 apply_tonemapping(vec3 color, float white) { // inputs are LINEAR, always outputs clamped [0;1] color +#ifdef USE_REINHARD_TONEMAPPER + return tonemap_reinhard(color, white); +#endif - #ifdef USE_FILMIC_TONEMAPPER - return tonemap_filmic(color, white); - #endif +#ifdef USE_FILMIC_TONEMAPPER + return tonemap_filmic(color, white); +#endif - #ifdef USE_ACES_TONEMAPPER - return tonemap_aces(color, white); - #endif +#ifdef USE_ACES_TONEMAPPER + return tonemap_aces(color, white); +#endif - return clamp(color, vec3(0.0f), vec3(1.0f)); // no other seleced -> linear + return clamp(color, vec3(0.0f), vec3(1.0f)); // no other selected -> linear } -vec3 gather_glow(sampler2D tex, vec2 uv) // sample all selected glow levels -{ +vec3 gather_glow(sampler2D tex, vec2 uv) { // sample all selected glow levels vec3 glow = vec3(0.0f); - #ifdef USE_GLOW_LEVEL1 - glow += GLOW_TEXTURE_SAMPLE(tex, uv, 1).rgb; - #endif +#ifdef USE_GLOW_LEVEL1 + glow += GLOW_TEXTURE_SAMPLE(tex, uv, 1).rgb; +#endif - #ifdef USE_GLOW_LEVEL2 - glow += GLOW_TEXTURE_SAMPLE(tex, uv, 2).rgb; - #endif +#ifdef USE_GLOW_LEVEL2 + glow += GLOW_TEXTURE_SAMPLE(tex, uv, 2).rgb; +#endif - #ifdef USE_GLOW_LEVEL3 - glow += GLOW_TEXTURE_SAMPLE(tex, uv, 3).rgb; - #endif +#ifdef USE_GLOW_LEVEL3 + glow += GLOW_TEXTURE_SAMPLE(tex, uv, 3).rgb; +#endif - #ifdef USE_GLOW_LEVEL4 - glow += GLOW_TEXTURE_SAMPLE(tex, uv, 4).rgb; - #endif +#ifdef USE_GLOW_LEVEL4 + glow += GLOW_TEXTURE_SAMPLE(tex, uv, 4).rgb; +#endif - #ifdef USE_GLOW_LEVEL5 - glow += GLOW_TEXTURE_SAMPLE(tex, uv, 5).rgb; - #endif +#ifdef USE_GLOW_LEVEL5 + glow += GLOW_TEXTURE_SAMPLE(tex, uv, 5).rgb; +#endif - #ifdef USE_GLOW_LEVEL6 - glow += GLOW_TEXTURE_SAMPLE(tex, uv, 6).rgb; - #endif +#ifdef USE_GLOW_LEVEL6 + glow += GLOW_TEXTURE_SAMPLE(tex, uv, 6).rgb; +#endif - #ifdef USE_GLOW_LEVEL7 - glow += GLOW_TEXTURE_SAMPLE(tex, uv, 7).rgb; - #endif +#ifdef USE_GLOW_LEVEL7 + glow += GLOW_TEXTURE_SAMPLE(tex, uv, 7).rgb; +#endif return glow; } -vec3 apply_glow(vec3 color, vec3 glow) // apply glow using the selected blending mode -{ - #ifdef USE_GLOW_REPLACE - color = glow; - #endif +vec3 apply_glow(vec3 color, vec3 glow) { // apply glow using the selected blending mode +#ifdef USE_GLOW_REPLACE + color = glow; +#endif - #ifdef USE_GLOW_SCREEN - color = max((color + glow) - (color * glow), vec3(0.0)); - #endif +#ifdef USE_GLOW_SCREEN + color = max((color + glow) - (color * glow), vec3(0.0)); +#endif - #ifdef USE_GLOW_SOFTLIGHT - glow = glow * vec3(0.5f) + vec3(0.5f); +#ifdef USE_GLOW_SOFTLIGHT + glow = glow * vec3(0.5f) + vec3(0.5f); - color.r = (glow.r <= 0.5f) ? (color.r - (1.0f - 2.0f * glow.r) * color.r * (1.0f - color.r)) : (((glow.r > 0.5f) && (color.r <= 0.25f)) ? (color.r + (2.0f * glow.r - 1.0f) * (4.0f * color.r * (4.0f * color.r + 1.0f) * (color.r - 1.0f) + 7.0f * color.r)) : (color.r + (2.0f * glow.r - 1.0f) * (sqrt(color.r) - color.r))); - color.g = (glow.g <= 0.5f) ? (color.g - (1.0f - 2.0f * glow.g) * color.g * (1.0f - color.g)) : (((glow.g > 0.5f) && (color.g <= 0.25f)) ? (color.g + (2.0f * glow.g - 1.0f) * (4.0f * color.g * (4.0f * color.g + 1.0f) * (color.g - 1.0f) + 7.0f * color.g)) : (color.g + (2.0f * glow.g - 1.0f) * (sqrt(color.g) - color.g))); - color.b = (glow.b <= 0.5f) ? (color.b - (1.0f - 2.0f * glow.b) * color.b * (1.0f - color.b)) : (((glow.b > 0.5f) && (color.b <= 0.25f)) ? (color.b + (2.0f * glow.b - 1.0f) * (4.0f * color.b * (4.0f * color.b + 1.0f) * (color.b - 1.0f) + 7.0f * color.b)) : (color.b + (2.0f * glow.b - 1.0f) * (sqrt(color.b) - color.b))); - #endif + color.r = (glow.r <= 0.5f) ? (color.r - (1.0f - 2.0f * glow.r) * color.r * (1.0f - color.r)) : (((glow.r > 0.5f) && (color.r <= 0.25f)) ? (color.r + (2.0f * glow.r - 1.0f) * (4.0f * color.r * (4.0f * color.r + 1.0f) * (color.r - 1.0f) + 7.0f * color.r)) : (color.r + (2.0f * glow.r - 1.0f) * (sqrt(color.r) - color.r))); + color.g = (glow.g <= 0.5f) ? (color.g - (1.0f - 2.0f * glow.g) * color.g * (1.0f - color.g)) : (((glow.g > 0.5f) && (color.g <= 0.25f)) ? (color.g + (2.0f * glow.g - 1.0f) * (4.0f * color.g * (4.0f * color.g + 1.0f) * (color.g - 1.0f) + 7.0f * color.g)) : (color.g + (2.0f * glow.g - 1.0f) * (sqrt(color.g) - color.g))); + color.b = (glow.b <= 0.5f) ? (color.b - (1.0f - 2.0f * glow.b) * color.b * (1.0f - color.b)) : (((glow.b > 0.5f) && (color.b <= 0.25f)) ? (color.b + (2.0f * glow.b - 1.0f) * (4.0f * color.b * (4.0f * color.b + 1.0f) * (color.b - 1.0f) + 7.0f * color.b)) : (color.b + (2.0f * glow.b - 1.0f) * (sqrt(color.b) - color.b))); +#endif - #if !defined(USE_GLOW_SCREEN) && !defined(USE_GLOW_SOFTLIGHT) && !defined(USE_GLOW_REPLACE) // no other selected -> additive - color += glow; - #endif +#if !defined(USE_GLOW_SCREEN) && !defined(USE_GLOW_SOFTLIGHT) && !defined(USE_GLOW_REPLACE) // no other selected -> additive + color += glow; +#endif return color; } -vec3 apply_bcs(vec3 color, vec3 bcs) -{ +vec3 apply_bcs(vec3 color, vec3 bcs) { color = mix(vec3(0.0f), color, bcs.x); color = mix(vec3(0.5f), color, bcs.y); color = mix(vec3(dot(vec3(1.0f), color) * 0.33333f), color, bcs.z); @@ -258,8 +246,7 @@ vec3 apply_bcs(vec3 color, vec3 bcs) return color; } -vec3 apply_color_correction(vec3 color, sampler2D correction_tex) -{ +vec3 apply_color_correction(vec3 color, sampler2D correction_tex) { color.r = texture(correction_tex, vec2(color.r, 0.0f)).r; color.g = texture(correction_tex, vec2(color.g, 0.0f)).g; color.b = texture(correction_tex, vec2(color.b, 0.0f)).b; @@ -267,15 +254,14 @@ vec3 apply_color_correction(vec3 color, sampler2D correction_tex) return color; } -void main() -{ +void main() { vec3 color = textureLod(source, uv_interp, 0.0f).rgb; // Exposure - #ifdef USE_AUTO_EXPOSURE - color /= texelFetch(source_auto_exposure, ivec2(0, 0), 0).r / auto_exposure_grey; - #endif +#ifdef USE_AUTO_EXPOSURE + color /= texelFetch(source_auto_exposure, ivec2(0, 0), 0).r / auto_exposure_grey; +#endif color *= exposure; @@ -283,33 +269,33 @@ void main() color = apply_tonemapping(color, white); - #ifdef KEEP_3D_LINEAR - // leave color as is (-> don't convert to SRGB) - #else - color = linear_to_srgb(color); // regular linear -> SRGB conversion - #endif +#ifdef KEEP_3D_LINEAR + // leave color as is (-> don't convert to SRGB) +#else + color = linear_to_srgb(color); // regular linear -> SRGB conversion +#endif // Glow - #ifdef USING_GLOW - vec3 glow = gather_glow(source_glow, uv_interp) * glow_intensity; +#ifdef USING_GLOW + vec3 glow = gather_glow(source_glow, uv_interp) * glow_intensity; - // high dynamic range -> SRGB - glow = apply_tonemapping(glow, white); - glow = linear_to_srgb(glow); + // high dynamic range -> SRGB + glow = apply_tonemapping(glow, white); + glow = linear_to_srgb(glow); - color = apply_glow(color, glow); - #endif + color = apply_glow(color, glow); +#endif // Additional effects - #ifdef USE_BCS - color = apply_bcs(color, bcs); - #endif +#ifdef USE_BCS + color = apply_bcs(color, bcs); +#endif - #ifdef USE_COLOR_CORRECTION - color = apply_color_correction(color, color_correction); - #endif +#ifdef USE_COLOR_CORRECTION + color = apply_color_correction(color, color_correction); +#endif frag_color = vec4(color, 1.0f); } diff --git a/drivers/png/SCsub b/drivers/png/SCsub index 39480351a6..22fb1817d1 100644 --- a/drivers/png/SCsub +++ b/drivers/png/SCsub @@ -26,14 +26,24 @@ if env['builtin_libpng']: ] thirdparty_sources = [thirdparty_dir + file for file in thirdparty_sources] - env_png.add_source_files(env.drivers_sources, thirdparty_sources) env_png.Append(CPPPATH=[thirdparty_dir]) + # Needed for drivers includes and in platform/javascript + env.Append(CPPPATH=[thirdparty_dir]) # Currently .ASM filter_neon.S does not compile on NT. import os - if ("neon_enabled" in env and env["neon_enabled"]) and os.name != "nt": + use_neon = "neon_enabled" in env and env["neon_enabled"] and os.name != "nt" + if use_neon: env_png.Append(CPPFLAGS=["-DPNG_ARM_NEON_OPT=2"]) - env_neon = env_png.Clone() + else: + env_png.Append(CPPFLAGS=["-DPNG_ARM_NEON_OPT=0"]) + + env_thirdparty = env_png.Clone() + env_thirdparty.disable_warnings() + env_thirdparty.add_source_files(env.drivers_sources, thirdparty_sources) + + if use_neon: + env_neon = env_thirdparty.Clone() if "S_compiler" in env: env_neon['CC'] = env['S_compiler'] neon_sources = [] @@ -41,8 +51,6 @@ if env['builtin_libpng']: neon_sources.append(env_neon.Object(thirdparty_dir + "/arm/filter_neon_intrinsics.c")) neon_sources.append(env_neon.Object(thirdparty_dir + "/arm/filter_neon.S")) env.drivers_sources += neon_sources - else: - env_png.Append(CPPFLAGS=["-DPNG_ARM_NEON_OPT=0"]) # Godot source files env_png.add_source_files(env.drivers_sources, "*.cpp") diff --git a/drivers/png/image_loader_png.cpp b/drivers/png/image_loader_png.cpp index 3f512af8d5..a4ea889d3b 100644 --- a/drivers/png/image_loader_png.cpp +++ b/drivers/png/image_loader_png.cpp @@ -30,8 +30,8 @@ #include "image_loader_png.h" -#include "os/os.h" -#include "print_string.h" +#include "core/os/os.h" +#include "core/print_string.h" #include <string.h> @@ -227,10 +227,7 @@ static void user_read_data(png_structp png_ptr, png_bytep data, png_size_t p_len PNGReadStatus *rstatus; rstatus = (PNGReadStatus *)png_get_io_ptr(png_ptr); - png_size_t to_read = p_length; - if (rstatus->size >= 0) { - to_read = MIN(p_length, rstatus->size - rstatus->offset); - } + png_size_t to_read = MIN(p_length, rstatus->size - rstatus->offset); memcpy(data, &rstatus->image[rstatus->offset], to_read); rstatus->offset += to_read; @@ -271,7 +268,6 @@ static void _write_png_data(png_structp png_ptr, png_bytep data, png_size_t p_le v.resize(vs + p_length); PoolVector<uint8_t>::Write w = v.write(); copymem(&w[vs], data, p_length); - //print_line("png write: "+itos(p_length)); } static PoolVector<uint8_t> _lossless_pack_png(const Ref<Image> &p_image) { diff --git a/drivers/png/image_loader_png.h b/drivers/png/image_loader_png.h index 48f48e6bea..5dff7e3902 100644 --- a/drivers/png/image_loader_png.h +++ b/drivers/png/image_loader_png.h @@ -31,7 +31,7 @@ #ifndef IMAGE_LOADER_PNG_H #define IMAGE_LOADER_PNG_H -#include "io/image_loader.h" +#include "core/io/image_loader.h" #include <png.h> diff --git a/drivers/png/resource_saver_png.cpp b/drivers/png/resource_saver_png.cpp index 270ae36e1e..c5729f70b2 100644 --- a/drivers/png/resource_saver_png.cpp +++ b/drivers/png/resource_saver_png.cpp @@ -31,8 +31,8 @@ #include "resource_saver_png.h" #include "core/image.h" -#include "os/file_access.h" -#include "project_settings.h" +#include "core/os/file_access.h" +#include "core/project_settings.h" #include "scene/resources/texture.h" #include <png.h> diff --git a/drivers/png/resource_saver_png.h b/drivers/png/resource_saver_png.h index 109b4801da..34950f6723 100644 --- a/drivers/png/resource_saver_png.h +++ b/drivers/png/resource_saver_png.h @@ -31,8 +31,8 @@ #ifndef RESOURCE_SAVER_PNG_H #define RESOURCE_SAVER_PNG_H -#include "image.h" -#include "io/resource_saver.h" +#include "core/image.h" +#include "core/io/resource_saver.h" class ResourceSaverPNG : public ResourceFormatSaver { public: diff --git a/drivers/pulseaudio/SCsub b/drivers/pulseaudio/SCsub index ee39fd2631..28b315ae66 100644 --- a/drivers/pulseaudio/SCsub +++ b/drivers/pulseaudio/SCsub @@ -3,5 +3,3 @@ Import('env') env.add_source_files(env.drivers_sources, "*.cpp") - -Export('env') diff --git a/drivers/pulseaudio/audio_driver_pulseaudio.cpp b/drivers/pulseaudio/audio_driver_pulseaudio.cpp index 987cd9c85f..4988557dcb 100644 --- a/drivers/pulseaudio/audio_driver_pulseaudio.cpp +++ b/drivers/pulseaudio/audio_driver_pulseaudio.cpp @@ -32,10 +32,8 @@ #ifdef PULSEAUDIO_ENABLED -#include <pulse/pulseaudio.h> - -#include "os/os.h" -#include "project_settings.h" +#include "core/os/os.h" +#include "core/project_settings.h" void AudioDriverPulseAudio::pa_state_cb(pa_context *c, void *userdata) { AudioDriverPulseAudio *ad = (AudioDriverPulseAudio *)userdata; @@ -45,10 +43,13 @@ void AudioDriverPulseAudio::pa_state_cb(pa_context *c, void *userdata) { case PA_CONTEXT_FAILED: ad->pa_ready = -1; break; - case PA_CONTEXT_READY: ad->pa_ready = 1; break; + default: + // TODO: Check if we want to handle some of the other + // PA context states like PA_CONTEXT_UNCONNECTED. + break; } } @@ -183,10 +184,8 @@ Error AudioDriverPulseAudio::init_device() { buffer_frames = closest_power_of_2(latency * mix_rate / 1000); pa_buffer_size = buffer_frames * pa_map.channels; - if (OS::get_singleton()->is_stdout_verbose()) { - print_line("PulseAudio: detected " + itos(pa_map.channels) + " channels"); - print_line("PulseAudio: audio buffer frames: " + itos(buffer_frames) + " calculated latency: " + itos(buffer_frames * 1000 / mix_rate) + "ms"); - } + print_verbose("PulseAudio: detected " + itos(pa_map.channels) + " channels"); + print_verbose("PulseAudio: audio buffer frames: " + itos(buffer_frames) + " calculated latency: " + itos(buffer_frames * 1000 / mix_rate) + "ms"); pa_sample_spec spec; spec.format = PA_SAMPLE_S16LE; @@ -344,12 +343,12 @@ void AudioDriverPulseAudio::thread_func(void *p_udata) { unsigned int out_idx = 0; for (unsigned int i = 0; i < ad->buffer_frames; i++) { - for (unsigned int j = 0; j < ad->pa_map.channels - 1; j++) { + for (int j = 0; j < ad->pa_map.channels - 1; j++) { ad->samples_out.write[out_idx++] = ad->samples_in[in_idx++] >> 16; } - uint32_t l = ad->samples_in[in_idx++]; - uint32_t r = ad->samples_in[in_idx++]; - ad->samples_out.write[out_idx++] = (l >> 1 + r >> 1) >> 16; + uint32_t l = ad->samples_in[in_idx++] >> 16; + uint32_t r = ad->samples_in[in_idx++] >> 16; + ad->samples_out.write[out_idx++] = (l + r) / 2; } } } @@ -373,9 +372,9 @@ void AudioDriverPulseAudio::thread_func(void *p_udata) { if (bytes > 0) { size_t bytes_to_write = MIN(bytes, avail_bytes); const void *ptr = ad->samples_out.ptr(); - ret = pa_stream_write(ad->pa_str, ptr + write_ofs, bytes_to_write, NULL, 0LL, PA_SEEK_RELATIVE); + ret = pa_stream_write(ad->pa_str, (char *)ptr + write_ofs, bytes_to_write, NULL, 0LL, PA_SEEK_RELATIVE); if (ret != 0) { - ERR_PRINT("pa_stream_write error"); + ERR_PRINTS("PulseAudio: pa_stream_write error: " + String(pa_strerror(ret))); } else { avail_bytes -= bytes_to_write; write_ofs += bytes_to_write; @@ -402,6 +401,9 @@ void AudioDriverPulseAudio::thread_func(void *p_udata) { break; } } + + avail_bytes = 0; + write_ofs = 0; } if (ad->pa_rec_str && pa_stream_get_state(ad->pa_rec_str) == PA_STREAM_READY) { @@ -614,22 +616,18 @@ Error AudioDriverPulseAudio::capture_init_device() { break; } - if (OS::get_singleton()->is_stdout_verbose()) { - print_line("PulseAudio: detected " + itos(pa_rec_map.channels) + " input channels"); - } - pa_sample_spec spec; spec.format = PA_SAMPLE_S16LE; spec.channels = pa_rec_map.channels; spec.rate = mix_rate; - int latency = 30; - input_buffer_frames = closest_power_of_2(latency * mix_rate / 1000); - int buffer_size = input_buffer_frames * spec.channels; + int input_latency = 30; + int input_buffer_frames = closest_power_of_2(input_latency * mix_rate / 1000); + int input_buffer_size = input_buffer_frames * spec.channels; pa_buffer_attr attr; - attr.fragsize = buffer_size * sizeof(int16_t); + attr.fragsize = input_buffer_size * sizeof(int16_t); pa_rec_str = pa_stream_new(pa_ctx, "Record", &spec, &pa_rec_map); if (pa_rec_str == NULL) { @@ -645,9 +643,10 @@ Error AudioDriverPulseAudio::capture_init_device() { ERR_FAIL_V(ERR_CANT_OPEN); } - input_buffer.resize(input_buffer_frames * 8); - input_position = 0; - input_size = 0; + input_buffer_init(input_buffer_frames); + + print_verbose("PulseAudio: detected " + itos(pa_rec_map.channels) + " input channels"); + print_verbose("PulseAudio: input buffer frames: " + itos(input_buffer_frames) + " calculated latency: " + itos(input_buffer_frames * 1000 / mix_rate) + "ms"); return OK; } @@ -744,36 +743,28 @@ String AudioDriverPulseAudio::capture_get_device() { return name; } -AudioDriverPulseAudio::AudioDriverPulseAudio() { - - pa_ml = NULL; - pa_ctx = NULL; - pa_str = NULL; - pa_rec_str = NULL; - - mutex = NULL; - thread = NULL; - - device_name = "Default"; - new_device = "Default"; - default_device = ""; - +AudioDriverPulseAudio::AudioDriverPulseAudio() : + thread(NULL), + mutex(NULL), + pa_ml(NULL), + pa_ctx(NULL), + pa_str(NULL), + pa_rec_str(NULL), + device_name("Default"), + new_device("Default"), + default_device(""), + mix_rate(0), + buffer_frames(0), + pa_buffer_size(0), + channels(0), + pa_ready(0), + pa_status(0), + active(false), + thread_exited(false), + exit_thread(false), + latency(0) { samples_in.clear(); samples_out.clear(); - - mix_rate = 0; - buffer_frames = 0; - input_buffer_frames = 0; - pa_buffer_size = 0; - channels = 0; - pa_ready = 0; - pa_status = 0; - - active = false; - thread_exited = false; - exit_thread = false; - - latency = 0; } AudioDriverPulseAudio::~AudioDriverPulseAudio() { diff --git a/drivers/pulseaudio/audio_driver_pulseaudio.h b/drivers/pulseaudio/audio_driver_pulseaudio.h index f8358a452b..d8bab841ff 100644 --- a/drivers/pulseaudio/audio_driver_pulseaudio.h +++ b/drivers/pulseaudio/audio_driver_pulseaudio.h @@ -64,7 +64,6 @@ class AudioDriverPulseAudio : public AudioDriver { unsigned int mix_rate; unsigned int buffer_frames; - unsigned int input_buffer_frames; unsigned int pa_buffer_size; int channels; int pa_ready; diff --git a/drivers/register_driver_types.cpp b/drivers/register_driver_types.cpp index c6d36a5749..9f5d9c1abf 100644 --- a/drivers/register_driver_types.cpp +++ b/drivers/register_driver_types.cpp @@ -31,11 +31,11 @@ #include "register_driver_types.h" #include "core/math/geometry.h" -#include "png/image_loader_png.h" -#include "png/resource_saver_png.h" +#include "drivers/png/image_loader_png.h" +#include "drivers/png/resource_saver_png.h" #ifdef TOOLS_ENABLED -#include "convex_decomp/b2d_decompose.h" +#include "drivers/convex_decomp/b2d_decompose.h" #endif #ifdef TOOLS_ENABLED diff --git a/drivers/rtaudio/SCsub b/drivers/rtaudio/SCsub index 2b0a602965..285658073c 100644 --- a/drivers/rtaudio/SCsub +++ b/drivers/rtaudio/SCsub @@ -11,9 +11,12 @@ thirdparty_sources = [ ] thirdparty_sources = [thirdparty_dir + file for file in thirdparty_sources] -env.add_source_files(env.drivers_sources, thirdparty_sources) env.Append(CPPPATH=[thirdparty_dir]) +env_thirdparty = env.Clone() +env_thirdparty.disable_warnings() +env_thirdparty.add_source_files(env.drivers_sources, thirdparty_sources) + # Driver source files env.add_source_files(env.drivers_sources, "*.cpp") diff --git a/drivers/rtaudio/audio_driver_rtaudio.cpp b/drivers/rtaudio/audio_driver_rtaudio.cpp index 365788e192..80537315af 100644 --- a/drivers/rtaudio/audio_driver_rtaudio.cpp +++ b/drivers/rtaudio/audio_driver_rtaudio.cpp @@ -30,8 +30,8 @@ #include "audio_driver_rtaudio.h" -#include "os/os.h" -#include "project_settings.h" +#include "core/os/os.h" +#include "core/project_settings.h" #ifdef RTAUDIO_ENABLED @@ -112,16 +112,14 @@ Error AudioDriverRtAudio::init() { int latency = GLOBAL_DEF("audio/output_latency", DEFAULT_OUTPUT_LATENCY); unsigned int buffer_frames = closest_power_of_2(latency * mix_rate / 1000); + print_verbose("Audio buffer frames: " + itos(buffer_frames) + " calculated latency: " + itos(buffer_frames * 1000 / mix_rate) + "ms"); - if (OS::get_singleton()->is_stdout_verbose()) { - print_line("audio buffer frames: " + itos(buffer_frames) + " calculated latency: " + itos(buffer_frames * 1000 / mix_rate) + "ms"); - } - - short int tries = 2; + short int tries = 4; - while (tries >= 0) { + while (tries > 0) { switch (speaker_mode) { case SPEAKER_MODE_STEREO: parameters.nChannels = 2; break; + case SPEAKER_SURROUND_31: parameters.nChannels = 4; break; case SPEAKER_SURROUND_51: parameters.nChannels = 6; break; case SPEAKER_SURROUND_71: parameters.nChannels = 8; break; }; @@ -131,12 +129,14 @@ Error AudioDriverRtAudio::init() { active = true; break; - } catch (RtAudioError &e) { + } catch (RtAudioError) { // try with less channels ERR_PRINT("Unable to open audio, retrying with fewer channels..."); switch (speaker_mode) { - case SPEAKER_SURROUND_51: speaker_mode = SPEAKER_MODE_STEREO; break; + case SPEAKER_MODE_STEREO: break; // Required to silence unhandled enum value warning. + case SPEAKER_SURROUND_31: speaker_mode = SPEAKER_MODE_STEREO; break; + case SPEAKER_SURROUND_51: speaker_mode = SPEAKER_SURROUND_31; break; case SPEAKER_SURROUND_71: speaker_mode = SPEAKER_SURROUND_51; break; } @@ -194,13 +194,12 @@ void AudioDriverRtAudio::finish() { } } -AudioDriverRtAudio::AudioDriverRtAudio() { - - active = false; - mutex = NULL; - dac = NULL; - mix_rate = DEFAULT_MIX_RATE; - speaker_mode = SPEAKER_MODE_STEREO; +AudioDriverRtAudio::AudioDriverRtAudio() : + speaker_mode(SPEAKER_MODE_STEREO), + mutex(NULL), + dac(NULL), + mix_rate(DEFAULT_MIX_RATE), + active(false) { } #endif diff --git a/drivers/unix/SCsub b/drivers/unix/SCsub index ada8255580..4888f56099 100644 --- a/drivers/unix/SCsub +++ b/drivers/unix/SCsub @@ -5,5 +5,3 @@ Import('env') env.add_source_files(env.drivers_sources, "*.cpp") env["check_c_headers"] = [ [ "mntent.h", "HAVE_MNTENT" ] ] - -Export('env') diff --git a/drivers/unix/dir_access_unix.cpp b/drivers/unix/dir_access_unix.cpp index 5a4be6df4f..bea249d4b6 100644 --- a/drivers/unix/dir_access_unix.cpp +++ b/drivers/unix/dir_access_unix.cpp @@ -32,18 +32,19 @@ #if defined(UNIX_ENABLED) || defined(LIBC_FILEIO_ENABLED) -#ifndef ANDROID_ENABLED -#include <sys/statvfs.h> -#endif - #include "core/list.h" -#include "os/memory.h" -#include "print_string.h" +#include "core/os/memory.h" +#include "core/print_string.h" + #include <errno.h> #include <stdio.h> #include <stdlib.h> #include <string.h> +#ifndef ANDROID_ENABLED +#include <sys/statvfs.h> +#endif + #ifdef HAVE_MNTENT #include <mntent.h> #endif @@ -59,7 +60,7 @@ Error DirAccessUnix::list_dir_begin() { //char real_current_dir_name[2048]; //is this enough?! //getcwd(real_current_dir_name,2048); - //chdir(curent_path.utf8().get_data()); + //chdir(current_path.utf8().get_data()); dir_stream = opendir(current_dir.utf8().get_data()); //chdir(real_current_dir_name); if (!dir_stream) @@ -308,7 +309,7 @@ Error DirAccessUnix::change_dir(String p_dir) { // prev_dir is the directory we are changing out of String prev_dir; char real_current_dir_name[2048]; - getcwd(real_current_dir_name, 2048); + ERR_FAIL_COND_V(getcwd(real_current_dir_name, 2048) == NULL, ERR_BUG); if (prev_dir.parse_utf8(real_current_dir_name)) prev_dir = real_current_dir_name; //no utf8, maybe latin? @@ -327,9 +328,20 @@ Error DirAccessUnix::change_dir(String p_dir) { return ERR_INVALID_PARAMETER; } + String base = _get_root_path(); + if (base != String() && !try_dir.begins_with(base)) { + ERR_FAIL_COND_V(getcwd(real_current_dir_name, 2048) == NULL, ERR_BUG); + String new_dir; + new_dir.parse_utf8(real_current_dir_name); + + if (!new_dir.begins_with(base)) { + try_dir = current_dir; //revert + } + } + // the directory exists, so set current_dir to try_dir current_dir = try_dir; - chdir(prev_dir.utf8().get_data()); + ERR_FAIL_COND_V(chdir(prev_dir.utf8().get_data()) != 0, ERR_BUG); return OK; } @@ -390,7 +402,7 @@ size_t DirAccessUnix::get_space_left() { return vfs.f_bfree * vfs.f_bsize; #else -#warning THIS IS BROKEN + // FIXME: Implement this. return 0; #endif }; @@ -404,7 +416,7 @@ DirAccessUnix::DirAccessUnix() { // set current directory to an absolute path of the current directory char real_current_dir_name[2048]; - getcwd(real_current_dir_name, 2048); + ERR_FAIL_COND(getcwd(real_current_dir_name, 2048) == NULL); if (current_dir.parse_utf8(real_current_dir_name)) current_dir = real_current_dir_name; diff --git a/drivers/unix/dir_access_unix.h b/drivers/unix/dir_access_unix.h index a55acdbd34..26978930bd 100644 --- a/drivers/unix/dir_access_unix.h +++ b/drivers/unix/dir_access_unix.h @@ -33,16 +33,17 @@ #if defined(UNIX_ENABLED) || defined(LIBC_FILEIO_ENABLED) +#include "core/os/dir_access.h" + #include <dirent.h> #include <sys/stat.h> #include <sys/types.h> #include <unistd.h> -#include "os/dir_access.h" - /** @author Juan Linietsky <reduzio@gmail.com> */ + class DirAccessUnix : public DirAccess { DIR *dir_stream; diff --git a/drivers/unix/file_access_unix.cpp b/drivers/unix/file_access_unix.cpp index c25d34125d..10f6be3cbe 100644 --- a/drivers/unix/file_access_unix.cpp +++ b/drivers/unix/file_access_unix.cpp @@ -33,7 +33,8 @@ #if defined(UNIX_ENABLED) || defined(LIBC_FILEIO_ENABLED) #include "core/os/os.h" -#include "print_string.h" +#include "core/print_string.h" + #include <sys/stat.h> #include <sys/types.h> @@ -106,7 +107,6 @@ Error FileAccessUnix::_open(const String &p_path, int p_mode_flags) { if (is_backup_save_enabled() && (p_mode_flags & WRITE) && !(p_mode_flags & READ)) { save_path = path; path = path + ".tmp"; - //print_line("saving instead to "+path); } f = fopen(path.utf8().get_data(), mode_string); @@ -134,9 +134,6 @@ void FileAccessUnix::close() { } if (save_path != "") { - - //unlink(save_path.utf8().get_data()); - //print_line("renaming..."); int rename_error = rename((save_path + ".tmp").utf8().get_data(), save_path.utf8().get_data()); if (rename_error && close_fail_notify) { @@ -291,8 +288,7 @@ uint64_t FileAccessUnix::_get_modified_time(const String &p_file) { if (!err) { return flags.st_mtime; } else { - print_line("ERROR IN: " + p_file); - + ERR_EXPLAIN("Failed to get modified time for: " + p_file); ERR_FAIL_V(0); }; } @@ -313,11 +309,10 @@ FileAccess *FileAccessUnix::create_libc() { CloseNotificationFunc FileAccessUnix::close_notification_func = NULL; -FileAccessUnix::FileAccessUnix() { - - f = NULL; - flags = 0; - last_error = OK; +FileAccessUnix::FileAccessUnix() : + f(NULL), + flags(0), + last_error(OK) { } FileAccessUnix::~FileAccessUnix() { diff --git a/drivers/unix/file_access_unix.h b/drivers/unix/file_access_unix.h index 88bb39fbd1..d4a4f8230c 100644 --- a/drivers/unix/file_access_unix.h +++ b/drivers/unix/file_access_unix.h @@ -31,8 +31,9 @@ #ifndef FILE_ACCESS_UNIX_H #define FILE_ACCESS_UNIX_H -#include "os/file_access.h" -#include "os/memory.h" +#include "core/os/file_access.h" +#include "core/os/memory.h" + #include <stdio.h> #if defined(UNIX_ENABLED) || defined(LIBC_FILEIO_ENABLED) diff --git a/drivers/unix/ip_unix.h b/drivers/unix/ip_unix.h index d3d1ccfa85..83535045b1 100644 --- a/drivers/unix/ip_unix.h +++ b/drivers/unix/ip_unix.h @@ -31,7 +31,7 @@ #ifndef IP_UNIX_H #define IP_UNIX_H -#include "io/ip.h" +#include "core/io/ip.h" #if defined(UNIX_ENABLED) || defined(WINDOWS_ENABLED) diff --git a/drivers/unix/mutex_posix.cpp b/drivers/unix/mutex_posix.cpp index 1f13720f1e..e0004c5730 100644 --- a/drivers/unix/mutex_posix.cpp +++ b/drivers/unix/mutex_posix.cpp @@ -29,7 +29,8 @@ /*************************************************************************/ #include "mutex_posix.h" -#include "os/memory.h" + +#include "core/os/memory.h" #if defined(UNIX_ENABLED) || defined(PTHREAD_ENABLED) diff --git a/drivers/unix/mutex_posix.h b/drivers/unix/mutex_posix.h index a4de1603f3..80d85eee61 100644 --- a/drivers/unix/mutex_posix.h +++ b/drivers/unix/mutex_posix.h @@ -33,7 +33,8 @@ #if defined(UNIX_ENABLED) || defined(PTHREAD_ENABLED) -#include "os/mutex.h" +#include "core/os/mutex.h" + #include <pthread.h> class MutexPosix : public Mutex { diff --git a/drivers/unix/net_socket_posix.cpp b/drivers/unix/net_socket_posix.cpp new file mode 100644 index 0000000000..740b1edb9c --- /dev/null +++ b/drivers/unix/net_socket_posix.cpp @@ -0,0 +1,616 @@ +/*************************************************************************/ +/* net_socket_posix.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#include "net_socket_posix.h" + +#if defined(UNIX_ENABLED) + +#include <errno.h> +#include <netdb.h> +#include <poll.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/ioctl.h> +#include <sys/types.h> +#include <unistd.h> +#ifndef NO_FCNTL +#include <fcntl.h> +#else +#include <sys/ioctl.h> +#endif +#include <netinet/in.h> + +#include <sys/socket.h> +#ifdef JAVASCRIPT_ENABLED +#include <arpa/inet.h> +#endif + +#include <netinet/tcp.h> + +#if defined(__APPLE__) +#define MSG_NOSIGNAL SO_NOSIGPIPE +#endif + +// Some custom defines to minimize ifdefs +#define SOCK_EMPTY -1 +#define SOCK_BUF(x) x +#define SOCK_CBUF(x) x +#define SOCK_IOCTL ioctl +#define SOCK_CLOSE ::close + +/* Windows */ +#elif defined(WINDOWS_ENABLED) +#include <winsock2.h> +#include <ws2tcpip.h> + +#include <mswsock.h> +// Some custom defines to minimize ifdefs +#define SOCK_EMPTY INVALID_SOCKET +#define SOCK_BUF(x) (char *)(x) +#define SOCK_CBUF(x) (const char *)(x) +#define SOCK_IOCTL ioctlsocket +#define SOCK_CLOSE closesocket + +// Windows doesn't have this flag +#ifndef MSG_NOSIGNAL +#define MSG_NOSIGNAL 0 +#endif +// Workaround missing flag in MinGW +#if defined(__MINGW32__) && !defined(SIO_UDP_NETRESET) +#define SIO_UDP_NETRESET _WSAIOW(IOC_VENDOR, 15) +#endif + +#endif + +size_t NetSocketPosix::_set_addr_storage(struct sockaddr_storage *p_addr, const IP_Address &p_ip, uint16_t p_port, IP::Type p_ip_type) { + + memset(p_addr, 0, sizeof(struct sockaddr_storage)); + if (p_ip_type == IP::TYPE_IPV6 || p_ip_type == IP::TYPE_ANY) { // IPv6 socket + + // IPv6 only socket with IPv4 address + ERR_FAIL_COND_V(!p_ip.is_wildcard() && p_ip_type == IP::TYPE_IPV6 && p_ip.is_ipv4(), 0); + + struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)p_addr; + addr6->sin6_family = AF_INET6; + addr6->sin6_port = htons(p_port); + if (p_ip.is_valid()) { + copymem(&addr6->sin6_addr.s6_addr, p_ip.get_ipv6(), 16); + } else { + addr6->sin6_addr = in6addr_any; + } + return sizeof(sockaddr_in6); + } else { // IPv4 socket + + // IPv4 socket with IPv6 address + ERR_FAIL_COND_V(!p_ip.is_wildcard() && !p_ip.is_ipv4(), 0); + + struct sockaddr_in *addr4 = (struct sockaddr_in *)p_addr; + addr4->sin_family = AF_INET; + addr4->sin_port = htons(p_port); // short, network byte order + + if (p_ip.is_valid()) { + copymem(&addr4->sin_addr.s_addr, p_ip.get_ipv4(), 4); + } else { + addr4->sin_addr.s_addr = INADDR_ANY; + } + + return sizeof(sockaddr_in); + } +} + +void NetSocketPosix::_set_ip_port(struct sockaddr_storage *p_addr, IP_Address &r_ip, uint16_t &r_port) { + + if (p_addr->ss_family == AF_INET) { + + struct sockaddr_in *addr4 = (struct sockaddr_in *)p_addr; + r_ip.set_ipv4((uint8_t *)&(addr4->sin_addr.s_addr)); + + r_port = ntohs(addr4->sin_port); + + } else if (p_addr->ss_family == AF_INET6) { + + struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)p_addr; + r_ip.set_ipv6(addr6->sin6_addr.s6_addr); + + r_port = ntohs(addr6->sin6_port); + }; +} + +NetSocket *NetSocketPosix::_create_func() { + return memnew(NetSocketPosix); +} + +void NetSocketPosix::make_default() { +#if defined(WINDOWS_ENABLED) + if (_create == NULL) { + WSADATA data; + WSAStartup(MAKEWORD(2, 2), &data); + } +#endif + _create = _create_func; +} + +void NetSocketPosix::cleanup() { +#if defined(WINDOWS_ENABLED) + if (_create != NULL) { + WSACleanup(); + } + _create = NULL; +#endif +} + +NetSocketPosix::NetSocketPosix() : + _sock(SOCK_EMPTY), + _ip_type(IP::TYPE_NONE), + _is_stream(false) { +} + +NetSocketPosix::~NetSocketPosix() { + close(); +} + +NetSocketPosix::NetError NetSocketPosix::_get_socket_error() { +#if defined(WINDOWS_ENABLED) + int err = WSAGetLastError(); + + if (err == WSAEISCONN) + return ERR_NET_IS_CONNECTED; + if (err == WSAEINPROGRESS || err == WSAEALREADY) + return ERR_NET_IN_PROGRESS; + if (err == WSAEWOULDBLOCK) + return ERR_NET_WOULD_BLOCK; + ERR_PRINTS("Socket error: " + itos(err)); + return ERR_NET_OTHER; +#else + if (errno == EISCONN) + return ERR_NET_IS_CONNECTED; + if (errno == EINPROGRESS || errno == EALREADY) + return ERR_NET_IN_PROGRESS; + if (errno == EAGAIN || errno == EWOULDBLOCK) + return ERR_NET_WOULD_BLOCK; + ERR_PRINTS("Socket error: " + itos(errno)); + return ERR_NET_OTHER; +#endif +} + +bool NetSocketPosix::_can_use_ip(const IP_Address p_ip, const bool p_for_bind) const { + + if (p_for_bind && !(p_ip.is_valid() || p_ip.is_wildcard())) { + return false; + } else if (!p_for_bind && !p_ip.is_valid()) { + return false; + } + // Check if socket support this IP type. + IP::Type type = p_ip.is_ipv4() ? IP::TYPE_IPV4 : IP::TYPE_IPV6; + if (_ip_type != IP::TYPE_ANY && !p_ip.is_wildcard() && _ip_type != type) { + return false; + } + return true; +} + +void NetSocketPosix::_set_socket(SOCKET_TYPE p_sock, IP::Type p_ip_type, bool p_is_stream) { + _sock = p_sock; + _ip_type = p_ip_type; + _is_stream = p_is_stream; +} + +Error NetSocketPosix::open(Type p_sock_type, IP::Type &ip_type) { + ERR_FAIL_COND_V(is_open(), ERR_ALREADY_IN_USE); + ERR_FAIL_COND_V(ip_type > IP::TYPE_ANY || ip_type < IP::TYPE_NONE, ERR_INVALID_PARAMETER); + +#if defined(__OpenBSD__) + // OpenBSD does not support dual stacking, fallback to IPv4 only. + if (ip_type == IP::TYPE_ANY) + ip_type = IP::TYPE_IPV4; +#endif + + int family = ip_type == IP::TYPE_IPV4 ? AF_INET : AF_INET6; + int protocol = p_sock_type == TYPE_TCP ? IPPROTO_TCP : IPPROTO_UDP; + int type = p_sock_type == TYPE_TCP ? SOCK_STREAM : SOCK_DGRAM; + _sock = socket(family, type, protocol); + + if (_sock == SOCK_EMPTY && ip_type == IP::TYPE_ANY) { + // Careful here, changing the referenced parameter so the caller knows that we are using an IPv4 socket + // in place of a dual stack one, and further calls to _set_sock_addr will work as expected. + ip_type = IP::TYPE_IPV4; + family = AF_INET; + _sock = socket(family, type, protocol); + } + + ERR_FAIL_COND_V(_sock == SOCK_EMPTY, FAILED); + _ip_type = ip_type; + + if (family == AF_INET6) { + // Select IPv4 over IPv6 mapping + set_ipv6_only_enabled(ip_type != IP::TYPE_ANY); + } + + if (protocol == IPPROTO_UDP && ip_type != IP::TYPE_IPV6) { + // Enable broadcasting for UDP sockets if it's not IPv6 only (IPv6 has no broadcast option). + set_broadcasting_enabled(true); + } + + _is_stream = p_sock_type == TYPE_TCP; + +#if defined(WINDOWS_ENABLED) + if (!_is_stream) { + // Disable windows feature/bug reporting WSAECONNRESET/WSAENETRESET when + // recv/recvfrom and an ICMP reply was received from a previous send/sendto. + unsigned long disable = 0; + if (ioctlsocket(_sock, SIO_UDP_CONNRESET, &disable) == SOCKET_ERROR) { + print_verbose("Unable to turn off UDP WSAECONNRESET behaviour on Windows"); + } + if (ioctlsocket(_sock, SIO_UDP_NETRESET, &disable) == SOCKET_ERROR) { + // This feature seems not to be supported on wine. + print_verbose("Unable to turn off UDP WSAENETRESET behaviour on Windows"); + } + } +#endif + return OK; +} + +void NetSocketPosix::close() { + + if (_sock != SOCK_EMPTY) + SOCK_CLOSE(_sock); + + _sock = SOCK_EMPTY; + _ip_type = IP::TYPE_NONE; + _is_stream = false; +} + +Error NetSocketPosix::bind(IP_Address p_addr, uint16_t p_port) { + + ERR_FAIL_COND_V(!is_open(), ERR_UNCONFIGURED); + ERR_FAIL_COND_V(!_can_use_ip(p_addr, true), ERR_INVALID_PARAMETER); + + sockaddr_storage addr; + size_t addr_size = _set_addr_storage(&addr, p_addr, p_port, _ip_type); + + if (::bind(_sock, (struct sockaddr *)&addr, addr_size) == SOCK_EMPTY) { + close(); + ERR_FAIL_V(ERR_UNAVAILABLE); + } + + return OK; +} + +Error NetSocketPosix::listen(int p_max_pending) { + ERR_FAIL_COND_V(!is_open(), ERR_UNCONFIGURED); + + if (::listen(_sock, p_max_pending) == SOCK_EMPTY) { + + close(); + ERR_FAIL_V(FAILED); + }; + + return OK; +} + +Error NetSocketPosix::connect_to_host(IP_Address p_host, uint16_t p_port) { + + ERR_FAIL_COND_V(!is_open(), ERR_UNCONFIGURED); + ERR_FAIL_COND_V(!_can_use_ip(p_host, false), ERR_INVALID_PARAMETER); + + struct sockaddr_storage addr; + size_t addr_size = _set_addr_storage(&addr, p_host, p_port, _ip_type); + + if (::connect(_sock, (struct sockaddr *)&addr, addr_size) == SOCK_EMPTY) { + + NetError err = _get_socket_error(); + + switch (err) { + // We are already connected + case ERR_NET_IS_CONNECTED: + return OK; + // Still waiting to connect, try again in a while + case ERR_NET_WOULD_BLOCK: + case ERR_NET_IN_PROGRESS: + return ERR_BUSY; + default: + ERR_PRINT("Connection to remote host failed!"); + close(); + return FAILED; + } + } + + return OK; +} + +Error NetSocketPosix::poll(PollType p_type, int p_timeout) const { + + ERR_FAIL_COND_V(!is_open(), ERR_UNCONFIGURED); + +#if defined(WINDOWS_ENABLED) + bool ready = false; + fd_set rd, wr, ex; + fd_set *rdp = NULL; + fd_set *wrp = NULL; + FD_ZERO(&rd); + FD_ZERO(&wr); + FD_ZERO(&ex); + FD_SET(_sock, &ex); + struct timeval timeout = { p_timeout, 0 }; + // For blocking operation, pass NULL timeout pointer to select. + struct timeval *tp = NULL; + if (p_timeout >= 0) { + // If timeout is non-negative, we want to specify the timeout instead. + tp = &timeout; + } + + switch (p_type) { + case POLL_TYPE_IN: + FD_SET(_sock, &rd); + rdp = &rd; + break; + case POLL_TYPE_OUT: + FD_SET(_sock, &wr); + wrp = ≀ + break; + case POLL_TYPE_IN_OUT: + FD_SET(_sock, &rd); + FD_SET(_sock, &wr); + rdp = &rd; + wrp = ≀ + } + int ret = select(1, rdp, wrp, &ex, tp); + + ERR_FAIL_COND_V(ret == SOCKET_ERROR, FAILED); + + if (ret == 0) + return ERR_BUSY; + + ERR_FAIL_COND_V(FD_ISSET(_sock, &ex), FAILED); + + if (rdp && FD_ISSET(_sock, rdp)) + ready = true; + if (wrp && FD_ISSET(_sock, wrp)) + ready = true; + + return ready ? OK : ERR_BUSY; +#else + struct pollfd pfd; + pfd.fd = _sock; + pfd.events = POLLIN; + pfd.revents = 0; + + switch (p_type) { + case POLL_TYPE_IN: + pfd.events = POLLIN; + break; + case POLL_TYPE_OUT: + pfd.events = POLLOUT; + break; + case POLL_TYPE_IN_OUT: + pfd.events = POLLOUT || POLLIN; + } + + int ret = ::poll(&pfd, 1, p_timeout); + + ERR_FAIL_COND_V(ret < 0, FAILED); + ERR_FAIL_COND_V(pfd.revents & POLLERR, FAILED); + + if (ret == 0) + return ERR_BUSY; + + return OK; +#endif +} + +Error NetSocketPosix::recv(uint8_t *p_buffer, int p_len, int &r_read) { + ERR_FAIL_COND_V(!is_open(), ERR_UNCONFIGURED); + + r_read = ::recv(_sock, SOCK_BUF(p_buffer), p_len, 0); + + if (r_read < 0) { + NetError err = _get_socket_error(); + if (err == ERR_NET_WOULD_BLOCK) + return ERR_BUSY; + + return FAILED; + } + + return OK; +} + +Error NetSocketPosix::recvfrom(uint8_t *p_buffer, int p_len, int &r_read, IP_Address &r_ip, uint16_t &r_port) { + ERR_FAIL_COND_V(!is_open(), ERR_UNCONFIGURED); + + struct sockaddr_storage from; + socklen_t len = sizeof(struct sockaddr_storage); + memset(&from, 0, len); + + r_read = ::recvfrom(_sock, SOCK_BUF(p_buffer), p_len, 0, (struct sockaddr *)&from, &len); + + if (r_read < 0) { + NetError err = _get_socket_error(); + if (err == ERR_NET_WOULD_BLOCK) + return ERR_BUSY; + + return FAILED; + } + + if (from.ss_family == AF_INET) { + struct sockaddr_in *sin_from = (struct sockaddr_in *)&from; + r_ip.set_ipv4((uint8_t *)&sin_from->sin_addr); + r_port = ntohs(sin_from->sin_port); + } else if (from.ss_family == AF_INET6) { + struct sockaddr_in6 *s6_from = (struct sockaddr_in6 *)&from; + r_ip.set_ipv6((uint8_t *)&s6_from->sin6_addr); + r_port = ntohs(s6_from->sin6_port); + } else { + // Unsupported socket family, should never happen. + ERR_FAIL_V(FAILED); + } + + return OK; +} + +Error NetSocketPosix::send(const uint8_t *p_buffer, int p_len, int &r_sent) { + ERR_FAIL_COND_V(!is_open(), ERR_UNCONFIGURED); + + int flags = 0; + if (_is_stream) + flags = MSG_NOSIGNAL; + r_sent = ::send(_sock, SOCK_CBUF(p_buffer), p_len, flags); + + if (r_sent < 0) { + NetError err = _get_socket_error(); + if (err == ERR_NET_WOULD_BLOCK) + return ERR_BUSY; + + return FAILED; + } + + return OK; +} + +Error NetSocketPosix::sendto(const uint8_t *p_buffer, int p_len, int &r_sent, IP_Address p_ip, uint16_t p_port) { + ERR_FAIL_COND_V(!is_open(), ERR_UNCONFIGURED); + + struct sockaddr_storage addr; + size_t addr_size = _set_addr_storage(&addr, p_ip, p_port, _ip_type); + r_sent = ::sendto(_sock, SOCK_CBUF(p_buffer), p_len, 0, (struct sockaddr *)&addr, addr_size); + + if (r_sent < 0) { + NetError err = _get_socket_error(); + if (err == ERR_NET_WOULD_BLOCK) + return ERR_BUSY; + + return FAILED; + } + + return OK; +} + +void NetSocketPosix::set_broadcasting_enabled(bool p_enabled) { + ERR_FAIL_COND(!is_open()); + // IPv6 has no broadcast support. + ERR_FAIL_COND(_ip_type == IP::TYPE_IPV6); + + int par = p_enabled ? 1 : 0; + if (setsockopt(_sock, SOL_SOCKET, SO_BROADCAST, SOCK_CBUF(&par), sizeof(int)) != 0) { + WARN_PRINT("Unable to change broadcast setting"); + } +} + +void NetSocketPosix::set_blocking_enabled(bool p_enabled) { + ERR_FAIL_COND(!is_open()); + + int ret = 0; +#if defined(WINDOWS_ENABLED) || defined(NO_FCNTL) + unsigned long par = p_enabled ? 0 : 1; + ret = SOCK_IOCTL(_sock, FIONBIO, &par); +#else + int opts = fcntl(_sock, F_GETFL); + if (p_enabled) + ret = fcntl(_sock, F_SETFL, opts & ~O_NONBLOCK); + else + ret = fcntl(_sock, F_SETFL, opts | O_NONBLOCK); +#endif + + if (ret != 0) + WARN_PRINT("Unable to change non-block mode"); +} + +void NetSocketPosix::set_ipv6_only_enabled(bool p_enabled) { + ERR_FAIL_COND(!is_open()); + // This option is only avaiable in IPv6 sockets. + ERR_FAIL_COND(_ip_type == IP::TYPE_IPV4); + + int par = p_enabled ? 1 : 0; + if (setsockopt(_sock, IPPROTO_IPV6, IPV6_V6ONLY, SOCK_CBUF(&par), sizeof(int)) != 0) { + WARN_PRINT("Unable to change IPv4 address mapping over IPv6 option"); + } +} + +void NetSocketPosix::set_tcp_no_delay_enabled(bool p_enabled) { + ERR_FAIL_COND(!is_open()); + ERR_FAIL_COND(!_is_stream); // Not TCP + + int par = p_enabled ? 1 : 0; + if (setsockopt(_sock, IPPROTO_TCP, TCP_NODELAY, SOCK_CBUF(&par), sizeof(int)) < 0) { + ERR_PRINT("Unable to set TCP no delay option"); + } +} + +void NetSocketPosix::set_reuse_address_enabled(bool p_enabled) { + ERR_FAIL_COND(!is_open()); + + int par = p_enabled ? 1 : 0; + if (setsockopt(_sock, SOL_SOCKET, SO_REUSEADDR, SOCK_CBUF(&par), sizeof(int)) < 0) { + WARN_PRINT("Unable to set socket REUSEADDR option!"); + } +} + +void NetSocketPosix::set_reuse_port_enabled(bool p_enabled) { +// Windows does not have this option, as it is always ON when setting REUSEADDR. +#ifndef WINDOWS_ENABLED + ERR_FAIL_COND(!is_open()); + + int par = p_enabled ? 1 : 0; + if (setsockopt(_sock, SOL_SOCKET, SO_REUSEPORT, SOCK_CBUF(&par), sizeof(int)) < 0) { + WARN_PRINT("Unable to set socket REUSEPORT option!"); + } +#endif +} + +bool NetSocketPosix::is_open() const { + return _sock != SOCK_EMPTY; +} + +int NetSocketPosix::get_available_bytes() const { + + ERR_FAIL_COND_V(_sock == SOCK_EMPTY, -1); + + unsigned long len; + int ret = SOCK_IOCTL(_sock, FIONREAD, &len); + ERR_FAIL_COND_V(ret == -1, 0); + return len; +} + +Ref<NetSocket> NetSocketPosix::accept(IP_Address &r_ip, uint16_t &r_port) { + + Ref<NetSocket> out; + ERR_FAIL_COND_V(!is_open(), out); + + struct sockaddr_storage their_addr; + socklen_t size = sizeof(their_addr); + SOCKET_TYPE fd = ::accept(_sock, (struct sockaddr *)&their_addr, &size); + ERR_FAIL_COND_V(fd == SOCK_EMPTY, out); + + _set_ip_port(&their_addr, r_ip, r_port); + + NetSocketPosix *ns = memnew(NetSocketPosix); + ns->_set_socket(fd, _ip_type, _is_stream); + ns->set_blocking_enabled(false); + return Ref<NetSocket>(ns); +} diff --git a/drivers/windows/stream_peer_tcp_winsock.h b/drivers/unix/net_socket_posix.h index a0177d374e..010f2ea6e0 100644 --- a/drivers/windows/stream_peer_tcp_winsock.h +++ b/drivers/unix/net_socket_posix.h @@ -1,5 +1,5 @@ /*************************************************************************/ -/* stream_peer_tcp_winsock.h */ +/* net_socket_posix.h */ /*************************************************************************/ /* This file is part of: */ /* GODOT ENGINE */ @@ -28,65 +28,74 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ -#ifdef WINDOWS_ENABLED +#ifndef NET_SOCKET_UNIX_H +#define NET_SOCKET_UNIX_H -#ifndef STREAM_PEER_TCP_WINSOCK_H -#define STREAM_PEER_TCP_WINSOCK_H +#include "core/io/net_socket.h" -#include "error_list.h" +#if defined(WINDOWS_ENABLED) +#include <winsock2.h> +#include <ws2tcpip.h> +#define SOCKET_TYPE SOCKET -#include "core/io/ip_address.h" -#include "core/io/stream_peer_tcp.h" +#else +#include <sys/socket.h> +#define SOCKET_TYPE int -class StreamPeerTCPWinsock : public StreamPeerTCP { - -protected: - mutable Status status; - IP::Type sock_type; +#endif - int sockfd; +class NetSocketPosix : public NetSocket { - Error _block(int p_sockfd, bool p_read, bool p_write) const; +private: + SOCKET_TYPE _sock; + IP::Type _ip_type; + bool _is_stream; - Error _poll_connection() const; + enum NetError { + ERR_NET_WOULD_BLOCK, + ERR_NET_IS_CONNECTED, + ERR_NET_IN_PROGRESS, + ERR_NET_OTHER + }; - IP_Address peer_host; - int peer_port; + NetError _get_socket_error(); + void _set_socket(SOCKET_TYPE p_sock, IP::Type p_ip_type, bool p_is_stream); - Error write(const uint8_t *p_data, int p_bytes, int &r_sent, bool p_block); - Error read(uint8_t *p_buffer, int p_bytes, int &r_received, bool p_block); +protected: + static NetSocket *_create_func(); - static StreamPeerTCP *_create(); + bool _can_use_ip(const IP_Address p_ip, const bool p_for_bind) const; public: - virtual Error connect_to_host(const IP_Address &p_host, uint16_t p_port); - - virtual Error put_data(const uint8_t *p_data, int p_bytes); - virtual Error put_partial_data(const uint8_t *p_data, int p_bytes, int &r_sent); - - virtual Error get_data(uint8_t *p_buffer, int p_bytes); - virtual Error get_partial_data(uint8_t *p_buffer, int p_bytes, int &r_received); - - virtual int get_available_bytes() const; - - void set_socket(int p_sockfd, IP_Address p_host, int p_port, IP::Type p_sock_type); - - virtual IP_Address get_connected_host() const; - virtual uint16_t get_connected_port() const; - - virtual bool is_connected_to_host() const; - virtual Status get_status() const; - virtual void disconnect_from_host(); - static void make_default(); static void cleanup(); + static void _set_ip_port(struct sockaddr_storage *p_addr, IP_Address &r_ip, uint16_t &r_port); + static size_t _set_addr_storage(struct sockaddr_storage *p_addr, const IP_Address &p_ip, uint16_t p_port, IP::Type p_ip_type); + + virtual Error open(Type p_sock_type, IP::Type &ip_type); + virtual void close(); + virtual Error bind(IP_Address p_addr, uint16_t p_port); + virtual Error listen(int p_max_pending); + virtual Error connect_to_host(IP_Address p_addr, uint16_t p_port); + virtual Error poll(PollType p_type, int timeout) const; + virtual Error recv(uint8_t *p_buffer, int p_len, int &r_read); + virtual Error recvfrom(uint8_t *p_buffer, int p_len, int &r_read, IP_Address &r_ip, uint16_t &r_port); + virtual Error send(const uint8_t *p_buffer, int p_len, int &r_sent); + virtual Error sendto(const uint8_t *p_buffer, int p_len, int &r_sent, IP_Address p_ip, uint16_t p_port); + virtual Ref<NetSocket> accept(IP_Address &r_ip, uint16_t &r_port); + + virtual bool is_open() const; + virtual int get_available_bytes() const; - virtual void set_no_delay(bool p_enabled); + virtual void set_broadcasting_enabled(bool p_enabled); + virtual void set_blocking_enabled(bool p_enabled); + virtual void set_ipv6_only_enabled(bool p_enabled); + virtual void set_tcp_no_delay_enabled(bool p_enabled); + virtual void set_reuse_address_enabled(bool p_enabled); + virtual void set_reuse_port_enabled(bool p_enabled); - StreamPeerTCPWinsock(); - ~StreamPeerTCPWinsock(); + NetSocketPosix(); + ~NetSocketPosix(); }; -#endif // STREAM_PEER_TCP_WINSOCK_H - #endif diff --git a/drivers/unix/os_unix.cpp b/drivers/unix/os_unix.cpp index 05dfd69f58..279274734f 100644 --- a/drivers/unix/os_unix.cpp +++ b/drivers/unix/os_unix.cpp @@ -32,30 +32,27 @@ #ifdef UNIX_ENABLED -#include "servers/visual_server.h" - #include "core/os/thread_dummy.h" -#include "mutex_posix.h" -#include "rw_lock_posix.h" -#include "semaphore_posix.h" -#include "thread_posix.h" - -//#include "core/io/file_access_buffered_fa.h" -#include "dir_access_unix.h" -#include "file_access_unix.h" -#include "packet_peer_udp_posix.h" -#include "stream_peer_tcp_posix.h" -#include "tcp_server_posix.h" +#include "core/project_settings.h" +#include "drivers/unix/dir_access_unix.h" +#include "drivers/unix/file_access_unix.h" +#include "drivers/unix/mutex_posix.h" +#include "drivers/unix/net_socket_posix.h" +#include "drivers/unix/rw_lock_posix.h" +#include "drivers/unix/semaphore_posix.h" +#include "drivers/unix/thread_posix.h" +#include "servers/visual_server.h" #ifdef __APPLE__ #include <mach-o/dyld.h> +#include <mach/mach_time.h> #endif #if defined(__FreeBSD__) || defined(__OpenBSD__) #include <sys/param.h> #include <sys/sysctl.h> #endif -#include "project_settings.h" + #include <assert.h> #include <dlfcn.h> #include <errno.h> @@ -68,6 +65,32 @@ #include <sys/wait.h> #include <unistd.h> +/// Clock Setup function (used by get_ticks_usec) +static uint64_t _clock_start = 0; +#if defined(__APPLE__) +static double _clock_scale = 0; +static void _setup_clock() { + mach_timebase_info_data_t info; + kern_return_t ret = mach_timebase_info(&info); + ERR_EXPLAIN("OS CLOCK IS NOT WORKING!"); + ERR_FAIL_COND(ret != 0); + _clock_scale = ((double)info.numer / (double)info.denom) / 1000.0; + _clock_start = mach_absolute_time() * _clock_scale; +} +#else +#if defined(CLOCK_MONOTONIC_RAW) && !defined(JAVASCRIPT_ENABLED) // This is a better clock on Linux. +#define GODOT_CLOCK CLOCK_MONOTONIC_RAW +#else +#define GODOT_CLOCK CLOCK_MONOTONIC +#endif +static void _setup_clock() { + struct timespec tv_now = { 0, 0 }; + ERR_EXPLAIN("OS CLOCK IS NOT WORKING!"); + ERR_FAIL_COND(clock_gettime(GODOT_CLOCK, &tv_now) != 0); + _clock_start = ((uint64_t)tv_now.tv_nsec / 1000L) + (uint64_t)tv_now.tv_sec * 1000000L; +} +#endif + void OS_Unix::debug_break() { assert(false); @@ -126,14 +149,11 @@ void OS_Unix::initialize_core() { DirAccess::make_default<DirAccessUnix>(DirAccess::ACCESS_FILESYSTEM); #ifndef NO_NETWORK - TCPServerPosix::make_default(); - StreamPeerTCPPosix::make_default(); - PacketPeerUDPPosix::make_default(); + NetSocketPosix::make_default(); IP_Unix::make_default(); #endif - ticks_start = 0; - ticks_start = get_ticks_usec(); + _setup_clock(); struct sigaction sa; sa.sa_handler = &handle_sigchld; @@ -145,6 +165,8 @@ void OS_Unix::initialize_core() { } void OS_Unix::finalize_core() { + + NetSocketPosix::cleanup(); } void OS_Unix::alert(const String &p_alert, const String &p_title) { @@ -250,17 +272,27 @@ void OS_Unix::delay_usec(uint32_t p_usec) const { } uint64_t OS_Unix::get_ticks_usec() const { - struct timeval tv_now; - gettimeofday(&tv_now, NULL); - - uint64_t longtime = (uint64_t)tv_now.tv_usec + (uint64_t)tv_now.tv_sec * 1000000L; - longtime -= ticks_start; +#if defined(__APPLE__) + uint64_t longtime = mach_absolute_time() * _clock_scale; +#else + // Unchecked return. Static analyzers might complain. + // If _setup_clock() succeded, we assume clock_gettime() works. + struct timespec tv_now = { 0, 0 }; + clock_gettime(GODOT_CLOCK, &tv_now); + uint64_t longtime = ((uint64_t)tv_now.tv_nsec / 1000L) + (uint64_t)tv_now.tv_sec * 1000000L; +#endif + longtime -= _clock_start; return longtime; } Error OS_Unix::execute(const String &p_path, const List<String> &p_arguments, bool p_blocking, ProcessID *r_child_id, String *r_pipe, int *r_exitcode, bool read_stderr) { +#ifdef __EMSCRIPTEN__ + // Don't compile this code at all to avoid undefined references. + // Actual virtual call goes to OS_JavaScript. + ERR_FAIL_V(ERR_BUG); +#else if (p_blocking && r_pipe) { String argss; @@ -327,6 +359,7 @@ Error OS_Unix::execute(const String &p_path, const List<String> &p_arguments, bo } return OK; +#endif } Error OS_Unix::kill(const ProcessID &p_pid) { @@ -460,9 +493,11 @@ String OS_Unix::get_executable_path() const { //fix for running from a symlink char buf[256]; memset(buf, 0, 256); - readlink("/proc/self/exe", buf, sizeof(buf)); + ssize_t len = readlink("/proc/self/exe", buf, sizeof(buf)); String b; - b.parse_utf8(buf); + if (len > 0) { + b.parse_utf8(buf, len); + } if (b == "") { WARN_PRINT("Couldn't get executable path from /proc/self/exe, using argv[0]"); return OS::get_executable_path(); diff --git a/drivers/unix/os_unix.h b/drivers/unix/os_unix.h index 95b74d23ff..b702454603 100644 --- a/drivers/unix/os_unix.h +++ b/drivers/unix/os_unix.h @@ -37,13 +37,11 @@ #ifdef UNIX_ENABLED +#include "core/os/os.h" #include "drivers/unix/ip_unix.h" -#include "os/os.h" class OS_Unix : public OS { - uint64_t ticks_start; - protected: // UNIX only handles the core functions. // inheriting platforms under unix (eg. X11) should handle the rest diff --git a/drivers/unix/packet_peer_udp_posix.cpp b/drivers/unix/packet_peer_udp_posix.cpp deleted file mode 100644 index 1380c1d88b..0000000000 --- a/drivers/unix/packet_peer_udp_posix.cpp +++ /dev/null @@ -1,317 +0,0 @@ -/*************************************************************************/ -/* packet_peer_udp_posix.cpp */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ - -#include "packet_peer_udp_posix.h" - -#ifdef UNIX_ENABLED - -#include <errno.h> -#include <netdb.h> -#include <sys/socket.h> -#include <sys/types.h> -#include <unistd.h> - -#include <netinet/in.h> -#include <stdio.h> - -#ifndef NO_FCNTL -#ifdef __HAIKU__ -#include <fcntl.h> -#else -#include <sys/fcntl.h> -#endif -#else -#include <sys/ioctl.h> -#endif - -#ifdef JAVASCRIPT_ENABLED -#include <arpa/inet.h> -#endif - -#include "drivers/unix/socket_helpers.h" - -int PacketPeerUDPPosix::get_available_packet_count() const { - - Error err = const_cast<PacketPeerUDPPosix *>(this)->_poll(false); - if (err != OK) - return 0; - - return queue_count; -} - -Error PacketPeerUDPPosix::get_packet(const uint8_t **r_buffer, int &r_buffer_size) { - - Error err = const_cast<PacketPeerUDPPosix *>(this)->_poll(false); - if (err != OK) - return err; - if (queue_count == 0) - return ERR_UNAVAILABLE; - - uint32_t size = 0; - uint8_t type = IP::TYPE_NONE; - rb.read(&type, 1, true); - if (type == IP::TYPE_IPV4) { - uint8_t ip[4]; - rb.read(ip, 4, true); - packet_ip.set_ipv4(ip); - } else { - uint8_t ipv6[16]; - rb.read(ipv6, 16, true); - packet_ip.set_ipv6(ipv6); - }; - rb.read((uint8_t *)&packet_port, 4, true); - rb.read((uint8_t *)&size, 4, true); - rb.read(packet_buffer, size, true); - --queue_count; - *r_buffer = packet_buffer; - r_buffer_size = size; - return OK; -} -Error PacketPeerUDPPosix::put_packet(const uint8_t *p_buffer, int p_buffer_size) { - - ERR_FAIL_COND_V(!peer_addr.is_valid(), ERR_UNCONFIGURED); - - if (sock_type == IP::TYPE_NONE) - sock_type = peer_addr.is_ipv4() ? IP::TYPE_IPV4 : IP::TYPE_IPV6; - - int sock = _get_socket(); - ERR_FAIL_COND_V(sock == -1, FAILED); - struct sockaddr_storage addr; - size_t addr_size = _set_sockaddr(&addr, peer_addr, peer_port, sock_type); - - errno = 0; - int err; - - _set_sock_blocking(blocking); - - while ((err = sendto(sock, p_buffer, p_buffer_size, 0, (struct sockaddr *)&addr, addr_size)) != p_buffer_size) { - - if (errno != EAGAIN) { - return FAILED; - } else if (!blocking) { - return ERR_UNAVAILABLE; - } - } - - return OK; -} - -int PacketPeerUDPPosix::get_max_packet_size() const { - - return 512; // uhm maybe not -} - -Error PacketPeerUDPPosix::listen(int p_port, const IP_Address &p_bind_address, int p_recv_buffer_size) { - - ERR_FAIL_COND_V(sockfd != -1, ERR_ALREADY_IN_USE); - ERR_FAIL_COND_V(!p_bind_address.is_valid() && !p_bind_address.is_wildcard(), ERR_INVALID_PARAMETER); - -#ifdef __OpenBSD__ - sock_type = IP::TYPE_IPV4; // OpenBSD does not support dual stacking, fallback to IPv4 only. -#else - sock_type = IP::TYPE_ANY; -#endif - - if (p_bind_address.is_valid()) - sock_type = p_bind_address.is_ipv4() ? IP::TYPE_IPV4 : IP::TYPE_IPV6; - - int sock = _get_socket(); - - if (sock == -1) - return ERR_CANT_CREATE; - - sockaddr_storage addr = { 0 }; - size_t addr_size = _set_listen_sockaddr(&addr, p_port, sock_type, IP_Address()); - - if (bind(sock, (struct sockaddr *)&addr, addr_size) == -1) { - close(); - return ERR_UNAVAILABLE; - } - rb.resize(nearest_shift(p_recv_buffer_size)); - return OK; -} - -void PacketPeerUDPPosix::close() { - - if (sockfd != -1) - ::close(sockfd); - sockfd = -1; - sock_type = IP::TYPE_NONE; - rb.resize(16); - queue_count = 0; -} - -Error PacketPeerUDPPosix::wait() { - - return _poll(true); -} - -Error PacketPeerUDPPosix::_poll(bool p_block) { - - if (sockfd == -1) { - return FAILED; - } - - _set_sock_blocking(p_block); - - struct sockaddr_storage from = { 0 }; - socklen_t len = sizeof(struct sockaddr_storage); - int ret; - while ((ret = recvfrom(sockfd, recv_buffer, MIN((int)sizeof(recv_buffer), MAX(rb.space_left() - 24, 0)), 0, (struct sockaddr *)&from, &len)) > 0) { - - uint32_t port = 0; - - if (from.ss_family == AF_INET) { - uint8_t type = (uint8_t)IP::TYPE_IPV4; - rb.write(&type, 1); - struct sockaddr_in *sin_from = (struct sockaddr_in *)&from; - rb.write((uint8_t *)&sin_from->sin_addr, 4); - port = ntohs(sin_from->sin_port); - - } else if (from.ss_family == AF_INET6) { - - uint8_t type = (uint8_t)IP::TYPE_IPV6; - rb.write(&type, 1); - - struct sockaddr_in6 *s6_from = (struct sockaddr_in6 *)&from; - rb.write((uint8_t *)&s6_from->sin6_addr, 16); - - port = ntohs(s6_from->sin6_port); - - } else { - // WARN_PRINT("Ignoring packet with unknown address family"); - uint8_t type = (uint8_t)IP::TYPE_NONE; - rb.write(&type, 1); - }; - - rb.write((uint8_t *)&port, 4); - rb.write((uint8_t *)&ret, 4); - rb.write(recv_buffer, ret); - - len = sizeof(struct sockaddr_storage); - ++queue_count; - if (p_block) - break; - }; - - // TODO: Should ECONNRESET be handled here? - if (ret == 0 || (ret == -1 && errno != EAGAIN)) { - close(); - return FAILED; - }; - - return OK; -} -bool PacketPeerUDPPosix::is_listening() const { - - return sockfd != -1; -} - -IP_Address PacketPeerUDPPosix::get_packet_address() const { - - return packet_ip; -} - -int PacketPeerUDPPosix::get_packet_port() const { - - return packet_port; -} - -int PacketPeerUDPPosix::_get_socket() { - - ERR_FAIL_COND_V(sock_type == IP::TYPE_NONE, -1); - - if (sockfd != -1) - return sockfd; - - sockfd = _socket_create(sock_type, SOCK_DGRAM, IPPROTO_UDP); - - if (sockfd != -1) - _set_sock_blocking(false); - - return sockfd; -} - -void PacketPeerUDPPosix::_set_sock_blocking(bool p_blocking) { - - if (sock_blocking == p_blocking) - return; - - sock_blocking = p_blocking; - -#ifndef NO_FCNTL - int opts = fcntl(sockfd, F_GETFL); - int ret = 0; - if (sock_blocking) - ret = fcntl(sockfd, F_SETFL, opts & ~O_NONBLOCK); - else - ret = fcntl(sockfd, F_SETFL, opts | O_NONBLOCK); - if (ret == -1) - perror("setting non-block mode"); -#else - int bval = sock_blocking ? 0 : 1; - if (ioctl(sockfd, FIONBIO, &bval) == -1) - perror("setting non-block mode"); -#endif -} - -void PacketPeerUDPPosix::set_dest_address(const IP_Address &p_address, int p_port) { - - peer_addr = p_address; - peer_port = p_port; -} - -PacketPeerUDP *PacketPeerUDPPosix::_create() { - - return memnew(PacketPeerUDPPosix); -}; - -void PacketPeerUDPPosix::make_default() { - - PacketPeerUDP::_create = PacketPeerUDPPosix::_create; -}; - -PacketPeerUDPPosix::PacketPeerUDPPosix() { - - blocking = true; - sock_blocking = true; - sockfd = -1; - packet_port = 0; - queue_count = 0; - peer_port = 0; - sock_type = IP::TYPE_NONE; - rb.resize(16); -} - -PacketPeerUDPPosix::~PacketPeerUDPPosix() { - - close(); -} -#endif diff --git a/drivers/unix/packet_peer_udp_posix.h b/drivers/unix/packet_peer_udp_posix.h deleted file mode 100644 index 7f72a9bfc9..0000000000 --- a/drivers/unix/packet_peer_udp_posix.h +++ /dev/null @@ -1,88 +0,0 @@ -/*************************************************************************/ -/* packet_peer_udp_posix.h */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ - -#ifndef PACKET_PEER_UDP_POSIX_H -#define PACKET_PEER_UDP_POSIX_H - -#ifdef UNIX_ENABLED - -#include "io/packet_peer_udp.h" -#include "ring_buffer.h" - -class PacketPeerUDPPosix : public PacketPeerUDP { - - enum { - PACKET_BUFFER_SIZE = 65536 - }; - - RingBuffer<uint8_t> rb; - uint8_t recv_buffer[PACKET_BUFFER_SIZE]; - uint8_t packet_buffer[PACKET_BUFFER_SIZE]; - IP_Address packet_ip; - int packet_port; - int queue_count; - int sockfd; - bool sock_blocking; - IP::Type sock_type; - - IP_Address peer_addr; - int peer_port; - - _FORCE_INLINE_ int _get_socket(); - - static PacketPeerUDP *_create(); - void _set_sock_blocking(bool p_blocking); - virtual Error _poll(bool p_block); - -public: - virtual int get_available_packet_count() const; - virtual Error get_packet(const uint8_t **r_buffer, int &r_buffer_size); - virtual Error put_packet(const uint8_t *p_buffer, int p_buffer_size); - - virtual int get_max_packet_size() const; - - virtual Error listen(int p_port, const IP_Address &p_bind_address = IP_Address("*"), int p_recv_buffer_size = 65536); - virtual void close(); - virtual Error wait(); - virtual bool is_listening() const; - - virtual IP_Address get_packet_address() const; - virtual int get_packet_port() const; - - virtual void set_dest_address(const IP_Address &p_address, int p_port); - - static void make_default(); - - PacketPeerUDPPosix(); - ~PacketPeerUDPPosix(); -}; - -#endif // PACKET_PEER_UDP_POSIX_H -#endif diff --git a/drivers/unix/rw_lock_posix.cpp b/drivers/unix/rw_lock_posix.cpp index 4df965cabb..27b19c30d5 100644 --- a/drivers/unix/rw_lock_posix.cpp +++ b/drivers/unix/rw_lock_posix.cpp @@ -32,8 +32,8 @@ #include "rw_lock_posix.h" -#include "error_macros.h" -#include "os/memory.h" +#include "core/error_macros.h" +#include "core/os/memory.h" #include <stdio.h> void RWLockPosix::read_lock() { diff --git a/drivers/unix/rw_lock_posix.h b/drivers/unix/rw_lock_posix.h index 617b9705da..897b617f98 100644 --- a/drivers/unix/rw_lock_posix.h +++ b/drivers/unix/rw_lock_posix.h @@ -33,7 +33,7 @@ #if defined(UNIX_ENABLED) || defined(PTHREAD_ENABLED) -#include "os/rw_lock.h" +#include "core/os/rw_lock.h" #include <pthread.h> class RWLockPosix : public RWLock { diff --git a/drivers/unix/semaphore_posix.cpp b/drivers/unix/semaphore_posix.cpp index 5cabfe4937..26c2aeab28 100644 --- a/drivers/unix/semaphore_posix.cpp +++ b/drivers/unix/semaphore_posix.cpp @@ -32,7 +32,7 @@ #if defined(UNIX_ENABLED) || defined(PTHREAD_ENABLED) -#include "os/memory.h" +#include "core/os/memory.h" #include <errno.h> #include <stdio.h> diff --git a/drivers/unix/semaphore_posix.h b/drivers/unix/semaphore_posix.h index 283174bb2e..025b87c0d7 100644 --- a/drivers/unix/semaphore_posix.h +++ b/drivers/unix/semaphore_posix.h @@ -31,7 +31,7 @@ #ifndef SEMAPHORE_POSIX_H #define SEMAPHORE_POSIX_H -#include "os/semaphore.h" +#include "core/os/semaphore.h" #if defined(UNIX_ENABLED) || defined(PTHREAD_ENABLED) diff --git a/drivers/unix/socket_helpers.h b/drivers/unix/socket_helpers.h deleted file mode 100644 index 5b42c13eae..0000000000 --- a/drivers/unix/socket_helpers.h +++ /dev/null @@ -1,156 +0,0 @@ -/*************************************************************************/ -/* socket_helpers.h */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ - -#ifndef SOCKET_HELPERS_H -#define SOCKET_HELPERS_H - -#include <string.h> - -#if defined(__MINGW32__) && (!defined(__MINGW64_VERSION_MAJOR) || __MINGW64_VERSION_MAJOR < 4) -// Workaround for mingw-w64 < 4.0 -#ifndef IPV6_V6ONLY -#define IPV6_V6ONLY 27 -#endif -#endif - -// helpers for sockaddr -> IP_Address and back, should work for posix and winsock. All implementations should use this - -static size_t _set_sockaddr(struct sockaddr_storage *p_addr, const IP_Address &p_ip, int p_port, IP::Type p_sock_type = IP::TYPE_ANY) { - - memset(p_addr, 0, sizeof(struct sockaddr_storage)); - - ERR_FAIL_COND_V(!p_ip.is_valid(), 0); - - // IPv6 socket - if (p_sock_type == IP::TYPE_IPV6 || p_sock_type == IP::TYPE_ANY) { - - // IPv6 only socket with IPv4 address - ERR_FAIL_COND_V(p_sock_type == IP::TYPE_IPV6 && p_ip.is_ipv4(), 0); - - struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)p_addr; - addr6->sin6_family = AF_INET6; - addr6->sin6_port = htons(p_port); - copymem(&addr6->sin6_addr.s6_addr, p_ip.get_ipv6(), 16); - return sizeof(sockaddr_in6); - - } else { // IPv4 socket - - // IPv4 socket with IPv6 address - ERR_FAIL_COND_V(!p_ip.is_ipv4(), 0); - - struct sockaddr_in *addr4 = (struct sockaddr_in *)p_addr; - addr4->sin_family = AF_INET; - addr4->sin_port = htons(p_port); // short, network byte order - copymem(&addr4->sin_addr.s_addr, p_ip.get_ipv4(), 16); - return sizeof(sockaddr_in); - }; -}; - -static size_t _set_listen_sockaddr(struct sockaddr_storage *p_addr, int p_port, IP::Type p_sock_type, const IP_Address p_bind_address) { - - memset(p_addr, 0, sizeof(struct sockaddr_storage)); - if (p_sock_type == IP::TYPE_IPV4) { - struct sockaddr_in *addr4 = (struct sockaddr_in *)p_addr; - addr4->sin_family = AF_INET; - addr4->sin_port = htons(p_port); - if (p_bind_address.is_valid()) { - copymem(&addr4->sin_addr.s_addr, p_bind_address.get_ipv4(), 4); - } else { - addr4->sin_addr.s_addr = INADDR_ANY; - } - return sizeof(sockaddr_in); - } else { - struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)p_addr; - - addr6->sin6_family = AF_INET6; - addr6->sin6_port = htons(p_port); - if (p_bind_address.is_valid()) { - copymem(&addr6->sin6_addr.s6_addr, p_bind_address.get_ipv6(), 16); - } else { - addr6->sin6_addr = in6addr_any; - } - return sizeof(sockaddr_in6); - }; -}; - -static int _socket_create(IP::Type &p_type, int type, int protocol) { - - ERR_FAIL_COND_V(p_type > IP::TYPE_ANY || p_type < IP::TYPE_NONE, ERR_INVALID_PARAMETER); - - int family = p_type == IP::TYPE_IPV4 ? AF_INET : AF_INET6; - int sockfd = socket(family, type, protocol); - - if (sockfd == -1 && p_type == IP::TYPE_ANY) { - // Careful here, changing the referenced parameter so the caller knows that we are using an IPv4 socket - // in place of a dual stack one, and further calls to _set_sock_addr will work as expected. - p_type = IP::TYPE_IPV4; - family = AF_INET; - sockfd = socket(family, type, protocol); - } - - ERR_FAIL_COND_V(sockfd == -1, -1); - - if (family == AF_INET6) { - // Select IPv4 over IPv6 mapping - int opt = p_type != IP::TYPE_ANY; - if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_V6ONLY, (const char *)&opt, sizeof(opt)) != 0) { - WARN_PRINT("Unable to set/unset IPv4 address mapping over IPv6"); - } - } - if (protocol == IPPROTO_UDP && p_type != IP::TYPE_IPV6) { - // Enable broadcasting for UDP sockets if it's not IPv6 only (IPv6 has no broadcast option). - int broadcast = 1; - if (setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, (char *)&broadcast, sizeof(broadcast)) != 0) { - WARN_PRINT("Error when enabling broadcasting"); - } - } - - return sockfd; -} - -static void _set_ip_addr_port(IP_Address &r_ip, int &r_port, struct sockaddr_storage *p_addr) { - - if (p_addr->ss_family == AF_INET) { - - struct sockaddr_in *addr4 = (struct sockaddr_in *)p_addr; - r_ip.set_ipv4((uint8_t *)&(addr4->sin_addr.s_addr)); - - r_port = ntohs(addr4->sin_port); - - } else if (p_addr->ss_family == AF_INET6) { - - struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)p_addr; - r_ip.set_ipv6(addr6->sin6_addr.s6_addr); - - r_port = ntohs(addr6->sin6_port); - }; -}; - -#endif diff --git a/drivers/unix/stream_peer_tcp_posix.cpp b/drivers/unix/stream_peer_tcp_posix.cpp deleted file mode 100644 index 44b9ef1d7c..0000000000 --- a/drivers/unix/stream_peer_tcp_posix.cpp +++ /dev/null @@ -1,411 +0,0 @@ -/*************************************************************************/ -/* stream_peer_tcp_posix.cpp */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ - -#ifdef UNIX_ENABLED - -#include "stream_peer_tcp_posix.h" - -#include <errno.h> -#include <netdb.h> -#include <poll.h> -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <sys/ioctl.h> -#include <sys/types.h> -#include <unistd.h> -#ifndef NO_FCNTL -#ifdef __HAIKU__ -#include <fcntl.h> -#else -#include <sys/fcntl.h> -#endif -#else -#include <sys/ioctl.h> -#endif -#include <netinet/in.h> - -#include <sys/socket.h> -#ifdef JAVASCRIPT_ENABLED -#include <arpa/inet.h> -#endif - -#include <netinet/tcp.h> - -#if defined(OSX_ENABLED) || defined(IPHONE_ENABLED) -#define MSG_NOSIGNAL SO_NOSIGPIPE -#endif - -#include "drivers/unix/socket_helpers.h" - -StreamPeerTCP *StreamPeerTCPPosix::_create() { - - return memnew(StreamPeerTCPPosix); -}; - -void StreamPeerTCPPosix::make_default() { - - StreamPeerTCP::_create = StreamPeerTCPPosix::_create; -}; - -Error StreamPeerTCPPosix::_block(int p_sockfd, bool p_read, bool p_write) const { - - struct pollfd pfd; - pfd.fd = p_sockfd; - pfd.events = 0; - if (p_read) - pfd.events |= POLLIN; - if (p_write) - pfd.events |= POLLOUT; - pfd.revents = 0; - - int ret = poll(&pfd, 1, -1); - return ret < 0 ? FAILED : OK; -}; - -Error StreamPeerTCPPosix::_poll_connection() const { - - ERR_FAIL_COND_V(status != STATUS_CONNECTING || sockfd == -1, FAILED); - - struct sockaddr_storage their_addr; - size_t addr_size = _set_sockaddr(&their_addr, peer_host, peer_port, sock_type); - - if (::connect(sockfd, (struct sockaddr *)&their_addr, addr_size) == -1) { - - if (errno == EISCONN) { - status = STATUS_CONNECTED; - return OK; - }; - - if (errno == EINPROGRESS || errno == EALREADY) { - return OK; - } - - status = STATUS_ERROR; - return ERR_CONNECTION_ERROR; - } else { - - status = STATUS_CONNECTED; - return OK; - }; - - return OK; -}; - -void StreamPeerTCPPosix::set_socket(int p_sockfd, IP_Address p_host, int p_port, IP::Type p_sock_type) { - - sock_type = p_sock_type; - sockfd = p_sockfd; -#ifndef NO_FCNTL - if (fcntl(sockfd, F_SETFL, O_NONBLOCK) < 0) { - WARN_PRINT("Error setting socket as non blocking"); - } -#else - int bval = 1; - if (ioctl(sockfd, FIONBIO, &bval) < 0) { - WARN_PRINT("Error setting socket as non blocking"); - } -#endif - status = STATUS_CONNECTING; - - peer_host = p_host; - peer_port = p_port; -}; - -Error StreamPeerTCPPosix::connect_to_host(const IP_Address &p_host, uint16_t p_port) { - - ERR_FAIL_COND_V(!p_host.is_valid(), ERR_INVALID_PARAMETER); - - sock_type = p_host.is_ipv4() ? IP::TYPE_IPV4 : IP::TYPE_IPV6; - sockfd = _socket_create(sock_type, SOCK_STREAM, IPPROTO_TCP); - if (sockfd == -1) { - ERR_PRINT("Socket creation failed!"); - disconnect_from_host(); - //perror("socket"); - return FAILED; - }; - -#ifndef NO_FCNTL - if (fcntl(sockfd, F_SETFL, O_NONBLOCK) < 0) { - WARN_PRINT("Error setting socket as non blocking"); - } -#else - int bval = 1; - if (ioctl(sockfd, FIONBIO, &bval) < 0) { - WARN_PRINT("Error setting socket as non blocking"); - } -#endif - - struct sockaddr_storage their_addr; - size_t addr_size = _set_sockaddr(&their_addr, p_host, p_port, sock_type); - - errno = 0; - if (::connect(sockfd, (struct sockaddr *)&their_addr, addr_size) == -1 && errno != EINPROGRESS) { - - ERR_PRINT("Connection to remote host failed!"); - disconnect_from_host(); - return FAILED; - }; - - if (errno == EINPROGRESS) { - status = STATUS_CONNECTING; - } else { - status = STATUS_CONNECTED; - }; - - peer_host = p_host; - peer_port = p_port; - - return OK; -}; - -Error StreamPeerTCPPosix::write(const uint8_t *p_data, int p_bytes, int &r_sent, bool p_block) { - - if (status == STATUS_NONE || status == STATUS_ERROR) { - - return FAILED; - }; - - if (status != STATUS_CONNECTED) { - - if (_poll_connection() != OK) { - - return FAILED; - }; - - if (status != STATUS_CONNECTED) { - r_sent = 0; - return OK; - }; - }; - - int data_to_send = p_bytes; - const uint8_t *offset = p_data; - if (sockfd == -1) return FAILED; - errno = 0; - int total_sent = 0; - - while (data_to_send) { - - int sent_amount = send(sockfd, offset, data_to_send, MSG_NOSIGNAL); - //printf("Sent TCP data of %d bytes, errno %d\n", sent_amount, errno); - - if (sent_amount == -1) { - - if (errno != EAGAIN) { - - perror("Nothing sent"); - disconnect_from_host(); - ERR_PRINT("Server disconnected!\n"); - return FAILED; - }; - - if (!p_block) { - r_sent = total_sent; - return OK; - }; - - _block(sockfd, false, true); - } else { - - data_to_send -= sent_amount; - offset += sent_amount; - total_sent += sent_amount; - }; - } - - r_sent = total_sent; - - return OK; -}; - -Error StreamPeerTCPPosix::read(uint8_t *p_buffer, int p_bytes, int &r_received, bool p_block) { - - if (!is_connected_to_host()) { - - return FAILED; - }; - - if (status == STATUS_CONNECTING) { - - if (_poll_connection() != OK) { - - return FAILED; - }; - - if (status != STATUS_CONNECTED) { - r_received = 0; - return OK; - }; - }; - - int to_read = p_bytes; - int total_read = 0; - errno = 0; - - while (to_read) { - - int read = recv(sockfd, p_buffer + total_read, to_read, 0); - - if (read == -1) { - - if (errno != EAGAIN) { - - perror("Nothing read"); - disconnect_from_host(); - ERR_PRINT("Server disconnected!\n"); - return FAILED; - }; - - if (!p_block) { - - r_received = total_read; - return OK; - }; - _block(sockfd, true, false); - - } else if (read == 0) { - - sockfd = -1; - status = STATUS_NONE; - peer_port = 0; - peer_host = IP_Address(); - r_received = total_read; - return ERR_FILE_EOF; - - } else { - - to_read -= read; - total_read += read; - }; - }; - - r_received = total_read; - - return OK; -}; - -void StreamPeerTCPPosix::set_no_delay(bool p_enabled) { - - ERR_FAIL_COND(!is_connected_to_host()); - int flag = p_enabled ? 1 : 0; - if (setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, (char *)&flag, sizeof(int)) < 0) { - ERR_PRINT("Unable to set TCP no delay option"); - } -} - -bool StreamPeerTCPPosix::is_connected_to_host() const { - - if (status == STATUS_NONE || status == STATUS_ERROR) { - - return false; - }; - if (status != STATUS_CONNECTED) { - return true; - }; - - return (sockfd != -1); -}; - -StreamPeerTCP::Status StreamPeerTCPPosix::get_status() const { - - if (status == STATUS_CONNECTING) { - _poll_connection(); - }; - - return status; -}; - -void StreamPeerTCPPosix::disconnect_from_host() { - - if (sockfd != -1) - close(sockfd); - - sock_type = IP::TYPE_NONE; - sockfd = -1; - - status = STATUS_NONE; - peer_port = 0; - peer_host = IP_Address(); -}; - -Error StreamPeerTCPPosix::put_data(const uint8_t *p_data, int p_bytes) { - - int total; - return write(p_data, p_bytes, total, true); -}; - -Error StreamPeerTCPPosix::put_partial_data(const uint8_t *p_data, int p_bytes, int &r_sent) { - - return write(p_data, p_bytes, r_sent, false); -}; - -Error StreamPeerTCPPosix::get_data(uint8_t *p_buffer, int p_bytes) { - - int total; - return read(p_buffer, p_bytes, total, true); -}; - -Error StreamPeerTCPPosix::get_partial_data(uint8_t *p_buffer, int p_bytes, int &r_received) { - - return read(p_buffer, p_bytes, r_received, false); -}; - -int StreamPeerTCPPosix::get_available_bytes() const { - - unsigned long len; - int ret = ioctl(sockfd, FIONREAD, &len); - ERR_FAIL_COND_V(ret == -1, 0) - return len; -} -IP_Address StreamPeerTCPPosix::get_connected_host() const { - - return peer_host; -}; - -uint16_t StreamPeerTCPPosix::get_connected_port() const { - - return peer_port; -}; - -StreamPeerTCPPosix::StreamPeerTCPPosix() { - - sock_type = IP::TYPE_NONE; - sockfd = -1; - status = STATUS_NONE; - peer_port = 0; -}; - -StreamPeerTCPPosix::~StreamPeerTCPPosix() { - - disconnect_from_host(); -}; - -#endif diff --git a/drivers/unix/stream_peer_tcp_posix.h b/drivers/unix/stream_peer_tcp_posix.h deleted file mode 100644 index bcebe57771..0000000000 --- a/drivers/unix/stream_peer_tcp_posix.h +++ /dev/null @@ -1,90 +0,0 @@ -/*************************************************************************/ -/* stream_peer_tcp_posix.h */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ - -#ifdef UNIX_ENABLED - -#ifndef STREAM_PEER_TCP_POSIX_H -#define STREAM_PEER_TCP_POSIX_H - -#include "core/io/ip_address.h" -#include "core/io/stream_peer_tcp.h" -#include "error_list.h" - -class StreamPeerTCPPosix : public StreamPeerTCP { - -protected: - mutable Status status; - - IP::Type sock_type; - int sockfd; - - Error _block(int p_sockfd, bool p_read, bool p_write) const; - - Error _poll_connection() const; - - IP_Address peer_host; - int peer_port; - - Error write(const uint8_t *p_data, int p_bytes, int &r_sent, bool p_block); - Error read(uint8_t *p_buffer, int p_bytes, int &r_received, bool p_block); - - static StreamPeerTCP *_create(); - -public: - virtual Error connect_to_host(const IP_Address &p_host, uint16_t p_port); - - virtual Error put_data(const uint8_t *p_data, int p_bytes); - virtual Error put_partial_data(const uint8_t *p_data, int p_bytes, int &r_sent); - - virtual Error get_data(uint8_t *p_buffer, int p_bytes); - virtual Error get_partial_data(uint8_t *p_buffer, int p_bytes, int &r_received); - - virtual int get_available_bytes() const; - - void set_socket(int p_sockfd, IP_Address p_host, int p_port, IP::Type p_sock_type); - - virtual IP_Address get_connected_host() const; - virtual uint16_t get_connected_port() const; - - virtual bool is_connected_to_host() const; - virtual Status get_status() const; - virtual void disconnect_from_host(); - - virtual void set_no_delay(bool p_enabled); - - static void make_default(); - - StreamPeerTCPPosix(); - ~StreamPeerTCPPosix(); -}; - -#endif // TCP_CLIENT_POSIX_H - -#endif diff --git a/drivers/unix/syslog_logger.cpp b/drivers/unix/syslog_logger.cpp index 727672458c..c7b4daf4ad 100644 --- a/drivers/unix/syslog_logger.cpp +++ b/drivers/unix/syslog_logger.cpp @@ -31,7 +31,7 @@ #ifdef UNIX_ENABLED #include "syslog_logger.h" -#include "print_string.h" +#include "core/print_string.h" #include <syslog.h> void SyslogLogger::logv(const char *p_format, va_list p_list, bool p_err) { diff --git a/drivers/unix/syslog_logger.h b/drivers/unix/syslog_logger.h index 40bf26cee1..745264ab6f 100644 --- a/drivers/unix/syslog_logger.h +++ b/drivers/unix/syslog_logger.h @@ -33,7 +33,7 @@ #ifdef UNIX_ENABLED -#include "io/logger.h" +#include "core/io/logger.h" class SyslogLogger : public Logger { public: @@ -45,4 +45,4 @@ public: #endif -#endif
\ No newline at end of file +#endif diff --git a/drivers/unix/tcp_server_posix.cpp b/drivers/unix/tcp_server_posix.cpp deleted file mode 100644 index 67ab981f46..0000000000 --- a/drivers/unix/tcp_server_posix.cpp +++ /dev/null @@ -1,207 +0,0 @@ -/*************************************************************************/ -/* tcp_server_posix.cpp */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ - -#include "tcp_server_posix.h" -#include "stream_peer_tcp_posix.h" - -#ifdef UNIX_ENABLED - -#include <poll.h> - -#include <errno.h> -#include <netdb.h> -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <sys/types.h> -#include <unistd.h> -#ifndef NO_FCNTL -#ifdef __HAIKU__ -#include <fcntl.h> -#else -#include <sys/fcntl.h> -#endif -#else -#include <sys/ioctl.h> -#endif -#ifdef JAVASCRIPT_ENABLED -#include <arpa/inet.h> -#endif -#include <assert.h> -#include <netinet/in.h> -#include <sys/socket.h> - -#include "drivers/unix/socket_helpers.h" - -TCP_Server *TCPServerPosix::_create() { - - return memnew(TCPServerPosix); -}; - -void TCPServerPosix::make_default() { - - TCP_Server::_create = TCPServerPosix::_create; -}; - -Error TCPServerPosix::listen(uint16_t p_port, const IP_Address &p_bind_address) { - - ERR_FAIL_COND_V(listen_sockfd != -1, ERR_ALREADY_IN_USE); - ERR_FAIL_COND_V(!p_bind_address.is_valid() && !p_bind_address.is_wildcard(), ERR_INVALID_PARAMETER); - - int sockfd; -#ifdef __OpenBSD__ - sock_type = IP::TYPE_IPV4; // OpenBSD does not support dual stacking, fallback to IPv4 only. -#else - sock_type = IP::TYPE_ANY; -#endif - - // If the bind address is valid use its type as the socket type - if (p_bind_address.is_valid()) - sock_type = p_bind_address.is_ipv4() ? IP::TYPE_IPV4 : IP::TYPE_IPV6; - - sockfd = _socket_create(sock_type, SOCK_STREAM, IPPROTO_TCP); - - ERR_FAIL_COND_V(sockfd == -1, FAILED); - -#ifndef NO_FCNTL - if (fcntl(sockfd, F_SETFL, O_NONBLOCK) < 0) { - WARN_PRINT("Error setting socket as non blocking"); - } -#else - int bval = 1; - if (ioctl(sockfd, FIONBIO, &bval) < 0) { - WARN_PRINT("Error setting socket as non blocking"); - } -#endif - - int reuse = 1; - if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (char *)&reuse, sizeof(reuse)) < 0) { - WARN_PRINT("REUSEADDR failed!") - } - - struct sockaddr_storage addr; - size_t addr_size = _set_listen_sockaddr(&addr, p_port, sock_type, p_bind_address); - - if (bind(sockfd, (struct sockaddr *)&addr, addr_size) != -1) { - - if (::listen(sockfd, 1) == -1) { - - close(sockfd); - ERR_FAIL_V(FAILED); - }; - } else { - close(sockfd); - return ERR_ALREADY_IN_USE; - }; - - if (listen_sockfd != -1) { - stop(); - }; - - listen_sockfd = sockfd; - - return OK; -}; - -bool TCPServerPosix::is_connection_available() const { - - if (listen_sockfd == -1) { - return false; - }; - - struct pollfd pfd; - pfd.fd = listen_sockfd; - pfd.events = POLLIN; - pfd.revents = 0; - - int ret = poll(&pfd, 1, 0); - ERR_FAIL_COND_V(ret < 0, FAILED); - - if (ret && (pfd.revents & POLLIN)) { - return true; - }; - - return false; -}; - -Ref<StreamPeerTCP> TCPServerPosix::take_connection() { - - if (!is_connection_available()) { - return Ref<StreamPeerTCP>(); - }; - - struct sockaddr_storage their_addr; - socklen_t size = sizeof(their_addr); - int fd = accept(listen_sockfd, (struct sockaddr *)&their_addr, &size); - ERR_FAIL_COND_V(fd == -1, Ref<StreamPeerTCP>()); -#ifndef NO_FCNTL - if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0) { - WARN_PRINT("Error setting socket as non blocking"); - } -#else - int bval = 1; - if (ioctl(fd, FIONBIO, &bval) < 0) { - WARN_PRINT("Error setting socket as non blocking"); - } -#endif - - Ref<StreamPeerTCPPosix> conn = memnew(StreamPeerTCPPosix); - IP_Address ip; - - int port = 0; - _set_ip_addr_port(ip, port, &their_addr); - - conn->set_socket(fd, ip, port, sock_type); - - return conn; -}; - -void TCPServerPosix::stop() { - - if (listen_sockfd != -1) { - int ret = close(listen_sockfd); - ERR_FAIL_COND(ret != 0); - }; - - listen_sockfd = -1; - sock_type = IP::TYPE_NONE; -}; - -TCPServerPosix::TCPServerPosix() { - - listen_sockfd = -1; - sock_type = IP::TYPE_NONE; -}; - -TCPServerPosix::~TCPServerPosix() { - - stop(); -}; -#endif diff --git a/drivers/unix/tcp_server_posix.h b/drivers/unix/tcp_server_posix.h deleted file mode 100644 index c749314fb3..0000000000 --- a/drivers/unix/tcp_server_posix.h +++ /dev/null @@ -1,58 +0,0 @@ -/*************************************************************************/ -/* tcp_server_posix.h */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ - -#ifndef TCP_SERVER_POSIX_H -#define TCP_SERVER_POSIX_H - -#ifdef UNIX_ENABLED -#include "core/io/tcp_server.h" - -class TCPServerPosix : public TCP_Server { - - int listen_sockfd; - IP::Type sock_type; - - static TCP_Server *_create(); - -public: - virtual Error listen(uint16_t p_port, const IP_Address &p_bind_address = IP_Address("*")); - virtual bool is_connection_available() const; - virtual Ref<StreamPeerTCP> take_connection(); - - virtual void stop(); - - static void make_default(); - - TCPServerPosix(); - ~TCPServerPosix(); -}; - -#endif // TCP_SERVER_POSIX_H -#endif diff --git a/drivers/unix/thread_posix.cpp b/drivers/unix/thread_posix.cpp index a73b40a6f2..54bbbf2dad 100644 --- a/drivers/unix/thread_posix.cpp +++ b/drivers/unix/thread_posix.cpp @@ -29,7 +29,7 @@ /*************************************************************************/ #include "thread_posix.h" -#include "script_language.h" +#include "core/script_language.h" #if (defined(UNIX_ENABLED) || defined(PTHREAD_ENABLED)) && !defined(NO_THREADS) @@ -37,8 +37,8 @@ #include <pthread_np.h> #endif +#include "core/os/memory.h" #include "core/safe_refcount.h" -#include "os/memory.h" static pthread_key_t _create_thread_id_key() { pthread_key_t key; @@ -103,8 +103,6 @@ void ThreadPosix::wait_to_finish_func_posix(Thread *p_thread) { Error ThreadPosix::set_name_func_posix(const String &p_name) { - pthread_t running_thread = pthread_self(); - #ifdef PTHREAD_NO_RENAME return ERR_UNAVAILABLE; @@ -117,6 +115,7 @@ Error ThreadPosix::set_name_func_posix(const String &p_name) { #else + pthread_t running_thread = pthread_self(); #ifdef PTHREAD_BSD_SET_NAME pthread_set_name_np(running_thread, p_name.utf8().get_data()); int err = 0; // Open/FreeBSD ignore errors in this function diff --git a/drivers/unix/thread_posix.h b/drivers/unix/thread_posix.h index ea2de61bd5..20d103232e 100644 --- a/drivers/unix/thread_posix.h +++ b/drivers/unix/thread_posix.h @@ -37,7 +37,7 @@ #if (defined(UNIX_ENABLED) || defined(PTHREAD_ENABLED)) && !defined(NO_THREADS) -#include "os/thread.h" +#include "core/os/thread.h" #include <pthread.h> #include <sys/types.h> diff --git a/drivers/wasapi/SCsub b/drivers/wasapi/SCsub index 233593b0f9..4c24925192 100644 --- a/drivers/wasapi/SCsub +++ b/drivers/wasapi/SCsub @@ -4,5 +4,3 @@ Import('env') # Driver source files env.add_source_files(env.drivers_sources, "*.cpp") - -Export('env') diff --git a/drivers/wasapi/audio_driver_wasapi.cpp b/drivers/wasapi/audio_driver_wasapi.cpp index a2f619a6ef..8665f701b1 100644 --- a/drivers/wasapi/audio_driver_wasapi.cpp +++ b/drivers/wasapi/audio_driver_wasapi.cpp @@ -32,10 +32,8 @@ #include "audio_driver_wasapi.h" -#include <Functiondiscoverykeys_devpkey.h> - -#include "os/os.h" -#include "project_settings.h" +#include "core/os/os.h" +#include "core/project_settings.h" #include <functiondiscoverykeys.h> @@ -67,7 +65,6 @@ const IID IID_IAudioCaptureClient = __uuidof(IAudioCaptureClient); #define CAPTURE_BUFFER_CHANNELS 2 -static StringName capture_device_id; static bool default_render_device_changed = false; static bool default_capture_device_changed = false; @@ -130,7 +127,6 @@ public: default_render_device_changed = true; } else if (flow == eCapture) { default_capture_device_changed = true; - capture_device_id = String(pwstrDeviceId); } } @@ -323,10 +319,8 @@ Error AudioDriverWASAPI::init_render_device(bool reinit) { input_position = 0; input_size = 0; - if (OS::get_singleton()->is_stdout_verbose()) { - print_line("WASAPI: detected " + itos(channels) + " channels"); - print_line("WASAPI: audio buffer frames: " + itos(buffer_frames) + " calculated latency: " + itos(buffer_frames * 1000 / mix_rate) + "ms"); - } + print_verbose("WASAPI: detected " + itos(channels) + " channels"); + print_verbose("WASAPI: audio buffer frames: " + itos(buffer_frames) + " calculated latency: " + itos(buffer_frames * 1000 / mix_rate) + "ms"); return OK; } @@ -342,10 +336,7 @@ Error AudioDriverWASAPI::init_capture_device(bool reinit) { HRESULT hr = audio_input.audio_client->GetBufferSize(&max_frames); ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN); - // Set the buffer size - input_buffer.resize(max_frames * CAPTURE_BUFFER_CHANNELS); - input_position = 0; - input_size = 0; + input_buffer_init(max_frames); return OK; } @@ -663,6 +654,9 @@ void AudioDriverWASAPI::thread_func(void *p_udata) { if (err == OK) { ad->start(); } + + avail_frames = 0; + write_ofs = 0; } if (ad->audio_input.active) { @@ -799,19 +793,18 @@ Error AudioDriverWASAPI::capture_start() { return err; } - if (audio_input.active == false) { - audio_input.audio_client->Start(); - audio_input.active = true; - - return OK; + if (audio_input.active) { + return FAILED; } - return FAILED; + audio_input.audio_client->Start(); + audio_input.active = true; + return OK; } Error AudioDriverWASAPI::capture_stop() { - if (audio_input.active == true) { + if (audio_input.active) { audio_input.audio_client->Stop(); audio_input.active = false; diff --git a/drivers/wasapi/audio_driver_wasapi.h b/drivers/wasapi/audio_driver_wasapi.h index 3d94f3ba49..71f56cfb5b 100644 --- a/drivers/wasapi/audio_driver_wasapi.h +++ b/drivers/wasapi/audio_driver_wasapi.h @@ -58,17 +58,17 @@ class AudioDriverWASAPI : public AudioDriver { String device_name; String new_device; - AudioDeviceWASAPI() { - audio_client = NULL; - render_client = NULL; - capture_client = NULL; - active = false; - format_tag = 0; - bits_per_sample = 0; - channels = 0; - frame_size = 0; - device_name = "Default"; - new_device = "Default"; + AudioDeviceWASAPI() : + audio_client(NULL), + render_client(NULL), + capture_client(NULL), + active(false), + format_tag(0), + bits_per_sample(0), + channels(0), + frame_size(0), + device_name("Default"), + new_device("Default") { } }; diff --git a/drivers/windows/SCsub b/drivers/windows/SCsub index ee39fd2631..28b315ae66 100644 --- a/drivers/windows/SCsub +++ b/drivers/windows/SCsub @@ -3,5 +3,3 @@ Import('env') env.add_source_files(env.drivers_sources, "*.cpp") - -Export('env') diff --git a/drivers/windows/dir_access_windows.cpp b/drivers/windows/dir_access_windows.cpp index cf4d82fb07..589e9e0870 100644 --- a/drivers/windows/dir_access_windows.cpp +++ b/drivers/windows/dir_access_windows.cpp @@ -32,9 +32,9 @@ #include "dir_access_windows.h" -#include "os/memory.h" +#include "core/os/memory.h" +#include "core/print_string.h" -#include "print_string.h" #include <stdio.h> #include <wchar.h> #include <windows.h> diff --git a/drivers/windows/dir_access_windows.h b/drivers/windows/dir_access_windows.h index b18c8f5b5d..9f5d0b6d93 100644 --- a/drivers/windows/dir_access_windows.h +++ b/drivers/windows/dir_access_windows.h @@ -33,7 +33,7 @@ #ifdef WINDOWS_ENABLED -#include "os/dir_access.h" +#include "core/os/dir_access.h" /** @author Juan Linietsky <reduz@gmail.com> diff --git a/drivers/windows/file_access_windows.cpp b/drivers/windows/file_access_windows.cpp index ea194e5eae..a289945aa7 100644 --- a/drivers/windows/file_access_windows.cpp +++ b/drivers/windows/file_access_windows.cpp @@ -31,11 +31,13 @@ #ifdef WINDOWS_ENABLED #include "file_access_windows.h" -#include "os/os.h" -#include "shlwapi.h" + +#include "core/os/os.h" +#include "core/print_string.h" + +#include <shlwapi.h> #include <windows.h> -#include "print_string.h" #include <sys/stat.h> #include <sys/types.h> #include <tchar.h> @@ -112,7 +114,7 @@ Error FileAccessWindows::_open(const String &p_path, int p_mode_flags) { path = path + ".tmp"; } - f = _wfopen(path.c_str(), mode_string); + _wfopen_s(&f, path.c_str(), mode_string); if (f == NULL) { last_error = ERR_FILE_CANT_OPEN; @@ -133,11 +135,6 @@ void FileAccessWindows::close() { if (save_path != "") { - //unlink(save_path.utf8().get_data()); - //print_line("renaming..."); - //_wunlink(save_path.c_str()); //unlink if exists - //int rename_error = _wrename((save_path+".tmp").c_str(),save_path.c_str()); - bool rename_error = true; int attempts = 4; while (rename_error && attempts) { @@ -281,7 +278,7 @@ bool FileAccessWindows::file_exists(const String &p_name) { FILE *g; //printf("opening file %s\n", p_fname.c_str()); String filename = fix_path(p_name); - g = _wfopen(filename.c_str(), L"rb"); + _wfopen_s(&g, filename.c_str(), L"rb"); if (g == NULL) { return false; @@ -305,17 +302,15 @@ uint64_t FileAccessWindows::_get_modified_time(const String &p_file) { return st.st_mtime; } else { - print_line("no access to " + file); + ERR_EXPLAIN("Failed to get modified time for: " + file); + ERR_FAIL_V(0); } +} - ERR_FAIL_V(0); -}; - -FileAccessWindows::FileAccessWindows() { - - f = NULL; - flags = 0; - last_error = OK; +FileAccessWindows::FileAccessWindows() : + f(NULL), + flags(0), + last_error(OK) { } FileAccessWindows::~FileAccessWindows() { diff --git a/drivers/windows/file_access_windows.h b/drivers/windows/file_access_windows.h index 0462c1e942..6f985e68b4 100644 --- a/drivers/windows/file_access_windows.h +++ b/drivers/windows/file_access_windows.h @@ -33,13 +33,15 @@ #ifdef WINDOWS_ENABLED -#include "os/file_access.h" -#include "os/memory.h" +#include "core/os/file_access.h" +#include "core/os/memory.h" + #include <stdio.h> /** @author Juan Linietsky <reduzio@gmail.com> */ + class FileAccessWindows : public FileAccess { FILE *f; diff --git a/drivers/windows/mutex_windows.cpp b/drivers/windows/mutex_windows.cpp index 359a79209c..9fc6485be3 100644 --- a/drivers/windows/mutex_windows.cpp +++ b/drivers/windows/mutex_windows.cpp @@ -29,7 +29,8 @@ /*************************************************************************/ #include "mutex_windows.h" -#include "os/memory.h" + +#include "core/os/memory.h" #ifdef WINDOWS_ENABLED diff --git a/drivers/windows/mutex_windows.h b/drivers/windows/mutex_windows.h index 4dff2c2456..5c3a8eb331 100644 --- a/drivers/windows/mutex_windows.h +++ b/drivers/windows/mutex_windows.h @@ -33,11 +33,14 @@ #ifdef WINDOWS_ENABLED -#include "os/mutex.h" +#include "core/os/mutex.h" + #include <windows.h> + /** @author Juan Linietsky <reduzio@gmail.com> */ + class MutexWindows : public Mutex { #ifdef WINDOWS_USE_MUTEX diff --git a/drivers/windows/packet_peer_udp_winsock.cpp b/drivers/windows/packet_peer_udp_winsock.cpp deleted file mode 100644 index 609096d02e..0000000000 --- a/drivers/windows/packet_peer_udp_winsock.cpp +++ /dev/null @@ -1,298 +0,0 @@ -/*************************************************************************/ -/* packet_peer_udp_winsock.cpp */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ - -#ifdef WINDOWS_ENABLED - -#include "packet_peer_udp_winsock.h" - -#include <winsock2.h> -#include <ws2tcpip.h> - -#include "drivers/unix/socket_helpers.h" - -int PacketPeerUDPWinsock::get_available_packet_count() const { - - Error err = const_cast<PacketPeerUDPWinsock *>(this)->_poll(false); - if (err != OK) - return 0; - - return queue_count; -} - -Error PacketPeerUDPWinsock::get_packet(const uint8_t **r_buffer, int &r_buffer_size) { - - Error err = const_cast<PacketPeerUDPWinsock *>(this)->_poll(false); - if (err != OK) - return err; - if (queue_count == 0) - return ERR_UNAVAILABLE; - - uint32_t size; - uint8_t type; - rb.read(&type, 1, true); - if (type == IP::TYPE_IPV4) { - uint8_t ip[4]; - rb.read(ip, 4, true); - packet_ip.set_ipv4(ip); - } else { - uint8_t ip[16]; - rb.read(ip, 16, true); - packet_ip.set_ipv6(ip); - }; - rb.read((uint8_t *)&packet_port, 4, true); - rb.read((uint8_t *)&size, 4, true); - rb.read(packet_buffer, size, true); - --queue_count; - *r_buffer = packet_buffer; - r_buffer_size = size; - return OK; -} -Error PacketPeerUDPWinsock::put_packet(const uint8_t *p_buffer, int p_buffer_size) { - - ERR_FAIL_COND_V(!peer_addr.is_valid(), ERR_UNCONFIGURED); - - if (sock_type == IP::TYPE_NONE) - sock_type = peer_addr.is_ipv4() ? IP::TYPE_IPV4 : IP::TYPE_IPV6; - - int sock = _get_socket(); - ERR_FAIL_COND_V(sock == -1, FAILED); - struct sockaddr_storage addr; - size_t addr_size = _set_sockaddr(&addr, peer_addr, peer_port, sock_type); - - _set_sock_blocking(blocking); - - errno = 0; - int err; - while ((err = sendto(sock, (const char *)p_buffer, p_buffer_size, 0, (struct sockaddr *)&addr, addr_size)) != p_buffer_size) { - - if (WSAGetLastError() != WSAEWOULDBLOCK) { - return FAILED; - } else if (!blocking) { - return ERR_UNAVAILABLE; - } - } - - return OK; -} - -int PacketPeerUDPWinsock::get_max_packet_size() const { - - return 512; // uhm maybe not -} - -void PacketPeerUDPWinsock::_set_sock_blocking(bool p_blocking) { - - if (sock_blocking == p_blocking) - return; - - sock_blocking = p_blocking; - unsigned long par = sock_blocking ? 0 : 1; - if (ioctlsocket(sockfd, FIONBIO, &par)) { - perror("setting non-block mode"); - //close(); - //return -1; - }; -} - -Error PacketPeerUDPWinsock::listen(int p_port, const IP_Address &p_bind_address, int p_recv_buffer_size) { - - ERR_FAIL_COND_V(sockfd != -1, ERR_ALREADY_IN_USE); - ERR_FAIL_COND_V(!p_bind_address.is_valid() && !p_bind_address.is_wildcard(), ERR_INVALID_PARAMETER); - - sock_type = IP::TYPE_ANY; - - if (p_bind_address.is_valid()) - sock_type = p_bind_address.is_ipv4() ? IP::TYPE_IPV4 : IP::TYPE_IPV6; - - int sock = _get_socket(); - if (sock == -1) - return ERR_CANT_CREATE; - - struct sockaddr_storage addr = { 0 }; - size_t addr_size = _set_listen_sockaddr(&addr, p_port, sock_type, IP_Address()); - - if (bind(sock, (struct sockaddr *)&addr, addr_size) == -1) { - close(); - return ERR_UNAVAILABLE; - } - - printf("UDP Connection listening on port %i\n", p_port); - rb.resize(nearest_shift(p_recv_buffer_size)); - return OK; -} - -void PacketPeerUDPWinsock::close() { - - if (sockfd != -1) - ::closesocket(sockfd); - sockfd = -1; - sock_type = IP::TYPE_NONE; - rb.resize(16); - queue_count = 0; -} - -Error PacketPeerUDPWinsock::wait() { - - return _poll(true); -} -Error PacketPeerUDPWinsock::_poll(bool p_wait) { - - if (sockfd == -1) { - return FAILED; - } - - _set_sock_blocking(p_wait); - - struct sockaddr_storage from = { 0 }; - int len = sizeof(struct sockaddr_storage); - int ret; - while ((ret = recvfrom(sockfd, (char *)recv_buffer, MIN((int)sizeof(recv_buffer), MAX(rb.space_left() - 24, 0)), 0, (struct sockaddr *)&from, &len)) > 0) { - - uint32_t port = 0; - - if (from.ss_family == AF_INET) { - uint8_t type = (uint8_t)IP::TYPE_IPV4; - rb.write(&type, 1); - struct sockaddr_in *sin_from = (struct sockaddr_in *)&from; - rb.write((uint8_t *)&sin_from->sin_addr, 4); - port = ntohs(sin_from->sin_port); - - } else if (from.ss_family == AF_INET6) { - - uint8_t type = (uint8_t)IP::TYPE_IPV6; - rb.write(&type, 1); - - struct sockaddr_in6 *s6_from = (struct sockaddr_in6 *)&from; - rb.write((uint8_t *)&s6_from->sin6_addr, 16); - - port = ntohs(s6_from->sin6_port); - - } else { - // WARN_PRINT("Ignoring packet with unknown address family"); - uint8_t type = (uint8_t)IP::TYPE_NONE; - rb.write(&type, 1); - }; - - rb.write((uint8_t *)&port, 4); - rb.write((uint8_t *)&ret, 4); - rb.write(recv_buffer, ret); - - len = sizeof(struct sockaddr_storage); - ++queue_count; - if (p_wait) - break; - }; - - if (ret == SOCKET_ERROR) { - int error = WSAGetLastError(); - - if (error == WSAEWOULDBLOCK) { - // Expected when doing non-blocking sockets, retry later. - } else if (error == WSAECONNRESET) { - // If the remote target does not accept messages, this error may occur, but is harmless. - // Once the remote target gets available, this message will disappear for new messages. - } else { - close(); - return FAILED; - } - } - - if (ret == 0) { - close(); - return FAILED; - }; - - return OK; -} - -bool PacketPeerUDPWinsock::is_listening() const { - - return sockfd != -1; -} - -IP_Address PacketPeerUDPWinsock::get_packet_address() const { - - return packet_ip; -} - -int PacketPeerUDPWinsock::get_packet_port() const { - - return packet_port; -} - -int PacketPeerUDPWinsock::_get_socket() { - - ERR_FAIL_COND_V(sock_type == IP::TYPE_NONE, -1); - - if (sockfd != -1) - return sockfd; - - sockfd = _socket_create(sock_type, SOCK_DGRAM, IPPROTO_UDP); - - if (sockfd != -1) - _set_sock_blocking(false); - - return sockfd; -} - -void PacketPeerUDPWinsock::set_dest_address(const IP_Address &p_address, int p_port) { - - peer_addr = p_address; - peer_port = p_port; -} - -void PacketPeerUDPWinsock::make_default() { - - PacketPeerUDP::_create = PacketPeerUDPWinsock::_create; -}; - -PacketPeerUDP *PacketPeerUDPWinsock::_create() { - - return memnew(PacketPeerUDPWinsock); -}; - -PacketPeerUDPWinsock::PacketPeerUDPWinsock() { - - blocking = true; - sock_blocking = true; - sockfd = -1; - packet_port = 0; - queue_count = 0; - peer_port = 0; - sock_type = IP::TYPE_NONE; - rb.resize(16); -} - -PacketPeerUDPWinsock::~PacketPeerUDPWinsock() { - - close(); -} - -#endif diff --git a/drivers/windows/packet_peer_udp_winsock.h b/drivers/windows/packet_peer_udp_winsock.h deleted file mode 100644 index 8d575c2033..0000000000 --- a/drivers/windows/packet_peer_udp_winsock.h +++ /dev/null @@ -1,89 +0,0 @@ -/*************************************************************************/ -/* packet_peer_udp_winsock.h */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ - -#ifdef WINDOWS_ENABLED - -#ifndef PACKET_PEER_UDP_WINSOCK_H -#define PACKET_PEER_UDP_WINSOCK_H - -#include "io/packet_peer_udp.h" -#include "ring_buffer.h" - -class PacketPeerUDPWinsock : public PacketPeerUDP { - - enum { - PACKET_BUFFER_SIZE = 65536 - }; - - RingBuffer<uint8_t> rb; - uint8_t recv_buffer[PACKET_BUFFER_SIZE]; - uint8_t packet_buffer[PACKET_BUFFER_SIZE]; - IP_Address packet_ip; - int packet_port; - int queue_count; - int sockfd; - bool sock_blocking; - IP::Type sock_type; - - IP_Address peer_addr; - int peer_port; - - _FORCE_INLINE_ int _get_socket(); - - static PacketPeerUDP *_create(); - - void _set_sock_blocking(bool p_blocking); - - Error _poll(bool p_wait); - -public: - virtual int get_available_packet_count() const; - virtual Error get_packet(const uint8_t **r_buffer, int &r_buffer_size); - virtual Error put_packet(const uint8_t *p_buffer, int p_buffer_size); - - virtual int get_max_packet_size() const; - - virtual Error listen(int p_port, const IP_Address &p_bind_address = IP_Address("*"), int p_recv_buffer_size = 65536); - virtual void close(); - virtual Error wait(); - virtual bool is_listening() const; - - virtual IP_Address get_packet_address() const; - virtual int get_packet_port() const; - - virtual void set_dest_address(const IP_Address &p_address, int p_port); - - static void make_default(); - PacketPeerUDPWinsock(); - ~PacketPeerUDPWinsock(); -}; -#endif // PACKET_PEER_UDP_WINSOCK_H - -#endif diff --git a/drivers/windows/rw_lock_windows.cpp b/drivers/windows/rw_lock_windows.cpp index 29c24d3d02..ef00141928 100644 --- a/drivers/windows/rw_lock_windows.cpp +++ b/drivers/windows/rw_lock_windows.cpp @@ -32,8 +32,9 @@ #include "rw_lock_windows.h" -#include "error_macros.h" -#include "os/memory.h" +#include "core/error_macros.h" +#include "core/os/memory.h" + #include <stdio.h> void RWLockWindows::read_lock() { diff --git a/drivers/windows/rw_lock_windows.h b/drivers/windows/rw_lock_windows.h index fdce28574a..742a0930d4 100644 --- a/drivers/windows/rw_lock_windows.h +++ b/drivers/windows/rw_lock_windows.h @@ -33,7 +33,8 @@ #if defined(WINDOWS_ENABLED) -#include "os/rw_lock.h" +#include "core/os/rw_lock.h" + #include <windows.h> class RWLockWindows : public RWLock { diff --git a/drivers/windows/semaphore_windows.cpp b/drivers/windows/semaphore_windows.cpp index 25fced93ce..34dd387705 100644 --- a/drivers/windows/semaphore_windows.cpp +++ b/drivers/windows/semaphore_windows.cpp @@ -32,7 +32,7 @@ #if defined(WINDOWS_ENABLED) -#include "os/memory.h" +#include "core/os/memory.h" Error SemaphoreWindows::wait() { diff --git a/drivers/windows/semaphore_windows.h b/drivers/windows/semaphore_windows.h index e099ee437a..1e2f9c152e 100644 --- a/drivers/windows/semaphore_windows.h +++ b/drivers/windows/semaphore_windows.h @@ -31,14 +31,16 @@ #ifndef SEMAPHORE_WINDOWS_H #define SEMAPHORE_WINDOWS_H -#include "os/semaphore.h" +#include "core/os/semaphore.h" #ifdef WINDOWS_ENABLED #include <windows.h> + /** @author Juan Linietsky <reduzio@gmail.com> */ + class SemaphoreWindows : public Semaphore { mutable HANDLE semaphore; diff --git a/drivers/windows/shell_windows.h b/drivers/windows/shell_windows.h index 41cc6b72a2..98972a9bb1 100644 --- a/drivers/windows/shell_windows.h +++ b/drivers/windows/shell_windows.h @@ -31,12 +31,14 @@ #ifndef SHELL_WINDOWS_H #define SHELL_WINDOWS_H -#include "os/shell.h" +#include "core/os/shell.h" #ifdef WINDOWS_ENABLED + /** @author Juan Linietsky <reduzio@gmail.com> */ + class ShellWindows : public Shell { public: virtual void execute(String p_path); diff --git a/drivers/windows/stream_peer_tcp_winsock.cpp b/drivers/windows/stream_peer_tcp_winsock.cpp deleted file mode 100644 index 19c937170b..0000000000 --- a/drivers/windows/stream_peer_tcp_winsock.cpp +++ /dev/null @@ -1,375 +0,0 @@ -/*************************************************************************/ -/* stream_peer_tcp_winsock.cpp */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ - -#ifdef WINDOWS_ENABLED - -#include "stream_peer_tcp_winsock.h" - -#include <winsock2.h> -#include <ws2tcpip.h> - -#include "drivers/unix/socket_helpers.h" - -int winsock_refcount = 0; - -StreamPeerTCP *StreamPeerTCPWinsock::_create() { - - return memnew(StreamPeerTCPWinsock); -}; - -void StreamPeerTCPWinsock::make_default() { - - StreamPeerTCP::_create = StreamPeerTCPWinsock::_create; - - if (winsock_refcount == 0) { - WSADATA data; - WSAStartup(MAKEWORD(2, 2), &data); - }; - ++winsock_refcount; -}; - -void StreamPeerTCPWinsock::cleanup() { - - --winsock_refcount; - if (winsock_refcount == 0) { - - WSACleanup(); - }; -}; - -Error StreamPeerTCPWinsock::_block(int p_sockfd, bool p_read, bool p_write) const { - - fd_set read, write; - FD_ZERO(&read); - FD_ZERO(&write); - - if (p_read) - FD_SET(p_sockfd, &read); - if (p_write) - FD_SET(p_sockfd, &write); - - int ret = select(p_sockfd + 1, &read, &write, NULL, NULL); // block forever - return ret < 0 ? FAILED : OK; -}; - -Error StreamPeerTCPWinsock::_poll_connection() const { - - ERR_FAIL_COND_V(status != STATUS_CONNECTING || sockfd == INVALID_SOCKET, FAILED); - - struct sockaddr_storage their_addr; - size_t addr_size = _set_sockaddr(&their_addr, peer_host, peer_port, sock_type); - - if (::connect(sockfd, (struct sockaddr *)&their_addr, addr_size) == SOCKET_ERROR) { - - int err = WSAGetLastError(); - if (err == WSAEISCONN) { - status = STATUS_CONNECTED; - return OK; - }; - - if (err == WSAEINPROGRESS || err == WSAEALREADY) { - return OK; - } - - status = STATUS_ERROR; - return ERR_CONNECTION_ERROR; - } else { - - status = STATUS_CONNECTED; - return OK; - }; - - return OK; -}; - -Error StreamPeerTCPWinsock::write(const uint8_t *p_data, int p_bytes, int &r_sent, bool p_block) { - - if (status == STATUS_NONE || status == STATUS_ERROR) { - - return FAILED; - }; - - if (status != STATUS_CONNECTED) { - - if (_poll_connection() != OK) { - - return FAILED; - }; - - if (status != STATUS_CONNECTED) { - r_sent = 0; - return OK; - }; - }; - - int data_to_send = p_bytes; - const uint8_t *offset = p_data; - if (sockfd == -1) return FAILED; - int total_sent = 0; - - while (data_to_send) { - - int sent_amount = send(sockfd, (const char *)offset, data_to_send, 0); - - if (sent_amount == -1) { - - if (WSAGetLastError() != WSAEWOULDBLOCK) { - - perror("Nothing sent"); - disconnect_from_host(); - ERR_PRINT("Server disconnected!\n"); - return FAILED; - }; - - if (!p_block) { - r_sent = total_sent; - return OK; - }; - - _block(sockfd, false, true); - } else { - - data_to_send -= sent_amount; - offset += sent_amount; - total_sent += sent_amount; - }; - } - - r_sent = total_sent; - - return OK; -}; - -Error StreamPeerTCPWinsock::read(uint8_t *p_buffer, int p_bytes, int &r_received, bool p_block) { - - if (!is_connected_to_host()) { - - return FAILED; - }; - - if (status != STATUS_CONNECTED) { - - if (_poll_connection() != OK) { - - return FAILED; - }; - - if (status != STATUS_CONNECTED) { - r_received = 0; - return OK; - }; - }; - - int to_read = p_bytes; - int total_read = 0; - - while (to_read) { - - int read = recv(sockfd, (char *)p_buffer + total_read, to_read, 0); - - if (read == -1) { - - if (WSAGetLastError() != WSAEWOULDBLOCK) { - - perror("Nothing read"); - disconnect_from_host(); - ERR_PRINT("Server disconnected!\n"); - return FAILED; - }; - - if (!p_block) { - - r_received = total_read; - return OK; - }; - _block(sockfd, true, false); - } else if (read == 0) { - disconnect_from_host(); - r_received = total_read; - return ERR_FILE_EOF; - } else { - - to_read -= read; - total_read += read; - }; - }; - - r_received = total_read; - - return OK; -}; - -Error StreamPeerTCPWinsock::put_data(const uint8_t *p_data, int p_bytes) { - - int total; - return write(p_data, p_bytes, total, true); -}; - -Error StreamPeerTCPWinsock::put_partial_data(const uint8_t *p_data, int p_bytes, int &r_sent) { - - return write(p_data, p_bytes, r_sent, false); -}; - -Error StreamPeerTCPWinsock::get_data(uint8_t *p_buffer, int p_bytes) { - - int total; - return read(p_buffer, p_bytes, total, true); -}; - -Error StreamPeerTCPWinsock::get_partial_data(uint8_t *p_buffer, int p_bytes, int &r_received) { - - return read(p_buffer, p_bytes, r_received, false); -}; - -StreamPeerTCP::Status StreamPeerTCPWinsock::get_status() const { - - if (status == STATUS_CONNECTING) { - _poll_connection(); - }; - - return status; -}; - -bool StreamPeerTCPWinsock::is_connected_to_host() const { - - if (status == STATUS_NONE || status == STATUS_ERROR) { - - return false; - }; - if (status != STATUS_CONNECTED) { - return true; - }; - - return (sockfd != INVALID_SOCKET); -}; - -void StreamPeerTCPWinsock::disconnect_from_host() { - - if (sockfd != INVALID_SOCKET) - closesocket(sockfd); - sockfd = INVALID_SOCKET; - sock_type = IP::TYPE_NONE; - - status = STATUS_NONE; - - peer_host = IP_Address(); - peer_port = 0; -}; - -void StreamPeerTCPWinsock::set_socket(int p_sockfd, IP_Address p_host, int p_port, IP::Type p_sock_type) { - - sockfd = p_sockfd; - sock_type = p_sock_type; - status = STATUS_CONNECTING; - peer_host = p_host; - peer_port = p_port; -}; - -Error StreamPeerTCPWinsock::connect_to_host(const IP_Address &p_host, uint16_t p_port) { - - ERR_FAIL_COND_V(!p_host.is_valid(), ERR_INVALID_PARAMETER); - - sock_type = p_host.is_ipv4() ? IP::TYPE_IPV4 : IP::TYPE_IPV6; - sockfd = _socket_create(sock_type, SOCK_STREAM, IPPROTO_TCP); - if (sockfd == INVALID_SOCKET) { - ERR_PRINT("Socket creation failed!"); - disconnect_from_host(); - //perror("socket"); - return FAILED; - }; - - unsigned long par = 1; - if (ioctlsocket(sockfd, FIONBIO, &par)) { - perror("setting non-block mode"); - disconnect_from_host(); - return FAILED; - }; - - struct sockaddr_storage their_addr; - size_t addr_size = _set_sockaddr(&their_addr, p_host, p_port, sock_type); - - if (::connect(sockfd, (struct sockaddr *)&their_addr, addr_size) == SOCKET_ERROR) { - - if (WSAGetLastError() != WSAEWOULDBLOCK) { - ERR_PRINT("Connection to remote host failed!"); - disconnect_from_host(); - return FAILED; - }; - status = STATUS_CONNECTING; - } else { - status = STATUS_CONNECTED; - }; - - peer_host = p_host; - peer_port = p_port; - - return OK; -}; - -void StreamPeerTCPWinsock::set_no_delay(bool p_enabled) { - ERR_FAIL_COND(!is_connected_to_host()); - int flag = p_enabled ? 1 : 0; - if (setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, (char *)&flag, sizeof(int)) != 0) { - ERR_PRINT("Unable to set TCP no delay option"); - } -} - -int StreamPeerTCPWinsock::get_available_bytes() const { - - unsigned long len; - int ret = ioctlsocket(sockfd, FIONREAD, &len); - ERR_FAIL_COND_V(ret == -1, 0) - return len; -} - -IP_Address StreamPeerTCPWinsock::get_connected_host() const { - - return peer_host; -}; - -uint16_t StreamPeerTCPWinsock::get_connected_port() const { - - return peer_port; -}; - -StreamPeerTCPWinsock::StreamPeerTCPWinsock() { - - sock_type = IP::TYPE_NONE; - sockfd = INVALID_SOCKET; - status = STATUS_NONE; - peer_port = 0; -}; - -StreamPeerTCPWinsock::~StreamPeerTCPWinsock() { - - disconnect_from_host(); -}; - -#endif diff --git a/drivers/windows/tcp_server_winsock.cpp b/drivers/windows/tcp_server_winsock.cpp deleted file mode 100644 index ddb955549f..0000000000 --- a/drivers/windows/tcp_server_winsock.cpp +++ /dev/null @@ -1,189 +0,0 @@ -/*************************************************************************/ -/* tcp_server_winsock.cpp */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ - -#ifdef WINDOWS_ENABLED - -#include "tcp_server_winsock.h" - -#include "stream_peer_tcp_winsock.h" - -#include <winsock2.h> -#include <ws2tcpip.h> - -#include "drivers/unix/socket_helpers.h" - -extern int winsock_refcount; - -TCP_Server *TCPServerWinsock::_create() { - - return memnew(TCPServerWinsock); -}; - -void TCPServerWinsock::make_default() { - - TCP_Server::_create = TCPServerWinsock::_create; - - if (winsock_refcount == 0) { - WSADATA data; - WSAStartup(MAKEWORD(2, 2), &data); - }; - ++winsock_refcount; -}; - -void TCPServerWinsock::cleanup() { - - --winsock_refcount; - if (winsock_refcount == 0) { - - WSACleanup(); - }; -}; - -Error TCPServerWinsock::listen(uint16_t p_port, const IP_Address &p_bind_address) { - - ERR_FAIL_COND_V(listen_sockfd != -1, ERR_ALREADY_IN_USE); - ERR_FAIL_COND_V(!p_bind_address.is_valid() && !p_bind_address.is_wildcard(), ERR_INVALID_PARAMETER); - - int sockfd; - sock_type = IP::TYPE_ANY; - - // If the bind address is valid use its type as the socket type - if (p_bind_address.is_valid()) - sock_type = p_bind_address.is_ipv4() ? IP::TYPE_IPV4 : IP::TYPE_IPV6; - - sockfd = _socket_create(sock_type, SOCK_STREAM, IPPROTO_TCP); - ERR_FAIL_COND_V(sockfd == INVALID_SOCKET, FAILED); - - unsigned long par = 1; - if (ioctlsocket(sockfd, FIONBIO, &par)) { - perror("setting non-block mode"); - stop(); - return FAILED; - }; - - struct sockaddr_storage my_addr; - size_t addr_size = _set_listen_sockaddr(&my_addr, p_port, sock_type, p_bind_address); - - int reuse = 1; - if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (char *)&reuse, sizeof(reuse)) < 0) { - - printf("REUSEADDR failed!"); - } - - if (bind(sockfd, (struct sockaddr *)&my_addr, addr_size) != SOCKET_ERROR) { - - if (::listen(sockfd, SOMAXCONN) == SOCKET_ERROR) { - - closesocket(sockfd); - ERR_FAIL_V(FAILED); - }; - } else { - closesocket(sockfd); - return ERR_ALREADY_IN_USE; - }; - - if (listen_sockfd != INVALID_SOCKET) { - - stop(); - }; - - listen_sockfd = sockfd; - - return OK; -}; - -bool TCPServerWinsock::is_connection_available() const { - - if (listen_sockfd == -1) { - return false; - }; - - timeval timeout; - timeout.tv_sec = 0; - timeout.tv_usec = 0; - - fd_set pfd; - FD_ZERO(&pfd); - FD_SET(listen_sockfd, &pfd); - - int ret = select(listen_sockfd + 1, &pfd, NULL, NULL, &timeout); - ERR_FAIL_COND_V(ret < 0, 0); - - if (ret && (FD_ISSET(listen_sockfd, &pfd))) { - - return true; - }; - - return false; -}; - -Ref<StreamPeerTCP> TCPServerWinsock::take_connection() { - - if (!is_connection_available()) { - return NULL; - }; - - struct sockaddr_storage their_addr; - int sin_size = sizeof(their_addr); - int fd = accept(listen_sockfd, (struct sockaddr *)&their_addr, &sin_size); - ERR_FAIL_COND_V(fd == INVALID_SOCKET, NULL); - - Ref<StreamPeerTCPWinsock> conn = memnew(StreamPeerTCPWinsock); - IP_Address ip; - int port; - _set_ip_addr_port(ip, port, &their_addr); - - conn->set_socket(fd, ip, port, sock_type); - - return conn; -}; - -void TCPServerWinsock::stop() { - - if (listen_sockfd != INVALID_SOCKET) { - closesocket(listen_sockfd); - }; - - listen_sockfd = -1; - sock_type = IP::TYPE_NONE; -}; - -TCPServerWinsock::TCPServerWinsock() { - - listen_sockfd = INVALID_SOCKET; - sock_type = IP::TYPE_NONE; -}; - -TCPServerWinsock::~TCPServerWinsock() { - - stop(); -}; - -#endif diff --git a/drivers/windows/tcp_server_winsock.h b/drivers/windows/tcp_server_winsock.h deleted file mode 100644 index a6979db42d..0000000000 --- a/drivers/windows/tcp_server_winsock.h +++ /dev/null @@ -1,61 +0,0 @@ -/*************************************************************************/ -/* tcp_server_winsock.h */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ - -#ifdef WINDOWS_ENABLED - -#ifndef TCP_SERVER_WINSOCK_H -#define TCP_SERVER_WINSOCK_H - -#include "core/io/tcp_server.h" - -class TCPServerWinsock : public TCP_Server { - - int listen_sockfd; - IP::Type sock_type; - - static TCP_Server *_create(); - -public: - virtual Error listen(uint16_t p_port, const IP_Address &p_bind_address = IP_Address("*")); - virtual bool is_connection_available() const; - virtual Ref<StreamPeerTCP> take_connection(); - - virtual void stop(); //stop listening - - static void make_default(); - static void cleanup(); - - TCPServerWinsock(); - ~TCPServerWinsock(); -}; - -#endif - -#endif diff --git a/drivers/windows/thread_windows.cpp b/drivers/windows/thread_windows.cpp index 5e0b017a5c..dbccad3b5d 100644 --- a/drivers/windows/thread_windows.cpp +++ b/drivers/windows/thread_windows.cpp @@ -32,7 +32,7 @@ #if defined(WINDOWS_ENABLED) && !defined(UWP_ENABLED) -#include "os/memory.h" +#include "core/os/memory.h" Thread::ID ThreadWindows::get_id() const { @@ -93,9 +93,8 @@ void ThreadWindows::make_default() { wait_to_finish_func = wait_to_finish_func_windows; } -ThreadWindows::ThreadWindows() { - - handle = NULL; +ThreadWindows::ThreadWindows() : + handle(NULL) { } ThreadWindows::~ThreadWindows() { diff --git a/drivers/windows/thread_windows.h b/drivers/windows/thread_windows.h index d7a8389d9e..5d2838e54f 100644 --- a/drivers/windows/thread_windows.h +++ b/drivers/windows/thread_windows.h @@ -31,16 +31,17 @@ #ifndef THREAD_WINDOWS_H #define THREAD_WINDOWS_H -/** - @author Juan Linietsky <reduzio@gmail.com> -*/ - #ifdef WINDOWS_ENABLED -#include "os/thread.h" -#include "script_language.h" +#include "core/os/thread.h" +#include "core/script_language.h" + #include <windows.h> +/** + @author Juan Linietsky <reduzio@gmail.com> +*/ + class ThreadWindows : public Thread { ThreadCreateCallback callback; diff --git a/drivers/winmidi/SCsub b/drivers/winmidi/SCsub index 233593b0f9..4c24925192 100644 --- a/drivers/winmidi/SCsub +++ b/drivers/winmidi/SCsub @@ -4,5 +4,3 @@ Import('env') # Driver source files env.add_source_files(env.drivers_sources, "*.cpp") - -Export('env') diff --git a/drivers/winmidi/win_midi.cpp b/drivers/winmidi/win_midi.cpp index 6da6e31b2b..1d4bf1a1e2 100644 --- a/drivers/winmidi/win_midi.cpp +++ b/drivers/winmidi/win_midi.cpp @@ -31,7 +31,8 @@ #ifdef WINMIDI_ENABLED #include "win_midi.h" -#include "print_string.h" + +#include "core/print_string.h" void MIDIDriverWinMidi::read(HMIDIIN hMidiIn, UINT wMsg, DWORD_PTR dwInstance, DWORD_PTR dwParam1, DWORD_PTR dwParam2) { @@ -53,6 +54,12 @@ Error MIDIDriverWinMidi::open() { char err[256]; midiInGetErrorText(res, err, 256); ERR_PRINTS("midiInOpen error: " + String(err)); + + MIDIINCAPS caps; + res = midiInGetDevCaps(i, &caps, sizeof(MIDIINCAPS)); + if (res == MMSYSERR_NOERROR) { + ERR_PRINTS("Can't open MIDI device \"" + String(caps.szPname) + "\", is it being used by another application?"); + } } } diff --git a/drivers/winmidi/win_midi.h b/drivers/winmidi/win_midi.h index 1cf9b19b5d..87a349d5d1 100644 --- a/drivers/winmidi/win_midi.h +++ b/drivers/winmidi/win_midi.h @@ -33,14 +33,14 @@ #ifndef WIN_MIDI_H #define WIN_MIDI_H +#include "core/os/midi_driver.h" +#include "core/vector.h" + #include <stdio.h> #include <windows.h> #include <mmsystem.h> -#include "core/vector.h" -#include "os/midi_driver.h" - class MIDIDriverWinMidi : public MIDIDriver { Vector<HMIDIIN> connected_sources; diff --git a/drivers/xaudio2/SCsub b/drivers/xaudio2/SCsub index cb780a893b..3dca95b429 100644 --- a/drivers/xaudio2/SCsub +++ b/drivers/xaudio2/SCsub @@ -5,5 +5,3 @@ Import('env') env.add_source_files(env.drivers_sources, "*.cpp") env.Append(CXXFLAGS=['-DXAUDIO2_ENABLED']) env.Append(LINKFLAGS=['xaudio2_8.lib']) - -Export('env') diff --git a/drivers/xaudio2/audio_driver_xaudio2.cpp b/drivers/xaudio2/audio_driver_xaudio2.cpp index a1002ef4f9..ce2f0db0fc 100644 --- a/drivers/xaudio2/audio_driver_xaudio2.cpp +++ b/drivers/xaudio2/audio_driver_xaudio2.cpp @@ -30,8 +30,8 @@ #include "audio_driver_xaudio2.h" -#include "os/os.h" -#include "project_settings.h" +#include "core/os/os.h" +#include "core/project_settings.h" const char *AudioDriverXAudio2::get_name() const { return "XAudio2"; @@ -91,7 +91,7 @@ Error AudioDriverXAudio2::init() { thread = Thread::create(AudioDriverXAudio2::thread_func, this); return OK; -}; +} void AudioDriverXAudio2::thread_func(void *p_udata) { @@ -131,10 +131,10 @@ void AudioDriverXAudio2::thread_func(void *p_udata) { WaitForSingleObject(ad->voice_callback.buffer_end_event, INFINITE); } } - }; + } ad->thread_exited = true; -}; +} void AudioDriverXAudio2::start() { @@ -144,17 +144,17 @@ void AudioDriverXAudio2::start() { ERR_EXPLAIN("XAudio2 start error " + itos(hr)); ERR_FAIL(); } -}; +} int AudioDriverXAudio2::get_mix_rate() const { return mix_rate; -}; +} AudioDriver::SpeakerMode AudioDriverXAudio2::get_speaker_mode() const { return speaker_mode; -}; +} float AudioDriverXAudio2::get_latency() { @@ -172,13 +172,13 @@ void AudioDriverXAudio2::lock() { if (!thread || !mutex) return; mutex->lock(); -}; +} void AudioDriverXAudio2::unlock() { if (!thread || !mutex) return; mutex->unlock(); -}; +} void AudioDriverXAudio2::finish() { @@ -195,12 +195,12 @@ void AudioDriverXAudio2::finish() { if (samples_in) { memdelete_arr(samples_in); - }; + } if (samples_out[0]) { for (int i = 0; i < AUDIO_BUFFERS; i++) { memdelete_arr(samples_out[i]); } - }; + } mastering_voice->DestroyVoice(); @@ -208,20 +208,18 @@ void AudioDriverXAudio2::finish() { if (mutex) memdelete(mutex); thread = NULL; -}; - -AudioDriverXAudio2::AudioDriverXAudio2() { +} - mutex = NULL; - thread = NULL; +AudioDriverXAudio2::AudioDriverXAudio2() : + thread(NULL), + mutex(NULL), + current_buffer(0) { wave_format = { 0 }; for (int i = 0; i < AUDIO_BUFFERS; i++) { xaudio_buffer[i] = { 0 }; samples_out[i] = 0; } - current_buffer = 0; -}; - -AudioDriverXAudio2::~AudioDriverXAudio2(){ +} -}; +AudioDriverXAudio2::~AudioDriverXAudio2() { +} diff --git a/drivers/xaudio2/audio_driver_xaudio2.h b/drivers/xaudio2/audio_driver_xaudio2.h index 42e1adb2b7..0867c56128 100644 --- a/drivers/xaudio2/audio_driver_xaudio2.h +++ b/drivers/xaudio2/audio_driver_xaudio2.h @@ -51,7 +51,7 @@ class AudioDriverXAudio2 : public AudioDriver { HANDLE buffer_end_event; XAudio2DriverVoiceCallback() : buffer_end_event(CreateEvent(NULL, FALSE, FALSE, NULL)) {} - void STDMETHODCALLTYPE OnBufferEnd(void *pBufferContext) { /*print_line("buffer ended");*/ + void STDMETHODCALLTYPE OnBufferEnd(void *pBufferContext) { SetEvent(buffer_end_event); } diff --git a/drivers/zlib/SCsub b/drivers/zlib/SCsub deleted file mode 100644 index 407deb5f6e..0000000000 --- a/drivers/zlib/SCsub +++ /dev/null @@ -1,26 +0,0 @@ -#!/usr/bin/env python - -Import('env') - -# Not cloning the env, the includes need to be accessible for core/ - -# Thirdparty source files -# No check here as already done in drivers/SCsub -thirdparty_dir = "#thirdparty/zlib/" -thirdparty_sources = [ - "adler32.c", - "compress.c", - "crc32.c", - "deflate.c", - "infback.c", - "inffast.c", - "inflate.c", - "inftrees.c", - "trees.c", - "uncompr.c", - "zutil.c", -] -thirdparty_sources = [thirdparty_dir + file for file in thirdparty_sources] - -env.add_source_files(env.drivers_sources, thirdparty_sources) -env.Append(CPPPATH=[thirdparty_dir]) |